all repos — mgba @ 9ba250e9bb85b8ff61f5ebc06006161611d44bda

mGBA Game Boy Advance Emulator

src/gba/renderers/video-software.c (view raw)

   1#include "video-software.h"
   2
   3#include "gba.h"
   4#include "gba-io.h"
   5
   6static const int _objSizes[32] = {
   7	8, 8,
   8	16, 16,
   9	32, 32,
  10	64, 64,
  11	16, 8,
  12	32, 8,
  13	32, 16,
  14	64, 32,
  15	8, 16,
  16	8, 32,
  17	16, 32,
  18	32, 64,
  19	0, 0,
  20	0, 0,
  21	0, 0,
  22	0, 0
  23};
  24
  25static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
  26static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
  27static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
  28static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  29static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  30static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
  31static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
  32static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels);
  33static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels);
  34
  35static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer);
  36static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value);
  37static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  38static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  39static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  40static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  41static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  42static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  43static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  44static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  45static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value);
  46
  47static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y);
  48static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  49static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  50static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  51static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  52static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  53static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer);
  54static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y);
  55static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority);
  56
  57static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer);
  58static inline unsigned _brighten(unsigned color, int y);
  59static inline unsigned _darken(unsigned color, int y);
  60static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB);
  61
  62void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
  63	renderer->d.init = GBAVideoSoftwareRendererInit;
  64	renderer->d.reset = GBAVideoSoftwareRendererInit;
  65	renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
  66	renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
  67	renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;
  68	renderer->d.writePalette = GBAVideoSoftwareRendererWritePalette;
  69	renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
  70	renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
  71	renderer->d.getPixels = GBAVideoSoftwareRendererGetPixels;
  72	renderer->d.putPixels = GBAVideoSoftwareRendererPutPixels;
  73}
  74
  75static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
  76	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
  77	int i;
  78
  79	softwareRenderer->dispcnt = 0x0080;
  80
  81	softwareRenderer->target1Obj = 0;
  82	softwareRenderer->target1Bd = 0;
  83	softwareRenderer->target2Obj = 0;
  84	softwareRenderer->target2Bd = 0;
  85	softwareRenderer->blendEffect = BLEND_NONE;
  86	memset(softwareRenderer->normalPalette, 0, sizeof(softwareRenderer->normalPalette));
  87	memset(softwareRenderer->variantPalette, 0, sizeof(softwareRenderer->variantPalette));
  88
  89	softwareRenderer->blda = 0;
  90	softwareRenderer->bldb = 0;
  91	softwareRenderer->bldy = 0;
  92
  93	softwareRenderer->winN[0] = (struct WindowN) { .control = { .priority = 0 } };
  94	softwareRenderer->winN[1] = (struct WindowN) { .control = { .priority = 1 } };
  95	softwareRenderer->objwin = (struct WindowControl) { .priority = 2 };
  96	softwareRenderer->winout = (struct WindowControl) { .priority = 3 };
  97	softwareRenderer->oamMax = 0;
  98
  99	softwareRenderer->mosaic = 0;
 100
 101	for (i = 0; i < 4; ++i) {
 102		struct GBAVideoSoftwareBackground* bg = &softwareRenderer->bg[i];
 103		bg->index = i;
 104		bg->enabled = 0;
 105		bg->priority = 0;
 106		bg->charBase = 0;
 107		bg->mosaic = 0;
 108		bg->multipalette = 0;
 109		bg->screenBase = 0;
 110		bg->overflow = 0;
 111		bg->size = 0;
 112		bg->target1 = 0;
 113		bg->target2 = 0;
 114		bg->x = 0;
 115		bg->y = 0;
 116		bg->refx = 0;
 117		bg->refy = 0;
 118		bg->dx = 256;
 119		bg->dmx = 0;
 120		bg->dy = 0;
 121		bg->dmy = 256;
 122		bg->sx = 0;
 123		bg->sy = 0;
 124	}
 125}
 126
 127static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
 128	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 129	UNUSED(softwareRenderer);
 130}
 131
 132static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 133	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 134	switch (address) {
 135	case REG_DISPCNT:
 136		softwareRenderer->dispcnt = value;
 137		GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
 138		break;
 139	case REG_BG0CNT:
 140		value &= 0xFFCF;
 141		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[0], value);
 142		break;
 143	case REG_BG1CNT:
 144		value &= 0xFFCF;
 145		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[1], value);
 146		break;
 147	case REG_BG2CNT:
 148		value &= 0xFFCF;
 149		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[2], value);
 150		break;
 151	case REG_BG3CNT:
 152		value &= 0xFFCF;
 153		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[3], value);
 154		break;
 155	case REG_BG0HOFS:
 156		value &= 0x01FF;
 157		softwareRenderer->bg[0].x = value;
 158		break;
 159	case REG_BG0VOFS:
 160		value &= 0x01FF;
 161		softwareRenderer->bg[0].y = value;
 162		break;
 163	case REG_BG1HOFS:
 164		value &= 0x01FF;
 165		softwareRenderer->bg[1].x = value;
 166		break;
 167	case REG_BG1VOFS:
 168		value &= 0x01FF;
 169		softwareRenderer->bg[1].y = value;
 170		break;
 171	case REG_BG2HOFS:
 172		value &= 0x01FF;
 173		softwareRenderer->bg[2].x = value;
 174		break;
 175	case REG_BG2VOFS:
 176		value &= 0x01FF;
 177		softwareRenderer->bg[2].y = value;
 178		break;
 179	case REG_BG3HOFS:
 180		value &= 0x01FF;
 181		softwareRenderer->bg[3].x = value;
 182		break;
 183	case REG_BG3VOFS:
 184		value &= 0x01FF;
 185		softwareRenderer->bg[3].y = value;
 186		break;
 187	case REG_BG2PA:
 188		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[2], value);
 189		break;
 190	case REG_BG2PB:
 191		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[2], value);
 192		break;
 193	case REG_BG2PC:
 194		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[2], value);
 195		break;
 196	case REG_BG2PD:
 197		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[2], value);
 198		break;
 199	case REG_BG2X_LO:
 200		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[2], value);
 201		break;
 202	case REG_BG2X_HI:
 203		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[2], value);
 204		break;
 205	case REG_BG2Y_LO:
 206		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[2], value);
 207		break;
 208	case REG_BG2Y_HI:
 209		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[2], value);
 210		break;
 211	case REG_BG3PA:
 212		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[3], value);
 213		break;
 214	case REG_BG3PB:
 215		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[3], value);
 216		break;
 217	case REG_BG3PC:
 218		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[3], value);
 219		break;
 220	case REG_BG3PD:
 221		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[3], value);
 222		break;
 223	case REG_BG3X_LO:
 224		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[3], value);
 225		break;
 226	case REG_BG3X_HI:
 227		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[3], value);
 228		break;
 229	case REG_BG3Y_LO:
 230		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[3], value);
 231		break;
 232	case REG_BG3Y_HI:
 233		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[3], value);
 234		break;
 235	case REG_BLDCNT:
 236		GBAVideoSoftwareRendererWriteBLDCNT(softwareRenderer, value);
 237		break;
 238	case REG_BLDALPHA:
 239		softwareRenderer->blda = value & 0x1F;
 240		if (softwareRenderer->blda > 0x10) {
 241			softwareRenderer->blda = 0x10;
 242		}
 243		softwareRenderer->bldb = (value >> 8) & 0x1F;
 244		if (softwareRenderer->bldb > 0x10) {
 245			softwareRenderer->bldb = 0x10;
 246		}
 247		break;
 248	case REG_BLDY:
 249		softwareRenderer->bldy = value & 0x1F;
 250		if (softwareRenderer->bldy > 0x10) {
 251			softwareRenderer->bldy = 0x10;
 252		}
 253		_updatePalettes(softwareRenderer);
 254		break;
 255	case REG_WIN0H:
 256		softwareRenderer->winN[0].h.end = value;
 257		softwareRenderer->winN[0].h.start = value >> 8;
 258		if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end) {
 259			softwareRenderer->winN[0].h.start = 0;
 260		}
 261		if (softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end || softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
 262			softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
 263		}
 264		break;
 265	case REG_WIN1H:
 266		softwareRenderer->winN[1].h.end = value;
 267		softwareRenderer->winN[1].h.start = value >> 8;
 268		if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end) {
 269			softwareRenderer->winN[1].h.start = 0;
 270		}
 271		if (softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end || softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
 272			softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
 273		}
 274		break;
 275	case REG_WIN0V:
 276		softwareRenderer->winN[0].v.end = value;
 277		softwareRenderer->winN[0].v.start = value >> 8;
 278		if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end) {
 279			softwareRenderer->winN[0].v.start = 0;
 280		}
 281		if (softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end || softwareRenderer->winN[0].v.end > VIDEO_HORIZONTAL_PIXELS) {
 282			softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
 283		}
 284		break;
 285	case REG_WIN1V:
 286		softwareRenderer->winN[1].v.end = value;
 287		softwareRenderer->winN[1].v.start = value >> 8;
 288		if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end) {
 289			softwareRenderer->winN[1].v.start = 0;
 290		}
 291		if (softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end || softwareRenderer->winN[1].v.end > VIDEO_HORIZONTAL_PIXELS) {
 292			softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
 293		}
 294		break;
 295	case REG_WININ:
 296		softwareRenderer->winN[0].control.packed = value;
 297		softwareRenderer->winN[1].control.packed = value >> 8;
 298		break;
 299	case REG_WINOUT:
 300		softwareRenderer->winout.packed = value;
 301		softwareRenderer->objwin.packed = value >> 8;
 302		break;
 303	case REG_MOSAIC:
 304		softwareRenderer->mosaic = value;
 305		break;
 306	case REG_GREENSWP:
 307		GBALog(0, GBA_LOG_STUB, "Stub video register write: 0x%03X", address);
 308		break;
 309	default:
 310		GBALog(0, GBA_LOG_GAME_ERROR, "Invalid video register: 0x%03X", address);
 311	}
 312	return value;
 313}
 314
 315static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 316	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 317	softwareRenderer->oamDirty = 1;
 318	UNUSED(oam);
 319}
 320
 321static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 322	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 323#ifdef COLOR_16_BIT
 324#ifdef COLOR_5_6_5
 325	unsigned color = 0;
 326	color |= (value & 0x001F) << 11;
 327	color |= (value & 0x03E0) << 1;
 328	color |= (value & 0x7C00) >> 10;
 329#else
 330	unsigned color = value;
 331#endif
 332#else
 333	unsigned color = 0;
 334	color |= (value << 3) & 0xF8;
 335	color |= (value << 6) & 0xF800;
 336	color |= (value << 9) & 0xF80000;
 337#endif
 338	softwareRenderer->normalPalette[address >> 1] = color;
 339	if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
 340		softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
 341	} else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
 342		softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
 343	}
 344}
 345
 346static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
 347	int activeWindow;
 348	int startX = 0;
 349	if (win->h.end > 0) {
 350		for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
 351			if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
 352				// Insert a window before the end of the active window
 353				struct Window oldWindow = softwareRenderer->windows[activeWindow];
 354				if (win->h.start > startX) {
 355					// And after the start of the active window
 356					int nextWindow = softwareRenderer->nWindows;
 357					++softwareRenderer->nWindows;
 358					for (; nextWindow > activeWindow; --nextWindow) {
 359						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
 360					}
 361					softwareRenderer->windows[activeWindow].endX = win->h.start;
 362					++activeWindow;
 363				}
 364				softwareRenderer->windows[activeWindow].control = win->control;
 365				softwareRenderer->windows[activeWindow].endX = win->h.end;
 366				if (win->h.end >= oldWindow.endX) {
 367					// Trim off extra windows we've overwritten
 368					for (++activeWindow; softwareRenderer->nWindows > activeWindow + 1 && win->h.end >= softwareRenderer->windows[activeWindow].endX; ++activeWindow) {
 369						softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
 370						--softwareRenderer->nWindows;
 371					}
 372				} else {
 373					++activeWindow;
 374					int nextWindow = softwareRenderer->nWindows;
 375					++softwareRenderer->nWindows;
 376					for (; nextWindow > activeWindow; --nextWindow) {
 377						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
 378					}
 379					softwareRenderer->windows[activeWindow] = oldWindow;
 380				}
 381				break;
 382			}
 383			startX = softwareRenderer->windows[activeWindow].endX;
 384		}
 385	}
 386}
 387
 388static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
 389	int i;
 390	int oamMax = 0;
 391	for (i = 0; i < 128; ++i) {
 392		struct GBAObj obj;
 393		LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
 394		LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
 395		LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
 396		if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
 397			int height = _objSizes[GBAObjAttributesAGetShape(obj.a) * 8 + GBAObjAttributesBGetSize(obj.b) * 2 + 1];
 398			if (GBAObjAttributesAIsTransformed(obj.a)) {
 399				height <<= GBAObjAttributesAGetDoubleSize(obj.a);
 400			}
 401			if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
 402				renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
 403				renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
 404				renderer->sprites[oamMax].obj = obj;
 405				++oamMax;
 406			}
 407		}
 408	}
 409	renderer->oamMax = oamMax;
 410	renderer->oamDirty = 0;
 411}
 412
 413
 414static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 415	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 416
 417	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 418	if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
 419		int x;
 420		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 421			row[x] = GBA_COLOR_WHITE;
 422		}
 423		return;
 424	}
 425
 426	int x;
 427	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
 428		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
 429		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
 430		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
 431		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
 432	}
 433
 434	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
 435	softwareRenderer->nWindows = 1;
 436	if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
 437		softwareRenderer->windows[0].control = softwareRenderer->winout;
 438		if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) && y < softwareRenderer->winN[1].v.end && y >= softwareRenderer->winN[1].v.start) {
 439			_breakWindow(softwareRenderer, &softwareRenderer->winN[1]);
 440		}
 441		if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) && y < softwareRenderer->winN[0].v.end && y >= softwareRenderer->winN[0].v.start) {
 442			_breakWindow(softwareRenderer, &softwareRenderer->winN[0]);
 443		}
 444	} else {
 445		softwareRenderer->windows[0].control.packed = 0xFF;
 446	}
 447
 448	int w;
 449	x = 0;
 450	for (w = 0; w < softwareRenderer->nWindows; ++w) {
 451		// TOOD: handle objwin on backdrop
 452		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
 453		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
 454			backdrop |= softwareRenderer->normalPalette[0];
 455		} else {
 456			backdrop |= softwareRenderer->variantPalette[0];
 457		}
 458		int end = softwareRenderer->windows[w].endX;
 459		for (; x < end; ++x) {
 460			softwareRenderer->row[x] = backdrop;
 461		}
 462	}
 463
 464	_drawScanline(softwareRenderer, y);
 465
 466	if (softwareRenderer->target2Bd) {
 467		x = 0;
 468		for (w = 0; w < softwareRenderer->nWindows; ++w) {
 469		uint32_t backdrop = FLAG_UNWRITTEN;
 470			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
 471				backdrop |= softwareRenderer->normalPalette[0];
 472			} else {
 473				backdrop |= softwareRenderer->variantPalette[0];
 474			}
 475			int end = softwareRenderer->windows[w].endX;
 476			for (; x < end; ++x) {
 477				uint32_t color = softwareRenderer->row[x];
 478				if (color & FLAG_TARGET_1) {
 479					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
 480				}
 481			}
 482		}
 483	}
 484
 485#ifdef COLOR_16_BIT
 486#ifdef __arm__
 487	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
 488#else
 489	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 490		row[x] = softwareRenderer->row[x];
 491	}
 492#endif
 493#else
 494	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
 495#endif
 496}
 497
 498static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 499	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 500
 501	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
 502	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
 503	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
 504	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
 505}
 506
 507static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels) {
 508	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 509
 510	*stride = softwareRenderer->outputBufferStride;
 511	*pixels = softwareRenderer->outputBuffer;
 512}
 513
 514static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels) {
 515	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 516
 517	uint32_t* colorPixels = pixels;
 518	unsigned i;
 519	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
 520		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
 521	}
 522}
 523
 524static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
 525	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt);
 526	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt);
 527	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt);
 528	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt);
 529}
 530
 531static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 532	UNUSED(renderer);
 533	bg->priority = GBARegisterBGCNTGetPriority(value);
 534	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
 535	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
 536	bg->multipalette = GBARegisterBGCNTGet256Color(value);
 537	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
 538	bg->overflow = GBARegisterBGCNTGetOverflow(value);
 539	bg->size = GBARegisterBGCNTGetSize(value);
 540}
 541
 542static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 543	bg->dx = value;
 544}
 545
 546static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 547	bg->dmx = value;
 548}
 549
 550static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 551	bg->dy = value;
 552}
 553
 554static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 555	bg->dmy = value;
 556}
 557
 558static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 559	bg->refx = (bg->refx & 0xFFFF0000) | value;
 560	bg->sx = bg->refx;
 561}
 562
 563static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 564	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
 565	bg->refx <<= 4;
 566	bg->refx >>= 4;
 567	bg->sx = bg->refx;
 568}
 569
 570static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 571	bg->refy = (bg->refy & 0xFFFF0000) | value;
 572	bg->sy = bg->refy;
 573}
 574
 575static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 576	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
 577	bg->refy <<= 4;
 578	bg->refy >>= 4;
 579	bg->sy = bg->refy;
 580}
 581
 582static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
 583	enum BlendEffect oldEffect = renderer->blendEffect;
 584
 585	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
 586	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
 587	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
 588	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
 589	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
 590	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
 591	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
 592	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
 593
 594	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
 595	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
 596	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
 597	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
 598	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
 599
 600	renderer->anyTarget2 = value & 0x3F00;
 601
 602	if (oldEffect != renderer->blendEffect) {
 603		_updatePalettes(renderer);
 604	}
 605}
 606
 607#define TEST_LAYER_ENABLED(X) \
 608	(renderer->bg[X].enabled && \
 609	(GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
 610	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
 611	renderer->bg[X].priority == priority)
 612
 613static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
 614	int w;
 615	renderer->end = 0;
 616	int spriteLayers = 0;
 617	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt)) {
 618		if (renderer->oamDirty) {
 619			_cleanOAM(renderer);
 620		}
 621		int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
 622		int mosaicY = y - (y % mosaicV);
 623		for (w = 0; w < renderer->nWindows; ++w) {
 624			renderer->start = renderer->end;
 625			renderer->end = renderer->windows[w].endX;
 626			renderer->currentWindow = renderer->windows[w].control;
 627			if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
 628				continue;
 629			}
 630			int i;
 631			int drawn;
 632			for (i = 0; i < renderer->oamMax; ++i) {
 633				int localY = y;
 634				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
 635				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
 636					localY = mosaicY;
 637				}
 638				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
 639					continue;
 640				}
 641				drawn = _preprocessSprite(renderer, &sprite->obj, localY);
 642				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
 643			}
 644		}
 645	}
 646
 647	int priority;
 648	for (priority = 0; priority < 4; ++priority) {
 649		if (spriteLayers & (1 << priority)) {
 650			_postprocessSprite(renderer, priority);
 651		}
 652		renderer->end = 0;
 653		for (w = 0; w < renderer->nWindows; ++w) {
 654			renderer->start = renderer->end;
 655			renderer->end = renderer->windows[w].endX;
 656			renderer->currentWindow = renderer->windows[w].control;
 657			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
 658				_drawBackgroundMode0(renderer, &renderer->bg[0], y);
 659			}
 660			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
 661				_drawBackgroundMode0(renderer, &renderer->bg[1], y);
 662			}
 663			if (TEST_LAYER_ENABLED(2)) {
 664				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
 665				case 0:
 666					_drawBackgroundMode0(renderer, &renderer->bg[2], y);
 667					break;
 668				case 1:
 669				case 2:
 670					_drawBackgroundMode2(renderer, &renderer->bg[2], y);
 671					break;
 672				case 3:
 673					_drawBackgroundMode3(renderer, &renderer->bg[2], y);
 674					break;
 675				case 4:
 676					_drawBackgroundMode4(renderer, &renderer->bg[2], y);
 677					break;
 678				case 5:
 679					_drawBackgroundMode5(renderer, &renderer->bg[2], y);
 680					break;
 681				}
 682			}
 683			if (TEST_LAYER_ENABLED(3)) {
 684				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
 685				case 0:
 686					_drawBackgroundMode0(renderer, &renderer->bg[3], y);
 687					break;
 688				case 2:
 689					_drawBackgroundMode2(renderer, &renderer->bg[3], y);
 690					break;
 691				}
 692			}
 693		}
 694	}
 695	renderer->bg[2].sx += renderer->bg[2].dmx;
 696	renderer->bg[2].sy += renderer->bg[2].dmy;
 697	renderer->bg[3].sx += renderer->bg[3].dmx;
 698	renderer->bg[3].sy += renderer->bg[3].dmy;
 699}
 700
 701// We stash the priority on the top bits so we can do a one-operator comparison
 702// The lower the number, the higher the priority, and sprites take precendence over backgrounds
 703// We want to do special processing if the color pixel is target 1, however
 704
 705static inline void _compositeBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 706	if (color >= current) {
 707		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 708			color = _mix(renderer->blda, current, renderer->bldb, color);
 709		} else {
 710			color = current & 0x00FFFFFF;
 711		}
 712	} else {
 713		color = (color & ~FLAG_TARGET_2) | (current & FLAG_OBJWIN);
 714	}
 715	*pixel = color;
 716}
 717
 718static inline void _compositeBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 719	// We stash the priority on the top bits so we can do a one-operator comparison
 720	// The lower the number, the higher the priority, and sprites take precendence over backgrounds
 721	// We want to do special processing if the color pixel is target 1, however
 722	if (color >= current) {
 723		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 724			color = _mix(renderer->blda, current, renderer->bldb, color);
 725		} else {
 726			color = current & 0x00FFFFFF;
 727		}
 728	} else {
 729		color = color & ~FLAG_TARGET_2;
 730	}
 731	*pixel = color;
 732}
 733
 734static inline void _compositeNoBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 735	UNUSED(renderer);
 736	if (color < current) {
 737		*pixel = color | (current & FLAG_OBJWIN);
 738	}
 739}
 740
 741static inline void _compositeNoBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 742	UNUSED(renderer);
 743	if (color < current) {
 744		*pixel = color;
 745	}
 746}
 747
 748#define COMPOSITE_16_OBJWIN(BLEND) \
 749	if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
 750		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[paletteData | pixelData] : palette[pixelData]; \
 751		_composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
 752	}
 753
 754#define COMPOSITE_16_NO_OBJWIN(BLEND) \
 755	_composite ## BLEND ## NoObjwin(renderer, pixel, palette[pixelData] | flags, current);
 756
 757#define COMPOSITE_256_OBJWIN(BLEND) \
 758	if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
 759		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[pixelData] : palette[pixelData]; \
 760		_composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
 761	}
 762
 763#define COMPOSITE_256_NO_OBJWIN(BLEND) \
 764	COMPOSITE_16_NO_OBJWIN(BLEND)
 765
 766#define BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN) \
 767	pixelData = tileData & 0xF; \
 768	current = *pixel; \
 769	if (pixelData && IS_WRITABLE(current)) { \
 770		COMPOSITE_16_ ## OBJWIN (BLEND); \
 771	} \
 772	tileData >>= 4;
 773
 774#define BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN) \
 775	pixelData = tileData & 0xFF; \
 776	current = *pixel; \
 777	if (pixelData && IS_WRITABLE(current)) { \
 778		COMPOSITE_256_ ## OBJWIN (BLEND); \
 779	} \
 780	tileData >>= 8;
 781
 782#define BACKGROUND_TEXT_SELECT_CHARACTER \
 783	localX = tileX * 8 + inX; \
 784	xBase = localX & 0xF8; \
 785	if (background->size & 1) { \
 786		xBase += (localX & 0x100) << 5; \
 787	} \
 788	screenBase = yBase + (xBase >> 3); \
 789	LOAD_16(mapData, screenBase << 1, vram); \
 790	localY = inY & 0x7; \
 791	if (GBA_TEXT_MAP_VFLIP(mapData)) { \
 792		localY = 7 - localY; \
 793	}
 794
 795#define PREPARE_OBJWIN \
 796	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt); \
 797	int objwinOnly = 0; \
 798	int objwinForceEnable = 0; \
 799	color_t* objwinPalette; \
 800	if (objwinSlowPath) { \
 801		if (background->target1 && GBAWindowControlIsBlendEnable(renderer->objwin.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN)) { \
 802			objwinPalette = renderer->variantPalette; \
 803		} else { \
 804			objwinPalette = renderer->normalPalette; \
 805		} \
 806		switch (background->index) { \
 807		case 0: \
 808			objwinForceEnable = GBAWindowControlIsBg0Enable(renderer->objwin.packed) && GBAWindowControlIsBg0Enable(renderer->currentWindow.packed); \
 809			objwinOnly = !GBAWindowControlIsBg0Enable(renderer->objwin.packed); \
 810			break; \
 811		case 1: \
 812			objwinForceEnable = GBAWindowControlIsBg1Enable(renderer->objwin.packed) && GBAWindowControlIsBg1Enable(renderer->currentWindow.packed); \
 813			objwinOnly = !GBAWindowControlIsBg1Enable(renderer->objwin.packed); \
 814			break; \
 815		case 2: \
 816			objwinForceEnable = GBAWindowControlIsBg2Enable(renderer->objwin.packed) && GBAWindowControlIsBg2Enable(renderer->currentWindow.packed); \
 817			objwinOnly = !GBAWindowControlIsBg2Enable(renderer->objwin.packed); \
 818			break; \
 819		case 3: \
 820			objwinForceEnable = GBAWindowControlIsBg3Enable(renderer->objwin.packed) && GBAWindowControlIsBg3Enable(renderer->currentWindow.packed); \
 821			objwinOnly = !GBAWindowControlIsBg3Enable(renderer->objwin.packed); \
 822			break; \
 823		} \
 824	}
 825
 826#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_16(BLEND, OBJWIN) \
 827	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 828	palette = &mainPalette[paletteData]; \
 829	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 830	LOAD_32(tileData, charBase, vram); \
 831	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 832		tileData >>= 4 * mod8; \
 833		for (; outX < end; ++outX) { \
 834			uint32_t* pixel = &renderer->row[outX]; \
 835			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 836		} \
 837	} else { \
 838		for (outX = end - 1; outX >= renderer->start; --outX) { \
 839			uint32_t* pixel = &renderer->row[outX]; \
 840			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 841		} \
 842	}
 843
 844#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_16(BLEND, OBJWIN) \
 845	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 846	LOAD_32(tileData, charBase, vram); \
 847	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 848	palette = &mainPalette[paletteData]; \
 849	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 850		outX = renderer->end - mod8; \
 851		if (outX < renderer->start) { \
 852			tileData >>= 4 * (renderer->start - outX); \
 853			outX = renderer->start; \
 854		} \
 855		for (; outX < renderer->end; ++outX) { \
 856			uint32_t* pixel = &renderer->row[outX]; \
 857			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 858		} \
 859	} else { \
 860		tileData >>= 4 * (0x8 - mod8); \
 861		int end2 = renderer->end - 8; \
 862		if (end2 < -1) { \
 863			end2 = -1; \
 864		} \
 865		for (outX = renderer->end - 1; outX > end2; --outX) { \
 866			uint32_t* pixel = &renderer->row[outX]; \
 867			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 868		} \
 869	}
 870
 871#define DRAW_BACKGROUND_MODE_0_MOSAIC_16(BLEND, OBJWIN) \
 872	for (; tileX < tileEnd; ++tileX) { \
 873		BACKGROUND_TEXT_SELECT_CHARACTER; \
 874		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 875		tileData = carryData; \
 876		for (x = 0; x < 8; ++x) { \
 877			if (!mosaicWait) { \
 878				paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 879				palette = &mainPalette[paletteData]; \
 880				LOAD_32(tileData, charBase, vram); \
 881				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 882					tileData >>= x * 4; \
 883				} else { \
 884					tileData >>= (7 - x) * 4; \
 885				} \
 886				tileData &= 0xF; \
 887				tileData |= tileData << 4; \
 888				tileData |= tileData << 8; \
 889				tileData |= tileData << 12; \
 890				tileData |= tileData << 16; \
 891				tileData |= tileData << 20; \
 892				tileData |= tileData << 24; \
 893				tileData |= tileData << 28; \
 894				carryData = tileData; \
 895				mosaicWait = mosaicH; \
 896			} \
 897			--mosaicWait; \
 898			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 899			++pixel; \
 900		} \
 901	}
 902
 903#define DRAW_BACKGROUND_MODE_0_TILES_16(BLEND, OBJWIN) \
 904	for (; tileX < tileEnd; ++tileX) { \
 905		BACKGROUND_TEXT_SELECT_CHARACTER; \
 906		paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 907		palette = &mainPalette[paletteData]; \
 908		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 909		LOAD_32(tileData, charBase, vram); \
 910		if (tileData) { \
 911			if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 912				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 913				++pixel; \
 914				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 915				++pixel; \
 916				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 917				++pixel; \
 918				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 919				++pixel; \
 920				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 921				++pixel; \
 922				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 923				++pixel; \
 924				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 925				++pixel; \
 926				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 927				++pixel; \
 928			} else { \
 929				pixel += 7; \
 930				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 931				--pixel; \
 932				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 933				--pixel; \
 934				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 935				--pixel; \
 936				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 937				--pixel; \
 938				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 939				--pixel; \
 940				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 941				--pixel; \
 942				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 943				--pixel; \
 944				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 945				pixel += 8; \
 946			} \
 947		} else { \
 948			pixel += 8; \
 949		} \
 950	}
 951
 952#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_256(BLEND, OBJWIN) \
 953	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
 954	int end2 = end - 4; \
 955	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 956		int shift = inX & 0x3; \
 957		if (end2 > outX) { \
 958			LOAD_32(tileData, charBase, vram); \
 959			tileData >>= 8 * shift; \
 960			shift = 0; \
 961			for (; outX < end2; ++outX) { \
 962				pixel = &renderer->row[outX]; \
 963				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 964			} \
 965		} \
 966		\
 967		LOAD_32(tileData, charBase + 4, vram); \
 968		tileData >>= 8 * shift; \
 969		for (; outX < end; ++outX) { \
 970			pixel = &renderer->row[outX]; \
 971			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 972		} \
 973	} else { \
 974		int start = outX; \
 975		outX = end - 1; \
 976		if (end2 > start) { \
 977			LOAD_32(tileData, charBase, vram); \
 978			for (; outX >= end2; --outX) { \
 979				pixel = &renderer->row[outX]; \
 980				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 981			} \
 982			charBase += 4; \
 983		} \
 984		\
 985		LOAD_32(tileData, charBase, vram); \
 986		for (; outX >= renderer->start; --outX) { \
 987			pixel = &renderer->row[outX]; \
 988			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 989		} \
 990		outX = end; \
 991	}
 992
 993#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_256(BLEND, OBJWIN) \
 994	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
 995	outX = renderer->end - 8 + end; \
 996	int end2 = 4 - end; \
 997	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 998		if (end2 > 0) { \
 999			LOAD_32(tileData, charBase, vram); \
1000			for (; outX < renderer->end - end2; ++outX) { \
1001				pixel = &renderer->row[outX]; \
1002				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1003			} \
1004			charBase += 4; \
1005		} \
1006		\
1007		LOAD_32(tileData, charBase, vram); \
1008		for (; outX < renderer->end; ++outX) { \
1009			pixel = &renderer->row[outX]; \
1010			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1011		} \
1012	} else { \
1013		int shift = end & 0x3; \
1014		int start = outX; \
1015		outX = renderer->end - 1; \
1016		if (end2 > 0) { \
1017			LOAD_32(tileData, charBase, vram); \
1018			tileData >>= 8 * shift; \
1019			for (; outX >= start + 4; --outX) { \
1020				pixel = &renderer->row[outX]; \
1021				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1022			} \
1023			shift = 0; \
1024		} \
1025		\
1026		LOAD_32(tileData, charBase + 4, vram); \
1027		tileData >>= 8 * shift; \
1028		for (; outX >= start; --outX) { \
1029			pixel = &renderer->row[outX]; \
1030			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1031		} \
1032	}
1033
1034#define DRAW_BACKGROUND_MODE_0_TILES_256(BLEND, OBJWIN) \
1035	for (; tileX < tileEnd; ++tileX) { \
1036		BACKGROUND_TEXT_SELECT_CHARACTER; \
1037		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1038		if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1039			LOAD_32(tileData, charBase, vram); \
1040			if (tileData) { \
1041					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1042					++pixel; \
1043					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1044					++pixel; \
1045					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1046					++pixel; \
1047					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1048					++pixel; \
1049			} else { \
1050				pixel += 4; \
1051			} \
1052			LOAD_32(tileData, charBase + 4, vram); \
1053			if (tileData) { \
1054					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1055					++pixel; \
1056					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1057					++pixel; \
1058					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1059					++pixel; \
1060					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1061					++pixel; \
1062			} else { \
1063				pixel += 4; \
1064			} \
1065		} else { \
1066			LOAD_32(tileData, charBase + 4, vram); \
1067			if (tileData) { \
1068				pixel += 3; \
1069				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1070				--pixel; \
1071				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1072				--pixel; \
1073				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1074				--pixel; \
1075				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1076			} \
1077			pixel += 4; \
1078			LOAD_32(tileData, charBase, vram); \
1079			if (tileData) { \
1080				pixel += 3; \
1081				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1082				--pixel; \
1083				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1084				--pixel; \
1085				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1086				--pixel; \
1087				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1088			} \
1089			pixel += 4; \
1090		} \
1091	}
1092
1093#define DRAW_BACKGROUND_MODE_0_MOSAIC_256(BLEND, OBJWIN) \
1094	for (; tileX < tileEnd; ++tileX) { \
1095		BACKGROUND_TEXT_SELECT_CHARACTER; \
1096		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1097		tileData = carryData; \
1098		for (x = 0; x < 8; ++x) { \
1099			if (!mosaicWait) { \
1100				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1101					if (x >= 4) { \
1102						LOAD_32(tileData, charBase + 4, vram); \
1103						tileData >>= (x - 4) * 8; \
1104					} else { \
1105						LOAD_32(tileData, charBase, vram); \
1106						tileData >>= x * 8; \
1107					} \
1108				} else { \
1109					if (x >= 4) { \
1110						LOAD_32(tileData, charBase, vram); \
1111						tileData >>= (7 - x) * 8; \
1112					} else { \
1113						LOAD_32(tileData, charBase + 4, vram); \
1114						tileData >>= (3 - x) * 8; \
1115					} \
1116				} \
1117				tileData &= 0xFF; \
1118				carryData = tileData; \
1119				mosaicWait = mosaicH; \
1120			} \
1121			tileData |= tileData << 8; \
1122			--mosaicWait; \
1123			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1124			++pixel; \
1125		} \
1126	}
1127
1128#define DRAW_BACKGROUND_MODE_0(BPP, BLEND, OBJWIN) \
1129	uint32_t* pixel = &renderer->row[outX]; \
1130	if (background->mosaic && GBAMosaicControlGetBgH(renderer->mosaic)) { \
1131		int mosaicH = GBAMosaicControlGetBgH(renderer->mosaic) + 1; \
1132		int x; \
1133		int mosaicWait = outX % mosaicH; \
1134		int carryData = 0; \
1135		paletteData = 0; /* Quiets compiler warning */ \
1136		DRAW_BACKGROUND_MODE_0_MOSAIC_ ## BPP (BLEND, OBJWIN) \
1137		return; \
1138	} \
1139	\
1140	if (inX & 0x7) { \
1141		int mod8 = inX & 0x7; \
1142		BACKGROUND_TEXT_SELECT_CHARACTER; \
1143		\
1144		int end = outX + 0x8 - mod8; \
1145		if (end > renderer->end) { \
1146			/* TODO: ensure tiles are properly aligned from this*/ \
1147			end = renderer->end; \
1148		} \
1149		if (end == outX) { \
1150			return; \
1151		} \
1152		DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_ ## BPP (BLEND, OBJWIN) \
1153	} \
1154	if (inX & 0x7 || (renderer->end - renderer->start) & 0x7) { \
1155		tileX = tileEnd; \
1156		int pixelData; \
1157		int mod8 = (inX + renderer->end - renderer->start) & 0x7; \
1158		BACKGROUND_TEXT_SELECT_CHARACTER; \
1159		\
1160		int end = 0x8 - mod8; \
1161		UNUSED(end); \
1162		DRAW_BACKGROUND_MODE_0_TILE_PREFIX_ ## BPP (BLEND, OBJWIN) \
1163		\
1164		tileX = (inX & 0x7) != 0; \
1165		outX = renderer->start + tileX * 8 - (inX & 0x7); \
1166	} \
1167	\
1168	pixel = &renderer->row[outX]; \
1169	DRAW_BACKGROUND_MODE_0_TILES_ ## BPP (BLEND, OBJWIN)
1170
1171static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y) {
1172	int inX = renderer->start + background->x;
1173	if (background->mosaic) {
1174		int mosaicV = GBAMosaicControlGetBgV(renderer->mosaic) + 1;
1175		y -= y % mosaicV;
1176	}
1177	int inY = y + background->y;
1178	uint16_t mapData;
1179
1180	unsigned yBase = inY & 0xF8;
1181	if (background->size == 2) {
1182		yBase += inY & 0x100;
1183	} else if (background->size == 3) {
1184		yBase += (inY & 0x100) << 1;
1185	}
1186	yBase = (background->screenBase >> 1) + (yBase << 2);
1187
1188	int localX;
1189	int localY;
1190
1191	unsigned xBase;
1192
1193	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND;
1194	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA);
1195	flags |= FLAG_TARGET_2 * background->target2;
1196	if (renderer->blda == 0x10 && renderer->bldb == 0) {
1197		flags &= ~(FLAG_TARGET_1 | FLAG_TARGET_2);
1198	}
1199
1200	uint32_t screenBase;
1201	uint32_t charBase;
1202	int variant = background->target1 && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1203	color_t* mainPalette = renderer->normalPalette;
1204	if (variant) {
1205		mainPalette = renderer->variantPalette;
1206	}
1207	color_t* palette = mainPalette;
1208	PREPARE_OBJWIN;
1209
1210	int outX = renderer->start;
1211
1212	uint32_t tileData;
1213	uint32_t current;
1214	int pixelData;
1215	int paletteData;
1216	int tileX = 0;
1217	int tileEnd = (renderer->end - renderer->start + (inX & 0x7)) >> 3;
1218	uint16_t* vram = renderer->d.vram;
1219
1220	if (!objwinSlowPath) {
1221		if (!(flags & FLAG_TARGET_2)) {
1222			if (!background->multipalette) {
1223				DRAW_BACKGROUND_MODE_0(16, NoBlend, NO_OBJWIN);
1224			} else {
1225				DRAW_BACKGROUND_MODE_0(256, NoBlend, NO_OBJWIN);
1226			}
1227		} else {
1228			if (!background->multipalette) {
1229				DRAW_BACKGROUND_MODE_0(16, Blend, NO_OBJWIN);
1230			} else {
1231				DRAW_BACKGROUND_MODE_0(256, Blend, NO_OBJWIN);
1232			}
1233		}
1234	} else {
1235		if (!(flags & FLAG_TARGET_2)) {
1236			if (!background->multipalette) {
1237				DRAW_BACKGROUND_MODE_0(16, NoBlend, OBJWIN);
1238			} else {
1239				DRAW_BACKGROUND_MODE_0(256, NoBlend, OBJWIN);
1240			}
1241		} else {
1242			if (!background->multipalette) {
1243				DRAW_BACKGROUND_MODE_0(16, Blend, OBJWIN);
1244			} else {
1245				DRAW_BACKGROUND_MODE_0(256, Blend, OBJWIN);
1246			}
1247		}
1248	}
1249}
1250
1251#define BACKGROUND_BITMAP_INIT \
1252	UNUSED(unused); \
1253	int32_t x = background->sx + (renderer->start - 1) * background->dx; \
1254	int32_t y = background->sy + (renderer->start - 1) * background->dy; \
1255	int32_t localX; \
1256	int32_t localY; \
1257	\
1258	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
1259	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA); \
1260	flags |= FLAG_TARGET_2 * background->target2; \
1261	if (renderer->blda == 0x10 && renderer->bldb == 0) { \
1262		flags &= ~(FLAG_TARGET_1 | FLAG_TARGET_2); \
1263	} \
1264	int variant = background->target1 && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
1265	color_t* palette = renderer->normalPalette; \
1266	if (variant) { \
1267		palette = renderer->variantPalette; \
1268	} \
1269	PREPARE_OBJWIN;
1270
1271#define BACKGROUND_BITMAP_ITERATE(W, H) \
1272	x += background->dx; \
1273	y += background->dy; \
1274	\
1275	if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
1276		continue; \
1277	} else { \
1278		localX = x; \
1279		localY = y; \
1280	}
1281
1282static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1283	int sizeAdjusted = 0x8000 << background->size;
1284
1285	BACKGROUND_BITMAP_INIT;
1286
1287	uint32_t screenBase = background->screenBase;
1288	uint32_t charBase = background->charBase;
1289	uint8_t mapData;
1290	uint8_t tileData;
1291
1292	int outX;
1293	uint32_t* pixel;
1294	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1295		x += background->dx;
1296		y += background->dy;
1297
1298		if (background->overflow) {
1299			localX = x & (sizeAdjusted - 1);
1300			localY = y & (sizeAdjusted - 1);
1301		} else if ((x | y) & ~(sizeAdjusted - 1)) {
1302			continue;
1303		} else {
1304			localX = x;
1305			localY = y;
1306		}
1307		mapData = ((uint8_t*)renderer->d.vram)[screenBase + (localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)];
1308		tileData = ((uint8_t*)renderer->d.vram)[charBase + (mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
1309
1310		uint32_t current = *pixel;
1311		if (tileData && IS_WRITABLE(current)) {
1312			if (!objwinSlowPath) {
1313				_compositeBlendNoObjwin(renderer, pixel, palette[tileData] | flags, current);
1314			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1315				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1316				_compositeBlendObjwin(renderer, pixel, currentPalette[tileData] | flags, current);
1317			}
1318		}
1319	}
1320}
1321
1322static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1323	BACKGROUND_BITMAP_INIT;
1324
1325	uint32_t color;
1326
1327	int outX;
1328	uint32_t* pixel;
1329	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1330		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1331
1332		LOAD_16(color, ((localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS) << 1, renderer->d.vram);
1333#ifndef COLOR_16_BIT
1334		unsigned color32;
1335		color32 = 0;
1336		color32 |= (color << 3) & 0xF8;
1337		color32 |= (color << 6) & 0xF800;
1338		color32 |= (color << 9) & 0xF80000;
1339		color = color32;
1340#endif
1341
1342		uint32_t current = *pixel;
1343		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1344			if (!variant) {
1345				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1346			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1347				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1348			} else if (renderer->blendEffect == BLEND_DARKEN) {
1349				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1350			}
1351		}
1352	}
1353}
1354
1355static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1356	BACKGROUND_BITMAP_INIT;
1357
1358	uint16_t color;
1359	uint32_t offset = 0;
1360	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1361		offset = 0xA000;
1362	}
1363
1364	int outX;
1365	uint32_t* pixel;
1366	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1367		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1368
1369		color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1370
1371		uint32_t current = *pixel;
1372		if (color && IS_WRITABLE(current)) {
1373			if (!objwinSlowPath) {
1374				_compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
1375			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1376				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1377				_compositeBlendObjwin(renderer, pixel, currentPalette[color] | flags, current);
1378			}
1379		}
1380	}
1381}
1382
1383static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1384	BACKGROUND_BITMAP_INIT;
1385
1386	uint32_t color;
1387	uint32_t offset = 0;
1388	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1389		offset = 0xA000;
1390	}
1391
1392	int outX;
1393	uint32_t* pixel;
1394	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1395		BACKGROUND_BITMAP_ITERATE(160, 128);
1396
1397		LOAD_16(color, (offset + (localX >> 8) + (localY >> 8) * 160) << 1, renderer->d.vram);
1398#ifndef COLOR_16_BIT
1399		unsigned color32 = 0;
1400		color32 |= (color << 9) & 0xF80000;
1401		color32 |= (color << 3) & 0xF8;
1402		color32 |= (color << 6) & 0xF800;
1403		color = color32;
1404#endif
1405
1406		uint32_t current = *pixel;
1407		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1408			if (!variant) {
1409				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1410			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1411				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1412			} else if (renderer->blendEffect == BLEND_DARKEN) {
1413				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1414			}
1415		}
1416	}
1417}
1418
1419#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
1420	SPRITE_YBASE_ ## DEPTH(inY); \
1421	unsigned tileData; \
1422	for (; outX < condition; ++outX, inX += xOffset) { \
1423		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1424			continue; \
1425		} \
1426		SPRITE_XBASE_ ## DEPTH(inX); \
1427		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(inX); \
1428	}
1429
1430#define SPRITE_MOSAIC_LOOP(DEPTH, TYPE) \
1431	SPRITE_YBASE_ ## DEPTH(inY); \
1432	unsigned tileData; \
1433	if (outX % mosaicH) { \
1434		inX += (mosaicH - (outX % mosaicH)) * xOffset; \
1435		outX += mosaicH - (outX % mosaicH); \
1436	} \
1437	for (; outX < condition; ++outX, inX += xOffset) { \
1438		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1439			continue; \
1440		} \
1441		int localX = inX - xOffset * (outX % mosaicH); \
1442		SPRITE_XBASE_ ## DEPTH(localX); \
1443		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1444	}
1445
1446#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
1447	unsigned tileData; \
1448	for (; outX < x + totalWidth && outX < end; ++outX, ++inX) { \
1449		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1450			continue; \
1451		} \
1452		xAccum += mat.a; \
1453		yAccum += mat.c; \
1454		int localX = (xAccum >> 8) + (width >> 1); \
1455		int localY = (yAccum >> 8) + (height >> 1); \
1456		\
1457		if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
1458			continue; \
1459		} \
1460		\
1461		SPRITE_YBASE_ ## DEPTH(localY); \
1462		SPRITE_XBASE_ ## DEPTH(localX); \
1463		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1464	}
1465
1466#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
1467#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width >> 1 : 0x80) + (localY & 0x7) * 4;
1468
1469#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
1470	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1471	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1472	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1473		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1474	}
1475
1476#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
1477	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1478	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1479	if (tileData) { \
1480		renderer->row[outX] |= FLAG_OBJWIN; \
1481	}
1482
1483#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
1484#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width : 0x80) + (localY & 0x7) * 8;
1485
1486#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
1487	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1488	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1489	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1490		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1491	}
1492
1493#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
1494	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1495	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1496	if (tileData) { \
1497		renderer->row[outX] |= FLAG_OBJWIN; \
1498	}
1499
1500static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
1501	int width = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2];
1502	int height = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2 + 1];
1503	int start = renderer->start;
1504	int end = renderer->end;
1505	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
1506	flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1507	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
1508	int32_t x = GBAObjAttributesBGetX(sprite->b) << 23;
1509	x >>= 23;
1510	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
1511	unsigned charBase = GBAObjAttributesCGetTile(sprite->c) * 0x20;
1512	int variant = renderer->target1Obj && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1513	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1514		// Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1515		variant = 0;
1516	}
1517	color_t* palette = &renderer->normalPalette[0x100];
1518	if (variant) {
1519		palette = &renderer->variantPalette[0x100];
1520	}
1521
1522	int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
1523
1524	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1525		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
1526		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
1527		struct GBAOAMMatrix mat;
1528		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1529		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1530		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1531		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1532
1533		if (inY < 0) {
1534			inY += 256;
1535		}
1536		int outX = x >= start ? x : start;
1537		int inX = outX - x;
1538		int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1));
1539		int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1));
1540
1541		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1542			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1543			if (flags & FLAG_OBJWIN) {
1544				SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
1545			} else {
1546				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
1547			}
1548		} else {
1549			if (flags & FLAG_OBJWIN) {
1550				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
1551			} else {
1552				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
1553			}
1554		}
1555	} else {
1556		int outX = x >= start ? x : start;
1557		int condition = x + width;
1558		int mosaicH = 1;
1559		if (GBAObjAttributesAIsMosaic(sprite->a)) {
1560			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
1561			if (condition % mosaicH) {
1562				condition += mosaicH - (condition % mosaicH);
1563			}
1564		}
1565		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
1566			inY += 256;
1567		}
1568		if (GBAObjAttributesBIsVFlip(sprite->b)) {
1569			inY = height - inY - 1;
1570		}
1571		if (end < condition) {
1572			condition = end;
1573		}
1574		int inX = outX - x;
1575		int xOffset = 1;
1576		if (GBAObjAttributesBIsHFlip(sprite->b)) {
1577			inX = width - inX - 1;
1578			xOffset = -1;
1579		}
1580		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1581			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1582			if (flags & FLAG_OBJWIN) {
1583				SPRITE_NORMAL_LOOP(16, OBJWIN);
1584			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1585				SPRITE_MOSAIC_LOOP(16, NORMAL);
1586			} else {
1587				SPRITE_NORMAL_LOOP(16, NORMAL);
1588			}
1589		} else {
1590			if (flags & FLAG_OBJWIN) {
1591				SPRITE_NORMAL_LOOP(256, OBJWIN);
1592			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1593				SPRITE_MOSAIC_LOOP(256, NORMAL);
1594			} else {
1595				SPRITE_NORMAL_LOOP(256, NORMAL);
1596			}
1597		}
1598	}
1599	return 1;
1600}
1601
1602static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
1603	int x;
1604	uint32_t* pixel = renderer->row;
1605	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
1606
1607	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
1608	bool objwinDisable = false;
1609	bool objwinOnly = false;
1610	if (objwinSlowPath) {
1611		objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
1612		// TODO: Fix this for current window when WIN0/1 are enabled
1613		objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->winout.packed);
1614	}
1615	if (objwinSlowPath) {
1616		if (objwinDisable) {
1617			for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1618				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1619				uint32_t current = *pixel;
1620				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1621					_compositeBlendObjwin(renderer, pixel, color | flags, current);
1622				}
1623			}
1624			return;
1625		} else if (objwinOnly) {
1626			for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1627				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1628				uint32_t current = *pixel;
1629				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1630					_compositeBlendObjwin(renderer, pixel, color | flags, current);
1631				}
1632			}
1633			return;
1634		} else {
1635			for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1636				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1637				uint32_t current = *pixel;
1638				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1639					_compositeBlendObjwin(renderer, pixel, color | flags, current);
1640				}
1641			}
1642			return;
1643		}
1644	}
1645	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1646		uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1647		uint32_t current = *pixel;
1648		if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1649			_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
1650		}
1651	}
1652}
1653
1654static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
1655	int i;
1656	if (renderer->blendEffect == BLEND_BRIGHTEN) {
1657		for (i = 0; i < 512; ++i) {
1658			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
1659		}
1660	} else if (renderer->blendEffect == BLEND_DARKEN) {
1661		for (i = 0; i < 512; ++i) {
1662			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
1663		}
1664	} else {
1665		for (i = 0; i < 512; ++i) {
1666			renderer->variantPalette[i] = renderer->normalPalette[i];
1667		}
1668	}
1669}
1670
1671static inline unsigned _brighten(unsigned color, int y) {
1672	unsigned c = 0;
1673	unsigned a;
1674#ifdef COLOR_16_BIT
1675	a = color & 0x1F;
1676	c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
1677
1678#ifdef COLOR_5_6_5
1679	a = color & 0x7C0;
1680	c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
1681
1682	a = color & 0xF800;
1683	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1684#else
1685	a = color & 0x3E0;
1686	c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
1687
1688	a = color & 0x7C00;
1689	c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
1690#endif
1691#else
1692	a = color & 0xF8;
1693	c |= (a + ((0xF8 - a) * y) / 16) & 0xF8;
1694
1695	a = color & 0xF800;
1696	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1697
1698	a = color & 0xF80000;
1699	c |= (a + ((0xF80000 - a) * y) / 16) & 0xF80000;
1700#endif
1701	return c;
1702}
1703
1704static inline unsigned _darken(unsigned color, int y) {
1705	unsigned c = 0;
1706	unsigned a;
1707#ifdef COLOR_16_BIT
1708	a = color & 0x1F;
1709	c |= (a - (a * y) / 16) & 0x1F;
1710
1711#ifdef COLOR_5_6_5
1712	a = color & 0x7C0;
1713	c |= (a - (a * y) / 16) & 0x7C0;
1714
1715	a = color & 0xF800;
1716	c |= (a - (a * y) / 16) & 0xF800;
1717#else
1718	a = color & 0x3E0;
1719	c |= (a - (a * y) / 16) & 0x3E0;
1720
1721	a = color & 0x7C00;
1722	c |= (a - (a * y) / 16) & 0x7C00;
1723#endif
1724#else
1725	a = color & 0xF8;
1726	c |= (a - (a * y) / 16) & 0xF8;
1727
1728	a = color & 0xF800;
1729	c |= (a - (a * y) / 16) & 0xF800;
1730
1731	a = color & 0xF80000;
1732	c |= (a - (a * y) / 16) & 0xF80000;
1733#endif
1734	return c;
1735}
1736
1737static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
1738	unsigned c = 0;
1739	unsigned a, b;
1740#ifdef COLOR_16_BIT
1741#ifdef COLOR_5_6_5
1742	a = colorA & 0xF81F;
1743	b = colorB & 0xF81F;
1744	a |= (colorA & 0x7C0) << 16;
1745	b |= (colorB & 0x7C0) << 16;
1746	c = ((a * weightA + b * weightB) / 16);
1747	if (c & 0x08000000) {
1748		c = (c & ~0x0FC00000) | 0x07C00000;
1749	}
1750	if (c & 0x0020) {
1751		c = (c & ~0x003F) | 0x001F;
1752	}
1753	if (c & 0x10000) {
1754		c = (c & ~0x1F800) | 0xF800;
1755	}
1756	c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
1757#else
1758	a = colorA & 0x7C1F;
1759	b = colorB & 0x7C1F;
1760	a |= (colorA & 0x3E0) << 16;
1761	b |= (colorB & 0x3E0) << 16;
1762	c = ((a * weightA + b * weightB) / 16);
1763	if (c & 0x04000000) {
1764		c = (c & ~0x07E00000) | 0x03E00000;
1765	}
1766	if (c & 0x0020) {
1767		c = (c & ~0x003F) | 0x001F;
1768	}
1769	if (c & 0x10000) {
1770		c = (c & ~0x1F800) | 0xF800;
1771	}
1772	c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
1773#endif
1774#else
1775	a = colorA & 0xF8;
1776	b = colorB & 0xF8;
1777	c |= ((a * weightA + b * weightB) / 16) & 0x1F8;
1778	if (c & 0x00000100) {
1779		c = 0x000000F8;
1780	}
1781
1782	a = colorA & 0xF800;
1783	b = colorB & 0xF800;
1784	c |= ((a * weightA + b * weightB) / 16) & 0x1F800;
1785	if (c & 0x00010000) {
1786		c = (c & 0x000000F8) | 0x0000F800;
1787	}
1788
1789	a = colorA & 0xF80000;
1790	b = colorB & 0xF80000;
1791	c |= ((a * weightA + b * weightB) / 16) & 0x1F80000;
1792	if (c & 0x01000000) {
1793		c = (c & 0x0000F8F8) | 0x00F80000;
1794	}
1795#endif
1796	return c;
1797}