-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmapper1.asm
1907 lines (1620 loc) · 26.3 KB
/
mapper1.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
; This file is part of Pwn Adventure Z.
; Pwn Adventure Z is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; Pwn Adventure Z is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with Pwn Adventure Z. If not, see <http://www.gnu.org/licenses/>.
.include "defines.inc"
.segment "FIXED"
PROC write_mapper_8000
sta $8000
lsr
sta $8000
lsr
sta $8000
lsr
sta $8000
lsr
sta $8000
rts
.endproc
PROC write_mapper_a000
sta $a000
lsr
sta $a000
lsr
sta $a000
lsr
sta $a000
lsr
sta $a000
rts
.endproc
PROC write_mapper_e000
sta $e000
lsr
sta $e000
lsr
sta $e000
lsr
sta $e000
lsr
sta $e000
rts
.endproc
PROC reset_mapper
; Reset the mapper write state
lda #$80
sta $8000
; Init settings (8k CHR, 16k PRG, $8000 swap, vertical)
lda #$0e
jsr write_mapper_8000
; Set CHR select to 0, disable save RAM for some SNROM boards
lda #$10
jsr write_mapper_a000
; Map first bank into $8000, disable save RAM until accessing
lda #$10
jsr write_mapper_e000
rts
.endproc
PROC bankswitch
pha
lda save_ram_enabled
bne ramenabled
lda in_nmi
bne nmi
loop:
lda vblank_count
sta bankswitch_vblank_count
nmi:
lda #$80
sta $8000
pla
pha
ora #$10
jsr write_mapper_e000
lda in_nmi
bne done
lda vblank_count
cmp bankswitch_vblank_count
bne loop
done:
pla
rts
ramenabled:
lda in_nmi
bne ramnmi
ramloop:
lda vblank_count
sta bankswitch_vblank_count
ramnmi:
lda #$80
sta $8000
pla
pha
jsr write_mapper_e000
lda in_nmi
bne ramdone
lda vblank_count
cmp bankswitch_vblank_count
bne ramloop
ramdone:
pla
rts
.endproc
PROC has_save_ram
lda #1
rts
.endproc
PROC enable_save_ram
lda #1
sta save_ram_enabled
lda in_nmi
bne nmi
loop:
lda vblank_count
sta bankswitch_vblank_count
nmi:
; Enable RAM in both CHR and PRG select registers, as the original SNROM boards
; used CHR select bit 4 as a write protect as well
lda #$80
sta $8000
lda current_bank
jsr write_mapper_e000
lda #$00
jsr write_mapper_a000
lda in_nmi
bne done
lda vblank_count
cmp bankswitch_vblank_count
bne loop
done:
rts
.endproc
PROC disable_save_ram
lda #0
sta save_ram_enabled
lda in_nmi
bne nmi
loop:
lda vblank_count
sta bankswitch_vblank_count
nmi:
; Disable RAM in both CHR and PRG select registers, as the original SNROM boards
; used CHR select bit 4 as a write protect as well
lda #$80
sta $8000
lda current_bank
ora #$10
jsr write_mapper_e000
lda #$10
jsr write_mapper_a000
lda in_nmi
bne done
lda vblank_count
cmp bankswitch_vblank_count
bne loop
done:
rts
.endproc
PROC clear_save_header
ldy #0
lda #0
clearloop:
sta (ptr), y
iny
cpy #$20
bne clearloop
rts
.endproc
PROC clear_slot_0
jsr enable_save_ram
ldx #0
lda #0
clearloop:
sta $6300, x
sta $6400, x
sta $6500, x
sta $6d00, x
sta $6e00, x
sta $6f00, x
sta $7700, x
sta $7800, x
sta $7900, x
inx
bne clearloop
LOAD_PTR $6c00
jsr clear_save_header
LOAD_PTR $6ca0
jsr clear_save_header
LOAD_PTR $76a0
jsr clear_save_header
jsr disable_save_ram
rts
.endproc
PROC clear_slot_1
jsr enable_save_ram
ldx #0
lda #0
clearloop:
sta $6600, x
sta $6700, x
sta $6800, x
sta $7000, x
sta $7100, x
sta $7200, x
sta $7a00, x
sta $7b00, x
sta $7c00, x
inx
bne clearloop
LOAD_PTR $6c20
jsr clear_save_header
LOAD_PTR $6cc0
jsr clear_save_header
LOAD_PTR $76c0
jsr clear_save_header
jsr disable_save_ram
rts
.endproc
PROC clear_slot_2
jsr enable_save_ram
ldx #0
lda #0
clearloop:
sta $6900, x
sta $6a00, x
sta $6b00, x
sta $7300, x
sta $7400, x
sta $7500, x
sta $7d00, x
sta $7e00, x
sta $7f00, x
inx
bne clearloop
LOAD_PTR $6c40
jsr clear_save_header
LOAD_PTR $6ce0
jsr clear_save_header
LOAD_PTR $76e0
jsr clear_save_header
jsr disable_save_ram
rts
.endproc
PROC update_checksum
eor checksum
sta checksum
ldx #0
loop:
lda checksum
and #1
bne one
clc
ror checksum + 1
ror checksum
jmp next
one:
clc
ror checksum + 1
ror checksum
lda checksum
eor #1
sta checksum
lda checksum + 1
eor #$a0
sta checksum + 1
next:
inx
cpx #8
bne loop
rts
.endproc
PROC update_header_checksum
ldy #0
updateloop:
lda (ptr), y
jsr update_checksum
iny
cpy #SAVE_HEADER_CHECKSUM
bne updateloop
rts
.endproc
PROC write_header_checksum
lda checksum
sta (ptr), y
iny
lda checksum + 1
sta (ptr), y
rts
.endproc
PROC write_save_header
lda #'P'
ldy #0
sta (ptr), y
iny
lda #'w'
sta (ptr), y
iny
lda #'n'
sta (ptr), y
iny
lda #'Z'
sta (ptr), y
iny
ldx #0
nameloop:
lda name, x
sta (ptr), y
iny
inx
cpx #14
bne nameloop
lda difficulty
sta (ptr), y
iny
lda key_count
sta (ptr), y
iny
ldx #0
timeloop:
lda time_played, x
sta (ptr), y
iny
inx
cpx #6
bne timeloop
rts
.endproc
PROC save_ram_to_slot_0
jsr enable_save_ram
lda #0
sta checksum
sta checksum + 1
lda version_hash
jsr update_checksum
lda version_hash + 1
jsr update_checksum
lda version_hash + 2
jsr update_checksum
lda version_hash + 3
jsr update_checksum
ldy #0
saveloop1:
lda $0000, y
sta $6300, y
jsr update_checksum
lda $0300, y
sta $6400, y
jsr update_checksum
lda $0400, y
sta $6500, y
jsr update_checksum
iny
bne saveloop1
LOAD_PTR $6c00
jsr write_save_header
jsr update_header_checksum
jsr write_header_checksum
lda #0
sta checksum
sta checksum + 1
ldy #0
saveloop2:
lda $0000, y
sta $6d00, y
lda $0300, y
sta $6e00, y
lda $0400, y
sta $6f00, y
iny
bne saveloop2
LOAD_PTR $6ca0
jsr write_save_header
jsr write_header_checksum
lda #0
sta checksum
sta checksum + 1
ldy #0
saveloop3:
lda $0000, y
sta $7700, y
lda $0300, y
sta $7800, y
lda $0400, y
sta $7900, y
iny
bne saveloop3
LOAD_PTR $76a0
jsr write_save_header
jsr write_header_checksum
jsr disable_save_ram
rts
.endproc
PROC save_ram_to_slot_1
jsr enable_save_ram
lda #0
sta checksum
sta checksum + 1
lda version_hash
jsr update_checksum
lda version_hash + 1
jsr update_checksum
lda version_hash + 2
jsr update_checksum
lda version_hash + 3
jsr update_checksum
ldy #0
saveloop1:
lda $0000, y
sta $6600, y
jsr update_checksum
lda $0300, y
sta $6700, y
jsr update_checksum
lda $0400, y
sta $6800, y
jsr update_checksum
iny
bne saveloop1
LOAD_PTR $6c20
jsr write_save_header
jsr update_header_checksum
jsr write_header_checksum
lda #0
sta checksum
sta checksum + 1
ldy #0
saveloop2:
lda $0000, y
sta $7000, y
lda $0300, y
sta $7100, y
lda $0400, y
sta $7200, y
iny
bne saveloop2
LOAD_PTR $6cc0
jsr write_save_header
jsr write_header_checksum
lda #0
sta checksum
sta checksum + 1
ldy #0
saveloop3:
lda $0000, y
sta $7a00, y
lda $0300, y
sta $7b00, y
lda $0400, y
sta $7c00, y
iny
bne saveloop3
LOAD_PTR $76c0
jsr write_save_header
jsr write_header_checksum
jsr disable_save_ram
rts
.endproc
PROC save_ram_to_slot_2
jsr enable_save_ram
lda #0
sta checksum
sta checksum + 1
lda version_hash
jsr update_checksum
lda version_hash + 1
jsr update_checksum
lda version_hash + 2
jsr update_checksum
lda version_hash + 3
jsr update_checksum
ldy #0
saveloop1:
lda $0000, y
sta $6900, y
jsr update_checksum
lda $0300, y
sta $6a00, y
jsr update_checksum
lda $0400, y
sta $6b00, y
jsr update_checksum
iny
bne saveloop1
LOAD_PTR $6c40
jsr write_save_header
jsr update_header_checksum
jsr write_header_checksum
lda #0
sta checksum
sta checksum + 1
ldy #0
saveloop2:
lda $0000, y
sta $7300, y
lda $0300, y
sta $7400, y
lda $0400, y
sta $7500, y
iny
bne saveloop2
LOAD_PTR $6ce0
jsr write_save_header
jsr write_header_checksum
lda #0
sta checksum
sta checksum + 1
ldy #0
saveloop3:
lda $0000, y
sta $7d00, y
lda $0300, y
sta $7e00, y
lda $0400, y
sta $7f00, y
iny
bne saveloop3
LOAD_PTR $76e0
jsr write_save_header
jsr write_header_checksum
jsr disable_save_ram
rts
.endproc
PROC restore_ram_from_slot_0
jsr enable_save_ram
ldx #0
saveloop:
lda $6300, x
sta $0000, x
lda $6400, x
sta $0300, x
lda $6500, x
sta $0400, x
inx
bne saveloop
jsr disable_save_ram
rts
.endproc
PROC restore_ram_from_slot_1
jsr enable_save_ram
ldx #0
saveloop:
lda $6600, x
sta $0000, x
lda $6700, x
sta $0300, x
lda $6800, x
sta $0400, x
inx
bne saveloop
jsr disable_save_ram
rts
.endproc
PROC restore_ram_from_slot_2
jsr enable_save_ram
ldx #0
saveloop:
lda $6900, x
sta $0000, x
lda $6a00, x
sta $0300, x
lda $6b00, x
sta $0400, x
inx
bne saveloop
jsr disable_save_ram
rts
.endproc
PROC clear_slot
cmp #1
beq slot1
cmp #2
beq slot2
jmp clear_slot_0
slot1:
jmp clear_slot_1
slot2:
jmp clear_slot_2
.endproc
PROC save_ram_to_slot
cmp #1
beq slot1
cmp #2
beq slot2
jmp save_ram_to_slot_0
slot1:
jmp save_ram_to_slot_1
slot2:
jmp save_ram_to_slot_2
.endproc
PROC restore_ram_from_slot
cmp #1
beq slot1
cmp #2
beq slot2
jmp restore_ram_from_slot_0
slot1:
jmp restore_ram_from_slot_1
slot2:
jmp restore_ram_from_slot_2
.endproc
PROC is_save_slot_valid
pha
jsr enable_save_ram
pla
asl
asl
asl
asl
asl
tax
lda $6c00, x
cmp #'P'
bne notvalid
lda $6c01, x
cmp #'w'
bne notvalid
lda $6c02, x
cmp #'n'
bne notvalid
lda $6c03, x
cmp #'Z'
notvalid:
php
jsr disable_save_ram
plp
rts
.endproc
PROC validate_saves
lda current_bank
pha
lda #^do_validate_saves
jsr bankswitch
jsr do_validate_saves & $ffff
pla
jsr bankswitch
rts
.endproc
.segment "UI"
PROC do_validate_saves
; Check primary copy of save slot 0 for accuracy
jsr enable_save_ram
lda $6c00
cmp #'P'
bne save0aheadernotvalid
lda $6c01
cmp #'w'
bne save0aheadernotvalid
lda $6c02
cmp #'n'
bne save0aheadernotvalid
lda $6c03
cmp #'Z'
bne save0aheadernotvalid
jmp save0aheadervalid & $ffff
save0aheadernotvalid:
jmp save0anotvalid & $ffff
save0aheadervalid:
lda #0
sta checksum
sta checksum + 1
lda version_hash
jsr update_checksum
lda version_hash + 1
jsr update_checksum
lda version_hash + 2
jsr update_checksum
lda version_hash + 3
jsr update_checksum
ldy #0
checksumloop0a:
lda $6300, y
jsr update_checksum
lda $6400, y
jsr update_checksum
lda $6500, y
jsr update_checksum
iny
bne checksumloop0a
LOAD_PTR $6c00
jsr update_header_checksum
lda checksum
cmp $6c00 + SAVE_HEADER_CHECKSUM
bne save0anotvalid
lda checksum + 1
cmp $6c00 + SAVE_HEADER_CHECKSUM + 1
bne save0anotvalid
; First save is valid, copy to backup locations to ensure they are valid as well
ldy #0
copyloop0a:
lda $6300, y
sta $6d00, y
sta $7700, y
lda $6400, y
sta $6e00, y
sta $7800, y
lda $6500, y
sta $6f00, y
sta $7900, y
iny
bne copyloop0a
ldy #0
headerloop0a:
lda $6c00, y
sta $6ca0, y
sta $76a0, y
iny
cpy #$20
bne headerloop0a
jmp checksave1 & $ffff
save0anotvalid:
; Check second copy of save slot 0 for accuracy
lda $6ca0
cmp #'P'
bne save0bheadernotvalid
lda $6ca1
cmp #'w'
bne save0bheadernotvalid
lda $6ca2
cmp #'n'
bne save0bheadernotvalid
lda $6ca3
cmp #'Z'
bne save0bheadernotvalid
jmp save0bheadervalid & $ffff
save0bheadernotvalid:
jmp save0bnotvalid & $ffff
save0bheadervalid:
lda #0
sta checksum
sta checksum + 1
lda version_hash
jsr update_checksum
lda version_hash + 1
jsr update_checksum
lda version_hash + 2
jsr update_checksum
lda version_hash + 3
jsr update_checksum
ldy #0
checksumloop0b:
lda $6d00, y
jsr update_checksum
lda $6e00, y
jsr update_checksum
lda $6f00, y
jsr update_checksum
iny
bne checksumloop0b
LOAD_PTR $6ca0
jsr update_header_checksum
lda checksum
cmp $6ca0 + SAVE_HEADER_CHECKSUM
bne save0bnotvalid
lda checksum + 1
cmp $6ca0 + SAVE_HEADER_CHECKSUM + 1
bne save0bnotvalid
; Second save is valid, copy to backup locations to ensure they are valid as well
ldy #0
copyloop0b:
lda $6d00, y
sta $6300, y
sta $7700, y
lda $6e00, y
sta $6400, y
sta $7800, y
lda $6f00, y
sta $6500, y
sta $7900, y
iny
bne copyloop0b
ldy #0
headerloop0b:
lda $6ca0, y
sta $6c00, y
sta $76a0, y
iny
cpy #$20
bne headerloop0b
jmp checksave1 & $ffff
save0bnotvalid:
; Check third copy of save slot 0 for accuracy
lda $76a0
cmp #'P'
bne save0cheadernotvalid
lda $76a1
cmp #'w'
bne save0cheadernotvalid
lda $76a2
cmp #'n'
bne save0cheadernotvalid
lda $76a3
cmp #'Z'
bne save0cheadernotvalid
jmp save0cheadervalid & $ffff
save0cheadernotvalid:
jmp save0cnotvalid & $ffff
save0cheadervalid:
lda #0
sta checksum
sta checksum + 1
lda version_hash
jsr update_checksum
lda version_hash + 1
jsr update_checksum
lda version_hash + 2
jsr update_checksum
lda version_hash + 3
jsr update_checksum
ldy #0
checksumloop0c:
lda $7700, y
jsr update_checksum
lda $7800, y
jsr update_checksum
lda $7900, y
jsr update_checksum
iny
bne checksumloop0c
LOAD_PTR $76a0
jsr update_header_checksum
lda checksum
cmp $76a0 + SAVE_HEADER_CHECKSUM
bne save0cnotvalid
lda checksum + 1
cmp $76a0 + SAVE_HEADER_CHECKSUM + 1
bne save0cnotvalid
; Third save is valid, copy to backup locations to ensure they are valid as well
ldy #0
copyloop0c: