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]];
135 row[x + 1] = softwareRenderer->palette[softwareRenderer->row[x + 1]];
136 row[x + 2] = softwareRenderer->palette[softwareRenderer->row[x + 2]];
137 row[x + 3] = softwareRenderer->palette[softwareRenderer->row[x + 3]];
138 row[x + 4] = softwareRenderer->palette[softwareRenderer->row[x + 4]];
139 row[x + 5] = softwareRenderer->palette[softwareRenderer->row[x + 5]];
140 row[x + 6] = softwareRenderer->palette[softwareRenderer->row[x + 6]];
141 row[x + 7] = softwareRenderer->palette[softwareRenderer->row[x + 7]];
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 (GBObjAttributesIsBank(attrs)) {
192 localData += GB_SIZE_VRAM_BANK0;
193 }
194 if (GBObjAttributesIsYFlip(attrs)) {
195 localY = 7 - bottomY;
196 }
197 if (GBObjAttributesIsXFlip(attrs)) {
198 bottomX = 7 - bottomX;
199 bottomX = 7 - bottomX;
200 }
201 }
202 uint8_t tileDataLower = data[(bgTile * 8 + localY) * 2];
203 uint8_t tileDataUpper = data[(bgTile * 8 + localY) * 2 + 1];
204 tileDataUpper >>= bottomX;
205 tileDataLower >>= bottomX;
206 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
207 }
208 startX = startX2;
209 }
210 for (x = startX; x < endX; x += 8) {
211 uint8_t* localData = data;
212 int localY = bottomY;
213 int topX = ((x + sx) >> 3) & 0x1F;
214 int bgTile;
215 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
216 bgTile = maps[topX + topY];
217 } else {
218 bgTile = ((int8_t*) maps)[topX + topY];
219 }
220 int p = 0;
221 if (renderer->model >= GB_MODEL_CGB) {
222 GBObjAttributes attrs = attr[topX + topY];
223 p = GBObjAttributesGetCGBPalette(attrs) * 4;
224 if (GBObjAttributesIsBank(attrs)) {
225 localData += GB_SIZE_VRAM_BANK0;
226 }
227 if (GBObjAttributesIsYFlip(attrs)) {
228 localY = 7 - bottomY;
229 }
230 if (GBObjAttributesIsXFlip(attrs)) {
231 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
232 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
233 renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
234 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
235 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
236 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
237 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
238 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
239 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
240 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
241 continue;
242 }
243 }
244 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
245 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
246 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
247 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
248 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
249 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
250 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
251 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
252 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
253 renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
254 }
255}
256
257static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
258 int ix = obj->x - 8;
259 if (endX < ix || startX >= ix + 8) {
260 return;
261 }
262 if (obj->x < endX) {
263 endX = obj->x;
264 }
265 if (obj->x - 8 > startX) {
266 startX = obj->x - 8;
267 }
268 if (startX < 0) {
269 startX = 0;
270 }
271 uint8_t* data = renderer->d.vram;
272 int tileOffset = 0;
273 int bottomY;
274 if (GBObjAttributesIsYFlip(obj->attr)) {
275 bottomY = 7 - ((y - obj->y - 16) & 7);
276 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
277 ++tileOffset;
278 }
279 } else {
280 bottomY = (y - obj->y - 16) & 7;
281 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
282 ++tileOffset;
283 }
284 }
285 uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? ~0x1C : ~0x3F;
286 int p;
287 if (renderer->model >= GB_MODEL_CGB) {
288 p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
289 if (GBObjAttributesIsBank(obj->attr)) {
290 data += GB_SIZE_VRAM_BANK0;
291 }
292 } else {
293 p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
294 }
295 int bottomX;
296 int x;
297 for (x = startX; x < endX; ++x) {
298 if (GBObjAttributesIsXFlip(obj->attr)) {
299 bottomX = (x - obj->x) & 7;
300 } else {
301 bottomX = 7 - ((x - obj->x) & 7);
302 }
303 int objTile = obj->tile + tileOffset;
304 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
305 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
306 tileDataUpper >>= bottomX;
307 tileDataLower >>= bottomX;
308 color_t current = renderer->row[x];
309 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask)) {
310 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
311 }
312 }
313}
314
315static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
316 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
317 // TODO: Share with GBAVideoSoftwareRendererGetPixels
318#ifdef COLOR_16_BIT
319 *stride = GB_VIDEO_HORIZONTAL_PIXELS;
320 if (!softwareRenderer->temporaryBuffer) {
321 softwareRenderer->temporaryBuffer = anonymousMemoryMap(GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
322 }
323 *pixels = softwareRenderer->temporaryBuffer;
324 unsigned y, x;
325 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
326 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
327 color_t inColor = softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + x];
328 uint32_t outColor;
329#ifdef COLOR_5_6_5
330 outColor = (inColor & 0x1F) << 19;
331 outColor |= (inColor & 0x7C0) << 5;
332 outColor |= (inColor & 0xF800) >> 8;
333#else
334 outColor = (inColor & 0x1F) << 3;
335 outColor |= (inColor & 0x3E0) << 6;
336 outColor |= (inColor & 0x7C00) << 9;
337#endif
338 softwareRenderer->temporaryBuffer[GB_VIDEO_HORIZONTAL_PIXELS * y + x] = outColor;
339 }
340 }
341#else
342 *stride = softwareRenderer->outputBufferStride;
343 *pixels = softwareRenderer->outputBuffer;
344#endif
345}