all repos — mgba @ defaeb0d80ebbdfbd3f0135cb36dc56dc35cdab5

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