-
Notifications
You must be signed in to change notification settings - Fork 1
/
explorer.s
2094 lines (1923 loc) · 65 KB
/
explorer.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; V I C C A V E R N E X P L O R E R
;
; for the unexpanded Commodore VIC-20
;
; by Davide Bucci, August-September 2018
;
; Assembler: ca65
; General-use addresses
GRCHARS1 = $1C00 ; Address of user-defined characters. Since in the
; unexpanded VIC the screen matrix starts at
; $1E00, there are 512 bytes free, i.e. 64 chars
; that can be defined. That leaves 3059 bytes free
; for the machine language code (counting the
; 752 SYS4109 stub in BASIC that launches the
; program. (3073 bytes?)
; Colour constants for the VIC 20
BLACK = $00
WHITE = $01
RED = $02
CYAN = $03
MAGENTA = $04
GREEN = $05
BLUE = $06
YELLOW = $07
MULTICOLOUR = $08
; KERNAL routines used
GETIN = $FFE4
; Page-0 addresses used (for indirect indexed addressing and other things)
Val = $0 ; Used for the BCD conversion (word)
Res = $2 ; The result of the BCD conversion (3 bytes)
tmpindex1 = $5 ; Temporary variables (byte) in DrawCavern
str1 = $9 ; Address of the string to print with PrintStr (w.)
tmp4 = $B ; Temporary (b.)
ColourRead= $C ; Colour read by GetChar (b.)
POSCHARPT = $D ; Pointer for a character in memory (w.)
POSCOLPT = $F ; Pointer for a colour in memory (w.)
SPRITECH = $11 ; Pointer to the group of 4 ch. for a sprite (word)
CHRPTR = $13 ; Pointer to the original ch. in a sprite (word)
SpriteX = $15 ; X position (offset in a char) of a sprite (byte)
SpriteY = $16 ; Y position (offset in a char) of a sprite (byte)
CharShr = $17 ; Employed in LoadSprite (b.)
CharCode = $18 ; Employed in DrawChar (b.)
PosX = $19 ; Positioning of the current char X (b.)
PosY = $1A ; Positioning of the current char Y (b.)
Colour = $1B ; Colour to be used by the printing routines (b.)
CurrentCode=$1C ; Code being processed for drawing walls (b.)
Pos = $1D ; Used by PrintStr
CavernPTR = $1E ; Pointer to the cavern data (w.)
TempOr = $20 ; Temp. for PrepareCopy (b.)
CurrentYPos=$21 ; Position in the cavern (b.)
ShipYSpeed= $22 ; Vertical speed of ship (relative to scroll, b.)
IrqCn = $23 ; Counter for the IRQ
Period = $24 ; Period for the IRQ events
CavernPosX= $25 ; While drawing cavern, the hor. shift in chrs (b.)
VertPosPx = $26 ; Vertical shift position in pixels of the ship (b.)
ShipPosX = $27 ; X position of the ship in the game in pixels (b.)
ShipPosY = $28 ; Y position of the ship in the game in pixels (b.)
Win = $29 ; Specify if the game should be stopped (b.)
VoiceBase = $2A ; = 128 music on, = 0 music off (b.)
keyin = $2B ; Last key typed.
ShipChrX = $2C ; X position of the ship in characters (b.)
ShipChrY = $2D ; Y position of the ship in characters (b.)
ShipXSpeed= $2E ; Horisontal speed of the ship (b.)
CurrXPosL = $2F ; Current shift (in ch.) of the left wall (b.)
CurrXPosR = $30 ; Current shift (in ch.) of the right wall (b.)
CurrCode = $31 ; Current code while drawing the cavern (b.)
SFXCounter= $32 ; Sound Effects counter (b.)
Direction = $33 ; =0 if right border =128 if left border (b. cavern)
SOURCE = $34 ; Source pointer for CopyMem (w.)
DEST = $36 ; Destination pointer for CopyMem (w.)
BLENDCHA = $38 ; Saved ch. 1 to be blended with sprite 1 (b.)
BLENDCHB = $39 ; Saved ch. 2 to be blended with sprite 1 (b.)
BLENDCHC = $3A ; Saved ch. 3 to be blended with sprite 1 (b.)
BLENDCHD = $3B ; Saved ch. 4 to be blended with sprite 1 (b.)
PlantShoot= $3C ; Position of the plant shoot (b.)
PlantDir = $3D ; Direction of the plant shoot (=0 or =128) (b.)
TmpChar = $3E ; Temporary for DrawCharFast (b.)
Score = $3F ; Current score (w.)
HiScore = $41 ; High score (w.)
CanIncLev = $42 ; =0 if the level can be incremented =128 otherwise
Level = $43 ; Current level number (b.)
UpdateWPos= $44 ; =0 if the wall position has to be shifted (b.)
IRQfreec = $45 ; Free running counter sync with IRQ (b.)
TmpPos = $46 ; Temporary for PosChar (b.)
PlantPos = $47 ; Temporary for PutPlant1/2 (b.)
GremlinX = $48 ; X position of the gremlin (b.)
GremlinY = $49 ; Y position of the gremlin (b.)
; Video settings (those things that depend between NTSC/PAL
Vpos = $50 ; Vertical position of the screen (top, b.)
Vposm15 = $51 ; Same as Vpos but minus 15 :-) (b.)
Vposm16 = $52 ; Vposm15 minus one (b.)
RasterlNMI= $53 ; Raster line for the NMI interrupt (b.)
NormSCRpos= $54 ; Centered position of the screen (b.)
MaxShipPos= $55 ; Maximum vertical position of the ship (b.)
CavernSize= $56 ; Number of lines of the cavern (b.)
IsNTSC = $57 ; =128 if NTSC, =0 if PAL (b.)
INITVALC=$ede4
; VIC-chip addresses
VICSCRHO = $9000 ; Horizontal position of the screen
VICSCRVE = $9001 ; Vertical position of the screen
VICCOLNC = $9002 ; Screen width in columns and video memory addr.
VICROWNC = $9003 ; Screen height, 8x8 or 8x16 chars, scan line addr.
VICRAST = $9004 ; Bits 8-1 of the current raster line
VICCHGEN = $9005 ; Character gen. and video matrix addresses.
GEN1 = $900A ; First sound generator
GEN2 = $900B ; Second sound generator
GEN3 = $900C ; Third sound generator
NOISE = $900D ; Noise sound generator
VOLUME = $900E ; Volume and additional colour info
VICCOLOR = $900F ; Screen and border colours
; VIA addresses
PORTAVIA1 = $9111 ; Port A 6522 (joystick)
ACRVIA1 = $911B
PORTAVIA1d = $9113 ; Port A 6522 (joystick)
T1CLVIA1 = $9114
T1CHVIA1 = $9115
T1LLVIA1 = $9116
T1LHVIA1 = $9117
IFRVIA1 = $911D ; Interrupt flags
IERVIA1 = $911E ; Interrupt enable
PORTBVIA2 = $9120 ; Port B 6522 2 value (joystick)
PORTBVIA2d = $9122 ; Port B 6522 2 direction (joystick
T1CLVIA2 = $9124
T1CHVIA2 = $9125
T1LLVIA2 = $9126
T1LHVIA2 = $9127
ACRVIA2 = $912B
IFRVIA2 = $912D ; Interrupt flags
IERVIA2 = $912E ; Interrupt enable
MEMSCR = $1E00 ; Start address of the screen memory (unexp. VIC)
MEMCLR = $9600 ; Start address of the colour memory (unexp. VIC)
REPEATKE = $028A ; Repeat all keys
VOICE1 = GEN1 ; Voice 1 for music
VOICE2 = GEN2 ; Voice 2 for music
EFFECTS = GEN3 ; Sound effects (not noise)
.export main
.segment "STARTUP"
.segment "LOWCODE"
.segment "INIT"
.segment "GRCHARS"
.segment "CODE"
; Main program here.
main: jsr Init ; Init the game (load graphic chars, etc...)
restart: jsr StartGame
mainloop: lda #251
sta IRQfreec
@wait: lda IRQfreec
bmi @wait
jsr UpdateSpeedX
jsr UpdateSpeedY
jsr ValidatePos
jsr GETIN ; Main loop waiting for keyboard/joystick events
sta keyin
jsr CheckJoystick
lda keyin
beq mainloop
cmp #$0D ; Wait for return if the game stopped
bne @norestart
lda Win ; If the game has stopped, restart
bne restart
@norestart: lda keyin
cmp #$58 ; X: right
beq right
cmp #$5A ; Z: left
beq left
cmp #$53 ; S: up
beq up
cmp #$20 ; Space: fire!
beq fire
cmp #$4D ; M toggle music on/off
bne mainloop
lda VoiceBase
eor #$80
sta VoiceBase
@continue4: jmp mainloop
up: dec ShipPosY
dec ShipPosY
dec ShipYSpeed
dec ShipYSpeed
jmp mainloop
right: inc ShipPosX
inc ShipXSpeed
inc ShipXSpeed
jmp mainloop
left: dec ShipPosX
dec ShipXSpeed
dec ShipXSpeed
jmp mainloop
fire: lda Win ; If the game has stopped, restart
bne restart
jmp mainloop
UpdateSpeedX:
lda ShipXSpeed
beq @nobrake
bmi @negative
dec ShipXSpeed ; Reduce the speed
jmp @nobrake
@negative: inc ShipXSpeed
@nobrake: lda ShipPosX
clc
adc ShipXSpeed
sta ShipPosX
rts
UpdateSpeedY:
lda ShipYSpeed
beq @nobrake
bmi @negative
dec ShipYSpeed ; Reduce the speed
jmp @nobrake
@negative: inc ShipYSpeed
@nobrake: lda ShipPosY
clc
adc ShipYSpeed
sta ShipPosY
@nospeed: rts
ValidatePos:
lda ShipPosY
cmp #60
bpl @ok
lda #60
sta ShipPosY
lda #0
sta ShipYSpeed
@ok: rts
CheckJoystick:
lda PORTAVIA1
and #%00010000 ; Left
beq left
lda PORTBVIA2
and #%10000000 ; Right
beq right
lda PORTAVIA1
and #%00100000 ; Fire
beq fire
lda PORTAVIA1
and #%00000100 ; Up
beq up
rts
StartGame: clc
lda #$2F ; Turn on the volume, set multicolour add. colour 2
sta VOLUME
lda #4
sta Period
lda #$08
sta VICCOLOR
lda #$0
sta CurrentYPos
sta ShipXSpeed
sta ShipYSpeed
sta VertPosPx
sta PlantShoot
sta PlantDir
sta Win
sta Score
sta Score+1
sta CanIncLev
sta Level
sta IRQfreec
sta IrqCn
lda #$80
sta UpdateWPos
sta GremlinX
jsr CLSA
ldx #4
stx CurrXPosL
ldx #11
stx CurrXPosR
lda #64
sta ShipPosX
sta ShipPosY
lda Vpos ; Put the screen again in the bottom position
sta VICSCRVE
jsr PutRings
jmp ChangeRingState
; Draw a cavern wall in the position specified in PosX, PosY
.macro OneLine
tya
adc #16
tay
.endmacro
DrawCavern:
ldy CurrentYPos
ldx CavernPosX ; Read the current position of the wall
dex
dex
bit UpdateWPos ; Skip the adjust of the position?
bmi loop
lda (CavernPTR),y ; Read the first step and adjust the position
and #S_MASK ; for the next run
cmp #S_RIGH
bne @nn2
inc CavernPosX ; Increment the x pos if there is S_RIGHT
jmp loop
@nn2: cmp #S_LEFT
bne loop
dec CavernPosX ; Decrement the x pos if there is S_LEFT
loop: sty tmpindex1 ; Start of the main loop
lda (CavernPTR),y ; Read the current step
sta CurrCode
ldy PosY ; Get the current vertical position
jsr PosChar
ldy #2 ; Starting at 2 allows to avoid negative values
lda CurrCode
clv ; Clear the oVerflow bit (to use bvc as "bra")
; clc ; The carry is clear after PosChar
and #S_MASK
@no1: cmp #S_RIGH ; Code 01: StepRight+StepStay
bcc @no0 ; If it is less than 01, it must be 00
bne @no2
@goright: jsr StepRight
OneLine
jsr StepStay
bvc @cont ; Branch always!
@no0: jsr StepLeft ; Code 00: StepLeft + StepRight
OneLine
jsr StepRight
bvc @cont ; Branch always!
@no2: cmp #S_LEFT ; Code 10: StepLeft+StepStay
bne @no3
@goleft: jsr StepLeft
OneLine
jsr StepStay
bvc @cont ; Branch always!
@no3: jsr StepRight ; Code 11: StepRight + StepLeft
OneLine
jsr StepLeft
@cont: lda CurrCode
and #%11111100 ; Shortcut!
beq NoRings
lda CurrCode ; Handle the current decorations
and #D_MASK ; (rocks and skeletons in the walls)
beq NoDecoration
cmp #D_ROCK1
bne @nd1
jmp PutRock1
@nd1: cmp #D_ROCK2
bne @sk
jmp PutRock2
@sk: jmp PutSkeleton
NoDecoration: ; End of the decoration code
lda CurrCode ; Handle plants, surface decorations, gremlins
and #P_MASK
beq NoSurface
cmp #P_PLANT1
bne @nd1
jmp PutPlant1
@nd1: cmp #P_PLANT2
bne @nd3
jmp PutPlant2
@nd3: lda #7 ; Put a gremlin here
sta GremlinX
lda #31
sta GremlinY
NoSurface: lda CurrCode ; Handle rings and levels
and #L_RING
beq NoRings
jmp PutRing
NoRings: lda CurrCode
and #L_LEVEL
beq NoLevel
jmp IncrementLevel
NoLevel:
lda PosY
clc ; The carry should be always clear here
adc #4 ; Every step is 4 lines
sta PosY
cmp CavernSize ; Check if we got to the end of the screen
bcs exitloop
ldy tmpindex1
iny
jmp loop
exitloop:
rts
; The adc operation is done in StepStay, StepRight and StepLeft. However, it
; is expected that there is NEVER an overflow condition.
StepStay: lda #WALL0A
sta (POSCHARPT),Y
lda Colour
sta (POSCOLPT),Y
OneLine
lda #WALL0B
sta (POSCHARPT),Y
lda Colour
sta (POSCOLPT),Y
rts
StepRight: lda #WALL1A
sta (POSCHARPT),Y
lda Colour
sta (POSCOLPT),Y
tya
clc
adc #17
tay
lda #WALL1B
sta (POSCHARPT),Y
lda Colour
sta (POSCOLPT),Y
inx
rts
StepLeft: lda #WALL2A
sta (POSCHARPT),Y
lda Colour
sta (POSCOLPT),Y
tya
clc
adc #15
tay
lda #WALL2B
sta (POSCHARPT),Y
lda Colour
sta (POSCOLPT),Y
dex
rts
MoveBack: bit Direction
bmi @left
iny
iny
rts
@left: dey
dey
rts
MoveForward:
bit Direction
bmi @left
dey
dey
rts
@left: iny
iny
rts
; Put a first type of rocky "decoration" outside the walls. Colour depends
; on the number of level
PutRock1: jsr MoveBack
lda #ROCK1
sta (POSCHARPT),Y
lda Period
sta (POSCOLPT),Y
jsr MoveForward
jmp NoDecoration
; Put a second type of rocky "decoration" outside the walls.
PutRock2: lda #ROCK2
jsr MoveBack
sta (POSCHARPT),Y
lda #RED
sta (POSCOLPT),Y
jsr MoveForward
jmp NoDecoration
PutRing:
lda #RING
jsr MoveForward
sta (POSCHARPT),Y
lda #CYAN
sta (POSCOLPT),Y
jsr MoveBack
clc ; The code in DrawCavern expects carry clear
jmp NoRings
PutSkeleton:
lda #SKELETON1
jsr MoveBack
sta (POSCHARPT),Y
lda #YELLOW
sta (POSCOLPT),Y
tya
pha
clc
adc #16
tay
lda #SKELETON2
sta (POSCHARPT),Y
lda #YELLOW
sta (POSCOLPT),Y
pla
tay
jsr MoveForward
jmp NoDecoration
PutPlant1: lda #PLANT1 ; Position the plant
sta (POSCHARPT),Y
lda #GREEN
sta (POSCOLPT),Y
sty PlantPos ; Calculate the position of the shoot
tya
iny
adc PlantShoot
tay
bit PlantDir
bmi @right
lda #PLANTSHL
jmp @cc
@right: lda #PLANTSHR
@cc: sta (POSCHARPT),Y
lda #YELLOW
sta (POSCOLPT),Y
ldy PlantPos
lda SFXCounter
beq @exit
lda EFFECTS
ora VoiceBase
sta EFFECTS
@exit: jmp NoRings
PutPlant2: lda #PLANT2 ; Position the plant
sta (POSCHARPT),Y
lda #GREEN
sta (POSCOLPT),Y
sty PlantPos ; Calculate the position of the shoot
dey
tya
sbc PlantShoot
tay
bit PlantDir ; Check the direction of the plant's shoot
bmi @right
lda #PLANTSHR
bne @cc ; Branch always
@right: lda #PLANTSHL
@cc: sta (POSCHARPT),Y
lda #YELLOW
sta (POSCOLPT),Y
ldy PlantPos
lda SFXCounter ; Put a little SFX on
beq @exit
lda EFFECTS
ora VoiceBase
sta EFFECTS
@exit: jmp NoRings
; INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT
;
; Initialization code: prepare the screen to the correct size, center it and
; load the graphic chars and configure the IRQ handler.
;
; INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT - INIT
CenterScreenPAL:
lda #0
sta IsNTSC
lda #30
sta CavernSize
lda #$22
sta Vpos
lda #155
sta RasterlNMI
lda #$3E ; Set a 31 row-high column
sta VICROWNC
ldx #$12
ldy #$16
jmp ContInit
CenterScreenNTSC:
lda #128
sta IsNTSC
lda #25
sta CavernSize
lda #22
sta Vpos
lda #130
sta RasterlNMI
lda #$36 ; Set a 27 row-high column
sta VICROWNC
ldx #$0A
ldy #$10
jmp ContInit
Init: lda #$80 ; Autorepeat on on the keyboard
sta REPEATKE
sta VoiceBase
lda #$08 ; Define screen colour and background
sta VICCOLOR
lda #$90 ; Set a 16 column-wide screen
sta VICCOLNC
lda #$7F ; Set up the VIA 1 and 2 registers
sta IERVIA2
sta IFRVIA2
sta IERVIA1
sta IFRVIA1
sta PORTBVIA2d ; Prepare VIAs for joystick
lda INITVALC
cmp #$05 ; Determine if we run on a PAL or NTSC machine
beq CenterScreenNTSC ; Load the screen settings
bne CenterScreenPAL
ContInit: sty VICSCRVE ; Centre the screen vertically...
stx VICSCRHO ; ... and horizontally
stx NormSCRpos
lda Vpos
sec
sbc #15
sta Vposm15
sta Vposm16
dec Vposm16
lda #$FF ; Move the character generator address to $1C00
sta VICCHGEN ; while leaving ch. 128-255 to their original pos.
jsr MovCh ; Load the graphic chars
sei ; Configure the interrupt handler
lda #$40 ; Mode free run, output -> IRQ and NMI
sta ACRVIA1 ; Set VIA registers
sta ACRVIA2
bit IsNTSC
bmi SyncNTSC ; Load the screen settings
bpl SyncPAL
ContInit1: sta T1LLVIA2
stx T1CHVIA2 ; Set up the timer and start it
sta T1LLVIA1
jsr SyncLater
stx T1CHVIA1
lda #<IrqHandler; The IRQ handler
sta $0314
lda #>IrqHandler
sta $0315
lda #<NMIHandler; The NMI handler
sta $0318
lda #>NMIHandler
sta $0319
lda #$c0
sta IERVIA2
lda #$c0
sta IERVIA1
cli
lda #0
sta HiScore ; Zero the hi-score
sta HiScore+1
sta PORTAVIA1d ; Prepare VIAs for joystick
sta IrqCn
rts
; Synchronize raster on PAL systems
SyncPAL:
; Data for PAL machines. See for example:
; http://www.antimon.org/dl/c64/code/stable.txt
LINES_PAL = 312
CYCLES_PER_LINE_PAL = 71
TIMER_VALUE_PAL = LINES_PAL * CYCLES_PER_LINE_PAL - 2
@loopsync: lda VICRAST ; Synchronization loop
cmp #140
bne @loopsync
lda #<TIMER_VALUE_PAL
ldx #>TIMER_VALUE_PAL
jmp ContInit1
; and NTSC
SyncNTSC:
; Data for NTSC machines. See for example:
; http://www.antimon.org/dl/c64/code/stable.txt
LINES_NTSC = 261
CYCLES_PER_LINE_NTSC = 65
TIMER_VALUE_NTSC = LINES_NTSC * CYCLES_PER_LINE_NTSC - 2
@loopsync: lda VICRAST ; Synchronization loop
cmp #110
bne @loopsync
lda #<TIMER_VALUE_NTSC
ldx #>TIMER_VALUE_NTSC
jmp ContInit1
; Synchronize the timer to the NMI interrupt to a given raster line.
SyncLater:
lda RasterlNMI ; Synchronization loop
@loopsync: cmp VICRAST
bne @loopsync
rts
; Copy the graphic chars. They are subjected to be changed during the pixel-by
; pixel movement, so that routine moves only the characters not used as sprites
; Since the counter is the 8-bit register x, this routine can move a maximum
; of 30 characters.
MovCh: ldx #(LASTCH+1)*8+1
@loop: lda DefChars-1,x
sta GRCHARS1-1,x
dex
bne @loop
rts
; Print the values contained in Res+2, Res+1, Res.
PrintRes:
lda Res+2 ; Print all the BCD chars
jsr PrintBCD
lda Res+1
jsr PrintBCD
lda Res
jmp PrintBCD
; Put the rings in the cavern. Follow a quasi-random pattern using the
; code as "pseudo-random" number generator. There is a tendency to put rings
; on the right, as expected, the "pseudo-random" is not random at all, but
; this is not a big deal in the game.
PutRings: ldy #255
@loop: lda NMIHandler,y
cmp #5
bmi @noringr
lda CavernRight,y
ora #L_RING
sta CavernRight,y
bne @noringl ; Branch always
@noringr: lda NMIHandler,y
cmp #20
bmi @noringl
lda CavernLeft,y
ora #L_RING
sta CavernLeft,y
@noringl: dey
bne @loop
rts
; NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI
;
; This is the NMI handler, called 50 times each second when the VIC-20
; is a PAL unit or 60 when NTSC.
;
; NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI - NMI
NMIHandler: pha
lda NormSCRpos
sta VICSCRHO
bit T1CLVIA1
pla
rti
; IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ
;
; This is the interrupt handler, called 50 times each second when the VIC-20
; is a PAL unit or 60 when NTSC. It does the following things:
;
;
; IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ - IRQ
; Implement some functions as macros, as they are called only once and they
; deserve to be inlined so to save jrs+rts (12 cycles, i.e. 12 µs)
.macro DrawGremlin
ldy GremlinY
beq @deactivate
ldx GremlinX
lda #BLUE
sta Colour
lda #GREMLIN
jsr DrawChar
jmp @skipm
@deactivate:
lda #80
sta GremlinX
@skipm:
.endmacro
.macro UpdatePlants
bit PlantDir ; Update the position of plant shoots
bmi @negshoot
inc PlantShoot
lda PlantShoot
cmp #4
bne @nochange ; Change the direction of the shoot
lda #128
sta PlantDir
jmp @nochange
@negshoot: dec PlantShoot
lda PlantShoot
bne @nochange
lda #0
sta PlantDir
@nochange: asl
asl
sta EFFECTS
lda #10
sta SFXCounter
.endmacro
.macro UpdateGremlin
ldx GremlinX
lda GremlinY
sec
sbc #5
sta GremlinY
and #1
bne @dec
inc GremlinX
beq @skip ; Branch always
@dec: dec GremlinX
@skip:
.endmacro
negspeed: eor #$FF
clc
adc #1
;lda #0
;sec
;sbc ShipXSpeed
jmp posspeed
stopgame: jmp nodrawship
IrqHandler: pha
txa ; Save registers
pha
tya
pha
;lda #09
;sta VICCOLOR
lda #63 ; Switch "off" the screen, shifting all to right
sta VICSCRHO
lda ShipXSpeed
bmi negspeed
posspeed: asl
asl
asl
asl
ora VoiceBase
sta VOICE1
inc IRQfreec
bit Win
bmi stopgame
lda SFXCounter ; Check if we have a SFX on
bne @FX ; If no, shut off voice
lda #0
sta EFFECTS
beq @normalcont ; branch always
@FX: dec SFXCounter ; If yes, decrement counter
@normalcont:dec IrqCn ; Execute every PERIOD/60 of second
beq @contint
jmp @redraw
@contint: lda Period ; Restart the counter for the period
sta IrqCn
inc VertPosPx ; Since the screen will be scrolled upwards,
inc VertPosPx ; compensate the position of the ship by 2 pixels
inc ShipPosY ; Increment the position of the ship (with respect
bit ShipPosY ; to the field) up to line 127. Fall down by gravity
bpl @nopb ; Check if we past 127th line.
lda #127
sta ShipPosY
@nopb: dec VICSCRVE ; Decrement the screen position so that everything
lda VICSCRVE ; scrolls towards the top by 2 raster lines.
cmp Vposm16 ; 16 x 2 pixels = 4 lines
bne @redraw
lda Vpos ; Put the screen again in the bottom position
sta VICSCRVE
inc CurrentYPos ; We increase the position in the cavern
lda #0
sta VertPosPx ; We also put to 0 the shift in the ship position
lda CanIncLev ; And we decrement the counter for the dead time
beq @greml ; for incrementing the level
dec CanIncLev
@greml: bit GremlinX ; See if the gremlin position has to be updated
bmi @redraw
UpdateGremlin ; Inline a macro and save 12 cycles
@redraw: lda VICSCRVE ; Check if in the next iteration we should calculate
cmp Vposm15 ; the shift of the cavern's walls.
bne @chk
lda IrqCn
cmp #1
bne @chk ; Here we must prepare the shift in the walls for
lda #$00 ; the next step.
sta UpdateWPos
UpdatePlants ; Macro call that will be inlined
jsr ChangeRingState
@chk: jsr CLSA ; Launch a complete redraw of the cavern
; Prepare for the left wall
lda #<CavernLeft
sta CavernPTR
lda #>CavernLeft
sta CavernPTR+1
lda #WHITE
sta Colour
ldy #0
sty PosY
lda CurrXPosL
sta CavernPosX
lda #128
sta Direction
;clc ; check if useful!!! (It is not!)
jsr DrawCavern ; Draw the left wall of the cavern
lda CavernPosX
sta CurrXPosL
; Prepare for the right wall
lda #<CavernRight
sta CavernPTR
lda #>CavernRight
sta CavernPTR+1
ldy #0
sty PosY
lda CurrXPosR
sta CavernPosX
tya
sta Direction
jsr DrawCavern
lda CavernPosX
sta CurrXPosR
lda #128
sta UpdateWPos
@endscroll:
bit GremlinX
bmi @nogremlin
DrawGremlin
@nogremlin: jsr DrawShip
nodrawship: ;lda #$08
;sta VICCOLOR
pla ; Restore registers
tay
pla
tax
pla
jmp $EABF ; Jump to the standard IRQ handling routine
; Load the current shape of the ring in the generic character, following the
; value of PlantShoot (from 0 to 3).
ChangeRingState:
lda PlantShoot
and #$03
adc #RING1
tax
ldy #RING
; no rts here, as CopyChar is used afterwards
; Prepare for a copy of the memory of the character to be blended in the sprite
; 1 area.
; X = the caracter to be blended (source)
; Y = the sprite character (destination)
CopyChar:
jsr PrepareCopy
CopyMem: lda (SOURCE),y
sta (DEST),y
dey
bne CopyMem
lda (SOURCE),y
sta (DEST),y
rts
CheckCrash:
ldx ShipChrX
ldy ShipChrY
jsr PosChar
ldy #0
lda (POSCHARPT),y
cmp #SPRITE1A
beq @skip
sta BLENDCHA
iny
lda (POSCHARPT),y
sta BLENDCHC
ldy #16
lda (POSCHARPT),y
sta BLENDCHB
iny
lda (POSCHARPT),y
sta BLENDCHD
@skip: ;rts ; Put a rts here to disable collisions
ldx BLENDCHA ; The code 0 is for the EMPTY char
beq @next1
cpx #SPRITE1A
beq @next4
ldy #SPRITE1A
jsr CheckCollision