all repos — mgba @ 89f7a69f3a000bfa8c0e1cfb4a84f71ad5c940d2

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