all repos — mgba @ ec25074cec84c82e4105a6b84e8779b1cf3a937e

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