-
Notifications
You must be signed in to change notification settings - Fork 3
/
sonic.asm
8610 lines (7326 loc) · 239 KB
/
sonic.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
; =========================================================================
; | Sonic 1: Fixed |
; | Updated Sonic the Hedgehog Disassembly for Sega Mega Drive |
; =========================================================================
;
; Updated disassembly for AS by Clownacy and MarkeyJester
; Mods by RetroKoH, DeltaW, Hitaxas, Redhotsonic, and Mercury
; Additional Credits listed in README.md
;
; ===========================================================================
cpu 68000
ZoneCount = 6 ; discrete zones are: GHZ, MZ, SYZ, LZ, SLZ, and SBZ
zeroOffsetOptimization = 1 ; if 1, makes a handful of zero-offset instructions smaller
include "Mods.asm" ; S1Fixed Mod Variables (Sorted by Context)
include "MacroSetup.asm"
include "Constants.asm"
include "Variables.asm"
include "Macros.asm"
include "Debugger.asm"
; ===========================================================================
StartOfRom:
Vectors:
dc.l v_systemstack&$FFFFFF ; Initial stack pointer value
dc.l EntryPoint ; Start of program
dc.l BusError ; Bus error
dc.l AddressError ; Address error (4)
dc.l IllegalInstr ; Illegal instruction
dc.l ZeroDivide ; Division by zero
dc.l ChkInstr ; CHK exception
dc.l TrapvInstr ; TRAPV exception (8)
dc.l PrivilegeViol ; Privilege violation
dc.l Trace ; TRACE exception
dc.l Line1010Emu ; Line-A emulator
dc.l Line1111Emu ; Line-F emulator (12)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved) (16)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved) (20)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved)
dc.l ErrorExcept ; Unused (reserved) (24)
dc.l ErrorExcept ; Spurious exception
dc.l ErrorTrap ; IRQ level 1
dc.l ErrorTrap ; IRQ level 2
dc.l ErrorTrap ; IRQ level 3 (28)
dc.l HBlank ; IRQ level 4 (horizontal retrace interrupt)
dc.l ErrorTrap ; IRQ level 5
dc.l VBlank ; IRQ level 6 (vertical retrace interrupt)
dc.l ErrorTrap ; IRQ level 7 (32)
dc.l ErrorTrap ; TRAP #00 exception
dc.l ErrorTrap ; TRAP #01 exception
dc.l ErrorTrap ; TRAP #02 exception
dc.l ErrorTrap ; TRAP #03 exception (36)
dc.l ErrorTrap ; TRAP #04 exception
dc.l ErrorTrap ; TRAP #05 exception
dc.l ErrorTrap ; TRAP #06 exception
dc.l ErrorTrap ; TRAP #07 exception (40)
dc.l ErrorTrap ; TRAP #08 exception
dc.l ErrorTrap ; TRAP #09 exception
dc.l ErrorTrap ; TRAP #10 exception
dc.l ErrorTrap ; TRAP #11 exception (44)
dc.l ErrorTrap ; TRAP #12 exception
dc.l ErrorTrap ; TRAP #13 exception
dc.l ErrorTrap ; TRAP #14 exception
dc.l ErrorTrap ; TRAP #15 exception (48)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.l ErrorTrap ; Unused (reserved)
dc.b "SEGA MEGA DRIVE " ; Hardware system ID (Console name)
dc.b "(C)SEGA 1991.APR" ; Copyright holder and release date (generally year)
dc.b "SONIC THE HEDGEHOG " ; Domestic name
dc.b "SONIC THE HEDGEHOG " ; International name
dc.b "GM 00004049-01" ; Serial/version number (Rev non-0)
Checksum:
dc.w $7469 ; Hardcoded to make it easier to check for ROM correctness
dc.b "J " ; I/O support
dc.l StartOfRom ; Start address of ROM
RomEndLoc:
dc.l EndOfRom-1 ; End address of ROM
dc.l $FF0000 ; Start address of RAM
dc.l $FFFFFF ; End address of RAM
if EnableSRAM=1
dc.b $52, $41, $A0+(BackupSRAM<<6)+(AddressSRAM<<3), $20 ; SRAM support
dc.l $00200000 ; SRAM start ($200001)
dc.l $002001FF ; SRAM end ($20xxxx)
else
dc.l $20202020
dc.l $20202020 ; SRAM start ($200001)
dc.l $20202020 ; SRAM end ($20xxxx)
endif
dc.b " " ; Notes (unused, anything can be put in this space, but it has to be 52 bytes.)
dc.b "JUE " ; Region (Country code)
EndOfHeader:
; ===========================================================================
; Crash/Freeze the 68000. Unlike Sonic 2, Sonic 1 uses the 68000 for playing music, so it stops too
ErrorTrap:
nop
nop
bra.s ErrorTrap
; ===========================================================================
EntryPoint:
tst.l (z80_port_1_control).l ; test port A & B control registers
bne.s PortA_Ok
tst.w (z80_expansion_control).l ; test port C control register
PortA_Ok:
bne.s SkipSetup ; Skip the VDP and Z80 setup code if port A, B or C is ok...?
lea SetupValues(pc),a5 ; Load setup values array address.
movem.w (a5)+,d5-d7
movem.l (a5)+,a0-a4
move.b -$10FF(a1),d0 ; get hardware version (from $A10001)
andi.b #$F,d0
beq.s SkipSecurity ; If the console has no TMSS, skip the security stuff.
move.l #'SEGA',$2F00(a1) ; move "SEGA" to TMSS register ($A14000)
SkipSecurity:
move.w (a4),d0 ; clear write-pending flag in VDP to prevent issues if the 68k has been reset in the middle of writing a command long word to the VDP.
moveq #0,d0 ; clear d0
movea.l d0,a6 ; clear a6
move.l a6,usp ; set usp to $0
moveq #$17,d1
VDPInitLoop:
move.b (a5)+,d5 ; add $8000 to value
move.w d5,(a4) ; move value to VDP register
add.w d7,d5 ; next register
dbf d1,VDPInitLoop
move.l (a5)+,(a4)
move.w d0,(a3) ; clear the VRAM
move.w d7,(a1) ; stop the Z80
move.w d7,(a2) ; reset the Z80
WaitForZ80:
btst d0,(a1) ; has the Z80 stopped?
bne.s WaitForZ80 ; if not, branch
moveq #$25,d2
Z80InitLoop:
move.b (a5)+,(a0)+
dbf d2,Z80InitLoop
move.w d0,(a2)
move.w d0,(a1) ; start the Z80
move.w d7,(a2) ; reset the Z80
ClrRAMLoop:
move.l d0,-(a6) ; clear 4 bytes of RAM
dbf d6,ClrRAMLoop ; repeat until the entire RAM is clear
move.l (a5)+,(a4) ; set VDP display mode and increment mode
move.l (a5)+,(a4) ; set VDP to CRAM write
moveq #$1F,d3 ; set repeat times
ClrCRAMLoop:
move.l d0,(a3) ; clear 2 palettes
dbf d3,ClrCRAMLoop ; repeat until the entire CRAM is clear
move.l (a5)+,(a4) ; set VDP to VSRAM write
moveq #$13,d4
ClrVSRAMLoop:
move.l d0,(a3) ; clear 4 bytes of VSRAM.
dbf d4,ClrVSRAMLoop ; repeat until the entire VSRAM is clear
moveq #3,d5
PSGInitLoop:
move.b (a5)+,$11(a3) ; reset the PSG
dbf d5,PSGInitLoop ; repeat for other channels
move.w d0,(a2)
movem.l (a6),d0-a6 ; clear all registers
disable_ints
SkipSetup:
bra.s GameProgram ; begin game
; ===========================================================================
SetupValues: dc.w $8000 ; VDP register start number
dc.w $3FFF ; size of RAM/4
dc.w $100 ; VDP register diff
dc.l z80_ram ; start of Z80 RAM
dc.l z80_bus_request ; Z80 bus request
dc.l z80_reset ; Z80 reset
dc.l vdp_data_port ; VDP data
dc.l vdp_control_port ; VDP control
dc.b 4 ; VDP $80 - 8-colour mode
dc.b $14 ; VDP $81 - Megadrive mode, DMA enable
dc.b ($C000>>10) ; VDP $82 - foreground nametable address
dc.b ($F000>>10) ; VDP $83 - window nametable address
dc.b ($E000>>13) ; VDP $84 - background nametable address
dc.b ($D800>>9) ; VDP $85 - sprite table address
dc.b 0 ; VDP $86 - unused
dc.b 0 ; VDP $87 - background colour
dc.b 0 ; VDP $88 - unused
dc.b 0 ; VDP $89 - unused
dc.b 255 ; VDP $8A - HBlank register
dc.b 0 ; VDP $8B - full screen scroll
dc.b $81 ; VDP $8C - 40 cell display
dc.b ($DC00>>10) ; VDP $8D - hscroll table address
dc.b 0 ; VDP $8E - unused
dc.b 1 ; VDP $8F - VDP increment
dc.b 1 ; VDP $90 - 64 cell hscroll size
dc.b 0 ; VDP $91 - window h position
dc.b 0 ; VDP $92 - window v position
dc.w $FFFF ; VDP $93/94 - DMA length
dc.w 0 ; VDP $95/96 - DMA source
dc.b $80 ; VDP $97 - DMA fill VRAM
dc.l $40000080 ; VRAM address 0
; Z80 instructions (not the sound driver; that gets loaded later)
if (*)+$26 < $10000
save
CPU Z80 ; start assembling Z80 code
phase 0 ; pretend we're at address 0
xor a ; clear a to 0
ld bc,((z80_ram_end-z80_ram)-zStartupCodeEndLoc)-1 ; prepare to loop this many times
ld de,zStartupCodeEndLoc+1 ; initial destination address
ld hl,zStartupCodeEndLoc ; initial source address
ld sp,hl ; set the address the stack starts at
ld (hl),a ; set first byte of the stack to 0
ldir ; loop to fill the stack (entire remaining available Z80 RAM) with 0
pop ix ; clear ix
pop iy ; clear iy
ld i,a ; clear i
ld r,a ; clear r
pop de ; clear de
pop hl ; clear hl
pop af ; clear af
ex af,af' ; swap af with af'
exx ; swap bc/de/hl with their shadow registers too
pop bc ; clear bc
pop de ; clear de
pop hl ; clear hl
pop af ; clear af
ld sp,hl ; clear sp
di ; clear iff1 (for interrupt handler)
im 1 ; interrupt handling mode = 1
ld (hl),0E9h ; replace the first instruction with a jump to itself
jp (hl) ; jump to the first instruction (to stay there forever)
zStartupCodeEndLoc:
dephase ; stop pretending
restore
padding off ; unfortunately our flags got reset so we have to set them again...
else ; due to an address range limitation I could work around but don't think is worth doing so:
message "Warning: using pre-assembled Z80 startup code."
dc.w $AF01,$D91F,$1127,$0021,$2600,$F977,$EDB0,$DDE1,$FDE1,$ED47,$ED4F,$D1E1,$F108,$D9C1,$D1E1,$F1F9,$F3ED,$5636,$E9E9
endif
dc.w $8104 ; VDP display mode
dc.w $8F02 ; VDP increment
dc.l $C0000000 ; CRAM write mode
dc.l $40000010 ; VSRAM address 0
dc.b $9F, $BF, $DF, $FF ; values for PSG channel volumes
; ===========================================================================
GameProgram:
tst.w (vdp_control_port).l
btst #6,(z80_expansion_control+1).l
beq.s CheckSumCheck
cmpi.l #'init',(v_init).w ; has checksum routine already run?
beq.w GameInit ; if yes, branch
CheckSumCheck: ; FASTER CHECKSUM CHECK BY MARKEYJESTER
movea.w #$0200,a0 ; prepare start address
move.l (RomEndLoc).w,d7 ; load size
sub.l a0,d7 ; minus start address
move.b d7,d5 ; copy end nybble
andi.w #$000F,d5 ; get only the remaining nybble
lsr.l #$04,d7 ; divide the size by 20
move.w d7,d6 ; load lower word size
swap d7 ; get upper word size
moveq #$00,d0 ; clear d0
CS_MainBlock:
add.w (a0)+,d0 ; modular checksum (8 words)
add.w (a0)+,d0 ; ''
add.w (a0)+,d0 ; ''
add.w (a0)+,d0 ; ''
add.w (a0)+,d0 ; ''
add.w (a0)+,d0 ; ''
add.w (a0)+,d0 ; ''
add.w (a0)+,d0 ; ''
dbf d6,CS_MainBlock ; repeat until all main block sections are done
dbf d7,CS_MainBlock ; ''
subq.w #$01,d5 ; decrease remaining nybble for dbf
bpl.s CS_Finish ; if there is no remaining nybble, branch
CS_Remains:
add.w (a0)+,d0 ; add remaining words
dbf d5,CS_Remains ; repeat until the remaining words are done
CS_Finish:
; cmp.w (Checksum).w,d0 ; does the checksum match?
; bne.w CheckSumError ; if not, branch
CheckSumOk:
lea (v_crossresetram).w,a6
moveq #0,d7
move.w #(v_ram_end-v_crossresetram)/4-1,d6
.clearRAM:
move.l d7,(a6)+
dbf d6,.clearRAM ; clear RAM ($FE00-$FFFF)
move.b (z80_version).l,d0
andi.b #$C0,d0
move.b d0,(v_megadrive).w ; get region setting
move.l #'init',(v_init).w ; set flag so checksum won't run again
GameInit:
lea (v_ram_start&$FFFFFF).l,a6
moveq #0,d7
move.w #(v_crossresetram-v_ram_start)/4-1,d6
.clearRAM:
move.l d7,(a6)+
dbf d6,.clearRAM ; clear RAM ($0000-$FDFF)
jsr (InitDMAQueue).l ; Flamewing Ultra DMA Queue
bsr.w VDPSetupGame
bsr.w DACDriverLoad
bsr.w JoypadInit
move.b #id_Sega,(v_gamemode).w ; set Game Mode to Sega Screen
if SaveProgressMod=1
InitSRAM:
move.b #1,(sram_port).l ; Enable SRAM writing
if AddressSRAM=3
; no need to change this by yourself anymore -- Starleaf
lea ($200001).l,a0 ; Load SRAM memory into a0
else
lea ($200000).l,a0 ; Load SRAM memory into a0
endif
movep.l 0(a0),d0 ; Get the existing string at the start of SRAM
move.l #"SRAM",d1 ; Write the string "SRAM" to d1
cmp.l d0,d1 ; Was it already in SRAM?
beq.s .skip ; If so, skip
movep.l d1,0(a0) ; Write string "SRAM"
; Here is where you initialize values like lives or level. If you're using 8 bit values, you can only use every other byte.
; Example - 8(a0) => $A(a0)
.skip:
move.b #0,(sram_port).l ; Disable SRAM writing
endif
MainGameLoop:
moveq #0,d0 ; clear d0 before using it w/ the new Game Mode system to avoid bugs
move.b (v_gamemode).w,d0 ; load Game Mode
if NewLevelSelect
cmpi.b #$20,d0 ; limit Game Mode value to $20 max
ble.s .dontcap
clr.b d0
.dontcap:
else
andi.w #$1C,d0 ; limit Game Mode value to $1C max
endif
movea.l GameModeArray(pc,d0.w),a0 ; load location of game mode to a0
jsr (a0) ; jump to apt location in ROM -- RetroKoH S3K Game Mode Array
bra.s MainGameLoop ; loop indefinitely
; ===========================================================================
; ---------------------------------------------------------------------------
; Main game mode array -- RetroKoH S3K Game Mode Array
; ---------------------------------------------------------------------------
GameModeArray:
ptr_GM_Sega: dc.l GM_Sega ; Sega Screen ($00)
ptr_GM_Title: dc.l GM_Title ; Title Screen ($04)
ptr_GM_Demo: dc.l GM_Level ; Demo Mode ($08)
ptr_GM_Level: dc.l GM_Level ; Normal Level ($0C)
ptr_GM_Special: dc.l GM_Special ; Special Stage ($10)
ptr_GM_Cont: dc.l GM_Continue ; Continue Screen ($14)
ptr_GM_Ending: dc.l GM_Ending ; End of game sequence ($18)
ptr_GM_Credits: dc.l GM_Credits ; Credits ($1C)
if NewLevelSelect
ptr_GM_MenuScreen: dc.l GM_MenuScreen ; NEW Sonic 2 style Level Select ($20)
endif
; ===========================================================================
CheckSumError:
bsr.w VDPSetupGame
move.l #$C0000000,(vdp_control_port).l ; set VDP to CRAM write
moveq #$3F,d7
.fillred:
move.w #cRed,(vdp_data_port).l ; fill palette with red
dbf d7,.fillred ; repeat $3F more times
.endlessloop:
bra.s .endlessloop
; ===========================================================================
; Soulless Sentinel Level Select ASCII Mod
Art_Text: binclude "artunc/Menu Text.bin" ; text used in level select and debug mode
Art_Text_End: even
; ===========================================================================
; ---------------------------------------------------------------------------
; Vertical interrupt
; ---------------------------------------------------------------------------
VBlank:
movem.l d0-a6,-(sp)
tst.b (v_vbla_routine).w
beq.s VBla_00
move.w (vdp_control_port).l,d0
move.l #$40000010,(vdp_control_port).l
move.l (v_scrposy_vdp).w,(vdp_data_port).l ; send screen y-axis pos. to VSRAM
btst #6,(v_megadrive).w ; is Megadrive PAL?
beq.s .notPAL ; if not, branch
move.w #$700,d0
.waitPAL:
dbf d0,.waitPAL ; wait here in a loop doing nothing for a while...
.notPAL:
move.b (v_vbla_routine).w,d0
move.b #0,(v_vbla_routine).w
move.w #1,(f_hbla_pal).w
andi.w #$3E,d0
move.w VBla_Index(pc,d0.w),d0
jsr VBla_Index(pc,d0.w)
VBla_Music:
jsr (UpdateMusic).l
VBla_Exit:
addq.l #1,(v_vbla_count).w
movem.l (sp)+,d0-a6
rte
; ===========================================================================
VBla_Index: offsetTable
offsetTableEntry.w VBla_00
offsetTableEntry.w VBla_02
offsetTableEntry.w VBla_04
offsetTableEntry.w VBla_06
offsetTableEntry.w VBla_08
offsetTableEntry.w VBla_0A
offsetTableEntry.w VBla_0C
offsetTableEntry.w VBla_0E
offsetTableEntry.w VBla_10
offsetTableEntry.w VBla_12
offsetTableEntry.w VBla_14
offsetTableEntry.w VBla_16
offsetTableEntry.w VBla_0C
; ===========================================================================
VBla_00:
cmpi.b #$80+id_Level,(v_gamemode).w
beq.s .islevel
cmpi.b #id_Level,(v_gamemode).w ; is game on a level?
bne.s VBla_Music ; if not, branch
.islevel:
cmpi.b #id_LZ,(v_zone).w ; is level LZ ?
bne.s VBla_Music ; if not, branch
move.w (vdp_control_port).l,d0
btst #6,(v_megadrive).w ; is Megadrive PAL?
beq.s .notPAL ; if not, branch
move.w #$700,d0
.waitPAL:
dbf d0,.waitPAL
.notPAL:
move.w #1,(f_hbla_pal).w ; set HBlank flag
stopZ80
waitZ80
tst.b (f_wtr_state).w ; is water above top of screen?
bne.s .waterabove ; if yes, branch
writeCRAM v_palette,0
bra.s .waterbelow
.waterabove:
writeCRAM v_palette_water,0
.waterbelow:
move.w (v_hbla_hreg).w,(a5)
startZ80
; instead of branching back to VBla_Music, call directly.
jsr (UpdateMusic).l
addq.l #1,(v_vbla_count).w
movem.l (sp)+,d0-a6
rte
; ===========================================================================
VBla_02:
bsr.w sub_106E
VBla_14:
tst.w (v_demolength).w
beq.w .end
subq.w #1,(v_demolength).w
.end:
rts
; ===========================================================================
VBla_04:
bsr.w sub_106E
bsr.w LoadTilesAsYouMove_BGOnly
bsr.w sub_1642
tst.w (v_demolength).w
beq.w .end
subq.w #1,(v_demolength).w
.end:
rts
; ===========================================================================
VBla_06:
bsr.w sub_106E
rts
; ===========================================================================
VBla_10:
cmpi.b #id_Special,(v_gamemode).w ; is game on special stage?
beq.w VBla_0A ; if yes, branch
VBla_08:
stopZ80
waitZ80
bsr.w ReadJoypads
tst.b (f_wtr_state).w
bne.s .waterabove
writeCRAM v_palette,0
bra.s .waterbelow
.waterabove:
writeCRAM v_palette_water,0
.waterbelow:
move.w (v_hbla_hreg).w,(a5)
writeVRAM v_hscrolltablebuffer,vram_hscroll
writeVRAM v_spritetablebuffer,vram_sprites
bsr.w ProcessDMAQueue ; Mercury Use DMA Queue
startZ80
movem.l (v_screenposx).w,d0-d7
movem.l d0-d7,(v_screenposx_dup).w
movem.l (v_fg_scroll_flags).w,d0-d1
movem.l d0-d1,(v_fg_scroll_flags_dup).w
cmpi.b #96,(v_hbla_line).w
bhs.s Demo_Time
move.b #1,(f_doupdatesinhblank).w
addq.l #4,sp
bra.w VBla_Exit
; ---------------------------------------------------------------------------
; Subroutine to run a demo for an amount of time
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
Demo_Time:
bsr.w LoadTilesAsYouMove
; jsr (AnimateLevelGfx).l
jsr (HUD_Update).l
bsr.w ProcessDPLC2
tst.w (v_demolength).w ; is there time left on the demo?
beq.w .end ; if not, branch
subq.w #1,(v_demolength).w ; subtract 1 from time left
.end:
rts
; End of function Demo_Time
; ===========================================================================
VBla_0A:
stopZ80
waitZ80
bsr.w ReadJoypads
writeCRAM v_palette,0
writeVRAM v_spritetablebuffer,vram_sprites
writeVRAM v_hscrolltablebuffer,vram_hscroll
startZ80
bsr.w PalCycle_SS
bsr.w ProcessDMAQueue ; Mercury Use DMA Queue
if DynamicSpecialStageWalls=1 ; Mercury Dynamic Special Stage Walls
cmpi.b #96,(v_hbla_line).w
bcc.s .update
bra.s .end
.update:
jsr (SS_LoadWalls).l
if HUDInSpecialStage=1 ; Mercury HUD in Special Stage
jsr (HUD_Update_SS).l
endif ; HUD in Special Stage End
endif ; Dynamic Special Stage Walls End
tst.w (v_demolength).w ; is there time left on the demo?
beq.s .end ; if not, return
subq.w #1,(v_demolength).w ; subtract 1 from time left in demo
.end:
rts
; ===========================================================================
VBla_0C:
stopZ80
waitZ80
bsr.w ReadJoypads
tst.b (f_wtr_state).w
bne.s .waterabove
writeCRAM v_palette,0
bra.s .waterbelow
.waterabove:
writeCRAM v_palette_water,0
.waterbelow:
move.w (v_hbla_hreg).w,(a5)
writeVRAM v_hscrolltablebuffer,vram_hscroll
writeVRAM v_spritetablebuffer,vram_sprites
bsr.w ProcessDMAQueue ; Mercury Use DMA Queue
startZ80
movem.l (v_screenposx).w,d0-d7
movem.l d0-d7,(v_screenposx_dup).w
movem.l (v_fg_scroll_flags).w,d0-d1
movem.l d0-d1,(v_fg_scroll_flags_dup).w
bsr.w LoadTilesAsYouMove
; jsr (AnimateLevelGfx).l
jsr (HUD_Update).l
bra.w sub_1642
; ===========================================================================
VBla_0E:
bsr.w sub_106E
addq.b #1,(v_vbla_0e_counter).w ; Unused besides this one write...
move.b #$E,(v_vbla_routine).w
rts
; ===========================================================================
VBla_12:
bsr.w sub_106E
move.w (v_hbla_hreg).w,(a5)
bra.w sub_1642
; ===========================================================================
VBla_16:
stopZ80
waitZ80
bsr.w ReadJoypads
writeCRAM v_palette,0
writeVRAM v_spritetablebuffer,vram_sprites
writeVRAM v_hscrolltablebuffer,vram_hscroll
startZ80
bsr.w ProcessDMAQueue ; Mercury Use DMA Queue
if DynamicSpecialStageWalls=1 ; Mercury Dynamic Special Stage Walls
cmpi.b #96,(v_hbla_line).w
bcc.s .update
bra.s .end
.update:
jsr (SS_LoadWalls).l
if HUDInSpecialStage=1 ; Mercury HUD in Special Stage
jsr (HUD_Update_SS).l
endif ; HUD in Special Stage End
endif ; Dynamic Special Stage Walls End
tst.w (v_demolength).w
beq.s .end
subq.w #1,(v_demolength).w
.end:
rts
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
sub_106E:
stopZ80
waitZ80
bsr.w ReadJoypads
tst.b (f_wtr_state).w ; is water above top of screen?
bne.s .waterabove ; if yes, branch
writeCRAM v_palette,0
bra.s .waterbelow
.waterabove:
writeCRAM v_palette_water,0
.waterbelow:
writeVRAM v_spritetablebuffer,vram_sprites
writeVRAM v_hscrolltablebuffer,vram_hscroll
startZ80
bra.w ProcessDMAQueue ; Mercury Use DMA Queue
rts
; End of function sub_106E
; ---------------------------------------------------------------------------
; Horizontal interrupt
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
HBlank:
disable_ints
tst.w (f_hbla_pal).w ; is palette set to change?
beq.s .nochg ; if not, branch
move.w #0,(f_hbla_pal).w
movem.l a0-a1,-(sp)
lea (vdp_data_port).l,a1
lea (v_palette_water).w,a0 ; get palette from RAM
move.l #$C0000000,4(a1) ; set VDP to CRAM write
rept 32
move.l (a0)+,(a1) ; move palette to CRAM
endm
move.w #$8A00+223,4(a1) ; reset HBlank register
movem.l (sp)+,a0-a1
tst.b (f_doupdatesinhblank).w
bne.s loc_119E
.nochg:
rte
; ===========================================================================
loc_119E:
move.b #0,(f_doupdatesinhblank).w
movem.l d0-a6,-(sp)
bsr.w Demo_Time
jsr (UpdateMusic).l
movem.l (sp)+,d0-a6
rte
; End of function HBlank
; ---------------------------------------------------------------------------
; Subroutine to initialise joypads
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
JoypadInit:
stopZ80
waitZ80
moveq #$40,d0
move.b d0,(z80_port_1_control+1).l ; init port 1 (joypad 1)
move.b d0,(z80_port_2_control+1).l ; init port 2 (joypad 2)
move.b d0,(z80_expansion_control+1).l ; init port 3 (expansion/extra)
startZ80
rts
; End of function JoypadInit
; ---------------------------------------------------------------------------
; Subroutine to read joypad input, and send it to the RAM
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
ReadJoypads:
lea (v_jpadhold1).w,a0 ; address where joypad states are written
lea (z80_port_1_data+1).l,a1 ; first joypad port
bsr.s .read ; do the first joypad
addq.w #2,a1 ; do the second joypad
.read:
move.b #0,(a1) ; poll joypad data port
nop
nop
move.b (a1),d0 ; get joypad port data (start/A)
asl.b #2,d0
move.b #$40,(a1) ; poll joypad data port again
andi.w #$C0,d0
move.b (a1),d1 ; get joypad port data (B/C/Dpad)
andi.w #$3F,d1
or.b d1,d0 ; fuse together into one joypad bit array
not.b d0
move.b (a0),d1 ; get press button data
eor.b d0,d1 ; toggle off buttons that are being held
move.b d0,(a0)+ ; put raw joypad input (for held buttons) in F604/F606
and.b d0,d1
move.b d1,(a0)+ ; put pressed controller input in F605/F607
rts
; End of function ReadJoypads
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
VDPSetupGame:
lea (vdp_control_port).l,a0
lea (vdp_data_port).l,a1
lea VDPSetupArray(pc),a2
moveq #$12,d7
.setreg:
move.w (a2)+,(a0)
dbf d7,.setreg ; set the VDP registers
move.w VDPSetupArray+2(pc),(v_vdp_buffer1).w ; Saves 8 cycles
move.w #$8A00+223,(v_hbla_hreg).w ; H-INT every 224th scanline
moveq #0,d0
move.l #$C0000000,(vdp_control_port).l ; set VDP to CRAM write
moveq #$1F,d7
.clrCRAM:
move.l d0,(a1) ; clear longwords instead of words (384 cycles vs. 512)
dbf d7,.clrCRAM ; clear the CRAM
clr.l (v_scrposy_vdp).w
clr.l (v_scrposx_vdp).w
move.l d1,-(sp)
fillVRAM 0,0,$10000 ; clear the entirety of VRAM
move.l (sp)+,d1
rts
; End of function VDPSetupGame
; ===========================================================================
VDPSetupArray:
dc.w $8004 ; 8-colour mode
dc.w $8134 ; enable V.interrupts, enable DMA
dc.w $8200+(vram_fg>>10) ; set foreground nametable address
dc.w $8300+($A000>>10) ; set window nametable address
dc.w $8400+(vram_bg>>13) ; set background nametable address
dc.w $8500+(vram_sprites>>9) ; set sprite table address
dc.w $8600 ; unused
dc.w $8700 ; set background colour (palette entry 0)
dc.w $8800 ; unused
dc.w $8900 ; unused
dc.w $8A00 ; default H.interrupt register
dc.w $8B00 ; full-screen vertical scrolling
dc.w $8C81 ; 40-cell display mode
dc.w $8D00+(vram_hscroll>>10) ; set background hscroll address
dc.w $8E00 ; unused
dc.w $8F02 ; set VDP increment size
dc.w $9001 ; 64-cell hscroll size
dc.w $9100 ; window horizontal position
dc.w $9200 ; window vertical position
; ---------------------------------------------------------------------------
; Subroutine to clear the screen
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
ClearScreen:
fillVRAM 0, vram_fg, vram_fg+plane_size_64x32 ; clear foreground namespace
fillVRAM 0, vram_bg, vram_bg+plane_size_64x32 ; clear background namespace
clr.l (v_scrposy_vdp).w
clr.l (v_scrposx_vdp).w
; Fixed
clearRAM v_spritetablebuffer
clearRAM v_hscrolltablebuffer,v_hscrolltablebuffer_end_padded
rts
; End of function ClearScreen
; ---------------------------------------------------------------------------
; Subroutine to load the DAC driver
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; SoundDriverLoad:
DACDriverLoad:
nop
stopZ80
resetZ80
lea (DACDriver).l,a0 ; load DAC driver
lea (z80_ram).l,a1 ; target Z80 RAM
bsr.w KosDec ; decompress
resetZ80a
nop
nop
nop
nop
resetZ80
startZ80
rts
; End of function DACDriverLoad
include "_incObj/sub PlaySound.asm"
include "_inc/PauseGame.asm"
; ---------------------------------------------------------------------------
; Subroutine to copy a tile map from RAM to VRAM namespace
; input:
; a1 = tile map address
; d0 = VRAM address
; d1 = width (cells)
; d2 = height (cells)
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
TilemapToVRAM:
lea (vdp_data_port).l,a6
move.l #$800000,d4
Tilemap_Line:
move.l d0,4(a6) ; move d0 to VDP_control_port
move.w d1,d3
Tilemap_Cell:
move.w (a1)+,(a6) ; write value to namespace
dbf d3,Tilemap_Cell ; next tile
add.l d4,d0 ; goto next line
dbf d2,Tilemap_Line ; next line
rts
; End of function TilemapToVRAM
include "_inc/DMA Queue.asm"
include "_inc/Nemesis Decompression.asm"
; ---------------------------------------------------------------
; uncompressed art to VRAM loader -- AURORA☆FIELDS Title Card Optimization
; ---------------------------------------------------------------
; INPUT:
; a0 - Source Offset
; d0 - length in tiles
; ---------------------------------------------------------------
LoadUncArt:
disable_ints
lea $C00000.l,a6 ; get VDP data port
LoadArt_Loop:
move.l (a0)+,(a6) ; transfer 4 bytes
move.l (a0)+,(a6) ; transfer 4 more bytes
move.l (a0)+,(a6) ; and so on and so forth
move.l (a0)+,(a6) ;
move.l (a0)+,(a6) ;
move.l (a0)+,(a6) ;
move.l (a0)+,(a6) ; in total transfer 32 bytes
move.l (a0)+,(a6) ; which is 1 full tile
dbf d0,LoadArt_Loop ; loop until d0 = 0
enable_ints
rts
; ===========================================================================
; ---------------------------------------------------------------------------
; Subroutine to load pattern load cues (aka to queue pattern load requests)
; ---------------------------------------------------------------------------
; ARGUMENTS
; d0 = index of PLC list
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; LoadPLC:
AddPLC: