-
Notifications
You must be signed in to change notification settings - Fork 25
/
setup.asm
4287 lines (3981 loc) · 80.6 KB
/
setup.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
Startup:
ld bc,-arc_start
add hl,bc
ld (ArcBase),hl
; Get the calculator type from the OS header
ld hl,_OSHeader
call _GetFieldSizeFromType
ld de,$80C0
call _FindField
; If not found, calcType stays at default 0 (84+CE)
jr nz,_
call _GetFieldSizeFromType
ld a,(hl)
inc hl
and (hl)
ld (calcType),a
_
call _RunIndicOff
ACALL(LoadConfigFile)
CalculateEpochLoop:
; Grab current day count
ld hl,(mpRtcDayCount)
push hl
; Multiply year by 365
ld hl,(osYear)
push hl
pop de
ld b,(365-1) / 2
_
add hl,de
add hl,de
djnz -_
; Add in day-1
ld a,(osDay)
ld c,a
dec.s bc
add hl,bc
; If we're Feb. or earlier, don't count this leap year
ld a,(osMonth)
cp 3
jr nc,_
dec de
_
; Add in months
ld ix,monthLUT
_
ld c,(ix)
inc ix
add hl,bc
dec a
jr nz,-_
; Add in year/4
srl d
rr e
srl d
rr e
add hl,de
; Subtract out year/100
ex de,hl
ld a,25
call _DivHLByA
ex de,hl
or a
sbc hl,de
; Add in year/400
srl d
rr e
srl d
rr e
add hl,de
; Subtract out January 1, 1970
ld bc,-719527
add hl,bc
jr c,_
sbc hl,hl
_
ex de,hl
pop bc
; Make sure day count matches
ld hl,(mpRtcDayCount)
or a
sbc hl,bc
jr nz,CalculateEpochLoop
ex de,hl
sbc hl,bc
ld (epochDayCount),hl
ACALL(BackupOriginalHardwareSettings)
; Backup original LCD settings
ld hl,mpLcdTiming0
ld de,originalLcdSettings
ld bc,12
ldir
ld hl,(mpLcdCtrl)
ex de,hl
ld (hl),de
; Reinitialize hardware, this is slightly slow but should return SPI to a
; known state on Python Edition models.
call _boot_InitializeHardware
ld hl,GlobalErrorHandler
call _PushErrorHandler
NoRomMenuLoop:
; Set custom hardware settings
ACALL(SetCustomHardwareSettings)
; Set original gamma settings and initialize menu palette
ACALL(SetupOriginalGamma)
; Set current description and main menu selection
APTR(NoRomLoadedDescription)
ld (current_description),hl
ld a,7
ld (main_menu_selection),a
; Reset ROM name, state index, and selected config
xor a
ld (ROMName+1),a
ld (current_state),a
ld (current_config),a
; Start with Load ROM menu
inc a
ACALL(emulator_menu)
; Check for deleting a ROM
ex af,af'
cp 5
jr z,DeleteROM
; If not loading a new ROM, exit
dec a
jr nz,SaveConfigAndQuit
ACALL(RestoreOriginalHardwareSettings)
LoadNewGameLoop:
; Copy the name from ROMNameToLoad
ld hl,ROMName
push hl
pop de
ld bc,ROMNameToLoad - ROMName
add hl,bc
ldir
; Switch to per-game config editing
ld a,1
ld (current_config),a
ACALL_SAFERET(StartROM)
; If NC is returned, we're loading another game
jr nc,LoadNewGameLoop
; If an error code of 0 is returned, exit
or a
jr z,SaveConfigAndQuit
; Display the error, and return to the menu if ON wasn't pressed
ACALL(DisplayError)
NoRomMenuLoopTrampoline:
jr nz,NoRomMenuLoop
SaveConfigAndQuit:
call _PopErrorHandler
SaveConfigAndQuitForError:
ACALL(RestoreOriginalHardwareSettings)
ACALL_SAFERET(SaveConfigFile)
ld a,(brightness)
ld (mpBlLevel),a
RestoreHomeScreen:
; Set all palette entries to white to smooth the transition
ld hl,mpLcdPalette
push hl
pop de
inc de
ld bc,$01FF
ld (hl),c
ldir
; Clear pixelShadow etc.
ld hl,pixelShadow
push hl
pop de
inc de
ld (hl),c
ld bc,(8400*3) - 1
ldir
; Mark graph dirty
set graphDraw,(iy+graphFlags)
; Restore the frame buffer
call _ClrLCDFull
call _DrawStatusBar
call _HomeUp
; Change the LCD settings to fullscreen 16-bit
AJUMP(RestoreOriginalLcdSettings)
DeleteROM:
ACALL(RestoreOriginalHardwareSettings)
ACALL(DeleteROMFiles)
or a ; Non-zero
jr NoRomMenuLoopTrampoline
; Input: HL = insertion point
; DE = insertion size
; Output: Carry set and A=error if error occurred
; DE = insertion point
; BC = insertion size
InsertMemSafe:
push hl
push de
; Check to make sure there's room for at least 2 VAT entries
ld hl,30
add hl,de
push hl
call _MemChk
pop de
ex de,hl
scf
sbc hl,de
ccf
inc hl
ld (errorArg),hl
pop hl
pop de
ld a,ERROR_NOT_ENOUGH_MEMORY
ret c
push hl
call _InsertMem
pop bc
; Resets carry
ld hl,(asm_prgm_size)
add hl,bc
ld (asm_prgm_size),hl
ret
; Input: HL = filename
; DE = location to convert
; Note: Location must have size bytes initialized and
; the range must end exactly on userMem + (asm_prgm_size),
; and also the filename should not already exist.
; Output: Carry set, HL preserved.
; File is created and asm_prgm_size is updated.
ConvertMemToFile:
push hl
#ifdef DEBUG
; Assert that location + size bytes == userMem + (asm_prgm_size)
ex de,hl
ld de,(hl)
ex de,hl
inc.s hl
inc hl
add hl,de
ld bc,(asm_prgm_size)
sbc hl,bc
ld bc,userMem
sbc hl,bc
jr nz,$
pop hl
push hl
#endif
push de
call _Mov9ToOP1
call _CmpPrgNamLen
ld bc,0
call _CreatePVar4
push hl
pop ix
ld hl,2
add hl,sp
ld a,(hl)
pop hl
ld (ix-5),a
ld (ix-4),h
ld (ix-3),l
ld de,-userMem
add hl,de
ld (asm_prgm_size),hl
pop hl
ret
LoadROMAndRAMFailed:
push af
ld hl,game_config_start
ACALL(DelMemSafeSizeBytes)
pop af
ret
StartROM:
; Insert memory to hold the game config and initialize to default config
APTR(DefaultGameConfig)
push hl
ld hl,game_config_start
ld de,game_config_end - game_config_start
ACALL(InsertMemSafe)
pop hl
ret c
ldir
; Look up the game config file and apply it
ld hl,ROMName
push hl
xor a
_
inc hl
cp (hl)
jr nz,-_
ld (hl),'C'
inc hl
ld (hl),'f'
inc hl
ld (hl),'g'
inc hl
ld (hl),a
pop hl
ACALL(LookUpAppvar)
ld de,game_config_start + (FrameskipValue - config_start)
ACALL(LoadConfigFileAny)
ACALL_SAFERET(LoadROMAndRAMRestoreName)
jr c,LoadROMAndRAMFailed
xor a
ld (current_state),a
ACALL(LoadStateFiles)
push af
; Build the existing state map
ld a,'9'+1
_
dec a
ld (current_state),a
push hl
ACALL(GetStateFileName)
ACALL(LookUpAppvar)
pop hl
ccf
adc hl,hl
ld a,(current_state)
cp '0'
jr nz,-_
ld a,l
ld (existing_state_map),a
ld a,h
and 3
ld (existing_state_map+1),a
pop af
jr nc,StartROMAutoStateNoError
cp ERROR_NOT_ENOUGH_MEMORY
jr nz,RestartFromHere
ld hl,(cram_size)
ld de,(game_config_end - game_config_start) + 6
add hl,de
ex de,hl
ld hl,game_config_start
ACALL(DelMemSafe)
OutOfMemoryFinish:
ld a,ERROR_NOT_ENOUGH_MEMORY
scf
ret
StartROMAutoStateNoError:
ld de,AutoStateLoadedMessage
ACALL(SetEmulatorMessage)
jr StartFromHereCleanFrameTrampoline
RestartFromHere:
xor a
ld (emulatorMessageText),a
call reset_preserved_area
; Check for GBC support
ld hl,(rom_start)
ld bc,$0143
add hl,bc
bit 7,(hl)
; If no support, default to GB
jr z,++_
ld a,(GamePreferredModel)
cp $FF
jr nz,_
ld a,(PreferredModel)
_
or a
_
push af
ld bc,save_state_size + 1
jr z,_
ld bc,save_state_gbc_size + 1
_
ld hl,save_state_size_bytes
ld de,(hl)
ld (hl),bc
inc hl
inc hl
inc hl
dec de
dec bc
push hl
push bc
ACALL(DelMemSafe)
pop de
pop hl
ACALL(InsertMemSafe)
jr nc,StartROMInitStateContinue
StartROMInitStateOutOfMemory:
pop af
; Temporarily prevent auto state saving because state is clean
xor a
ld (should_auto_save),a
dec de
dec de
ld (de),a
dec de
inc a
ld (de),a
ld hl,(errorArg)
push hl
ACALL_SAFERET(SaveAutoStateFilesAndGameConfig)
pop hl
ld (errorArg),hl
jr OutOfMemoryFinish
StartFromHereCleanFrameTrampoline:
jr StartFromHereCleanFrame
RestartFromHereTrampoline:
jr RestartFromHere
StartROMInitStateContinue:
push de
pop hl
inc de
dec bc
ld (hl),0
ldir
inc hl
ld a,(hl)
dec a
inc hl
or (hl)
jr z,_
inc hl
inc hl
ld (cram_start),hl
_
APTR(hmem_init)
ld de,hram_saved + $0100
ld c,hmem_init_size
ldir
;HL=hram_init
push de
ex (sp),hl
dec hl
ld c,$80 - hmem_init_size + 1
ldir
push hl
ld (hl),c
ld c,$7F
ldir
pop de
pop hl
ld c,hram_init_size
ldir
;HL=regs_init
pop af
ld c,regs_init_gbc - regs_init
jr z,_
ld e,a
add hl,bc
; Update registers which are different on GBC
ld a,$7E
ld (hram_saved + $0100 + (KEY1-ioregs)),a
inc a ;A=$7F
ld (hram_saved + $0100 + (SC-ioregs)),a
rlca ;A=$FE
ld (hram_saved + $0100 + (VBK-ioregs)),a
; Set carry for GBA back-compat
add a,e
ld a,$F8
ld (hram_saved + $0100 + (SVBK-ioregs)),a
_
ld de,regs_saved
ldir
jr nc,_
; For GBA back-compat, update the F and B registers
ld hl,regs_saved + STATE_REG_AF
ld (hl),c ;0
inc hl
inc hl ;STATE_REG_BC
inc hl
inc (hl)
_
StartFromHereCleanFrame:
; Clear the mini frame backup for a clean initial frame
ld hl,mini_frame_backup
push hl
pop de
inc de
ld bc,160*144-1
ld (hl),SCREEN_OFF_COLOR
ldir
StartFromHere:
ld hl,(save_state_size_bytes)
ld a,l
dec a
or h
jr z,RestartFromHereTrampoline
ld a,3
ld (main_menu_selection),a
ld hl,SkinFileName
ACALL(LookUpAppvar)
ccf
jr c,_
sbc hl,hl
_
ld (skin_file_ptr),hl
jr nc,++_
; If found and system type is GBC, check if there's a second skin in the skin file
ld a,(regs_saved + STATE_SYSTEM_TYPE)
or a
jr z,++_
; Skip over the palette
ld a,32
_
cpi
dec a
jr nz,-_
; Decompress the first skin
ld de,decompress_buffer
call lzf_decompress
; Successful decompression means only one skin is present
jr z,_
; Otherwise, use the second skin
add hl,de
ld (skin_file_ptr),hl
_
ACALL(SetCustomHardwareSettings)
#ifdef DEBUG
APRINTF(StartText)
#endif
#ifdef FASTLOG
call fastlog_init
push de
pop hl
inc de
dec bc
ld (hl),0
ldir
#endif
#ifdef FUZZ_SAFERAM
; Randomize SafeRAM for fuzz testing
ld hl,pixelShadow
ld de,rombankLUT
_
ld a,r
ld (hl),a
inc hl
or a
sbc hl,de
add hl,de
jr nz,-_
#endif
ld iy,state_start+state_size
ld (saveSP),sp
ld sp,myADLstack + 3
ld hl,$004000 ; Dummy banked return address to act as a loop terminator
push hl
ld hl,z80codebase
push hl
pop de
inc de
ld bc,hram_start - z80codebase - 1
ld (hl),l
ldir
ld hl,hram_saved
ld b,2
ldir
APTR(z80code)
ld de,z80codebase
ld bc,z80codesize
ldir
APTR(flags_lut_init)
ld de,z80codebase + flags_lut
inc b
ldir
; Generate palette overlapped indices using a 6-bit LFSR.
; Palette register format is little endian, so the LFSR shifts right.
; The first iteration does not use the LFSR, handling the invalid
; state of 000. The first LFSR state is 100, which properly overlaps.
ld hl,overlapped_palette_index_lut
ld a,$10<<2 ; The first state of the LFSR
ld e,a ; Initial counter of 64
SetupOverlappedPaletteIndexLoop:
; Duplicate indices to ignore low 2 bits of input
ld b,4
_
ld (hl),c \ inc l
djnz -_
; Use the LFSR state as the next LUT entry
ld l,a
; Shift the LFSR right two bits (using tap bits $21<<2)
ld b,2
_
sra a ; Duplicate the top bit
bit 1,a ; Check the shifted-out bottom bit
jr z,_
xor $82 ; Reset the shifted-out bit and invert the top bit
_
djnz --_
; Advance to the next index
inc c
dec e
jr nz,SetupOverlappedPaletteIndexLoop
; Generate a routine in cursor memory to generate overlapped pixels,
; and also generate the overlapped pixel index LUT using 8-bit LFSR.
ld a,c ; Initial LFSR state = $40
ld de,overlapped_pixel_index_lut ; Initial pixel sequence is 0000
ld c,e ; Initial reversed pixel sequence is 0000
ld hl,mpLcdCursorImg + 512
ld (hl),$C9 ;RET
SetupOverlappedPixelIndexLoop:
; Write the index for the current pixel sequence into the LUT
ex de,hl
ld (hl),b
; Write the index for the reversed pixel sequence into the reverse LUT
push hl
ld l,c
inc h ;overlapped_pixel_rev_index_lut
ld (hl),b
pop hl
ex de,hl
; Initialize the opcode
dec hl
ld (hl),$70>>2 ;LD (HL),B/C/D/E
; Shift out a pixel from the sequence
sla e
; Shift the high bit of the pixel into the opcode
rl (hl)
; Shift a high pixel bit out of the LFSR into the sequences
add a,a
res 4,c
jr nc,_
xor $C3
set 4,c
inc e
_
; Shift a low pixel bit out of the LFSR into the sequences
add a,a
res 4,e
jr nc,_
xor $C3
set 4,e
scf
_
; Shift out a pixel from the reversed sequence
rr c
; Shift the low bit of the pixel into the opcode
rl (hl)
; Write the other opcode
dec hl
ld (hl),$2B ;DEC HL
; Advance the index
inc b
jr nz,SetupOverlappedPixelIndexLoop
ld a,(iy-state_size+STATE_SYSTEM_TYPE)
dec a
jr z,SetupOverlappedGBCPixels
;A=BG_PALETTE_COLOR_0
ld (scanline_fill_color_smc),a
; Generate overlapped BG pixels
ld c,b ;C=$00
dec b ;B=$FF
ld de,$0102
ld hl,overlapped_pixel_data + (256+3)
call mpLcdCursorImg + 512 - (3*2)
call mpLcdCursorImg
; Initialize cursor memory code
APTR(cursorcode)
ld de,mpLcdCursorImg
ld bc,cursorcodesize
ldir
; Get initial OBP palette indices
ld hl,overlapped_palette_index_lut + OBP0_COLORS_START
ld de,(iy-ioregs+OBP0)
ld a,l ;OBP0_COLORS_START
ld l,e
add a,(hl)
ld (overlapped_obp0_palette_index),a
ld a,OBP1_COLORS_START
ld l,d
add a,(hl) ; Resets carry flag
ld (overlapped_obp1_palette_index),a
; Fill the BGP value frequencies with 0
dec h ;BGP_frequencies
_
ld (hl),c ;0
inc l
djnz -_
dec h ;BGP_write_queue
; Initialize the BGP write queue
ld l,BGP_write_queue & $FF
ld (hl),$FF
; Fill the palette conversion LUT with the identity transform
ld h,(convert_palette_LUT >> 8) & $FF
_
ld (hl),l
inc l
djnz -_
; GB empty frame is BGP color 0
ld a,BG_COLOR_0
jr SetupOverlappedPixelsDone
SetupOverlappedGBCPixels:
; Generate overlapped BG palette pixels
ld hl,gbc_overlapped_pixel_data_end
ld b,GBC_BG_TRANSPARENT_COLORS + 7
ld a,GBC_BG_OPAQUE_COLORS + 24
_
; Generate the high priority palette
add a,GBC_BG_HIGH_PRIO_COLORS - GBC_BG_OPAQUE_COLORS - 3
ld c,a
ld d,c \ inc d
ld e,d \ inc e
call mpLcdCursorImg + 512 - (6*2)
call mpLcdCursorImg
call mpLcdCursorImg
; Generate the normal priority palette
sub GBC_BG_HIGH_PRIO_COLORS - GBC_BG_OPAQUE_COLORS
ld c,a
ld d,c \ inc d
ld e,d \ inc e
call mpLcdCursorImg + 512 - (6*2)
call mpLcdCursorImg
call mpLcdCursorImg
; Move to the previous palette
djnz -_
; Generate the sprite priority palettes
;B=GBC_OBJ_TRANSPARENT_COLOR
ld a,GBC_OBJ_LOW_PRIO_COLORS
_
ld c,a
ld d,c \ inc d
ld e,d \ inc e
call mpLcdCursorImg
add a,GBC_OBJ_NORMAL_PRIO_COLORS - GBC_OBJ_LOW_PRIO_COLORS
cp GBC_OBJ_HIGH_PRIO_COLORS+1
jr c,-_
ld h,(high_prio_sprite_palette_lut >> 8) & $FF
ld a,GBC_OBJ_OPAQUE_COLORS - GBC_OBJ_HIGH_PRIO_COLORS
ld c,256-8
call fill_sprite_palette_luts
; Initialize cursor memory code
APTR(cursorcode)
ld de,mpLcdCursorImg
ld bc,gbc_render_start - mpLcdCursorImg
ldir
ld bc,cursorcodesize - (gbc_render_start - mpLcdCursorImg)
add hl,bc
ld bc,gbc_cursorcodesize - (gbc_render_start - mpLcdCursorImg)
ldir
scf
SetupOverlappedPixelsDone:
; Fill the LYC write prediction list with vblank predictions
ld hl,lyc_prediction_list
_
ld (hl),VBLANK_SCANLINE
inc l
djnz -_
ld a,vram_tiles_start >> 16
ld mb,a
; Load BG and OBJ palettes for GBC
ld hl,bg_palettes_saved
ld de,z80codebase + gbc_bg_palette_data
ld a,64
ld c,a
ldir
inc d ;gbc_obj_palette_data
ld e,gbc_obj_palette_data & $FF
ld c,a
ldir
; Here carry is set for GBC or reset for GB
ld ix,vram_tiles_start + 128
ld hl,vram_start + $1800
ld c,a ;64
SetupTilemapCacheOuterLoop:
ld b,$20
SetupTilemapCacheInnerLoop:
ld e,(hl)
inc hl
ld d,a
mlt de
ld.s (ix-128),de
jr c,_
ld.s (ix-64),de ; GB only
_
bit 5,d
jr nz,_
set 6,d
_
ld.s (ix),de
jr c,_
ld.s (ix+64),de ; GB only
_
lea ix,ix+2
jr nc,_
lea ix,ix+2 ; GBC only
_
djnz SetupTilemapCacheInnerLoop
lea ix,ix+64
ld ixl,128
dec c
jr nz,SetupTilemapCacheOuterLoop
jr nc,SetupTilemapCacheDone
ld hl,vram_gbc_start + $2000 + $1800
ld ixh,(vram_tiles_start >> 8) & $FF
ld c,a ;$40
; Generate the GBC tile attribute LUTs
ld de,gbc_tile_attributes_lut
_
ld a,e
; Get only vertical flip, horizontal flip, and VRAM bank
and $68
; Move horizontal flip to bit 2
bit 5,a
jr z,_
xor $24
_
; Duplicate vertical flip into bits 4, 5, and 7
bit 6,a
jr z,_
or $B0
_
rrca
; Fill in gbc_tile_attributes_lut_2 first
inc d
ld (de),a
; Remove bit 6 for gbc_tile_attributes_lut
res 6,a
dec d
ld (de),a
inc e
jr nz,---_
; Now handle GBC tile attributes
SetupTilemapAttributesOuterLoop:
ld b,$20
SetupTilemapAttributesInnerLoop:
; Get the attributes byte
ld e,(hl)
inc hl
; Map the attributes to combine with the low byte of the tile cache
ld a,(de)
or (ix-128)
ld (ix-128),a
ld (ix),a
; Get the palette index from the palette and priority bits
ld a,e
and $87
rlca
; Multiply the index by (256+3)*2=512+6
add a,a
ld e,a
add a,a
add a,e
ld (ix-126),a
ld (ix+2),a
ld a,e
add a,(gbc_overlapped_pixel_data - vram_pixels_start) >> 8
ld (ix-125),a
ld (ix+3),a
lea ix,ix+4
djnz SetupTilemapAttributesInnerLoop
ld ixl,128
dec c
jr nz,SetupTilemapAttributesOuterLoop
; Treat BGP/OBP0/OBP1 as direct read/write ports
ld hl,(write_port_direct - mem_write_port_routines) * $010101
ld (z80codebase+mem_write_port_lut+(BGP-ioregs)),hl
; Fix up the GBC sprite drawing loop to iterate forward through OAM
ld a,$5C
ld (gbc_draw_sprites_smc_1),a
ld a,5
ld (gbc_draw_sprites_smc_2),a
ld a,-$60
ld (gbc_draw_sprites_smc_3),a
SetupTilemapCacheDone:
ld a,z80codebase >> 16
ld mb,a
APTR(digits_encoded)
ex de,hl
ld hl,digits
ld b,3*11
_
ld a,(de)
inc de
ld c,a
_
sla c
sbc a,a
or BLACK-(1+WHITE)
add a,1+WHITE
ld (hl),a
inc hl
ld a,l
and 7
jr nz,-_
djnz --_
;ld.sis sp,myz80stack ; This is set by custom hardware settings above
ld hl,callstack_ret_dummy_target ; Return handler when no cached calls are available
push.s hl
push.s bc ; Cycle count of 0 for default return handler
; Copy palette conversion code to SHA hardware, if possible
APTR(sha_code)
ld de,mpShaData
ld c,sha_code_size
or (iy-state_size+STATE_SYSTEM_TYPE)
jr z,_
add hl,bc ;sha_code_gbc
scf
_
call.is try_unlock_sha
ldir
jr nz,++_
; If the model is too new, execute from cached Flash instead
jr nc,_
ld c,sha_code_size-1
sbc hl,bc
ld (gbc_render_tile_loop_smc_1),hl
ld (gbc_render_tile_loop_smc_2),hl
ld (gbc_render_tile_loop_smc_3),hl
ld c,gbc_render_sprite_row - gbc_render_tile_loop
add hl,bc
ld (gbc_render_sprite_row_smc_1),hl
ld (gbc_render_sprite_row_smc_2),hl
ld (gbc_render_sprite_row_smc_3),hl
ld c,gbc_render_sprite_row_second_half - gbc_render_sprite_row
add hl,bc
ld (gbc_render_sprite_row_second_half_smc),hl
ld c,gbc_render_sprite_row_first_half - gbc_render_sprite_row_second_half
add hl,bc
ld (gbc_render_sprite_row_first_half_smc),hl
jr ++_
_
ld c,sha_code_size - sha_code_entry_offset
sbc hl,bc
ld (convert_palette_row_smc_1),hl
ld (convert_palette_row_smc_2),hl
ld (convert_palette_row_smc_3),hl
_
ld hl,(rom_start)
ld (rom_unbanked_base),hl
ld (rom_unbanked_base_for_read),hl
ld a,(rom_trim_value)
ld (z80codebase+rom_trimmed_read_value),a
inc a
jr nz,_