all repos — mgba @ 24f3b5f11d3d6f291746eb585bb19de6cb51d5b5

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