-
Notifications
You must be signed in to change notification settings - Fork 97
/
sonic.asm
9355 lines (8083 loc) · 249 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 the Hedgehog Disassembly for Sega Mega Drive |
; =========================================================================
;
; Disassembly created by Hivebrain
; thanks to drx, Stealth and Esrael L.G. Neto
; ===========================================================================
cpu 68000
EnableSRAM = 0 ; change to 1 to enable SRAM
BackupSRAM = 1
AddressSRAM = 3 ; 0 = odd+even; 2 = even only; 3 = odd only
; Change to 0 to build the original version of the game, dubbed REV00
; Change to 1 to build the later vesion, dubbed REV01, which includes various bugfixes and enhancements
; Change to 2 to build the version from Sonic Mega Collection, dubbed REVXB, which fixes the infamous "spike bug"
Revision = 1
ZoneCount = 6 ; discrete zones are: GHZ, MZ, SYZ, LZ, SLZ, and SBZ
FixBugs = 0 ; change to 1 to enable bugfixes
zeroOffsetOptimization = 0 ; if 1, makes a handful of zero-offset instructions smaller
include "MacroSetup.asm"
include "Constants.asm"
include "Variables.asm"
include "Macros.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)
if Revision<>2
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)
else
loc_E0:
; Relocated code from Spik_Hurt. REVXB was a nasty hex-edit.
move.l obY(a0),d3
move.w obVelY(a0),d0
ext.l d0
asl.l #8,d0
jmp (loc_D5A2).l
dc.w ErrorTrap
dc.l ErrorTrap
dc.l ErrorTrap
dc.l ErrorTrap
endif
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
if Revision=0
dc.b "GM 00001009-00" ; Serial/version number (Rev 0)
else
dc.b "GM 00004049-01" ; Serial/version number (Rev non-0)
endif
Checksum:
if Revision=0
dc.w $264A ; Hardcoded to make it easier to check for ROM correctness
else
dc.w $AFC7
endif
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
else
dc.l $20202020
endif
dc.l $20202020 ; SRAM start ($200001)
dc.l $20202020 ; SRAM end ($20xxxx)
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:
movea.l #EndOfHeader,a0 ; start checking bytes after the header ($200)
movea.l #RomEndLoc,a1 ; stop at end of ROM
move.l (a1),d0
moveq #0,d1
.loop:
add.w (a0)+,d1
cmp.l a0,d0
bhs.s .loop
movea.l #Checksum,a1 ; read the checksum
cmp.w (a1),d1 ; compare checksum in header to ROM
bne.w CheckSumError ; if they don't match, 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)
bsr.w VDPSetupGame
bsr.w DACDriverLoad
bsr.w JoypadInit
move.b #id_Sega,(v_gamemode).w ; set Game Mode to Sega Screen
MainGameLoop:
move.b (v_gamemode).w,d0 ; load Game Mode
andi.w #$1C,d0 ; limit Game Mode value to $1C max (change to a maximum of 7C to add more game modes)
jsr GameModeArray(pc,d0.w) ; jump to apt location in ROM
bra.s MainGameLoop ; loop indefinitely
; ===========================================================================
; ---------------------------------------------------------------------------
; Main game mode array
; ---------------------------------------------------------------------------
GameModeArray:
ptr_GM_Sega: bra.w GM_Sega ; Sega Screen ($00)
ptr_GM_Title: bra.w GM_Title ; Title Screen ($04)
ptr_GM_Demo: bra.w GM_Level ; Demo Mode ($08)
ptr_GM_Level: bra.w GM_Level ; Normal Level ($0C)
ptr_GM_Special: bra.w GM_Special ; Special Stage ($10)
ptr_GM_Cont: bra.w GM_Continue ; Continue Screen ($14)
ptr_GM_Ending: bra.w GM_Ending ; End of game sequence ($18)
ptr_GM_Credits: bra.w GM_Credits ; Credits ($1C)
rts
; ===========================================================================
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
; ===========================================================================
BusError:
move.b #2,(v_errortype).w
bra.s loc_43A
AddressError:
move.b #4,(v_errortype).w
bra.s loc_43A
IllegalInstr:
move.b #6,(v_errortype).w
addq.l #2,2(sp)
bra.s loc_462
ZeroDivide:
move.b #8,(v_errortype).w
bra.s loc_462
ChkInstr:
move.b #10,(v_errortype).w
bra.s loc_462
TrapvInstr:
move.b #12,(v_errortype).w
bra.s loc_462
PrivilegeViol:
move.b #14,(v_errortype).w
bra.s loc_462
Trace:
move.b #16,(v_errortype).w
bra.s loc_462
Line1010Emu:
move.b #18,(v_errortype).w
addq.l #2,2(sp)
bra.s loc_462
Line1111Emu:
move.b #20,(v_errortype).w
addq.l #2,2(sp)
bra.s loc_462
ErrorExcept:
move.b #0,(v_errortype).w
bra.s loc_462
; ===========================================================================
loc_43A:
disable_ints
addq.w #2,sp
move.l (sp)+,(v_spbuffer).w
addq.w #2,sp
movem.l d0-a7,(v_regbuffer).w
bsr.w ShowErrorMessage
move.l 2(sp),d0
bsr.w ShowErrorValue
move.l (v_spbuffer).w,d0
bsr.w ShowErrorValue
bra.s loc_478
; ===========================================================================
loc_462:
disable_ints
movem.l d0-a7,(v_regbuffer).w
bsr.w ShowErrorMessage
move.l 2(sp),d0
bsr.w ShowErrorValue
loc_478:
bsr.w ErrorWaitForC
movem.l (v_regbuffer).w,d0-a7
enable_ints
rte
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
ShowErrorMessage:
lea (vdp_data_port).l,a6
locVRAM ArtTile_Error_Handler_Font*tile_size
lea (Art_Text).l,a0
move.w #(Art_Text_End-Art_Text-tile_size)/2-1,d1 ; strangely, this does not load the final tile
.loadgfx:
move.w (a0)+,(a6)
dbf d1,.loadgfx
moveq #0,d0 ; clear d0
move.b (v_errortype).w,d0 ; load error code
move.w ErrorText(pc,d0.w),d0
lea ErrorText(pc,d0.w),a0
locVRAM vram_fg+$604
moveq #19-1,d1 ; number of characters (minus 1)
.showchars:
moveq #0,d0
move.b (a0)+,d0
addi.w #-'0'+ArtTile_Error_Handler_Font,d0 ; rebase from ASCII to a VRAM index
move.w d0,(a6)
dbf d1,.showchars ; repeat for number of characters
rts
; End of function ShowErrorMessage
; ===========================================================================
ErrorText: dc.w .exception-ErrorText, .bus-ErrorText
dc.w .address-ErrorText, .illinstruct-ErrorText
dc.w .zerodivide-ErrorText, .chkinstruct-ErrorText
dc.w .trapv-ErrorText, .privilege-ErrorText
dc.w .trace-ErrorText, .line1010-ErrorText
dc.w .line1111-ErrorText
.exception: dc.b "ERROR EXCEPTION "
.bus: dc.b "BUS ERROR "
.address: dc.b "ADDRESS ERROR "
.illinstruct: dc.b "ILLEGAL INSTRUCTION"
.zerodivide: dc.b "@ERO DIVIDE "
.chkinstruct: dc.b "CHK INSTRUCTION "
.trapv: dc.b "TRAPV INSTRUCTION "
.privilege: dc.b "PRIVILEGE VIOLATION"
.trace: dc.b "TRACE "
.line1010: dc.b "LINE 1010 EMULATOR "
.line1111: dc.b "LINE 1111 EMULATOR "
even
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
ShowErrorValue:
move.w #ArtTile_Error_Handler_Font+10,(a6) ; display "$" symbol
moveq #8-1,d2
.loop:
rol.l #4,d0
bsr.s .shownumber ; display 8 numbers
dbf d2,.loop
rts
; End of function ShowErrorValue
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
.shownumber:
move.w d0,d1
andi.w #$F,d1
cmpi.w #$A,d1
blo.s .chars0to9
addq.w #7,d1 ; add 7 for characters A-F
.chars0to9:
addi.w #ArtTile_Error_Handler_Font,d1
move.w d1,(a6)
rts
; End of function sub_5CA
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
ErrorWaitForC:
bsr.w ReadJoypads
cmpi.b #btnC,(v_jpadpress1).w ; is button C pressed?
bne.w ErrorWaitForC ; if not, branch
rts
; End of function ErrorWaitForC
; ===========================================================================
Art_Text: binclude "artunc/menutext.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: dc.w VBla_00-VBla_Index, VBla_02-VBla_Index
dc.w VBla_04-VBla_Index, VBla_06-VBla_Index
dc.w VBla_08-VBla_Index, VBla_0A-VBla_Index
dc.w VBla_0C-VBla_Index, VBla_0E-VBla_Index
dc.w VBla_10-VBla_Index, VBla_12-VBla_Index
dc.w VBla_14-VBla_Index, VBla_16-VBla_Index
dc.w VBla_0C-VBla_Index
; ===========================================================================
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.w VBla_Music ; if not, branch
.islevel:
cmpi.b #id_LZ,(v_zone).w ; is level LZ ?
bne.w 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
bra.w VBla_Music
; ===========================================================================
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
tst.b (f_sonframechg).w ; has Sonic's sprite changed?
beq.s .nochg ; if not, branch
writeVRAM v_sgfx_buffer,ArtTile_Sonic*tile_size ; load new Sonic gfx
move.b #0,(f_sonframechg).w
.nochg:
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
tst.b (f_sonframechg).w ; has Sonic's sprite changed?
beq.s .nochg ; if not, branch
writeVRAM v_sgfx_buffer,ArtTile_Sonic*tile_size ; load new Sonic gfx
move.b #0,(f_sonframechg).w
.nochg:
tst.w (v_demolength).w ; is there time left on the demo?
beq.w .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
tst.b (f_sonframechg).w
beq.s .nochg
writeVRAM v_sgfx_buffer,ArtTile_Sonic*tile_size
move.b #0,(f_sonframechg).w
.nochg:
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
bsr.w sub_1642
rts
; ===========================================================================
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
tst.b (f_sonframechg).w
beq.s .nochg
writeVRAM v_sgfx_buffer,ArtTile_Sonic*tile_size
move.b #0,(f_sonframechg).w
.nochg:
tst.w (v_demolength).w
beq.w .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
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
move.l (a0)+,(a1) ; move palette to CRAM
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
move.l (a0)+,(a1)
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:
clr.b (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)
nop
nop
move.b (a1),d0
lsl.b #2,d0
andi.b #$C0,d0
move.b #$40,(a1)
nop
nop
move.b (a1),d1
andi.b #$3F,d1
or.b d1,d0
not.b d0
move.b (a0),d1
eor.b d0,d1
move.b d0,(a0)+
and.b d0,d1
move.b d1,(a0)+
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).l,a2
moveq #$12,d7
.setreg:
move.w (a2)+,(a0)
dbf d7,.setreg ; set the VDP registers
move.w (VDPSetupArray+2).l,d0
move.w d0,(v_vdp_buffer1).w
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
move.w #$3F,d7
.clrCRAM:
move.w d0,(a1)
dbf d7,.clrCRAM ; clear the CRAM