all repos — mgba @ 65534d5dcd6ac54d3ed48bb76b4f622b2efae2b1

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		uint32_t mask = 0xFF000000 & ~FLAG_OBJWIN;
625		uint32_t match = FLAG_REBLEND;
626		if (GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
627			mask |= FLAG_OBJWIN;
628			if (GBAWindowControlIsBlendEnable(softwareRenderer->objwin.packed)) {
629				match |= FLAG_OBJWIN;
630			}
631		}
632		for (w = 0; w < softwareRenderer->nWindows; ++w) {
633			if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
634				continue;
635			}
636			int end = softwareRenderer->windows[w].endX;
637			if (softwareRenderer->blendEffect == BLEND_DARKEN) {
638				for (; x < end; ++x) {
639					uint32_t color = softwareRenderer->row[x];
640					if ((color & mask) == match) {
641						softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
642					}
643				}
644			} else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
645				for (; x < end; ++x) {
646					uint32_t color = softwareRenderer->row[x];
647					if ((color & mask) == match) {
648						softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
649					}
650				}
651			}
652		}
653	}
654
655#ifdef COLOR_16_BIT
656	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
657		row[x] = softwareRenderer->row[x];
658		row[x + 1] = softwareRenderer->row[x + 1];
659		row[x + 2] = softwareRenderer->row[x + 2];
660		row[x + 3] = softwareRenderer->row[x + 3];
661	}
662#else
663	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
664#endif
665	CLEAN_SCANLINE(softwareRenderer, y);
666}
667
668static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
669	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
670
671	softwareRenderer->nextY = 0;
672	if (softwareRenderer->temporaryBuffer) {
673		mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
674		softwareRenderer->temporaryBuffer = 0;
675	}
676	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
677	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
678	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
679	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
680}
681
682static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
683	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
684	*stride = softwareRenderer->outputBufferStride;
685	*pixels = softwareRenderer->outputBuffer;
686}
687
688static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
689	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
690
691	const color_t* colorPixels = pixels;
692	unsigned i;
693	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
694		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
695	}
696}
697
698static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
699	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt) && !renderer->d.disableBG[0];
700	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt) && !renderer->d.disableBG[1];
701	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt) && !renderer->d.disableBG[2];
702	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt) && !renderer->d.disableBG[3];
703}
704
705static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
706	UNUSED(renderer);
707	bg->priority = GBARegisterBGCNTGetPriority(value);
708	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
709	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
710	bg->multipalette = GBARegisterBGCNTGet256Color(value);
711	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
712	bg->overflow = GBARegisterBGCNTGetOverflow(value);
713	bg->size = GBARegisterBGCNTGetSize(value);
714}
715
716static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
717	bg->refx = (bg->refx & 0xFFFF0000) | value;
718	bg->sx = bg->refx;
719}
720
721static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
722	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
723	bg->refx <<= 4;
724	bg->refx >>= 4;
725	bg->sx = bg->refx;
726}
727
728static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
729	bg->refy = (bg->refy & 0xFFFF0000) | value;
730	bg->sy = bg->refy;
731}
732
733static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
734	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
735	bg->refy <<= 4;
736	bg->refy >>= 4;
737	bg->sy = bg->refy;
738}
739
740static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
741	enum BlendEffect oldEffect = renderer->blendEffect;
742
743	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
744	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
745	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
746	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
747	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
748	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
749	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
750	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
751
752	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
753	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
754	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
755	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
756	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
757
758	if (oldEffect != renderer->blendEffect) {
759		renderer->blendDirty = true;
760	}
761}
762
763#define TEST_LAYER_ENABLED(X) \
764	(renderer->bg[X].enabled && \
765	(GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
766	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
767	renderer->bg[X].priority == priority)
768
769static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
770	int w;
771	renderer->end = 0;
772	int spriteLayers = 0;
773	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
774		if (renderer->oamDirty) {
775			_cleanOAM(renderer);
776		}
777		renderer->spriteCyclesRemaining = GBARegisterDISPCNTIsHblankIntervalFree(renderer->dispcnt) ? OBJ_HBLANK_FREE_LENGTH : OBJ_LENGTH;
778		int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
779		int mosaicY = y - (y % mosaicV);
780		for (w = 0; w < renderer->nWindows; ++w) {
781			renderer->start = renderer->end;
782			renderer->end = renderer->windows[w].endX;
783			renderer->currentWindow = renderer->windows[w].control;
784			if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
785				continue;
786			}
787			int i;
788			int drawn;
789
790			for (i = 0; i < renderer->oamMax; ++i) {
791				int localY = y;
792				if (renderer->spriteCyclesRemaining <= 0) {
793					break;
794				}
795				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
796				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
797					localY = mosaicY;
798				}
799				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
800					continue;
801				}
802				drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
803				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
804			}
805		}
806	}
807
808	unsigned priority;
809	for (priority = 0; priority < 4; ++priority) {
810		renderer->end = 0;
811		for (w = 0; w < renderer->nWindows; ++w) {
812			renderer->start = renderer->end;
813			renderer->end = renderer->windows[w].endX;
814			renderer->currentWindow = renderer->windows[w].control;
815			if (spriteLayers & (1 << priority)) {
816				GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
817			}
818			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
819				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
820			}
821			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
822				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
823			}
824			if (TEST_LAYER_ENABLED(2)) {
825				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
826				case 0:
827					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
828					break;
829				case 1:
830				case 2:
831					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
832					break;
833				case 3:
834					GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
835					break;
836				case 4:
837					GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
838					break;
839				case 5:
840					GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
841					break;
842				}
843			}
844			if (TEST_LAYER_ENABLED(3)) {
845				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
846				case 0:
847					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
848					break;
849				case 2:
850					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
851					break;
852				}
853			}
854		}
855	}
856	if (GBARegisterDISPCNTGetMode(renderer->dispcnt) != 0) {
857		renderer->bg[2].sx += renderer->bg[2].dmx;
858		renderer->bg[2].sy += renderer->bg[2].dmy;
859		renderer->bg[3].sx += renderer->bg[3].dmx;
860		renderer->bg[3].sy += renderer->bg[3].dmy;
861	}
862}
863
864static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
865	int i;
866	if (renderer->blendEffect == BLEND_BRIGHTEN) {
867		for (i = 0; i < 512; ++i) {
868			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
869		}
870	} else if (renderer->blendEffect == BLEND_DARKEN) {
871		for (i = 0; i < 512; ++i) {
872			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
873		}
874	} else {
875		for (i = 0; i < 512; ++i) {
876			renderer->variantPalette[i] = renderer->normalPalette[i];
877		}
878	}
879}