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);
12static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer);
13static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer);
14static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_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
24#ifdef COLOR_16_BIT
25#ifdef COLOR_5_6_5
26static const color_t GB_PALETTE[4] = { 0xFFFF, 0x39C7, 0x18C3, 0x0000};
27#else
28static const color_t GB_PALETTE[4] = { 0x7FFF, 0x1DE7, 0x0C63, 0x0000};
29#endif
30#else
31static const color_t GB_PALETTE[4] = { 0xFFFFFF, 0x808080, 0x404040, 0x000000};
32#endif
33
34void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
35 renderer->d.init = GBVideoSoftwareRendererInit;
36 renderer->d.reset = GBVideoSoftwareRendererReset;
37 renderer->d.deinit = GBVideoSoftwareRendererDeinit;
38 renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
39 renderer->d.drawRange = GBVideoSoftwareRendererDrawRange;
40 renderer->d.finishScanline = GBVideoSoftwareRendererFinishScanline;
41 renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
42 renderer->d.getPixels = 0;
43 renderer->d.putPixels = 0;
44
45 renderer->temporaryBuffer = 0;
46}
47
48static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer) {
49 GBVideoSoftwareRendererReset(renderer);
50
51 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
52
53 int y;
54 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
55 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
56 int x;
57 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
58 row[x] = GB_PALETTE[0];
59 }
60 }
61}
62
63static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) {
64 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
65 softwareRenderer->scy = 0;
66 softwareRenderer->scx = 0;
67 softwareRenderer->wy = 0;
68 softwareRenderer->currentWy = 0;
69 softwareRenderer->wx = 0;
70}
71
72static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
73 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
74 UNUSED(softwareRenderer);
75}
76
77static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
78 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
79 switch (address) {
80 case REG_LCDC:
81 softwareRenderer->lcdc = value;
82 break;
83 case REG_BGP:
84 softwareRenderer->palette[0] = GB_PALETTE[value & 3];
85 softwareRenderer->palette[1] = GB_PALETTE[(value >> 2) & 3];
86 softwareRenderer->palette[2] = GB_PALETTE[(value >> 4) & 3];
87 softwareRenderer->palette[3] = GB_PALETTE[(value >> 6) & 3];
88 break;
89 case REG_OBP0:
90 softwareRenderer->palette[8 * 4 + 0] = GB_PALETTE[value & 3];
91 softwareRenderer->palette[8 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
92 softwareRenderer->palette[8 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
93 softwareRenderer->palette[8 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
94 break;
95 case REG_OBP1:
96 softwareRenderer->palette[9 * 4 + 0] = GB_PALETTE[value & 3];
97 softwareRenderer->palette[9 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
98 softwareRenderer->palette[9 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
99 softwareRenderer->palette[9 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
100 break;
101 case REG_SCY:
102 softwareRenderer->scy = value;
103 break;
104 case REG_SCX:
105 softwareRenderer->scx = value;
106 break;
107 case REG_WY:
108 softwareRenderer->wy = value;
109 break;
110 case REG_WX:
111 softwareRenderer->wx = value;
112 break;
113 }
114 return value;
115}
116
117static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
118 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
119 uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
120 if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
121 maps += GB_SIZE_MAP;
122 }
123 if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) {
124 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, y, softwareRenderer->scx, softwareRenderer->scy);
125
126 if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
127 maps = &softwareRenderer->d.vram[GB_BASE_MAP];
128 if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
129 maps += GB_SIZE_MAP;
130 }
131 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, y, 7 - softwareRenderer->wx, (softwareRenderer->currentWy - y) - softwareRenderer->wy);
132 }
133 } else {
134 int x;
135 for (x = startX; x < endX; ++x) {
136 softwareRenderer->row[x] = 0;
137 }
138 }
139
140 if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
141 size_t i;
142 for (i = 0; i < oamMax; ++i) {
143 GBVideoSoftwareRendererDrawObj(softwareRenderer, obj[i], startX, endX, y);
144 }
145 }
146
147 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
148 int x;
149 for (x = startX; x < endX; ++x) {
150 row[x] = softwareRenderer->palette[softwareRenderer->row[x]];
151 }
152}
153
154static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
155 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
156 if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) && GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
157 ++softwareRenderer->currentWy;
158 }
159}
160
161static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
162 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
163
164 if (softwareRenderer->temporaryBuffer) {
165 mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
166 softwareRenderer->temporaryBuffer = 0;
167 }
168 softwareRenderer->currentWy = softwareRenderer->wy;
169}
170
171static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy) {
172 uint8_t* data = renderer->d.vram;
173 if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
174 data += 0x1000;
175 }
176 int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
177 int bottomY = (y + sy) & 7;
178 if (startX < 0) {
179 startX = 0;
180 }
181 int x;
182 if ((startX + sx) & 7) {
183 int startX2 = startX + 8 - ((startX + sx) & 7);
184 for (x = startX; x < startX2; ++x) {
185 int topX = ((x + sx) >> 3) & 0x1F;
186 int bottomX = 7 - ((x + sx) & 7);
187 int bgTile;
188 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
189 bgTile = maps[topX + topY];
190 } else {
191 bgTile = ((int8_t*) maps)[topX + topY];
192 }
193 uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
194 uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
195 tileDataUpper >>= bottomX;
196 tileDataLower >>= bottomX;
197 renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
198 }
199 startX = startX2;
200 }
201 for (x = startX; x < endX; x += 8) {
202 int topX = ((x + sx) >> 3) & 0x1F;
203 int bgTile;
204 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
205 bgTile = maps[topX + topY];
206 } else {
207 bgTile = ((int8_t*) maps)[topX + topY];
208 }
209 uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
210 uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
211 renderer->row[x + 7] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
212 renderer->row[x + 6] = (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
213 renderer->row[x + 5] = ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
214 renderer->row[x + 4] = ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
215 renderer->row[x + 3] = ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
216 renderer->row[x + 2] = ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
217 renderer->row[x + 1] = ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
218 renderer->row[x + 0] = ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
219 }
220}
221
222static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
223 int ix = obj->x - 8;
224 if (endX < ix || startX >= ix + 8) {
225 return;
226 }
227 if (obj->x < endX) {
228 endX = obj->x;
229 }
230 if (obj->x - 8 > startX) {
231 startX = obj->x - 8;
232 }
233 if (startX < 0) {
234 startX = 0;
235 }
236 uint8_t* data = renderer->d.vram;
237 int tileOffset = 0;
238 int bottomY;
239 if (GBObjAttributesIsYFlip(obj->attr)) {
240 bottomY = 7 - ((y - obj->y - 16) & 7);
241 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
242 ++tileOffset;
243 }
244 } else {
245 bottomY = (y - obj->y - 16) & 7;
246 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
247 ++tileOffset;
248 }
249 }
250 uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20;
251 int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
252 int bottomX;
253 int x;
254 for (x = startX; x < endX; ++x) {
255 if (GBObjAttributesIsXFlip(obj->attr)) {
256 bottomX = (x - obj->x) & 7;
257 } else {
258 bottomX = 7 - ((x - obj->x) & 7);
259 }
260 int objTile = obj->tile + tileOffset;
261 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
262 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
263 tileDataUpper >>= bottomX;
264 tileDataLower >>= bottomX;
265 color_t current = renderer->row[x];
266 if (((tileDataUpper | tileDataLower) & 1) && current <= mask) {
267 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
268 }
269 }
270}