-
Notifications
You must be signed in to change notification settings - Fork 0
/
KUNGFU.s
13845 lines (11852 loc) · 308 KB
/
KUNGFU.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
;NEXT:
;3. CAN POSSIBLE REMOVE WORLDCOL AND USE TESTCOL.
;.COMMAND -w142
; NES KUNG FU GAME STATE MACHINE
;
;COPYRIGHT 1990 BY DANIEL LAWTON, ALL RIGHTS RESERVED.
;LAST UPDATE STARTED ON 9/15/90
; HOW THIS STATE MACHINE WORKS:
;
;TO ASSEMBLE, YOU JUST RUN A65 KUNGFU. THE KUNGFU.ASM FILE "INCLUDES"
;ALL NECESSARY FILES. THESE FILES ARE KUNGSUBS.ASM, KUNGTAB.ASM,
;YOURBACK.ASM, YOURCODE.ASM, YOURSND.ASM, AND DRAWDATA.ASM.
;YOURBACK AND DRAWDATA ARE MADE AUTOMATICALLY
;BY THE ND.EXE DRAWING PROGRAM. YOURBACK HAS BACKGROUND DATA FOR THE
;SECOND ROM PAGE AND DRAWDATA HAS PALETTE/SPRITE DEFFINITIONS. KUNGSUBS
;HAS SUBROUTINES THAT GO DIRECTLY TO THE HARDWARE. KUNGFU IS THE STATE
;MACHINE EXECUTION MAINLINE. KUNGTAB IS WHERE YOU PUT YOUR GAME PLAY
;DATA. YOURCODE AND YOURSND ARE EXPLAINED AT THE TOP OF KUNGTAB.ASM.
;THE RESULT OF THE ASSEMBLY IS CODE.PRG WHICH IS LOADED WITH
;THE COLOR DREAMS DEBUGGER BOARD FOR THE NES. IT WILL HAVE 2 32K SEGMENTS
;EACH WITH A 6 BYTE HEADER. THE FIRST LOADS INTO PAGE 0 AND THE SECOND
;INTO PAGE 1. YOU MUST ALSO HAVE YOUR .VID FILE WHICH IS ALSO CREATED
;AUTOMATICALLY BY THE DRAWING PROGRAM.
;
;THE STATE MACHINE USES THE DATA YOU PLACE IN KUNGTAB TO EXECUTE THE
;GAME. THERE ARE 3 MAIN THINGS YOU DO IN KUNGTAB. YOU DEFINE WORLDS
;WHICH ARE BACKGROUNDS AND PALETTES. YOU DEFINE "ENTITIES" WHICH ARE
;MOVING SPRITES. FINALLY, YOU DEFINE THINGS IN THE BACKGROUND WHICH
;INTERACT WITH THE SPRITES. YOU NEED TO CHECK OUT KUNGTAB.ASM AND SIMPLY
;GO THROUGH IT LINE BY LINE SETTING THE DEFINED PARAMETERS IF YOU WANT
;LEARN TO USE THE STATE MACHINE. THIS MODULE SHOULD OPERATE TRANSPARENTLY
;TO YOU, YOU SHOULD NOT MAKE MODIFICATIONS HERE.
;AT BOOT THIS PROGRAM SIMPLY ACTIVATES WORLD #0 AND ITS UP TO YOU TO
;DETECT RESET, INITIALIZE FLAGS, AND LOOK FOR THE START KEY.
;CHECKOUT KUNGTAB AND BE METHODICAL ABOUT SETTING AND UNDERSTANDING
;EACH ENTRY.
; PROGRAMMER'S WARNING
;
;ALL PROCESSING OF ENTITY MOVEMENTS AND WORLD SCROLL TAKES PLACE OUTSIDE
;NMI. DURING NMI WE ONLY SUPPORT JOYSTICKS (WHICH ONLY SET VARIABLES
;AND FLAGS). ALSO, NMI KEEPS THE TIME FOR THE MAINLINE BY SETTING
;TIMETICK AND PHASE4. YOU CAN'T MIX
;LEVELS ON CALLS TO THE KUNGSUBS IN SOME CASES. THINK ABOUT IT BEFORE
;YOU ADD CUSTOM CODE.
;
.include "ScoutDEF.h"
ROMVER = -1
.feature force_range
;INCLUDE( KUNGSUBS.ASM)
;**********************************************************************************************
;Stuff moved here from Kungsubs - Evenball
;**********************************************************************************************
;.CODE ;SPECIFY CODE SEGMENT IN CASE OF PAGEING
.segment "BANK_00"
;.ORG $8000 ;CODE ROM PROGRAM ORIGIN.
;************************** BOOT VECTOR ************************
;
;THIS BOOT VECTOR IS FOR THE SUBROUTINES. DON'T PUT YOUR CODE HERE,
;PUT IT IN THE LEGAL BOOT SUBROUTINE "USER_BOOT" AT THE END OF THIS ASSEMBLY.
;THIS ROUTINE SIMPLE DISABLES VIDEO AND NMI, WAITS FOR THE VIDEO PROCESSOR
;TO RESET, AND THEN INITIALIZES OUR VARIABLES. CODE WILL EVENTUALLY
;BE ADDED TO WORK IN CONJUNCTION WITH SPECIAL FEATURES OF THE COLOR
;DREAMS INC. CARTRIDGES BUT THIS IS NOT YET RELEASED INFO.
BOOT: SEI ;DISALLOW IRQ INTERRUPTS
LDA #0
STA $2000 ;MAKE SURE NMI INTS ARE OFF
;WAIT FOR VIDEO SYNC PULSES. WE NORMALLY JUST NEED TO DO THIS AT BOOT
;TO WAIT FOR THE VIDEO TO GET OUT OF RESET. AFTER THIS KEY CHIP LOGIC
;THIS IS PROBABLY UNNECESSARY.
JSR VERT_WAIT
JSR VERT_WAIT ;WAIT FOR 2 TO MAKE SURE BIT IS VALID
LDA #6
STA $2001
;THIS ROUTINE GENERATES 2 PULSES TO DRIVE THE NINTENDO KEY CHIP FOILER.
;THE PULSES MUST BE ON THE LS377 PIN Q3 WITH PIN Q2 USED AS A BLEED TO
;RAISE THE KEY INPUT BACK TO +5. THE PAGEING SOFTWARE MUST KEEP THESE
;PINS HIGH WHEN NOT IN USE.
JSR XPULSE
JSR XPULSE
LDA #12
STA TWELVE
JMP XEXIT
;THIS ROUTINE GENERATES A .005 SECOND HIGH AND THEN LOW PULSE.
XPULSE: LDA #8
STA EIGHT
JSR DELAY005
LDA #0
STA ZERO
JSR DELAY005
RTS
;THIS ROUTINE DELAYS .0025 SECONDS.
DELAY005:
LDY #4
LDX #58 ;(MAKES 3*256 + 1*58)
XXX10: DEX
BNE XXX10
DEY
BNE XXX10
RTS
ZERO: .byte 0
EIGHT: .byte 8
TWELVE: .byte 12
XEXIT:
;INITIALIZE THE VARIABLES WE USE.
LDA #0
STA NEW_PAL ;RESET FLAG OF PALETTE NEEDS INIT
STA SCCNT ;ZERO DATA TO WRITE TO SCREEN
STA XSCROLL
STA YSCROLL ;ZERO SCROLL
STA CURX
STA CURY ;ZERO BACKGROUND SCROLL LOW VALUES FOR PUTSPRT
STA BACKVALID
STA MOVEBACK ;RESET FLAG TO SCROLL BACKGROUND
STA BPAL_BLINK
STA SPAL_BLINK ;PREVENT PALETTE UPDATES
STA J1FLAGS
STA J2FLAGS
STA J1HOLD
STA J2HOLD
STA J1REPFLG
STA J2REPFLG ;RESET BUTTON LOGIC
STA REG2000 ;AND ZERO SCREEN PAGE
STA DOUBLE_BIT
STA REG4015 ;AND SOUND ENABLE REG
STA SPYADJ
STA BASESPRITE
STA ROTATEFLG
STA ROTATING
STA SHAKEAMT
STA STATBANK
STA STATBITS
STA BACKBANK
STA BACKBITS
LDA #REPRATE
STA REPCNT ;RELOAD THE JOYSTICK REPEAT RATE
LDA #10
STA STATDELAY
LDA #$18
STA REG2001 ;SET UP FOR SPRITES, BACKGROUND, NO ODD COLORS
;AND 31 COL MODE FOR ALL.
JSR HIDESPRITES ;FREE UP AND HIDE THE SPRITES
JSR ZERO_ANIM ;CLEAR THE ANIMATION RAM
JSR ZERO_SOUND
;CALL THE USER'S BOOT CODE.
JSR USER_BOOT
;ALLOW NMI INTERRUPTS.
JSR RESUME_NMI
;NON-INTERRUPT MAINLINE. WE TAKE LIGHT GUN SAMPLES AND
;CALL THE USER NON-NMI PROGRAM.
B1000: JSR USER_MAINLINE
JMP B1000
;************************ NMI INTERRUPT ***************************
;
;THIS IS THE NMI SERVICE ROUTINE. IT COMPLETELY CONTROLS ALL WRITES
;TO THE SCREEN AND YOUR CODE SHOULD NOT VIOLATE THIS SCHEME OR YOU
;MAY LOOSE COMPATIBILITY WITH THESE SUBS. IF YOU WANT TO DO PROCESSING
;UNDER NMI (AND IT IS STRONGLY RECOMMENDED THAT ALL OF YOUR PROCESSING
;IS DONE IN THIS MANNER) PUT YOUR CODE INTO THE ROUTINE "USER_NMI"
;AT THE END OF THIS ASSEMBLY. IF YOU WANT TO WRITE TO RAM, FOLLOW
;THE RULES FOR PUTTING DATA INTO SCNBUF. SEE THE COMMENTS ON SCNBUF
;IN THE RAM EQUATES.
;THIS NMI ROUTINE USES THE FOLLOWING STRATIGY:
;
;1. ALL DATA TO WRITE TO SCREEN IS KEPT IN A BUFFER WHICH CANNOT
;HOLD MORE THAN CAN BE SUCCESSFULLY WRITTEN. AT NMI START, THE
;SPRITES DMA TRANSFER IS STARTED AND THEN WHATEVER IS QUED IN THE
;BUFFER IS WRITTEN. THEN VIDEO IS RE-ENABLED (BUT NOT NMI INTS).
;
;2. ROUTINES THAT SET FLAGS TO GET PROCESSOR POWER OFF THE NMI TICK
;ARE CALLED IF THEIR FLAGS ARE SET. THEY ONLY CALCULATE AND PUT DATA
;INTO THE SCREEN BUFFER, THEY NEVER ACCESS VIDEO DIRECTLY. IF THEY
;CANNOT FIT THEIR DATA IN THE BUFFER THEY IGNORE THIS NMI.
;
;3. THE JOYSTICK IS CHECKED AND THE USER JOYSTICK ROUTINES ARE CALLED
;FOR ALL CHANGES.
;
;4. THE USER PROGRAM NMI PROCESSOR ROUTINE IS CALLED. THIS ROUTINE
;MUST ALSO USE ONLY THE BUFFER OR THE SUBROUTINES AND MUST NOT WRITE
;TO VIDEO.
;TRANSFER THE DMA AND WRITE OUT THE SCREEN BUFFER.
NMI: LDA #06
STA $2001 ;DISABLE VIDEO TO ALLOW SCREEN ACCESS
LDA PUTBAR ;GET ROW TO PUT IT ON
SEC
SBC SHAKEAMT
STA STATSPR ;ALWAYS REPOSITION THE TOP SPRITE IF STAT BAR
LDA ROTATING ;DON'T DO THE DMA IF ROTATING FLAG SET
BNE N10
STA $2003 ;ZERO SPRITE DMA BASE REG
LDA #STATSPR >> 8 ;GET PAGE OF SPRITE RAM
STA $4014 ;START THE SPRITE RAM DMA TRANSFER
N10: JSR STOP_NMI ;DISABLE NMI
LDA $2002 ;RESET ADDRESS REG SELECT FOR THROUGHOUT NMI.
AND #$20
STA TOOMANY ;KEEP SPRITE FLICKER BIT
TXA
PHA
TYA
PHA ;SAVE THE REGISTERS
JSR WRITE_SCNBUF ;WRITE OUT ANY DATA IN SCNBUF
LDA #0
STA SCCNT
LDA MATH1
PHA
LDA MATH2
PHA
LDA MATH3
PHA
LDA MATH4
PHA ;SAVE MATH SUBS VARIABLES
JSR MAKE_SCROLL ;SCREEN UPDATED, MAKE ANY NEW SCROLL NEEDED
;DONE WRITING OUT THE SPRITE AND SCREEN DATA. USE THE CORRECT LOGIC
;TO ENABLE VIDEO AND SCROLL AS NEEDED. THE 2 POSSIBILITIES ARE
;TOP OF SCREEN STATUS BAR OR NONE. WE JUST CALL A ROUTINE THAT IS
;SELECTIVELY LOADED DURING ASSEMBLY.
N100: JSR FINISH_TOP
;VIDEO IS ENABLED. CALL ROUTINES THAT DO PROCESSING WHEN ACTIVATED
;BY FLAGS. WE HAVE TO PRIORITIZE THEM.
;WE FIRST SAVE OFF A FEW MEMORY VARIABLES THAT ARE DEFINED TO BE USABLE
;BY ALL.
LDA SUBSVAR1
PHA
LDA SUBSVAR2
PHA
LDA SUBSVAR3
PHA
LDA SUBSVAR4
PHA
LDA SUBSVAR5
PHA
LDA SUBSVAR6
PHA ;SAVE SUB VARIABLES SO THAT THE SUBS MAY
;BE USED FROM ANY LEVEL.
LDA FETCHCOL
PHA
LDA FETCHCOL+1
PHA
LDA FETCHROW
PHA
JSR SWAPPTRS ;SWAP THE IMPORTANT BACKGROUND SCANNERS
;CHECK FOR JOYSTICK CHANGES.
LDA #1
STA $4016
LDA #0
STA $4016 ;LATCH A VALUE TO SHIFT. ALSO LATCHES 4017.
LDA #0
STA J1CHANGE ;ZERO RESULT
LDX #8
N200: LDA $4016
ROR A
ROL J1CHANGE ;MOVE BIT UP INTO OUR BYTE
DEX
BNE N200
LDA J1CHANGE ;GET NEW VALUE
TAX ;SAVE NEW VALUE
EOR J1FLAGS ;GET CHANGES
STA J1CHANGE ;SAVE CHANGES
STX J1FLAGS ;AND CURRENT STATE
LDA #0
STA J2CHANGE ;ZERO RESULT
LDX #8
N210: LDA $4017
ROR A
ROL J2CHANGE ;MOVE BIT UP INTO OUR BYTE
DEX
BNE N210
LDA J2CHANGE ;GET NEW VALUE
TAX ;SAVE NEW VALUE
EOR J2FLAGS ;GET CHANGES
STA J2CHANGE ;SAVE CHANGES
STX J2FLAGS ;AND CURRENT STATE
;DO THE NECESSARY AUTOREPEAT LOGIC.
;FIRST WE RESET ANY AUTOREPEATING KEYS THAT ARE NO LONGER DOWN.
LDA J1FLAGS
AND J1REPFLG
STA J1REPFLG
LDA J1FLAGS
AND J1HOLD
STA J1HOLD ;RESET ANY NO LONGER DOWN IN JOYSTICK 1 FLAGS
LDA J2FLAGS
AND J2REPFLG
STA J2REPFLG
LDA J2FLAGS
AND J2HOLD
STA J2HOLD ;RESET ANY NO LONGER DOWN IN JOYSTICK 2 FLAGS
;SEE IF THE REPEAT COUNTER HAS RUN DOWN.
DEC REPCNT ;COUNT DOWN THE REPEAT COUNTER
BNE N215 ;SKIP IF NOT TIME TO REPEAT AGAIN.
LDA #REPRATE
STA REPCNT ;RELOAD IT
LDA J1REPFLG ;GET THE REPEATING KEYS
ORA J1CHANGE
STA J1CHANGE ;SET AS IF ITS A CHANGED KEY
LDA J2REPFLG
ORA J2CHANGE
STA J2CHANGE
LDA J1HOLD ;GET KEYS WAITING TO AUTOREPEAT NEXT PASS
AND #J1REPENA ;BUT ONLY WANT THOSE ENABLED TO REPEAT
STA J1REPFLG ;SET TO REPEAT NEXT COUNT
LDA J1FLAGS
STA J1HOLD ;MOVE CURRENT STATE INTO THE HOLDING FLAG
LDA J2HOLD ;GET KEYS WAITING TO AUTOREPEAT NEXT PASS
AND #J2REPENA ;BUT ONLY WANT THOSE ENABLED TO REPEAT
STA J2REPFLG ;SET TO REPEAT NEXT COUNT
LDA J2FLAGS
STA J2HOLD ;MOVE CURRENT STATE INTO THE HOLDING FLAG
;CALL THE USER JOYSTICK ROUTINES TO SERVICE ANY CHANGED BUTTON.
;IF YOU WANT TO GET ONLY BUTTONS THAT GO DOWN, REMOVE THE COMMENT FROM
;THE AND INSTRUCTION IMMEDIATLY PRECEEDING THE BIT TESTS.
N215: LDA J1CHANGE ;GET JOYSTICK 1 CHANGES
AND J1FLAGS
BEQ N240 ;SKIP IF NO JOYSTICK CHANGES THIS PASS.
ROR J1CHANGE
BCC N220
JSR RIGHT1
N220: ROR J1CHANGE
BCC N221
JSR LEFT1
N221: ROR J1CHANGE
BCC N222
JSR DOWN1
N222: ROR J1CHANGE
BCC N223
JSR UP1
N223: ROR J1CHANGE
BCC N224
JSR STRT1
N224: ROR J1CHANGE
BCC N225
JSR SEL1
N225: ROR J1CHANGE
BCC N226
JSR B1
N226: ROR J1CHANGE
BCC N240
JSR A1
N240: LDA J2CHANGE ;GET JOYSTICK 2 CHANGES
AND J2FLAGS
BEQ N260
ROR J2CHANGE
BCC N250
JSR RIGHT2
N250: ROR J2CHANGE
BCC N251
JSR LEFT2
N251: ROR J2CHANGE
BCC N252
JSR DOWN2
N252: ROR J2CHANGE
BCC N253
JSR UP2
N253: ROR J2CHANGE
BCC N254
JSR STRT2
N254: ROR J2CHANGE
BCC N255
JSR SEL2
N255: ROR J2CHANGE
BCC N256
JSR B2
N256: ROR J2CHANGE
BCC N260
JSR A2
;CALL THE KUNGFU NMI ROUTINE. ITS THE ONE THAT MIGHT MAKE US NEED TO SCROLL
N260: LDA a:BPAUSE ;IF PAUSED, DON'T DO ANY OF IT.
BNE N380
JSR USER_NMI
JSR DOSCRL ;SCROLL THE BACKGROUND IF NEEDED
JSR DOPAL ;UPDATE PALETTES IF NEEDED
LDA ROTATING ;DON'T DO THE DMA IF ROTATING FLAG SET
BNE N265
JSR CHECK_ANIM ;SEE IF ANIMATIONS NEED PROCESSING
N265:
JSR FEED_SOUND ;FEED THE SOUND CHIP
;DONE WITH THIS NMI. RESTORE REGS AND REENABLE NMI INTERRUPTS.
N380: JSR SWAPPTRS ;SWAP THE IMPORTANT BACKGROUND SCANNERS
PLA
STA FETCHROW
PLA
STA FETCHCOL+1
PLA
STA FETCHCOL
PLA
STA SUBSVAR6
PLA
STA SUBSVAR5
PLA
STA SUBSVAR4
PLA
STA SUBSVAR3
PLA
STA SUBSVAR2
PLA
STA SUBSVAR1
PLA
STA MATH4
PLA
STA MATH3
PLA
STA MATH2
PLA
STA MATH1
PLA
TAY
PLA
TAX
JSR RESUME_NMI
RTS ;WE RTS TO PNMI WHICH RTIs
;THIS SUBROUTINE IS CALLED TO SWAP THE IMPORTANT BACKGROUND LINK AND SCAN
;VARIABLES WITH THE EXTRA COPY. IT IS CALLED AT THE START AND END OF
;NMI SO THAT WE CAN SCROLL FASTER AND WITHOUT DISTURBING SOLID CHECKING
;THAT TAKES PLACE OUTSIDE NMI.
SWAPPTRS:
LDA WORLDSPOT
LDX XWORLDSPOT
STA XWORLDSPOT
STX WORLDSPOT
LDA WORLDSPOT+1
LDX XWORLDSPOT+1
STA XWORLDSPOT+1
STX WORLDSPOT+1
LDA SPOTCOL
LDX XSPOTCOL
STA XSPOTCOL
STX SPOTCOL
LDA SPOTCOL+1
LDX XSPOTCOL+1
STA XSPOTCOL+1
STX SPOTCOL+1
LDA BACKBASE
LDX XBACKBASE
STA XBACKBASE
STX BACKBASE
LDA BACKBASE+1
LDX XBACKBASE+1
STA XBACKBASE+1
STX BACKBASE+1
LDA BPBASE
LDX XBPBASE
STA XBPBASE
STX BPBASE
LDA BPBASE+1
LDX XBPBASE+1
STA XBPBASE+1
STX BPBASE+1
LDA BACKSIZE
LDX XBACKSIZE
STA XBACKSIZE
STX BACKSIZE
LDA BACKWIDE
LDX XBACKWIDE
STA XBACKWIDE
STX BACKWIDE
LDA BACKHIGH
LDX XBACKHIGH
STA XBACKHIGH
STX BACKHIGH
LDA BPWIDE
LDX XBPWIDE
STA XBPWIDE
STX BPWIDE
RTS
; TOP OF SCREEN STATUS BAR LOGIC
;
;THIS ROUTINE WILL MOVE SPRITE #0 TO THE CORRECT LOCATION FOR TOP OF
;SCREEN STATUS BAR AND WILL MARK IT AS IN USE. IT IS CALLED AT BOOT
;AFTER THE SPRITES ARE FREED UP AND AFTER EACH TOPSTAT COMMAND IN
;KUNGFU.ASM. SPRITE 0 IS ALWAYS MARKED AS IN USE, EVEN IF WE HAVE NO
;TOP OF SCREEN STATUS BAR.
TOPSPRITE:
LDA PUTBAR ;GET ROW TO PUT IT ON
STA STATSPR
LDA #1 ;SET SPRITE TO CREEP OUT OF INVISIBLE FIRST
STA STATSPR+3 ;COLUMN BY ONLY 1 PIXEL.
LDA #STATSPRITE ;AND GET A SOLID LINE SPRITE
STA STATSPR+1
LDA #$28 ;PUT IT BEHIND ANYTHING ELSE, MARK IN USE.
STA STATSPR+2
TPS10: RTS
;THIS ROUTINE PROVIDES THE ACTUAL TOP OF SCREEN STATUS BAR SUPPORT.
;FIRST WE MAKE SURE OUR CHAR IS STILL THERE. WE ALWAYS PUT ONE THERE
;FOR SAFETY REASONS.
FINISH_TOP:
LDA STARTSTAT ;SEE IF WE HAVE A STATUS BAR
BNE FT01 ;IF SO, GO MAKE ONE
LDA XSCROLL
STA $2005 ;IF NOT, SELECT THE NORMAL X SCROLL
LDA YSCROLL
STA $2005 ;SELECT THE NORMAL Y SCROLL
LDA REG2001
STA $2001 ;ENABLE VIDEO
JMP FT35
FT01: LDA $2002
LDA #$27
STA $2006
LDA #$A0
STA $2006 ;ADDRESS LAST ROW OF SCREEN BUFFER, FIRST CHAR
LDA #STATCHAR
STA $2007 ;AND WRITE OUT THE CHAR TO DETECT AS SOLID
;SELECT THE VIDEO BANK USED FOR THE TOP OF SCREEN STATUS BAR. THEN
;SET THE SCREEN SCROLL TO DISPLAY THE STATUS BAR REGION AND ENABLE
;VIDEO. OUR STATUS BAR LOGIC PUTS THE SPRITE COLLISION DETECTION
;ON THE START OF THE LINE EXCEPT THAT IT CREEPS OUT OF THE INVISIBLE
;FIRST COLUMN (WE RUN WITH THE LEFT COLUMN INVISIBLE) BY ONLY 1 PIXEL.
;WE SELECT THE LAST PIXEL OF THE FIRST VIDEO PAGE THUS MAKING ONLY
;1 SMALL DOT DISPLAY OF OUR COLLISION SPRITE. THIS ALSO GIVES US
;A FULL LINE SCAN TIME (59 MICROSECONDS) TO CHANGE THE X SCROLL.
LDA STATBANK
LDY STATINDX
JSR SELPAGE ;SELECT THE PAGE ACCORDING TO ROM OR ROMLESS
LDA REG2000
AND #$E7
ORA STATBITS
STA REG2000 ;SET UPPER/LOWER BANK BITS FOR THE STATUS
;BAR VIDEO BANK.
AND #$FE
STA $2000 ;SELECT FIRST SCREEN PAGE BUT SCROLL TO LAST PIXEL
LDA REG2001
STA $2001 ;ENABLE VIDEO
LDA #255 ;SET SCROLL THAT JUST MAKES 1 PIXEL TO RIGHT OF
STA $2005 ;THE INVISIBLE ZONE SO WE CAN GET 1 PIXEL COLLISION
LDA STARTSTAT
CLC
ADC SHAKEAMT
STA $2005 ;MOVE VERTICAL ALONG WITH SHAKE
;NOW WAIT FOR THE BIT TO GO OFF THEN ON.
LDY #10
LDA #$40
FT31: BIT $2002
BEQ FT32 ;WAIT FOR 40H TO GO OFF
DEX
BNE FT31 ;OR FOR A TIMEOUT
DEY
BNE FT31
JMP FT33
LDY #10
FT32: BIT $2002
BNE FT33
DEX
BNE FT32
DEY
BNE FT32
;IT CHANGED STATE. SET THE NEW SCROLL VALUES
FT33: LDX STATDELAY
FT33A: DEX
BNE FT33A ;DON'T KNOW WHY BUT DELAY CLEARS UP JUNK
LDA XSCROLL
STA $2005 ;SELECT THE NORMAL X SCROLL
LDA YSCROLL
STA $2005 ;SELECT THE NORMAL Y SCROLL
FT35: LDA REG2000
AND #$E7
ORA BACKBITS
STA REG2000 ;SET UPPER/LOWER BANK BITS FOR THESE SETS
STA $2000 ;FIX THE PAGE # TO WHAT USER WANTED
LDA BACKBANK
LDY BACKINDX
JSR SELPAGE ;SELECT THE PAGE ACCORDING TO ROM OR ROMLESS
RTS
;********************* SUBROUTINE AREA ***************************
;
;THIS AREA CONTAINS BOTH ROUTINES YOU MAY CALL AS DOCUMENTED IN
;THE SUBS.DOC AND ALSO SUPPORT ROUTINES USED BY THE SUBS THEMSELVES.
;DON'T PUT ANY OF YOUR CODE HERE OR YOU WILL BECOME INCOMPATIBLE WITH
;NEW RELEASES.
;THIS ROUTINE WILL PLAY (ACTIVATE) THE SOUND STRUCTURE POINTED TO BY
;Y:X. THE SLOT TO PLAY IT IN IS PASSED IN A. A RETURNS Z IF SUCCESS
;AND NZ IF FAILURE. THE VALUES FOR A ARE:
;0 AND Z IF SUCCESS.
;-1 IF THE SOUND SLOT # WAS INVALID (OVER RANGE).
;1 IF THE SLOT WAS ALREADY IN USE (USE SILENCE TO FREE THE SLOT).
PLAY: STX SUBSVAR1
STY SUBSVAR2 ;SAVE ENTRY POINTERS
JSR SNDINDX ;IN RANGE?
BEQ P10
RTS
P10: LDA SND_RAM+SF,X ;IN USE?
BPL P20
LDA #1 ;IF IT IS, RETURN 1
RTS
P20: LDA #1
STA SND_RAM+ST,X ;SET NEXT NMI TO ACTIVATE SOUND
LDA #0
STA SND_RAM+SI,X ;SET INSTRUMENT 0 AS DEFAULT
STA SND_RAM+LOP,X ;ZERO LOOP COUNTER
LDA SUBSVAR1
STA SND_RAM+SBL,X
STA SND_RAM+SOL,X ;SET LOW OFFSET
LDA SUBSVAR2
STA SND_RAM+SBH,X
STA SND_RAM+SOH,X ;SET HIGH OFFSET
LDA #$C0
STA SND_RAM+SF,X ;SET AS ACTIVE SOUND STRUCTURE BUT NO REG.
RTS
;THIS ROUTINE IS USED AT BOOT TO ZERO THE SOUND CONTROL RAM. SILENCE
;CANNOT BE USED BECAUSE THE DATA WILL BE INTERPRETED AND IT IS INVALID AT
;THAT TIME.
ZERO_SOUND:
LDA #0
STA DRUMLOCK
TAX
ZS10: STA SND_RAM,X
INX
CPX #SNDMEM ;SEE IF DONE
BNE ZS10
RTS
;THIS ROUTINE WILL SILENCE (FREE UP) THE SOUND SLOT SPECIFIED BY A.
;IT IS OK TO SILENCE A SOUND THAT IS NOT ACTIVE. ON RETURN A=0 AND Z
;IF SUCCESS OR A=-1 AND NZ IF INVALID SLOT #.
SILENCE:
CMP #-1 ;SILENCE ALL?
BNE S10
ALLSILENCE:
LDA #0
S5: PHA
JSR SILENCE ;IF SO, LOOP WITH ALL VALUES
BNE S7
PLA
CLC
ADC #1
JMP S5
S7: PLA
LDA #0 ;AND RETURN OK.
RTS
S10: JSR SNDINDX
BEQ S20
RTS ;IF BAD INDEX, RETURN -1
;INTERNAL CALL LOCATION IF X HAS A VALID SLOT NUMBER TO SILENCE.
S20: LDA SND_RAM+SF,X ;GET THE FLAG BYTE
BPL S50 ;SKIP IF NOT IN USE
;THIS SLOT WAS ACTIVE. IF IT WAS NOT ACTUALLY PLAYING WE ARE DONE BUT
;IF IT WAS PLAYING, WE MIGHT HAVE TO REACTIVATE ANOTHER SUSPENDED SOUND
;SLOT. WE SCAN THE LIST FROM THE TOP DOWN AND ONLY REACTIVATE THE FIRST
;WE FIND.
TAY ;SAVE A COPY
AND #$60 ;SEE IF NOT ASSIGNED OR IF SUSPENDED
BEQ S25 ;SKIP IF TRULY ACTIVE
LDA #0
STA SND_RAM+SF,X ;IF INACTIVE, JUST FREE IT UP
JMP S50 ;IF THIS ONE'S INACTIVE, IT DIDN'T SUSPEND ANY
S25: JSR DISABLE_VOICE ;IF ACTIVE, DISABLE IT'S VOICE
LDA SND_RAM+SF,X ;GET BACK IT'S FLAG
PHA ;SAVE THE FLAG
LDA #0
STA SND_RAM+SF,X ;FREE UP THIS SOUND STRUCTURE
PLA
AND #$1F ;KEEP BASE OF REGISTERS IN USE
STA SUBSVAR1 ;SAVE HERE FOR COMPARE TO ONES WE FIND
CMP #$1F ;PERCUSSION?
BEQ S50
LDX #0 ;START AT LOWEST SLOT
S30: TXA
PHA
LDA SND_RAM+SF,X ;GET ITS FLAG BYTE
BPL S42 ;IF NOT IN USE, SKIP IT
AND #$20 ;SEE IF SUSPENDED
BEQ S42 ;WE DON'T CARE UNLESS IT IS
LDA SND_RAM+SF,X
AND #$1F ;GET THE VOICE IT WAS USING
EOR SUBSVAR1 ;SEE IF SAME AS OURS
BNE S42 ;IF NOT, WE DON'T CARE
PLA ;IF IT WAS, DISCARD SLOT INDEX
JSR INITSND ;AND INITIALIZE IT
JSR ENABLE_VOICE
LDA SND_RAM+SF,X
AND #$DF
STA SND_RAM+SF,X ;AND MARK AS NOT SUSPENDED ANY MORE
JMP S50
S42: PLA
CLC
ADC #SND_WIDE
TAX
CMP #SND_WIDE*SNDSLOTS ;SEE IF DONE WITH LIST
BCC S30 ;DO NEXT
JMP S50
S45: PLA ;IF LIST END, DONE
S50: LDA #0
RTS
;THIS ROUTINE WILL CHECK THE STATUS OF THE SOUND SLOT IN A. ON RETURN:
;A=0 AND Z IF NOT IN USE
;A=1 AND NZ IF IN USE
;A=-1 AND NZ IF INVALID SLOT #
PLAYING:
JSR SNDINDX ;IN RANGE?
BNE PL10
LDA SND_RAM+SF,X ;IN USE?
BMI PL20
LDA #0
PL10: RTS
PL20: LDA #1 ;IF IT IS, RETURN 1
RTS
;THIS ROUTINE IS CALLED TO INITIALIZE A NEW WORLD BACKGROUND OR TO RAPIDLY
;MOVE TO A NEW LOCATION IN AN EXISTING WORLD BACKGROUND. IT FILLS THE ENTIRE
;SCREEN BUFFER IN 1 PASS AND THUS PRODUCES A QUICK FLICKER ON THE SCREEN.
;TYPICALLY IT IS ONLY USED TO BOOT A SCREEN AND NOT TO SCROLL IT.
;A WORLD CONSISTS OF A LIST OF BACKGROUNDS TERMINATED WITH 0. THE
;NAME OF THE BACKGROUND THAT IS DESIRED TO BE PLACED AT A HORIZONTAL
;POSITION IN THE WORLD IS PLACED IN THE LIST AND THE SCROLLING ROUTINES
;WILL AUTOMATICALLY FETCH DATA FROM THERE AT LAUNCH TIME. SEE KUNGTAB.ASM
;FOR MORE INFO.
;TO USE THIS ROUTINE, SET XBACK (WORD) TO THE DESIRED X PIXEL LOCATION
;IN THE BACKGROUND. SET YBACK (WORD) TO THE DESIRED Y PIXEL LOCATION.
;POINT Y:X (Y HIGH BYTE, X LOW BYTE) TO THE BACKGROUND TO BE USED.
;OUT OF RANGE X AND Y LOCATIONS WILL BE CORRECTED AND THIS FACT CAN
;BE USED TO MOVE TO THE BOTTOM OR END OF A SCREEN.
;ON RETURN: SEE SIZE_LIST.
; SEE POINTBACK
; SEE MAKECOL
INITBACK:
JSR ALLOW_WRITES
STX WORLDPTR
STY WORLDPTR+1
JSR SIZE_LIST ;MAKE THE PARAMETERS ABOUT THIS BACKGROUND.
LDA #-1
STA BACKVALID ;ENABLE BACKGROUND SCROLL CREATION
JSR WRITE_SCNBUF ;CLEAR THE BUFFER WE NEED
JSR FIXSCROLL ;FIX THE USER'S WINDOW VALUES
JSR MAKECOL ;MAKE THE ROW AND COLUMN OF UPPER LEFT SCREEN
;BUILD THE COLUMNS NEEDED TO FILL THE SCREEN. COLUMNS ARE USED INSTEAD
;OF ROWS BECAUSE ALL COLUMNS ARE GUARANTEED TO BE CONTINUOUS FROM THE
;SAME BACKGROUND BUT ROWS MIGHT BRIDGE BACKGROUNDS. WE USE THE SAME
;ROUTINE USED TO SCROLL BUT CALL IT UNTIL DONE.
LDX #33 ;GET LOOP COUNT
LDA WORLD_WIDTH+1 ;SEE IF WORLD CAN TAKE IT
BNE IB50
CPX WORLD_WIDTH
BCC IB50
LDX WORLD_WIDTH ;IF WORLD SMALLER THAN 33, USE MAX
IB50: TXA
PHA
JSR FINDCOL ;FIND OUT WHERE THE NEXT COLUMN IS STORED
JSR BUILD_COL ;GET DATA ROW INTO SCNBUF.
JSR WRITE_SCNBUF
LDA FETCHCOL
CLC
ADC #1
STA FETCHCOL
BCC IB66
INC FETCHCOL+1 ;MOVE TO NEXT COLUMN
IB66: PLA
TAX
DEX
BNE IB50 ;DO ALL 33 COLS
;MAKE THE SCROLL VALUE FROM THE CURRENT WINDOW LOCATIONS
LDA XBACK
STA CURX
LDA XBACK+1
STA CURX+1
LDA YBACK
STA CURY
LDA YBACK+1
STA CURY+1 ;SET WINDOW AS CURRENT LOCATION
JSR MAKE_SCROLL ;CONVERT IT INTO SCROLL VALUES
;COPY THE SCAN VARIABLES INTO THE COPY USED BY NMI.
LDA WORLDSPOT
STA XWORLDSPOT
LDA WORLDSPOT+1
STA XWORLDSPOT+1
LDA SPOTCOL
STA XSPOTCOL
LDA SPOTCOL+1
STA XSPOTCOL+1
LDA BACKBASE
STA XBACKBASE
LDA BACKBASE+1
STA XBACKBASE+1
LDA BPBASE
STA XBPBASE
LDA BPBASE+1
STA XBPBASE+1
LDA BACKSIZE
STA XBACKSIZE
LDA BACKWIDE
STA XBACKWIDE
LDA BACKHIGH
STA XBACKHIGH
LDA BPWIDE
STA XBPWIDE
;THIS SUB CAN BE CALLED TO RE-ENABLE NMI. IT DOES NOT RE-ENABLE VIDEO.
;THE NEXT NMI WILL DO THAT.
RESUME_NMI:
LDA REG2000
ORA #$80
STA $2000 ;REENABLE NMI AND LET IT REENABLE VIDEO
RTS
;THIS SUB WILL DISABLE NMI, WAIT FOR VERTICAL RETRACE, AND THEN DISABLE
;VIDEO. IT SHOULD BE USED IN PREP FOR A SCREEN WRITE.
ALLOW_WRITES:
JSR STOP_NMI ;DISABLE NMI
JSR VERT_WAIT ;WAIT TO PREVENT SCREEN FLASH
LDA #6
STA $2001 ;ENABLE SCREEN WRITES, DISABLE VIDEO
RTS
;THIS ROUTINE WILL TAKE XBACK AND YBACK AND MAKE FETCHCOL AND FETCHROW
;FOR USE WITH BUILD_COL AND BUILD_ROW. IN OTHERWORDS, IT MAKES THE COLUMN
;(WORD) AND ROW (BYTE) OF THE UPPER LEFT CORNER OF THE VIEWABLE SCREEN.
;THE COLUMN IS THE COLUMN WITHIN THE CURRENT COMBINED BACKGROUND WORLD,
;NOT WITHIN THE LITTLE BACKGROUND AT THAT POINT. FINDCOL IS HELPFUL
;FOR MAKING THAT CORRECTION.
MAKECOL:
LDA XBACK
STA FETCHCOL
LDA XBACK+1
STA FETCHCOL+1
LSR FETCHCOL+1
ROR FETCHCOL
LSR FETCHCOL+1
ROR FETCHCOL
LSR FETCHCOL+1
ROR FETCHCOL ;MAKE /8 COLUMN (WORD)
LDA YBACK
STA FETCHROW
LDA YBACK+1
LSR A
ROR FETCHROW
LSR A
ROR FETCHROW