-
Notifications
You must be signed in to change notification settings - Fork 25
/
memory.asm
1668 lines (1547 loc) · 33.9 KB
/
memory.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
.assume adl=0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Routine tables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.block (push_routines_start+256)-$
mem_read_any_routines:
; This set of routines is for direct reads into the A register.
; Reads from (DE) and (BC) use these routines, and some absolute reads.
; The LSBs of these routines must match the get_ptr routine addresses.
; For (DE) and (BC) reads, the Z flag is reset on entry, and the JIT
; implementation may be patched for special read types.
; May also be used as generic read routines, if the Z flag is set.
; In the generic read case, a valid cycle offset must be passed.
; Inputs: AF' is swapped, BC=Game Boy address, Z=1 for generic read
; If generic read, BCDEHL' are swapped, and E=cycle offset
; Outputs: AF' is unswapped, A=read value
; If (DE) or (BC) read, JIT implementation may be patched
; Destroys: HL, F' (and for generic read, possibly E and BC)
rtc_latched = $
.db 0 ;seconds
.db 0 ;minutes
.db 0 ;hours
.dw 0 ;days
.fill 3,$FF ;non-registers
rtc_current = rtc_latched+8
;.db 0 ;seconds
;.db 0 ;minutes
;.db 0 ;hours
;.dw 0 ;days
;.block 3 ;non-registers
rtc_last = rtc_current+8
;.db 0 ;seconds
;.db 0 ;minutes
;.db 0 ;hours
;.dw 0 ;days
cram_open_bus_addr = rtc_last+5
;.dw 0
try_unlock_sha:
; 12 bytes of code, will be overwritten by RTC init
in0 a,($06)
ret.l z
set 2,a
out0 ($06),a
ret.l
; Reserved space for MBC writes
.block (mem_read_any_routines+(6*4)-1)-$
; Read-only regions come first for detection by stack handling
rom_banked_read_handler:
ex af,af'
rom_banked_read_any:
rom_bank_base_for_read = $+2+z80codebase
ld.lil hl,0
add.l hl,bc
ex af,af'
ld.l a,(hl)
ret
rom_unbanked_read_any:
rom_unbanked_base_for_read = $+2+z80codebase
ld.lil hl,0
add.l hl,bc
ex af,af'
ld.l a,(hl)
ret
rom_trimmed_read_any:
ex af,af'
rom_trimmed_read_value = $+1
ld a,0
ret
nop
nop
vram_banked_read_handler:
ex af,af'
vram_banked_read_any:
vram_bank_base_for_read = $+2+z80codebase
ld.lil hl,vram_base
add.l hl,bc
ex af,af'
ld.l a,(hl)
ret
; Read-write regions via long pointers start here
cram_banked_read_handler:
ex af,af'
cram_banked_read_any:
cram_banked_read_any_protect_smc = $
cram_bank_base_for_read = $+2+z80codebase
ld.lil hl,0
cram_banked_read_any_rtc_smc = $
cram_banked_read_any_mbc2_smc = $
add.l hl,bc
ex af,af'
ld.l a,(hl)
ret
nop
wram_banked_read_handler:
ex af,af'
wram_banked_read_any:
wram_bank_base_for_read = $+2+z80codebase
ld.lil hl,wram_gbc_base
_
add.l hl,bc
ex af,af'
ld.l a,(hl)
ret
wram_mirror_banked_read_any:
ld.lil hl,(wram_mirror_bank_base)
jr -_
wram_unbanked_read_any:
wram_unbanked_base_for_read = $+2+z80codebase
ld.lil hl,wram_base
_
add.l hl,bc
ex af,af'
ld.l a,(hl)
ret
wram_mirror_unbanked_read_any:
ld.lil hl,(wram_mirror_unbanked_base)
jr -_
; Read-write regions via short pointers start here
shadow_stack_read_any:
ld hl,(shadow_stack_base-z80codebase)
add hl,bc
ex af,af'
ld a,(hl)
ret
nop
nop
hmem_read_any:
jr nz,patch_bc_de_hmem_read
bit 7,c
jp z,port_read_any
oam_read_any:
ex af,af'
ld a,(bc)
ret
patch_bc_de_hmem_read:
ld hl,$FFFF-LY
add hl,bc
jp nc,patch_bc_de_port_read
; Patch to to a HRAM BC or DE read
pop hl
dec hl
dec hl
push af
ld a,(hl)
add a,op_read_de_hram-op_read_de_normal
ld (hl),a
inc hl
jr nc,_
inc (hl)
_
pop af
inc hl
ex af,af'
ld a,(bc)
jp (hl)
cram_open_bus_read_any:
jp do_cram_open_bus_read_any
cram_rtc_read_any:
; Patched in during setup if needed
;ex af,af'
;ld a,(hl)
;ret
cram_mbc2_read_any:
exx
pop hl
push hl
call do_cram_open_bus_read_any_with_return
ld l,a
push af
exx
ld a,b
srl b
ld b,$A0>>1
rl b
add.l hl,bc
ld b,a
ld.l a,(hl)
exx
xor l
and $0F
xor l
exx
ld l,a
pop af
ld a,l
ret
; GBC BG palette data
.block (mem_read_any_routines+256-64)-$
gbc_bg_palette_data:
.block 64
; LUT to index read routines.
; Additionally, direct memory region base pointers may be retrieved
; by adding 2 to the index and accessing from the get_ptr routine range.
.block (mem_read_any_routines+256)-$
mem_read_lut:
.fill $40, rom_unbanked_read_any & $FF
.fill $40, rom_banked_read_any & $FF
.fill $20, vram_banked_read_any & $FF
.fill $20, cram_banked_read_any & $FF
.fill $10, wram_unbanked_read_any & $FF
.fill $10, wram_banked_read_any & $FF
.fill $10, wram_mirror_unbanked_read_any & $FF
.fill $0E, wram_mirror_banked_read_any & $FF
.fill $01, oam_read_any & $FF
.fill $01, hmem_read_any & $FF
mem_get_ptr_routines:
; This set of routines is for direct pointer retrieval.
; Reads, writes, and modifies through (HL) use these routines.
; The LSBs of these routines must match the read_any/write_any addresses.
; For (HL) accesses, the Z flag is reset on entry, and the JIT
; implementation may be patched for special access types.
; May also be used as generic pointer getters, if the Z flag is set.
; Pointer retrieval may fail, in which case the caller must fall back on
; the direct read and/or write routines.
; Inputs: AF' is swapped, DE=Game Boy address, Z=1/C=0 for generic access
; Outputs: AF' is unswapped, UHL=direct pointer
; If (HL) access, JIT implementation may be patched.
; If generic access, the shadow C flag is set on failure.
; Destroys: HL, HL', F'
; This is used to identify I/O as a unique region (e.g. for stack accesses)
io_region_base = $+z80codebase
mbc_cram_protect_get_write_ptr:
jp nz,patch_hl_mbc_access
scf
ex af,af'
ret
mbc_rom_bank_get_write_ptr:
jp nz,patch_hl_mbc_access
scf
ex af,af'
ret
mbc_cram_bank_get_write_ptr:
jp nz,patch_hl_mbc_access
scf
ex af,af'
ret
mbc_specific_get_write_ptr:
jp nz,patch_hl_mbc_access
scf
ex af,af'
ret
rom_banked_get_ptr:
rom_bank_base = $+2+z80codebase
ld.lil hl,0
add.l hl,de
ex af,af'
ret
nop
nop
rom_unbanked_get_ptr:
rom_unbanked_base = $+2+z80codebase
ld.lil hl,0
add.l hl,de
ex af,af'
ret
nop
nop
rom_trimmed_get_ptr:
rom_trimmed_base = $+2+z80codebase
ld.lil hl,mpZeroPage
ex af,af'
ret
vram_banked_get_read_ptr:
vram_bank_base = $+2+z80codebase
ld.lil hl,vram_base
add.l hl,de
ex af,af'
ret
nop
nop
nop
cram_banked_get_ptr:
cram_banked_get_ptr_protect_smc = $
cram_bank_base = $+2+z80codebase
ld.lil hl,0
cram_banked_get_ptr_rtc_smc = $
cram_banked_get_ptr_mbc2_smc = $
add.l hl,de
ex af,af'
ret
cram_rtc_get_read_ptr_finish:
exx
pop af
ex af,af'
ret
wram_banked_get_ptr:
wram_bank_base = $+2+z80codebase
ld.lil hl,wram_gbc_base
_
add.l hl,de
ex af,af'
ret
nop
nop
wram_mirror_banked_get_ptr:
wram_mirror_bank_base = $+2+z80codebase
ld.lil hl,wram_gbc_base - $2000
jr -_
wram_unbanked_get_ptr:
wram_unbanked_base = $+2+z80codebase
ld.lil hl,wram_base
_
add.l hl,de
ex af,af'
ret
nop
nop
wram_mirror_unbanked_get_ptr:
wram_mirror_unbanked_base = $+2+z80codebase
ld.lil hl,wram_base-$2000
jr -_
shadow_stack_get_ptr:
shadow_stack_base = $+2+z80codebase
ld.lil hl,z80codebase
add.l hl,de
ex af,af'
ret
hmem_get_ptr:
; This is used for pointer lookups by the JIT engine, and only for reads,
; to more simply support HRAM execution.
hmem_unbanked_base = $+2+z80codebase
ld.lil hl,hram_base
jr hmem_get_pointer_impl
oam_get_ptr:
oam_unbanked_base = $+2+z80codebase
ld.lil hl,hram_base
add.l hl,de
ex af,af'
ret
vram_oam_get_write_ptr:
jp nz,patch_hl_vram_access
try_get_pointer_failed:
scf
ex af,af'
ret
hmem_get_pointer_impl:
jp nz,patch_hl_hmem_access
bit 7,e
jr z,try_get_pointer_failed
add.l hl,de
ex af,af'
ret
nop
cram_open_bus_get_ptr:
jp do_cram_open_bus_get_ptr
; This is treated as a union because MBC1 and RTC cannot coexist
mbc1_preserved_cram_bank_base = $+z80codebase
cram_rtc_get_ptr:
cram_mbc2_get_ptr:
; Check whether this is a read or write
exx
pop hl
push hl
push af
dec hl
ld a,(hl)
cp RST_GET_HL_READ_PTR
jr z,cram_rtc_get_read_ptr_finish
; Check if the RTC has changed since the last update
ld.lil a,(mpRtcIntStatus)
rra
call c,update_rtc
; Force a latch the next time one is requested
xor a
ld (mbc_rtc_latch_smc),a
exx
; Copy the read value to the write value in case of read-modify-write
ld a,(hl)
set 3,l
ld (hl),a
pop af
ex af,af'
ret
nop
; GBC OBJ palette data
.block (mem_get_ptr_routines+256-64)-$
gbc_obj_palette_data:
.block 64
; LUT to index write routines.
.block (mem_get_ptr_routines+256)-$
mem_write_lut:
.fill $20, mbc_cram_protect_get_write_ptr & $FF
.fill $20, mbc_rom_bank_get_write_ptr & $FF
.fill $20, mbc_cram_bank_get_write_ptr & $FF
.fill $20, mbc_specific_get_write_ptr & $FF
.fill $20, vram_oam_get_write_ptr & $FF
.fill $20, cram_banked_get_ptr & $FF
.fill $10, wram_unbanked_get_ptr & $FF
.fill $10, wram_banked_get_ptr & $FF
.fill $10, wram_mirror_unbanked_get_ptr & $FF
.fill $0E, wram_mirror_banked_get_ptr & $FF
.fill $01, vram_oam_get_write_ptr & $FF
.fill $01, hmem_get_ptr & $FF
mem_write_any_routines:
; This set of routines is for direct writes from the A register.
; Writes to (DE) and (BC) use these routines, and some absolute writes.
; The LSBs of these routines must match the get_ptr routine addresses.
; For (DE) and (BC) writes, the Z flag is reset on entry, and the JIT
; implementation may be patched for special write types.
; May also be used as generic write routines, if the Z flag is set.
; In the generic write case, a valid cycle offset must be passed.
; Inputs: AF' is swapped, BC=Game Boy address, Z=1 for generic write
; If generic write, BCDEHL' are swapped, and E=cycle offset
; Outputs: AF' is unswapped, A is written to address
; If (DE) or (BC) write, JIT implementation may be patched
; Destroys: HL, HL', F' (and for generic write, possibly E and BC)
; MBC write handlers come first for easy detection by absolute writes
mbc_cram_protect_write_any:
mbc_cram_protect_handler_smc = $+1
ld hl,mbc_write_cram_protect_handler
ex af,af'
jr handle_mbc_write_any
mbc_rom_bank_write_any:
ld hl,mbc_write_rom_bank_handler
ex af,af'
jr handle_mbc_write_any
mbc_cram_bank_write_any:
mbc1_write_large_rom_handler_smc = $+1
ld hl,mbc_write_cram_bank_handler
ex af,af'
jr handle_mbc_write_any
mbc_specific_write_any:
ld hl,mbc_write_denied_handler
; Reserved space for ROM/VRAM reads in other routine tables.
; Use this space for the MBC generic write handler.
; Since no additional information (e.g. cycles) is needed, just call
; the routine directly. We save BC' and DE' since the swap state of the
; shadow registers is arbitrary (unswapped for DE write, swapped otherwise)
handle_mbc_write_any:
ld (handle_mbc_write_any_smc),hl
push bc
push de
exx
ld l,a
handle_mbc_write_any_smc = $+1
call 0
exx
pop de
pop bc
ret
.block (mem_write_any_routines + (cram_banked_read_handler - mem_read_any_routines)) - $
cram_banked_write_handler:
ex af,af'
cram_banked_write_any:
cram_banked_write_any_protect_smc = $
cram_bank_base_for_write = $+2+z80codebase
ld.lil hl,0
cram_banked_write_any_rtc_smc = $
cram_banked_write_any_mbc2_smc = $
add.l hl,bc
ex af,af'
ld.l (hl),a
ret
nop
wram_banked_write_handler:
ex af,af'
wram_banked_write_any:
wram_bank_base_for_write = $+2+z80codebase
ld.lil hl,wram_gbc_base
_
add.l hl,bc
ex af,af'
ld.l (hl),a
ret
wram_mirror_banked_write_any:
ld.lil hl,(wram_mirror_bank_base)
jr -_
wram_unbanked_write_any:
wram_unbanked_base_for_write = $+2+z80codebase
ld.lil hl,wram_base
_
add.l hl,bc
ex af,af'
ld.l (hl),a
ret
wram_mirror_unbanked_write_any:
ld.lil hl,(wram_mirror_unbanked_base)
jr -_
shadow_stack_write_any:
ld hl,(shadow_stack_base-z80codebase)
add hl,bc
ex af,af'
ld (hl),a
ret
nop
nop
hmem_write_any:
jr nz,patch_bc_de_hmem_write
; This unswaps shadow registers, so swap them back before returning
call do_hmem_write_any
exx
ret
oam_write_any:
ex af,af'
ld (bc),a
ret
.block 6
vram_oam_write_any:
jp nz,patch_bc_de_vram_write
push bc
ld c,e
ld e,a
call updateSTAT
pop bc
; This unswaps shadow registers, so swap them back before returning
call do_vram_oam_write_any
exx
ret
nop
nop
nop
cram_open_bus_write_any:
ex af,af'
ret
nop
cram_rtc_write_any:
cram_mbc2_write_any:
exx
; Check if the RTC has changed since the last update
ld.lil hl,mpRtcIntStatus
bit.l 0,(hl)
call nz,update_rtc
; Force a latch the next time one is requested
ld hl,mbc_rtc_latch_smc
ld (hl),0
exx
set 3,l
ex af,af'
ld (hl),a
ret
patch_bc_de_hmem_write:
inc bc
ld hl,$007F
add hl,bc
dec bc
jp nc,patch_bc_de_port_write
; Patch to to a HRAM BC or DE write
pop hl
dec hl
dec hl
push af
ld a,(hl)
add a,op_write_de_hram-op_write_de_normal
ld (hl),a
inc hl
jr nc,_
inc (hl)
_
pop af
inc hl
ex af,af'
ld (bc),a
jp (hl)
; LUT for the middle byte of WRAM (mirror) bank bases
.block (mem_write_any_routines+256-8)-$
wram_bank_base_lut:
.db ((wram_gbc_base + $0000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $0000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $1000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $2000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $3000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $4000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $5000 - $2000) >> 8) & $FF
.db ((wram_gbc_base + $6000 - $2000) >> 8) & $FF
#include "ports.asm"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Generic memory access routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Input: DE=Game Boy address
; Output: Shadow carry set on failure
; On success, UHL=direct read address
; Destroys: HL', F'
try_get_mem_read_ptr:
ex af,af'
try_get_mem_read_ptr_swapped:
ld h,mem_read_lut >> 8
ld l,d
ld l,(hl)
inc h ;mem_get_ptr_routines
; Indicate a non-patchable caller
cp a ; Set Z flag, reset C flag
jp (hl)
; Input: DE=Game Boy address
; Output: Shadow carry set on failure
; On success, UHL=direct read/write address
; Destroys: F'
try_get_mem_readwrite_ptr:
ex af,af'
try_get_mem_readwrite_ptr_swapped:
ld h,mem_write_lut >> 8
ld l,d
ld l,(hl)
dec h ;mem_get_ptr_routines
; Indicate a non-patchable caller
cp a ; Set Z flag, reset C flag
jp (hl)
read_mem_any_stale_bus_next:
inc bc
inc e
ld a,$FF
; Input: BC'=Game Boy address, D',A'=cycle counter, E'=cycle offset, A=open bus value, BCDEHL' are swapped
; Output: A=read value, BCDEHL' are swapped
; Destroys: HL, E', HL', F'
read_mem_any:
ex af,af'
ld h,mem_read_lut >> 8
ld l,b
ld l,(hl)
dec h ;mem_read_any_routines
; Indicate a non-patchable caller
cp a ; Set Z flag
jp (hl)
; Input: BC'=Game Boy address, A=value to write, E'=cycle offset, BCDEHL' are swapped
; Output: Value is written to Game Boy address, BCDEHL' are swapped
; Destroys: HL, F', HL', E', BC'
write_mem_any:
ex af,af'
ld h,mem_write_lut >> 8
ld l,b
ld l,(hl)
inc h ;mem_write_any_routines
; Indicate a non-patchable caller
cp a ; Set Z flag
jp (hl)
generic_write_gb_address = $
.dw 0
do_hmem_write_any:
ex af,af'
exx
ld l,a
exx
ex af,af'
ld b,c
ld c,e
ld e,a
ld h,mem_write_port_lut >> 8
ld l,b
ld l,(hl)
inc h
jp (hl)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Slowmem patching routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
unpatch_hl_hram_access:
pop hl
ex af,af'
push af
; Convert .SIS to .LIS or .SIL to .LIL
ld a,(hl)
or $09
ld (hl),a
inc hl
ld a,(hl)
cp $CB
jr z,unpatch_hl_hram_access_bitwise
xor $36 ;LD (HL),n
jr z,unpatch_hl_hram_access_readwrite
and $07 ;LD r,(HL) or op A,(HL)
jr nz,unpatch_hl_hram_access_readwrite
unpatch_hl_hram_access_readonly:
dec hl
dec hl
ld (hl),RST_GET_HL_READ_PTR
pop af
jp (hl)
unpatch_hl_hram_access_bitwise:
inc hl
ld a,(hl)
dec hl
add a,$40
jp pe,unpatch_hl_hram_access_readonly
unpatch_hl_hram_access_readwrite:
dec hl
dec hl
ld (hl),RST_GET_HL_READWRITE_PTR
pop af
jp (hl)
patch_hl_hmem_access:
inc de
ld hl,$007F
add hl,de
dec de
jr nc,patch_hl_port_access
pop hl
dec hl
; Check for RST vs. (get_hl_readwrite_ptr_swapped >> 8) == 0
sla (hl)
jr nc,_
ex af,af'
ld (hl),RST_GET_HL_HRAM_PTR
inc hl
; Convert .LIS to .SIS or .LIL to .SIL
res 3,(hl)
res 0,(hl)
push de
ex (sp),hl
ret
_
pop hl
push hl
dec hl
ld (hl),do_swap_hl_hram >> 8
dec hl
ld (hl),do_swap_hl_hram & $FF
; Carry is reset, and AF is already unswapped
ld h,a
jp do_swap_hl_hram_finish
patch_hl_port_access:
exx
ld e,a
ex (sp),ix
ld a,(ix-1)
cp RST_GET_HL_READ_PTR
; Check for (get_hl_readwrite_ptr_swapped >> 8) == 0
jr c,patch_hl_port_readwrite_swap
push de
ld hl,(ix)
; Check for read
jp.lil z,patch_hl_port_read_helper
dec l ; Reset bit 0 to request a GB address
ld de,op_write_hl_port
jp.lil patch_hl_write_helper
patch_hl_vram_access:
exx
ld e,a
ex (sp),ix
ld a,(ix-1)
; Check for (get_hl_readwrite_ptr_swapped >> 8) == 0
or a
jr z,patch_hl_vram_readwrite_swap
push de
ld hl,(ix)
ld de,op_write_hl_vram
jp.lil patch_hl_write_helper
patch_hl_port_readwrite_swap:
ld hl,op_readwrite_hl_port_swap
ex af,af'
or a ; Request GB address
jr _
patch_hl_vram_readwrite_swap:
ld hl,op_readwrite_hl_vram_swap
ex af,af'
scf ; Request no GB address
_
pop ix
ex (sp),ix
ld e,a
push de
jp.lil patch_hl_readwrite_swap_helper
patch_bc_de_port_read:
; Get the address of the routine being called
ex (sp),ix
ld hl,(ix-2)
; Check EXX vs. EX AF,AF'
bit 7,(hl)
; Swap or unswap shadow registers
exx
ld hl,op_read_de_port
jr z,_
; For a BC write, shadow registers are now unswapped, so reswap
exx
ld hl,op_read_bc_port
; Remove the EXX following the call
ld (ix),0 ;NOP
; Indicate a following byte
scf
_
ld e,a
push de
jp.lil patch_bc_de_port_read_helper
patch_bc_de_port_write:
; Get the address of the routine being called
ex (sp),ix
ld hl,(ix-2)
; Check EXX vs. EX AF,AF'
bit 7,(hl)
; Swap or unswap shadow registers
exx
ld hl,op_write_de_port
jr z,_
; For a BC write, shadow registers are now unswapped, so reswap
exx
ld hl,op_write_bc_port
; Remove the EXX following the call
ld (ix),0 ;NOP
; Indicate a following byte
scf
_
ld e,a
push de
; Indicate a one-byte, two-cycle instruction and request GB address
ld a,$02
jp.lil patch_bc_de_write_helper
patch_bc_de_vram_write:
; Get the address of the routine being called
ex (sp),ix
ld hl,(ix-2)
; Clear carry
or a
; Check EXX vs. EX AF,AF'
bit 7,(hl)
; Swap or unswap shadow registers
exx
ld hl,op_write_de_vram
jr z,_
; For a BC write, shadow registers are now unswapped, so reswap
exx
ld hl,op_write_bc_vram
; Remove the EXX following the call
ld (ix),0 ;NOP
; Indicate a following byte
scf
_
ld e,a
push de
; Indicate a one-byte, two-cycle instruction and request no GB address
ld a,$42
jp.lil patch_bc_de_write_helper
patch_memory_access_finish:
pop de
ex (sp),ix
ld a,e
exx
ex af,af'
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read-only routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Input: IX=Game Boy BC
; Output: A=read value, BCDEHL' are swapped
; If not possible, JIT instruction is patched
; Destroys: BC', HL', F'
op_read_bc_normal:
exx
lea bc,ix
rom_any_read_handler:
; Input: BC=Game Boy DE
; Output: A=read value
; If not possible, JIT instruction is patched
; Destroys: HL, F'
op_read_de_normal:
ex af,af'
op_read_de_normal_swapped:
ld h,mem_read_lut >> 8
ld l,b
ld l,(hl)
; Implicitly reset Z flag to indicate a patchable caller
dec h ;mem_read_any_routines
jp (hl)
op_read_bc_hram:
exx
lea bc,ix
op_read_de_hram:
ex af,af'
ld hl,$FFFF-LY
add hl,bc
jr nc,_
ex af,af'
ld a,(bc)
ret
_
; Patch back to a normal BC or DE read
pop hl
push hl
dec hl
dec hl
push af
ld a,(hl)
sub op_read_de_hram-op_read_de_normal
ld (hl),a
jr nc,_
inc hl
dec (hl)
_
pop af
jr op_read_de_normal_swapped
do_cram_open_bus_read_any:
; Get the return address
pop hl
push hl
do_cram_open_bus_read_any_with_return:
push af
call.il lookup_code_bus
jr c,do_cram_open_bus_stale_read
pop af
; Get the address of the routine call
dec hl
dec hl
ld hl,(hl)
; If the Z flag is reset, it may be a BC/DE read
; If the Z flag is set, it may be a generic read
; For LD A,(nnnn) the Z flag input is arbitrary
jr z,do_cram_open_bus_read_not_bcde
bit 7,(hl) ; Check for EXX
jr nz,do_cram_open_bus_read_bc
inc hl
bit 2,(hl) ; Check for LD H,n or JR
jr nz,do_cram_open_bus_read_de
do_cram_open_bus_read_absolute:
; For LD A,(nnnn) read the MSB of the address
ex af,af'
ld a,b
ret
do_cram_open_bus_read_not_bcde:
inc hl
bit 2,(hl) ; Check for LD H,n/INC E or JR
jr z,do_cram_open_bus_read_absolute
; For generic reads, preserve the input open bus value
ex af,af'
ret