-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprg2.asm
2264 lines (1720 loc) · 26.6 KB
/
prg2.asm
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
; [ *************************************** PRG 2 ($8000 - $9FFF) *************************************** ]
Main:
sei ; disable irqs
cld ; disable decimal mode
ldx #$ff ; set the stack pointer to $01ff
txs
stx asp ; set the action stack pointer to $07ff
lda #0
sta ctrl0
sta ctrl0lh
sta ctrl0r
sta ctrl0l
sta ctrl0d
sta ctrl0u
sta ctrl0t
sta ctrl0e
sta ctrl0b
sta ctrl0a
sta nmi2001
sta nmiDMA
sta nmiBB
sta frmSync
sta loopCount
sta dmaReq
sta nextAction
sta currentAction
sta prevAction
sta retAction
sta softPpuScrollX
lda #%00000110 ; disable bkg and sprites
sta $2001
lda #%10001000 ; init ppu (NMI enabled)
sta $2000
lda #0 ; init state
.cs:
sta state
jsr ChangeState
jsr ChangePrgBanks ; change Prg banks
jsr ExeInitHandler
lda #%00000110 ; turn off background and sprites
jsr Set2001
jsr HideAllSprites
jsr ReqSpriteDMA ; sprite dma requested
jsr ChangeChrBanks ; change chr banks
ldx sprPalette ; draw background
ldy sprPalette + 1
jsr LoadSpritePalette
ldx bkgPalette
ldy bkgPalette + 1
jsr LoadBackgroundPalette
ldx nametable
ldy nametable + 1
jsr LoadNameTable
lda #%00011110 ; turn on background and sprites
jsr Set2001
.ca:
jsr ChangeAction
jsr ExeActionInitHandler
.loop:
inc loopCount
lda currentAction ; check for action change request
cmp nextAction
bne .ca
.sync:
lda frmSync ; wait for sync
beq .sync
lda #0
sta frmSync
jsr ReadController0
jsr ExeActionInputHandler
lda currentAction ; check for action change request
cmp nextAction
bne .ca
jsr ExeProcessHandler
cmp state ; check for state change request
bne .cs
jsr ExeActionProcessHandler
cmp state ; check for state change request
bne .cs
lda currentAction ; check for action change request
cmp nextAction
bne .ca
jsr ExeUpdateHandler
jsr ExeActionUpdateHandler
lda dmaReq ; check for sprite dma request (dmaReq != 0)
beq .loop
lda #0 ; clear dma request
sta dmaReq
jsr ReqSpriteDMA ; sprite dma requested
jmp .loop
; ****************************************************
; NMI
; ****************************************************
NMI:
pha
txa
pha
tya
pha
.set2001
lda nmi2001
beq .bar ; no set2001 request
lda soft2001
sta $2001
lda #0
sta nmi2001 ; clear set2001 bit
.bar:
lda nmiBB
beq .dma ; no draw bar request
lda $2002 ; reset latch to high
lda #$23
sta $2006
lda #$40
sta $2006
ldx #0
.next0:
lda BOTTOM_BAR_BUF, x
sta $2007
inx
cpx #96
bne .next0
; attribute table
lda $2002 ; reset latch to high
lda #$23
sta $2006
lda #$f0
sta $2006
ldx #96
.next1:
lda BOTTOM_BAR_BUF, x
sta $2007
inx
cpx #112
bne .next1
lda #0
sta nmiBB ; clear draw bar bit
.dma:
lda nmiDMA
beq .done ; no dma request
lda #2 ; dma from $0200
sta $4014
lda #0
sta nmiDMA ; clear dma bit
.done
lda #1
sta frmSync
lda softPpuScrollX ; reset scroll to (softPpuScrollX, 0)
sta $2005
lda #0
sta $2005
pla
tay
pla
tax
pla
rti
; ****************************************************
; global functions
; ****************************************************
; compare a chapter password to user input
; arg (ptr) - password
; arg (ptrII) - input
; ret (a) - non-zero if equal
PasswordCompare:
tya
pha
ldy #0
.next:
lda [ptrLo], y
cmp [ptrLoII], y
bne .ne
iny
cpy #8
bne .next
.eq:
pla
tay
lda #1
rts
.ne:
pla
tay
lda #0
rts
; change prg banks
ChangePrgBanks:
pha
; change prg banks
lda #%01000111 ; set prg $A000 - $BFFF to bank [prgBank + 1]
sta $8000
lda prgBank
sta $8001
lda #%01000110 ; set prg $C000 - $DFFF to bank [prgBank]
sta $8000
lda prgBank + 1
sta $8001
pla
rts
; change chr banks
ChangeChrBanks:
pha
; change chr bank
lda #%01000000 ; 2k chr
sta $8000
lda chrBank
sta $8001
lda #%01000001 ; 2k chr
sta $8000
lda chrBank + 1
sta $8001
lda #%01000010 ; 1k chr
sta $8000
lda chrBank + 2
sta $8001
lda #%01000011 ; 1k chr
sta $8000
lda chrBank + 3
sta $8001
lda #%01000100 ; 1k chr
sta $8000
lda chrBank + 4
sta $8001
lda #%01000101 ; 1k chr
sta $8000
lda chrBank + 5
sta $8001
pla
rts
; generate a pseudo-random value
; ret (a) - random value
Random:
tya
clc
adc rseed
eor tempI
eor loopCount
eor ctrl0
eor $0300
eor $0301
asl a
asl a
clc
adc rseed
eor tempII
adc loopCount
adc ctrl0
adc $0300
adc $0301
clc
adc #03
sta rseed
rts ; return high 8 bits
; push action data to action stack
; arg (a) - data
PhA:
pha
txa
pha
tsx
lda $0102, x
ldx asp
sta ACTION_STACK_HI, x
dex
stx asp
pla
tax
pla
rts
; pop action data from action stack
; ret (a) - data
PoA:
pha
txa
pha
ldx asp
inx
lda ACTION_STACK_HI, x
stx asp
tsx
sta $0102, x
pla
tax
pla
rts
; change the current (currentAction) action to nextAction
ChangeAction:
pha
txa
pha
tya
pha
lda nextAction
clc
asl a
adc actions
tax
lda actions + 1
adc #0
tay
jsr Dereference16
stx ptrLo
sty ptrHi
ldy #0
ldx #0
.next:
lda [ptrLo], y
sta actInit, x
iny
inx
cpx #10
bne .next
lda currentAction
sta prevAction
lda nextAction
sta currentAction
jsr PoA ; set return action
sta retAction
pla
tay
pla
tax
pla
rts
; clear the bottom bar area
ClearBottomBar:
pha
txa
pha
tya
pha
lda nametable
clc
adc #$40
sta ptrLo
lda nametable + 1
adc #$03
sta ptrHi
ldy #0
ldx #0
.next0:
lda [ptrLo], y
sta BOTTOM_BAR_BUF, x
iny
inx
cpx #96
bne .next0
lda nametable ; clear attr table part
clc
adc #$f0
sta ptrLo
lda nametable + 1
adc #$03
sta ptrHi
ldy #0
ldx #96
.next1:
lda [ptrLo], y
sta BOTTOM_BAR_BUF, x
iny
inx
cpx #112
bne .next1
.done:
lda nmiBB ; set nmi flag
bne .done
lda #1
sta nmiBB
pla
tay
pla
tax
pla
rts
; clear the buffer
; arg (x) - length hi
; arg (y) - length lo
; arg (ptr) - buffer address
; arg (a) - clear byte
ClearBuffer:
sty tempI
stx tempII
tax
pha
lda tempII
pha
tya
pha
ldy #0
.next:
txa
sta [ptrLo], y
dec tempI
lda tempI
bne .cont
lda tempII
beq .done
.cont:
iny
bne .next
inc ptrHi
dec tempII
jmp .next
.done:
pla
tay
pla
tax
pla
rts
; draw text to buffer
; arg (ptr) - buffer address
; arg (ptrII) - string address
DrawTextToBuffer:
pha
txa
pha
tya
pha
ldx #$04 ; 4 times 256 = 1kb
ldy #0
.next:
lda [ptrLoII], y
beq .done
cmp #$01
bne .store
clc
lda #8
adc ptrLo
sta ptrLo
lda #0
adc ptrHi
sta ptrHi
jmp .inc
.store:
sta [ptrLo], y
.inc
iny
bne .next
inc ptrHi
inc ptrHiII
dex
bne .next
.done:
pla
tay
pla
tax
pla
rts
; draw a text dialog on the bottom bar (tempI)
; arg (x) - text low address
; arg (y) - text high address
; ret (x) - incremented text low address
; ret (y) - incremented text high address
; ret (a) - non-zero if was trimmed
DrawTextDialog:
stx ptrLo
sty ptrHi
lda #$c2
sta BOTTOM_BAR_BUF
lda #$c4
sta BOTTOM_BAR_BUF + 31
lda #$c1
sta BOTTOM_BAR_BUF + 32
lda #$c5
sta BOTTOM_BAR_BUF + 63
lda #$c0
sta BOTTOM_BAR_BUF + 64
lda #$c6
sta BOTTOM_BAR_BUF + 95
ldx #1
lda #$c3
.next0:
sta BOTTOM_BAR_BUF, x
inx
cpx #31
bne .next0
ldx #65
lda #$c7
.next1:
sta BOTTOM_BAR_BUF, x
inx
cpx #95
bne .next1
ldy #0
sty tempI ; space indicator
ldx #33
.next2:
lda [ptrLo], y
bne .char ; not end of string
lda #$ff
dey
jmp .pad
.char:
cmp #$ff ; space
bne .nospace
sta tempI ; used later
.nospace:
cmp #$01 ; new line
beq .nl
cpx #63
beq .trim ; too long
jmp .store
.nl:
cpx #63
beq .nlpe ; new line padding end
lda #$ff
dey
jmp .store
.pad:
cpx #63
beq .no_trim
.store:
sta BOTTOM_BAR_BUF, x
iny
inx
jmp .next2
.nlpe:
iny
lda #1
pha
jmp .attrs
.no_trim:
lda #0
pha
jmp .attrs
.trim: ; trim a word
dey
lda tempI ; check if there was at least
beq .arrow ; a single space
ldx #62
.next3:
lda [ptrLo], y
cmp #$ff
beq .arrow
lda #$ff
sta BOTTOM_BAR_BUF, x
dex
dey
jmp .next3
.arrow:
lda #$fe
sta BOTTOM_BAR_BUF + 62
pha ; trimmed
.attrs:
; set attr table values to palette #0
tya ; save as we are going to change those
pha
lda ptrLo
pha
lda ptrHi
pha
lda nametable ; clear attr table part
clc
adc #$f0
sta ptrLo
lda nametable + 1
adc #$03
sta ptrHi
ldy #0
ldx #96
.next4:
lda [ptrLo], y
and #$0f
sta BOTTOM_BAR_BUF, x
iny
inx
cpx #104
bne .next4
ldx #104
lda #$00
.next5:
sta BOTTOM_BAR_BUF, x
inx
cpx #112
bne .next5
.done:
lda nmiBB ; set nmi flag
bne .done
lda #1
sta nmiBB
pla ; restore ptr
sta ptrHi
pla
sta ptrLo
pla ; restore y
clc ; calc next yx
adc ptrLo
tax
lda #0
adc ptrHi
tay
pla ; retval
rts
; move sprite right
; arg (p) - matrix address (saved)
; arg (x) - sprite structure low address (high is $03)
; ret (a) - btaaaaaa
MoveSpriteRight:
pha
tya
pha
txa
pha
lda $0301, x
tay
lda $0300, x
tax
jsr IsRightWalkable
tsx ; save result
sta $0103, x
and #%10000000
bne .wall
pla ; restore x
tax
lda $0300, x
clc ; inc x pos
adc #1
sta $0300, x
lda #%00011000 ; walk right
jmp .done
.wall:
pla ; restore x
tax
lda #%10011000 ; stand right
.done:
sta $0302, x
lda $0304, x
and #%10111111
sta $0304, x
pla
tay
pla ; result
rts
; move sprite left
; arg (p) - matrix address (saved)
; arg (x) - sprite structure low address (high is $03)
; ret (a) - btaaaaaa
MoveSpriteLeft:
pha
tya
pha
txa
pha
lda $0301, x
tay
lda $0300, x
tax
jsr IsLeftWalkable
tsx ; save result
sta $0103, x
and #%10000000
bne .wall
pla ; restore x
tax
lda $0300, x
clc ; dec x pos
adc #$ff
sta $0300, x
lda #%00011000 ; walk left
jmp .done
.wall:
pla ; restore x
tax
lda #%10011000 ; stand left
.done:
sta $0302, x
lda $0304, x
ora #%01000000
sta $0304, x
pla
tay
pla ; result
rts
; move sprite up
; arg (p) - matrix address (saved)
; arg (x) - sprite structure low address (high is $03)
; ret (a) - btaaaaaa
MoveSpriteUp:
pha
tya
pha
txa
pha
lda $0301, x
tay
lda $0300, x
tax
jsr IsUpWalkable
tsx ; save result
sta $0103, x
and #%10000000
bne .wall
pla ; restore x
tax
lda $0301, x
clc ; dec y pos
adc #$ff
sta $0301, x
lda #%00000000 ; walk up
jmp .done
.wall:
pla ; restore x
tax
lda #%10000000 ; stand up
.done:
sta $0302, x
lda $0304, x
and #%10111111
sta $0304, x
pla
tay
pla ; result
rts
; move sprite down
; arg (p) - matrix address (saved)
; arg (x) - sprite structure low address (high is $03)
; ret (a) - btaaaaaa
MoveSpriteDown:
pha
tya
pha
txa
pha
lda $0301, x
tay
lda $0300, x
tax
jsr IsDownWalkable
tsx ; save result
sta $0103, x
and #%10000000
bne .wall
pla ; restore x
tax
lda $0301, x
clc ; inc y pos
adc #1
sta $0301, x
lda #%00001100 ; walk down
jmp .done
.wall:
pla ; restore x
tax
lda #%10001100 ; stand down
.done:
sta $0302, x
lda $0304, x
and #%10111111
sta $0304, x
pla
tay
pla ; result
rts
; move the sprite randomly
; arg (p) - matrix address (saved)
; arg (x) - sprite structure low address (high is $03)
MoveSpriteRandomly:
pha
lda $0306, x
bne .step
jsr Random
and #%00000011
sta $0305, x ; mode
jsr Random
sta $0306, x ; steps
.step:
dec $0306, x ; dec steps counter