all repos — mgba @ 323a6326cd9d9f90f08e4ebd2c5b2f84fcdbd533

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