-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpu_2d.cpp
1200 lines (1037 loc) · 44.6 KB
/
gpu_2d.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
/*
Copyright 2019-2020 Hydr8gon
This file is part of NooDS.
NooDS is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NooDS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstring>
#include "gpu_2d.h"
#include "core.h"
#include "psp/GPU/draw.h"
#include "pspDmac.h"
int I_8[128];
Gpu2D::Gpu2D(Core *core, bool engine): core(core), engine(engine)
{
if (engine == 0)
{
// Set up 2D GPU engine A
bgVramAddr = 0x6000000;
objVramAddr = 0x6400000;
palette = core->memory.getPalette();
oam = core->memory.getOam();
extPalettes = core->memory.getEngAExtPal();
}
else
{
// Set up 2D GPU engine B
bgVramAddr = 0x6200000;
objVramAddr = 0x6600000;
palette = core->memory.getPalette() + 0x400;
oam = core->memory.getOam() + 0x400;
extPalettes = core->memory.getEngBExtPal();
}
for (int i = 0;i < 128; i++) I_8[i] = i * 8;
}
uint32_t Gpu2D::rgb5ToRgb6(uint32_t color)
{
// Convert an RGB5 value to an RGB6 value (the way the 2D engine does it)
// Also keep the extra bits because some of them are used to keep track of stuff
/*uint8_t r = ((color >> 0) & 0xFF) << 1;
uint8_t g = ((color >> 5) & 0xFF) << 1;
uint8_t b = ((color >> 10) & 0xFF) << 1;
return (color & 0xFFFC0000) | (0<<24)|(r << 16) | (g << 8) | b;*/
}
void Gpu2D::drawGbaScanline(int line)
{
}
void Gpu2D::drawScanline(int line)
{
// Reload the internal registers at the start of the frame
if (line == 0)
{
internalX[0] = bgX[0];
internalX[1] = bgX[1];
internalY[0] = bgY[0];
internalY[1] = bgY[1];
}
// Clear the layers
for (int i = 0; i < 5; i++)
memset(layers[i], 0, 256 * sizeof(uint32_t));
memset(objPrio, 4, 256 * sizeof(uint8_t));
// Draw the background layers
// The type of each layer depends on the BG mode
switch (dispCnt & 0x00000007)
{
case 0:
{
if (dispCnt & BIT(8)) drawText(0, line);
if (dispCnt & BIT(9)) drawText(1, line);
if (dispCnt & BIT(10)) drawText(2, line);
if (dispCnt & BIT(11)) drawText(3, line);
break;
}
case 1:
{
if (dispCnt & BIT(8)) drawText(0, line);
if (dispCnt & BIT(9)) drawText(1, line);
if (dispCnt & BIT(10)) drawText(2, line);
if (dispCnt & BIT(11)) drawAffine(3, line);
break;
}
case 2:
{
if (dispCnt & BIT(8)) drawText(0, line);
if (dispCnt & BIT(9)) drawText(1, line);
if (dispCnt & BIT(10)) drawAffine(2, line);
if (dispCnt & BIT(11)) drawAffine(3, line);
break;
}
case 3:
{
if (dispCnt & BIT(8)) drawText(0, line);
if (dispCnt & BIT(9)) drawText(1, line);
if (dispCnt & BIT(10)) drawText(2, line);
if (dispCnt & BIT(11)) drawExtended(3, line);
break;
}
case 4:
{
if (dispCnt & BIT(8)) drawText(0, line);
if (dispCnt & BIT(9)) drawText(1, line);
if (dispCnt & BIT(10)) drawAffine(2, line);
if (dispCnt & BIT(11)) drawExtended(3, line);
break;
}
case 5:
{
if (dispCnt & BIT(8)) drawText(0, line);
if (dispCnt & BIT(9)) drawText(1, line);
if (dispCnt & BIT(10)) drawExtended(2, line);
if (dispCnt & BIT(11)) drawExtended(3, line);
break;
}
case 6:
{
if (dispCnt & BIT(10)) drawLarge(2, line);
break;
}
default:
{
printf("Unknown engine %c BG mode: %d\n", ((engine == 0) ? 'A' : 'B'), dispCnt & 0x00000007);
break;
}
}
// Draw the objects
if (dispCnt & BIT(12)) drawObjects(line);
// Blend the layers to form the final image
for (int i = 0; i < 256; i++)
{
uint8_t enabled = BIT(5) | (dispCnt >> 8);
uint16_t *pixel = &framebuffer[line * 256 + i];
// If the current pixel is in the bounds of a window, disable layers that are disabled in that window
if (dispCnt & 0x0000E000) // Windows enabled
{
if ((dispCnt & BIT(13)) && i >= winX1[0] && i < winX2[0] && line >= winY1[0] && line < winY2[0])
enabled &= winIn >> 0; // Window 0
else if ((dispCnt & BIT(14)) && i >= winX1[1] && i < winX2[1] && line >= winY1[1] && line < winY2[1])
enabled &= winIn >> 8; // Window 1
else if ((dispCnt & BIT(15)) && (*pixel & BIT(24)))
enabled &= winOut >> 8; // Object window
else
enabled &= winOut >> 0; // Outside of windows
}
// Set the topmost two pixels to the backdrop color (first palette index)
uint32_t pixel2 = *pixel = U8TO16(palette, 0);
int blendBit = 5, blendBit2 = 5;
int priority = 4, priority2 = 4;
// If an object pixel exists, set it to the topmost pixel
// Objects are higher priority than background layers, so the priority is given a little boost
if ((enabled & BIT(4)) && (layers[4][i] & BIT(15)))
{
*pixel = layers[4][i];
blendBit = 4;
priority = objPrio[i] - 1;
}
// Look for pixels in the background layers
for (int j = 3; j >= 0; j--)
{
// Update the topmost pixels if a higher priority pixel is found
// 3D pixels (marked by bit 26) are special cases that have higher-precision alpha values
if ((enabled & BIT(j)) && (layers[j][i] & ((layers[j][i] & BIT(26)) ? 0xFC0000 : BIT(15))))
{
if ((bgCnt[j] & 0x0003) <= priority) // Higher than topmost
{
// Move the topmost pixel to the second topmost
pixel2 = *pixel;
blendBit2 = blendBit;
priority2 = priority;
// Update the topmost pixel
*pixel = layers[j][i];
blendBit = j;
priority = (bgCnt[j] & 0x0003);
}
else if ((bgCnt[j] & 0x0003) <= priority2) // Higher than second topmost
{
// Update the second topmost pixel
pixel2 = layers[j][i];
blendBit2 = j;
priority2 = (bgCnt[j] & 0x0003);
}
}
}
// Convert the pixels to 18-bit if they aren't 3D pixels that were already 18-bit
/*if (!(*pixel & BIT(26))) *pixel = rgb5ToRgb6(*pixel);
if (!(pixel2 & BIT(26))) pixel2 = rgb5ToRgb6(pixel2);*/
int mode = (bldCnt & 0x00C0) >> 6;
bool blend = ((enabled & BIT(5)) && (bldCnt & BIT(blendBit)));
// Blend the pixel if enabled
// Semi-transparent objects and 3D are special cases that force alpha blending (marked by bits 25 and 26)
// If special cases don't have a second target to blend with, they can fall back to brightness effects
// Blending is done with 18-bit colors on the DS
if (((blend && mode == 1) || (*pixel & (BIT(25) | BIT(26)))) && (bldCnt & BIT(8 + blendBit2))) // Alpha blending
{
if (*pixel & BIT(26)) // 3D
{
int eva = ((*pixel >> 18) & 0x3F) + 1;
int evb = 64 - eva;
int r = ((*pixel >> 0) & 0x3F) * eva / 64 + ((pixel2 >> 0) & 0x3F) * evb / 64; if (r > 63) r = 63;
int g = ((*pixel >> 5) & 0x3F) * eva / 64 + ((pixel2 >> 5) & 0x3F) * evb / 64; if (g > 63) g = 63;
int b = ((*pixel >> 10) & 0x3F) * eva / 64 + ((pixel2 >> 10) & 0x3F) * evb / 64; if (b > 63) b = 63;
*pixel = (b << 10) | (g << 5) | r;
}
else
{
int eva = (bldAlpha & 0x001F) >> 0; if (eva > 16) eva = 16;
int evb = (bldAlpha & 0x1F00) >> 8; if (evb > 16) evb = 16;
int r = ((*pixel >> 0) & 0x3F) * eva / 16 + ((pixel2 >> 0) & 0x3F) * evb / 16; if (r > 63) r = 63;
int g = ((*pixel >> 5) & 0x3F) * eva / 16 + ((pixel2 >> 5) & 0x3F) * evb / 16; if (g > 63) g = 63;
int b = ((*pixel >> 10) & 0x3F) * eva / 16 + ((pixel2 >> 10) & 0x3F) * evb / 16; if (b > 63) b = 63;
*pixel = (b << 10) | (g << 5) | r;
}
}
else if (blend && mode == 2) // Brightness increase
{
int r = (*pixel >> 0) & 0x3F; r += (63 - r) * bldY / 16;
int g = (*pixel >> 5) & 0x3F; g += (63 - g) * bldY / 16;
int b = (*pixel >> 10) & 0x3F; b += (63 - b) * bldY / 16;
*pixel = (b << 10) | (g << 5) | r;
}
else if (blend && mode == 3) // Brightness decrease
{
int r = (*pixel >> 0) & 0x3F; r -= r * bldY / 16;
int g = (*pixel >> 5) & 0x3F; g -= g * bldY / 16;
int b = (*pixel >> 10) & 0x3F; b -= b * bldY / 16;
*pixel = (b << 10) | (g << 5) | r;
}
}
}
void Gpu2D::finishScanline(int line)
{
// Redraw the scanline if the display isn't set to layer mode
switch ((dispCnt & 0x00030000) >> 16) // Display mode
{
case 0: // Display off
{
// Fill the display with white
memset(&framebuffer[line * 256], 0xFF, 256 * sizeof(uint32_t));
break;
}
case 2: // VRAM display
{
// Draw raw bitmap data from a VRAM block
uint32_t address = 0x6800000 + ((dispCnt & 0x000C0000) >> 18) * 0x20000 + line * 256 * 2;
for (int i = 0; i < 256; i++)
framebuffer[line * 256 + i] = (core->memory.read<uint16_t>(0, address + i * 2));
break;
}
case 3: // Main memory display
{
printf("Unimplemented engine %c display mode: display FIFO\n", ((engine == 0) ? 'A' : 'B'));
break;
}
}
// Apply the master brightness
// This is only on the DS, and is done with 18-bit colors
switch ((masterBright & 0xC000) >> 14) // Mode
{
case 1: // Up
{
// Get the brightness factor
int factor = (masterBright & 0x001F);
if (factor==0) break;
if (factor > 16) factor = 16;
for (int i = 0; i < 256; i++)
{
// Extract the RGB values
uint16_t *pixel = &framebuffer[line * 256 + i];
uint8_t r = (*pixel >> 0) & 0x3F;
uint8_t g = (*pixel >> 5) & 0x3F;
uint8_t b = (*pixel >> 10) & 0x3F;
// Adjust the values and put them back
r += (63 - r) * factor / 16;
g += (63 - g) * factor / 16;
b += (63 - b) * factor / 16;
*pixel = (0 << 15) | (b << 10) | (g << 5) | r;
}
break;
}
case 2: // Down
{
// Get the brightness factor
int factor = (masterBright & 0x001F);
if (factor==0) break;
if (factor > 16) factor = 16;
for (int i = 0; i < 256; i++)
{
// Extract the RGB values
uint16_t *pixel = &framebuffer[line * 256 + i];
uint8_t r = (*pixel >> 0) & 0x3F;
uint8_t g = (*pixel >> 5) & 0x3F;
uint8_t b = (*pixel >> 10) & 0x3F;
uint8_t a = (*pixel >> 15) & 0x2;
// Adjust the values and put them back
r -= r * factor / 16;
g -= g * factor / 16;
b -= b * factor / 16;
*pixel = (a << 15) | (b << 10) | (g << 5) | r;
}
break;
}
}
}
void Gpu2D::drawText(int bg, int line)
{
// If 3D is enabled, render it to BG0 in text mode
if (bg == 0 && (dispCnt & BIT(3)))
{
/*sceKernelDcacheWritebackInvalidateAll();
sceDmacMemcpy(layers[bg], core->gpu3DRenderer.getFramebuffer(line), 256 * sizeof(uint32_t));
sceKernelDcacheWritebackInvalidateAll();*/
memcpy(layers[bg], core->gpu3DRenderer.getFramebuffer(line), 256 * sizeof(uint32_t));
return;
}
// Get the base data addresses
uint32_t tileBase = bgVramAddr + ((bgCnt[bg] & 0x1F00) >> 8) * 0x0800 + ((dispCnt & 0x38000000) >> 27) * 0x10000;
uint32_t indexBase = bgVramAddr + ((bgCnt[bg] & 0x003C) >> 2) * 0x4000 + ((dispCnt & 0x07000000) >> 24) * 0x10000;
// Move the tile address to the current line
int yOffset = (line + bgVOfs[bg]) % 512;
tileBase += ((yOffset / 8) % 32) * 64;
// If the Y-offset exceeds 256 and the background is 512 pixels tall, move to the next 256x256 section
// If the background is 512 pixels wide, move 2 sections to skip the second X section
if (yOffset >= 256 && (bgCnt[bg] & BIT(15)))
tileBase += (bgCnt[bg] & BIT(14)) ? 0x1000 : 0x800;
// Draw a line
if (bgCnt[bg] & BIT(7)) // 8-bit
{
for (int i = 0; i <= 256; i += 8)
{
// Move the tile address to the current tile
int xOffset = (bgHOfs[bg] + i) % 512;
uint32_t tileAddr = tileBase + ((xOffset / 8) % 32) * 2;
// If the X-offset exceeds 256 and the background is 512 pixels wide, move to the next 256x256 section
if (xOffset >= 256 && (bgCnt[bg] & BIT(14)))
tileAddr += 0x800;
// Get the current tile
uint16_t tile = core->memory.read<uint16_t>(false, tileAddr);
// Get the tile's palette
uint8_t *pal;
if (dispCnt & BIT(30)) // Extended palette
{
// Determine the extended palette slot
// Backgrounds 0 and 1 can alternatively use slots 2 and 3
int slot = (bg < 2 && (bgCnt[bg] & BIT(13))) ? (bg + 2) : bg;
// In extended palette mode, the tile can select from multiple 256-color palettes
if (!extPalettes[slot]) return;
pal = &extPalettes[slot][(tile & 0xF000) >> 3];
}
else // Standard palette
{
pal = palette;
}
// Get the palette indices for the current line of the tile, flipped vertically if enabled
uint32_t indexAddr = indexBase + (tile & 0x03FF) * 64 + ((tile & BIT(11)) ? ((7 - yOffset % 8) * 8) : ((yOffset % 8) * 8));
uint64_t indices = core->memory.read<uint32_t>(false, indexAddr) |
((uint64_t)core->memory.read<uint32_t>(false, indexAddr + 4) << 32);
// Draw the current line of the tile
for (int j = 0; j < 8; j++)
{
// Flip the tile horizontally if enabled
int offset = i - (xOffset % 8) + ((tile & BIT(10)) ? (7 - j) : j);
// Draw a pixel
if (offset >= 0 && offset < 256 && (indices & 0xFF))
layers[bg][offset] = U8TO16(pal, (indices & 0xFF) * 2) | BIT(15);
// Move to the next palette index
indices >>= 8;
}
}
}
else // 4-bit
{
for (int i = 0; i <= 256; i += 8)
{
// Move the tile address to the current tile
int xOffset = (bgHOfs[bg] + i) % 512;
uint32_t tileAddr = tileBase + ((xOffset / 8) % 32) * 2;
// If the X-offset exceeds 256 and the background is 512 pixels wide, move to the next 256x256 section
if (xOffset >= 256 && (bgCnt[bg] & BIT(14)))
tileAddr += 0x800;
// Get the current tile
uint16_t tile = core->memory.read<uint16_t>(false, tileAddr);
// Get the tile's palette
// In 4-bit mode, the tile can select from multiple 16-color palettes
uint8_t *pal = &palette[((tile & 0xF000) >> 12) * 32];
// Get the palette indices for the current line of the tile, flipped vertically if enabled
uint32_t indexAddr = indexBase + (tile & 0x03FF) * 32 + ((tile & BIT(11)) ? ((7 - yOffset % 8) * 4) : ((yOffset % 8) * 4));
uint32_t indices = core->memory.read<uint32_t>(false, indexAddr);
// Draw the current line of the tile
for (int j = 0; j < 8; j++)
{
// Flip the tile horizontally if enabled
int offset = i - (xOffset % 8) + ((tile & BIT(10)) ? (7 - j) : j);
// Draw a pixel
if (offset >= 0 && offset < 256 && (indices & 0xF))
layers[bg][offset] = U8TO16(pal, (indices & 0xF) * 2) | BIT(15);
// Move to the next palette index
indices >>= 4;
}
}
}
}
void Gpu2D::drawAffine(int bg, int line)
{
// Get the base data addresses
uint32_t tileBase = bgVramAddr + ((bgCnt[bg] & 0x1F00) >> 8) * 0x0800 + ((dispCnt & 0x38000000) >> 27) * 0x10000;
uint32_t indexBase = bgVramAddr + ((bgCnt[bg] & 0x003C) >> 2) * 0x4000 + ((dispCnt & 0x07000000) >> 24) * 0x10000;
// Get the background's size
int size = 128 << ((bgCnt[bg] & 0xC000) >> 14);
// Draw a line
for (int i = 0; i < 256; i++)
{
// Calculate the rotscaled coordinates relative to the background
int rotscaleX = (internalX[bg - 2] + bgPA[bg - 2] * i) >> 8;
int rotscaleY = (internalY[bg - 2] + bgPC[bg - 2] * i) >> 8;
// Handle display area overflow
if (bg < 2 || (bgCnt[bg] & BIT(13))) // Wraparound
{
rotscaleX %= size; if (rotscaleX < 0) rotscaleX += size;
rotscaleY %= size; if (rotscaleY < 0) rotscaleY += size;
}
else if (rotscaleX < 0 || rotscaleX >= size || rotscaleY < 0 || rotscaleY >= size) // Transparent
{
continue;
}
// Get the current tile
uint8_t tile = core->memory.read<uint8_t>(false, tileBase + (rotscaleY / 8) * (size / 8) + (rotscaleX / 8));
// Get the palette index for the current pixel of the tile
uint32_t indexAddr = indexBase + tile * 64 + (rotscaleY % 8) * 8 + (rotscaleX % 8);
uint8_t index = core->memory.read<uint8_t>(false, indexAddr);
// Draw a pixel
if (index)
layers[bg][i] = U8TO16(palette, index * 2) | BIT(15);
}
// Increment the internal registers at the end of the scanline
internalX[bg - 2] += bgPB[bg - 2];
internalY[bg - 2] += bgPD[bg - 2];
}
void Gpu2D::drawExtended(int bg, int line)
{
if ((bgCnt[bg] & BIT(7))) // Bitmap
{
uint32_t dataBase = bgVramAddr;
int sizeX, sizeY;
// Get the base data address
dataBase += ((bgCnt[bg] & 0x1F00) >> 8) * 0x4000;
// Get the bitmap size
switch ((bgCnt[bg] & 0xC000) >> 14)
{
case 0: sizeX = 128; sizeY = 128; break;
case 1: sizeX = 256; sizeY = 256; break;
case 2: sizeX = 512; sizeY = 256; break;
case 3: sizeX = 512; sizeY = 512; break;
}
if (bgCnt[bg] & BIT(2)) // Direct color bitmap
{
// Draw a line
for (int i = 0; i < 256; i++)
{
// Calculate the rotscaled coordinates relative to the background
int rotscaleX = (internalX[bg - 2] + bgPA[bg - 2] * i) >> 8;
int rotscaleY = (internalY[bg - 2] + bgPC[bg - 2] * i) >> 8;
// Handle display area overflow
if (bgCnt[bg] & BIT(13)) // Wraparound
{
rotscaleX %= sizeX; if (rotscaleX < 0) rotscaleX += sizeX;
rotscaleY %= sizeY; if (rotscaleY < 0) rotscaleY += sizeY;
}
else if (rotscaleX < 0 || rotscaleX >= sizeX || rotscaleY < 0 || rotscaleY >= sizeY) // Transparent
{
continue;
}
// Draw a pixel
layers[bg][i] = core->memory.read<uint16_t>(false, dataBase + (rotscaleY * sizeX + rotscaleX) * 2);
}
}
else // 256 color bitmap
{
// Draw a line
for (int i = 0; i < 256; i++)
{
// Calculate the rotscaled coordinates relative to the background
int rotscaleX = (internalX[bg - 2] + bgPA[bg - 2] * i) >> 8;
int rotscaleY = (internalY[bg - 2] + bgPC[bg - 2] * i) >> 8;
// Handle display area overflow
if (bgCnt[bg] & BIT(13)) // Wraparound
{
rotscaleX %= sizeX; if (rotscaleX < 0) rotscaleX += sizeX;
rotscaleY %= sizeY; if (rotscaleY < 0) rotscaleY += sizeY;
}
else if (rotscaleX < 0 || rotscaleX >= sizeX || rotscaleY < 0 || rotscaleY >= sizeY) // Transparent
{
continue;
}
// Get the palette index for the current pixel
uint8_t index = core->memory.read<uint8_t>(false, dataBase + rotscaleY * sizeX + rotscaleX);
// Draw a pixel
if (index)
layers[bg][i] = U8TO16(palette, index * 2) | BIT(15);
}
}
}
else // Extended affine
{
// Get the base data addresses
uint32_t tileBase = bgVramAddr + ((bgCnt[bg] & 0x1F00) >> 8) * 0x0800 + ((dispCnt & 0x38000000) >> 27) * 0x10000;
uint32_t indexBase = bgVramAddr + ((bgCnt[bg] & 0x003C) >> 2) * 0x4000 + ((dispCnt & 0x07000000) >> 24) * 0x10000;
// Get the background's size
int size = 128 << ((bgCnt[bg] & 0xC000) >> 14);
// Draw a line
for (int i = 0; i < 256; i++)
{
// Calculate the rotscaled coordinates relative to the background
int rotscaleX = (internalX[bg - 2] + bgPA[bg - 2] * i) >> 8;
int rotscaleY = (internalY[bg - 2] + bgPC[bg - 2] * i) >> 8;
// Handle display area overflow
if (bg < 2 || (bgCnt[bg] & BIT(13))) // Wraparound
{
rotscaleX %= size; if (rotscaleX < 0) rotscaleX += size;
rotscaleY %= size; if (rotscaleY < 0) rotscaleY += size;
}
else if (rotscaleX < 0 || rotscaleX >= size || rotscaleY < 0 || rotscaleY >= size) // Transparent
{
continue;
}
// Get the current tile
uint32_t tileAddr = tileBase + ((rotscaleY / 8) * (size / 8) + (rotscaleX / 8)) * 2;
uint16_t tile = core->memory.read<uint16_t>(false, tileAddr);
// Get the tile's palette
uint8_t *pal;
if (dispCnt & BIT(30)) // Extended palette
{
// In extended palette mode, the tile can select from multiple 256-color palettes
if (!extPalettes[bg]) continue;
pal = &extPalettes[bg][(tile & 0xF000) >> 3];
}
else // Standard palette
{
pal = palette;
}
// Get the palette index for the current pixel of the tile, flipped vertically or horizontally if enabled
uint32_t indexAddr = indexBase + (tile & 0x03FF) * 64;
indexAddr += ((tile & BIT(11)) ? (7 - rotscaleY % 8) : (rotscaleY % 8)) * 8;
indexAddr += ((tile & BIT(10)) ? (7 - rotscaleX % 8) : (rotscaleX % 8));
uint8_t index = core->memory.read<uint8_t>(false, indexAddr);
// Draw a pixel
if (index)
layers[bg][i] = U8TO16(pal, index * 2) | BIT(15);
}
}
// Increment the internal registers at the end of the scanline
internalX[bg - 2] += bgPB[bg - 2];
internalY[bg - 2] += bgPD[bg - 2];
}
void Gpu2D::drawLarge(int bg, int line)
{
// Get the bitmap size
int sizeX, sizeY;
if (((bgCnt[bg] & 0xC000) >> 14) == 0)
{
sizeX = 512;
sizeY = 1024;
}
else
{
sizeX = 1024;
sizeY = 512;
}
// Draw a line
for (int i = 0; i < 256; i++)
{
// Calculate the rotscaled coordinates relative to the background
int rotscaleX = (internalX[bg - 2] + bgPA[bg - 2] * i) >> 8;
int rotscaleY = (internalY[bg - 2] + bgPC[bg - 2] * i) >> 8;
// Handle display area overflow
if (bgCnt[bg] & BIT(13)) // Wraparound
{
rotscaleX %= sizeX; if (rotscaleX < 0) rotscaleX += sizeX;
rotscaleY %= sizeY; if (rotscaleY < 0) rotscaleY += sizeY;
}
else if (rotscaleX < 0 || rotscaleX >= sizeX || rotscaleY < 0 || rotscaleY >= sizeY) // Transparent
{
continue;
}
// A full large bitmap requires 512KB of VRAM, but engine B can only use 128KB
// For engine B, wrap the 128KB bitmap 4 times to cover the full area
if (engine == 1)
rotscaleY %= sizeY / 4;
// Get the palette index for the current pixel
uint8_t index = core->memory.read<uint8_t>(0, bgVramAddr + rotscaleY * sizeX + rotscaleX);
// Draw a pixel
if (index)
layers[bg][i] = U8TO16(palette, index * 2) | BIT(15);
}
// Increment the internal registers at the end of the scanline
internalX[bg - 2] += bgPB[bg - 2];
internalY[bg - 2] += bgPD[bg - 2];
}
void Gpu2D::drawObjects(int line)
{
// Loop through and draw the 128 sprites in OAM
for (int i = 0; i < 128; i++)
{
// Get the current object
// Each object takes up 8 bytes in memory, but the last 2 bytes are reserved for rotscale
uint16_t object[3];
object[0] = U8TO16(oam, I_8[i]);
// Skip sprites that are disabled
if (!(object[0] & BIT(8)) && (object[0] & BIT(9)))
continue;
object[1] = U8TO16(oam, I_8[i] + 2);
object[2] = U8TO16(oam, I_8[i] + 4);
// Determine the dimensions of the object
int width = 0, height = 0;
switch ((object[1] & 0xC000) >> 14) // Size
{
case 0:
{
switch ((object[0] & 0xC000) >> 14) // Shape
{
case 0: width = 8; height = 8; break; // Square
case 1: width = 16; height = 8; break; // Horizontal
case 2: width = 8; height = 16; break; // Vertical
}
break;
}
case 1:
{
switch ((object[0] & 0xC000) >> 14) // Shape
{
case 0: width = 16; height = 16; break; // Square
case 1: width = 32; height = 8; break; // Horizontal
case 2: width = 8; height = 32; break; // Vertical
}
break;
}
case 2:
{
switch ((object[0] & 0xC000) >> 14) // Shape
{
case 0: width = 32; height = 32; break; // Square
case 1: width = 32; height = 16; break; // Horizontal
case 2: width = 16; height = 32; break; // Vertical
}
break;
}
case 3:
{
switch ((object[0] & 0xC000) >> 14) // Shape
{
case 0: width = 64; height = 64; break; // Square
case 1: width = 64; height = 32; break; // Horizontal
case 2: width = 32; height = 64; break; // Vertical
}
break;
}
}
// Double the object bounds for rotscale objects with the double size bit set
int width2 = width, height2 = height;
if ((object[0] & BIT(8)) && (object[0] & BIT(9)))
{
width2 <<= 2;
height2 <<= 2;
}
// Get the Y coordinate and wrap it around if it exceeds the screen bounds
int y = (object[0] & 0x00FF);
if (y >= 192) y -= 256;
// Don't draw anything if the current scanline lies outside of the object's bounds
int spriteY = line - y;
if (spriteY < 0 || spriteY >= height2)
continue;
// Get the X coordinate and wrap it around if it exceeds the screen bounds
int x = (object[1] & 0x01FF);
if (x >= 256) x -= 512;
int type = (object[0] & 0x0C00) >> 10;
uint8_t prio = (object[2] & 0x0C00) >> 10;
// Draw bitmap objects
if (type == 3)
{
uint32_t dataBase;
int bitmapWidth;
// Determine the address and width of the bitmap
if (dispCnt & BIT(6)) // 1D mapping
{
dataBase = objVramAddr + (object[2] & 0x03FF) * ((dispCnt & BIT(22)) ? 256 : 128);
bitmapWidth = width;
}
else // 2D mapping
{
uint8_t xMask = (dispCnt & BIT(5)) ? 0x1F : 0x0F;
dataBase = objVramAddr + (object[2] & 0x03FF & xMask) * 0x10 + (object[2] & 0x03FF & ~xMask) * 0x80;
bitmapWidth = (dispCnt & BIT(5)) ? 256 : 128;
}
if (object[0] & BIT(8)) // Rotscale
{
// Get the rotscale parameters
int params[4];
for (int j = 0; j < 4; j++)
params[j] = (int16_t)U8TO16(oam, ((object[1] & 0x3E00) >> 9) * 0x20 + j * 8 + 6);
// Draw a line of the object
for (int j = 0; j < width2; j++)
{
int offset = x + j;
if (offset < 0 || offset >= 256) continue;
// Calculate the rotscaled X coordinate relative to the object
int rotscaleX = ((params[0] * (j - width2 / 2) + params[1] * (spriteY - height2 / 2)) >> 8) + width / 2;
if (rotscaleX < 0 || rotscaleX >= width) continue;
// Calculate the rotscaled Y coordinate relative to the object
int rotscaleY = ((params[2] * (j - width2 / 2) + params[3] * (spriteY - height2 / 2)) >> 8) + height / 2;
if (rotscaleY < 0 || rotscaleY >= height) continue;
// Draw a pixel if the old one is lower priority
uint16_t pixel = core->memory.read<uint16_t>(0, dataBase + (rotscaleY * bitmapWidth + rotscaleX) * 2);
if ((pixel & BIT(15)) && prio < objPrio[offset])
{
layers[4][offset] = pixel;
objPrio[offset] = prio;
}
}
}
else
{
// Draw a line of the object
for (int j = 0; j < width; j++)
{
int offset = x + j;
if (offset < 0 || offset >= 256) continue;
// Draw a pixel if the old one is lower priority
uint16_t pixel = core->memory.read<uint16_t>(0, dataBase + (spriteY * bitmapWidth + j) * 2);
if ((pixel & BIT(15)) && prio < objPrio[offset])
{
layers[4][offset] = pixel;
objPrio[offset] = prio;
}
}
}
continue;
}
// Get the base tile address
uint16_t bound = (dispCnt & BIT(4)) ? (32 << ((dispCnt & 0x00300000) >> 20)) : 32;
uint32_t tileBase = objVramAddr + (object[2] & 0x03FF) * bound;
if (object[0] & BIT(8)) // Rotscale
{
// Get the rotscale parameters
int params[4];
for (int j = 0; j < 4; j++)
params[j] = (int16_t)U8TO16(oam, ((object[1] & 0x3E00) >> 9) * 0x20 + j * 8 + 6);
if (object[0] & BIT(13)) // 8-bit
{
int mapWidth = (dispCnt & BIT(4)) ? width : 128;
// Get the object's palette
uint8_t *pal;
if (dispCnt & BIT(31)) // Extended palette
{
// In extended palette mode, the object can select from multiple 256-color palettes
if (!extPalettes[4]) continue;
pal = &extPalettes[4][(object[2] & 0xF000) >> 3];
}
else // Standard palette
{
pal = &palette[0x200];
}
// Draw a line of the object
for (int j = 0; j < width2; j++)
{
int offset = x + j;
if (offset < 0 || offset >= 256) continue;
// Calculate the rotscaled X coordinate relative to the object
int rotscaleX = ((params[0] * (j - width2 / 2) + params[1] * (spriteY - height2 / 2)) >> 8) + width / 2;
if (rotscaleX < 0 || rotscaleX >= width) continue;
// Calculate the rotscaled Y coordinate relative to the object
int rotscaleY = ((params[2] * (j - width2 / 2) + params[3] * (spriteY - height2 / 2)) >> 8) + height / 2;
if (rotscaleY < 0 || rotscaleY >= height) continue;
// Get the palette index for the current pixel
uint8_t index = core->memory.read<uint8_t>(false, tileBase +
((rotscaleY / 8) * mapWidth + rotscaleY % 8) * 8 + (rotscaleX / 8) * 64 + rotscaleX % 8);
if (index && type == 2) // Object window
{
// Mark object window pixels with an extra bit, and don't draw anything
framebuffer[line * 256 + offset] |= BIT(24);
}
else
{
// Draw a pixel if the old one is transparent or lower priority
// Semi-transparent pixels are marked with an extra bit
if (index && (!(layers[4][offset] & BIT(15)) || prio < objPrio[offset]))
{
layers[4][offset] = ((type == 1) ? BIT(25) : 0) | BIT(15) | U8TO16(pal, index * 2);
objPrio[offset] = prio;
}
}
}
}
else // 4-bit
{
int mapWidth = (dispCnt & BIT(4)) ? width : 256;
// Get the object's palette
// In 4-bit mode, the object can select from multiple 16-color palettes
uint8_t *pal = &palette[0x200 + ((object[2] & 0xF000) >> 12) * 32];
// Draw a line of the object
for (int j = 0; j < width2; j++)
{
int offset = x + j;
if (offset < 0 || offset >= 256) continue;
// Calculate the rotscaled X coordinate relative to the object
int rotscaleX = ((params[0] * (j - width2 / 2) + params[1] * (spriteY - height2 / 2)) >> 8) + width / 2;
if (rotscaleX < 0 || rotscaleX >= width) continue;
// Calculate the rotscaled Y coordinate relative to the object
int rotscaleY = ((params[2] * (j - width2 / 2) + params[3] * (spriteY - height2 / 2)) >> 8) + height / 2;
if (rotscaleY < 0 || rotscaleY >= height) continue;
// Get the palette index for the current pixel
uint8_t index = core->memory.read<uint8_t>(false, tileBase +
((rotscaleY / 8) * mapWidth + rotscaleY % 8) * 4 + (rotscaleX / 8) * 32 + (rotscaleX % 8) / 2);
index = (rotscaleX % 2 == 1) ? ((index & 0xF0) >> 4) : (index & 0x0F);
if (index && type == 2) // Object window
{
// Mark object window pixels with an extra bit, and don't draw anything
framebuffer[line * 256 + offset] |= BIT(24);
}
else
{
// Draw a pixel if the old one is transparent or lower priority
// Semi-transparent pixels are marked with an extra bit
if (index && (!(layers[4][offset] & BIT(15)) || prio < objPrio[offset]))
{
layers[4][offset] = ((type == 1) ? BIT(25) : 0) | BIT(15) | U8TO16(pal, index * 2);
objPrio[offset] = prio;
}
}
}
}
}
else if (object[0] & BIT(13)) // 8-bit
{
// Adjust the current tile to align with the current Y coordinate relative to the object
int mapWidth = (dispCnt & BIT(4)) ? width : 128;
if (object[1] & BIT(13)) // Vertical flip
tileBase += (7 - (spriteY % 8) + ((height - 1 - spriteY) / 8) * mapWidth) * 8;
else
tileBase += ((spriteY % 8) + (spriteY / 8) * mapWidth) * 8;
// Get the object's palette
uint8_t *pal;
if (dispCnt & BIT(31)) // Extended palette
{
// In extended palette mode, the object can select from multiple 256-color palettes
if (!extPalettes[4]) continue;
pal = &extPalettes[4][(object[2] & 0xF000) >> 3];
}
else // Standard palette
{
pal = &palette[0x200];
}
// Draw a line of the object
for (int j = 0; j < width; j++)
{
// Determine the horizontal pixel offset based on whether or not the sprite is horizontally flipped
int offset = (object[1] & BIT(12)) ? (x + width - j - 1) : (x + j);
if (offset < 0 || offset >= 256) continue;
// Get the palette index for the current pixel
uint8_t index = core->memory.read<uint8_t>(false, tileBase + (j / 8) * 64 + j % 8);
if (index && type == 2) // Object window
{