-
Notifications
You must be signed in to change notification settings - Fork 25
/
ports.asm
1710 lines (1613 loc) · 33.2 KB
/
ports.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
; LUT to index I/O port and HRAM write routines
.block (mem_write_any_routines+256)-$
mem_write_port_lut:
;00
.db writeP1 - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db writeSC - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db writeDIV - mem_write_port_routines
.db writeTIMA - mem_write_port_routines
.db writeTMA - mem_write_port_routines
.db writeTAC - mem_write_port_routines
;08
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db writeIF - mem_write_port_routines
;10
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
;18
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
;20
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db write_audio - mem_write_port_routines
.db writeNR52 - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
;28
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
;30
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
;38
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
;40
.db writeLCDC - mem_write_port_routines
.db writeSTAT - mem_write_port_routines
.db write_scroll - mem_write_port_routines
.db write_scroll - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db writeLYC - mem_write_port_routines
.db writeDMA - mem_write_port_routines
.db writeBGP - mem_write_port_routines
;48
.db write_scroll - mem_write_port_routines
.db write_scroll - mem_write_port_routines
.db write_scroll - mem_write_port_routines
.db write_scroll - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db writeKEY1 - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db writeVBK - mem_write_port_routines
;50
.db write_port_ignore - mem_write_port_routines
.db write_hdma - mem_write_port_routines
.db write_hdma - mem_write_port_routines
.db write_hdma - mem_write_port_routines
.db write_hdma - mem_write_port_routines
.db writeHDMA5 - mem_write_port_routines
.db writeRP - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
;58
;60
.safe_fill $FF68 - $FF58, write_port_ignore - mem_write_port_routines
;68
.db write_palette_index - mem_write_port_routines
.db write_palette_data - mem_write_port_routines
.db write_palette_index - mem_write_port_routines
.db write_palette_data - mem_write_port_routines
.db writeOPRI - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
;70
.db writeSVBK - mem_write_port_routines
.db write_port_ignore - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db write_port_direct - mem_write_port_routines
.db writeFF75 - mem_write_port_routines
.safe_fill $FF80 - ($FF75+1), write_port_ignore - mem_write_port_routines
;80
.safe_fill IE - $FF80, write_hram_direct - mem_write_port_routines
;FF
.db writeIE - mem_write_port_routines
mem_write_port_routines:
; This set of routines is for direct writes to I/O ports or HRAM.
; Inputs: L=write value, B'=addr LSB, C'=cycle offset, DE'=cycle counter
; AFBCDEHL' are swapped
; Outputs: AFBCDEHL' are unswapped, A' is restored from E'
; Destroys: HL, BC', HL', F'
writeIEhandler:
ld e,a
writeIE:
exx
ld a,l
ld (IE),a
jr checkInt
writeIFhandler:
ld e,a
writeIF:
ld a,d
or a
call z,handle_events_for_mem_access
exx
ld a,l
and $1F
ld (active_ints),a
checkInt:
exx
; Check the pre-delay interrupt state, since if the interrupt enable
; delay is active then an interrupt check is already scheduled
ld a,(intstate_smc_2)
or a
jr z,checkIntDisabled
ld hl,(IE)
ld a,h
and l
jp nz,trigger_event
checkIntDisabled:
ld a,e
ex af,af'
exx
ret
writeTAChandler:
ld e,a
writeTAC:
call updateTIMA
jp.lil tac_write_helper
writeTIMAhandler:
ld e,a
writeTIMA:
call updateTIMA
jp nz,tima_write_helper
; Ignore writes directly on the reload cycle
jr checkIntDisabled
writeLCDChandler:
ld e,a
writeLCDC:
push bc
call updateSTAT
jp.lil lcdc_write_helper
writeSTAThandler:
ld e,a
writeSTAT:
push de
push bc
call updateSTAT
jp.lil stat_write_helper
writeLYChandler:
ld e,a
writeLYC:
exx
ld a,l
exx
ld hl,LYC
writeLYC_disable_smc = $
; Check for write value after the upcoming event line
writeLYC_event_line_smc = $+1
cp SCANLINES_PER_FRAME
jr c,writeLYC_nonpredicted
ld (hl),a
; Reset the LY=LYC coincidence bit
ld l,STAT & $FF
res 2,(hl)
writeLYC_same:
ld a,e
exx
ex af,af'
ret
writeLYC_nonpredicted:
; Check for a matching write
cp (hl)
jr z,writeLYC_same
ld (hl),a
push de
push bc
call updateSTAT
jp.lil lyc_write_helper
writeDIVhandler:
ld e,a
writeDIV:
call updateTIMA
jp.lil div_write_helper
writeSC:
jp _writeSC
writeKEY1:
jp _writeKEY1
writeHDMA5:
jp _writeHDMA5
;==============================================================================
; Everything above this point may cause a reschedule on write
;==============================================================================
write_hram_direct:
write_port_direct:
exx
ld a,l
exx
ld c,b
write_audio_finish:
ld b,$FF
write_hdma_finish:
ld (bc),a
write_port_ignore:
ld a,e
ex af,af'
exx
ret
write_audio_handler:
ex af,af'
ld e,a
write_audio:
write_audio_disable_smc = $
exx
ld a,l
exx
ld c,b
ld b,a
ld l,c
ld h,audio_port_value_base >> 8
ld (hl),b
set 7,l ;audio_port_masks
ld a,(hl)
; Handle writes to enable bits specially
or a
jp pe,write_audio_finish_fast
; For NR30, leave the high bit the same
; For NRx4, invert the high bit
xor b
jp p,write_audio_enable_disable
or b
jr write_audio_finish
write_hdma_handler:
ex af,af'
ld e,a
write_hdma:
exx
ld a,l
exx
ld c,b
ld b,hdma_port_value_base >> 8
jr write_hdma_finish
write_scroll:
jp _write_scroll
writeTMA:
jp _writeTMA
writeDMAhandler:
ld e,a
writeDMA:
call updateSTAT
push de
jp.lil dma_write_helper
writeBGPhandler:
ld e,a
writeBGP:
ld a,(BGP)
exx
cp l
exx
jr z,write_port_ignore
call updateSTAT
jp.lil BGP_write_helper
writeNR52:
jp _writeNR52
writeVBKhandler:
ld e,a
writeVBK:
exx
ld a,l
or $FE
ld (VBK),a
jp.lil writeVBK_helper
writeSVBK_stack:
; Adjust the direct stack pointer when it points to banked WRAM
ld c,a
dec h \ dec h
sub (hl)
add a,iyh
ld iyh,a
ld a,c
jr writeVBK_finish
writeP1:
jr _writeP1
writeRP:
jr _writeRP
writeOPRI:
jr _writeOPRI
writeFF75:
jr _writeFF75
write_palette_data:
djnz _write_palette_data
write_palette_index:
jr _write_palette_index
writeSVBKhandler:
ld e,a
writeSVBK:
exx
.echo mem_write_port_routines+256-$, " bytes remaining for port writes"
ld a,l
exx
or $F8
ld (SVBK),a
ld l,a
ld h,wram_bank_base_lut >> 8
ld a,(hl)
ld (wram_mirror_bank_base+1-z80codebase),a
add a,$20
ld l,(wram_bank_base_for_write+1) & $FF
ld (hl),a
writeSVBK_stack_smc = $
dec h \ dec h
writeVBK_finish:
ld (hl),a
dec h \ dec h
ld (hl),a
ld a,e
ex af,af'
exx
ret
writeP1handler:
ld e,a
_writeP1:
exx
ld a,l
exx
or $CF
bit 4,a
jr nz,_
keys_low = $+1
and $FF
_
bit 5,a
jr nz,_
keys_high = $+1
and $FF
_
ld (P1),a
ld a,e
ex af,af'
exx
ret
writeRPhandler:
ld e,a
_writeRP:
exx
ld a,l
exx
ld hl,RP
or $3E
cp h
jr nz,writeRP_finish
ld a,$FD ; Read own IR signal
jr writeRP_finish
writeOPRIhandler:
ld e,a
_writeOPRI:
exx
ld a,l
exx
ld hl,OPRI
or $FE
jr writeOPRI_finish
writeFF75handler:
ld e,a
_writeFF75:
exx
ld a,l
exx
ld hl,$FF75
or $8F
jr writeFF75_finish
write_palette_data_handler:
ex af,af'
ld e,a
_write_palette_data:
ld l,b
ld h,$FF
ld a,b
add a,(gbc_bg_palette_data >> 8) - (BGPI & $FF)
ld b,a
exx
ld a,l
exx
ld c,(hl)
write_palette_data_check_autoinc_smc = $
ld (bc),a
inc c
jr z,write_palette_data_autoinc_wrap
write_palette_data_autoinc_finish:
ld (hl),c
ld a,(bc)
write_palette_data_no_autoinc_finish:
inc hl
writeRP_finish:
writeOPRI_finish:
writeFF75_finish:
ld (hl),a
write_palette_index_finish:
ld a,e
ex af,af'
exx
ret
write_palette_index_handler:
ex af,af'
ld e,a
_write_palette_index:
ld l,b
ld h,$FF
ld a,b
add a,(gbc_bg_palette_data >> 8) - (BGPI & $FF)
ld b,a
exx
ld a,l
exx
ld c,a
; Check for an auto-increment change
xor (hl)
set 6,c
ld (hl),c
set 7,c
ld a,(bc)
inc hl
ld (hl),a
jp p,write_palette_index_finish
; Disable auto-increment checks only if both BG and OBJ are auto-increment
ld l,BGPI & $FF
ld a,(hl)
ld l,OBPI & $FF
and (hl)
ld hl,$18 | ((write_palette_data_check_autoinc - (write_palette_data_check_autoinc_smc+2)) << 8)
jp p,_
ld hl,$0C02 ; LD (BC),A \ INC C
_
ld (write_palette_data_check_autoinc_smc),hl
jr write_palette_index_finish
write_palette_data_check_autoinc:
bit 7,c
set 7,c
ld (bc),a
jr z,write_palette_data_no_autoinc_finish
inc c
jr nz,write_palette_data_autoinc_finish
write_palette_data_autoinc_wrap:
ld c,$C0
jr write_palette_data_autoinc_finish
writeSChandler:
ld e,a
_writeSC:
exx
ld a,l
exx
or $7E
ld (SC),a
inc a
jr nz,writeSC_disable
ld hl,serial_counter_checker
ld (event_counter_checker_slot_serial),hl
push de
; Get the current cycle offset in BC
ex de,hl
ld b,$FF
add hl,bc
ld b,h
ld c,l
; Get the DIV counter for the current cycle
ld hl,i
add hl,bc
; To make things simpler, since bits tick every 128 cycles,
; shift left once before overriding the low byte
add hl,hl
ld a,(serial_counter)
; Preserve the top bit of DIV in bit 0 while shifting left
rla
; Check whether the tick has already occurred
cp l
ld l,a
; Adjust the upper byte by 8 if the tick already occurred,
; otherwise by 7
ld a,h
adc a,7
ld h,a
; Rotate the counter back and save it
rra
rr l
rr h
ld (serial_counter),hl
; Get the relative time of the event from the currently scheduled event
ex de,hl
ld hl,i ; Resets carry
sbc hl,de
pop de
jp reschedule_event_resolved
writeSC_disable:
ld hl,disabled_counter_checker
ld (event_counter_checker_slot_serial),hl
writeTMA_finish:
ld a,e
ex af,af'
exx
ret
writeTMAhandler:
ld e,a
_writeTMA:
call updateTIMA
exx
ld a,l
exx
ld hl,TMA
ld (hl),a
; Subtract TMA from 256, without destroying Z
cpl
ld l,a
inc hl
; Check if the result was 256, without destroying Z
ld a,h
rlca
; Multiply by the timer factor
timer_cycles_reset_factor_smc = $+1
ld h,0
jr nc,_
mlt hl
_
ld (timer_period),hl
jr nz,writeTMA_finish
; Make sure writes on the reload cycle go through
ld hl,i ; Resets Z flag
add hl,bc
ld (timer_counter),hl
jr writeTMA_finish
writeNR52handler:
ld e,a
_writeNR52:
ld hl,NR52
ld a,(hl)
exx
xor l
ld a,l
exx
jp p,writeNR52_finish
; Whether enabling or disabling audio, all channels are off
and $80
or $70
ld (hl),a
; If enabling, un-ignore all writes
.db $21 ;LD HL,
exx
ld a,l
jp m,writeNR52_enable
push de
; Zero all audio registers
ld hl,audio_port_values
ld de,audio_port_values+1
ld bc,NR52 - NR10 - 1
ld (hl),b
ldir
; Copy masks to CPU-visible registers
ld l,audio_port_masks & $FF
ld de,NR10
ld c,NR52 - NR10
ldir
pop de
; If disabling, ignore all writes
ld hl,$18 | ((write_port_ignore - (write_audio_disable_smc+2)) << 8)
writeNR52_enable:
ld (write_audio_disable_smc),hl
writeNR52_finish:
write_scroll_no_change:
ld a,e
ex af,af'
exx
ret
write_scroll_handler:
ex af,af'
ld e,a
_write_scroll:
exx
ld a,l
exx
ld h,$FF
ld l,b
cp (hl)
jr z,write_scroll_no_change
push hl
call updateSTAT
jp.lil scroll_write_helper
writeKEY1handler:
ld e,a
_writeKEY1:
ld a,e
exx
srl l
ld hl,KEY1
rr (hl)
rlc (hl)
ex af,af'
ret
writeHDMA5handler:
ld e,a
_writeHDMA5:
ld hl,HDMA5
exx
ld a,l
exx
bit 7,(hl)
jr z,disableorupdateHDMA
add a,a
jp c,enableHDMA
rrca
ld (hl),a
; Save the original EI delay event and replace it
ld hl,(event_counter_checkers_ei_delay)
ld (gdma_event_restore_smc),hl
ld hl,gdma_transfer
ld (event_counter_checkers_ei_delay),hl
jp trigger_event
gdma_transfer:
gdma_event_restore_smc = $+1
ld hl,0
push hl
jp.lil gdma_transfer_helper
disableorupdateHDMA:
add a,$80
ld (hl),a
jr c,nodisableHDMA
ld hl,event_counter_checkers_ei_delay
ld (event_counter_checkers_ei_delay_smc_1),hl
ld (event_counter_checkers_ei_delay_smc_2),hl
ld hl,(event_counter_checkers_ei_delay_2)
ld (event_counter_checkers_ei_delay),hl
nodisableHDMA:
ld a,e
ex af,af'
exx
ret
schedule_hdma:
ld hl,event_counter_checkers_ei_delay
ld bc,(hl)
ld (hl),hdma_counter_checker & $FF
inc hl
ld (hl),hdma_counter_checker >> 8
inc hl
ld (hl),bc
ld (event_counter_checkers_ei_delay_smc_1),hl
ld (event_counter_checkers_ei_delay_smc_2),hl
ld a,(STAT)
and 3
dec a
jr z,schedule_hdma_vblank
rlca
CPU_SPEED_IMM8($+1)
ld hl,-MODE_0_CYCLES
jr nc,_
CPU_SPEED_IMM8($+1)
ld hl,MODE_2_CYCLES + MODE_3_CYCLES
_
ld a,(LY)
adc a,256-144
jr c,schedule_hdma_last_line
ld bc,(nextupdatecycle_LY)
schedule_hdma_finish:
sbc hl,bc
ld (hdma_counter),hl
ld (hdma_line_counter),a
ret
schedule_hdma_vblank:
CPU_SPEED_IMM16($+1)
ld hl,CYCLES_PER_VBLANK + MODE_2_CYCLES + MODE_3_CYCLES
jr _
schedule_hdma_last_line:
CPU_SPEED_IMM16($+1)
ld hl,CYCLES_PER_FRAME + CYCLES_PER_VBLANK + MODE_2_CYCLES + MODE_3_CYCLES
or a
_
ld bc,(vblank_counter)
ld a,256-144
jr schedule_hdma_finish
schedule_hdma_for_setup:
call schedule_hdma
ret.l
write_audio_enable_disable:
xor b
cp $80
jr z,write_audio_finish_fast
ld hl,NR52
jr c,writeNR30_disable
or b
ld b,h
ld (bc),a
; Set the appropriate bit in NR52
ld a,c
and 3
add a,-2
adc a,3
add a,a
daa
rra
or (hl)
ld (hl),a
ld a,e
ex af,af'
exx
ret
writeNR30_disable:
res 2,(hl)
write_audio_finish_fast:
or b
ld b,$FF
ld (bc),a
ld a,e
ex af,af'
exx
ret
;==============================================================================
; Port read handlers
;==============================================================================
unpatch_op_read_hl_normal:
inc a ; Switch from L access back to (HL)
pop hl
dec hl
ld (hl),a
dec hl
ld (hl),$49 ;.LIS
jr unpatch_op_read_hl_finish
unpatch_op_read_hl_port_l:
pop hl
ld a,(hl)
cp $CB ;bitwise prefix
jr c,unpatch_op_read_hl_normal
ld a,$6E ;LD L,(HL)
jr nz,unpatch_op_read_hl_tail_call ; Opcode was $EB == EX DE,HL
; BIT instruction, get the second opcode byte
inc hl
ld a,(hl)
pop hl
inc a ; Switch from L access back to (HL)
; Write the bitwise opcode
ld (hl),a
ld a,$CB
unpatch_op_read_hl_tail_call:
dec hl
ld (hl),a
dec hl
ld (hl),$5B ;.LIL
unpatch_op_read_hl_finish:
dec hl
ld (hl),RST_GET_HL_READ_PTR
; Restore the cycle counter
exx
ld a,e
ex af,af'
exx
jp (hl)
; Input: DE=Game Boy HL, C'=block cycle offset, BCDEHL' are swapped
; Output: L=read value
; Destroys: BC', E', HL', F'
op_read_hl_port_l:
ex af,af'
ld e,a
exx
ld a,d
inc a
jr nz,unpatch_op_read_hl_port_l
ld a,e
exx
op_read_hl_port_l_unchecked:
cp STAT & $FF
jr z,op_read_hl_STAT
cp LY & $FF
jr z,op_read_hl_LY
sub IF & $FF
jr z,op_read_hl_IF
add a,IF-TIMA
sbc a,$FF
jr z,op_read_hl_DIV_TIMA
ld a,e
exx
ex af,af'
ex de,hl
ld e,(hl)
ex de,hl
ret
op_read_hl_STAT:
; Quickly test to see if STAT is valid for this memory access
; Get the value of DIV at the end of the JIT block
ld hl,i
add hl,de
ld b,$FF
add hl,bc
ld bc,(nextupdatecycle_STAT)
add hl,bc
inc h
call nz,updateSTAT_fast
ld a,e
exx
ex af,af'
ex de,hl
ld e,(hl)
ex de,hl
ret
op_read_hl_LY:
ld hl,i
add hl,de
ld b,$FF
add hl,bc
ld bc,(nextupdatecycle_LY)
add hl,bc
inc h
call nz,updateLY_fast
ld a,e
exx
ex af,af'
ex de,hl
ld e,(hl)
ex de,hl
ret
op_read_hl_DIV_TIMA:
jr nc,op_read_hl_DIV
call updateTIMA
ld a,e
exx
ex af,af'
ex de,hl
ld e,(hl)
ex de,hl
ret
op_read_hl_IF:
or d
call z,handle_events_for_mem_access
ld a,(active_ints)
or $E0
exx
ld l,a
exx
ld a,e
ex af,af'
exx
ret
op_read_hl_DIV:
ld hl,i
add hl,de
ld b,$FF
add hl,bc
add hl,hl
add hl,hl
ld a,h
exx
ld l,a
exx
ld a,e
ex af,af'
exx
ret
port_read_any:
push de
exx
pop hl
push bc
push de
ld c,l
ld d,h
ld e,a
exx
call op_read_de_port_unchecked
exx
pop de
pop bc
exx
ret
unpatch_op_read_de_port:
exx
ld a,e
ex af,af'
exx
pop hl
dec hl
ld (hl),op_read_de_normal >> 8
dec hl
ld (hl),op_read_de_normal & $FF
dec hl
ld (hl),$CD
exx
ld a,e
ex af,af'
exx
jp (hl)
unpatch_op_read_bc_port:
ld a,e
ex af,af'
exx
pop hl
ld (hl),$D9 ;EXX