-
Notifications
You must be signed in to change notification settings - Fork 25
/
jit.asm
4572 lines (4388 loc) · 94.9 KB
/
jit.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
#define RST_GET_HL_READ_PTR $C7+r_get_hl_read_ptr
#define RST_GET_HL_READWRITE_PTR $C7+r_get_hl_readwrite_ptr
#define RST_GET_HL_HRAM_PTR $C7+r_get_hl_hram_ptr
#define RST_EVENT $C7+r_event
#define RST_CALL $C7+r_call
#define RST_CYCLE_CHECK $C7+r_cycle_check
#define RAM_PREFIX_SIZE 7
; Define such that cycles per block (at most 4x the bytes) is at most 249.
; This limitation is for two reasons:
; 1) Up to 6 cycles may be combined with the block cycle count for a taken CALL
; 2) Sub-block overflow and prefixed instruction detection when stepping through
; a block can be differentiated by range. Sub-block overflow gives a result
; no lower than -5 (aka 251), and a prefixed instruction (subtracting -1
; from the current cycle count gives a value no higher than 250.
#define MAX_OPCODE_BYTES_PER_BLOCK 62
#define MAX_BYTES_PER_OPCODE 3
#define MAX_CACHE_FLUSHES_ALLOWED 2
; The default value for no cycle offset available. Any non-negative offset
; is considered invalid, but the offset may be decremented by 1 during
; instructions, so it must stay non-negative in those cases.
#define NO_CYCLE_INFO 1
; This bit specifies that a memory access cannot cause a reschedule,
; so the Game Boy address will not be included in the cycle cache.
#define NO_RESCHEDULE 2
cache_flushes_allowed:
.db 0
; Do as in flush_code, but also reset RAM block padding amount.
; This is called only at startup, because block padding should
; persist between flushes to reduce flush rates overall.
flush_code_reset_padding:
; Set RAM padding to minimum (none)
or a
sbc hl,hl
ld (ram_block_padding),hl
; Flushes the recompiled code, generated routines, and all caches.
; Inputs: None.
; Destroys: AF, BC, HL, IX
flush_code:
push de
#ifdef DEBUG
APRINTF(FlushMessage)
#endif
#ifdef FASTLOG
FASTLOG_EVENT(JIT_FLUSH, 0)
#endif
; Empty recompiled code information struct
ld hl,recompile_struct
ld (hl),hl
ld l,8
ld (recompile_struct_end),hl
; Store first available block address to the first unused entry
ld de,z80codebase+jit_start
ld (hl),de
; Set the next memory access routine output below the Z80 stack
ld hl,z80codebase+trampoline_end
ld (z80codebase+trampoline_next),hl
ld de,ERROR_CATCHER
ld (hl),de
; Empty the recompiled code mapping cache
ld hl,recompile_cache_end
ld (recompile_cache),hl
; Fill unused memory with DI to catch bad execution
MEMSET_FAST(z80codebase+jit_start, trampoline_end - jit_start, $F3)
; Invalidate the memory routines, recompile index, and recompile cache LUT
MEMSET_FAST(recompile_index_LUT, $0300, 0)
MEMSET_FAST(recompile_cache_LUT+256, 256, (recompile_cache_end>>8)&$FF)
; Reset the event address
ld hl,event_value
ld.sis (event_address),hl
; Reset the max allowed cache flushes
ld a,MAX_CACHE_FLUSHES_ALLOWED
ld (cache_flushes_allowed),a
; Reset the RST target caches
ld ix,z80codebase+do_rst_00
ld de,decode_rst
ld b,8
xor a
_
ld hl,(ix+2)
ld.s (hl),a ;0-cycle dispatch
inc hl
inc hl
ld.s (hl),de ;decode_rst
lea ix,ix+do_rst_08-do_rst_00
djnz -_
; Resolve the interrupt target caches
ld hl,z80codebase+dispatch_vblank+2
ld d,a
ld e,$40
dec a
ld (recompile_cycle_offset_sp),a
_
push de
push hl
call lookup_code
pop hl
pop de
ld.s (hl),ix
dec hl
dec hl
; Add 5 cycles for taken interrupt
add a,5
ld (hl),a
inc hl
ld a,e
add a,8
ld e,a
sla l
jp p,-_
pop de
ret
; Gets a 24-bit unique identifier for a given Game Boy address, using the current GB memory map.
;
; Inputs: DE = GB address
; Outputs: DE = GB address plus (bank << 24), depending on region
; Destroys AF, HL
get_banked_address:
ld a,d
add a,$40
ret po
ld hl,(z80codebase+curr_rom_bank-2)
ld h,d
ld l,e
ex de,hl
ret
; Looks up the containing code block for a given code pointer and determines
; whether it is executing on external or internal bus. This is used for
; emulating open bus reads.
;
; Inputs: HL = 16-bit Z80 code pointer
; Outputs: C flag set if on internal bus
; A = $FF if C flag is set, for convenience
; Destroys: A
lookup_code_bus:
push ix
push de
push hl
ld ix,recompile_struct
ld de,recompile_index_LUT
ld e,h
ld a,(de)
ld ixl,a
inc d
ld a,(de)
ld ixh,a
ld a,l
cpl
ld e,a
ld a,h
cpl
ld d,a
call lookup_code_block_loop
pop hl
pop de
; For now, just treat OAM/HRAM as internal
ld a,(ix+3)
add a,2
sbc a,a
pop ix
ret.l
do_pop_instr_get_cycle_offset_helper:
; Check the original instruction
ld a,c
cp $F1 ;POP AF
push.s bc
; Get the JIT address after the pop routine call
ld hl,4
add.s hl,sp
ld.s bc,(hl)
; Skip the EX AF,AF' if not POP AF
jr z,_
inc bc
_
push ix
push de
call lookup_gb_code_address
pop de
; Get the cycle offset of the first pop read
cpl
dec a
ld e,a
pop ix
pop.s bc
jp.sis do_pop_instr_get_cycle_offset_return
do_push_instr_get_cycle_offset_helper:
ex af,af'
push.s af
; Set call cycle offset of 0
ld l,0
push.s hl
exx
push.s hl
ld a,h
exx
push ix
ld l,a
push hl
push de
call lookup_gb_code_address
; Save the lookup results for use by event triggers
ld.sis (generic_write_jit_address),bc
ld.sis (generic_write_gb_address),de
ld (z80codebase+generic_write_instr_cycle_offset),a
pop bc
; Get the cycle offset of the first push write
cpl
dec a
ld e,a
pop hl
ld d,h
ld a,l
pop ix
jp.sis do_push_instr_get_cycle_offset_return
; Gets the recompile struct entry for a given code pointer.
;
; Inputs: BC = 16-bit Z80 code pointer
; Outputs: IX = struct entry or $xx0000 if not found
; Destroys AF,DE,HL
lookup_code_block:
#ifdef 0
push bc
APRINTF(LookupGBMessage)
pop bc
#endif
ld ix,recompile_struct
ld hl,recompile_index_LUT
ld l,b
ld a,(hl)
ld ixl,a
inc h
ld a,(hl)
ld ixh,a
ld a,c
cpl
ld e,a
ld a,b
cpl
ld d,a
lookup_code_block_loop:
ld hl,(ix)
add.s hl,de
ret nc
ld hl,(ix-8)
add.s hl,de
jr nc,_
ld hl,(ix-16)
add.s hl,de
jr nc,++_
ld hl,(ix-24)
add.s hl,de
lea ix,ix-32
jr c,lookup_code_block_loop
lea ix,ix+8
ret
_
lea ix,ix-8
ret
_
lea ix,ix-16
ret
Z80InvalidOpcode_helper:
exx
pop.s bc
dec bc
dec bc
dec bc
#ifdef DEBUG
; Open debugger on CEmu
ld (exitReason),a
ld a,2
ld ($FFFFFF),a
ld a,(exitReason)
#endif
#ifdef FASTLOG
exx
ex af,af'
push af
push bc
push de
push hl
exx
ex af,af'
push af
push bc
push de
push hl
push ix
push iy
#endif
call lookup_gb_code_address
call get_banked_address
ld (errorArg),de
#ifdef FASTLOG
push de
FASTLOG_EVENT(INVALID_OPCODE, 33)
#endif
#ifdef DEBUG
push bc
APRINTF(InvalidOpcodeErrorMessage)
pop bc
#endif
ld a,(ERROR_INVALID_OPCODE << 2) + 5
jr runtime_error_finish
runtime_error:
#ifdef CEMU
; Open debugger on CEmu
ld (exitReason),a
ld a,2
ld ($FFFFFF),a
ld a,(exitReason)
#endif
#ifdef FASTLOG
exx
ex af,af'
push af
push bc
push de
push hl
exx
ex af,af'
push af
push bc
push de
push hl
push ix
push iy
FASTLOG_EVENT(RUNTIME_ERROR, 30)
#endif
#ifdef DEBUG
APRINTF(RuntimeErrorMessage)
#endif
ld a,(ERROR_RUNTIME << 2) + 5
runtime_error_finish:
ld (exitReason),a
; Temporarily prevent auto state saving because state is unrecoverable
xor a
#ifdef FASTLOG
call fastlog_dump_to_save
#endif
ld (should_auto_save),a
AJUMP(ExitEmulationWithoutState)
lookup_gb_found_start:
ld a,(ix+7)
bit 7,d
ret z
xor a
ret
lookup_gb_found_abs_read_write:
; Add a cycle for the memory access
dec a
; Get the real JIT pointer in HL
pop bc
push bc
push hl
lea hl,ix
add hl,bc
ld bc,3
; Check for LDH
bit 1,e
jr z,lookup_gb_found_abs_read_write_high
; Add another cycle
dec a
; Check the first JIT byte for short read/write
; This checks for a LD (nn),A or JR instruction,
; as opposed to .LIL, LD L,A, or EXX
bit.s 0,(hl)
jr z,lookup_gb_pop_add
; Check the second JIT byte for long read/write
; This checks for a LD (nnn),A or LD A,(nnn) instruction,
; as opposed to CALL, EXX, LD C,n, or LD BC,nnnn
inc hl
bit.s 5,(hl)
pop hl
ld c,5
#ifdef FASTLOG
jp nz,lookup_gb_add
#else
jr nz,lookup_gb_add
#endif
; Default to an 8-byte implementation
ld c,8
; Decrement to the MSB of the accessed address
dec hl
; Check for read vs. write
bit 4,e
jr z,lookup_gb_found_abs_write
; Check for a port read
ld e,(hl)
inc hl
inc e
jr nz,lookup_gb_add
; Port reads are 6 bytes
ld c,6
jr lookup_gb_add
lookup_gb_found_abs_write:
; Check for an MBC write
bit 7,(hl)
inc hl
jr nz,lookup_gb_add
; MBC writes are 4 bytes
ld c,4
jr lookup_gb_add
runtime_error_trampoline:
#ifdef FASTLOG
jp runtime_error
#else
jr runtime_error
#endif
lookup_gb_found_abs_read_write_high:
; Check the first JIT byte for short read/write
; This checks for a LD (nn),A or JR instruction,
; as opposed to .LIL, LD L,A, or EXX
bit.s 0,(hl)
lookup_gb_pop_add:
pop hl
jr z,lookup_gb_add
; Check for read vs. write
bit 4,e
; Port reads are 6 bytes
ld c,6
jr nz,lookup_gb_add
; Port writes are 8 bytes
ld c,8 ;8-6
jr lookup_gb_add
; Gets the Game Boy opcode address from a recompiled code pointer.
;
; The pointer must point either to the start of a recompiled instruction
; or directly following the end of the last recompiled instruction in a block.
;
; Locating the code block is O(log N) in number of blocks.
; Locating the address within the block is O(N) in number of instructions.
;
; Inputs: BC = 16-bit Z80 code pointer
; Outputs: DE = 16-bit GB code address
; A = Number of cycles until sub-block end
; Destroys F,HL,IX
lookup_gb_code_address:
#ifdef 0
push bc
APRINTF(LookupGBMessage)
pop bc
#endif
#ifdef FASTLOG
push bc
FASTLOG_EVENT(LOOKUP_GB, 2)
inc sp
#endif
call lookup_code_block
ld a,ixh
or ixl
jr z,runtime_error_trampoline
ld de,(ix)
ld hl,(ix+2)
ex.s de,hl
sbc hl,bc
jr z,lookup_gb_found_start
push hl
GET_BASE_ADDR_NO_ASSERT
#ifdef 0
ld a,d
sub $40
cp $40
jr nc,_
ld a,(z80codebase+curr_rom_bank)
cp (ix+4)
jr nz,$
_
#endif
; Get the LSB of the block end for overlap detection
ld a,l
add a,e
add a,(ix+5)
inc a
ld (lookup_gb_overlap_smc),a
; Get the cycle count of the first sub-block
ld a,(ix+7)
pop ix
push hl
push bc
add hl,de
bit 7,d ; Check whether the GB address is in ROM
ld de,opcoderecsizes
ld bc,RAM_PREFIX_SIZE
jr nz,lookup_gb_add
lookup_gb_found_loop:
ld e,(hl)
ex de,hl
inc h
inc h
ld c,(hl)
ex de,hl
add hl,bc
ex de,hl
dec h
sub (hl)
dec h
ld c,(hl)
ex de,hl
jr c,lookup_gb_new_sub_block
lookup_gb_add:
add ix,bc
jr nc,lookup_gb_found_loop
dec ix
add ix,ix
jr nc,runtime_error_trampoline
lookup_gb_finish_overlapped:
pop bc
lookup_gb_finish:
pop de
or a
sbc hl,de
ex de,hl
#ifdef 0
push af
push de
push bc
APRINTF(LookupGBFoundMessage)
pop bc
pop de
pop af
#endif
#ifdef FASTLOG
push af
push de
FASTLOG_EVENT(LOOKUP_GB_FOUND, 2)
inc sp
pop af
#endif
ret
lookup_gb_new_sub_block_end:
dec ix
add ix,bc
#ifdef FASTLOG
jp nc,runtime_error
#else
jr nc,runtime_error_trampoline
#endif
jr nz,_
inc ix ; For RET/JR/JP/RST, count is at -4 bytes
_
add.s a,(ix-4) ; For CALL, count is at -5 bytes
ASSERT_C
jr lookup_gb_finish
lookup_gb_new_sub_block:
cp -5
jr c,lookup_gb_variable_length
; Note: for an overlapped instruction, the following transformations
; may end up incorrect, but they will be discarded after the bounds check
inc e
bit 2,e
jr z,_
inc bc ; For CALL, offsets are stored -1
_
add ix,bc
pop bc
; This jump will not be taken for an overlapped instruction
jr c,lookup_gb_new_sub_block_end
push hl
lea hl,ix-4 ; For RET/JR/JP/RST, count is at -4 bytes
jr z,_
dec hl ; For CALL, count is at -5 bytes
_
add hl,bc
add.s a,(hl)
pop hl
push bc
lookup_gb_overlap_smc = $+1
ld bc,0
; Check if the end of the block was exceeded
ld e,a
ld a,c
sub l
rla
ld a,e
jr nc,lookup_gb_found_loop
lookup_gb_found_overlapped:
; TODO: error checking?
; Cycle offset after instruction should always be 0
xor a
jr lookup_gb_finish_overlapped
lookup_gb_variable_length:
; Count CB-prefixed opcode cycles
sub 1+2
; If the cycle count overflowed from a CB-prefix instruction,
; this means it must have been an overlapped instruction
; This path is also taken if we reached a HALT
jr c,lookup_gb_found_overlapped_or_halt
; Differentiate CB prefix and absolute reads/writes
bit 0,e
jp z,lookup_gb_found_abs_read_write
; Look up the second byte in the CB opcode table
dec hl
ld e,(hl)
inc hl
ex de,hl
dec h
ld c,(hl)
inc h
ex de,hl
; Check for (HL) access
srl c
jr nc,lookup_gb_add
; Check for BIT b,(HL)
jr nz,_
ld c,4
dec a ; Adjust cycles for read
jr lookup_gb_add
_
sub 2 ; Adjust cycles for read/write
jr lookup_gb_add
lookup_gb_found_overlapped_or_halt:
; Check if the opcode was a HALT or not
inc a
jr nz,lookup_gb_found_overlapped
; If it was a HALT, get the bugged instruction byte
dec hl
dec hl
ld e,(hl)
dec hl
; Check if at the start of the bugged instruction
ld c,9
add ix,bc
jr nc,_
; Get the cycle count of the bugged instruction
pop bc
dec bc
ld.s a,(bc)
inc bc
dec a
#ifdef FASTLOG
jp lookup_gb_finish
#else
jr lookup_gb_finish
#endif
_
; If not, determine the address after the bugged instruction
ld a,(de)
ld c,a
add hl,bc
; Cycle count will be 0
jr lookup_gb_found_overlapped
dynamic_jp_mismatch:
ld.s de,(ix+2)
pop hl
lea hl,ix-1
ld ix,recompile_struct
add ix,de
ld de,(ix)
ld bc,RAM_PREFIX_SIZE
ld.s (hl),$CD ;CALL
ldir.s
push de
exx
pop hl
exx
jp rerecompile_popped
; When a match is found, load it from the cache
lookup_code_cached_found:
ld a,(ix-3)
ld ix,(ix-5)
ret.l
lookup_code_cached_for_dynamic_jp:
ld a,(hl)
cp $C3
jr nz,dynamic_jp_mismatch
inc hl
ld hl,(hl)
ex.s de,hl
; Looks up a Game Boy address from the cached code mappings, and
; adds it to the cache upon miss.
;
; Lookups are O(log N) in number of cache entries.
; Insertions are O(N) (with LDIR constant factor), plus code lookup overhead.
;
; Inputs: DE = Game Boy address
; Outputs: IX = recompiled code address
; DE = Game Boy address (banked)
; A = number of cycles until block end
; Destroys AF,BC,DE,HL
lookup_code_cached:
; Get the banked address in DE
call get_banked_address
lookup_code_cached_with_bank:
; Search the cache for the pointer, indexing by the LSB
ld ix,recompile_cache_end
ld hl,recompile_cache_LUT
ld l,e
ld a,(hl)
dec l
ld c,(hl)
inc h
ld b,(hl)
inc l
jr z,_
ld ixl,c
ld ixh,b
_
sub ixl
jr z,lookup_code_cached_miss
ld bc,-5
lookup_code_cached_loop:
ld hl,(ix-3)
ld l,e
or a
sbc hl,de
jr z,lookup_code_cached_found
add ix,bc
sub c
jr nz,lookup_code_cached_loop
; If a cached code lookup misses, resolve it and insert into the cache
lookup_code_cached_miss:
push de
push ix
#ifdef DEBUG
push de
APRINTF(CacheMissMessage)
pop de
#endif
#ifdef FASTLOG
push de
FASTLOG_EVENT(CACHE_MISS, 3)
#endif
call lookup_code_with_bank
; Check if the cache needs to be flushed
ld hl,(recompile_struct_end)
ld de,(recompile_cache)
ld bc,3+5
add hl,bc
sbc hl,de
jr c,_
; Allow a certain number of cache flushes before flushing the entire JIT space
ld hl,cache_flushes_allowed
dec (hl)
; Get the GB banked pointer in HL
pop de
pop hl
push hl
push de
; If no cache flushes remaining, prepare a JIT flush (replacing IX and A)
call z,prepare_flush
; Flush the cache
call flush_cache
ex de,hl
_
pop hl
; Copy back the entries below the insert point to make room
or a
sbc hl,de
jr nc,_
cp a ;Set Z
_
ld b,h
ld c,l
ld hl,-5
add hl,de
ld (recompile_cache),hl
jr z,_
ex de,hl
ldir
ex de,hl
_
pop de
; Assign to the new entry
ld (hl),ix
inc hl
inc hl
ld (hl),de
ld (hl),a
; Update range bounds for each LSB range copied back
ld hl,recompile_cache_LUT + 5
ld b,l
ld l,e
ld c,a
_
ld a,(hl)
sub b
ld (hl),a
jr c,_
inc l
jr nz,-_
ld a,c
ret.l
_
inc h
dec (hl)
dec h
inc l
jr nz,--_
ld a,c
ret.l
lookup_found_special:
; Check whether this was a cycle overflow or a variable-length impl
cp -5
jr c,lookup_found_variable_length
; Found a new sub-block
; Add cycles for the next sub-block
add.s a,(ix-4)
ASSERT_C
inc e
bit 2,e
jr z,_ ; For RET/JR/JP/RST, offsets are normal
inc ix ; For CALL, offsets are stored -1
_
add hl,bc
add iy,bc
jr nc,lookup_found_loop
jr lookup_found_loop_finish
lookup_found_variable_length:
; Count variable-length opcode cycles (assume cycles=opcode bytes)
sbc a,c
; If we're stepping past a HALT, then continue
jr c,lookup_found_continue
; Differentiate CB prefix and absolute reads/writes
bit 0,e
jr z,lookup_found_abs_read_write
; Look up the second byte in the CB opcode table
inc hl
ld e,(hl)
inc hl
ex de,hl
dec h
ld c,(hl)
inc h
ex de,hl
; Check for (HL) access
srl c
jr nc,++_
; Check for BIT b,(HL)
jr nz,_
; Set actual recompiled code size
ld c,4
; Adjust cycle count for read
inc a
_
; Adjust cycle count for read/write
sub 2
_
; Add actual recompiled code size
add ix,bc
; Restore GB instruction size
ld c,2
jr lookup_found_continue_2
lookup_code_in_block:
add.s hl,de
push de
ex de,hl
GET_BASE_ADDR_FAST
add hl,de
ld de,opcoderecsizes
lookup_found_loop:
; Get current opcode
ld e,(hl)
ex de,hl
; Add recompiled instruction size
ld c,(hl)
add ix,bc
; Add cycles
inc h
sub (hl)
inc h
; Add GB instruction size
ld c,(hl)
dec h
dec h
ex de,hl
jr c,lookup_found_special
lookup_found_continue:
add hl,bc
lookup_found_continue_2:
add iy,bc
jr nc,lookup_found_loop
lookup_found_loop_finish:
pop de
dec iy
add iy,iy
ret
lookup_found_abs_read_write:
; Add a cycle for the memory access
dec a
; Check the first JIT byte for short read/write
; This checks for a LD (nn),A instruction,
; as opposed to .LIL, LD L,A, or EXX
bit.s 0,(ix)
lea ix,ix+3
jr z,lookup_found_continue
; Check for LDH
bit 0,c
jr z,lookup_found_abs_read_write_high
; Check the second JIT byte for long read/write
; This checks for a LD (nnn),A or LD A,(nnn) instruction,
; as opposed to CALL, EXX, LD C,n, or LD BC,nnnn
bit.s 5,(ix-3+1)
lea ix,ix+5-3
jr nz,lookup_found_continue
; Default to an 8-byte implementation
add ix,bc ;8-5
; Advance to the MSB of the accessed address
inc hl
inc hl
; Check for read vs. write
bit 4,e
jr z,lookup_found_abs_write
; Check for a port read
ld e,(hl)
inc hl
inc e
jr nz,lookup_found_continue_2
; Port reads are 6 bytes
lea ix,ix-2
jr lookup_found_continue_2
lookup_found_abs_write:
; Check for an MBC write
bit 7,(hl)
inc hl
jr nz,lookup_found_continue_2
; MBC writes are 4 bytes
lea ix,ix-4
jr lookup_found_continue_2
lookup_found_abs_read_write_high:
; Check for read vs. write
bit 4,e
; Port reads are 6 bytes
lea ix,ix+6-3
jr nz,lookup_found_continue
; Port writes are 8 bytes
add ix,bc ;8-6
jr lookup_found_continue
lookup_code_link_internal_with_bank_cached:
call.il lookup_code_cached_with_bank
or a
ret
internal_found_start:
ld a,(ix+7)
ld ix,(ix)
lea ix,ix+RAM_PREFIX_SIZE
scf
ret
lookup_code_link_internal:
call get_banked_address
; Looks up a recompiled code pointer from a banked GB address,
; allowing a direct link within the currently executing RAM block.
; Normally, only the start of a RAM block is allowed for direct links.
; If executing from ROM or target is not the same block, proceed as normal.
;
; Inputs: DE = banked GB address to look up
; IX = struct pointer of the currently executing block
; Outputs: IX = recompiled code pointer
; A = number of cycles until block end
; Carry is set if it is valid to use waitloop detection on the result
; Destroys AF,BC,DE,HL
lookup_code_link_internal_with_bank:
ld hl,(ix+2)
bit 7,h
jr z,lookup_code_with_bank
#ifdef FASTLOG
ex (sp),hl
push hl
push hl
push hl
push de