all repos — mgba @ cd2443356b3e6a6b19d15511de3531354591873e

mGBA Game Boy Advance Emulator

src/gba/renderers/video-software.c (view raw)

  1/* Copyright (c) 2013-2015 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-private.h"
  7
  8#include "gba/gba.h"
  9#include "gba/io.h"
 10
 11#include "util/arm-algo.h"
 12#include "util/memory.h"
 13
 14static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
 15static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
 16static void GBAVideoSoftwareRendererReset(struct GBAVideoRenderer* renderer);
 17static void GBAVideoSoftwareRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
 18static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
 19static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 20static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 21static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
 22static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
 23static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels);
 24static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels);
 25
 26static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer);
 27static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value);
 28static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 29static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 30static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 31static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 32static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 33static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 34static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 35static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 36static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value);
 37
 38static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer);
 39static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y);
 40
 41static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer);
 42
 43static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y);
 44static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win);
 45
 46void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
 47	renderer->d.init = GBAVideoSoftwareRendererInit;
 48	renderer->d.reset = GBAVideoSoftwareRendererReset;
 49	renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
 50	renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
 51	renderer->d.writeVRAM = GBAVideoSoftwareRendererWriteVRAM;
 52	renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;
 53	renderer->d.writePalette = GBAVideoSoftwareRendererWritePalette;
 54	renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
 55	renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
 56	renderer->d.getPixels = GBAVideoSoftwareRendererGetPixels;
 57	renderer->d.putPixels = GBAVideoSoftwareRendererPutPixels;
 58
 59	renderer->d.disableBG[0] = false;
 60	renderer->d.disableBG[1] = false;
 61	renderer->d.disableBG[2] = false;
 62	renderer->d.disableBG[3] = false;
 63	renderer->d.disableOBJ = false;
 64
 65	renderer->temporaryBuffer = 0;
 66}
 67
 68static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
 69	GBAVideoSoftwareRendererReset(renderer);
 70
 71	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 72
 73	int y;
 74	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
 75		color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 76		int x;
 77		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 78			row[x] = GBA_COLOR_WHITE;
 79		}
 80	}
 81}
 82
 83static void GBAVideoSoftwareRendererReset(struct GBAVideoRenderer* renderer) {
 84	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 85	int i;
 86
 87	softwareRenderer->dispcnt = 0x0080;
 88
 89	softwareRenderer->target1Obj = 0;
 90	softwareRenderer->target1Bd = 0;
 91	softwareRenderer->target2Obj = 0;
 92	softwareRenderer->target2Bd = 0;
 93	softwareRenderer->blendEffect = BLEND_NONE;
 94	memset(softwareRenderer->normalPalette, 0, sizeof(softwareRenderer->normalPalette));
 95	memset(softwareRenderer->variantPalette, 0, sizeof(softwareRenderer->variantPalette));
 96
 97	softwareRenderer->blda = 0;
 98	softwareRenderer->bldb = 0;
 99	softwareRenderer->bldy = 0;
100
101	softwareRenderer->winN[0] = (struct WindowN) { .control = { .priority = 0 } };
102	softwareRenderer->winN[1] = (struct WindowN) { .control = { .priority = 1 } };
103	softwareRenderer->objwin = (struct WindowControl) { .priority = 2 };
104	softwareRenderer->winout = (struct WindowControl) { .priority = 3 };
105	softwareRenderer->oamMax = 0;
106
107	softwareRenderer->mosaic = 0;
108
109	for (i = 0; i < 4; ++i) {
110		struct GBAVideoSoftwareBackground* bg = &softwareRenderer->bg[i];
111		bg->index = i;
112		bg->enabled = 0;
113		bg->priority = 0;
114		bg->charBase = 0;
115		bg->mosaic = 0;
116		bg->multipalette = 0;
117		bg->screenBase = 0;
118		bg->overflow = 0;
119		bg->size = 0;
120		bg->target1 = 0;
121		bg->target2 = 0;
122		bg->x = 0;
123		bg->y = 0;
124		bg->refx = 0;
125		bg->refy = 0;
126		bg->dx = 256;
127		bg->dmx = 0;
128		bg->dy = 0;
129		bg->dmy = 256;
130		bg->sx = 0;
131		bg->sy = 0;
132	}
133}
134
135static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
136	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
137	UNUSED(softwareRenderer);
138}
139
140static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
141	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
142	switch (address) {
143	case REG_DISPCNT:
144		softwareRenderer->dispcnt = value;
145		GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
146		break;
147	case REG_BG0CNT:
148		value &= 0xFFCF;
149		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[0], value);
150		break;
151	case REG_BG1CNT:
152		value &= 0xFFCF;
153		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[1], value);
154		break;
155	case REG_BG2CNT:
156		value &= 0xFFCF;
157		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[2], value);
158		break;
159	case REG_BG3CNT:
160		value &= 0xFFCF;
161		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[3], value);
162		break;
163	case REG_BG0HOFS:
164		value &= 0x01FF;
165		softwareRenderer->bg[0].x = value;
166		break;
167	case REG_BG0VOFS:
168		value &= 0x01FF;
169		softwareRenderer->bg[0].y = value;
170		break;
171	case REG_BG1HOFS:
172		value &= 0x01FF;
173		softwareRenderer->bg[1].x = value;
174		break;
175	case REG_BG1VOFS:
176		value &= 0x01FF;
177		softwareRenderer->bg[1].y = value;
178		break;
179	case REG_BG2HOFS:
180		value &= 0x01FF;
181		softwareRenderer->bg[2].x = value;
182		break;
183	case REG_BG2VOFS:
184		value &= 0x01FF;
185		softwareRenderer->bg[2].y = value;
186		break;
187	case REG_BG3HOFS:
188		value &= 0x01FF;
189		softwareRenderer->bg[3].x = value;
190		break;
191	case REG_BG3VOFS:
192		value &= 0x01FF;
193		softwareRenderer->bg[3].y = value;
194		break;
195	case REG_BG2PA:
196		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[2], value);
197		break;
198	case REG_BG2PB:
199		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[2], value);
200		break;
201	case REG_BG2PC:
202		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[2], value);
203		break;
204	case REG_BG2PD:
205		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[2], value);
206		break;
207	case REG_BG2X_LO:
208		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[2], value);
209		break;
210	case REG_BG2X_HI:
211		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[2], value);
212		break;
213	case REG_BG2Y_LO:
214		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[2], value);
215		break;
216	case REG_BG2Y_HI:
217		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[2], value);
218		break;
219	case REG_BG3PA:
220		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[3], value);
221		break;
222	case REG_BG3PB:
223		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[3], value);
224		break;
225	case REG_BG3PC:
226		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[3], value);
227		break;
228	case REG_BG3PD:
229		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[3], value);
230		break;
231	case REG_BG3X_LO:
232		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[3], value);
233		break;
234	case REG_BG3X_HI:
235		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[3], value);
236		break;
237	case REG_BG3Y_LO:
238		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[3], value);
239		break;
240	case REG_BG3Y_HI:
241		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[3], value);
242		break;
243	case REG_BLDCNT:
244		GBAVideoSoftwareRendererWriteBLDCNT(softwareRenderer, value);
245		break;
246	case REG_BLDALPHA:
247		softwareRenderer->blda = value & 0x1F;
248		if (softwareRenderer->blda > 0x10) {
249			softwareRenderer->blda = 0x10;
250		}
251		softwareRenderer->bldb = (value >> 8) & 0x1F;
252		if (softwareRenderer->bldb > 0x10) {
253			softwareRenderer->bldb = 0x10;
254		}
255		break;
256	case REG_BLDY:
257		softwareRenderer->bldy = value & 0x1F;
258		if (softwareRenderer->bldy > 0x10) {
259			softwareRenderer->bldy = 0x10;
260		}
261		_updatePalettes(softwareRenderer);
262		break;
263	case REG_WIN0H:
264		softwareRenderer->winN[0].h.end = value;
265		softwareRenderer->winN[0].h.start = value >> 8;
266		if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end) {
267			softwareRenderer->winN[0].h.start = 0;
268		}
269		if (softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
270			softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
271			if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS) {
272				softwareRenderer->winN[0].h.start = VIDEO_HORIZONTAL_PIXELS;
273			}
274		}
275		break;
276	case REG_WIN1H:
277		softwareRenderer->winN[1].h.end = value;
278		softwareRenderer->winN[1].h.start = value >> 8;
279		if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end) {
280			softwareRenderer->winN[1].h.start = 0;
281		}
282		if (softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
283			softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
284			if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS) {
285				softwareRenderer->winN[1].h.start = VIDEO_HORIZONTAL_PIXELS;
286			}
287		}
288		break;
289	case REG_WIN0V:
290		softwareRenderer->winN[0].v.end = value;
291		softwareRenderer->winN[0].v.start = value >> 8;
292		if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end) {
293			softwareRenderer->winN[0].v.start = 0;
294		}
295		if (softwareRenderer->winN[0].v.end > VIDEO_VERTICAL_PIXELS) {
296			softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
297			if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS) {
298				softwareRenderer->winN[0].v.start = VIDEO_VERTICAL_PIXELS;
299			}
300		}
301		break;
302	case REG_WIN1V:
303		softwareRenderer->winN[1].v.end = value;
304		softwareRenderer->winN[1].v.start = value >> 8;
305		if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end) {
306			softwareRenderer->winN[1].v.start = 0;
307		}
308		if (softwareRenderer->winN[1].v.end > VIDEO_VERTICAL_PIXELS) {
309			softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
310			if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS) {
311				softwareRenderer->winN[1].v.start = VIDEO_VERTICAL_PIXELS;
312			}
313		}
314		break;
315	case REG_WININ:
316		softwareRenderer->winN[0].control.packed = value;
317		softwareRenderer->winN[1].control.packed = value >> 8;
318		break;
319	case REG_WINOUT:
320		softwareRenderer->winout.packed = value;
321		softwareRenderer->objwin.packed = value >> 8;
322		break;
323	case REG_MOSAIC:
324		softwareRenderer->mosaic = value;
325		break;
326	case REG_GREENSWP:
327		GBALog(0, GBA_LOG_STUB, "Stub video register write: 0x%03X", address);
328		break;
329	default:
330		GBALog(0, GBA_LOG_GAME_ERROR, "Invalid video register: 0x%03X", address);
331	}
332	return value;
333}
334
335static void GBAVideoSoftwareRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
336	UNUSED(renderer);
337	UNUSED(address);
338}
339
340static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
341	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
342	softwareRenderer->oamDirty = 1;
343	UNUSED(oam);
344}
345
346static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
347	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
348#ifdef COLOR_16_BIT
349#ifdef COLOR_5_6_5
350	unsigned color = 0;
351	color |= (value & 0x001F) << 11;
352	color |= (value & 0x03E0) << 1;
353	color |= (value & 0x7C00) >> 10;
354#else
355	unsigned color = value;
356#endif
357#else
358	unsigned color = 0;
359	color |= (value << 3) & 0xF8;
360	color |= (value << 6) & 0xF800;
361	color |= (value << 9) & 0xF80000;
362#endif
363	softwareRenderer->normalPalette[address >> 1] = color;
364	if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
365		softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
366	} else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
367		softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
368	}
369}
370
371static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y) {
372	if (win->v.end >= win->v.start) {
373		if (y >= win->v.end) {
374			return;
375		}
376		if (y < win->v.start) {
377			return;
378		}
379	} else if (y >= win->v.end && y < win->v.start) {
380		return;
381	}
382	if (win->h.end > VIDEO_HORIZONTAL_PIXELS || win->h.end < win->h.start) {
383		struct WindowN splits[2] = { *win, *win };
384		splits[0].h.start = 0;
385		splits[1].h.end = VIDEO_HORIZONTAL_PIXELS;
386		_breakWindowInner(softwareRenderer, &splits[0]);
387		_breakWindowInner(softwareRenderer, &splits[1]);
388	} else {
389		_breakWindowInner(softwareRenderer, win);
390	}
391}
392
393static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
394	int activeWindow;
395	int startX = 0;
396	if (win->h.end > 0) {
397		for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
398			if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
399				// Insert a window before the end of the active window
400				struct Window oldWindow = softwareRenderer->windows[activeWindow];
401				if (win->h.start > startX) {
402					// And after the start of the active window
403					int nextWindow = softwareRenderer->nWindows;
404					++softwareRenderer->nWindows;
405					for (; nextWindow > activeWindow; --nextWindow) {
406						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
407					}
408					softwareRenderer->windows[activeWindow].endX = win->h.start;
409					++activeWindow;
410				}
411				softwareRenderer->windows[activeWindow].control = win->control;
412				softwareRenderer->windows[activeWindow].endX = win->h.end;
413				if (win->h.end >= oldWindow.endX) {
414					// Trim off extra windows we've overwritten
415					for (++activeWindow; softwareRenderer->nWindows > activeWindow + 1 && win->h.end >= softwareRenderer->windows[activeWindow].endX; ++activeWindow) {
416						if (VIDEO_CHECKS && activeWindow >= MAX_WINDOW) {
417							GBALog(0, GBA_LOG_FATAL, "Out of bounds window write will occur");
418							return;
419						}
420						softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
421						--softwareRenderer->nWindows;
422					}
423				} else {
424					++activeWindow;
425					int nextWindow = softwareRenderer->nWindows;
426					++softwareRenderer->nWindows;
427					for (; nextWindow > activeWindow; --nextWindow) {
428						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
429					}
430					softwareRenderer->windows[activeWindow] = oldWindow;
431				}
432				break;
433			}
434			startX = softwareRenderer->windows[activeWindow].endX;
435		}
436	}
437#ifdef DEBUG
438	if (softwareRenderer->nWindows > MAX_WINDOW) {
439		GBALog(0, GBA_LOG_FATAL, "Out of bounds window write occurred!");
440	}
441#endif
442}
443
444static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
445	int i;
446	int oamMax = 0;
447	for (i = 0; i < 128; ++i) {
448		struct GBAObj obj;
449		LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
450		LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
451		LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
452		if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
453			int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(obj.a) * 4 + GBAObjAttributesBGetSize(obj.b)][1];
454			if (GBAObjAttributesAIsTransformed(obj.a)) {
455				height <<= GBAObjAttributesAGetDoubleSize(obj.a);
456			}
457			if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
458				renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
459				renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
460				renderer->sprites[oamMax].obj = obj;
461				++oamMax;
462			}
463		}
464	}
465	renderer->oamMax = oamMax;
466	renderer->oamDirty = 0;
467}
468
469static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
470	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
471
472	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
473	if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
474		int x;
475		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
476			row[x] = GBA_COLOR_WHITE;
477		}
478		return;
479	}
480
481	int x;
482	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
483		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
484		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
485		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
486		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
487	}
488
489	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
490	softwareRenderer->nWindows = 1;
491	if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
492		softwareRenderer->windows[0].control = softwareRenderer->winout;
493		if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt)) {
494			_breakWindow(softwareRenderer, &softwareRenderer->winN[1], y);
495		}
496		if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt)) {
497			_breakWindow(softwareRenderer, &softwareRenderer->winN[0], y);
498		}
499	} else {
500		softwareRenderer->windows[0].control.packed = 0xFF;
501	}
502
503	GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
504
505	int w;
506	x = 0;
507	for (w = 0; w < softwareRenderer->nWindows; ++w) {
508		// TOOD: handle objwin on backdrop
509		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
510		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
511			backdrop |= softwareRenderer->normalPalette[0];
512		} else {
513			backdrop |= softwareRenderer->variantPalette[0];
514		}
515		int end = softwareRenderer->windows[w].endX;
516		for (; x < end; ++x) {
517			softwareRenderer->row[x] = backdrop;
518		}
519	}
520
521	_drawScanline(softwareRenderer, y);
522
523	if (softwareRenderer->target2Bd) {
524		x = 0;
525		for (w = 0; w < softwareRenderer->nWindows; ++w) {
526			uint32_t backdrop = 0;
527			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
528				backdrop |= softwareRenderer->normalPalette[0];
529			} else {
530				backdrop |= softwareRenderer->variantPalette[0];
531			}
532			int end = softwareRenderer->windows[w].endX;
533			for (; x < end; ++x) {
534				uint32_t color = softwareRenderer->row[x];
535				if (color & FLAG_TARGET_1) {
536					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
537				}
538			}
539		}
540	}
541	if (softwareRenderer->target1Obj && (softwareRenderer->blendEffect == BLEND_DARKEN || softwareRenderer->blendEffect == BLEND_BRIGHTEN)) {
542		x = 0;
543		for (w = 0; w < softwareRenderer->nWindows; ++w) {
544			if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
545				continue;
546			}
547			int end = softwareRenderer->windows[w].endX;
548			if (softwareRenderer->blendEffect == BLEND_DARKEN) {
549				for (; x < end; ++x) {
550					uint32_t color = softwareRenderer->row[x];
551					if ((color & 0xFF000000) == FLAG_REBLEND) {
552						softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
553					}
554				}
555			} else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
556				for (; x < end; ++x) {
557					uint32_t color = softwareRenderer->row[x];
558					if ((color & 0xFF000000) == FLAG_REBLEND) {
559						softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
560					}
561				}
562			}
563		}
564	}
565
566#ifdef COLOR_16_BIT
567#if defined(__ARM_NEON) && !defined(__APPLE__)
568	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
569#else
570	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
571		row[x] = softwareRenderer->row[x];
572	}
573#endif
574#else
575	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
576#endif
577}
578
579static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
580	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
581
582	if (softwareRenderer->temporaryBuffer) {
583		mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
584		softwareRenderer->temporaryBuffer = 0;
585	}
586	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
587	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
588	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
589	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
590}
591
592static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels) {
593	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
594
595#ifdef COLOR_16_BIT
596	*stride = VIDEO_HORIZONTAL_PIXELS;
597	if (!softwareRenderer->temporaryBuffer) {
598		softwareRenderer->temporaryBuffer = anonymousMemoryMap(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
599	}
600	*pixels = softwareRenderer->temporaryBuffer;
601	unsigned y, x;
602	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
603		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
604			color_t inColor = softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + x];
605			uint32_t outColor;
606#ifdef COLOR_5_6_5
607			outColor = (inColor & 0x1F) << 19;
608			outColor |= (inColor & 0x7C0) << 5;
609			outColor |= (inColor & 0xF800) >> 8;
610#else
611			outColor = (inColor & 0x1F) << 3;
612			outColor |= (inColor & 0x3E0) << 6;
613			outColor |= (inColor & 0x7C00) << 9;
614#endif
615			softwareRenderer->temporaryBuffer[VIDEO_HORIZONTAL_PIXELS * y + x] = outColor;
616		}
617	}
618#else
619	*stride = softwareRenderer->outputBufferStride;
620	*pixels = softwareRenderer->outputBuffer;
621#endif
622}
623
624static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels) {
625	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
626
627	uint32_t* colorPixels = pixels;
628	unsigned i;
629	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
630#ifdef COLOR_16_BIT
631		unsigned x;
632		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
633			uint32_t inColor = colorPixels[stride * i + x];
634			color_t outColor;
635#ifdef COLOR_5_6_5
636			outColor = (inColor >> 19) & 0x1F;
637			outColor |= (inColor >> 5) & 0x7C0;
638			outColor |= (inColor << 8) & 0xF800;
639#else
640			outColor = (inColor >> 3) & 0x1F;
641			outColor |= (inColor >> 6) & 0x3E0;
642			outColor |= (inColor >> 9) & 0x7C00;
643#endif
644			softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i + x] = outColor;
645		}
646#else
647		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
648#endif
649	}
650}
651
652static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
653	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt) && !renderer->d.disableBG[0];
654	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt) && !renderer->d.disableBG[1];
655	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt) && !renderer->d.disableBG[2];
656	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt) && !renderer->d.disableBG[3];
657}
658
659static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
660	UNUSED(renderer);
661	bg->priority = GBARegisterBGCNTGetPriority(value);
662	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
663	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
664	bg->multipalette = GBARegisterBGCNTGet256Color(value);
665	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
666	bg->overflow = GBARegisterBGCNTGetOverflow(value);
667	bg->size = GBARegisterBGCNTGetSize(value);
668}
669
670static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
671	bg->dx = value;
672}
673
674static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
675	bg->dmx = value;
676}
677
678static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
679	bg->dy = value;
680}
681
682static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
683	bg->dmy = value;
684}
685
686static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
687	bg->refx = (bg->refx & 0xFFFF0000) | value;
688	bg->sx = bg->refx;
689}
690
691static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
692	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
693	bg->refx <<= 4;
694	bg->refx >>= 4;
695	bg->sx = bg->refx;
696}
697
698static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
699	bg->refy = (bg->refy & 0xFFFF0000) | value;
700	bg->sy = bg->refy;
701}
702
703static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
704	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
705	bg->refy <<= 4;
706	bg->refy >>= 4;
707	bg->sy = bg->refy;
708}
709
710static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
711	enum BlendEffect oldEffect = renderer->blendEffect;
712
713	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
714	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
715	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
716	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
717	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
718	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
719	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
720	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
721
722	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
723	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
724	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
725	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
726	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
727
728	renderer->anyTarget2 = value & 0x3F00;
729
730	if (oldEffect != renderer->blendEffect) {
731		_updatePalettes(renderer);
732	}
733}
734
735#define TEST_LAYER_ENABLED(X) \
736	(renderer->bg[X].enabled && \
737	(GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
738	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
739	renderer->bg[X].priority == priority)
740
741static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
742	int w;
743	renderer->end = 0;
744	int spriteLayers = 0;
745	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
746		if (renderer->oamDirty) {
747			_cleanOAM(renderer);
748		}
749		int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
750		int mosaicY = y - (y % mosaicV);
751		for (w = 0; w < renderer->nWindows; ++w) {
752			renderer->start = renderer->end;
753			renderer->end = renderer->windows[w].endX;
754			renderer->currentWindow = renderer->windows[w].control;
755			if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
756				continue;
757			}
758			int i;
759			int drawn;
760			for (i = 0; i < renderer->oamMax; ++i) {
761				int localY = y;
762				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
763				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
764					localY = mosaicY;
765				}
766				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
767					continue;
768				}
769				drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
770				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
771			}
772		}
773	}
774
775	int priority;
776	for (priority = 0; priority < 4; ++priority) {
777		renderer->end = 0;
778		for (w = 0; w < renderer->nWindows; ++w) {
779			renderer->start = renderer->end;
780			renderer->end = renderer->windows[w].endX;
781			renderer->currentWindow = renderer->windows[w].control;
782			if (spriteLayers & (1 << priority)) {
783				GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
784			}
785			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
786				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
787			}
788			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
789				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
790			}
791			if (TEST_LAYER_ENABLED(2)) {
792				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
793				case 0:
794					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
795					break;
796				case 1:
797				case 2:
798					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
799					break;
800				case 3:
801					GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
802					break;
803				case 4:
804					GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
805					break;
806				case 5:
807					GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
808					break;
809				}
810			}
811			if (TEST_LAYER_ENABLED(3)) {
812				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
813				case 0:
814					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
815					break;
816				case 2:
817					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
818					break;
819				}
820			}
821		}
822	}
823	renderer->bg[2].sx += renderer->bg[2].dmx;
824	renderer->bg[2].sy += renderer->bg[2].dmy;
825	renderer->bg[3].sx += renderer->bg[3].dmx;
826	renderer->bg[3].sy += renderer->bg[3].dmy;
827}
828
829static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
830	int i;
831	if (renderer->blendEffect == BLEND_BRIGHTEN) {
832		for (i = 0; i < 512; ++i) {
833			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
834		}
835	} else if (renderer->blendEffect == BLEND_DARKEN) {
836		for (i = 0; i < 512; ++i) {
837			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
838		}
839	} else {
840		for (i = 0; i < 512; ++i) {
841			renderer->variantPalette[i] = renderer->normalPalette[i];
842		}
843	}
844}