all repos — mgba @ 3056655060b3be0eabd35742ea2b8e1cb18ccb09

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