all repos — mgba @ 2155a3017296fbe9de76a6db4ca69515ef113506

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, unsigned* stride, const void** pixels);
 25static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, 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	for (i = 0; i < 128; ++i) {
459		struct GBAObj obj;
460		LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
461		LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
462		LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
463		if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
464			int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(obj.a) * 4 + GBAObjAttributesBGetSize(obj.b)][1];
465			if (GBAObjAttributesAIsTransformed(obj.a)) {
466				height <<= GBAObjAttributesAGetDoubleSize(obj.a);
467			}
468			if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
469				renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
470				renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
471				renderer->sprites[oamMax].obj = obj;
472				++oamMax;
473			}
474		}
475	}
476	renderer->oamMax = oamMax;
477	renderer->oamDirty = 0;
478}
479
480static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
481	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
482
483	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
484	if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
485		int x;
486		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
487			row[x] = GBA_COLOR_WHITE;
488		}
489		return;
490	}
491
492	int x;
493	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
494		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
495		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
496		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
497		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
498	}
499
500	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
501	softwareRenderer->nWindows = 1;
502	if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
503		softwareRenderer->windows[0].control = softwareRenderer->winout;
504		if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt)) {
505			_breakWindow(softwareRenderer, &softwareRenderer->winN[1], y);
506		}
507		if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt)) {
508			_breakWindow(softwareRenderer, &softwareRenderer->winN[0], y);
509		}
510	} else {
511		softwareRenderer->windows[0].control.packed = 0xFF;
512	}
513
514	GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
515
516	int w;
517	x = 0;
518	for (w = 0; w < softwareRenderer->nWindows; ++w) {
519		// TOOD: handle objwin on backdrop
520		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
521		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
522			backdrop |= softwareRenderer->normalPalette[0];
523		} else {
524			backdrop |= softwareRenderer->variantPalette[0];
525		}
526		int end = softwareRenderer->windows[w].endX;
527		for (; x < end - 3; x += 4) {
528			softwareRenderer->row[x] = backdrop;
529			softwareRenderer->row[x + 1] = backdrop;
530			softwareRenderer->row[x + 2] = backdrop;
531			softwareRenderer->row[x + 3] = backdrop;
532		}
533		for (; x < end; ++x) {
534			softwareRenderer->row[x] = backdrop;
535		}
536	}
537
538	_drawScanline(softwareRenderer, y);
539
540	if (softwareRenderer->target2Bd) {
541		x = 0;
542		for (w = 0; w < softwareRenderer->nWindows; ++w) {
543			uint32_t backdrop = 0;
544			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
545				backdrop |= softwareRenderer->normalPalette[0];
546			} else {
547				backdrop |= softwareRenderer->variantPalette[0];
548			}
549			int end = softwareRenderer->windows[w].endX;
550			for (; x < end; ++x) {
551				uint32_t color = softwareRenderer->row[x];
552				if (color & FLAG_TARGET_1) {
553					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
554				}
555			}
556		}
557	}
558	if (softwareRenderer->target1Obj && (softwareRenderer->blendEffect == BLEND_DARKEN || softwareRenderer->blendEffect == BLEND_BRIGHTEN)) {
559		x = 0;
560		for (w = 0; w < softwareRenderer->nWindows; ++w) {
561			if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
562				continue;
563			}
564			int end = softwareRenderer->windows[w].endX;
565			if (softwareRenderer->blendEffect == BLEND_DARKEN) {
566				for (; x < end; ++x) {
567					uint32_t color = softwareRenderer->row[x];
568					if ((color & 0xFF000000) == FLAG_REBLEND) {
569						softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
570					}
571				}
572			} else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
573				for (; x < end; ++x) {
574					uint32_t color = softwareRenderer->row[x];
575					if ((color & 0xFF000000) == FLAG_REBLEND) {
576						softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
577					}
578				}
579			}
580		}
581	}
582
583#ifdef COLOR_16_BIT
584#if defined(__ARM_NEON) && !defined(__APPLE__)
585	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
586#else
587	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
588		row[x] = softwareRenderer->row[x];
589	}
590#endif
591#else
592	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
593#endif
594}
595
596static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
597	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
598
599	if (softwareRenderer->temporaryBuffer) {
600		mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
601		softwareRenderer->temporaryBuffer = 0;
602	}
603	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
604	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
605	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
606	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
607}
608
609static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels) {
610	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
611
612#ifdef COLOR_16_BIT
613	*stride = VIDEO_HORIZONTAL_PIXELS;
614	if (!softwareRenderer->temporaryBuffer) {
615		softwareRenderer->temporaryBuffer = anonymousMemoryMap(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
616	}
617	*pixels = softwareRenderer->temporaryBuffer;
618	unsigned y, x;
619	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
620		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
621			color_t inColor = softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + x];
622			uint32_t outColor;
623#ifdef COLOR_5_6_5
624			outColor = (inColor & 0x1F) << 19;
625			outColor |= (inColor & 0x7C0) << 5;
626			outColor |= (inColor & 0xF800) >> 8;
627#else
628			outColor = (inColor & 0x1F) << 3;
629			outColor |= (inColor & 0x3E0) << 6;
630			outColor |= (inColor & 0x7C00) << 9;
631#endif
632			softwareRenderer->temporaryBuffer[VIDEO_HORIZONTAL_PIXELS * y + x] = outColor;
633		}
634	}
635#else
636	*stride = softwareRenderer->outputBufferStride;
637	*pixels = softwareRenderer->outputBuffer;
638#endif
639}
640
641static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels) {
642	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
643
644	uint32_t* colorPixels = pixels;
645	unsigned i;
646	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
647#ifdef COLOR_16_BIT
648		unsigned x;
649		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
650			uint32_t inColor = colorPixels[stride * i + x];
651			color_t outColor;
652#ifdef COLOR_5_6_5
653			outColor = (inColor >> 19) & 0x1F;
654			outColor |= (inColor >> 5) & 0x7C0;
655			outColor |= (inColor << 8) & 0xF800;
656#else
657			outColor = (inColor >> 3) & 0x1F;
658			outColor |= (inColor >> 6) & 0x3E0;
659			outColor |= (inColor >> 9) & 0x7C00;
660#endif
661			softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i + x] = outColor;
662		}
663#else
664		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
665#endif
666	}
667}
668
669static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
670	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt) && !renderer->d.disableBG[0];
671	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt) && !renderer->d.disableBG[1];
672	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt) && !renderer->d.disableBG[2];
673	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt) && !renderer->d.disableBG[3];
674}
675
676static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
677	UNUSED(renderer);
678	bg->priority = GBARegisterBGCNTGetPriority(value);
679	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
680	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
681	bg->multipalette = GBARegisterBGCNTGet256Color(value);
682	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
683	bg->overflow = GBARegisterBGCNTGetOverflow(value);
684	bg->size = GBARegisterBGCNTGetSize(value);
685}
686
687static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
688	bg->dx = value;
689}
690
691static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
692	bg->dmx = value;
693}
694
695static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
696	bg->dy = value;
697}
698
699static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
700	bg->dmy = value;
701}
702
703static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
704	bg->refx = (bg->refx & 0xFFFF0000) | value;
705	bg->sx = bg->refx;
706}
707
708static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
709	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
710	bg->refx <<= 4;
711	bg->refx >>= 4;
712	bg->sx = bg->refx;
713}
714
715static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
716	bg->refy = (bg->refy & 0xFFFF0000) | value;
717	bg->sy = bg->refy;
718}
719
720static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
721	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
722	bg->refy <<= 4;
723	bg->refy >>= 4;
724	bg->sy = bg->refy;
725}
726
727static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
728	enum BlendEffect oldEffect = renderer->blendEffect;
729
730	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
731	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
732	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
733	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
734	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
735	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
736	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
737	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
738
739	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
740	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
741	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
742	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
743	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
744
745	if (oldEffect != renderer->blendEffect) {
746		_updatePalettes(renderer);
747	}
748}
749
750#define TEST_LAYER_ENABLED(X) \
751	(renderer->bg[X].enabled && \
752	(GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
753	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
754	renderer->bg[X].priority == priority)
755
756static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
757	int w;
758	renderer->end = 0;
759	int spriteLayers = 0;
760	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
761		if (renderer->oamDirty) {
762			_cleanOAM(renderer);
763		}
764		renderer->spriteCyclesRemaining = GBARegisterDISPCNTIsHblankIntervalFree(renderer->dispcnt) ? OBJ_HBLANK_FREE_LENGTH : OBJ_LENGTH;
765		int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
766		int mosaicY = y - (y % mosaicV);
767		for (w = 0; w < renderer->nWindows; ++w) {
768			renderer->start = renderer->end;
769			renderer->end = renderer->windows[w].endX;
770			renderer->currentWindow = renderer->windows[w].control;
771			if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
772				continue;
773			}
774			int i;
775			int drawn;
776			for (i = 0; i < renderer->oamMax; ++i) {
777				int localY = y;
778				if (renderer->spriteCyclesRemaining <= 0) {
779					break;
780				}
781				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
782				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
783					localY = mosaicY;
784				}
785				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
786					continue;
787				}
788				drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
789				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
790			}
791		}
792	}
793
794	unsigned priority;
795	for (priority = 0; priority < 4; ++priority) {
796		renderer->end = 0;
797		for (w = 0; w < renderer->nWindows; ++w) {
798			renderer->start = renderer->end;
799			renderer->end = renderer->windows[w].endX;
800			renderer->currentWindow = renderer->windows[w].control;
801			if (spriteLayers & (1 << priority)) {
802				GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
803			}
804			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
805				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
806			}
807			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
808				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
809			}
810			if (TEST_LAYER_ENABLED(2)) {
811				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
812				case 0:
813					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
814					break;
815				case 1:
816				case 2:
817					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
818					break;
819				case 3:
820					GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
821					break;
822				case 4:
823					GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
824					break;
825				case 5:
826					GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
827					break;
828				}
829			}
830			if (TEST_LAYER_ENABLED(3)) {
831				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
832				case 0:
833					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
834					break;
835				case 2:
836					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
837					break;
838				}
839			}
840		}
841	}
842	renderer->bg[2].sx += renderer->bg[2].dmx;
843	renderer->bg[2].sy += renderer->bg[2].dmy;
844	renderer->bg[3].sx += renderer->bg[3].dmx;
845	renderer->bg[3].sy += renderer->bg[3].dmy;
846}
847
848static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
849	int i;
850	if (renderer->blendEffect == BLEND_BRIGHTEN) {
851		for (i = 0; i < 512; ++i) {
852			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
853		}
854	} else if (renderer->blendEffect == BLEND_DARKEN) {
855		for (i = 0; i < 512; ++i) {
856			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
857		}
858	} else {
859		for (i = 0; i < 512; ++i) {
860			renderer->variantPalette[i] = renderer->normalPalette[i];
861		}
862	}
863}