all repos — mgba @ 1205ff1895b5a2d1c37b23bded43a31d039befec

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		renderer->end = 0;
 650		for (w = 0; w < renderer->nWindows; ++w) {
 651			renderer->start = renderer->end;
 652			renderer->end = renderer->windows[w].endX;
 653			renderer->currentWindow = renderer->windows[w].control;
 654			if (spriteLayers & (1 << priority)) {
 655				_postprocessSprite(renderer, priority);
 656			}
 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, ++pixel) { \
 834			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 835		} \
 836	} else { \
 837		for (outX = end - 1; outX >= renderer->start; --outX) { \
 838			uint32_t* pixel = &renderer->row[outX]; \
 839			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 840		} \
 841	}
 842
 843#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_16(BLEND, OBJWIN) \
 844	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 845	LOAD_32(tileData, charBase, vram); \
 846	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 847	palette = &mainPalette[paletteData]; \
 848	pixel = &renderer->row[outX]; \
 849	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 850		if (outX < renderer->start) { \
 851			tileData >>= 4 * (renderer->start - outX); \
 852			outX = renderer->start; \
 853			pixel = &renderer->row[outX]; \
 854		} \
 855		for (; outX < renderer->end; ++outX, ++pixel) { \
 856			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 857		} \
 858	} else { \
 859		tileData >>= 4 * (0x8 - mod8); \
 860		int end = renderer->end - 8; \
 861		if (end < -1) { \
 862			end = -1; \
 863		} \
 864		outX = renderer->end - 1; \
 865		pixel = &renderer->row[outX]; \
 866		for (; outX > end; --outX, --pixel) { \
 867			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 868		} \
 869		/* Needed for consistency checks */ \
 870		outX = renderer->end; \
 871		pixel = &renderer->row[outX]; \
 872	}
 873
 874#define DRAW_BACKGROUND_MODE_0_MOSAIC_16(BLEND, OBJWIN) \
 875	for (; tileX < tileEnd; ++tileX) { \
 876		BACKGROUND_TEXT_SELECT_CHARACTER; \
 877		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 878		tileData = carryData; \
 879		for (x = 0; x < 8; ++x) { \
 880			if (!mosaicWait) { \
 881				paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 882				palette = &mainPalette[paletteData]; \
 883				LOAD_32(tileData, charBase, vram); \
 884				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 885					tileData >>= x * 4; \
 886				} else { \
 887					tileData >>= (7 - x) * 4; \
 888				} \
 889				tileData &= 0xF; \
 890				tileData |= tileData << 4; \
 891				tileData |= tileData << 8; \
 892				tileData |= tileData << 12; \
 893				tileData |= tileData << 16; \
 894				tileData |= tileData << 20; \
 895				tileData |= tileData << 24; \
 896				tileData |= tileData << 28; \
 897				carryData = tileData; \
 898				mosaicWait = mosaicH; \
 899			} \
 900			--mosaicWait; \
 901			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 902			++pixel; \
 903		} \
 904	}
 905
 906#define DRAW_BACKGROUND_MODE_0_TILES_16(BLEND, OBJWIN) \
 907	for (; tileX < tileEnd; ++tileX) { \
 908		BACKGROUND_TEXT_SELECT_CHARACTER; \
 909		paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 910		palette = &mainPalette[paletteData]; \
 911		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 912		LOAD_32(tileData, charBase, vram); \
 913		if (tileData) { \
 914			if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 915				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 916				++pixel; \
 917				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 918				++pixel; \
 919				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 920				++pixel; \
 921				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 922				++pixel; \
 923				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 924				++pixel; \
 925				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 926				++pixel; \
 927				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 928				++pixel; \
 929				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 930				++pixel; \
 931			} else { \
 932				pixel += 7; \
 933				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 934				--pixel; \
 935				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 936				--pixel; \
 937				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 938				--pixel; \
 939				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 940				--pixel; \
 941				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 942				--pixel; \
 943				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 944				--pixel; \
 945				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 946				--pixel; \
 947				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 948				pixel += 8; \
 949			} \
 950		} else { \
 951			pixel += 8; \
 952		} \
 953	}
 954
 955#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_256(BLEND, OBJWIN) \
 956	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
 957	int end2 = end - 4; \
 958	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 959		int shift = inX & 0x3; \
 960		if (end2 > outX) { \
 961			LOAD_32(tileData, charBase, vram); \
 962			tileData >>= 8 * shift; \
 963			shift = 0; \
 964			for (; outX < end2; ++outX, ++pixel) { \
 965				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 966			} \
 967		} \
 968		\
 969		LOAD_32(tileData, charBase + 4, vram); \
 970		tileData >>= 8 * shift; \
 971		for (; outX < end; ++outX, ++pixel) { \
 972			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 973		} \
 974	} else { \
 975		int start = outX; \
 976		outX = end - 1; \
 977		pixel = &renderer->row[outX]; \
 978		if (end2 > start) { \
 979			LOAD_32(tileData, charBase, vram); \
 980			for (; outX >= end2; --outX, --pixel) { \
 981				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 982			} \
 983			charBase += 4; \
 984		} \
 985		\
 986		LOAD_32(tileData, charBase, vram); \
 987		for (; outX >= renderer->start; --outX, --pixel) { \
 988			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 989		} \
 990		outX = end; \
 991		pixel = &renderer->row[outX]; \
 992	}
 993
 994#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_256(BLEND, OBJWIN) \
 995	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
 996	int end = mod8 - 4; \
 997	pixel = &renderer->row[outX]; \
 998	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 999		if (end > 0) { \
1000			LOAD_32(tileData, charBase, vram); \
1001			for (; outX < renderer->end - end; ++outX, ++pixel) { \
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, ++pixel) { \
1009			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1010		} \
1011	} else { \
1012		int shift = (8 - mod8) & 0x3; \
1013		int start = outX; \
1014		outX = renderer->end - 1; \
1015		pixel = &renderer->row[outX]; \
1016		if (end > 0) { \
1017			LOAD_32(tileData, charBase, vram); \
1018			tileData >>= 8 * shift; \
1019			for (; outX >= start + 4; --outX, --pixel) { \
1020				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1021			} \
1022			shift = 0; \
1023		} \
1024		\
1025		LOAD_32(tileData, charBase + 4, vram); \
1026		tileData >>= 8 * shift; \
1027		for (; outX >= start; --outX, --pixel) { \
1028			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1029		} \
1030		/* Needed for consistency checks */ \
1031		outX = renderer->end; \
1032		pixel = &renderer->row[outX]; \
1033	}
1034
1035#define DRAW_BACKGROUND_MODE_0_TILES_256(BLEND, OBJWIN) \
1036	for (; tileX < tileEnd; ++tileX) { \
1037		BACKGROUND_TEXT_SELECT_CHARACTER; \
1038		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1039		if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1040			LOAD_32(tileData, charBase, vram); \
1041			if (tileData) { \
1042					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1043					++pixel; \
1044					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1045					++pixel; \
1046					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1047					++pixel; \
1048					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1049					++pixel; \
1050			} else { \
1051				pixel += 4; \
1052			} \
1053			LOAD_32(tileData, charBase + 4, vram); \
1054			if (tileData) { \
1055					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1056					++pixel; \
1057					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1058					++pixel; \
1059					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1060					++pixel; \
1061					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1062					++pixel; \
1063			} else { \
1064				pixel += 4; \
1065			} \
1066		} else { \
1067			LOAD_32(tileData, charBase + 4, vram); \
1068			if (tileData) { \
1069				pixel += 3; \
1070				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1071				--pixel; \
1072				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1073				--pixel; \
1074				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1075				--pixel; \
1076				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1077			} \
1078			pixel += 4; \
1079			LOAD_32(tileData, charBase, vram); \
1080			if (tileData) { \
1081				pixel += 3; \
1082				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1083				--pixel; \
1084				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1085				--pixel; \
1086				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1087				--pixel; \
1088				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1089			} \
1090			pixel += 4; \
1091		} \
1092	}
1093
1094#define DRAW_BACKGROUND_MODE_0_MOSAIC_256(BLEND, OBJWIN) \
1095	for (; tileX < tileEnd; ++tileX) { \
1096		BACKGROUND_TEXT_SELECT_CHARACTER; \
1097		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1098		tileData = carryData; \
1099		for (x = 0; x < 8; ++x) { \
1100			if (!mosaicWait) { \
1101				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1102					if (x >= 4) { \
1103						LOAD_32(tileData, charBase + 4, vram); \
1104						tileData >>= (x - 4) * 8; \
1105					} else { \
1106						LOAD_32(tileData, charBase, vram); \
1107						tileData >>= x * 8; \
1108					} \
1109				} else { \
1110					if (x >= 4) { \
1111						LOAD_32(tileData, charBase, vram); \
1112						tileData >>= (7 - x) * 8; \
1113					} else { \
1114						LOAD_32(tileData, charBase + 4, vram); \
1115						tileData >>= (3 - x) * 8; \
1116					} \
1117				} \
1118				tileData &= 0xFF; \
1119				carryData = tileData; \
1120				mosaicWait = mosaicH; \
1121			} \
1122			tileData |= tileData << 8; \
1123			--mosaicWait; \
1124			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1125			++pixel; \
1126		} \
1127	}
1128
1129#define DRAW_BACKGROUND_MODE_0(BPP, BLEND, OBJWIN) \
1130	uint32_t* pixel = &renderer->row[outX]; \
1131	if (background->mosaic && GBAMosaicControlGetBgH(renderer->mosaic)) { \
1132		int mosaicH = GBAMosaicControlGetBgH(renderer->mosaic) + 1; \
1133		int x; \
1134		int mosaicWait = outX % mosaicH; \
1135		int carryData = 0; \
1136		paletteData = 0; /* Quiets compiler warning */ \
1137		DRAW_BACKGROUND_MODE_0_MOSAIC_ ## BPP (BLEND, OBJWIN) \
1138		return; \
1139	} \
1140	\
1141	if (inX & 0x7) { \
1142		BACKGROUND_TEXT_SELECT_CHARACTER; \
1143		\
1144		int mod8 = inX & 0x7; \
1145		int end = outX + 0x8 - mod8; \
1146		if (end > renderer->end) { \
1147			end = renderer->end; \
1148		} \
1149		if (UNLIKELY(end == outX)) { \
1150			return; \
1151		} \
1152		if (UNLIKELY(end < outX)) { \
1153			GBALog(0, GBA_LOG_DANGER, "Out of bounds background draw!"); \
1154			return; \
1155		} \
1156		DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_ ## BPP (BLEND, OBJWIN) \
1157		outX = end; \
1158		if (tileX < tileEnd) { \
1159			++tileX; \
1160		} else if (UNLIKELY(tileX > tileEnd)) { \
1161			GBALog(0, GBA_LOG_DANGER, "Invariant doesn't hold in background draw! tileX (%u) > tileEnd (%u)", tileX, tileEnd); \
1162			return; \
1163		} \
1164		length -= end - renderer->start; \
1165	} \
1166	/*! TODO: Make sure these lines can be removed */ \
1167	/*!*/ pixel = &renderer->row[outX]; \
1168	outX += (tileEnd - tileX) * 8; \
1169	/*!*/ if (UNLIKELY(outX > VIDEO_HORIZONTAL_PIXELS)) { \
1170	/*!*/	GBALog(0, GBA_LOG_DANGER, "Out of bounds background draw would occur!"); \
1171	/*!*/	return; \
1172	/*!*/ } \
1173	DRAW_BACKGROUND_MODE_0_TILES_ ## BPP (BLEND, OBJWIN) \
1174	if (length & 0x7) { \
1175		BACKGROUND_TEXT_SELECT_CHARACTER; \
1176		\
1177		int mod8 = length & 0x7; \
1178		if (UNLIKELY(outX + mod8 != renderer->end)) { \
1179			GBALog(0, GBA_LOG_DANGER, "Invariant doesn't hold in background draw!"); \
1180			return; \
1181		} \
1182		DRAW_BACKGROUND_MODE_0_TILE_PREFIX_ ## BPP (BLEND, OBJWIN) \
1183	} \
1184	if (UNLIKELY(&renderer->row[outX] != pixel)) { \
1185		GBALog(0, GBA_LOG_DANGER, "Background draw ended in the wrong place! Diff: %lx", &renderer->row[outX] - pixel); \
1186	} \
1187	if (UNLIKELY(outX > VIDEO_HORIZONTAL_PIXELS)) { \
1188		GBALog(0, GBA_LOG_FATAL, "Out of bounds background draw occurred!"); \
1189		return; \
1190	}
1191
1192static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y) {
1193	int inX = renderer->start + background->x;
1194	int length = renderer->end - renderer->start;
1195	if (background->mosaic) {
1196		int mosaicV = GBAMosaicControlGetBgV(renderer->mosaic) + 1;
1197		y -= y % mosaicV;
1198	}
1199	int inY = y + background->y;
1200	uint16_t mapData;
1201
1202	unsigned yBase = inY & 0xF8;
1203	if (background->size == 2) {
1204		yBase += inY & 0x100;
1205	} else if (background->size == 3) {
1206		yBase += (inY & 0x100) << 1;
1207	}
1208	yBase = (background->screenBase >> 1) + (yBase << 2);
1209
1210	int localX;
1211	int localY;
1212
1213	unsigned xBase;
1214
1215	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND;
1216	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA);
1217	flags |= FLAG_TARGET_2 * background->target2;
1218	if (renderer->blda == 0x10 && renderer->bldb == 0) {
1219		flags &= ~(FLAG_TARGET_1 | FLAG_TARGET_2);
1220	}
1221
1222	uint32_t screenBase;
1223	uint32_t charBase;
1224	int variant = background->target1 && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1225	color_t* mainPalette = renderer->normalPalette;
1226	if (variant) {
1227		mainPalette = renderer->variantPalette;
1228	}
1229	color_t* palette = mainPalette;
1230	PREPARE_OBJWIN;
1231
1232	int outX = renderer->start;
1233
1234	uint32_t tileData;
1235	uint32_t current;
1236	int pixelData;
1237	int paletteData;
1238	int tileX = 0;
1239	int tileEnd = ((length + inX) >> 3) - (inX >> 3);
1240	uint16_t* vram = renderer->d.vram;
1241
1242	if (!objwinSlowPath) {
1243		if (!(flags & FLAG_TARGET_2)) {
1244			if (!background->multipalette) {
1245				DRAW_BACKGROUND_MODE_0(16, NoBlend, NO_OBJWIN);
1246			} else {
1247				DRAW_BACKGROUND_MODE_0(256, NoBlend, NO_OBJWIN);
1248			}
1249		} else {
1250			if (!background->multipalette) {
1251				DRAW_BACKGROUND_MODE_0(16, Blend, NO_OBJWIN);
1252			} else {
1253				DRAW_BACKGROUND_MODE_0(256, Blend, NO_OBJWIN);
1254			}
1255		}
1256	} else {
1257		if (!(flags & FLAG_TARGET_2)) {
1258			if (!background->multipalette) {
1259				DRAW_BACKGROUND_MODE_0(16, NoBlend, OBJWIN);
1260			} else {
1261				DRAW_BACKGROUND_MODE_0(256, NoBlend, OBJWIN);
1262			}
1263		} else {
1264			if (!background->multipalette) {
1265				DRAW_BACKGROUND_MODE_0(16, Blend, OBJWIN);
1266			} else {
1267				DRAW_BACKGROUND_MODE_0(256, Blend, OBJWIN);
1268			}
1269		}
1270	}
1271}
1272
1273#define BACKGROUND_BITMAP_INIT \
1274	int32_t x = background->sx + (renderer->start - 1) * background->dx; \
1275	int32_t y = background->sy + (renderer->start - 1) * background->dy; \
1276	int mosaicH = 0; \
1277	int mosaicWait = 0; \
1278	if (background->mosaic) { \
1279		int mosaicV = GBAMosaicControlGetBgV(renderer->mosaic) + 1; \
1280		y -= (inY % mosaicV) * background->dmy; \
1281		x -= (inY % mosaicV) * background->dmx; \
1282		mosaicH = GBAMosaicControlGetBgH(renderer->mosaic); \
1283		mosaicWait = renderer->start % (mosaicH + 1); \
1284	} \
1285	int32_t localX; \
1286	int32_t localY; \
1287	\
1288	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
1289	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA); \
1290	flags |= FLAG_TARGET_2 * background->target2; \
1291	if (renderer->blda == 0x10 && renderer->bldb == 0) { \
1292		flags &= ~(FLAG_TARGET_1 | FLAG_TARGET_2); \
1293	} \
1294	int variant = background->target1 && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
1295	color_t* palette = renderer->normalPalette; \
1296	if (variant) { \
1297		palette = renderer->variantPalette; \
1298	} \
1299	PREPARE_OBJWIN;
1300
1301#define BACKGROUND_BITMAP_ITERATE(W, H) \
1302	x += background->dx; \
1303	y += background->dy; \
1304	\
1305	if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
1306		continue; \
1307	} else { \
1308		localX = x; \
1309		localY = y; \
1310	}
1311
1312static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
1313	int sizeAdjusted = 0x8000 << background->size;
1314
1315	BACKGROUND_BITMAP_INIT;
1316
1317	uint32_t screenBase = background->screenBase;
1318	uint32_t charBase = background->charBase;
1319	uint8_t mapData;
1320	uint8_t tileData = 0;
1321
1322	int outX;
1323	uint32_t* pixel;
1324	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1325		x += background->dx;
1326		y += background->dy;
1327
1328		if (!mosaicWait) {
1329			if (background->overflow) {
1330				localX = x & (sizeAdjusted - 1);
1331				localY = y & (sizeAdjusted - 1);
1332			} else if ((x | y) & ~(sizeAdjusted - 1)) {
1333				continue;
1334			} else {
1335				localX = x;
1336				localY = y;
1337			}
1338			mapData = ((uint8_t*)renderer->d.vram)[screenBase + (localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)];
1339			tileData = ((uint8_t*)renderer->d.vram)[charBase + (mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
1340
1341			mosaicWait = mosaicH;
1342		} else {
1343			--mosaicWait;
1344		}
1345
1346		uint32_t current = *pixel;
1347		if (tileData && IS_WRITABLE(current)) {
1348			if (!objwinSlowPath) {
1349				_compositeBlendNoObjwin(renderer, pixel, palette[tileData] | flags, current);
1350			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1351				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1352				_compositeBlendObjwin(renderer, pixel, currentPalette[tileData] | flags, current);
1353			}
1354		}
1355	}
1356}
1357
1358static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
1359	BACKGROUND_BITMAP_INIT;
1360
1361	uint32_t color = renderer->normalPalette[0];
1362
1363	int outX;
1364	uint32_t* pixel;
1365	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1366		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1367
1368		if (!mosaicWait) {
1369			LOAD_16(color, ((localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS) << 1, renderer->d.vram);
1370#ifndef COLOR_16_BIT
1371			unsigned color32;
1372			color32 = 0;
1373			color32 |= (color << 3) & 0xF8;
1374			color32 |= (color << 6) & 0xF800;
1375			color32 |= (color << 9) & 0xF80000;
1376			color = color32;
1377#endif
1378			mosaicWait = mosaicH;
1379		} else {
1380			--mosaicWait;
1381		}
1382
1383		uint32_t current = *pixel;
1384		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1385			if (!variant) {
1386				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1387			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1388				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1389			} else if (renderer->blendEffect == BLEND_DARKEN) {
1390				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1391			}
1392		}
1393	}
1394}
1395
1396static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
1397	BACKGROUND_BITMAP_INIT;
1398
1399	uint16_t color = renderer->normalPalette[0];
1400	uint32_t offset = 0;
1401	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1402		offset = 0xA000;
1403	}
1404
1405	int outX;
1406	uint32_t* pixel;
1407	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1408		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1409
1410		if (!mosaicWait) {
1411			color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1412
1413			mosaicWait = mosaicH;
1414		} else {
1415			--mosaicWait;
1416		}
1417
1418		uint32_t current = *pixel;
1419		if (color && IS_WRITABLE(current)) {
1420			if (!objwinSlowPath) {
1421				_compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
1422			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1423				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1424				_compositeBlendObjwin(renderer, pixel, currentPalette[color] | flags, current);
1425			}
1426		}
1427	}
1428}
1429
1430static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
1431	BACKGROUND_BITMAP_INIT;
1432
1433	uint32_t color = renderer->normalPalette[0];
1434	uint32_t offset = 0;
1435	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1436		offset = 0xA000;
1437	}
1438
1439	int outX;
1440	uint32_t* pixel;
1441	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1442		BACKGROUND_BITMAP_ITERATE(160, 128);
1443
1444		if (!mosaicWait) {
1445			LOAD_16(color, (offset + (localX >> 8) + (localY >> 8) * 160) << 1, renderer->d.vram);
1446#ifndef COLOR_16_BIT
1447			unsigned color32 = 0;
1448			color32 |= (color << 9) & 0xF80000;
1449			color32 |= (color << 3) & 0xF8;
1450			color32 |= (color << 6) & 0xF800;
1451			color = color32;
1452#endif
1453			mosaicWait = mosaicH;
1454		} else {
1455			--mosaicWait;
1456		}
1457
1458		uint32_t current = *pixel;
1459		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1460			if (!variant) {
1461				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1462			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1463				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1464			} else if (renderer->blendEffect == BLEND_DARKEN) {
1465				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1466			}
1467		}
1468	}
1469}
1470
1471#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
1472	SPRITE_YBASE_ ## DEPTH(inY); \
1473	unsigned tileData; \
1474	for (; outX < condition; ++outX, inX += xOffset) { \
1475		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1476			continue; \
1477		} \
1478		SPRITE_XBASE_ ## DEPTH(inX); \
1479		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(inX); \
1480	}
1481
1482#define SPRITE_MOSAIC_LOOP(DEPTH, TYPE) \
1483	SPRITE_YBASE_ ## DEPTH(inY); \
1484	unsigned tileData; \
1485	if (outX % mosaicH) { \
1486		inX += (mosaicH - (outX % mosaicH)) * xOffset; \
1487		outX += mosaicH - (outX % mosaicH); \
1488	} \
1489	for (; outX < condition; ++outX, inX += xOffset) { \
1490		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1491			continue; \
1492		} \
1493		int localX = inX - xOffset * (outX % mosaicH); \
1494		SPRITE_XBASE_ ## DEPTH(localX); \
1495		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1496	}
1497
1498#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
1499	unsigned tileData; \
1500	for (; outX < x + totalWidth && outX < end; ++outX, ++inX) { \
1501		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1502			continue; \
1503		} \
1504		xAccum += mat.a; \
1505		yAccum += mat.c; \
1506		int localX = (xAccum >> 8) + (width >> 1); \
1507		int localY = (yAccum >> 8) + (height >> 1); \
1508		\
1509		if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
1510			continue; \
1511		} \
1512		\
1513		SPRITE_YBASE_ ## DEPTH(localY); \
1514		SPRITE_XBASE_ ## DEPTH(localX); \
1515		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1516	}
1517
1518#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
1519#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width >> 1 : 0x80) + (localY & 0x7) * 4;
1520
1521#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
1522	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1523	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1524	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1525		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1526	}
1527
1528#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
1529	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1530	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1531	if (tileData) { \
1532		renderer->row[outX] |= FLAG_OBJWIN; \
1533	}
1534
1535#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
1536#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width : 0x80) + (localY & 0x7) * 8;
1537
1538#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
1539	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1540	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1541	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1542		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1543	}
1544
1545#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
1546	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1547	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1548	if (tileData) { \
1549		renderer->row[outX] |= FLAG_OBJWIN; \
1550	}
1551
1552static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
1553	int width = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2];
1554	int height = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2 + 1];
1555	int start = renderer->start;
1556	int end = renderer->end;
1557	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
1558	flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1559	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
1560	int32_t x = GBAObjAttributesBGetX(sprite->b) << 23;
1561	x >>= 23;
1562	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
1563	unsigned charBase = GBAObjAttributesCGetTile(sprite->c) * 0x20;
1564	int variant = renderer->target1Obj && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1565	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1566		// Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1567		variant = 0;
1568	}
1569	color_t* palette = &renderer->normalPalette[0x100];
1570	if (variant) {
1571		palette = &renderer->variantPalette[0x100];
1572	}
1573
1574	int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
1575
1576	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1577		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
1578		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
1579		struct GBAOAMMatrix mat;
1580		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1581		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1582		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1583		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1584
1585		if (inY < 0) {
1586			inY += 256;
1587		}
1588		int outX = x >= start ? x : start;
1589		int inX = outX - x;
1590		int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1));
1591		int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1));
1592
1593		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1594			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1595			if (flags & FLAG_OBJWIN) {
1596				SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
1597			} else {
1598				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
1599			}
1600		} else {
1601			if (flags & FLAG_OBJWIN) {
1602				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
1603			} else {
1604				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
1605			}
1606		}
1607	} else {
1608		int outX = x >= start ? x : start;
1609		int condition = x + width;
1610		int mosaicH = 1;
1611		if (GBAObjAttributesAIsMosaic(sprite->a)) {
1612			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
1613			if (condition % mosaicH) {
1614				condition += mosaicH - (condition % mosaicH);
1615			}
1616		}
1617		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
1618			inY += 256;
1619		}
1620		if (GBAObjAttributesBIsVFlip(sprite->b)) {
1621			inY = height - inY - 1;
1622		}
1623		if (end < condition) {
1624			condition = end;
1625		}
1626		int inX = outX - x;
1627		int xOffset = 1;
1628		if (GBAObjAttributesBIsHFlip(sprite->b)) {
1629			inX = width - inX - 1;
1630			xOffset = -1;
1631		}
1632		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1633			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1634			if (flags & FLAG_OBJWIN) {
1635				SPRITE_NORMAL_LOOP(16, OBJWIN);
1636			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1637				SPRITE_MOSAIC_LOOP(16, NORMAL);
1638			} else {
1639				SPRITE_NORMAL_LOOP(16, NORMAL);
1640			}
1641		} else {
1642			if (flags & FLAG_OBJWIN) {
1643				SPRITE_NORMAL_LOOP(256, OBJWIN);
1644			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1645				SPRITE_MOSAIC_LOOP(256, NORMAL);
1646			} else {
1647				SPRITE_NORMAL_LOOP(256, NORMAL);
1648			}
1649		}
1650	}
1651	return 1;
1652}
1653
1654static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
1655	int x;
1656	uint32_t* pixel = &renderer->row[renderer->start];
1657	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
1658
1659	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
1660	bool objwinDisable = false;
1661	bool objwinOnly = false;
1662	if (objwinSlowPath) {
1663		objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
1664		objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
1665
1666		if (objwinDisable) {
1667			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
1668				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1669				uint32_t current = *pixel;
1670				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1671					_compositeBlendObjwin(renderer, pixel, color | flags, current);
1672				}
1673			}
1674			return;
1675		} else if (objwinOnly) {
1676			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
1677				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1678				uint32_t current = *pixel;
1679				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1680					_compositeBlendObjwin(renderer, pixel, color | flags, current);
1681				}
1682			}
1683			return;
1684		} else {
1685			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
1686				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1687				uint32_t current = *pixel;
1688				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1689					_compositeBlendObjwin(renderer, pixel, color | flags, current);
1690				}
1691			}
1692			return;
1693		}
1694	}
1695	for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
1696		uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1697		uint32_t current = *pixel;
1698		if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1699			_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
1700		}
1701	}
1702}
1703
1704static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
1705	int i;
1706	if (renderer->blendEffect == BLEND_BRIGHTEN) {
1707		for (i = 0; i < 512; ++i) {
1708			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
1709		}
1710	} else if (renderer->blendEffect == BLEND_DARKEN) {
1711		for (i = 0; i < 512; ++i) {
1712			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
1713		}
1714	} else {
1715		for (i = 0; i < 512; ++i) {
1716			renderer->variantPalette[i] = renderer->normalPalette[i];
1717		}
1718	}
1719}
1720
1721static inline unsigned _brighten(unsigned color, int y) {
1722	unsigned c = 0;
1723	unsigned a;
1724#ifdef COLOR_16_BIT
1725	a = color & 0x1F;
1726	c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
1727
1728#ifdef COLOR_5_6_5
1729	a = color & 0x7C0;
1730	c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
1731
1732	a = color & 0xF800;
1733	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1734#else
1735	a = color & 0x3E0;
1736	c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
1737
1738	a = color & 0x7C00;
1739	c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
1740#endif
1741#else
1742	a = color & 0xF8;
1743	c |= (a + ((0xF8 - a) * y) / 16) & 0xF8;
1744
1745	a = color & 0xF800;
1746	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1747
1748	a = color & 0xF80000;
1749	c |= (a + ((0xF80000 - a) * y) / 16) & 0xF80000;
1750#endif
1751	return c;
1752}
1753
1754static inline unsigned _darken(unsigned color, int y) {
1755	unsigned c = 0;
1756	unsigned a;
1757#ifdef COLOR_16_BIT
1758	a = color & 0x1F;
1759	c |= (a - (a * y) / 16) & 0x1F;
1760
1761#ifdef COLOR_5_6_5
1762	a = color & 0x7C0;
1763	c |= (a - (a * y) / 16) & 0x7C0;
1764
1765	a = color & 0xF800;
1766	c |= (a - (a * y) / 16) & 0xF800;
1767#else
1768	a = color & 0x3E0;
1769	c |= (a - (a * y) / 16) & 0x3E0;
1770
1771	a = color & 0x7C00;
1772	c |= (a - (a * y) / 16) & 0x7C00;
1773#endif
1774#else
1775	a = color & 0xF8;
1776	c |= (a - (a * y) / 16) & 0xF8;
1777
1778	a = color & 0xF800;
1779	c |= (a - (a * y) / 16) & 0xF800;
1780
1781	a = color & 0xF80000;
1782	c |= (a - (a * y) / 16) & 0xF80000;
1783#endif
1784	return c;
1785}
1786
1787static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
1788	unsigned c = 0;
1789	unsigned a, b;
1790#ifdef COLOR_16_BIT
1791#ifdef COLOR_5_6_5
1792	a = colorA & 0xF81F;
1793	b = colorB & 0xF81F;
1794	a |= (colorA & 0x7C0) << 16;
1795	b |= (colorB & 0x7C0) << 16;
1796	c = ((a * weightA + b * weightB) / 16);
1797	if (c & 0x08000000) {
1798		c = (c & ~0x0FC00000) | 0x07C00000;
1799	}
1800	if (c & 0x0020) {
1801		c = (c & ~0x003F) | 0x001F;
1802	}
1803	if (c & 0x10000) {
1804		c = (c & ~0x1F800) | 0xF800;
1805	}
1806	c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
1807#else
1808	a = colorA & 0x7C1F;
1809	b = colorB & 0x7C1F;
1810	a |= (colorA & 0x3E0) << 16;
1811	b |= (colorB & 0x3E0) << 16;
1812	c = ((a * weightA + b * weightB) / 16);
1813	if (c & 0x04000000) {
1814		c = (c & ~0x07E00000) | 0x03E00000;
1815	}
1816	if (c & 0x0020) {
1817		c = (c & ~0x003F) | 0x001F;
1818	}
1819	if (c & 0x10000) {
1820		c = (c & ~0x1F800) | 0xF800;
1821	}
1822	c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
1823#endif
1824#else
1825	a = colorA & 0xF8;
1826	b = colorB & 0xF8;
1827	c |= ((a * weightA + b * weightB) / 16) & 0x1F8;
1828	if (c & 0x00000100) {
1829		c = 0x000000F8;
1830	}
1831
1832	a = colorA & 0xF800;
1833	b = colorB & 0xF800;
1834	c |= ((a * weightA + b * weightB) / 16) & 0x1F800;
1835	if (c & 0x00010000) {
1836		c = (c & 0x000000F8) | 0x0000F800;
1837	}
1838
1839	a = colorA & 0xF80000;
1840	b = colorB & 0xF80000;
1841	c |= ((a * weightA + b * weightB) / 16) & 0x1F80000;
1842	if (c & 0x01000000) {
1843		c = (c & 0x0000F8F8) | 0x00F80000;
1844	}
1845#endif
1846	return c;
1847}