-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSegaVDP.s
1176 lines (1049 loc) · 36 KB
/
SegaVDP.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;@
;@ SegaVDP.s
;@ Sega VDP chip emulator for GBA/NDS.
;@
;@ Created by Fredrik Ahlström on 2012-03-10.
;@ Copyright © 2012-2023 Fredrik Ahlström. All rights reserved.
;@
#ifdef __arm__
#include "../ARMZ80/ARMZ80mac.h"
#include "SegaVDP.i"
.global VDPReset
.global VDPScanlineBPReset
.global VDPClearDirtyTiles
.global VDPSaveState
.global VDPLoadState
.global VDPGetStateSize
.global defaultScanlineHook
.global VDPNewFrame
.global VDPDoScanline
.global VDPCheckIRQ
.global VDPLatchHCounter
.global VDPSetMode
.global VDPSetScanline
.global ntFinnish
.global VDPReg08W
.global VDPVCounterR
.global VDPHCounterR
.global VDPDataSMSW
.global VDPDataGGW
.global VDPDataMDW
.global VDPDataTMSW
.global VDPDataR
.global VDPStatR
.global VDPCtrlW
.global VDPCtrlMDW
.global VDPGetRGBFromIndex
.global VDPGetRGBFromIndexSG
.syntax unified
.arm
.section .text
.align 2
;@----------------------------------------------------------------------------
VDPReset: ;@ Called from gfxReset, r0=vdp/tv type, r1=irq routine, r2 = debounce routine, r12 = vdpptr.
;@----------------------------------------------------------------------------
stmfd sp!,{lr}
and r3,r0,#TVTYPEPAL
strb r3,[vdpptr,#vdpTVType]
and r3,r0,#GGMODE
strb r3,[vdpptr,#vdpGGMode]
and r0,r0,#VDPTYPE_MASK ;@ Mask out VDP type
strb r0,[vdpptr,#vdpType]
cmp r1,#0
adreq r1,VDPSetPinDummy
str r1,[vdpptr,#irqRoutine]
cmp r0,#VDPTMS9918
moveq r2,#0 ;@ TMS9918 doesn't have debounce circuit
str r2,[vdpptr,#debounceRoutine]
add r0,vdpptr,#vdpState
mov r1,#(vdpRegisters-vdpState)/4
bl memclr_ ;@ Clear VDP state
bl VDPMakeModes
bl VDPSetupType
bl VDPRegistersReset
bl VDPVRAMReset
bl VDPSetDefaultPalette ;@ Don't clear palette when loading a savestate.
bl VDPScanlineBPReset
bl VDPClearDirtyTiles
bl VDPNewFrame
bl earlyFrame
ldmfd sp!,{pc}
;@----------------------------------------------------------------------------
VDPSaveState: ;@ In r0=destination, r1=vdpptr. Out r0=size.
.type VDPSaveState STT_FUNC
;@----------------------------------------------------------------------------
stmfd sp!,{r4,r5,lr}
mov r4,r0 ;@ Store destination
mov r5,r1 ;@ Store vdpptr (r1)
ldr r1,[r5,#VRAMPtr]
mov r2,#0x4000
bl memcpy
add r0,r4,#0x4000
add r1,r5,#vdpState
mov r2,#VDPSTATESIZE
bl memcpy
ldmfd sp!,{r4,r5,lr}
ldr r0,=0x4000 + VDPSTATESIZE
bx lr
;@----------------------------------------------------------------------------
VDPLoadState: ;@ In r0=vdpptr, r1=source. Out r0=size.
.type VDPLoadState STT_FUNC
;@----------------------------------------------------------------------------
stmfd sp!,{r4,r5,lr}
mov r5,r0 ;@ Store vdpptr (r0)
mov r4,r1 ;@ Store source
ldr r0,[r5,#VRAMPtr]
mov r2,#0x4000
bl memcpy
add r0,r5,#vdpState
add r1,r4,#0x4000
mov r2,#VDPSTATESIZE
bl memcpy
mov vdpptr,r5
bl VDPClearDirtyTiles
bl paletteTxAll
ldmfd sp!,{r4,r5,lr}
;@----------------------------------------------------------------------------
VDPGetStateSize: ;@ Out r0=state size.
.type VDPGetStateSize STT_FUNC
;@----------------------------------------------------------------------------
ldr r0,=0x4000 + VDPSTATESIZE
bx lr
;@----------------------------------------------------------------------------
VDPRegistersReset:
;@----------------------------------------------------------------------------
stmfd sp!,{r4-r6,lr}
ldrb r0,[vdpptr,#vdpType]
cmp r0,#VDPTMS9918
ldr r4,=VDPRegsDefaults
ldreq r4,=TMSRegsDefaults
add r5,vdpptr,#vdpJumpTable
ldrb r6,[r4],#1
RegResetLoop:
ldrb r1,[r4],#1
ldr r2,[r5],#4
blx r2
subs r6,r6,#1
bne RegResetLoop
ldmfd sp!,{r4-r6,pc}
;@----------------------------------------------------------------------------
VDPVRAMReset: ;@ Clear VDP RAM.
;@----------------------------------------------------------------------------
mov r3,#-1
mov r2,r3,lsl#16
mov r3,r3,lsr#16
ldr r0,[vdpptr,#VRAMPtr]
mov r1,#0x4000/8
vramLoop:
subs r1,r1,#1
stmiapl r0!,{r2,r3}
bhi vramLoop
bx lr
;@----------------------------------------------------------------------------
;@VDPRegsDefaults:
;@----------------------------------------------------------------------------
;@ 0x0 Mode Control 1. mode2 & mode 4 bits set???, GG Bios = 0x16 (line IRQ on), SMS1 Bios = 0x36 (line IRQ + border on).
;@ 0x1 Mode Control 2. bit 7 set ?, GG & SMS1 Bios = 0xA0, bit 5 = frame interrupt enable.
;@ 0x2 Nametable. 0xFF?
;@ 0x3 Colortable. 0xFF?
;@ 0x4 Patterntable. 0xFF?
;@ 0x5 Sprite attribute. 0xFF?
;@ 0x6 Sprite patterntable. 0xFB? GG = 0xFF
;@ 0x7 Background color
;@ 0x8 Horizontal scroll
;@ 0x9 Vertical scroll
;@ 0xA IRQ counter. GG & SMS1 Bios = 0xFF.
;@ 0xB
;@ 0xC
;@ 0xD
;@ 0xE
;@ 0xF
TMSRegsDefaults:
.byte 8
;@ 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7
.byte 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
VDPRegsDefaults:
.byte 16
;@ 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
.byte 0x06, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
MDRegsDefaults:
// .byte 32
;@ 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
// .byte 0x06, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
;@ 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F
// .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
.align 2
;@----------------------------------------------------------------------------
VDPSetPinDummy:
;@----------------------------------------------------------------------------
bx lr
;@----------------------------------------------------------------------------
VDPScanlineBPReset:
;@----------------------------------------------------------------------------
ldrb r0,[vdpptr,#vdpTVType]
tst r0,#TVTYPEPAL
moveq r0,#0xDA ;@ NTSC
ldreq r2,=261 ;@ 262
movne r0,#0xF2 ;@ PAL
ldrne r2,=312 ;@ 313
str r0,[vdpptr,#vdpScanlineBP]
str r2,[vdpptr,#vdpLastScanline]
sub r2,r2,#1
str r2,[vdpptr,#vdp2ndLastScanline]
add r2,r2,#2
str r2,[vdpptr,#vdpTotalScanlines]
bx lr
;@----------------------------------------------------------------------------
VDPMakeModes:
;@----------------------------------------------------------------------------
adr r1,VDPModes_Sega3155246 ;@ For SMS2 & GG.
ldrb r0,[vdpptr,#vdpType]
cmp r0,#VDPTMS9918
adreq r1,VDPModes_TMS9918
cmp r0,#VDPSega3155124
adreq r1,VDPModes_Sega3155124
cmp r0,#VDPSega3155313
adreq r1,VDPModes_Sega3155313
str r1,[vdpptr,#vdpModesPtr]
bx lr
VDPModes_TMS9918: ;@ SG-1000, SC-3000, Coleco, MSX VDP
.byte VDPMODE_0,VDPMODE_0,VDPMODE_3,VDPMODE_3,VDPMODE_1,VDPMODE_1,VDPMODE_B,VDPMODE_B
.byte VDPMODE_2,VDPMODE_2,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B
.byte VDPMODE_0,VDPMODE_0,VDPMODE_3,VDPMODE_3,VDPMODE_1,VDPMODE_1,VDPMODE_B,VDPMODE_B
.byte VDPMODE_2,VDPMODE_2,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B
VDPModes_Sega3155124: ;@ SMS1 VDP
.byte VDPMODE_0,VDPMODE_0,VDPMODE_3,VDPMODE_3,VDPMODE_1,VDPMODE_1,VDPMODE_B,VDPMODE_B
.byte VDPMODE_2,VDPMODE_2,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B
.byte VDPMODE_4,VDPMODE_4,VDPMODE_4,VDPMODE_4,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B
.byte VDPMODE_4,VDPMODE_4,VDPMODE_4,VDPMODE_4,VDPMODE_B,VDPMODE_B,VDPMODE_B,VDPMODE_B
VDPModes_Sega3155246: ;@ SMS2 VDP
VDPModes_Sega3155378: ;@ GG VDP, correct number or is it 377?
.byte VDPMODE_0,VDPMODE_0,VDPMODE_3, VDPMODE_3, VDPMODE_1, VDPMODE_1, VDPMODE_B,VDPMODE_B
.byte VDPMODE_2,VDPMODE_2,VDPMODE_B, VDPMODE_B, VDPMODE_B, VDPMODE_B, VDPMODE_B,VDPMODE_B
.byte VDPMODE_4,VDPMODE_4,VDPMODE_4, VDPMODE_4, VDPMODE_B, VDPMODE_B, VDPMODE_B,VDPMODE_B
.byte VDPMODE_4,VDPMODE_4,VDPMODE_4_240,VDPMODE_4_240,VDPMODE_4_224,VDPMODE_4_224,VDPMODE_4,VDPMODE_4
VDPModes_Sega3155313: ;@ Mega Drive VDP
.byte VDPMODE_B,VDPMODE_5_224,VDPMODE_B,VDPMODE_5_224,VDPMODE_B,VDPMODE_5_224,VDPMODE_B,VDPMODE_5_224
.byte VDPMODE_B,VDPMODE_5_240,VDPMODE_B,VDPMODE_5_240,VDPMODE_B,VDPMODE_5_240,VDPMODE_B,VDPMODE_5_240
.byte VDPMODE_4,VDPMODE_5_224,VDPMODE_4,VDPMODE_5_224,VDPMODE_4,VDPMODE_5_224,VDPMODE_4,VDPMODE_5_224
.byte VDPMODE_4,VDPMODE_5_240,VDPMODE_4,VDPMODE_5_240,VDPMODE_4,VDPMODE_5_240,VDPMODE_4,VDPMODE_5_240
;@(M4)
;@(M2)
;@(M1)
;@(M3)
;@(M5)
;@----------------------------------------------------------------------------
VDPSetupType:
;@----------------------------------------------------------------------------
stmfd sp!,{r4,lr}
ldrb r4,[vdpptr,#vdpType]
cmp r4,#VDPSega3155313 ;@ MD VDP?
moveq r0,#0x13 ;@ Startup scanline on MegaDrive.
ldrne r0,[vdpptr,#vdpTotalScanlines] ;@ number of scanlines
blne getRandomNumber
bl VDPSetScanline
cmp r4,#VDPSega3155313 ;@ MD VDP?
mov r0,#12 ;@ HCount 0xF4.
moveq r0,#13 ;@ HCount 0xF6 (second one).
strb r0,[vdpptr,#vdpVCountBP] ;@ Which HCount VCount changes.
mov r0,#27
moveq r0,#31
strb r0,[vdpptr,#vdpHCountBP] ;@ Which HCount HIRQ happens.
strb r0,[vdpptr,#vdpHCountOffset] ;@ Offset for HC calculation.
adr r1,VDPRegsSMS
adreq r1,VDPRegsMD
cmp r4,#VDPTMS9918
adreq r1,VDPRegsTMS9918
add r0,vdpptr,#vdpJumpTable
mov r2,#32
VDPRegsLoop:
ldr r3,[r1],#4
str r3,[r0],#4
subs r2,r2,#1
bne VDPRegsLoop
add r0,vdpptr,#vdpCtrlTable
adr r1,VDPdest
mov r2,#4
VDPCtrlLoop:
ldr r3,[r1],#4
str r3,[r0],#4
subs r2,r2,#1
bne VDPCtrlLoop
cmp r4,#VDPTMS9918
ldreq r1,=VDPctrl2W
streq r1,[vdpptr,#vdpCtrlTable + 0x0C]
mov r0,#0xE
cmpne r4,#VDPSega3155124
orrne r0,r0,#1
strb r0,[vdpptr,#vdpNTMask]
ldr r0,=vdpStateTable
add r0,vdpptr,r0
adr r1,VDPLineStateTable
mov r2,#18
VDPLineStateLoop:
ldr r3,[r1],#4
str r3,[r0],#4
subs r2,r2,#1
bne VDPLineStateLoop
ldmfd sp!,{r4,pc}
VDPRegsTMS9918:
.long VDPReg00W, VDPReg01W, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
.long VDPReg00W, VDPReg01W, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
.long VDPReg00W, VDPReg01W, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
.long VDPReg00W, VDPReg01W, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
VDPRegsSMS:
.long VDPReg00W, VDPReg01W, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
.long VDPReg08W, VDPReg09W, VDPReg0AW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW
.long VDPReg00W, VDPReg01W, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
.long VDPReg08W, VDPReg09W, VDPReg0AW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW
VDPRegsMD:
.long VDPReg00W, VDPReg01MDW, VDPReg02W, VDPReg03W, VDPReg04W, VDPReg05W, VDPReg06W, VDPReg07W
.long VDPReg08W, VDPReg09W, VDPReg0AW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW
.long VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW
.long VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW, VDPReg0FW
VDPdest:
.long VDPctrl0W
.long VDPctrl1W
.long VDPctrl2W
.long VDPctrl3W
VDPLineStateTable:
.long 0, VDPNewFrame ;@ vdpZeroLine
.long 0, earlyFrame ;@ vdpScrStartLine
.long 96, midFrame
.long 192, endFrame ;@ vdpEndFrameLine
.long 192, startVbl ;@ vdpVBlLine
.long 193, VBL_Hook ;@ vdpVBlEndLine
.long 260, secondLastScanline ;@ vdp2ndLastScanline
.long 261, lastScanline ;@ vdpLastScanline
.long 262, frameEndHook ;@ vdpTotalScanlines
;@----------------------------------------------------------------------------
VDPSetScanline: ;@ in r0 = scanline
;@----------------------------------------------------------------------------
mov r1,#0
str r1,[vdpptr,#vdpNextLineChange]
strb r1,[vdpptr,#vdpLineState]
sub r0,r0,#1
str r0,[vdpptr,#vdpScanline] ;@ Set scanline
bx lr
;@----------------------------------------------------------------------------
VDPSetDefaultPalette:
;@----------------------------------------------------------------------------
stmfd sp!,{lr}
ldrb r0,[vdpptr,#vdpType]
cmp r0,#VDPSega3155313 ;@ MD VDP?
adr r1,SMSDefaultPalette
adreq r1,MDDefaultPalette
add r0,vdpptr,#vdpPaletteRAM
mov r2,#64
bl bytecopy_
ldmfd sp!,{pc}
;@----------------------------------------------------------------------------
SMSDefaultPalette:
.short 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
.short 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
MDDefaultPalette:
.short 0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF
.short 0x000F,0x0005,0x0000,0x0000,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0FFF,0x0AFF,0x0FFF
;@----------------------------------------------------------------------------
;@ This should not be used for realtime calculation of palette but filled in a LUT.
;@----------------------------------------------------------------------------
VDPGetRGBFromIndex: ;@ in r0=index, out r0=RGB.
;@----------------------------------------------------------------------------
ldrb r1,[vdpptr,#vdpType]
cmp r1,#VDPSega3155378 ;@ GG VDP?
beq VDPGetRGBFromIndexGG
adr r3,SMS2ColorLevels ;@ SMS2 VDP
cmp r1,#VDPSega3155124 ;@ SMS1 VDP?
adreq r3,SMS1ColorLevels
cmp r1,#VDPSega3155313 ;@ MD VDP?
adreq r3,MegaDriveMSColorLevels
;@----------------------------------------------------------------------------
fetchRGBFromSMSLUT:
mov r0,r0,lsr#2 ;@ Convert bbbbggggrrrr -> bbggrr
and r1,r0,#0x330
orr r1,r1,r1,lsr#2
and r1,r1,#0x0F0
and r0,r0,#0x003
orr r0,r0,r1,lsr#2
and r1,r0,#0x03 ;@ Red
ldrb r1,[r3,r1]
and r2,r0,#0x0C ;@ Green
ldrb r2,[r3,r2,lsr#2]
add r3,r3,#4
ldrb r0,[r3,r0,lsr#4] ;@ Blue
orr r0,r2,r0,lsl#8
orr r0,r1,r0,lsl#8
bx lr
;@----------------------------------------------------------------------------
VDPGetRGBFromIndexGG: ;@ bbbbggggrrrr -> bbbbbbbbggggggggrrrrrrrr
;@----------------------------------------------------------------------------
mov r2,r0,lsr#8 ;@ Blue
and r1,r0,#0xF0 ;@ Green
and r0,r0,#0x0F ;@ Red
orr r0,r0,r1,lsl#8
orr r0,r0,r2,lsl#16
orr r0,r0,r0,lsl#4
bx lr
;@----------------------------------------------------------------------------
SMS2ColorLevels:
.byte 0, 85,170,255 ;@ RGB
SMS1ColorLevels:
.byte 0, 85,170,255 ;@ RG
.byte 0,104,170,255 ;@ B
MegaDriveMSColorLevels:
.byte 0, 87,158,255 ;@ RG
.byte 0,101,158,255 ;@ B
;@ MegaDrive Color levels.
;@ Normal : 0 52 87 116 144 172 206 255
;@ Shadow : 0 29 52 70 87 101 116 130
;@ HiLite : 130 144 158 172 187 206 228 255
;@----------------------------------------------------------------------------
VDPGetRGBFromIndexSG: ;@ index -> BGR24
;@----------------------------------------------------------------------------
ldrb r1,[vdpptr,#vdpType]
cmp r1,#VDPTMS9918
bne mapSGFromSMS
adr r3,SGPalette
add r3,r3,r0
ldrb r0,[r3,r0,lsl#1]!
ldrb r1,[r3,#1]
ldrb r2,[r3,#2]
orr r0,r0,r1,lsl#8
orr r0,r0,r2,lsl#16
bx lr
;@----------------------------------------------------------------------------
mapSGFromSMS:
adr r1,SGPaletteSMSIndex
add r1,r1,r0,lsl#1
ldrh r0,[r1]
b VDPGetRGBFromIndex
;@----------------------------------------------------------------------------
SGPalette: ;@ RGB24
.byte 0,0,0, 0,0,0, 36,218,36, 109,255,109, 36,36,255, 72,109,255, 182,36,36, 72,218,255
.byte 255,36,36, 255,109,109, 218,218,36, 218,218,145, 36,145,36, 218,72,182, 182,182,182, 255,255,255
SGPaletteSMS: ;@ RGB24
.byte 0,0,0, 0,0,0, 0,170,0, 0,255,0, 0,0,85, 0,0,255, 85,0,0, 0,255,255
.byte 170,0,0, 255,0,0, 85,85,0, 255,255,0, 0,85,0, 255,0,255, 85,85,85, 255,255,255
;@TMS9918Palette: ;@ RGB24
;@ .byte 0,0,0, 0,0,0, 33,200,66, 94,220,120, 84,85,237, 125,118,252, 212,82,77, 66,235,245
;@ .byte 252,85,84, 255,121,120, 212,193,84, 230,206,128, 33,176,59, 201,91,186, 204,204,204, 255,255,255
SGPaletteSMSIndex: ;@ BGR12
.short 0x000, 0x000, 0x0A0, 0x0F0, 0x500, 0xF00, 0x005, 0xFF0, 0x00A, 0x00F, 0x055, 0x0FF, 0x050, 0xF0F, 0x555, 0xFFF
;@----------------------------------------------------------------------------
VDPClearDirtyTiles:
;@----------------------------------------------------------------------------
add r0,vdpptr,#dirtyTiles
mov r1,#0x200/4
b memclr_
;@----------------------------------------------------------------------------
;@----------------------------------------
VDPDoScanline:
stmfd sp!,{lr}
ldr r1,[vdpptr,#vdpScanline]
add r1,r1,#1
str r1,[vdpptr,#vdpScanline]
line0Ret:
ldr r0,[vdpptr,#vdpNextLineChange]
cmp r1,r0
ldrmi pc,[vdpptr,#vdpScanlineHook]
ldrb r2,[vdpptr,#vdpLineState]
add r2,r2,#8
strb r2,[vdpptr,#vdpLineState]
add r1,vdpptr,#vdpStateTable-4
ldrd r0,r1,[r1,r2]
str r1,[vdpptr,#vdpNextLineChange]
blxeq r0
ldr r1,[vdpptr,#vdpScanline]
b line0Ret
defaultScanlineHook:
;@----------------------------------------------------------------------------
bl SpriteParserM4
checkScanlineIRQ:
ldr r0,[vdpptr,#vdpLineIRQ]
subs r0,r0,#1
ldrbmi r0,[vdpptr,#vdpCounter]
str r0,[vdpptr,#vdpLineIRQ]
blmi VDPSetHIRQ ;@ Scanline bit
borderScanlineHook:
mov r0,#0
ldmfd sp!,{pc}
frameEndHook:
mov r0,#0
bl VDPSetScanline
mov r0,#1
ldmfd sp!,{pc}
;@----------------------------------------------------------------------------
SpriteParserM4: ;@ in r1 = scanline
;@----------------------------------------------------------------------------
stmfd sp!,{r1,r3-r10}
sub r1,r1,#1
ldrb r10,[vdpptr,#vdpStat]
ldr r9,[vdpptr,#VRAMPtr]
ldrb r0,[vdpptr,#vdpSATOffset]
and r0,r0,#0x7E
add r9,r9,r0,lsl#7
add r8,r9,#0x100
// ldrb r11,[vdpptr,#vdpSPROffset] ;@ First or second half of VRAM for sprites?
// and r11,r11,#4
mov r3,#0x08 ;@ Normal sprite height.
ldrb r4,[vdpptr,#vdpMode2]
movs r0,r4,lsl#31 ;@ Double pixels/8x16 size
movcs r3,#0x10 ;@ 8x16 size
movmi r3,r3,lsl#1 ;@ Double size pixels
add r5,vdpptr,#vdpSpritePosBuffer
ldr r2,[vdpptr,#vdpSprStop]
mov r6,#0
mov r7,#-0x80
sp0Loop:
ldrb r0,[r9],#1 ;@ MasterSystem OBJ, r0=Ypos.
cmp r0,r2
beq sp0End
subs r0,r1,r0
cmppl r3,r0
bhi sp0Add
sp0Chk:
adds r7,r7,#2
bne sp0Loop
sp0End:
tst r4,#0x40 ;@ Check if display is on and then check for collision.
beq sc0End
tst r10,#0x20 ;@ Is collision already set?
bne sc0End
sc1Loop:
subs r6,r6,#1 ;@ If there is only 1 sprite it can't collide with itself.
ble sc0End
mov r0,r6
ldrb r3,[r5],#4
sc0Loop:
subs r0,r0,#1
bmi sc1Loop
ldrb r2,[r5,r0,lsl#2]
subs r2,r2,r3
rsbmi r2,r2,#0
movs r2,r2,lsr#3
bne sc0Loop
;@ Do pixel check here, Fantastic Dizzy needs it for "damage display".
orr r10,r10,#0x20 ;@ Collision flag.
sc0End:
strb r10,[vdpptr,#vdpStat]
ldmfd sp!,{r1,r3-r10}
bx lr
sp0Add:
cmp r6,#0x8 ;@ This should be 4 in TMS9918 mode.
ldrhmi r0,[r8,r7] ;@ MasterSystem OBJ, r0=Tile,Xpos.
strmi r0,[r5,r6,lsl#2]
addmi r6,r6,#1
bmi sp0Chk
orr r10,r10,#0x40 ;@ Overflow flag ( >8 sprites on a line).
b sp0End
;@----------------------------------------------------------------------------
VDPNewFrame: ;@ Called before line 0 (r0, r1 & r2 safe to use)
;@----------------------------------------------------------------------------
stmfd sp!,{r3-r11,lr}
bl transferVRAM
ldmfd sp!,{r3-r11,lr}
mov r0,#0
str r0,[vdpptr,#vdpNametableLine]
adr r0,defaultScanlineHook
str r0,[vdpptr,#vdpScanlineHook]
ldrb r0,[vdpptr,#vdpCounter]
str r0,[vdpptr,#vdpLineIRQ]
ldrb r0,[vdpptr,#vdpYScroll]
strb r0,[vdpptr,#vdpYScrollBak1]
ldrb r0,[vdpptr,#vdpRealMode]
cmp r0,#VDPMODE_4
bmi mode03_newframe
ldrb r0,[vdpptr,#vdpMode1]
ands r0,r0,#0x40
movne r0,#15 ;@ 16 topmost lines frozen.
str r0,[vdpptr,#vdpScrollXLine]
add r0,vdpptr,#scrollBuff
ldrb r1,[vdpptr,#vdpXScroll]
rsb r1,r1,#0
strbeq r1,[r0]
ldrne r1,=0x00000000
movne r2,#16/4
bne memset_
bx lr
;@-------------------------------------------------------------------------------
mode03_newframe:
;@-------------------------------------------------------------------------------
mov r0,#191
str r0,[vdpptr,#vdpScrollXLine]
mov r0,#0
strb r0,[vdpptr,#vdpYScrollBak1]
add r0,vdpptr,#scrollBuff
mov r1,#192/4
b memclr_
;@----------------------------------------------------------------------------
midFrame: ;@ Called at line 96
;@----------------------------------------------------------------------------
stmfd sp!,{r3-r9,r11,lr}
stmfd sp!,{vdpptr}
bl paletteTxAll
ldmfd sp!,{vdpptr}
ldrb r0,[vdpptr,#vdpMode2]
strb r0,[vdpptr,#vdpMode2Bak1] ;@ Screen on/off
ldrb r0,[vdpptr,#vdpPGOffset]
strb r0,[vdpptr,#vdpPGOffsetBak1] ;@ Pattern offset
ldrb r0,[vdpptr,#vdpCalcMode]
strb r0,[vdpptr,#vdpHeightMode]
and r0,r0,#VDPMODE_MASK
strb r0,[vdpptr,#vdpRealMode]
ldmfd sp!,{r3-r9,r11,pc}
;@----------------------------------------
startVbl: ;@ 192/224/240
eatcycles 1 ;@ VBlank is 1 cycle before HBlank
ldrb r0,[vdpptr,#vdpHCountBP] ;@ This is so that the vbl-bit can be read before the int happens
sub r0,r0,#1
strb r0,[vdpptr,#vdpHCountOffset] ;@ HC Latch need to be tweaked for this scanline.
mov r0,#0x80 ;@ Prime VBlank bit
strb r0,[vdpptr,#vdpPrimedVBl]
bx lr
;@----------------------------------------
VBL_Hook: ;@ 193/225/241
add z80cyc,z80cyc,#1*CYCLE
ldrb r0,[vdpptr,#vdpHCountBP]
strb r0,[vdpptr,#vdpHCountOffset] ;@ HC Latch need to be reset for the rest of the scanlines.
adr r0,borderScanlineHook
str r0,[vdpptr,#vdpScanlineHook]
ldrb r2,[vdpptr,#vdpPrimedVBl]
strb rclr,[vdpptr,#vdpPrimedVBl] ;@ Clear byte.
ldrb r0,[vdpptr,#vdpStat]
orr r0,r0,r2 ;@ VBlank bit
strb r0,[vdpptr,#vdpStat]
b VDPCheckIRQ
;@----------------------------------------
secondLastScanline: ;@ 261/312
add z80cyc,z80cyc,#1*CYCLE ;@ 'Pause' IRQ is 1 cycle after HBlank
ldrb r0,[vdpptr,#vdpHCountBP]
add r0,r0,#1
strb r0,[vdpptr,#vdpHCountOffset] ;@ HC Latch need to be reset for the rest of the scanlines.
bx lr
;@----------------------------------------
lastScanline: ;@ 262/313
eatcycles 1
ldrb r0,[vdpptr,#vdpHCountBP]
strb r0,[vdpptr,#vdpHCountOffset] ;@ HC Latch need to be reset for the rest of the scanlines.
ldrb r0,[vdpptr,#vdpDebouncePin]
ldr r1,[vdpptr,#debounceRoutine] ;@ Set IRQ/NMI pin on CPU
cmp r1,#0
bxne r1
bx lr
;@----------------------------------------------------------------------------
VDPSetHIRQ:
;@----------------------------------------------------------------------------
mov r0,#0x40 ;@ Scanline bit
strb r0,[vdpptr,#vdpPending]
;@----------------------------------------------------------------------------
VDPCheckIRQ:
;@----------------------------------------------------------------------------
ldrb r2,[vdpptr,#vdpMode2]
ldrb r0,[vdpptr,#vdpStat]
and r0,r0,#0x80
ands r0,r0,r2,lsl#2
ldrbeq r2,[vdpptr,#vdpMode1]
ldrbeq r0,[vdpptr,#vdpPending]
andseq r0,r0,r2,lsl#2
ldr r2,[vdpptr,#irqRoutine] ;@ Set IRQ/NMI pin on CPU
bx r2
;@-------------------------------------------------------------------------------
VDPLatchHCounter: ;@ 228cpu=684master, r0 & r2 free to use.
;@-------------------------------------------------------------------------------
;@ mov r11,r11 ;@ No$GBA breakpoint
ldrb r0,[vdpptr,#vdpHCountOffset] ;@ 27/26 cycles.
rsbs r0,r0,z80cyc,asr#CYC_SHIFT ;@ Tweak for OUT instruction taking some cycles before writing value to port.
addmi r0,r0,#228
add r0,r0,r0,lsl#1 ;@ cycle*3
rsb r0,r0,#600 ;@ hc=(0xE9->0x93)
sub r0,r0,#10
mov r0,r0,asr#2
strb r0,[vdpptr,#vdpHCountLatch]
bx lr
;@----------------------------------------------------------------------------
VDPSetMode:
;@----------------------------------------------------------------------------
ldrb r0,[vdpptr,#vdpMode1]
ldrb r1,[vdpptr,#vdpMode2]
mov r0,r0,lsr#1
orr r0,r0,r1,lsl#27
mov r0,r0,ror#2
ldr r1,[vdpptr,#vdpModesPtr]
ldrb r0,[r1,r0,lsr#27]
strb r0,[vdpptr,#vdpCalcMode]
ldrb r1,[vdpptr,#vdpTVType]
tst r1,#TVTYPEPAL
mov r1,#0xDA
addne r1,r1,#0x18 ;@ NTSC/PAL diff
ands r0,r0,#VDPMODE_HEIGHTMASK ;@ 224 and/or 240 height
addne r1,r1,#0x10
str r1,[vdpptr,#vdpScanlineBP]
mov r1,#208 ;@ Sprite stop pos.
movne r1,#0x200
str r1,[vdpptr,#vdpSprStop]
moveq r1,#32 ;@ Maxpan
movne r1,#64
add r0,r1,#192 ;@ 224/256
str r0,[vdpptr,#vdpScrollMask]
sub r0,r0,#32
str r0,[vdpptr,#vdpVBlLine]
add r0,r0,#1
str r0,[vdpptr,#vdpVBlEndLine]
ldrb r0,[vdpptr,#vdpNTMask]
orreq r0,r0,#0x2
bicne r0,r0,#0x2
strb r0,[vdpptr,#vdpNTMask]
ldrb r0,[vdpptr,#vdpGGMode]
tst r0,#GGMODE
movne r0,#152
addne r0,r0,r1,lsr#1
addeq r0,r1,#160
str r0,[vdpptr,#vdpEndFrameLine]
mov r0,#0
movne r0,r1,lsr#1
addne r0,r0,#8
strb r0,[vdpptr,#vdpScrStartLine]
b VDPCheckIRQ
;@----------------------------------------------------------------------------
VDPStatR:
;@----------------------------------------------------------------------------
;@ mov r11,r11 ;@ No$GBA breakpoint
stmfd sp!,{vdpptr,lr}
mov r0,#0
ldr r1,[vdpptr,#irqRoutine] ;@ Clear IRQ/NMI pin on CPU
blx r1
ldmfd sp!,{vdpptr,lr}
strb rclr,[vdpptr,#vdpToggle]
ldrb r0,[vdpptr,#vdpStat]
orr r0,r0,#0x02 ;@ PGA Tour Golf expects "something" in the low 5 bits
strb rclr,[vdpptr,#vdpStat]
strb rclr,[vdpptr,#vdpPending]
ldrb r1,[vdpptr,#vdpPrimedVBl]
cmp r1,#0
bxeq lr
cmp z80cyc,#11*CYCLE
orrmi r0,r0,#0x80
strbmi rclr,[vdpptr,#vdpPrimedVBl]
bx lr
;@----------------------------------------------------------------------------
VDPVCounterR:
;@----------------------------------------------------------------------------
;@ mov r11,r11 ;@ No$GBA breakpoint
ldr r0,[vdpptr,#vdpScanline]
ldrb r1,[vdpptr,#vdpVCountBP]
cmp z80cyc,r1,lsl#CYC_SHIFT
addcc r0,r0,#1 ;@ Unsigned lower
ldr r1,[vdpptr,#vdpScanlineBP]
cmp r0,r1
ldrhi r1,[vdpptr,#vdpTotalScanlines]
subhi r0,r0,r1
bx lr
;@----------------------------------------------------------------------------
VDPHCounterR:
;@----------------------------------------------------------------------------
ldrb r0,[vdpptr,#vdpHCountLatch]
bx lr
;@----------------------------------------------------------------------------
VDPCtrlMDW:
;@----------------------------------------------------------------------------
ldrb r1,[vdpptr,#vdpToggle]
eors r1,r1,#1
strb r1,[vdpptr,#vdpToggle]
strbne r0,[vdpptr,#vdpBuffMD]
bxne lr
and r0,r0,#0xFF
ldrb r1,[vdpptr,#vdpBuffMD]
orr r2,r1,r0,lsl#8
ldr r1,[vdpptr,#vdpAdr]
mov r1,r1,lsl#14
mov r1,r1,lsr#14
orr r1,r1,r2,lsl#18
str r1,[vdpptr,#vdpAdr]
ldrb r2,[vdpptr,#vdpRealMode]
cmp r2,#VDPMODE_5
bne vdpCtrlBW
mov r2,r0,lsr#6
cmp r2,#2
ldrbne r2,[vdpptr,#vdpToggle]
eorne r2,r2,#2
strbne r2,[vdpptr,#vdpToggle]
b vdpCtrlBW
;@----------------------------------------------------------------------------
VDPCtrlW:
;@----------------------------------------------------------------------------
ldrb r1,[vdpptr,#vdpToggle]
eors r1,r1,#1
strb r1,[vdpptr,#vdpToggle]
and r0,r0,#0xFF
ldr r1,[vdpptr,#vdpAdr]
biceq r1,r1,#0xFC000000
bicne r1,r1,#0x03FC0000
orreq r1,r1,r0,lsl#26
orrne r1,r1,r0,lsl#18
str r1,[vdpptr,#vdpAdr]
bxne lr
vdpCtrlBW:
movs r0,r0,lsr#6
add r2,vdpptr,#vdpCtrlTable
strb r0,[vdpptr,#vdpCtrl]
ldr pc,[r2,r0,lsl#2]
;@----------------------------------------------------------------------------
VDPDataR:
;@----------------------------------------------------------------------------
ldrb r0,[vdpptr,#vdpBuff]
ldr r1,[vdpptr,#vdpAdr]
;@----------------------------------------------------------------------------
VDPctrl0W: ;@ Set read address, fill buffer.
;@----------------------------------------------------------------------------
add r2,r1,#0x00040000
str r2,[vdpptr,#vdpAdr]
ldr r2,[vdpptr,#VRAMPtr]
ldrb r1,[r2,r1,lsr#18]
str r1,[vdpptr,#vdpBuff] ;@ Write to vdpbuffer and clear vdptoggle.
VDPctrl1W: ;@ Set VRAM write adress
VDPctrl3W: ;@ Set CRAM write adress
bx lr
;@----------------------------------------------------------------------------
VDPctrl2W: ;@ Write to vdp registers.
;@----------------------------------------------------------------------------
mov r1,r1,lsr#18
and r0,r1,#0x1F00
add r2,vdpptr,#vdpJumpTable
ldr pc,[r2,r0,lsr#6]
;@----------------------------------------------------------------------------
VDPReg00W:
;@----------------------------------------------------------------------------
strb r1,[vdpptr,#vdpMode1]
b VDPSetMode
;@----------------------------------------------------------------------------
VDPReg01MDW:
;@----------------------------------------------------------------------------
and r1,r1,#0xFE ;@ Mask out zoomed sprites on MD.
;@----------------------------------------------------------------------------
VDPReg01W:
;@----------------------------------------------------------------------------
strb r1,[vdpptr,#vdpMode2]
b VDPSetMode
;@----------------------------------------------------------------------------
VDPReg02W:
;@----------------------------------------------------------------------------
ldrb r0,[vdpptr,#vdpNameTable]
strb r1,[vdpptr,#vdpNameTable]
ldr r2,[vdpptr,#vdpScanline] ;@ r2=scanline
ntFinnish:
cmp r2,#224
movhi r2,#224
ldr r1,[vdpptr,#vdpNametableLine]
str r2,[vdpptr,#vdpNametableLine]
sub r1,r1,r2
add r2,r2,vdpptr
add r2,r2,#TMapBuff
nt1:
strb r0,[r2],#-1 ;@ Fill backwards from scanline to lastline
adds r1,r1,#1
ble nt1
bx lr
;@----------------------------------------------------------------------------
VDPReg03W: ;@ Color Table - offset
;@----------------------------------------------------------------------------
ldrb r0,[vdpptr,#vdpCTOffset]
strb r1,[vdpptr,#vdpCTOffset]
eor r0,r0,r1
ands r0,r0,#0x80
bxeq lr
DT_clear:
and r1,r1,#0x80
add r0,vdpptr,#dirtyTiles
add r0,r0,r1,lsl#1
mov r1,#0x40
b memclr_
;@----------------------------------------------------------------------------
VDPReg04W: ;@ Pattern Generator Table - offset
;@----------------------------------------------------------------------------
and r1,r1,#7
ldrb r0,[vdpptr,#vdpPGOffset]
strb r1,[vdpptr,#vdpPGOffset]
eor r0,r0,r1
ands r0,r0,#4
bxeq lr
mov r1,r1,lsl#5
b DT_clear
;@----------------------------------------------------------------------------
VDPReg05W: ;@ Sprite Attribute Table - offset
;@----------------------------------------------------------------------------
strb r1,[vdpptr,#vdpSATOffset]
bx lr
;@----------------------------------------------------------------------------
VDPReg06W: ;@ Sprite tiles - offset
;@----------------------------------------------------------------------------
and r1,r1,#7
ldrb r0,[vdpptr,#vdpSPROffset]
strb r1,[vdpptr,#vdpSPROffset]
cmp r0,r1
bxeq lr
add r0,vdpptr,#dirtyTiles
add r0,r0,r1,lsl#6
mov r1,#0x10
b memclr_
;@----------------------------------------------------------------------------
VDPReg07W: ;@ Backdrop Color
;@----------------------------------------------------------------------------
strb r1,[vdpptr,#vdpBDColor]
bx lr
;@----------------------------------------------------------------------------