all repos — mgba @ 881dc1d8a3167dc0269e9ecbd4ff5af9a89668a9

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