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 uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? ~0x1C : ~0x1F;
292 int p;
293 if (renderer->model >= GB_MODEL_CGB) {
294 p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
295 if (GBObjAttributesIsBank(obj->attr)) {
296 data += GB_SIZE_VRAM_BANK0;
297 }
298 } else {
299 p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
300 }
301 int bottomX;
302 int x;
303 for (x = startX; x < endX; ++x) {
304 if (GBObjAttributesIsXFlip(obj->attr)) {
305 bottomX = (x - obj->x) & 7;
306 } else {
307 bottomX = 7 - ((x - obj->x) & 7);
308 }
309 int objTile = obj->tile + tileOffset;
310 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
311 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
312 tileDataUpper >>= bottomX;
313 tileDataLower >>= bottomX;
314 color_t current = renderer->row[x];
315 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask)) {
316 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
317 }
318 }
319}
320
321static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
322 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
323 // TODO: Share with GBAVideoSoftwareRendererGetPixels
324#ifdef COLOR_16_BIT
325 *stride = GB_VIDEO_HORIZONTAL_PIXELS;
326 if (!softwareRenderer->temporaryBuffer) {
327 softwareRenderer->temporaryBuffer = anonymousMemoryMap(GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
328 }
329 *pixels = softwareRenderer->temporaryBuffer;
330 unsigned y, x;
331 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
332 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
333 color_t inColor = softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + x];
334 uint32_t outColor;
335#ifdef COLOR_5_6_5
336 outColor = (inColor & 0x1F) << 19;
337 outColor |= (inColor & 0x7C0) << 5;
338 outColor |= (inColor & 0xF800) >> 8;
339#else
340 outColor = (inColor & 0x1F) << 3;
341 outColor |= (inColor & 0x3E0) << 6;
342 outColor |= (inColor & 0x7C00) << 9;
343#endif
344 softwareRenderer->temporaryBuffer[GB_VIDEO_HORIZONTAL_PIXELS * y + x] = outColor;
345 }
346 }
347#else
348 *stride = softwareRenderer->outputBufferStride;
349 *pixels = softwareRenderer->outputBuffer;
350#endif
351}