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