-
Notifications
You must be signed in to change notification settings - Fork 25
/
stack.asm
1295 lines (1234 loc) · 27.3 KB
/
stack.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Stack pointer access routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ADD HL,SP
ophandler39:
push de
; The starting Game Boy address of the current stack window, minus $7F
stack_window_base = $+1
ld hl,0
; Get the current stack window offset and add it
ld e,iyl
ld d,0
add hl,de
pop de
add hl,de
ex de,hl
ret
; LD HL,SP+0
ophandlerF8_zero:
; Get the value of SP in DE
ld hl,(stack_window_base)
ld d,e
ld e,iyl
add hl,de
ex de,hl
; Reset all flags
ld hl,i
ret
; LD HL,SP-nn
ophandlerF8_negative:
ld d,a
; Get the value of SP minus 256 in HL
; This transforms it into an unsigned addition
ld hl,(stack_window_base)
ld a,l
add a,iyl
jr c,_
dec h
jr _
; LD HL,SP+nn
ophandlerF8_positive:
ld d,a
; Get the value of SP in HL
ld hl,(stack_window_base)
ld a,l
add a,iyl
jr nc,_
inc h
_
; Add the positive offset and set N/H/C flags
add a,e
ld l,a
ld a,d
; Put result in DE
ex de,hl
; If no carry, we are guaranteed NZ because we added at least one
ret nc
; Increment D without destroying flags
ex af,af'
inc d
ex af,af'
; If NZ, flags are correct
ret nz
; Reset Z flag but preserve H/C flags and keep N flag reset.
ld a,$04
daa ; Resets H but increases low nibble to $A if H was set.
daa ; Iff low nibble is $A, sets H. Z is reset always.
ld a,h
ret
; LD [nnnn],SP
ophandler08_fast:
ex af,af'
ld b,d
ld c,a
ex de,hl
call try_get_mem_readwrite_ptr_swapped
ex af,af'
; Calculate the real value of SP
ld de,(stack_window_base)
ld a,e
add a,iyl
jr nc,_
inc d
_
; Write to the memory addresses
ld.l (hl),a
inc.l hl
ld.l (hl),d
ld d,b
ld a,c
ex af,af'
exx
ret
; LD [FFnn],sp
ophandler08_hram:
ex af,af'
ld e,a
ld bc,(stack_window_base)
ld a,c
add a,iyl
ld c,a
jr nc,_
inc b
_
ld (hl),bc
ld a,e
ex af,af'
exx
ret
; LD [nnnn],SP
ophandler08_slow:
; Save the GB address provided by the trampoline
ld (generic_write_gb_address),hl
; Get the cycle offset and write address
pop hl
ld e,(hl)
inc hl
ld bc,(hl)
inc hl
inc hl
push hl
; Save the JIT address
ld (generic_write_jit_address),hl
push af
; Calculate and save the end-of-instruction offset
ld a,-2
sub e
ld (generic_write_instr_cycle_offset),a
; Calculate the real value of SP
ld hl,(stack_window_base)
ld a,l
add a,iyl
jr nc,_
inc h
_
; Write to the memory addresses
push bc
ld l,e
push hl
call write_mem_any
pop hl
ld a,h
ld e,l
pop bc
; Increment the address and cycle offset
inc e
inc bc
call write_mem_any
pop af
exx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Stack adjustment routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; INC SP
ophandler33:
ex af,af'
inc iyl
; Results between $80 and $FF are valid
ret m
; Inputs: IYL=out-of-bounds stack offset
; Outputs: IY and stack access SMC are adjusted
; H = Input value of A
; Destroys: F, BC', E', HL'
shift_stack_window_higher:
ld h,a
shift_stack_window_higher_preserved_a:
exx
#ifdef FASTLOG
ld b,iyl
push bc
inc sp
ld hl,(stack_window_base)
push hl
FASTLOG_EVENT_Z80(SHIFT_STACK_HIGHER, 7)
dec sp \ dec sp \ dec sp \ dec sp
#endif
ld hl,stack_window_base
; Attempt shifting the stack window by 64 bytes
ld a,iyl
sub $40
jr nc,shift_stack_window_higher_2
ld e,a
ld a,(hl)
; Check for moving forward from $80, causing a new page overlap
dec a
jr z,shift_stack_window_higher_check_overlap
add a,$41
ld (hl),a
jr nc,shift_stack_window_higher_finish
; Increment the MSB of the window base
inc hl
inc (hl)
shift_stack_window_higher_finish:
ld hl,do_pop_any_ptr_offset_smc
ld a,(hl)
xor $80
ld iyl,a
sub $40
lea.l iy,iy+$40
or a
shift_stack_window_higher_any_finish:
ld (hl),a
ld iyl,e
jp apply_stack_offset_smc
shift_stack_window_higher_2:
; Shift the stack window by 128 bytes
sub $40
ld e,a
ld a,(hl)
add a,$80
ld (hl),a
inc hl
jr nc,_
; Increment the MSB of the window base
inc (hl)
_
; Check for moving forward from $40 or $80, causing a new page overlap
; This means a transition from $C1 to $41 or $01 to $81, so check parity
or a
jp pe,shift_stack_window_higher_2_check_overlap
shift_stack_window_higher_2_finish:
ld hl,do_pop_any_ptr_offset_smc
ld bc,$0080
ld a,(hl)
xor c
ld iyl,a
add.l iy,bc
jr shift_stack_window_higher_any_finish
shift_stack_window_higher_check_overlap:
ld (hl),$41
inc hl
; Compare the current and next stack regions
ld l,(hl)
ld h,mem_read_lut >> 8
ld a,(hl)
inc l
cp (hl)
jr z,shift_stack_window_higher_finish
#ifdef SHADOW_STACK
; Check and see if we want to try shifting the shadow stack
cp shadow_stack_get_ptr & $FF
jr z,shift_shadow_stack_higher
#endif
shift_stack_window_higher_overlap:
ld b,l
ld a,(stack_window_base)
add a,e
ld c,a
exx
ld a,h
exx
scf
jp set_gb_stack
shift_stack_window_higher_2_check_overlap:
; Compare the current and next stack regions
ld l,(hl)
ld h,mem_read_lut >> 8
ld a,(hl)
inc l
cp (hl)
jr z,shift_stack_window_higher_2_finish
#ifdef SHADOW_STACK
; Check and see if we want to try shifting the shadow stack
cp shadow_stack_get_ptr & $FF
jr nz,shift_stack_window_higher_overlap
shift_shadow_stack_higher:
FIXME
#else
jr shift_stack_window_higher_overlap
#endif
; DEC SP
ophandler3B:
ex af,af'
dec iyl
; Results between $7F and $FE are valid
ret m
ret pe
; Inputs: IYL=out-of-bounds stack offset
; Outputs: IY and stack access SMC are adjusted
; H = Input value of A
; Destroys: F, BC', E', HL'
shift_stack_window_lower:
ld h,a
shift_stack_window_lower_preserved_a:
exx
shift_stack_window_lower_preserved_a_swapped:
#ifdef FASTLOG
ld b,iyl
push bc
inc sp
ld hl,(stack_window_base)
push hl
FASTLOG_EVENT_Z80(SHIFT_STACK_LOWER, 7)
dec sp \ dec sp \ dec sp \ dec sp
#endif
ld hl,stack_window_base
; Attempt shifting the stack window by 64 bytes
ld a,iyl
add a,$41
jp po,shift_stack_window_lower_2
dec a
ld e,a
ld a,(hl)
; Check for moving backard from $00, causing a new page overlap
sub $40
ld (hl),a
jp pe,shift_stack_window_lower_check_overlap
jr nc,shift_stack_window_lower_finish
inc hl
; Special case for HRAM, force a region transition immediately
ld c,(hl)
inc c
jr z,shift_stack_window_lower_io
; Decrement the MSB of the window base
dec (hl)
shift_stack_window_lower_finish:
ld hl,do_pop_any_ptr_offset_smc
ld a,(hl)
xor $80
ld iyl,a
add a,$40
lea.l iy,iy-$40
or a
shift_stack_window_lower_any_finish:
ld (hl),a
ld iyl,e
jp apply_stack_offset_smc
shift_stack_window_lower_2:
; Shift the stack window by 128 bytes
add a,$3F
ld e,a
ld a,(hl)
; Check for moving backward from $00 or $40, causing a new page overlap
sub $80
ld (hl),a
jr nc,shift_stack_window_lower_2_check_overlap
inc hl
; Special case for HRAM, force a region transition immediately
ld c,(hl)
inc c
jr z,shift_stack_window_lower_io
; Decrement the MSB of the window base
dec (hl)
shift_stack_window_lower_2_finish:
ld hl,do_pop_any_ptr_offset_smc
ld a,(hl)
xor $80
ld iyl,a
lea.l iy,iy-$80
jr shift_stack_window_lower_any_finish
shift_stack_window_lower_check_overlap:
; Compare the next and current pages
ld b,mem_read_lut >> 8
inc hl
ld c,(hl)
ld a,(bc)
ld l,a
inc c
ld a,(bc)
cp l
jr z,shift_stack_window_lower_finish
#ifdef SHADOW_STACK
; Check and see if we want to try shifting the shadow stack
ld a,l
cp shadow_stack_get_ptr & $FF
jr z,shift_shadow_stack_lower
#endif
shift_stack_window_lower_overlap:
ld a,(stack_window_base)
add a,e
jr c,++_
_
dec c
_
ld b,c
ld c,a
exx
ld a,h
exx
scf
jp set_gb_stack
shift_stack_window_lower_io:
add a,e
jr --_
shift_stack_window_lower_2_check_overlap:
; Compare the next and current pages
ld b,mem_read_lut >> 8
inc hl
ld c,(hl)
ld a,(bc)
ld l,a
inc c
ld a,(bc)
cp l
jr z,shift_stack_window_lower_finish
#ifdef SHADOW_STACK
; Check and see if we want to try shifting the shadow stack
ld a,l
cp shadow_stack_get_ptr & $FF
jr nz,shift_stack_window_lower_overlap
shift_shadow_stack_lower:
FIXME
#else
jr shift_stack_window_lower_overlap
#endif
; ADD SP,+nn
ophandlerE8_non_negative:
ld h,a
; Add the offset to IYL and check for a bounds overflow
ld a,iyl
add a,l
ld iyl,a
; Results between $7F and $FF are valid
jr c,_
ophandlerE8_finish:
; Get the current LSB of the stack pointer
ld a,(stack_window_base)
add a,iyl
; Subtract the offset and re-add to get half-carry and carry flags
sub l
add a,l
ld a,h
ret nz ; If Z flag is reset, all flags are correct
; Reset Z flag but preserve H/C flags and keep N flag reset.
ld a,$04
daa ; Resets H but increases low nibble to $A if H was set.
daa ; Iff low nibble is $A, sets H. Z is reset always.
ld a,h
ret
_
call shift_stack_window_higher_preserved_a
jr ophandlerE8_finish
; ADD SP,-nn
ophandlerE8_negative:
ld h,a
; Add the negative offset to IYL and check for a bounds overflow
ld a,iyl
add a,l
ld iyl,a
; Results between $7F and $FE are valid
inc a
jp m,ophandlerE8_finish
call shift_stack_window_lower_preserved_a
jr ophandlerE8_finish
; LD SP,HL
ophandlerF9:
push de
exx
pop bc
; LD SP,nnnn
ophandler31:
; Get a literal 24-bit pointer to the Game Boy stack.
; Does not use a traditional call/return, must be jumped to directly.
;
; This routine is invoked whenever SP is set to a new value which may be outside
; its current bank. If the bank has changed, any relevant stack routines are modified.
;
; Inputs: BC' = 16-bit Game Boy SP
; BCDEHL' have been swapped
; Outputs: IY = 24-bit literal SP
; BCDEHL' have been unswapped
; SMC applied to stack operations
; Destroys: HL, BC', E', HL'
set_gb_stack:
push af
#ifdef FASTLOG
push bc
FASTLOG_EVENT_Z80(SET_STACK, 2)
#endif
; Determine the new stack window start address.
; Aligns to 128 bytes by default for simplicity.
; If the new stack pointer is on a 128-byte boundary, the preceding
; window is selected (assuming the bottom of the stack is being set).
ld e,c
dec bc
ld a,c
ld hl,$FF80
and l
inc a
ld c,a
add hl,bc
ld (stack_window_base),hl
; Get the low byte relative to the window start
ld a,e
sub l
ld e,a
; Get the memory region directly preceding the stack pointer
ld.lil hl,z80codebase+mem_read_lut
ld l,b
ld l,(hl)
inc h ;mem_get_ptr_routines
inc l \ inc l
ld.l iy,(hl)
; Calculate the 24-bit stack window base
add.l iy,bc
; Check if the type of stack region changed, to apply SMC
ld a,l
; Special-case the $FF00 area
inc b
jr nz,_
; Check for I/O (setting A to io_region_base=0 if so)
cp c
sbc a,a
and l
_
curr_gb_stack_region = $+1
cp $FF
jr nz,set_gb_stack_region
set_gb_stack_region_finish:
; Get the new offset from the window base
ld a,iyl
xor $80
; Set the low byte of the stack index
ld iyl,e
; Apply offset SMC if the offset has changed
ld hl,do_pop_any_ptr_offset_smc
cp (hl)
ld (hl),a
jp nz,pop_apply_stack_offset_smc
exx
pop af
ret
; Apply SMC as needed to stack accesses when SP changes banks.
set_gb_stack_region:
ld (curr_gb_stack_region),a
ld bc,$2525 ; DEC H \ DEC H
; Check for read-only vs. read-write
cp cram_bank_base & $FF
jr c,set_gb_stack_region_read_only
; Special-case cartridge RAM based on current mapping
jr z,set_gb_stack_region_cram_rtc
; Check for long vs. short pointer
cp shadow_stack_base & $FF
jr c,set_gb_stack_region_long_ptr
; Set push handlers
ld a,do_push_bc_short_ptr - push_routines_start
ld (ophandlerC5_smc),a
ld a,do_push_de_short_ptr - push_routines_start
ld (ophandlerD5_smc),a
ld a,do_push_hl_short_ptr - push_routines_start
ld (ophandlerE5_smc),a
ld a,do_push_any_short_ptr - push_routines_start
ld (ophandlerF5_smc),a
ld hl,$FD49 ;LD.LIS (IY),...
ld (do_call_shadow_stack_smc),hl
ld (do_rst_shadow_stack_smc),hl
ld (do_push_for_interrupt_shadow_stack_smc),hl
.db $21
pop.l hl
ld (callstack_ret_shadow_stack_smc),hl
ld (callstack_ret_cond_shadow_stack_smc),hl
ld a,apply_stack_offset_smc_short_ptr - (apply_stack_offset_smc_offset_smc+1)
ld l,pop_short_ptr_src - pop_routines_start
jr set_gb_stack_region_apply_pop_smc
set_gb_stack_region_read_only:
; Check for I/O region
or a
set_gb_stack_region_io:
; Set push handlers
ld a,do_push_bc_slow - push_routines_start
ld (ophandlerC5_smc),a
ld a,do_push_de_slow - push_routines_start
ld (ophandlerD5_smc),a
ld a,do_push_hl_slow - push_routines_start
ld (ophandlerE5_smc),a
ld a,do_push_instr_slow - push_routines_start
ld (ophandlerF5_smc),a
ld hl,$18 | ((do_call_no_shadow_stack - (do_call_shadow_stack_smc+2)) << 8)
ld (do_call_shadow_stack_smc),hl
ld h,do_rst_no_shadow_stack - (do_rst_shadow_stack_smc+2)
ld (do_rst_shadow_stack_smc),hl
ld h,do_push_for_interrupt_no_shadow_stack - (do_push_for_interrupt_shadow_stack_smc+2)
ld (do_push_for_interrupt_shadow_stack_smc),hl
ld h,callstack_ret_no_shadow_stack - (callstack_ret_shadow_stack_smc+2)
ld (callstack_ret_shadow_stack_smc),hl
ld h,callstack_ret_cond_no_shadow_stack - (callstack_ret_cond_shadow_stack_smc+2)
ld (callstack_ret_cond_shadow_stack_smc),hl
ld a,apply_stack_offset_smc_read_only - (apply_stack_offset_smc_offset_smc+1)
jr nz,set_gb_stack_long_ptr_finish
ld a,apply_stack_offset_smc_slow - (apply_stack_offset_smc_offset_smc+1)
ld l,pop_slow_src - pop_routines_start
jr set_gb_stack_region_apply_pop_smc
set_gb_stack_region_cram_rtc:
; Check if an RTC register is mapped (or MBC2 RAM is present)
ld a,(cram_banked_get_ptr_rtc_smc)
rra ;JR vs. ADD.L
jr nc,set_gb_stack_region_io ; Note: Z is already set
set_gb_stack_region_cram:
; Check if cart RAM is currently protected
ld a,(cram_banked_get_ptr_protect_smc)
cp $18 ;JR
jr z,set_gb_stack_region_io
set_gb_stack_region_long_ptr:
cp wram_bank_base & $FF
jr nz,_
ld bc,$18 | ((writeSVBK_stack - (writeSVBK_stack_smc+2)) << 8)
_
; Set push handlers
ld a,do_push_bc_long_ptr - push_routines_start
ld (ophandlerC5_smc),a
ld a,do_push_de_long_ptr - push_routines_start
ld (ophandlerD5_smc),a
ld a,do_push_hl_long_ptr - push_routines_start
ld (ophandlerE5_smc),a
ld a,do_push_any_long_ptr - push_routines_start
ld (ophandlerF5_smc),a
#ifdef SHADOW_STACK
ld hl,$18 | ((do_call_set_shadow_stack - (do_call_shadow_stack_smc+2)) << 8)
ld (do_call_shadow_stack_smc),hl
ld h,do_rst_set_shadow_stack - (do_rst_shadow_stack_smc+2)
ld (do_rst_shadow_stack_smc),hl
ld h,do_push_for_interrupt_set_shadow_stack - (do_push_for_interrupt_shadow_stack_smc+2)
ld (do_push_for_interrupt_shadow_stack_smc),hl
ld h,callstack_ret_set_shadow_stack - (callstack_ret_shadow_stack_smc+2)
ld (callstack_ret_shadow_stack_smc),hl
ld h,callstack_ret_cond_set_shadow_stack - (callstack_ret_cond_shadow_stack_smc+2)
ld (callstack_ret_cond_shadow_stack_smc),hl
#else
ld hl,$FD49 ;LD.LIS (IY),...
ld (do_call_shadow_stack_smc),hl
ld (do_rst_shadow_stack_smc),hl
ld (do_push_for_interrupt_shadow_stack_smc),hl
.db $21
pop.l hl
ld (callstack_ret_shadow_stack_smc),hl
ld (callstack_ret_cond_shadow_stack_smc),hl
#endif
ld a,apply_stack_offset_smc_long_ptr - (apply_stack_offset_smc_offset_smc+1)
set_gb_stack_long_ptr_finish:
ld l,pop_long_ptr_src - pop_routines_start
set_gb_stack_region_apply_pop_smc:
; Apply WRAM banking SMC
ld (writeSVBK_stack_smc),bc
; Select the appropriate stack offset routine
ld (apply_stack_offset_smc_offset_smc),a
; Get the new offset from the window base
ld a,iyl
xor $80
ld (do_pop_any_ptr_offset_smc),a
; Set the low byte of the stack index
ld iyl,e
; Apply pop SMC
push de
ld de,ophandlerC1_smc
ld h,d
ld bc,ophandlerC1_smc_size
ldir
ld e,ophandlerD1_smc & $FF
ld c,ophandlerD1_smc_size
ldir
ld e,ophandlerE1_smc & $FF
ld c,ophandlerE1_smc_size
ldir
pop de
ld hl,(hl)
ld (ophandlerF1_smc),hl
pop_apply_stack_offset_smc:
ld l,a
pop af
exx
ld h,a
exx
ld a,l
apply_stack_offset_smc:
#ifdef FASTLOG
push af
ld b,a
ld c,iyl
push bc
ld hl,(stack_window_base)
push hl
FASTLOG_EVENT_Z80(APPLY_STACK_OFFSET, 4)
pop af
#endif
exx
apply_stack_offset_smc_offset_smc = $+1
jr apply_stack_offset_smc_short_ptr
apply_stack_offset_smc_short_ptr:
ld (do_push_short_ptr_offset_smc_1),a
ld (do_push_short_ptr_offset_smc_2),a
ld (do_push_short_ptr_offset_smc_3),a
ld (do_push_short_ptr_offset_smc_4),a
ld (do_pop_short_ptr_offset_smc_1),a
ld (do_pop_short_ptr_offset_smc_2),a
ld (do_pop_short_ptr_offset_smc_3),a
ld (callstack_ret_pop_offset_smc),a
ld (callstack_ret_cond_pop_offset_smc),a
ld (do_call_push_offset_smc_1),a
ld (do_call_push_offset_smc_2),a
ld (do_rst_push_offset_smc_1),a
ld (do_rst_push_offset_smc_2),a
ld (trigger_interrupt_push_offset_smc_1),a
ld (trigger_interrupt_push_offset_smc_2),a
apply_stack_offset_smc_slow:
ld a,h
ret
apply_stack_offset_smc_long_ptr:
ld (do_push_long_ptr_offset_smc_1),a
ld (do_push_long_ptr_offset_smc_2),a
ld (do_push_long_ptr_offset_smc_3),a
ld (do_push_long_ptr_offset_smc_4),a
ld (do_push_long_ptr_offset_smc_5),a
ld (do_push_long_ptr_offset_smc_6),a
ld (callstack_ret_pop_offset_smc),a
ld (callstack_ret_cond_pop_offset_smc),a
ld (do_call_push_offset_smc_1),a
ld (do_call_push_offset_smc_2),a
ld (do_rst_push_offset_smc_1),a
ld (do_rst_push_offset_smc_2),a
ld (trigger_interrupt_push_offset_smc_1),a
ld (trigger_interrupt_push_offset_smc_2),a
apply_stack_offset_smc_read_only:
ld (do_pop_long_ptr_offset_smc_1),a
ld (do_pop_long_ptr_offset_smc_2),a
ld (do_pop_long_ptr_offset_smc_3),a
ld a,h
ret
#ifdef SHADOW_STACK
set_shadow_stack_rollback:
dec iyl
dec iyl
set_shadow_stack:
push hl
exx
push af
push bc
push de
push hl
; Get the current shadow stack region and base
curr_shadow_stack_region = $+1
ld de,$FF00 | (hmem_get_ptr & $FF)
; Get the stack pointer in IY
ld bc,(stack_window_base)
ld iyh,0
add iy,bc
; Get the MSB preceding the end of the stack window
dec bc
dec bc
ld a,(curr_gb_stack_region)
ld.lil hl,z80codebase+mem_read_lut+2
sub l ;2
ld c,a
_
ld (curr_shadow_stack_region),bc
; Check if the first 256 bytes are in the current stack region.
; The second 256 bytes are already guaranteed to be in that region.
ld l,b
sub (hl)
jp.lil z,set_shadow_stack_contiguous_helper
; Start the shadow stack 256 bytes higher.
; All eligible regions are at least 512 bytes large so this will
; force the shadow stack into a contiguous region.
; In the case the following 256 bytes are already in the shadow
; stack, for now don't bother optimizing the stack move.
inc b
ld a,c
jr -_
set_shadow_stack_finish:
call set_gb_stack
exx
pop hl
pop de
pop bc
pop af
exx
pop hl
ret
#endif
; Input: Carry reset for unconditional RET, set for conditional RET
; C = open bus value, DA = cycle counter
do_pop_for_ret_slow:
ld e,a
push ix
push af
push de
; Advance the cycle count past the end of the RET
adc a,4
jr nc,_
inc d
_
ld e,-3 ; The first read is 3 cycles before the end of the instruction
call do_pop_any_slow
push hl
exx
pop de
jp do_pop_for_ret_slow_finish
do_push_for_call_slow_swap:
exx
; Input: E = cycles for specific call (RST, CALL, interrupt)
; HL = value to push, DA = cycle counter, AFBCDEHL' are swapped
; Output: AFBCDEHL' are unswapped, cycle counter updated if event triggered
do_push_for_call_slow:
; Add cycles for the specific call type
add a,e
#ifdef DEBUG
; Prevent assertions when handling events during a memory access
ld (event_cycle_count),a
#endif
jr nc,_
inc d
_
ex af,af'
push af
; Save the call cycle offset
push de
push hl
ld a,h
; Ensure a triggered event won't clobber any code
ld hl,generic_write_instr_cycle_offset
ld (generic_write_jit_address),hl
#ifdef DEBUG
; Be consistent with debug assertions...
inc d
dec d
jr z,_
ld hl,event_debug_address
_
#endif
ld (event_address),hl
ld e,-2 ; First call push is two cycles before the end
ld hl,(stack_window_base)
ld c,iyl
ld b,0
add hl,bc
ld b,h
ld c,l
do_push_instr_get_cycle_offset_return:
push de
push bc
call write_mem_any
pop bc
pop hl
; Increment cycle offset, decrement stack pointer and address
ld e,l
inc e
dec iyl
dec bc
pop hl
ld a,l
call write_mem_any
pop hl
pop af
; Subtract the call cycle offset again
ex af,af'
sub l
jr nc,_
dec d
_
ex af,af'
exx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Pop routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.block (-$)&255
pop_routines_start:
; POP BC
ophandlerC1:
ex af,af'
ophandlerC1_smc = $
do_pop_short_ptr_offset_smc_1 = $+2
;ld ix,(iy)
do_pop_long_ptr_offset_smc_1 = $+3
ld.l ix,(iy)
inc iyl
inc iyl
ophandlerC1_smc_size = $-ophandlerC1_smc
ret m
call shift_stack_window_higher
ret nc
do_pop_bc_slow:
exx
ld c,$C1 ;POP BC
call do_pop_instr_slow
push hl
pop ix
ret
ophandlerC1_slow:
call p,shift_stack_window_higher
jr do_pop_bc_slow
; POP DE
ophandlerD1:
ex af,af'
ophandlerD1_smc = $
do_pop_short_ptr_offset_smc_2 = $+2
;ld bc,(iy)
do_pop_long_ptr_offset_smc_2 = $+3
ld.l bc,(iy)
inc bc \ dec bc ;BCU=0
inc iyl
ophandlerD1_smc_size = $+1-ophandlerD1_smc
inc iyl
ret m
call shift_stack_window_higher
ret nc
do_pop_de_slow:
exx
ld c,$D1 ;POP DE
call do_pop_instr_slow
ld b,h
ld c,l
ret
ophandlerD1_slow:
call shift_stack_window_higher
jr do_pop_de_slow
; POP HL
ophandlerE1:
ex af,af'
ophandlerE1_smc = $
do_pop_short_ptr_offset_smc_3 = $+2
;ld de,(iy)
do_pop_long_ptr_offset_smc_3 = $+3
ld.l hl,(iy)
ex de,hl ;DEU=0
inc iyl
inc iyl
ophandlerE1_smc_size = $-ophandlerE1_smc
ret m
call shift_stack_window_higher
ret nc
do_pop_hl_slow:
exx
ld c,$E1 ;POP HL
call do_pop_instr_slow
ex de,hl
ret
ophandlerE1_slow:
call shift_stack_window_higher
jr do_pop_hl_slow
; POP AF
ophandlerF1:
do_pop_any_ptr_offset_smc = $+3
ld.l hl,(iy)
inc iyl
inc iyl
ophandlerF1_smc = $
jp p,ophandlerF1_overflow
do_pop_af_slow_finish:
ld a,h
do_pop_af_finish:
ld h,flags_lut >> 8
res 3,l
ld l,(hl)
ld h,a
push hl
pop af
ret
ophandlerF1_slow:
call p,shift_stack_window_higher_preserved_a
jr do_pop_af_slow
ophandlerF1_overflow:
call shift_stack_window_higher_preserved_a
jr nc,do_pop_af_finish
do_pop_af_slow:
exx
ld c,$F1 ;POP AF
ex af,af'
call do_pop_instr_slow
ex af,af'