all repos — mgba @ 5425cff3e27be9ec09828cd7967bbe604eaa9e60

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