src/gb/renderers/software.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "software.h"
7
8#include "gb/io.h"
9#include "util/memory.h"
10
11static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
12static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer);
13static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
14static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
15static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax);
16static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
17static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer);
18static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
19static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
20
21static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy);
22static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y);
23
24void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
25 renderer->d.init = GBVideoSoftwareRendererInit;
26 renderer->d.deinit = GBVideoSoftwareRendererDeinit;
27 renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
28 renderer->d.writePalette = GBVideoSoftwareRendererWritePalette,
29 renderer->d.drawRange = GBVideoSoftwareRendererDrawRange;
30 renderer->d.finishScanline = GBVideoSoftwareRendererFinishScanline;
31 renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
32 renderer->d.getPixels = GBVideoSoftwareRendererGetPixels;
33 renderer->d.putPixels = 0;
34
35 renderer->temporaryBuffer = 0;
36}
37
38static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
39 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
40 softwareRenderer->scy = 0;
41 softwareRenderer->scx = 0;
42 softwareRenderer->wy = 0;
43 softwareRenderer->currentWy = 0;
44 softwareRenderer->wx = 0;
45 softwareRenderer->model = model;
46
47 int y;
48 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
49 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
50 int x;
51 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
52 row[x] = softwareRenderer->palette[0];
53 }
54 }
55}
56
57static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
58 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
59 UNUSED(softwareRenderer);
60}
61
62static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
63 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
64 switch (address) {
65 case REG_LCDC:
66 softwareRenderer->lcdc = value;
67 break;
68 case REG_SCY:
69 softwareRenderer->scy = value;
70 break;
71 case REG_SCX:
72 softwareRenderer->scx = value;
73 break;
74 case REG_WY:
75 softwareRenderer->wy = value;
76 softwareRenderer->currentWy = value;
77 break;
78 case REG_WX:
79 softwareRenderer->wx = value;
80 break;
81 }
82 return value;
83}
84
85static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
86 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
87#ifdef COLOR_16_BIT
88#ifdef COLOR_5_6_5
89 color_t color = 0;
90 color |= (value & 0x001F) << 11;
91 color |= (value & 0x03E0) << 1;
92 color |= (value & 0x7C00) >> 10;
93#else
94 color_t color = value;
95#endif
96#else
97 color_t color = 0;
98 color |= (value << 3) & 0xF8;
99 color |= (value << 6) & 0xF800;
100 color |= (value << 9) & 0xF80000;
101#endif
102 softwareRenderer->palette[index] = color;
103}
104
105static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
106 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
107 uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
108 if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
109 maps += GB_SIZE_MAP;
110 }
111 if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) {
112 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, y, softwareRenderer->scx, softwareRenderer->scy);
113
114 if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
115 maps = &softwareRenderer->d.vram[GB_BASE_MAP];
116 if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
117 maps += GB_SIZE_MAP;
118 }
119 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, y, 7 - softwareRenderer->wx, (softwareRenderer->currentWy - y) - softwareRenderer->wy);
120 }
121 } else {
122 memset(&softwareRenderer->row[startX], 0, endX - startX);
123 }
124
125 if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
126 size_t i;
127 for (i = 0; i < oamMax; ++i) {
128 GBVideoSoftwareRendererDrawObj(softwareRenderer, obj[i], startX, endX, y);
129 }
130 }
131 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
132 int x;
133 for (x = startX; x < endX; x += 8) {
134 row[x] = softwareRenderer->palette[softwareRenderer->row[x] & 0x7F];
135 row[x + 1] = softwareRenderer->palette[softwareRenderer->row[x + 1] & 0x7F];
136 row[x + 2] = softwareRenderer->palette[softwareRenderer->row[x + 2] & 0x7F];
137 row[x + 3] = softwareRenderer->palette[softwareRenderer->row[x + 3] & 0x7F];
138 row[x + 4] = softwareRenderer->palette[softwareRenderer->row[x + 4] & 0x7F];
139 row[x + 5] = softwareRenderer->palette[softwareRenderer->row[x + 5] & 0x7F];
140 row[x + 6] = softwareRenderer->palette[softwareRenderer->row[x + 6] & 0x7F];
141 row[x + 7] = softwareRenderer->palette[softwareRenderer->row[x + 7] & 0x7F];
142 }
143}
144
145static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
146 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
147 if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) && GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
148 ++softwareRenderer->currentWy;
149 }
150}
151
152static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
153 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
154
155 if (softwareRenderer->temporaryBuffer) {
156 mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
157 softwareRenderer->temporaryBuffer = 0;
158 }
159 softwareRenderer->currentWy = softwareRenderer->wy;
160}
161
162static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy) {
163 uint8_t* data = renderer->d.vram;
164 uint8_t* attr = &maps[GB_SIZE_VRAM_BANK0];
165 if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
166 data += 0x1000;
167 }
168 int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
169 int bottomY = (y + sy) & 7;
170 if (startX < 0) {
171 startX = 0;
172 }
173 int x;
174 if ((startX + sx) & 7) {
175 int startX2 = startX + 8 - ((startX + sx) & 7);
176 for (x = startX; x < startX2; ++x) {
177 uint8_t* localData = data;
178 int localY = bottomY;
179 int topX = ((x + sx) >> 3) & 0x1F;
180 int bottomX = 7 - ((x + sx) & 7);
181 int bgTile;
182 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
183 bgTile = maps[topX + topY];
184 } else {
185 bgTile = ((int8_t*) maps)[topX + topY];
186 }
187 int p = 0;
188 if (renderer->model >= GB_MODEL_CGB) {
189 GBObjAttributes attrs = attr[topX + topY];
190 p = GBObjAttributesGetCGBPalette(attrs) * 4;
191 if (GBObjAttributesIsPriority(attrs)) {
192 p |= 0x80;
193 }
194 if (GBObjAttributesIsBank(attrs)) {
195 localData += GB_SIZE_VRAM_BANK0;
196 }
197 if (GBObjAttributesIsYFlip(attrs)) {
198 localY = 7 - bottomY;
199 }
200 if (GBObjAttributesIsXFlip(attrs)) {
201 bottomX = 7 - bottomX;
202 bottomX = 7 - bottomX;
203 }
204 }
205 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
206 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
207 tileDataUpper >>= bottomX;
208 tileDataLower >>= bottomX;
209 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
210 }
211 startX = startX2;
212 }
213 for (x = startX; x < endX; x += 8) {
214 uint8_t* localData = data;
215 int localY = bottomY;
216 int topX = ((x + sx) >> 3) & 0x1F;
217 int bgTile;
218 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
219 bgTile = maps[topX + topY];
220 } else {
221 bgTile = ((int8_t*) maps)[topX + topY];
222 }
223 int p = 0;
224 if (renderer->model >= GB_MODEL_CGB) {
225 GBObjAttributes attrs = attr[topX + topY];
226 p = GBObjAttributesGetCGBPalette(attrs) * 4;
227 if (GBObjAttributesIsPriority(attrs)) {
228 p |= 0x80;
229 }
230 if (GBObjAttributesIsBank(attrs)) {
231 localData += GB_SIZE_VRAM_BANK0;
232 }
233 if (GBObjAttributesIsYFlip(attrs)) {
234 localY = 7 - bottomY;
235 }
236 if (GBObjAttributesIsXFlip(attrs)) {
237 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
238 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
239 renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
240 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
241 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
242 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
243 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
244 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
245 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
246 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
247 continue;
248 }
249 }
250 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
251 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
252 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
253 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
254 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
255 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
256 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
257 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
258 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
259 renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
260 }
261}
262
263static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
264 int ix = obj->x - 8;
265 if (endX < ix || startX >= ix + 8) {
266 return;
267 }
268 if (obj->x < endX) {
269 endX = obj->x;
270 }
271 if (obj->x - 8 > startX) {
272 startX = obj->x - 8;
273 }
274 if (startX < 0) {
275 startX = 0;
276 }
277 uint8_t* data = renderer->d.vram;
278 int tileOffset = 0;
279 int bottomY;
280 if (GBObjAttributesIsYFlip(obj->attr)) {
281 bottomY = 7 - ((y - obj->y - 16) & 7);
282 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
283 ++tileOffset;
284 }
285 } else {
286 bottomY = (y - obj->y - 16) & 7;
287 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
288 ++tileOffset;
289 }
290 }
291 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && obj->tile & 1) {
292 --tileOffset;
293 }
294 uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? ~0x1C : ~0x1F;
295 int p;
296 if (renderer->model >= GB_MODEL_CGB) {
297 p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
298 if (GBObjAttributesIsBank(obj->attr)) {
299 data += GB_SIZE_VRAM_BANK0;
300 }
301 } else {
302 p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
303 }
304 int bottomX;
305 int x = startX;
306 if ((x - obj->x) & 7) {
307 for (; x < endX; ++x) {
308 if (GBObjAttributesIsXFlip(obj->attr)) {
309 bottomX = (x - obj->x) & 7;
310 } else {
311 bottomX = 7 - ((x - obj->x) & 7);
312 }
313 int objTile = obj->tile + tileOffset;
314 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
315 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
316 tileDataUpper >>= bottomX;
317 tileDataLower >>= bottomX;
318 color_t current = renderer->row[x];
319 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask)) {
320 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
321 }
322 }
323 } else if (GBObjAttributesIsXFlip(obj->attr)) {
324 int objTile = obj->tile + tileOffset;
325 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
326 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
327 color_t current;
328 current = renderer->row[x];
329 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask)) {
330 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
331 }
332 current = renderer->row[x + 1];
333 if (((tileDataUpper | tileDataLower) & 2) && !(current & mask)) {
334 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
335 }
336 current = renderer->row[x + 2];
337 if (((tileDataUpper | tileDataLower) & 4) && !(current & mask)) {
338 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
339 }
340 current = renderer->row[x + 3];
341 if (((tileDataUpper | tileDataLower) & 8) && !(current & mask)) {
342 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
343 }
344 current = renderer->row[x + 4];
345 if (((tileDataUpper | tileDataLower) & 16) && !(current & mask)) {
346 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
347 }
348 current = renderer->row[x + 5];
349 if (((tileDataUpper | tileDataLower) & 32) && !(current & mask)) {
350 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
351 }
352 current = renderer->row[x + 6];
353 if (((tileDataUpper | tileDataLower) & 64) && !(current & mask)) {
354 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
355 }
356 current = renderer->row[x + 7];
357 if (((tileDataUpper | tileDataLower) & 128) && !(current & mask)) {
358 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
359 }
360 } else {
361 int objTile = obj->tile + tileOffset;
362 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
363 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
364 color_t current;
365 current = renderer->row[x + 7];
366 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask)) {
367 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
368 }
369 current = renderer->row[x + 6];
370 if (((tileDataUpper | tileDataLower) & 2) && !(current & mask)) {
371 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
372 }
373 current = renderer->row[x + 5];
374 if (((tileDataUpper | tileDataLower) & 4) && !(current & mask)) {
375 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
376 }
377 current = renderer->row[x + 4];
378 if (((tileDataUpper | tileDataLower) & 8) && !(current & mask)) {
379 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
380 }
381 current = renderer->row[x + 3];
382 if (((tileDataUpper | tileDataLower) & 16) && !(current & mask)) {
383 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
384 }
385 current = renderer->row[x + 2];
386 if (((tileDataUpper | tileDataLower) & 32) && !(current & mask)) {
387 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
388 }
389 current = renderer->row[x + 1];
390 if (((tileDataUpper | tileDataLower) & 64) && !(current & mask)) {
391 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
392 }
393 current = renderer->row[x];
394 if (((tileDataUpper | tileDataLower) & 128) && !(current & mask)) {
395 renderer->row[x] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
396 }
397 }
398}
399
400static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
401 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
402 // TODO: Share with GBAVideoSoftwareRendererGetPixels
403#ifdef COLOR_16_BIT
404 *stride = GB_VIDEO_HORIZONTAL_PIXELS;
405 if (!softwareRenderer->temporaryBuffer) {
406 softwareRenderer->temporaryBuffer = anonymousMemoryMap(GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
407 }
408 *pixels = softwareRenderer->temporaryBuffer;
409 unsigned y, x;
410 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
411 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
412 color_t inColor = softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + x];
413 uint32_t outColor;
414#ifdef COLOR_5_6_5
415 outColor = (inColor & 0x1F) << 19;
416 outColor |= (inColor & 0x7C0) << 5;
417 outColor |= (inColor & 0xF800) >> 8;
418#else
419 outColor = (inColor & 0x1F) << 3;
420 outColor |= (inColor & 0x3E0) << 6;
421 outColor |= (inColor & 0x7C00) << 9;
422#endif
423 softwareRenderer->temporaryBuffer[GB_VIDEO_HORIZONTAL_PIXELS * y + x] = outColor;
424 }
425 }
426#else
427 *stride = softwareRenderer->outputBufferStride;
428 *pixels = softwareRenderer->outputBuffer;
429#endif
430}