forked from calc84maniac/tiboyce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.asm
2629 lines (2416 loc) · 42 KB
/
setup.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MENU_ITEM_COUNT = 15
.org 0
Startup:
ld (ArcBase),hl
; Get the calculator type from the OS header
ld hl,_OSHeader
call _GetFieldSizeFromType
ld de,$80C0
call _FindField
; If not found, calcType stays at default 0 (84+CE)
jr nz,_
call _GetFieldSizeFromType
ld a,(hl)
inc hl
and (hl)
ld (calcType),a
_
call _RunIndicOff
ACALL(LoadConfigFile)
CalculateEpochLoop:
; Grab current day count
ld hl,(mpRtcDayCount)
push hl
; Multiply year by 365
ld hl,(osYear)
push hl
pop de
ld b,(365-1) / 2
_
add hl,de
add hl,de
djnz -_
; Add in day-1
ld a,(osDay)
ld c,a
dec.s bc
add hl,bc
; If we're Feb. or earlier, don't count this leap year
ld a,(osMonth)
cp 3
jr nc,_
dec de
_
; Add in months
ld ix,monthLUT
_
ld c,(ix)
inc ix
add hl,bc
dec a
jr nz,-_
; Add in year/4
srl d
rr e
srl d
rr e
add hl,de
; Subtract out year/100
ex de,hl
ld a,25
call _DivHLByA
ex de,hl
or a
sbc hl,de
; Add in year/400
srl d
rr e
srl d
rr e
add hl,de
; Subtract out January 1, 1970
ld bc,-719527
add hl,bc
jr c,_
sbc hl,hl
_
ex de,hl
pop bc
; Make sure day count matches
ld hl,(mpRtcDayCount)
or a
sbc hl,bc
jr nz,CalculateEpochLoop
ex de,hl
sbc hl,bc
ld (epochDayCount),hl
ACALL(BackupOriginalHardwareSettings)
; Backup original LCD settings
ld hl,mpLcdTiming0
ld de,originalLcdSettings
ld bc,12
ldir
ld hl,(mpLcdCtrl)
ex de,hl
ld (hl),de
ld hl,GlobalErrorHandler
call _PushErrorHandler
NoRomMenuLoop:
; Set custom hardware settings
ACALL(SetCustomHardwareSettings)
; Set current description and main menu selection
APTR(NoRomLoadedDescription)
ld (current_description),hl
ld a,6
ld (main_menu_selection),a
; Reset exit reason, ROM name, and state index
xor a
ld (exitReason),a
ld (ROMName+1),a
ld (current_state),a
; Start with Load ROM menu
inc a
ACALL(emulator_menu)
; If not loading a new ROM, exit
ld a,(exitReason)
dec a
jr nz,SaveConfigAndQuit
ACALL(RestoreOriginalHardwareSettings)
ei
LoadNewGameLoop:
; Copy the name from ROMNameToLoad
ld hl,ROMName+1
push hl
pop de
ld bc,9
add hl,bc
ldir
ACALL_SAFERET(StartROM)
; If NC is returned, we're loading another game
jr nc,LoadNewGameLoop
; If an error code of 0 is returned, exit
or a
jr z,SaveConfigAndQuit
; Display the error, and return to the menu if ON wasn't pressed
ACALL(DisplayError)
jr nz,NoRomMenuLoop
SaveConfigAndQuit:
call _PopErrorHandler
SaveConfigAndQuitForError:
ACALL(RestoreOriginalHardwareSettings)
ei
ACALL_SAFERET(SaveConfigFile)
RestoreHomeScreen:
; Set all palette entries to white to smooth the transition
ld hl,mpLcdPalette
push hl
pop de
inc de
ld bc,$01FF
ld (hl),c
ldir
; Clear pixelShadow etc.
ld hl,pixelShadow
push hl
pop de
inc de
ld (hl),c
ld bc,(8400*3) - 1
ldir
; Mark graph dirty
set graphDraw,(iy+graphFlags)
; Restore the frame buffer
call _ClrLCDFull
call _DrawStatusBar
call _HomeUp
; Change the LCD settings to fullscreen 16-bit
AJUMP(RestoreOriginalLcdSettings)
; Input: HL = insertion point
; DE = insertion size
; Output: Carry set and A=error if error occurred
; DE = insertion point
; BC = insertion size
InsertMemSafe:
push hl
push de
; Check to make sure there's room for at least 2 VAT entries
ld hl,30
add hl,de
push hl
call _MemChk
pop de
ex de,hl
scf
sbc hl,de
ccf
inc hl
ld (errorArg),hl
pop hl
pop de
ld a,ERROR_NOT_ENOUGH_MEMORY
ret c
push hl
call _InsertMem
pop bc
; Resets carry
ld hl,(asm_prgm_size)
add hl,bc
ld (asm_prgm_size),hl
ret
; Input: HL = deletion point, pointing to (fake) size bytes
; Output: Memory is deleted according to size bytes, asm_prgm_size is adjusted
DelMemSafeSizeBytes:
ld de,(hl)
inc.s de
inc de
; Input: HL = deletion point
; DE = deletion size
; Output: Memory is deleted, asm_prgm_size is adjusted
DelMemSafe:
push hl
ld hl,(asm_prgm_size)
or a
sbc hl,de
ld (asm_prgm_size),hl
pop hl
jp _DelMem
; Input: HL = filename
; DE = location to convert
; Note: Location must have size bytes initialized and
; the range must end exactly on userMem + (asm_prgm_size),
; and also the filename should not already exist.
; Output: Carry set, HL preserved.
; File is created and asm_prgm_size is updated.
ConvertMemToFile:
push hl
#ifdef DEBUG
; Assert that location + size bytes == userMem + (asm_prgm_size)
ex de,hl
ld de,(hl)
ex de,hl
inc.s hl
inc hl
add hl,de
ld bc,(asm_prgm_size)
sbc hl,bc
ld bc,userMem
sbc hl,bc
jr nz,$
pop hl
push hl
#endif
push de
call _Mov9ToOP1
call _CmpPrgNamLen
ld bc,0
call _CreatePVar4
push hl
pop ix
ld hl,2
add hl,sp
ld a,(hl)
pop hl
ld (ix-5),a
ld (ix-4),h
ld (ix-3),l
ld de,-userMem
add hl,de
ld (asm_prgm_size),hl
pop hl
ret
StartROMAutoStateError:
cp ERROR_NOT_ENOUGH_MEMORY
jr nz,RestartFromHere
ld hl,(cram_size)
ld de,6
add hl,de
ex de,hl
ld hl,save_state_size_bytes
ACALL(DelMemSafe)
_
ld a,ERROR_NOT_ENOUGH_MEMORY
scf
ret
StartROMInitStateOutOfMemory:
xor a
ld (current_state),a
dec de
dec de
ld (de),a
dec de
inc a
ld (de),a
; Temporarily prevent auto state saving because state is clean
ld hl,AutoSaveState
set 1,(hl)
ld hl,(errorArg)
push hl
ACALL_SAFERET(SaveStateFiles)
pop hl
ld (errorArg),hl
jr -_
StartROM:
ACALL_SAFERET(LoadROMAndRAM)
ret c
xor a
ld (emulatorMessageText),a
ld (current_state),a
ACALL(LoadStateFiles)
push af
ld a,'0'
ld (current_state),a
pop af
jr c,StartROMAutoStateError
ld de,AutoStateLoadedMessage
ACALL(SetEmulatorMessage)
jr StartFromHere
RestartFromHere:
ld hl,save_state_size_bytes
ld de,(hl)
ld bc,save_state_size + 1
ld (hl),bc
inc hl
inc hl
inc hl
dec de
dec bc
push hl
push bc
ACALL(DelMemSafe)
pop de
pop hl
ACALL(InsertMemSafe)
jr c,StartROMInitStateOutOfMemory
push de
pop hl
inc de
dec bc
ld (hl),0
ldir
inc hl
ld a,(hl)
dec a
inc hl
or (hl)
jr z,_
inc hl
inc hl
ld (cram_start),hl
_
APTR(regs_init)
ld de,regs_saved
ld bc,hmem_init - regs_init
ldir
ld de,hram_saved + $0100
ld c,hmem_init_size
ldir
push de
pop hl
dec hl
ld c,$80 - hmem_init_size + 1
ldir
ld (hl),c
ld c,$7F
ldir
StartFromHere:
ld hl,(save_state_size_bytes)
ld a,l
dec a
or h
jr z,RestartFromHere
ld a,3
ld (main_menu_selection),a
ld hl,SkinFileName
ACALL(LookUpAppvar)
jr nc,_
or a
sbc hl,hl
_
ld (skin_file_ptr),hl
ACALL(SetCustomHardwareSettings)
APTR(palettecode)
ld de,mpLcdPalette + 32
ld bc,palettecodesize
ldir
#ifdef DEBUG
APRINTF(StartText)
#endif
ld (saveSP),sp
ld sp,myADLstack
ld hl,z80codebase
push hl
pop de
inc de
ld bc,hram_start - z80codebase - 1
ld (hl),l
ldir
ld hl,hram_saved
ld b,2
ldir
APTR(cursorcode)
ld de,mpLcdCursorImg
ld bc,cursorcodesize
ldir
APTR(z80code)
ld de,z80codebase
ld bc,z80codesize
ldir
APTR(flags_lut_init)
ld de,z80codebase + flags_lut
inc b
ldir
ld hl,convert_palette_LUT
_
ld (hl),l
inc l
jr nz,-_
ld hl,vram_start
ld de,vram_pixels_start
ld c,$0C
_
push hl
ld hl,(hl)
push bc
ld b,8
_
sla h
ccf
sbc a,a
or $01 ; The VRAM will be converted later to double scale if needed
sla l
jr nc,$+5 \ rlca \ adc a,0
ld (de),a
inc de
djnz -_
pop bc
pop hl
inc hl
inc hl
djnz --_
dec c
jr nz,--_
ld a,vram_tiles_start >> 16
ld mb,a
ld ix,vram_tiles_start + 128
ld c,$40
ld a,c
_
ld b,$20
_
ld e,(hl)
inc hl
ld d,a
mlt de
ld.s (ix-128),de
ld.s (ix-64),de
bit 5,d
jr nz,_
set 6,d
_
ld.s (ix),de
ld.s (ix+64),de
lea ix,ix+2
djnz --_
lea ix,ix-64
inc ixh
dec c
jr nz,---_
ld a,z80codebase >> 16
ld mb,a
ld hl,fake_tile
push hl
pop de
inc de
ld (hl),$FF
ld c,64
ldir
ld (hl),b
ld a,(fake_tile - vram_pixels_start) >> 8
ld (de),a
inc de
ld c,40
ldir
ld iy,state_start+state_size
ld.sis sp,myz80stack
ld hl,ophandlerRET
push.s hl
ld a,(iy-state_size+STATE_ROM_BANK)
ld (z80codebase+curr_rom_bank),a
ld (z80codebase+rom_bank_check_smc_1),a
ld (z80codebase+rom_bank_check_smc_2),a
ld (z80codebase+rom_bank_check_smc_3),a
ld c,a
ld b,3
mlt bc
ld hl,rombankLUT
add hl,bc
ld hl,(hl)
ld (rom_bank_base),hl
ld a,(cram_size+1)
and $80
add a,$B7 ; or a / scf
ld (z80codebase+cram_size_smc),a
xor a
ld (exitReason),a
ld (memroutine_rtc_smc_1),a
ld a,$18 ;JR
ld (memroutine_rtc_smc_2),a
ld hl,(cram_start)
ld bc,$A000
sbc hl,bc
ld (z80codebase+cram_base_0),hl
ld a,(mbc)
ld (z80codebase+mbc_z80),a
cp 4 ;MBC3+RTC
jr nz,setup_ram_bank
; Update rtc_last
call update_rtc
ld ix,save_state_size_bytes - 44
ld bc,(ix+44)
add ix,bc
ld bc,(ix+46)
add ix,bc
ld b,10
ld de,z80codebase+rtc_latched
_
ld a,(ix)
ld (de),a
lea ix,ix+4
inc de
djnz -_
; Ignore timestamps in the future
push ix
ACALL(GetUnixTimeStamp)
ex (sp),ix
ex (sp),hl
ld de,(ix)
or a
sbc hl,de
pop hl
ld de,(ix+3)
sbc hl,de
jr c,_
ld hl,(ix+6)
ld a,h
or l
jr nz,_
ex de,hl
ld ix,(ix)
ACALL(ExtractUnixTimeStamp)
lea hl,ix
ld de,(epochDayCount)
or a
sbc hl,de
ld.sis (rtc_last),bc
ld.sis (rtc_last+2),a
ld.sis (rtc_last+3),hl
_
ld a,(iy-state_size+STATE_RAM_BANK)
sub 8
jr c,setup_ram_bank
ld c,a
call mbc_rtc_toggle_smc
call update_rtc
ld b,0
ld hl,z80codebase+rtc_latched
jr _
setup_ram_bank:
ld a,(cram_size+1)
add a,a
sbc a,a
and (iy-state_size+STATE_RAM_BANK)
and 3
rrca
rrca
rrca
ld b,a
ld c,0
ld hl,(z80codebase+cram_base_0)
_
add hl,bc
ld (cram_bank_base),hl
ld a,(iy-state_size+STATE_MBC_MODE)
and 1
ld (mbc_rtc_last_latch),a
jr z,_
ld a,$20 ;JR NZ (overriding JR Z)
ld (z80codebase+mbc1_ram_smc),a
_
ld a,(iy-state_size+STATE_INTERRUPTS)
rra
jr c,_
ld a,$AF ;XOR A (overriding AND (HL))
ld (z80codebase+intstate_smc),a
_
; Set the initial frame-relative event to one cycle in the future
ld de,(iy-state_size+STATE_FRAME_COUNTER)
inc.s de
ld hl,-CYCLES_PER_FRAME
add hl,de
jr c,_
ex de,hl
_
ld.sis (frame_cycle_target),hl
; Get the current DIV counter
ld de,(iy-state_size+STATE_DIV_COUNTER)
; Initialize timer values
ld a,(iy-ioregs+TAC)
bit 2,a
jr z,_
and 3
ld c,a
ld hl,timer_smc_data
add hl,bc
add hl,bc
ld a,(hl)
ld (z80codebase+updateTIMA_smc),a
inc hl
ld h,(hl)
ld a,(iy-ioregs+TIMA)
cpl
ld l,a
ld a,h
ld (z80codebase+timer_cycles_reset_factor_smc),a
ld (writeTIMA_smc),a
mlt hl
add hl,hl
add hl,de
add a,a
dec a
or l
ld l,a
inc hl
ld.sis (timer_cycle_target),hl
ld a,$20 ;JR NZ (overriding JR Z)
ld (z80codebase+timer_enable_smc),a
_
; Set the initial DIV-relative event to one cycle in the future
inc de
ld.sis (div_cycle_count),de
ld a,(iy-ioregs+SC)
cpl
and $81
jr nz,_
; Set the serial counter to one cycle in the future
ld hl,(iy-state_size+STATE_SERIAL_COUNTER)
dec hl
ld.sis (serial_cycle_count),hl
ld a,$38 ;JR C (overriding JR NC)
ld (z80codebase+serial_enable_smc),a
_
lea hl,iy-ioregs+NR10
ld ix,z80codebase + audio_port_values
ld b,audio_port_masks - audio_port_values
_
ld a,(hl)
ld (ix),a
or (ix + audio_port_masks - audio_port_values)
ld (hl),a
inc hl
inc ix
djnz -_
ld hl,(iy-ioregs+LCDC-2)
add hl,hl
jr c,_
ld a,$20 ;JR NZ (overriding JR Z)
ld (LCDC_7_smc),a
_
add hl,hl
add hl,hl
jr nc,_
ld a,$38 ;JR C (overriding JR NC)
ld (LCDC_5_smc),a
_
add hl,hl
jr nc,_
xor a ;(overriding $80)
ld (LCDC_4_smc),a
ld (window_tile_ptr),a
_
add hl,hl
jr nc,_
ld a,((vram_tiles_start + $2000) >> 8) & $FF
ld (LCDC_3_smc),a
_
add hl,hl
jr nc,_
ld a,$78 ;(overriding $38)
ld (LCDC_2_smc_1),a
ld a,15 ;(overriding 7)
ld (LCDC_2_smc_2),a
ld (LCDC_2_smc_4),a
ld a,$81 ;RES 0,C (overriding RES 0,B)
ld (LCDC_2_smc_3),a
ld a,1 ;(overriding 9)
ld (LCDC_2_smc_5),a
_
add hl,hl
jr c,_
ld a,$C9 ;RET (overriding LD C,myspriteLY)
ld (LCDC_1_smc),a
_
add hl,hl
jr c,_
ld a,$31 ;LD SP (overriding ADD HL,SP)
ld (LCDC_0_smc),a
_
ld hl,(iy-ioregs+SCY)
ld a,l
ld (SCY_smc),a
ld a,h
rrca
rrca
and $3E
ld (SCX_smc_1),a
ld a,h
cpl
and 7
inc a
ld (SCX_smc_2),a
ld a,(iy-ioregs+LYC)
or a
ld hl,CYCLES_PER_SCANLINE * 153 + 2
jr z,_
ld l,a
ld h,CYCLES_PER_SCANLINE
mlt hl
_
ld.sis (current_lyc_target_count),hl
ld hl,(iy-ioregs+WY)
ld a,l
ld (WY_smc),a
ld a,h
ld (WX_smc_2),a
cp 167
inc a
ld (WX_smc_3),a
jr c,_
ld a,$18 ;JR (overriding default JR C)
ld (WX_smc_1),a
_
call swap_buffers
; Initialize the scanline LUT pointer to what prepare_next_frame expects
ld a,h
cp (gb_frame_buffer_1 >> 8) & $FF
ld hl,scanlineLUT_2
jr nz,_
sbc hl,hl
_
ld (scanlineLUT_ptr),hl
call prepare_next_frame
ACALL(IdentifyDefaultPalette)
ACALL(ApplyConfiguration)
ACALL(SetScalingMode)
ld hl,(curr_palettes)
call update_palettes_always
call flush_code_reset_padding
; Generate the initial code block
ld.s de,(iy-state_size+STATE_REG_PC)
call lookup_code
; Get the initial GB instruction address from the block info
ld hl,(recompile_struct+8+2)
ld (event_gb_address),hl
add hl,hl
jr nc,_
; If the GB address was in RAM, skip over the block prefix
lea ix,ix+RAM_PREFIX_SIZE
_
; Save the event address and write an event handler call
lea hl,ix
ld.sis (event_address),hl
ld.s a,(hl)
ld (z80codebase+event_value),a
ld.s (hl),RST_EVENT
; Get the GB registers in BDEHL'
ld.s bc,(iy-state_size+STATE_REG_BC)
ld.s de,(iy-state_size+STATE_REG_DE)
ld.s hl,(iy-state_size+STATE_REG_HL)
exx
; Push the (remapped) GB AF to the stack
ld de,(iy-state_size+STATE_REG_AF)
ld h,flags_lut >> 8
ld l,e
ld.s e,(hl)
push de
; Get GB SP before we destroy IY
ld.s hl,(iy-state_size+STATE_REG_SP)
; Get the block cycle count (need to use this in case of a RAM block)
ld a,(recompile_struct+8+7)
; Set the cycle count at block end relative to the current event,
; which has been set to 1 cycle after block start
ld iy,0
dec a
ld iyl,a
; Set the negative block cycle offset of the current instruction
cpl
ld (z80codebase+event_cycle_count),a
ld bc,(CALL_STACK_DEPTH+1)*256
; Pop GB AF into AF
pop af
jp set_gb_stack
ExitEmulation:
call.il get_event_gb_address
ld ix,state_start+state_size
ld (ix-state_size+STATE_REG_PC),hl
exx
ld de,(z80codebase+sp_base_address)
xor a
ld (ix-state_size+STATE_SYSTEM_TYPE),a
sbc hl,de
ld.s (ix-state_size+STATE_REG_SP),hl
ex af,af'
push af
pop hl
ld h,flags_lut >> 8
set 3,l
ld.s l,(hl)
ld h,a
ld (ix-state_size+STATE_REG_AF),hl
xor a
ld b,a
sbc hl,hl
add.s hl,sp
lea de,ix-state_size+STATE_REG_BC
ld c,6
ldir.s
; Calculate the current event cycle offset
dec bc
ld a,(z80codebase+event_cycle_count)
ld c,a
lea hl,iy
add hl,bc
ex de,hl
; Save the frame-relative cycle count
ld.sis hl,(frame_cycle_target)
add.s hl,de
jr c,_
ld bc,CYCLES_PER_FRAME
add hl,bc
_
ld (ix-state_size+STATE_FRAME_COUNTER),hl
; Save the serial cycle count
ld.sis hl,(serial_cycle_count)
or a
sbc hl,de
ld (ix-state_size+STATE_SERIAL_COUNTER),hl
; Save the DIV cycle count
ld.sis hl,(div_cycle_count)
add hl,de
ld (ix-state_size+STATE_DIV_COUNTER),hl
; Save the actual value of TAC if the timer is running
ld a,(ix-ioregs+TAC)
and 4
jr z,+++_
ld.sis de,(timer_cycle_target)
sbc hl,de
ld a,(z80codebase+updateTIMA_smc)
sub 6
jr z,++_
_
add hl,hl
inc a
jr nz,-_
_
ld (ix-ioregs+TIMA),h
_
ld a,(z80codebase+intstate_smc)
rra
sbc a,a
inc a
ld (ix-state_size+STATE_INTERRUPTS),a
ld a,(z80codebase+curr_rom_bank)
ld (ix-state_size+STATE_ROM_BANK),a
; Save the current MBC mode
ld a,(cram_size)
or a
ld a,(mbc_rtc_last_latch)
jr nz,_
ld a,(z80codebase+mbc1_ram_smc)
sub $28
rlca
_
and 1
ld (ix-state_size+STATE_MBC_MODE),a
; Save the currently mapped RAM bank
ld a,(cram_bank_base+2)
cp z80codebase >> 16
jr nz,_
ld a,(cram_bank_base)
sub (rtc_latched-8)&$FF
jr ++_
_
ld a,(cram_bank_base+1)
ld hl,(z80codebase+cram_base_0)
sub h
rlca
rlca
rlca
and 3
_
ld (ix-state_size+STATE_RAM_BANK),a
; Save the actual audio port vales in the audio port space
ld hl,z80codebase + audio_port_values
lea de,ix-ioregs+NR10
ld bc,audio_port_masks - audio_port_values
ldir
; Zero-fill the rest of the state
lea hl,ix-state_size+STATE_END
lea de,ix-state_size+STATE_END+1
ld c,state_size-STATE_END-1
ld (hl),b
ldir