-
Notifications
You must be signed in to change notification settings - Fork 25
/
vblank.asm
1594 lines (1515 loc) · 28 KB
/
vblank.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
; VBlank handler.
;
; This is called once per frame, even when the LCD is disabled.
; It finishes rendering the current frame and updates palettes and key inputs.
; In addition, it handles frame synchronization and frameskip logic.
;
; Inputs: BC = current DIV time
; DE = (negative) cycles until next PPU event
; Outputs: None
; Destroys AF,HL
vblank_helper:
; Update emulated frame counter
frame_emulated_count = $+1
ld hl,0
ld a,l
add a,1
daa
ld l,a
ld a,h
adc a,0
daa
ld h,a
ld (frame_emulated_count),hl
; Update real frame count if expired
push de
push ix
push bc
push iy
; Finish rendering, if applicable
ld a,(z80codebase+updateSTAT_enable_catchup_smc)
and 1
jp z,skip_this_frame
; Finish rendering the frame
ld a,144
inc.s bc ;BCU=0
call render_scanlines
; Draw sprites and do palette conversion, if the LCD is enabled
ld a,(hram_base+LCDC)
rla
jr nc,_
ld a,144
call draw_sprites
call sync_frame_flip
ld a,(regs_saved + STATE_SYSTEM_TYPE)
or a
call z,convert_palette
_
preservedAreaHeight = $+1
ld a,0
or a
jr z,_
ld hl,(current_display)
ld de,(current_buffer)
PreservedAreaCopyLoop:
preservedAreaWidth = $+1
ld bc,0
ldir
preserve_copy_smc = $+1
ld c,160
add hl,bc
ex de,hl
add hl,bc
ex de,hl
dec a
jr nz,PreservedAreaCopyLoop
jr NoSpeedDisplay
_
ld hl,emulatorMessageText
or (hl)
speed_display_smc_1 = $+1
jr z,NoSpeedDisplay ;smc'd to YesSpeedDisplay
AJUMP(PutEmulatorMessage)
YesSpeedDisplay:
ld a,(turbo_active)
or a
speed_display_smc_2 = $
jr z,_
speed_display_smc_0 = $+1
ld a,0
low_perf_digits_smc = $+1
add a,0
daa
high_perf_digits_smc = $+1
ld a,0
speed_display_smc_3 = $
adc a,$FF
jr c,NoSpeedDisplay
_
xor a
ld c,a
ld hl,perf_digits
ld b,4
_
or (hl)
jr nz,_
inc hl
djnz -_
dec hl
inc b
_
inc b
_
push hl
push bc
ld b,(hl)
call display_digit
pop bc
pop hl
inc hl
inc c
djnz -_
ld l,c
ld h,6
PutEmulatorMessageRet:
call set_preserved_area
NoSpeedDisplay:
; Swap buffers
call prepare_next_frame
call do_frame_flip
ld a,(active_scaling_mode)
or a
call nz,do_scale_fill
call sync_frame_flip
xor a
; Signify frame was rendered
scf
skip_this_frame:
ld hl,frame_excess_count
turbo_active = $+1
ld b,1
dec b ; We want this to affect Z flag
jr nz,no_frame_sync
; Handle frame synchronization
dec (hl)
jp p,no_frame_sync
; If we didn't render, save for later
jr nc,frame_sync_later
frame_sync_loop:
push hl
call sync_frame_flip_wait
pop hl
bit 7,(hl)
jr nz,frame_sync_loop
frame_sync_later:
; Set Z
xor a
no_frame_sync:
; Handle frameskip logic
; At this point A=0, Z holds auto state
ex de,hl
ld hl,skippable_frames
frameskip_type_smc:
jr z,no_frameskip ;JR no_frameskip when off, JR Z,$+2 when manual
dec (hl)
jr nz,frameskip_end
no_frameskip:
frameskip_value_smc = $+1
ld a,1
ld (hl),a
ex de,hl
bit 7,(hl)
jr nz,_
cp (hl)
jr nc,_
ld (hl),a
_
ld a,$4F-$7E
frameskip_end:
;LD R,A if frame should be rendered, otherwise RSMIX
add a,$7E
ld (z80codebase+updateSTAT_enable_catchup_smc),a
ld (z80codebase+updateSTAT_full_enable_catchup_smc),a
ld (z80codebase+ppu_mode0_enable_catchup_smc),a
ld (z80codebase+ppu_mode2_enable_catchup_smc),a
ld (z80codebase+ppu_lyc_enable_catchup_smc),a
; Get keys
ld ix,mpKeypadGrp0
key_smc_turbo = $+2
bit 2,(ix+1*2) ;ZOOM
turbo_keypress_smc = $
jr z,_
turbo_toggle_smc = $+1
jr z,turbo_skip_toggle
turbo_active_no_toggle = $+1
ld a,(turbo_active)
xor 1
ld (turbo_active),a
turbo_skip_toggle:
ld a,(turbo_keypress_smc)
xor 8
ld (turbo_keypress_smc),a
_
ld a,$FF
key_smc_right = $+2
bit 2,(ix+7*2) ;Right
jr z,_
dec a
_
key_smc_left = $+2
bit 1,(ix+7*2) ;Left
jr z,_
rlca
xor 2
_
key_smc_up = $+2
bit 3,(ix+7*2) ;Up
jr z,_
res 2,a
_
key_smc_down = $+2
bit 0,(ix+7*2) ;Down
jr z,_
sub 4
xor 8
set 2,a
_
ld (z80codebase+keys_low),a
ld a,$FF
key_smc_a = $+2
bit 5,(ix+1*2) ;2ND
jr z,_
dec a
_
key_smc_b = $+2
bit 7,(ix+2*2) ;ALPHA
jr z,_
res 1,a
_
key_smc_select = $+2
bit 7,(ix+3*2) ;X,T,0,n
jr z,_
res 2,a
_
key_smc_start = $+2
bit 6,(ix+1*2) ;MODE
jr z,_
res 3,a
_
ld (z80codebase+keys_high),a
key_smc_menu = $+2
bit 6,(ix+6*2) ;CLEAR
jr z,_
ex af,af'
push af
ACALL(emulator_menu_ingame)
pop af
ex af,af'
or a
jr nz,do_exit_trampoline
state_operation_denied:
ACALL(SetScalingMode)
call reset_preserved_area
jr keys_done
_
ld a,2
key_smc_save_state = $+2
bit 1,(ix+2*2) ;STO>
jr nz,_
key_smc_load_state = $+2
bit 2,(ix+2*2) ;LN
jr z,++_
call check_valid_state
ld hl,(current_state)
ld de,StateNotFoundMessage
jr z,load_state_not_found
_
ld (main_menu_selection),a
ACALL(BackupAndShowConfirmStateOperation)
jr z,state_operation_denied
ld a,4
ld (exitReason),a
do_exit_trampoline:
jr do_exit
_
key_smc_state_slot = $+2
bit 3,(ix+2*2) ;LOG
jr z,_
call update_state_with_numpad
ld a,l
ld (current_state),a
ld de,StateSlotMessage
load_state_not_found:
push hl
ld a,30
ACALL(SetEmulatorMessageWithDuration)
pop hl
jr keys_done
_
xor a
key_smc_brightness_up = $+2
bit 1,(ix+1*2)
jr nz,_
inc a
key_smc_brightness_down = $+2
bit 2,(ix+1*2)
jr z,++_
_
add a,a
dec a
ld hl,mpBlLevel
add a,(hl)
jr z,_
ld (hl),a
_
keys_done:
ld a,(mpIntMaskedStatus)
or a
jr z,_
ld (mpIntAcknowledge),a
#ifdef FASTLOG
ld hl,runtime_error
jr do_exit_any
#else
inc a
ld (exitReason),a
#endif
_
; Reasons for exiting:
; 1: load new game
; 2: exit emulator
; 3: restart game
; 4: load or save state (depending on main_menu_selection)
; 5: delete save state or ROM (depending on current_menu)
; 5+(error*4): exit rom and show error
exitReason = $+1
or 0
jr z,_
do_exit:
APTR(ExitEmulation)
do_exit_any:
ex de,hl
ld hl,z80codebase+event_not_expired
; Emit JP.LIL ExitEmulation
ld (hl),$5B \ inc hl \ ld (hl),$C3 \ inc hl \ ld (hl),de
_
pop iy
pop bc
pop ix
pop de
jp.sis ppu_scheduled
; Input: IX = mpKeypadGrp0
; Output: L = new state
update_state_with_numpad:
ld a,(ix+3*2)
ld l,'0'
rra
ret c
inc l
rra
ret c
ld e,3
add hl,de
rra
ret c
add hl,de
rra
ret c
ld a,(ix+4*2)
ld l,'2'
rra
rra
ret c
add hl,de
rra
ret c
add hl,de
rra
ret c
ld a,(ix+5*2)
ld l,'3'
rra
rra
ret c
add hl,de
rra
ret c
add hl,de
rra
ret c
ld hl,(current_state)
ret
; Acknowledges one or more interrupt sources and then waits on them.
; Interrupt is neither acknowledged nor handled once it triggers.
;
; Inputs: DE = interrupt source mask to wait on
; Interrupts are disabled
; Outputs: Original interrupt mask is restored
; Destroys IX,DE,HL
ack_and_wait_for_interrupt:
ld (mpIntAcknowledge),de
; Waits on one or more interrupt sources. May return immediately.
; Interrupt is neither acknowledged nor handled once it triggers.
;
; Inputs: DE = interrupt source mask to wait on
; Interrupts are disabled
; Outputs: Original interrupt mask is restored
; Destroys BC,DE,HL
wait_for_interrupt:
ld hl,mpIntEnable
ld bc,(hl)
ld (hl),de
ex de,hl
ld hl,z80codebase+rst38h
ld (hl),$C9 ;RET
call.is wait_for_interrupt_stub
ld (hl),$D9 ;EXX
ex de,hl
ld (hl),bc
ret
frame_interrupt:
push de
push bc
call sync_frame_flip_always
pop bc
pop de
jp.sis frame_interrupt_return
; Prepares to render the next frame.
; This swaps the current buffer, resets internal render variables,
; and prepares the current palette pointers.
;
; Inputs: None
; Destroys: AF, BC, DE, HL, IX
prepare_next_frame:
; Swap buffers
ld de,(current_display)
ld hl,(current_buffer)
ld (current_display),hl
ld (current_buffer),de
ld hl,(scanlineLUT_ptr)
ld a,l
cp scanlineLUT_2 & $FF
jr z,_
ld hl,scanlineLUT_1
_
ld (scanlineLUT_ptr),hl
ld (scanlineLUT_sprite_ptr),hl
ld (scanlineLUT_palette_ptr),hl
prepare_initial_frame:
; Reset window state
ld a,(hram_base+LCDC)
rrca
and $20
add a,(vram_tiles_start >> 8) & $FF
ld (window_tile_ptr+1),a
ld a,$A9 ;XOR C
ld (window_trigger_smc),a
; Disable rendering catchup during vblank (or LCD off)
xor a
ld r,a
ld (window_tile_offset),a
ld (myLY),a
ld (myspriteLY),a
ld (mypaletteLY),a
ld (z80codebase+sprite_catchup_available),a
ld (BGP_max_frequency),a
; Make the next rendering operation sync with frame flip,
; if the frame flip hasn't happened yet
ld hl,sync_frame_flip_or_wait
ld (render_scanlines_wait_smc),hl
; Get the negative end-of-buffer pointer
frame_dma_size_smc = $+1
ld hl,0
sbc hl,de
ld (frame_flip_end_check_smc),hl
prepare_next_frame_gbc_smc = $
; Clear the frequency count for the native BGP value
ld a,(BGP_max_value)
ld hl,BGP_frequencies
ld l,a
ld (hl),0
; Get the indices for each palette type
and 3
add a,a
add a,bg_palette_colors & $FF
ld (update_palettes_bgp0_index),a
inc h
ld a,(hl)
add a,a
ld (update_palettes_bgp123_index),a
ret
prepare_next_frame_gbc_no_adjust:
; Fill the scanline usage LUT
or a
sbc hl,hl
add hl,sp
ld b,144/18
ld de,$0A0A0A
ld sp,scanline_sprite_counts + 144
_
push de
push de
push de
push de
push de
push de
djnz -_
ld sp,hl
; Copy current palette values to the internal palette buffer,
; without adjusting colors
ld hl,z80codebase+gbc_bg_palette_data
ld de,gbc_bg_transparent_colors
ld bc,$0816
ld a,l
_
ldi
ldi
add a,b
ld l,a
jr nz,-_
ld l,gbc_bg_palette_data & $FF
ld b,a
ld a,c
_
inc hl
inc hl
ld c,a
ldir
cp l
jr c,-_
ld hl,z80codebase+gbc_obj_palette_data
_
inc hl
inc hl
ld c,a
ldir
cp l
jr c,-_
ret
prepare_next_frame_gbc_adjust:
; Fill the scanline usage LUT
or a
sbc hl,hl
add hl,sp
ld b,144/18
ld de,$0A0A0A
ld sp,scanline_sprite_counts + 144
_
push de
push de
push de
push de
push de
push de
djnz -_
ld sp,hl
; Copy current palette values to the internal palette buffer,
; adjusting colors
ld ix,z80codebase+gbc_bg_palette_data
ld hl,adjust_color_lut
ld de,gbc_bg_transparent_colors
_
ld bc,(ix)
ld l,c
ld a,b
and h ;$7C
or (hl)
ld l,a
dec h ;adjust_green_lsb_lut
ld a,(hl)
add a,c
ld (de),a
inc de
inc h
inc h ;adjust_green_msb_lut
ld a,(hl)
adc a,b
ld (de),a
inc de
dec h ;adjust_color_lut
ld a,ixl
add a,8
ld ixl,a
jr nc,-_
call _
inc ixh ;gbc_obj_palette_data >> 8
_
ld ixl,(gbc_bg_palette_data+2) & $FF
_
lea ix,ix+2
_
ld bc,(ix-2)
ld l,c
ld a,b
and h ;$7C
or (hl)
ld l,a
dec h ;adjust_green_lsb_lut
ld a,(hl)
add a,c
ld (de),a
inc de
inc h
inc h ;adjust_green_msb_lut
ld a,(hl)
adc a,b
ld (de),a
inc de
dec h ;adjust_color_lut
ld a,ixl
and 7
jr nz,--_
or ixl
lea ix,ix+4
jr nz,-_
ret
convert_palette_for_menu:
; Get the current native BGP palette
ld a,(BGP_max_value)
ld e,a
call convert_palette_setup
ld hl,(current_display)
ld c,240
_
ld b,convert_palette_row_loop_count
convert_palette_row_smc_3 = $+1
call convert_palette_row
dec c
jr nz,-_
ret
do_scale_fill:
ld hl,(current_display)
ld ix,160
active_scaling_type = $+1
ld b,0
djnz do_scale_full
ld a,(hram_base+SCY)
last_frame_scy = $+1
ld b,0
ld (last_frame_scy),a
last_scale_offset = $+1
add a,0
sub b
jp m,++_
_
sub 3
jr nc,-_
_
add a,3
jr nc,-_
ld (last_scale_offset),a
ld b,a
ld a,(hram_base+LCDC)
and $20
jr z,do_scale_full
ld a,(hram_base+WY)
dec a
cp 143
jr c,_
do_scale_full:
ld a,143
_
inc a
push af
cpl
do_scale_fill_smc = $+1
call scale_offset
inc a
pop bc
ld c,a
add a,b
sub 144+6
ld b,c
scale_offset:
add a,3
ret c
djnz _
scale_offset_1_loop:
lea de,ix
ld c,e
add hl,de
ex de,hl
add hl,de
ldir
ex de,hl
lea hl,ix
ld c,l
add hl,de
ldir
add a,3
jr nc,scale_offset_1_loop
ret
_
djnz _
lea de,ix
scale_offset_2_loop:
ld c,e
ex de,hl
add hl,de
ex de,hl
ldir
lea hl,ix
ld c,l
add hl,de
ex de,hl
ldir
ex de,hl
lea de,ix
add hl,de
add a,3
jr nc,scale_offset_2_loop
ret
_
ld b,0
scale_offset_0_loop:
ex de,hl
lea hl,ix
ld c,l
add hl,de
ex de,hl
ldir
lea hl,ix
ld c,l
ex de,hl
add hl,bc
ex de,hl
add hl,de
ldir
add a,3
jr nc,scale_offset_0_loop
ret
scale_offset_preserve:
scale_offset_preserve_smc_1 = $+1
ld c,2
scale_offset_preserve_loop:
add a,3
jr nc,_
; Switch to window offset
pop de
pop de
ld b,a
add a,d
sub 144+6
ld de,scale_offset
push de
_
push bc
call scale_offset_preserve_draw
pop bc
dec c
jr nz,scale_offset_preserve_loop
jr scale_offset
scale_offset_preserve_draw:
scale_offset_preserve_smc_2 = $+1
ld c,0
lea de,ix
add hl,de
dec hl
push de
djnz _
add hl,de
ex de,hl
add hl,de
jr +++_
_
ex de,hl
add hl,de
ex de,hl
djnz _
push bc
lddr
pop bc
ex de,hl
inc hl
pop de
add hl,de
ex de,hl
add hl,de
ex de,hl
ldir
lea hl,ix
add hl,de
ret
_
ld b,0
_
push bc
lddr
pop bc
ex de,hl
inc hl
pop de
add hl,de
add hl,de
ex de,hl
add hl,de
ldir
ret
; Displays a digit onscreen at the given framebuffer offset in bytes.
; Draws to the current buffer.
;
; Inputs: B = digit (0-9)
; C = offset
; Outputs: A = 0
; Destroys AF,BC,DE,HL
display_digit:
ld hl,(current_buffer)
ld d,4
ld e,c
mlt de
add hl,de
ex de,hl
ld hl,digits
ld c,24
mlt bc
add hl,bc
ld b,0
ld a,6
_
ld c,4
ldir
ex de,hl
ld c,160 - 4
add hl,bc
ex de,hl
dec a
jr nz,-_
ret
sync_frame_flip_or_wait:
ld a,(mpLcdMis)
or a
inc_real_frame_count_smc_1 = $+1
call nz,inc_real_frame_count
ld hl,(mpLcdCurr)
ld de,(frame_flip_end_check_smc)
add hl,de
jr c,do_frame_flip_always
sync_frame_flip_wait:
ld de,$000800
call wait_for_interrupt
ld a,(mpLcdMis)
bit 3,a
jr nz,sync_frame_flip_always
call flip_gram_display
jr sync_frame_flip_wait
sync_frame_flip:
ld a,(mpLcdMis)
or a
ret z
sync_frame_flip_always:
inc_real_frame_count_smc_2 = $+1
call inc_real_frame_count
do_frame_flip:
ld hl,(mpLcdCurr)
frame_flip_end_check_smc = $+1
ld de,0
add hl,de
ret nc
do_frame_flip_always:
ld (frame_flip_end_check_smc),hl
ld hl,(current_display)
ld (mpLcdBase),hl
active_scaling_mode = $+1
ld a,0
or a
call nz,flip_gram_draw_buffer
do_frame_flip_always_no_gram_flip:
ld hl,sync_frame_flip
ld (render_scanlines_wait_smc),hl
; Update the host LCD palettes based on the currently set GB palettes.
;
; Uses the overlapped_bg_palette_colors table as the source BG colors.
;
; Destroys AF,BC,DE,HL
update_palettes:
update_palettes_gbc_smc = $
update_palettes_bgp0_index = $+1
ld hl,bg_palette_colors
ld de,mpLcdPalette + (255*2)
ld bc,2
ldir
update_palettes_bgp123_index = $+1
ld l,overlapped_bg_palette_colors & $FF
ld d,mpLcdPalette >> 8 & $FF
ld c,3*2
ldir
ret
update_palettes_gbc:
ld hl,gbc_bg_transparent_colors
ld de,mpLcdPalette + (GBC_BG_TRANSPARENT_COLORS*2)
ld bc,8*2
ldir
ld e,GBC_BG_OPAQUE_COLORS*2
ld c,24*2
ld a,c
ldir
ld e,GBC_OBJ_OPAQUE_COLORS*2
ld c,a
ldir
ld l,gbc_bg_opaque_colors & $FF
ld e,GBC_BG_HIGH_PRIO_COLORS*2
ld c,a
ldir
ret
; Swap GRAM sub-buffers in stretched display mode to avoid tearing.
; Input: A=1
; Destroys AF,BC,DE,HL
flip_gram_draw_buffer:
gram_curr_draw_buffer = $+1
xor 0
ld (gram_curr_draw_buffer),a
ld de,spiDrawBufferLeft
jr z,_
ld de,spiDrawBufferRight
_
ld b,spiDrawBufferSize
call spiFastTransfer
; Check if VSYNC was reached before this command completed
ld hl,mpLcdRis
bit 2,(hl)
ld (next_vertical_scroll),de
jr nz,_
; Enable LCD base address update interrupt for display buffer flip
ld l,mpLcdImsc & $FF
ld (hl),$0C
ret
_
; Manually reset the window address because VSYNC may have set the old one
ld de,spiResetWindowAddress
ld b,spiResetWindowAddressSize
call spiFastTransfer
; Go ahead and issue the GRAM display flip command instead of scheduling it
ld a,4
flip_gram_display:
ld (mpLcdIcr),a
ld c,a
next_vertical_scroll = $+1
ld de,spiVerticalScrollLeft
ld b,spiVerticalScrollSize
call spiFastTransfer
; Reset LCD interrupt mask
ld a,8
ld (mpLcdImsc),a
and c
ret z
jr inc_real_frame_count_always
inc_real_frame_count_or_flip_gram_display:
bit 2,a
jr nz,flip_gram_display
ld a,(mpLcdImsc)
xor 4
inc_real_frame_count:
ld (mpLcdIcr),a
inc_real_frame_count_always:
frame_excess_count = $+1
ld a,0
inc a
jp pe,_
ld (frame_excess_count),a
_
ld hl,emulatorMessageDuration
xor a
cp (hl)
jr z,_
dec (hl)
jr nz,_