Debian Codegraph @

gba/ppu/background.cpp

raw
1 void PPU::render_backgrounds() {
2 switch(regs.control.bgmode) {
3 case 0:
4 render_background_linear(regs.bg[3]);
5 render_background_linear(regs.bg[2]);
6 render_background_linear(regs.bg[1]);
7 render_background_linear(regs.bg[0]);
8 break;
9 case 1:
10 render_background_affine(regs.bg[2]);
11 render_background_linear(regs.bg[1]);
12 render_background_linear(regs.bg[0]);
13 break;
14 case 2:
15 render_background_affine(regs.bg[3]);
16 render_background_affine(regs.bg[2]);
17 break;
18 case 3: case 4: case 5:
19 render_background_bitmap(regs.bg[2]);
20 break;
21 }
22 }
23
24 void PPU::render_background_linear(Registers::Background& bg) {
25 if(regs.control.enable[bg.id] == false) return;
26 auto& output = layer[bg.id];
27
28 if(bg.control.mosaic == false || (regs.vcounter % (1 + regs.mosaic.bgvsize)) == 0) {
29 bg.vmosaic = regs.vcounter;
30 }
31
32 uint9 voffset = bg.vmosaic + bg.voffset;
33 uint9 hoffset = bg.hoffset;
34
35 unsigned basemap = bg.control.screenbaseblock << 11;
36 unsigned basechr = bg.control.characterbaseblock << 14;
37 unsigned px = hoffset & 7, py = voffset & 7;
38
39 Tile tile;
40 uint8 data[8];
41
42 for(unsigned x = 0; x < 240; x++) {
43 if(x == 0 || px & 8) {
44 px &= 7;
45
46 unsigned tx = hoffset / 8, ty = voffset / 8;
47 unsigned offset = (ty & 31) * 32 + (tx & 31);
48 if(bg.control.screensize & 1) if(tx & 32) offset += 32 * 32;
49 if(bg.control.screensize & 2) if(ty & 32) offset += 32 * 32 * (1 + (bg.control.screensize & 1));
50 offset = basemap + offset * 2;
51 uint16 mapdata = vram_read(offset, Half);
52
53 tile.character = mapdata >> 0;
54 tile.hflip = mapdata >> 10;
55 tile.vflip = mapdata >> 11;
56 tile.palette = mapdata >> 12;
57
58 if(bg.control.colormode == 0) {
59 offset = basechr + tile.character * 32 + (py ^ (tile.vflip ? 7 : 0)) * 4;
60 uint32 word = vram_read(offset, Word);
61 for(unsigned n = 0; n < 8; n++) data[n] = (word >> (n * 4)) & 15;
62 } else {
63 offset = basechr + tile.character * 64 + (py ^ (tile.vflip ? 7 : 0)) * 8;
64 uint32 wordlo = vram_read(offset + 0, Word);
65 uint32 wordhi = vram_read(offset + 4, Word);
66 for(unsigned n = 0; n < 4; n++) data[0 + n] = (wordlo >> (n * 8)) & 255;
67 for(unsigned n = 0; n < 4; n++) data[4 + n] = (wordhi >> (n * 8)) & 255;
68 }
69 }
70
71 hoffset++;
72 uint8 color = data[px++ ^ (tile.hflip ? 7 : 0)];
73
74 if(color) {
75 if(bg.control.colormode == 0) output[x].write(true, bg.control.priority, pram[tile.palette * 16 + color]);
76 if(bg.control.colormode == 1) output[x].write(true, bg.control.priority, pram[color]);
77 }
78 }
79 }
80
81 void PPU::render_background_affine(Registers::Background& bg) {
82 if(regs.control.enable[bg.id] == false) return;
83 auto& output = layer[bg.id];
84
85 unsigned basemap = bg.control.screenbaseblock << 11;
86 unsigned basechr = bg.control.characterbaseblock << 14;
87 unsigned screensize = 16 << bg.control.screensize;
88 unsigned screenwrap = (1 << (bg.control.affinewrap ? 7 + bg.control.screensize : 20)) - 1;
89
90 if(bg.control.mosaic == false || (regs.vcounter % (1 + regs.mosaic.bgvsize)) == 0) {
91 bg.hmosaic = bg.lx;
92 bg.vmosaic = bg.ly;
93 }
94
95 int28 fx = bg.hmosaic;
96 int28 fy = bg.vmosaic;
97
98 for(unsigned x = 0; x < 240; x++) {
99 unsigned cx = (fx >> 8) & screenwrap, tx = cx / 8, px = cx & 7;
100 unsigned cy = (fy >> 8) & screenwrap, ty = cy / 8, py = cy & 7;
101
102 if(tx < screensize && ty < screensize) {
103 uint8 character = vram[basemap + ty * screensize + tx];
104 uint8 color = vram[basechr + (character * 64) + py * 8 + px];
105 if(color) output[x].write(true, bg.control.priority, pram[color]);
106 }
107
108 fx += bg.pa;
109 fy += bg.pc;
110 }
111
112 bg.lx += bg.pb;
113 bg.ly += bg.pd;
114 }
115
116 void PPU::render_background_bitmap(Registers::Background& bg) {
117 if(regs.control.enable[bg.id] == false) return;
118 auto& output = layer[bg.id];
119
120 uint1 depth = regs.control.bgmode != 4; //0 = 8-bit (Mode 4), 1 = 15-bit (Mode 3, Mode 5)
121 unsigned basemap = regs.control.bgmode == 3 ? 0 : 0xa000 * regs.control.frame;
122
123 unsigned width = regs.control.bgmode == 5 ? 160 : 240;
124 unsigned height = regs.control.bgmode == 5 ? 128 : 160;
125 unsigned size = depth ? Half : Byte;
126
127 if(bg.control.mosaic == false || (regs.vcounter % (1 + regs.mosaic.bgvsize)) == 0) {
128 bg.hmosaic = bg.lx;
129 bg.vmosaic = bg.ly;
130 }
131
132 int28 fx = bg.hmosaic;
133 int28 fy = bg.vmosaic;
134
135 for(unsigned x = 0; x < 240; x++) {
136 unsigned px = fx >> 8;
137 unsigned py = fy >> 8;
138
139 if(px < width && py < height) {
140 unsigned offset = py * width + px;
141 unsigned color = vram_read(basemap + (offset << depth), size);
142
143 if(depth || color) { //8bpp color 0 is transparent; 15bpp color is always opaque
144 if(depth == 0) color = pram[color];
145 if(depth == 1) color = color & 0x7fff;
146 output[x].write(true, bg.control.priority, color);
147 }
148 }
149
150 fx += bg.pa;
151 fy += bg.pc;
152 }
153
154 bg.lx += bg.pb;
155 bg.ly += bg.pd;
156 }