-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcd.cpp
1766 lines (1596 loc) · 67.7 KB
/
lcd.cpp
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
#include "lcd.h"
#include<iostream>
#include<cstring>
#include<cassert>
#include<fstream>
#include<string>
#ifdef DEBUG
#define ASSERT assert
#else
#define ASSERT //assert
#endif
lcd::lcd() : cgb_mode(false), debug(false), during_dma(false), cycle(0), next_line(0), control{.val=0x00}, bg_scroll_y(0), bg_scroll_x(0),
bgpal{{0,1,2,3}}, obj1pal{{0,1,2,3}}, obj2pal{{0,1,2,3}},
win_scroll_y(0), win_scroll_x(0), active_cycle(0), frame(0),
lyc_next_cycle(0), m0_next_cycle(0), m1_next_cycle(0), m2_next_cycle(0),
cpu_control{.val=0x00}, cpu_status(0), cpu_bg_scroll_y(0), cpu_bg_scroll_x(0), cpu_lyc(0), cpu_dma_addr(0),
cpu_bgpal{{0,1,2,3}}, cpu_obj1pal{{0,1,2,3}}, cpu_obj2pal{{0,1,2,3}}, cpu_win_scroll_y(0), cpu_win_scroll_x(0), cpu_active_cycle(0),
screen(NULL), renderer(NULL), buffer(NULL), texture(NULL), overlay(NULL), lps(NULL), hps(NULL), bg1(NULL), bg2(NULL), sgb_border(NULL), sgb_texture(NULL),
sgb_mode(false), sgb_dump_filename(""), sgb_vram_transfer_type(0), sgb_mask_mode(0), trace(false), vram_bank(0), cpu_vram_bank(0) {
/* Initialize the SDL library */
cur_x_res=SCREEN_WIDTH;
cur_y_res=SCREEN_HEIGHT;
bool success = util::reinit_sdl_screen(&screen, &renderer, &texture, cur_x_res, cur_y_res);
if(!success) {
fprintf(stderr, "Something failed while init'ing video.\n");
return;
}
render = std::bind(&lcd::dmg_render, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
buffer = SDL_CreateRGBSurface(0,SCREEN_WIDTH,SCREEN_HEIGHT,32,0,0,0,0);
overlay = SDL_CreateRGBSurface(0,SCREEN_WIDTH,SCREEN_HEIGHT,32,0,0,0,0);
lps = SDL_CreateRGBSurface(0,SCREEN_WIDTH,SCREEN_HEIGHT,32,0,0,0,0);
hps = SDL_CreateRGBSurface(0,SCREEN_WIDTH,SCREEN_HEIGHT,32,0,0,0,0);
bg1 = SDL_CreateRGBSurface(0,512,512,32,0,0,0,0);
bg2 = SDL_CreateRGBSurface(0,512,512,32,0,0,0,0);
if(buffer && bg1) {
ASSERT(buffer->pitch == SCREEN_WIDTH*4);
ASSERT(bg1->pitch == 512 * 4);
}
//printf("lcd::Setting render draw color to black\n");
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
//printf("lcd::Clearing rendering output\n");
SDL_RenderClear(renderer);
//printf("lcd::Pushing video update to renderer\n");
SDL_RenderPresent(renderer);
//printf("lcd::constructor reached end\n");
//Set default palettes; pal#0 is DMG default, 0-3 are used for SGB, 0-7 are used for CGB
//Semi-colorized default palette
// uint8_t default_palette[] =
// 0 1 2 3
//*bg*/ {255, 255, 255, 170, 170, 170, 85, 85, 85, 0, 0, 0, //B+W, going from white to black
//*win*/ 255, 210, 210, 200, 140, 140, 120, 70, 70, 30, 0, 0, //Light pink to deep, dark red
//*ob1*/ 210, 255, 210, 140, 200, 140, 70, 120, 70, 0, 30, 0, //Seafoam green to Schwarzwald at midnight
//*ob2*/ 210, 210, 255, 140, 140, 200, 70, 70, 120, 0, 0, 30}; //Powder blue to Mariana trench
//DMG inspired palette
uint default_palette[] =
/*bg*/ {155, 188, 15, 139, 172, 15, 48, 98, 48, 15, 56, 15,
/*win*/ 155, 188, 15, 139, 172, 15, 48, 98, 48, 15, 56, 15,
/*ob1*/ 155, 188, 15, 139, 172, 15, 48, 98, 48, 15, 56, 15,
/*ob2*/ 155, 188, 15, 139, 172, 25, 48, 98, 48, 15, 56, 15};
//GB Pocket inspired palette
// uint8_t default_palette[] =
// 0 1 2 3
//*bg*/ {224, 219, 205, 168, 159, 148, 112, 107, 102, 43, 43, 38,
//*win*/ 224, 219, 205, 168, 159, 148, 112, 107, 102, 43, 43, 38,
//*ob1*/ 224, 219, 205, 168, 159, 148, 112, 107, 102, 43, 43, 38,
//*ob2*/ 224, 219, 205, 168, 159, 148, 112, 107, 102, 43, 43, 38};
for(int i=0; buffer && i<4; i++) {
sys_bgpal[0][i] = SDL_MapRGB(buffer->format, default_palette[i*3], default_palette[i*3+1], default_palette[i*3+2]);
sys_winpal[0][i] = SDL_MapRGB(buffer->format, default_palette[i*3+12], default_palette[i*3+13], default_palette[i*3+14]);
sys_obj1pal[0][i] = SDL_MapRGB(buffer->format, default_palette[i*3+24], default_palette[i*3+25], default_palette[i*3+26]);
sys_obj2pal[0][i] = SDL_MapRGB(buffer->format, default_palette[i*3+36], default_palette[i*3+37], default_palette[i*3+38]);
}
//Documentation of how the values in the table above were generated
/*
for(int i=0;buffer && i<4;i++) {
sys_bgpal[0][i] = SDL_MapRGB(buffer->format, 85*(3-i), 85*(3-i), 85*(3-i));
sys_winpal[0][i] = SDL_MapRGB(buffer->format, 80*(3-i)+15, 70*(3-i), 70*(3-i));
sys_obj1pal[0][i] = SDL_MapRGB(buffer->format, 70*(3-i), 80*(3-i)+15, 70*(3-i));
sys_obj2pal[0][i] = SDL_MapRGB(buffer->format, 70*(3-i), 70*(3-i), 80*(3-i)+15);
}
*/
//Print out all SDL palette colors as a debug step
/*
for(int i = 0;i<4;i++) {
for(int j=0;j<4;j++) {
printf("%08x ", sys_bgpal[i][j]);
printf("%08x ", sys_winpal[i][j]);
printf("%08x ", sys_obj1pal[i][j]);
printf("%08x ", sys_obj2pal[i][j]);
}
printf("\n");
}
*/
//Precalculate possible tile rows (about 1/2 MB of data)
for(int b2=0;b2<256;b2++) {
for(int b1=0;b1<256;b1++) {
int index = (b2<<8|b1);
for(int x = 0; x < 8; x++) {
int shift = 128>>(x);
rows[index][x] = ((b1&shift)/shift + 2*((b2&shift)/shift));
}
}
}
}
lcd::~lcd() {
//std::ofstream dump("screen_dump.dat");
//dump.write(reinterpret_cast<char*>(vram[0].data()), 0x2000);
//dump.write(reinterpret_cast<char*>(vram[1].data()), 0x2000);
if(screen) {
SDL_DestroyWindow(screen);
screen = NULL;
}
if(buffer) {
SDL_FreeSurface(buffer);
buffer = NULL;
}
if(overlay) {
SDL_FreeSurface(overlay);
overlay = NULL;
}
if(lps) {
SDL_FreeSurface(lps);
lps = NULL;
}
if(hps) {
SDL_FreeSurface(hps);
hps = NULL;
}
if(bg1) {
SDL_FreeSurface(bg1);
bg1 = NULL;
}
if(bg2) {
SDL_FreeSurface(bg2);
bg2 = NULL;
}
}
uint64_t lcd::run(uint64_t run_to) {
ASSERT(cycle < run_to);
//printf("PPU: Start: %ld run_to: %ld\n", cycle, run_to);
uint64_t render_cycle = 0;
while(!cmd_queue.empty()) {
util::cmd current = cmd_queue.front();
if(current.cycle >= cycle) {
if(!render_cycle) {
render_cycle = render(frame, cycle, current.cycle);
}
else {
render(frame, cycle, current.cycle);
}
}
apply(current.addr, current.val, current.cycle);
cmd_queue.pop();
cycle = current.cycle;
}
//printf("PPU: renderend Startline: %ld endline: %ld\n",start_line,end_line);
if(cycle < run_to) {
if(!render_cycle) {
render_cycle = render(frame, cycle, run_to);
}
else {
render(frame, cycle, run_to);
}
}
cycle = run_to;
//printf("Reporting render at cycle: %lld\n", render_cycle);
return render_cycle; //0 means a frame hasn't been rendered during this timeslice
}
//Apply the data extracted from the command queue, and apply it to the PPU view of the state
void lcd::apply(int addr, uint8_t val, uint64_t cycle) {
//uint8_t prev = 0xb5; //stands for "BS"
if(addr >= 0x8000 && addr < 0xa000) {
uint8_t prev = vram[vram_bank][addr&0x1fff];
vram[vram_bank][addr & 0x1fff] = val;
if(addr < 0x9800 && prev != val) {
update_row_cache(addr, val);
}
}
else if(addr >= 0xfe00 && addr < 0xfea0) {
//prev = oam[addr-0xfe00];
oam[addr-0xfe00] = val;
}
else {
switch(addr) {
case 0xff40:
{
control_reg old_val{.val = control.val};
control.val = val;
if(control.display_enable && !old_val.display_enable) {
active_cycle = cycle;
}
//prev = old_val.val;
}
break;
case 0xff42:
//printf("cycle: %ld (mode: %d line: %d cycle: %d) bg_scroll_y = %d\n", cycle, get_mode(cycle, true), ((cycle-active_cycle)%17556)/CYCLES_PER_LINE, (cycle - active_cycle)%CYCLES_PER_LINE, val);
//prev = bg_scroll_y;
bg_scroll_y = val;
break;
case 0xff43:
//printf("cycle: %ld (mode: %d line: %d cycle: %d) bg_scroll_x = %d\n", cycle, get_mode(cycle, true), ((cycle-active_cycle)%17556)/CYCLES_PER_LINE, (cycle - active_cycle)%CYCLES_PER_LINE ,val);
//prev = bg_scroll_x;
bg_scroll_x = val;
break;
case 0xff46://OAM DMA (deprecated; I'm just going to do this with regular register writes, to maintain the timing)
during_dma = (val==1);
break;
case 0xff47:
bgpal.pal[0] = (val & 0x03);
bgpal.pal[1] = (val & 0x0c)>>2;
bgpal.pal[2] = (val & 0x30)>>4;
bgpal.pal[3] = (val & 0xc0)>>6;
break;
case 0xff48:
obj1pal.pal[0] = (val & 0x03);
obj1pal.pal[1] = (val & 0x0c)>>2;
obj1pal.pal[2] = (val & 0x30)>>4;
obj1pal.pal[3] = (val & 0xc0)>>6;
break;
case 0xff49:
obj2pal.pal[0] = (val & 0x03);
obj2pal.pal[1] = (val & 0x0c)>>2;
obj2pal.pal[2] = (val & 0x30)>>4;
obj2pal.pal[3] = (val & 0xc0)>>6;
break;
case 0xff4a:
win_scroll_y = val;
break;
case 0xff4b:
win_scroll_x = val;
break;
case 0xff4f:
vram_bank = val;
break;
case 0xff68:
cgb_bgpal_index = (val & 0x3f);
cgb_bgpal_advance = ((val & 0x80) == 0x80);
break;
case 0xff69:
{
uint8_t hilo = (cgb_bgpal_index & 1);
uint8_t col = ((cgb_bgpal_index>>1) & 3);
uint8_t pal = (cgb_bgpal_index >> 3);
if(!hilo) { //low byte
cgb_bgpal[pal][col].low = val;
}
else {
cgb_bgpal[pal][col].high = val;
}
sys_bgpal[pal][col] = SDL_MapRGB(buffer->format, cgb_bgpal[pal][col].red * 8, cgb_bgpal[pal][col].green * 8, cgb_bgpal[pal][col].blue * 8);
if(cgb_bgpal_advance) {
cgb_bgpal_index++;
cgb_bgpal_index &= 0x3f;
}
//if(hilo)
//printf("BG pal %d col %d is now (%d, %d, %d)\n", pal, col, cgb_bgpal[pal][col].red * 8, cgb_bgpal[pal][col].green * 8, cgb_bgpal[pal][col].blue * 8);
}
break;
case 0xff6a:
cgb_objpal_index = (val & 0x3f);
cgb_objpal_advance = ((val & 0x80) == 0x80);
break;
case 0xff6b:
{
uint8_t hilo = (cgb_objpal_index & 1);
uint8_t col = ((cgb_objpal_index>>1) & 3);
uint8_t pal = (cgb_objpal_index >> 3);
if(!hilo) { //low byte
cgb_objpal[pal][col].low = val;
}
else {
cgb_objpal[pal][col].high = val;
}
sys_obj1pal[pal][col] = SDL_MapRGB(buffer->format, cgb_objpal[pal][col].red * 8, cgb_objpal[pal][col].green * 8, cgb_objpal[pal][col].blue * 8);
if(cgb_objpal_advance) {
cgb_objpal_index++;
cgb_objpal_index &= 0x3f;
}
//if(hilo)
//printf("Obj pal %d col %d is now (%d, %d, %d)\n", pal, col, cgb_objpal[pal][col].red * 8, cgb_objpal[pal][col].green * 8, cgb_objpal[pal][col].blue * 8);
}
break;
default:
printf("Ignoring 0x%04x = %02x @ %ld\n", addr, val, cycle);
//Various data aren't necessary to render the screen, so we ignore anything like interrupts and status
break;
}
}
//printf("Processing %04x = %02x (prev: %02x) @ %lld\n", addr, val, prev, cycle);
return;
}
void lcd::update_row_cache(uint16_t addr, uint8_t val) {
addr -= 0x8000;
int tilenum = addr>>4;
int row = ((addr>>1) & 7);
int index = vram_bank * 3072 + tilenum * 8 + row;
int data_base = addr & 0xfffe;
int b1 = 0;
int b2 = 0;
if(addr&1) {
b1 = vram[vram_bank][data_base];
b2 = val;
}
else {
b1 = val;
b2 = vram[vram_bank][data_base + 1];
}
row_cache[index] = rows[b2<<8|b1];
return;
}
void lcd::write(int addr, uint8_t val, uint64_t cycle) {
if(trace) {printf("PPU write : %s @ %ld\n", lcd_to_string(addr, val).c_str(), cycle);}
//printf("PPU: 0x%04X = 0x%02x @ %ld (mode %d)\n", addr, *((uint8_t *)val), cycle, get_mode(cycle));
if(addr >= 0x8000 && addr < 0xa000) {
if(get_mode(cycle) != 3 || !cpu_control.display_enable) {
cpu_vram[cpu_vram_bank][addr-0x8000] = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
}
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,0,0});
}
}
else if(addr >= 0xfe00 && addr < 0xfea0) {
if(get_mode(cycle) < 2 || !cpu_control.display_enable || cpu_during_dma) {
cpu_oam[addr - 0xfe00] = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
}
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,1,cpu_during_dma});
}
//else {
// printf("PPU: Cycle %010ld Denied write during mode 2/3: 0x%04x = 0x%02x\n", cycle, addr, *((uint8_t *)val));
//}
}
else {
switch(addr) {
case 0xff40:
{
// printf("FF40: %02x\n", val & 0x04);
control_reg old_val{.val = cpu_control.val};
cpu_control.val = val;
//Re-activates STAT interrupts when screen is reenabled
if(cpu_control.display_enable && !old_val.display_enable) {
cpu_active_cycle = cycle;
update_estimates(cycle);
}
//Disables STAT interrupts when screen is disabled
else if(old_val.display_enable && !cpu_control.display_enable) {
update_estimates(cycle);
}
}
/*
std::cout<<"PPU: CTRL change"<<
" Priority: "<<cpu_control.priority<<
" sprites on : "<<cpu_control.sprite_enable<<
" sprite size: "<<cpu_control.sprite_size<<
" bg map: "<<cpu_control.bg_map<<
" tile addr mode: "<<cpu_control.tile_addr_mode<<
" window enable: "<<cpu_control.window_enable<<
" window map: "<<cpu_control.window_map<<
" display on: "<<cpu_control.display_enable<<std::endl;
*/
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,2,0});
}
break;
case 0xff41:
{
uint8_t old_val = cpu_status;
cpu_status = val&0x78;
//If status flags are changed and screen is active, update times for newly active/inactive interrupts
if(cpu_status != old_val && cpu_control.display_enable) {
update_estimates(cycle);
}
}
//printf("LCD status set to %02X\n", cpu_status);
break;
case 0xff42:
cpu_bg_scroll_y = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,3,0});
}
break;
case 0xff43:
cpu_bg_scroll_x = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,4,0});
}
break;
case 0xff45:
{
uint8_t old_val = cpu_lyc;
cpu_lyc = val;
if(cpu_lyc > 153) { //Out of range to actually trigger
lyc_next_cycle = -1;
}
//If LYC is a new value, then we need to recalculate the interrupt time
else if(old_val != cpu_lyc && cpu_control.display_enable) {
update_estimates(cycle);
}
}
break;
case 0xff46://OAM DMA
//Send whether DMA is active or inactive (this is now just a bool, *not* the actual DMA; the DMA is transmitted as a series of writes directly to OAM)
cmd_queue.emplace(util::cmd{cycle, addr, val});
//Value of DMA addr is set when DMA is first requested, so we don't handle it here
break;
case 0xff47:
cpu_bgpal.pal[0] = val & 0x03;
cpu_bgpal.pal[1] = (val & 0x0c)>>2;
cpu_bgpal.pal[2] = (val & 0x30)>>4;
cpu_bgpal.pal[3] = (val & 0xc0)>>6;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,6,0});
}
break;
case 0xff48:
cpu_obj1pal.pal[0] = val & 0x03;
cpu_obj1pal.pal[1] = (val & 0x0c)>>2;
cpu_obj1pal.pal[2] = (val & 0x30)>>4;
cpu_obj1pal.pal[3] = (val & 0xc0)>>6;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,7,0});
}
break;
case 0xff49:
cpu_obj2pal.pal[0] = val & 0x03;
cpu_obj2pal.pal[1] = (val & 0x0c)>>2;
cpu_obj2pal.pal[2] = (val & 0x30)>>4;
cpu_obj2pal.pal[3] = (val & 0xc0)>>6;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,8,0});
}
break;
case 0xff4a: //TODO: Actually influences mode3 timing
cpu_win_scroll_y = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,9,0});
}
break;
case 0xff4b: //TODO: influences mode3 timing
cpu_win_scroll_x = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(debug) {
uint64_t line = ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE;
int line_cycle = (cycle - cpu_active_cycle) % CYCLES_PER_LINE;
timing_queue.emplace(util::timing_data{line,line_cycle,0x0a,0});
}
break;
case 0xff4f: //CGB VRAM bank
cpu_vram_bank = (val & 1);
cmd_queue.emplace(util::cmd{cycle, addr, uint8_t(val & 1)});
break;
case 0xff68:
cpu_cgb_bgpal_index = (val & 0x3f);
cpu_cgb_bgpal_advance = ((val & 0x80) == 0x80);
cmd_queue.emplace(util::cmd{cycle, addr, val});
break;
case 0xff69:
if(get_mode(cycle) != 3 || !cpu_control.display_enable) {
cpu_cgb_bgpal[cpu_cgb_bgpal_index] = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(cpu_cgb_bgpal_advance) {
cpu_cgb_bgpal_index++;
cpu_cgb_bgpal_index %= 0x3f;
}
}
break;
case 0xff6a:
cpu_cgb_objpal_index = (val & 0x3f);
cpu_cgb_objpal_advance = ((val & 0x80) == 0x80);
cmd_queue.emplace(util::cmd{cycle, addr, val});
break;
case 0xff6b:
if(get_mode(cycle) != 3 || !cpu_control.display_enable) {
cpu_cgb_objpal[cpu_cgb_objpal_index] = val;
cmd_queue.emplace(util::cmd{cycle, addr, val});
if(cpu_cgb_objpal_advance) {
cpu_cgb_objpal_index++;
cpu_cgb_objpal_index %= 0x3f;
}
}
break;
default:
//std::cout<<"Write to video hardware: 0x"<<std::hex<<addr<<" = 0x"<<int(*((uint8_t *)val))<<" (not implemented yet)"<<std::endl;
break;
}
}
//if(trace) printf("PPU write (post): %s\n", lcd_to_string(addr, *((uint8_t *)val)).c_str());
return;
}
//TODO: Doesn't account for variations in mode2 and mode3 times
uint8_t lcd::get_mode(uint64_t cycle, bool ppu_view/*=false*/) {
control_reg cnt = cpu_control;
uint64_t active = cpu_active_cycle;
if(ppu_view) {
cnt = control;
active = active_cycle;
}
if(!cnt.display_enable) {
return 1;
}
int frame_cycle = (cycle - active) % 17556;
div_t line_cycle_dat = div(frame_cycle, CYCLES_PER_LINE);
ASSERT(line_cycle_dat.quot < LINES_PER_FRAME);
ASSERT(line_cycle_dat.quot >= 0);
int mode = 0; //hblank; largest amount of time per frame. May as well use it.
if(line_cycle_dat.quot > LAST_RENDER_LINE) {
mode = 1; //vblank
}
else {
//1. oam access around 20 cycles (mode 2)
//2. transfer to lcd for around 43 cycles (mode 3)
//3. h-blank around 51 cycles (mode 0)
//4. repeat 144 times, then vblank for 1140 cycles (mode 1)
if(line_cycle_dat.rem < MODE_2_LENGTH) mode = 2; //OAM access
else if (line_cycle_dat.rem < MODE_2_LENGTH+MODE_3_LENGTH) mode = 3; //LCD transfer
}
return mode;
}
uint64_t lcd::get_active_cycle() {
return cpu_active_cycle;
}
//Reads the CPU's view of the current state of the PPU
uint8_t lcd::read(int addr, uint64_t cycle) {
uint8_t retval = 0;
//if(size > 1 && size != 0x1000) return;
if(addr >= 0x8000 && addr < 0xa000) {
if(get_mode(cycle) != 3) {
retval = cpu_vram[cpu_vram_bank][addr-0x8000];
}
else retval = 0xff;
}
else if(addr >= 0xfe00 && addr < 0xfea0) {
if(get_mode(cycle) < 2) {
retval = cpu_oam[addr-0xfe00];
}
else retval = 0xff;
}
else {
switch(addr) {
case 0xff40:
retval = cpu_control.val;
break;
case 0xff41:
if(!cpu_control.display_enable) {
retval = BIT7|cpu_status; //return current interrupt flags and v-blank mode, if screen is disabled.
}
else {
int mode = get_mode(cycle);
if(cpu_lyc == ((cycle - cpu_active_cycle) % 17556) / CYCLES_PER_LINE) {
mode |= BIT2;
}
retval = mode | cpu_status | BIT7;
}
break;
case 0xff42:
retval = cpu_bg_scroll_y;
break;
case 0xff43:
retval = cpu_bg_scroll_x;
break;
case 0xff44:
if(!cpu_control.display_enable) {
retval = 0;
}
else {
int frame_cycle = (cycle - cpu_active_cycle) % 17556;
int line = frame_cycle / CYCLES_PER_LINE;
//Weird timing bug, that line 153 reads as line 0 for most of the time
if(line == 153 && frame_cycle % CYCLES_PER_LINE >= 4) {
line = 0;
}
retval = line;
}
break;
case 0xff45:
retval = cpu_lyc;
break;
case 0xff46: //This value is now set in lcd::dma
retval = cpu_dma_addr;
break;
case 0xff47:
retval = cpu_bgpal.pal[0] | cpu_bgpal.pal[1]<<2 | cpu_bgpal.pal[2]<<4 | cpu_bgpal.pal[3]<<6;
break;
case 0xff48:
retval = cpu_obj1pal.pal[0] | cpu_obj1pal.pal[1]<<2 | cpu_obj1pal.pal[2]<<4 | cpu_obj1pal.pal[3]<<6;
break;
case 0xff49:
retval = cpu_obj2pal.pal[0] | cpu_obj2pal.pal[1]<<2 | cpu_obj2pal.pal[2]<<4 | cpu_obj2pal.pal[3]<<6;
break;
case 0xff4a:
retval = cpu_win_scroll_y;
break;
case 0xff4b:
retval = cpu_win_scroll_x;
break;
case 0xff4f:
retval = (0xfe | cpu_vram_bank);
break;
case 0xff68:
retval = cpu_cgb_bgpal_index;
break;
case 0xff69:
retval = cpu_cgb_bgpal[cpu_cgb_bgpal_index];
break;
case 0xff6a:
retval = cpu_cgb_objpal_index;
break;
case 0xff6b:
retval = cpu_cgb_objpal[cpu_cgb_objpal_index];
break;
default:
//std::cout<<"PPU: Read from video hardware: 0x"<<std::hex<<addr<<" (not implemented yet)"<<std::endl;
retval = 0xff;
break;
}
}
if(trace) printf("PPU read: %s\n", lcd_to_string(addr, retval).c_str());
return retval;
}
void lcd::get_tile_row(int tilenum, int row, bool reverse, Arr<uint8_t, 8>& pixels, int bank/*=0*/) {
#ifdef UNCACHED
ASSERT(tilenum < 384); ASSERT(row < 16); ASSERT(pixels.size() == 8);
int addr = tilenum * 16 + row * 2;
int b1 = vram[bank][addr];
int b2 = vram[bank][addr + 1];
for(int x = 0; x < 8; x++) {
int x_i = x;
if(reverse) x_i = 7 - x;
int shift = 128>>(x_i);
pixels[x] = ((b1&shift)/shift + 2*((b2&shift)/shift));
}
return;
#else
int index = bank * 3072 + tilenum * 8 + row;
if(reverse) {
std::copy(row_cache[index].begin(), row_cache[index].end(), pixels.rbegin());
} else {
std::copy(row_cache[index].begin(), row_cache[index].end(), pixels.begin());
}
#endif
}
uint64_t lcd::dmg_render(int frame, uint64_t start_cycle, uint64_t end_cycle) {
if(!control.display_enable) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
return 0;
}
//printf("Raw: start cycle: %lld end: %lld\n", start_cycle, end_cycle);
if(sgb_dump_filename != "") {
std::ofstream chr_out(sgb_dump_filename.c_str());
chr_out.write(reinterpret_cast<char *>(&vram[0][0x0]), 0x2000);
chr_out.close();
sgb_dump_filename = "";
}
uint64_t start_frame_cycle = (start_cycle - active_cycle) % 17556;
uint64_t start_frame_line = start_frame_cycle / CYCLES_PER_LINE;
if(get_mode(start_cycle, true) == 0) { //output from that line has gone to the screen already
start_frame_line++;
}
uint64_t end_frame_cycle = (end_cycle - active_cycle) % 17556;
uint64_t end_frame_line = end_frame_cycle / CYCLES_PER_LINE;
if(get_mode(end_cycle, true) >= 2) { //output from that line *hasn't* gone to the screen yet
end_frame_line--;
}
if(end_frame_line > 153) return 0;
if(end_frame_line < start_frame_line && end_frame_cycle - start_frame_cycle >= CYCLES_PER_LINE) {
end_frame_line+=LINES_PER_FRAME;
}
bool output_sdl = true;
uint64_t output_cycle = 0;
Arr<uint8_t, 8> tile_line;
//uint32_t * pixels = NULL;
Vect<uint8_t> bgmap(SCREEN_WIDTH, 0);
if(!screen||!texture||!renderer) {
printf("PPU: problem!\n");
output_sdl = false;
}
else {
//pixels = ((uint32_t *)buffer->pixels);
//pixels = &out_buf[0];
}
//printf("Render starting conditions: startline: %lld endline: %lld\n", start_frame_line, end_frame_line);
for(unsigned int line=start_frame_line;line <= end_frame_line; line++) {
//printf("Running line: %d\n", line);
int render_line = line % LINES_PER_FRAME;
if(debug && !sgb_mode && render_line == 153 && output_sdl) {
draw_debug_overlay();
continue;
}
else if(render_line > LAST_RENDER_LINE) continue;
//Draw the background
if(control.priority) { //cpu_controls whether the background displays, in regular DMG mode
if(output_sdl && start_frame_line == 0) { //Draw the background color
SDL_SetRenderDrawColor(renderer, 85*(3 - bgpal.pal[0]), 85*(3 - bgpal.pal[0]), 85*(3 - bgpal.pal[0]), 255);
SDL_RenderClear(renderer);
}
uint32_t bgbase = 0x1800 + 0x400 * control.bg_map;
int y_in_pix = (render_line + bg_scroll_y) & 0xff;
int y_tile = y_in_pix / 8;
int y_tile_pix = y_in_pix % 8;
bool unloaded = true;
int tile_num = 0;
int pal_index = 0;
for(int x_out_pix = 0; x_out_pix < SCREEN_WIDTH; x_out_pix++) {
if(x_out_pix % 8 == 0) {
pal_index = sgb_attrs[(render_line/8)*SCREEN_TILE_WIDTH+x_out_pix/8];
}
int x_in_pix = (x_out_pix + bg_scroll_x) & 0xff;
int x_tile = x_in_pix / 8;
int x_tile_pix = x_in_pix % 8;
if(x_tile_pix == 0 || unloaded) {
tile_num = vram[vram_bank][bgbase+y_tile*32+x_tile];
if(!control.tile_addr_mode) {
tile_num = 256 + int8_t(tile_num);
}
get_tile_row(tile_num, y_tile_pix, false, tile_line);
unloaded = false;
}
if(output_sdl /*&& c != 0*/) {
out_buf[render_line * SCREEN_WIDTH + x_out_pix] = sys_bgpal[pal_index][bgpal.pal[tile_line[x_tile_pix]]];
bgmap[x_out_pix] = tile_line[x_tile_pix];
}
}
}
//Draw the window
if(control.window_enable) {
uint32_t winbase = 0x1800 + 0x400 * control.window_map;
int win_y = (render_line - win_scroll_y);
if(win_y >= 0) {
int tile_y = win_y / 8;
int y_tile_pix = win_y % 8;
for(int tile_x = 0; tile_x * 8 + win_scroll_x - 7 < SCREEN_WIDTH; tile_x++) {
int tile_num = vram[vram_bank][winbase+tile_y*32+tile_x];
if(!control.tile_addr_mode) {
tile_num = 256+int8_t(tile_num);
}
get_tile_row(tile_num, y_tile_pix, false, tile_line);
for(int x_tile_pix = 0; x_tile_pix < 8 && x_tile_pix + win_scroll_x + tile_x * 8 - 7 < SCREEN_WIDTH; x_tile_pix++) {
int ycoord = tile_y * 8 + y_tile_pix + win_scroll_y;
int xcoord = tile_x * 8 + x_tile_pix + (win_scroll_x - 7);
int pal_index = 0;
if(xcoord >= 0 && xcoord < SCREEN_WIDTH) {
pal_index = sgb_attrs[(ycoord/8)*SCREEN_TILE_WIDTH+xcoord/8];
}
if(output_sdl && xcoord >= 0) {
out_buf[ycoord * SCREEN_WIDTH + xcoord] = sys_winpal[pal_index][bgpal.pal[tile_line[x_tile_pix]]];
bgmap[xcoord] = tile_line[x_tile_pix];
}
}
}
}
}
//Draw the sprites
if(control.sprite_enable && !during_dma) {
for(int spr = 0; spr < 40; spr++) {
oam_data sprite_dat;
memcpy(&sprite_dat, &oam[spr*4], 4);
int sprite_y = sprite_dat.ypos - 16;
int sprite_x = sprite_dat.xpos - 8;
uint8_t tile = oam[spr*4+2];
int sprite_size = 8 + (control.sprite_size * 8);
//If sprite isn't displayed on this line...
if(sprite_y > render_line || sprite_y + sprite_size <= render_line) {
continue;
}
if(control.sprite_size) {
tile &= 0xfe;
}
int y_i = render_line - sprite_y;
if(sprite_dat.yflip == 1) {
y_i = sprite_size - (y_i + 1);
}
get_tile_row(tile, y_i, sprite_dat.xflip, tile_line);
int pal_index = 0;
if(sprite_x >=0 && sprite_x < SCREEN_WIDTH) {
pal_index = sgb_attrs[(render_line/8)*SCREEN_TILE_WIDTH+sprite_x/8];
}
for(int x=0;x!=8;x++) {
uint8_t col = tile_line[x];
uint32_t color = 0;
if(!col) continue;
int xcoord = sprite_x + x;
int ycoord = render_line;
if(xcoord % 8 == 0 && xcoord >= 0 && xcoord < SCREEN_WIDTH) {
pal_index = sgb_attrs[(render_line/8)*SCREEN_TILE_WIDTH+sprite_x/8];
}
if(sprite_dat.palnum_dmg) {
color = sys_obj2pal[pal_index][obj2pal.pal[col]];
}
else {
color = sys_obj1pal[pal_index][obj1pal.pal[col]];
}
if(xcoord > 159 || xcoord < 0) continue;
if(xcoord >= 0 && xcoord < SCREEN_WIDTH && (bgmap[xcoord] == 0 || !sprite_dat.priority)) {
out_buf[ycoord * SCREEN_WIDTH + xcoord] = color;
}
}
}
}
if(output_sdl && render_line == LAST_RENDER_LINE) {
if(sgb_vram_transfer_type != 0) {
//printf("Map:%02x Scroll:(%02x. %02x) AddrMode:%02x pal:(%x,%x,%x,%x)\n", control.bg_map, bg_scroll_x, bg_scroll_y, control.tile_addr_mode, bgpal.pal[0], bgpal.pal[1], bgpal.pal[2], bgpal.pal[3]);
//uint16_t map_base=0x1800 + 0x400 * control.bg_map;
//for(int i=0;i<0x400;i++) {
//printf("%02x ", vram[map_base+i]);
//if((i+1)%32==0) printf("\n");
//}
do_vram_transfer();
sgb_vram_transfer_type = 0;
}
if(texture) {
SDL_UpdateTexture(texture, NULL, out_buf.data(), 160*4);
}
if(sgb_mode) { //Super GameBoy has a few modes that mask video output
if(sgb_mask_mode == 0) {
float xscale = float(win_x_res) / float(cur_x_res);
float yscale = float(win_y_res) / float(cur_y_res);
sgb_color bgcol = sgb_frame_pals[0].col[0];
SDL_SetRenderDrawColor(renderer, bgcol.red, bgcol.green, bgcol.blue, 255);
SDL_RenderClear(renderer);
SDL_Rect screen_middle{int(48.0*xscale),int(40.0*yscale),int(xscale * SCREEN_WIDTH),int(yscale * SCREEN_HEIGHT)};
SDL_RenderCopy(renderer,texture,NULL,&screen_middle);
SDL_RenderCopy(renderer, sgb_texture, NULL, NULL);
}
else if(sgb_mask_mode == 1) { //Screen frozen
}
else if(sgb_mask_mode == 2) { //Screen to black
SDL_SetRenderDrawColor(renderer, 0,0,0,255);
SDL_RenderClear(renderer);
}
else if(sgb_mask_mode == 3) { //Screen to bg color 0
SDL_SetRenderDrawColor(renderer, sys_bgpal[0][bgpal.pal[0]], sys_bgpal[0][bgpal.pal[0]], sys_bgpal[0][bgpal.pal[0]], 255);
SDL_RenderClear(renderer);
}
if(sgb_mask_mode != 1) {
SDL_RenderPresent(renderer);
}
}
else if(!debug) {
int ww,wh;
SDL_GetWindowSize(screen, &ww, &wh);
int bh = 0;
int bw = 0;
if(ww >= wh) {
bh = wh;
bw = ((wh*160)/144);
}
else {
bw = ww;
bh = ((ww*144)/160);
}
SDL_Rect dontstretch{(ww-bw)>>1, (wh-bh)>>1, bw, bh};
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,&dontstretch);
SDL_RenderPresent(renderer);
}
else { //Debug, but not SGB
SDL_Rect debug_space{CYCLES_PER_LINE,0,SCREEN_WIDTH,SCREEN_HEIGHT};
SDL_RenderCopy(renderer, texture, NULL, &debug_space);
}
}
if(render_line == LAST_RENDER_LINE) {
output_cycle = start_cycle - start_frame_cycle + (LAST_RENDER_LINE*CYCLES_PER_LINE) + MODE_2_LENGTH + MODE_3_LENGTH;
//printf("Outputting at cycle: %lld\n", output_cycle);
}
}
return output_cycle;
}
uint64_t lcd::cgb_render(int frame, uint64_t start_cycle, uint64_t end_cycle) {
if(!control.display_enable) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
return 0;
}
if(sgb_dump_filename != "") {
std::ofstream chr_out(sgb_dump_filename.c_str());
chr_out.write(reinterpret_cast<char *>(&vram[0][0x0]), 0x2000);
chr_out.write(reinterpret_cast<char *>(&vram[1][0x0]), 0x2000);
chr_out.close();
sgb_dump_filename = "";
}
//printf("Raw: start cycle: %lld end: %lld\n", start_cycle, end_cycle);
uint64_t start_frame_cycle = (start_cycle - active_cycle) % 17556;
uint64_t start_frame_line = start_frame_cycle / CYCLES_PER_LINE;
if(get_mode(start_cycle, true) == 0) { //output from that line has gone to the screen already