all repos — mgba @ 51a122f20d45119f34cca7e920c0eb9dacc15902

mGBA Game Boy Advance Emulator

src/gb/mbc.c (view raw)

   1/* Copyright (c) 2013-2016 Jeffrey Pfau
   2 *
   3 * This Source Code Form is subject to the terms of the Mozilla Public
   4 * License, v. 2.0. If a copy of the MPL was not distributed with this
   5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
   6#include <mgba/internal/gb/mbc.h>
   7
   8#include <mgba/core/interface.h>
   9#include <mgba/internal/sm83/sm83.h>
  10#include <mgba/internal/gb/gb.h>
  11#include <mgba/internal/gb/memory.h>
  12#include <mgba-util/crc32.h>
  13#include <mgba-util/vfs.h>
  14
  15const uint32_t GB_LOGO_HASH = 0x46195417;
  16
  17mLOG_DEFINE_CATEGORY(GB_MBC, "GB MBC", "gb.mbc");
  18
  19static void _GBMBCNone(struct GB* gb, uint16_t address, uint8_t value) {
  20	UNUSED(gb);
  21	UNUSED(address);
  22	UNUSED(value);
  23
  24	mLOG(GB_MBC, GAME_ERROR, "Wrote to invalid MBC");
  25}
  26
  27static void _GBMBC1(struct GB*, uint16_t address, uint8_t value);
  28static void _GBMBC2(struct GB*, uint16_t address, uint8_t value);
  29static void _GBMBC3(struct GB*, uint16_t address, uint8_t value);
  30static void _GBMBC5(struct GB*, uint16_t address, uint8_t value);
  31static void _GBMBC6(struct GB*, uint16_t address, uint8_t value);
  32static void _GBMBC7(struct GB*, uint16_t address, uint8_t value);
  33static void _GBMMM01(struct GB*, uint16_t address, uint8_t value);
  34static void _GBHuC1(struct GB*, uint16_t address, uint8_t value);
  35static void _GBHuC3(struct GB*, uint16_t address, uint8_t value);
  36static void _GBPocketCam(struct GB* gb, uint16_t address, uint8_t value);
  37static void _GBTAMA5(struct GB* gb, uint16_t address, uint8_t value);
  38static void _GBWisdomTree(struct GB* gb, uint16_t address, uint8_t value);
  39
  40static uint8_t _GBMBC2Read(struct GBMemory*, uint16_t address);
  41static uint8_t _GBMBC6Read(struct GBMemory*, uint16_t address);
  42static uint8_t _GBMBC7Read(struct GBMemory*, uint16_t address);
  43static void _GBMBC7Write(struct GBMemory* memory, uint16_t address, uint8_t value);
  44
  45static uint8_t _GBTAMA5Read(struct GBMemory*, uint16_t address);
  46
  47static uint8_t _GBPocketCamRead(struct GBMemory*, uint16_t address);
  48static void _GBPocketCamCapture(struct GBMemory*);
  49
  50void GBMBCSwitchBank(struct GB* gb, int bank) {
  51	size_t bankStart = bank * GB_SIZE_CART_BANK0;
  52	if (bankStart + GB_SIZE_CART_BANK0 > gb->memory.romSize) {
  53		mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid ROM bank: %0X", bank);
  54		bankStart &= (gb->memory.romSize - 1);
  55		bank = bankStart / GB_SIZE_CART_BANK0;
  56	}
  57	gb->memory.romBank = &gb->memory.rom[bankStart];
  58	gb->memory.currentBank = bank;
  59	if (gb->cpu->pc < GB_BASE_VRAM) {
  60		gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
  61	}
  62}
  63
  64void GBMBCSwitchBank0(struct GB* gb, int bank) {
  65	size_t bankStart = bank * GB_SIZE_CART_BANK0;
  66	if (bankStart + GB_SIZE_CART_BANK0 > gb->memory.romSize) {
  67		mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid ROM bank: %0X", bank);
  68		bankStart &= (gb->memory.romSize - 1);
  69	}
  70	gb->memory.romBase = &gb->memory.rom[bankStart];
  71	if (gb->cpu->pc < GB_SIZE_CART_BANK0) {
  72		gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
  73	}
  74}
  75
  76void GBMBCSwitchHalfBank(struct GB* gb, int half, int bank) {
  77	size_t bankStart = bank * GB_SIZE_CART_HALFBANK;
  78	if (bankStart + GB_SIZE_CART_HALFBANK > gb->memory.romSize) {
  79		mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid ROM bank: %0X", bank);
  80		bankStart &= (gb->memory.romSize - 1);
  81		bank = bankStart / GB_SIZE_CART_HALFBANK;
  82		if (!bank) {
  83			++bank;
  84		}
  85	}
  86	if (!half) {
  87		gb->memory.romBank = &gb->memory.rom[bankStart];
  88		gb->memory.currentBank = bank;
  89	} else {
  90		gb->memory.mbcState.mbc6.romBank1 = &gb->memory.rom[bankStart];
  91		gb->memory.mbcState.mbc6.currentBank1 = bank;
  92	}
  93	if (gb->cpu->pc < GB_BASE_VRAM) {
  94		gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
  95	}
  96}
  97
  98static bool _isMulticart(const uint8_t* mem) {
  99	bool success;
 100	struct VFile* vf;
 101
 102	vf = VFileFromConstMemory(&mem[GB_SIZE_CART_BANK0 * 0x10], 1024);
 103	success = GBIsROM(vf);
 104	vf->close(vf);
 105
 106	if (!success) {
 107		return false;
 108	}
 109
 110	vf = VFileFromConstMemory(&mem[GB_SIZE_CART_BANK0 * 0x20], 1024);
 111	success = GBIsROM(vf);
 112	vf->close(vf);
 113
 114	if (!success) {
 115		vf = VFileFromConstMemory(&mem[GB_SIZE_CART_BANK0 * 0x30], 1024);
 116		success = GBIsROM(vf);
 117		vf->close(vf);
 118	}
 119	
 120	return success;
 121}
 122
 123static bool _isWisdomTree(const uint8_t* mem, size_t size) {
 124	size_t i;
 125	for (i = 0x134; i < 0x14C; i += 4) {
 126		if (*(uint32_t*) &mem[i] != 0) {
 127			return false;
 128		}
 129	}
 130	for (i = 0xF0; i < 0x100; i += 4) {
 131		if (*(uint32_t*) &mem[i] != 0) {
 132			return false;
 133		}
 134	}
 135	if (mem[0x14D] != 0xE7) {
 136		return false;
 137	}
 138	for (i = 0x300; i < size - 11; ++i) {
 139		if (memcmp(&mem[i], "WISDOM", 6) == 0 && memcmp(&mem[i + 7], "TREE", 4) == 0) {
 140			return true;
 141		}
 142	}
 143	return false;
 144}
 145
 146void GBMBCSwitchSramBank(struct GB* gb, int bank) {
 147	size_t bankStart = bank * GB_SIZE_EXTERNAL_RAM;
 148	if (bankStart + GB_SIZE_EXTERNAL_RAM > gb->sramSize) {
 149		mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid RAM bank: %0X", bank);
 150		bankStart &= (gb->sramSize - 1);
 151		bank = bankStart / GB_SIZE_EXTERNAL_RAM;
 152	}
 153	gb->memory.sramBank = &gb->memory.sram[bankStart];
 154	gb->memory.sramCurrentBank = bank;
 155}
 156
 157void GBMBCSwitchSramHalfBank(struct GB* gb, int half, int bank) {
 158	size_t bankStart = bank * GB_SIZE_EXTERNAL_RAM_HALFBANK;
 159	if (bankStart + GB_SIZE_EXTERNAL_RAM_HALFBANK > gb->sramSize) {
 160		mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid RAM bank: %0X", bank);
 161		bankStart &= (gb->sramSize - 1);
 162		bank = bankStart / GB_SIZE_EXTERNAL_RAM_HALFBANK;
 163	}
 164	if (!half) {
 165		gb->memory.sramBank = &gb->memory.sram[bankStart];
 166		gb->memory.sramCurrentBank = bank;
 167	} else {
 168		gb->memory.mbcState.mbc6.sramBank1 = &gb->memory.sram[bankStart];
 169		gb->memory.mbcState.mbc6.currentSramBank1 = bank;
 170	}
 171}
 172
 173void GBMBCInit(struct GB* gb) {
 174	const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
 175	if (gb->memory.rom) {
 176		if (gb->memory.romSize >= 0x8000) {
 177			const struct GBCartridge* cartFooter = (const struct GBCartridge*) &gb->memory.rom[gb->memory.romSize - 0x7F00];
 178			if (doCrc32(cartFooter->logo, sizeof(cartFooter->logo)) == GB_LOGO_HASH && cartFooter->type >= 0x0B && cartFooter->type <= 0x0D) {
 179				cart = cartFooter;
 180			}
 181		}
 182		switch (cart->ramSize) {
 183		case 0:
 184			gb->sramSize = 0;
 185			break;
 186		case 1:
 187			gb->sramSize = 0x800;
 188			break;
 189		default:
 190		case 2:
 191			gb->sramSize = 0x2000;
 192			break;
 193		case 3:
 194			gb->sramSize = 0x8000;
 195			break;
 196		case 4:
 197			gb->sramSize = 0x20000;
 198			break;
 199		case 5:
 200			gb->sramSize = 0x10000;
 201			break;
 202		}
 203
 204		if (gb->memory.mbcType == GB_MBC_AUTODETECT) {
 205			switch (cart->type) {
 206			case 0:
 207				if (_isWisdomTree(gb->memory.rom, gb->memory.romSize)) {
 208					gb->memory.mbcType = GB_UNL_WISDOM_TREE;
 209					break;
 210				}
 211				// Fall through
 212			case 8:
 213			case 9:
 214				gb->memory.mbcType = GB_MBC_NONE;
 215				break;
 216			case 1:
 217			case 2:
 218			case 3:
 219				gb->memory.mbcType = GB_MBC1;
 220				break;
 221			case 5:
 222			case 6:
 223				gb->memory.mbcType = GB_MBC2;
 224				break;
 225			case 0x0B:
 226			case 0x0C:
 227			case 0x0D:
 228				gb->memory.mbcType = GB_MMM01;
 229				break;
 230			case 0x0F:
 231			case 0x10:
 232				gb->memory.mbcType = GB_MBC3_RTC;
 233				break;
 234			case 0x11:
 235			case 0x12:
 236			case 0x13:
 237				gb->memory.mbcType = GB_MBC3;
 238				break;
 239			default:
 240				mLOG(GB_MBC, WARN, "Unknown MBC type: %02X", cart->type);
 241				// Fall through
 242			case 0x19:
 243			case 0x1A:
 244			case 0x1B:
 245				gb->memory.mbcType = GB_MBC5;
 246				break;
 247			case 0x1C:
 248			case 0x1D:
 249			case 0x1E:
 250				gb->memory.mbcType = GB_MBC5_RUMBLE;
 251				break;
 252			case 0x20:
 253				gb->memory.mbcType = GB_MBC6;
 254				break;
 255			case 0x22:
 256				gb->memory.mbcType = GB_MBC7;
 257				break;
 258			case 0xFC:
 259				gb->memory.mbcType = GB_POCKETCAM;
 260				break;
 261			case 0xFD:
 262				gb->memory.mbcType = GB_TAMA5;
 263				break;
 264			case 0xFE:
 265				gb->memory.mbcType = GB_HuC3;
 266				break;
 267			case 0xFF:
 268				gb->memory.mbcType = GB_HuC1;
 269				break;
 270			}
 271		}
 272	} else {
 273		gb->memory.mbcType = GB_MBC_NONE;
 274	}
 275	gb->memory.mbcRead = NULL;
 276	switch (gb->memory.mbcType) {
 277	case GB_MBC_NONE:
 278		gb->memory.mbcWrite = _GBMBCNone;
 279		break;
 280	case GB_MBC1:
 281		gb->memory.mbcWrite = _GBMBC1;
 282		if (gb->memory.romSize >= GB_SIZE_CART_BANK0 * 0x31 && _isMulticart(gb->memory.rom)) {
 283			gb->memory.mbcState.mbc1.multicartStride = 4;
 284		} else {
 285			gb->memory.mbcState.mbc1.multicartStride = 5;
 286		}
 287		break;
 288	case GB_MBC2:
 289		gb->memory.mbcWrite = _GBMBC2;
 290		gb->memory.mbcRead = _GBMBC2Read;
 291		gb->sramSize = 0x100;
 292		break;
 293	case GB_MBC3:
 294		gb->memory.mbcWrite = _GBMBC3;
 295		break;
 296	default:
 297		mLOG(GB_MBC, WARN, "Unknown MBC type: %02X", cart->type);
 298		// Fall through
 299	case GB_MBC5:
 300		gb->memory.mbcWrite = _GBMBC5;
 301		break;
 302	case GB_MBC6:
 303		mLOG(GB_MBC, WARN, "unimplemented MBC: MBC6");
 304		gb->memory.mbcWrite = _GBMBC6;
 305		gb->memory.mbcRead = _GBMBC6Read;
 306		break;
 307	case GB_MBC7:
 308		gb->memory.mbcWrite = _GBMBC7;
 309		gb->memory.mbcRead = _GBMBC7Read;
 310		gb->sramSize = 0x100;
 311		break;
 312	case GB_MMM01:
 313		gb->memory.mbcWrite = _GBMMM01;
 314		break;
 315	case GB_HuC1:
 316		gb->memory.mbcWrite = _GBHuC1;
 317		break;
 318	case GB_HuC3:
 319		gb->memory.mbcWrite = _GBHuC3;
 320		break;
 321	case GB_TAMA5:
 322		mLOG(GB_MBC, WARN, "unimplemented MBC: TAMA5");
 323		memset(gb->memory.rtcRegs, 0, sizeof(gb->memory.rtcRegs));
 324		gb->memory.mbcWrite = _GBTAMA5;
 325		gb->memory.mbcRead = _GBTAMA5Read;
 326		gb->sramSize = 0x20;
 327		break;
 328	case GB_MBC3_RTC:
 329		memset(gb->memory.rtcRegs, 0, sizeof(gb->memory.rtcRegs));
 330		gb->memory.mbcWrite = _GBMBC3;
 331		break;
 332	case GB_MBC5_RUMBLE:
 333		gb->memory.mbcWrite = _GBMBC5;
 334		break;
 335	case GB_POCKETCAM:
 336		gb->memory.mbcWrite = _GBPocketCam;
 337		gb->memory.mbcRead = _GBPocketCamRead;
 338		if (gb->memory.cam && gb->memory.cam->startRequestImage) {
 339			gb->memory.cam->startRequestImage(gb->memory.cam, GBCAM_WIDTH, GBCAM_HEIGHT, mCOLOR_ANY);
 340		}
 341		break;
 342	case GB_UNL_WISDOM_TREE:
 343		gb->memory.mbcWrite = _GBWisdomTree;
 344		break;
 345	}
 346
 347	gb->memory.currentBank = 1;
 348	gb->memory.sramCurrentBank = 0;
 349	gb->memory.sramAccess = false;
 350	gb->memory.rtcAccess = false;
 351	gb->memory.activeRtcReg = 0;
 352	gb->memory.rtcLatched = false;
 353	gb->memory.rtcLastLatch = 0;
 354	if (gb->memory.rtc) {
 355		if (gb->memory.rtc->sample) {
 356			gb->memory.rtc->sample(gb->memory.rtc);
 357		}
 358		gb->memory.rtcLastLatch = gb->memory.rtc->unixTime(gb->memory.rtc);
 359	} else {
 360		gb->memory.rtcLastLatch = time(0);
 361	}
 362	memset(&gb->memory.rtcRegs, 0, sizeof(gb->memory.rtcRegs));
 363
 364	GBResizeSram(gb, gb->sramSize);
 365
 366	if (gb->memory.mbcType == GB_MBC3_RTC) {
 367		GBMBCRTCRead(gb);
 368	}
 369}
 370
 371static void _latchRtc(struct mRTCSource* rtc, uint8_t* rtcRegs, time_t* rtcLastLatch) {
 372	time_t t;
 373	if (rtc) {
 374		if (rtc->sample) {
 375			rtc->sample(rtc);
 376		}
 377		t = rtc->unixTime(rtc);
 378	} else {
 379		t = time(0);
 380	}
 381	time_t currentLatch = t;
 382	t -= *rtcLastLatch;
 383	*rtcLastLatch = currentLatch;
 384
 385	int64_t diff;
 386	diff = rtcRegs[0] + t % 60;
 387	if (diff < 0) {
 388		diff += 60;
 389		t -= 60;
 390	}
 391	rtcRegs[0] = diff % 60;
 392	t /= 60;
 393	t += diff / 60;
 394
 395	diff = rtcRegs[1] + t % 60;
 396	if (diff < 0) {
 397		diff += 60;
 398		t -= 60;
 399	}
 400	rtcRegs[1] = diff % 60;
 401	t /= 60;
 402	t += diff / 60;
 403
 404	diff = rtcRegs[2] + t % 24;
 405	if (diff < 0) {
 406		diff += 24;
 407		t -= 24;
 408	}
 409	rtcRegs[2] = diff % 24;
 410	t /= 24;
 411	t += diff / 24;
 412
 413	diff = rtcRegs[3] + ((rtcRegs[4] & 1) << 8) + (t & 0x1FF);
 414	rtcRegs[3] = diff;
 415	rtcRegs[4] &= 0xFE;
 416	rtcRegs[4] |= (diff >> 8) & 1;
 417	if (diff & 0x200) {
 418		rtcRegs[4] |= 0x80;
 419	}
 420}
 421
 422void _GBMBC1(struct GB* gb, uint16_t address, uint8_t value) {
 423	struct GBMemory* memory = &gb->memory;
 424	int bank = value & 0x1F;
 425	int stride = 1 << memory->mbcState.mbc1.multicartStride;
 426	switch (address >> 13) {
 427	case 0x0:
 428		switch (value) {
 429		case 0:
 430			memory->sramAccess = false;
 431			break;
 432		case 0xA:
 433			memory->sramAccess = true;
 434			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
 435			break;
 436		default:
 437			// TODO
 438			mLOG(GB_MBC, STUB, "MBC1 unknown value %02X", value);
 439			break;
 440		}
 441		break;
 442	case 0x1:
 443		if (!bank) {
 444			++bank;
 445		}
 446		bank &= stride - 1;
 447		GBMBCSwitchBank(gb, bank | (memory->currentBank & (3 * stride)));
 448		break;
 449	case 0x2:
 450		bank &= 3;
 451		if (memory->mbcState.mbc1.mode) {
 452			GBMBCSwitchBank0(gb, bank << gb->memory.mbcState.mbc1.multicartStride);
 453			GBMBCSwitchSramBank(gb, bank);
 454		}
 455		GBMBCSwitchBank(gb, (bank << memory->mbcState.mbc1.multicartStride) | (memory->currentBank & (stride - 1)));
 456		break;
 457	case 0x3:
 458		memory->mbcState.mbc1.mode = value & 1;
 459		if (memory->mbcState.mbc1.mode) {
 460			GBMBCSwitchBank0(gb, memory->currentBank & ~((1 << memory->mbcState.mbc1.multicartStride) - 1));
 461		} else {
 462			GBMBCSwitchBank0(gb, 0);
 463			GBMBCSwitchSramBank(gb, 0);
 464		}
 465		break;
 466	default:
 467		// TODO
 468		mLOG(GB_MBC, STUB, "MBC1 unknown address: %04X:%02X", address, value);
 469		break;
 470	}
 471}
 472
 473void _GBMBC2(struct GB* gb, uint16_t address, uint8_t value) {
 474	struct GBMemory* memory = &gb->memory;
 475	int shift = (address & 1) * 4;
 476	int bank = value & 0xF;
 477	switch (address >> 13) {
 478	case 0x0:
 479		switch (value) {
 480		case 0:
 481			memory->sramAccess = false;
 482			break;
 483		case 0xA:
 484			memory->sramAccess = true;
 485			break;
 486		default:
 487			// TODO
 488			mLOG(GB_MBC, STUB, "MBC1 unknown value %02X", value);
 489			break;
 490		}
 491		break;
 492	case 0x1:
 493		if (!bank) {
 494			++bank;
 495		}
 496		GBMBCSwitchBank(gb, bank);
 497		break;
 498	case 0x5:
 499		if (!memory->sramAccess) {
 500			return;
 501		}
 502		address &= 0x1FF;
 503		memory->sramBank[(address >> 1)] &= 0xF0 >> shift;
 504		memory->sramBank[(address >> 1)] |= (value & 0xF) << shift;
 505		break;
 506	default:
 507		// TODO
 508		mLOG(GB_MBC, STUB, "MBC2 unknown address: %04X:%02X", address, value);
 509		break;
 510	}
 511}
 512
 513static uint8_t _GBMBC2Read(struct GBMemory* memory, uint16_t address) {
 514	address &= 0x1FF;
 515	int shift = (address & 1) * 4;
 516	return (memory->sramBank[(address >> 1)] >> shift) | 0xF0;
 517}
 518
 519void _GBMBC3(struct GB* gb, uint16_t address, uint8_t value) {
 520	struct GBMemory* memory = &gb->memory;
 521	int bank = value & 0x7F;
 522	switch (address >> 13) {
 523	case 0x0:
 524		switch (value) {
 525		case 0:
 526			memory->sramAccess = false;
 527			break;
 528		case 0xA:
 529			memory->sramAccess = true;
 530			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
 531			break;
 532		default:
 533			// TODO
 534			mLOG(GB_MBC, STUB, "MBC3 unknown value %02X", value);
 535			break;
 536		}
 537		break;
 538	case 0x1:
 539		if (!bank) {
 540			++bank;
 541		}
 542		GBMBCSwitchBank(gb, bank);
 543		break;
 544	case 0x2:
 545		if (value < 8) {
 546			GBMBCSwitchSramBank(gb, value);
 547			memory->rtcAccess = false;
 548		} else if (value <= 0xC) {
 549			memory->activeRtcReg = value - 8;
 550			memory->rtcAccess = true;
 551		}
 552		break;
 553	case 0x3:
 554		if (memory->rtcLatched && value == 0) {
 555			memory->rtcLatched = false;
 556		} else if (!memory->rtcLatched && value == 1) {
 557			_latchRtc(gb->memory.rtc, gb->memory.rtcRegs, &gb->memory.rtcLastLatch);
 558			memory->rtcLatched = true;
 559		}
 560		break;
 561	}
 562}
 563
 564void _GBMBC5(struct GB* gb, uint16_t address, uint8_t value) {
 565	struct GBMemory* memory = &gb->memory;
 566	int bank;
 567	switch (address >> 12) {
 568	case 0x0:
 569	case 0x1:
 570		switch (value) {
 571		case 0:
 572			memory->sramAccess = false;
 573			break;
 574		case 0xA:
 575			memory->sramAccess = true;
 576			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
 577			break;
 578		default:
 579			// TODO
 580			mLOG(GB_MBC, STUB, "MBC5 unknown value %02X", value);
 581			break;
 582		}
 583		break;
 584	case 0x2:
 585		bank = (memory->currentBank & 0x100) | value;
 586		GBMBCSwitchBank(gb, bank);
 587		break;
 588	case 0x3:
 589		bank = (memory->currentBank & 0xFF) | ((value & 1) << 8);
 590		GBMBCSwitchBank(gb, bank);
 591		break;
 592	case 0x4:
 593	case 0x5:
 594		if (memory->mbcType == GB_MBC5_RUMBLE && memory->rumble) {
 595			memory->rumble->setRumble(memory->rumble, (value >> 3) & 1);
 596			value &= ~8;
 597		}
 598		GBMBCSwitchSramBank(gb, value & 0xF);
 599		break;
 600	default:
 601		// TODO
 602		mLOG(GB_MBC, STUB, "MBC5 unknown address: %04X:%02X", address, value);
 603		break;
 604	}
 605}
 606
 607void _GBMBC6(struct GB* gb, uint16_t address, uint8_t value) {
 608	struct GBMemory* memory = &gb->memory;
 609	int bank = value;
 610	switch (address >> 10) {
 611	case 0:
 612		switch (value) {
 613		case 0:
 614			memory->mbcState.mbc6.sramAccess = false;
 615			break;
 616		case 0xA:
 617			memory->mbcState.mbc6.sramAccess = true;
 618			break;
 619		default:
 620			// TODO
 621			mLOG(GB_MBC, STUB, "MBC6 unknown value %02X", value);
 622			break;
 623		}
 624		break;
 625	case 0x1:
 626		GBMBCSwitchSramHalfBank(gb, 0, bank);
 627		break;
 628	case 0x2:
 629		GBMBCSwitchSramHalfBank(gb, 1, bank);
 630		break;
 631	case 0x8:
 632	case 0x9:
 633		GBMBCSwitchHalfBank(gb, 0, bank);
 634		break;
 635	case 0xC:
 636	case 0xD:
 637		GBMBCSwitchHalfBank(gb, 1, bank);
 638		break;
 639	case 0x28:
 640	case 0x29:
 641	case 0x2A:
 642	case 0x2B:
 643		if (memory->mbcState.mbc6.sramAccess) {
 644			memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM_HALFBANK - 1)] = value;
 645		}
 646		break;
 647	case 0x2C:
 648	case 0x2D:
 649	case 0x2E:
 650	case 0x2F:
 651		if (memory->mbcState.mbc6.sramAccess) {
 652			memory->mbcState.mbc6.sramBank1[address & (GB_SIZE_EXTERNAL_RAM_HALFBANK - 1)] = value;
 653		}
 654		break;
 655	default:
 656		mLOG(GB_MBC, STUB, "MBC6 unknown address: %04X:%02X", address, value);
 657		break;
 658	}
 659}
 660
 661uint8_t _GBMBC6Read(struct GBMemory* memory, uint16_t address) {
 662	if (!memory->mbcState.mbc6.sramAccess) {
 663		return 0xFF;
 664	}
 665	switch (address >> 12) {
 666	case 0xA:
 667		return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM_HALFBANK - 1)];
 668	case 0xB:
 669		return memory->mbcState.mbc6.sramBank1[address & (GB_SIZE_EXTERNAL_RAM_HALFBANK - 1)];
 670	}
 671	return 0xFF;
 672}
 673
 674void _GBMBC7(struct GB* gb, uint16_t address, uint8_t value) {
 675	int bank = value & 0x7F;
 676	switch (address >> 13) {
 677	case 0x0:
 678		switch (value) {
 679		default:
 680		case 0:
 681			gb->memory.mbcState.mbc7.access = 0;
 682			break;
 683		case 0xA:
 684			gb->memory.mbcState.mbc7.access |= 1;
 685			break;
 686		}
 687		break;
 688	case 0x1:
 689		GBMBCSwitchBank(gb, bank);
 690		break;
 691	case 0x2:
 692		if (value == 0x40) {
 693			gb->memory.mbcState.mbc7.access |= 2;
 694		} else {
 695			gb->memory.mbcState.mbc7.access &= ~2;
 696		}
 697		break;
 698	case 0x5:
 699		_GBMBC7Write(&gb->memory, address, value);
 700		break;
 701	default:
 702		// TODO
 703		mLOG(GB_MBC, STUB, "MBC7 unknown address: %04X:%02X", address, value);
 704		break;
 705	}
 706}
 707
 708uint8_t _GBMBC7Read(struct GBMemory* memory, uint16_t address) {
 709	struct GBMBC7State* mbc7 = &memory->mbcState.mbc7;
 710	if (mbc7->access != 3) {
 711		return 0xFF;
 712	}
 713	switch (address & 0xF0) {
 714	case 0x20:
 715		if (memory->rotation && memory->rotation->readTiltX) {
 716			int32_t x = -memory->rotation->readTiltX(memory->rotation);
 717			x >>= 21;
 718			x += 0x81D0;
 719			return x;
 720		}
 721		return 0xFF;
 722	case 0x30:
 723		if (memory->rotation && memory->rotation->readTiltX) {
 724			int32_t x = -memory->rotation->readTiltX(memory->rotation);
 725			x >>= 21;
 726			x += 0x81D0;
 727			return x >> 8;
 728		}
 729		return 7;
 730	case 0x40:
 731		if (memory->rotation && memory->rotation->readTiltY) {
 732			int32_t y = -memory->rotation->readTiltY(memory->rotation);
 733			y >>= 21;
 734			y += 0x81D0;
 735			return y;
 736		}
 737		return 0xFF;
 738	case 0x50:
 739		if (memory->rotation && memory->rotation->readTiltY) {
 740			int32_t y = -memory->rotation->readTiltY(memory->rotation);
 741			y >>= 21;
 742			y += 0x81D0;
 743			return y >> 8;
 744		}
 745		return 7;
 746	case 0x60:
 747		return 0;
 748	case 0x80:
 749		return mbc7->eeprom;
 750	default:
 751		return 0xFF;
 752	}
 753}
 754
 755static void _GBMBC7Write(struct GBMemory* memory, uint16_t address, uint8_t value) {
 756	struct GBMBC7State* mbc7 = &memory->mbcState.mbc7;
 757	if (mbc7->access != 3) {
 758		return;
 759	}
 760	switch (address & 0xF0) {
 761	case 0x00:
 762		mbc7->latch = (value & 0x55) == 0x55;
 763		return;
 764	case 0x10:
 765		mbc7->latch |= (value & 0xAA);
 766		if (mbc7->latch == 0xAB && memory->rotation && memory->rotation->sample) {
 767			memory->rotation->sample(memory->rotation);
 768		}
 769		mbc7->latch = 0;
 770		return;
 771	default:
 772		mLOG(GB_MBC, STUB, "MBC7 unknown register: %04X:%02X", address, value);
 773		return;
 774	case 0x80:
 775		break;
 776	}
 777	GBMBC7Field old = memory->mbcState.mbc7.eeprom;
 778	value = GBMBC7FieldFillDO(value); // Hi-Z
 779	if (!GBMBC7FieldIsCS(old) && GBMBC7FieldIsCS(value)) {
 780		mbc7->state = GBMBC7_STATE_IDLE;
 781	}
 782	if (!GBMBC7FieldIsCLK(old) && GBMBC7FieldIsCLK(value)) {
 783		if (mbc7->state == GBMBC7_STATE_READ_COMMAND || mbc7->state == GBMBC7_STATE_EEPROM_WRITE || mbc7->state == GBMBC7_STATE_EEPROM_WRAL) {
 784			mbc7->sr <<= 1;
 785			mbc7->sr |= GBMBC7FieldGetDI(value);
 786			++mbc7->srBits;
 787		}
 788		switch (mbc7->state) {
 789		case GBMBC7_STATE_IDLE:
 790			if (GBMBC7FieldIsDI(value)) {
 791				mbc7->state = GBMBC7_STATE_READ_COMMAND;
 792				mbc7->srBits = 0;
 793				mbc7->sr = 0;
 794			}
 795			break;
 796		case GBMBC7_STATE_READ_COMMAND:
 797			if (mbc7->srBits == 10) {
 798				mbc7->state = 0x10 | (mbc7->sr >> 6);
 799				if (mbc7->state & 0xC) {
 800					mbc7->state &= ~0x3;
 801				}
 802				mbc7->srBits = 0;
 803				mbc7->address = mbc7->sr & 0x7F;
 804			}
 805			break;
 806		case GBMBC7_STATE_DO:
 807			value = GBMBC7FieldSetDO(value, mbc7->sr >> 15);
 808			mbc7->sr <<= 1;
 809			--mbc7->srBits;
 810			if (!mbc7->srBits) {
 811				mbc7->state = GBMBC7_STATE_IDLE;
 812			}
 813			break;
 814		default:
 815			break;
 816		}
 817		switch (mbc7->state) {
 818		case GBMBC7_STATE_EEPROM_EWEN:
 819			mbc7->writable = true;
 820			mbc7->state = GBMBC7_STATE_IDLE;
 821			break;
 822		case GBMBC7_STATE_EEPROM_EWDS:
 823			mbc7->writable = false;
 824			mbc7->state = GBMBC7_STATE_IDLE;
 825			break;
 826		case GBMBC7_STATE_EEPROM_WRITE:
 827			if (mbc7->srBits == 16) {
 828				if (mbc7->writable) {
 829					memory->sram[mbc7->address * 2] = mbc7->sr >> 8;
 830					memory->sram[mbc7->address * 2 + 1] = mbc7->sr;
 831				}
 832				mbc7->state = GBMBC7_STATE_IDLE;
 833			}
 834			break;
 835		case GBMBC7_STATE_EEPROM_ERASE:
 836			if (mbc7->writable) {
 837				memory->sram[mbc7->address * 2] = 0xFF;
 838				memory->sram[mbc7->address * 2 + 1] = 0xFF;
 839			}
 840			mbc7->state = GBMBC7_STATE_IDLE;
 841			break;
 842		case GBMBC7_STATE_EEPROM_READ:
 843			mbc7->srBits = 16;
 844			mbc7->sr = memory->sram[mbc7->address * 2] << 8;
 845			mbc7->sr |= memory->sram[mbc7->address * 2 + 1];
 846			mbc7->state = GBMBC7_STATE_DO;
 847			value = GBMBC7FieldClearDO(value);
 848			break;
 849		case GBMBC7_STATE_EEPROM_WRAL:
 850			if (mbc7->srBits == 16) {
 851				if (mbc7->writable) {
 852					int i;
 853					for (i = 0; i < 128; ++i) {
 854						memory->sram[i * 2] = mbc7->sr >> 8;
 855						memory->sram[i * 2 + 1] = mbc7->sr;
 856					}
 857				}
 858				mbc7->state = GBMBC7_STATE_IDLE;
 859			}
 860			break;
 861		case GBMBC7_STATE_EEPROM_ERAL:
 862			if (mbc7->writable) {
 863				int i;
 864				for (i = 0; i < 128; ++i) {
 865					memory->sram[i * 2] = 0xFF;
 866					memory->sram[i * 2 + 1] = 0xFF;
 867				}
 868			}
 869			mbc7->state = GBMBC7_STATE_IDLE;
 870			break;
 871		default:
 872			break;
 873		}
 874	} else if (GBMBC7FieldIsCS(value) && GBMBC7FieldIsCLK(old) && !GBMBC7FieldIsCLK(value)) {
 875		value = GBMBC7FieldSetDO(value, GBMBC7FieldGetDO(old));
 876	}
 877	mbc7->eeprom = value;
 878}
 879
 880void _GBMMM01(struct GB* gb, uint16_t address, uint8_t value) {
 881	struct GBMemory* memory = &gb->memory;
 882	if (!memory->mbcState.mmm01.locked) {
 883		switch (address >> 13) {
 884		case 0x0:
 885			memory->mbcState.mmm01.locked = true;
 886			GBMBCSwitchBank0(gb, memory->mbcState.mmm01.currentBank0);
 887			break;
 888		case 0x1:
 889			memory->mbcState.mmm01.currentBank0 &= ~0x7F;
 890			memory->mbcState.mmm01.currentBank0 |= value & 0x7F;
 891			break;
 892		case 0x2:
 893			memory->mbcState.mmm01.currentBank0 &= ~0x180;
 894			memory->mbcState.mmm01.currentBank0 |= (value & 0x30) << 3;
 895			break;
 896		default:
 897			// TODO
 898			mLOG(GB_MBC, STUB, "MMM01 unknown address: %04X:%02X", address, value);
 899			break;
 900		}
 901		return;
 902	}
 903	switch (address >> 13) {
 904	case 0x0:
 905		switch (value) {
 906		case 0xA:
 907			memory->sramAccess = true;
 908			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
 909			break;
 910		default:
 911			memory->sramAccess = false;
 912			break;
 913		}
 914		break;
 915	case 0x1:
 916		GBMBCSwitchBank(gb, value + memory->mbcState.mmm01.currentBank0);
 917		break;
 918	case 0x2:
 919		GBMBCSwitchSramBank(gb, value);
 920		break;
 921	default:
 922		// TODO
 923		mLOG(GB_MBC, STUB, "MMM01 unknown address: %04X:%02X", address, value);
 924		break;
 925	}
 926}
 927
 928void _GBHuC1(struct GB* gb, uint16_t address, uint8_t value) {
 929	struct GBMemory* memory = &gb->memory;
 930	int bank = value & 0x3F;
 931	switch (address >> 13) {
 932	case 0x0:
 933		switch (value) {
 934		case 0xE:
 935			memory->sramAccess = false;
 936			break;
 937		default:
 938			memory->sramAccess = true;
 939			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
 940			break;
 941		}
 942		break;
 943	case 0x1:
 944		GBMBCSwitchBank(gb, bank);
 945		break;
 946	case 0x2:
 947		GBMBCSwitchSramBank(gb, value);
 948		break;
 949	default:
 950		// TODO
 951		mLOG(GB_MBC, STUB, "HuC-1 unknown address: %04X:%02X", address, value);
 952		break;
 953	}
 954}
 955
 956void _GBHuC3(struct GB* gb, uint16_t address, uint8_t value) {
 957	struct GBMemory* memory = &gb->memory;
 958	int bank = value & 0x3F;
 959	if (address & 0x1FFF) {
 960		mLOG(GB_MBC, STUB, "HuC-3 unknown value %04X:%02X", address, value);
 961	}
 962
 963	switch (address >> 13) {
 964	case 0x0:
 965		switch (value) {
 966		case 0xA:
 967			memory->sramAccess = true;
 968			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
 969			break;
 970		default:
 971			memory->sramAccess = false;
 972			break;
 973		}
 974		break;
 975	case 0x1:
 976		GBMBCSwitchBank(gb, bank);
 977		break;
 978	case 0x2:
 979		GBMBCSwitchSramBank(gb, bank);
 980		break;
 981	default:
 982		// TODO
 983		mLOG(GB_MBC, STUB, "HuC-3 unknown address: %04X:%02X", address, value);
 984		break;
 985	}
 986}
 987
 988void _GBPocketCam(struct GB* gb, uint16_t address, uint8_t value) {
 989	struct GBMemory* memory = &gb->memory;
 990	int bank = value & 0x3F;
 991	switch (address >> 13) {
 992	case 0x0:
 993		switch (value) {
 994		case 0:
 995			memory->sramAccess = false;
 996			break;
 997		case 0xA:
 998			memory->sramAccess = true;
 999			GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
1000			break;
1001		default:
1002			// TODO
1003			mLOG(GB_MBC, STUB, "Pocket Cam unknown value %02X", value);
1004			break;
1005		}
1006		break;
1007	case 0x1:
1008		GBMBCSwitchBank(gb, bank);
1009		break;
1010	case 0x2:
1011		if (value < 0x10) {
1012			GBMBCSwitchSramBank(gb, value);
1013			memory->mbcState.pocketCam.registersActive = false;
1014		} else {
1015			memory->mbcState.pocketCam.registersActive = true;
1016		}
1017		break;
1018	case 0x5:
1019		address &= 0x7F;
1020		if (address == 0 && value & 1) {
1021			value &= 6; // TODO: Timing
1022			_GBPocketCamCapture(memory);
1023		}
1024		if (address < sizeof(memory->mbcState.pocketCam.registers)) {
1025			memory->mbcState.pocketCam.registers[address] = value;
1026		}
1027		break;
1028	default:
1029		mLOG(GB_MBC, STUB, "Pocket Cam unknown address: %04X:%02X", address, value);
1030		break;
1031	}
1032}
1033
1034uint8_t _GBPocketCamRead(struct GBMemory* memory, uint16_t address) {
1035	if (memory->mbcState.pocketCam.registersActive) {
1036		if ((address & 0x7F) == 0) {
1037			return memory->mbcState.pocketCam.registers[0];
1038		}
1039		return 0;
1040	}
1041	return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)];
1042}
1043
1044void _GBPocketCamCapture(struct GBMemory* memory) {
1045	if (!memory->cam) {
1046		return;
1047	}
1048	const void* image = NULL;
1049	size_t stride;
1050	enum mColorFormat format;
1051	memory->cam->requestImage(memory->cam, &image, &stride, &format);
1052	if (!image) {
1053		return;
1054	}
1055	memset(&memory->sram[0x100], 0, GBCAM_HEIGHT * GBCAM_WIDTH / 4);
1056	struct GBPocketCamState* pocketCam = &memory->mbcState.pocketCam;
1057	size_t x, y;
1058	for (y = 0; y < GBCAM_HEIGHT; ++y) {
1059		for (x = 0; x < GBCAM_WIDTH; ++x) {
1060			uint32_t gray;
1061			uint32_t color;
1062			switch (format) {
1063			case mCOLOR_XBGR8:
1064			case mCOLOR_XRGB8:
1065			case mCOLOR_ARGB8:
1066			case mCOLOR_ABGR8:
1067				color = ((const uint32_t*) image)[y * stride + x];
1068				gray = (color & 0xFF) + ((color >> 8) & 0xFF) + ((color >> 16) & 0xFF);
1069				break;
1070			case mCOLOR_BGRX8:
1071			case mCOLOR_RGBX8:
1072			case mCOLOR_RGBA8:
1073			case mCOLOR_BGRA8:
1074				color = ((const uint32_t*) image)[y * stride + x];
1075				gray = ((color >> 8) & 0xFF) + ((color >> 16) & 0xFF) + ((color >> 24) & 0xFF);
1076				break;
1077			case mCOLOR_BGR5:
1078			case mCOLOR_RGB5:
1079			case mCOLOR_ARGB5:
1080			case mCOLOR_ABGR5:
1081				color = ((const uint16_t*) image)[y * stride + x];
1082				gray = ((color << 3) & 0xF8) + ((color >> 2) & 0xF8) + ((color >> 7) & 0xF8);
1083				break;
1084			case mCOLOR_BGR565:
1085			case mCOLOR_RGB565:
1086				color = ((const uint16_t*) image)[y * stride + x];
1087				gray = ((color << 3) & 0xF8) + ((color >> 3) & 0xFC) + ((color >> 8) & 0xF8);
1088				break;
1089			case mCOLOR_BGRA5:
1090			case mCOLOR_RGBA5:
1091				color = ((const uint16_t*) image)[y * stride + x];
1092				gray = ((color << 2) & 0xF8) + ((color >> 3) & 0xF8) + ((color >> 8) & 0xF8);
1093				break;
1094			default:
1095				mLOG(GB_MBC, WARN, "Unsupported pixel format: %X", format);
1096				return;
1097			}
1098			uint16_t exposure = (pocketCam->registers[2] << 8) | (pocketCam->registers[3]);
1099			gray = (gray + 1) * exposure / 0x300;
1100			// TODO: Additional processing
1101			int matrixEntry = 3 * ((x & 3) + 4 * (y & 3));
1102			if (gray < pocketCam->registers[matrixEntry + 6]) {
1103				gray = 0x101;
1104			} else if (gray < pocketCam->registers[matrixEntry + 7]) {
1105				gray = 0x100;
1106			} else if (gray < pocketCam->registers[matrixEntry + 8]) {
1107				gray = 0x001;
1108			} else {
1109				gray = 0;
1110			}
1111			int coord = (((x >> 3) & 0xF) * 8 + (y & 0x7)) * 2 + (y & ~0x7) * 0x20;
1112			uint16_t existing;
1113			LOAD_16LE(existing, coord + 0x100, memory->sram);
1114			existing |= gray << (7 - (x & 7));
1115			STORE_16LE(existing, coord + 0x100, memory->sram);
1116		}
1117	}
1118}
1119
1120void _GBTAMA5(struct GB* gb, uint16_t address, uint8_t value) {
1121	struct GBMemory* memory = &gb->memory;
1122	struct GBTAMA5State* tama5 = &memory->mbcState.tama5;
1123	switch (address >> 13) {
1124	case 0x5:
1125		if (address & 1) {
1126			tama5->reg = value;
1127		} else {
1128			value &= 0xF;
1129			if (tama5->reg < GBTAMA5_MAX) {
1130				tama5->registers[tama5->reg] = value;
1131				uint8_t address = ((tama5->registers[GBTAMA5_CS] << 4) & 0x10) | tama5->registers[GBTAMA5_ADDR_LO];
1132				uint8_t out = (tama5->registers[GBTAMA5_WRITE_HI] << 4) | tama5->registers[GBTAMA5_WRITE_LO];
1133				switch (tama5->reg) {
1134				case GBTAMA5_BANK_LO:
1135				case GBTAMA5_BANK_HI:
1136					GBMBCSwitchBank(gb, tama5->registers[GBTAMA5_BANK_LO] | (tama5->registers[GBTAMA5_BANK_HI] << 4));
1137					break;
1138				case GBTAMA5_WRITE_LO:
1139				case GBTAMA5_WRITE_HI:
1140				case GBTAMA5_CS:
1141					break;
1142				case GBTAMA5_ADDR_LO:
1143					switch (tama5->registers[GBTAMA5_CS] >> 1) {
1144					case 0x0: // RAM write
1145						memory->sram[address] = out;
1146						break;
1147					case 0x1: // RAM read
1148						break;
1149					default:
1150						mLOG(GB_MBC, STUB, "TAMA5 unknown address: %X-%02X:%02X", tama5->registers[GBTAMA5_CS] >> 1, address, out);
1151					}
1152					break;
1153				default:
1154					mLOG(GB_MBC, STUB, "TAMA5 unknown write: %02X:%X", tama5->reg, value);
1155					break;
1156				}
1157			} else {
1158				mLOG(GB_MBC, STUB, "TAMA5 unknown write: %02X", tama5->reg);
1159			}
1160		}
1161		break;
1162	default:
1163		mLOG(GB_MBC, STUB, "TAMA5 unknown address: %04X:%02X", address, value);
1164	}
1165}
1166
1167uint8_t _GBTAMA5Read(struct GBMemory* memory, uint16_t address) {
1168	struct GBTAMA5State* tama5 = &memory->mbcState.tama5;
1169	if ((address & 0x1FFF) > 1) {
1170		mLOG(GB_MBC, STUB, "TAMA5 unknown address: %04X", address);
1171	}
1172	if (address & 1) {
1173		return 0xFF;
1174	} else {
1175		uint8_t value = 0xF0;
1176		uint8_t address = ((tama5->registers[GBTAMA5_CS] << 4) & 0x10) | tama5->registers[GBTAMA5_ADDR_LO];
1177		switch (tama5->reg) {
1178		case GBTAMA5_ACTIVE:
1179			return 0xF1;
1180		case GBTAMA5_READ_LO:
1181		case GBTAMA5_READ_HI:
1182			switch (tama5->registers[GBTAMA5_CS] >> 1) {
1183			case 1:
1184				value = memory->sram[address];
1185				break;
1186			default:
1187				mLOG(GB_MBC, STUB, "TAMA5 unknown read: %02X", tama5->reg);
1188				break;
1189			}
1190			if (tama5->reg == GBTAMA5_READ_HI) {
1191				value >>= 4;
1192			}
1193			value |= 0xF0;
1194			return value;
1195		default:
1196			mLOG(GB_MBC, STUB, "TAMA5 unknown read: %02X", tama5->reg);
1197			return 0xF1;
1198		}
1199	}
1200}
1201
1202void _GBWisdomTree(struct GB* gb, uint16_t address, uint8_t value) {
1203	UNUSED(value);
1204	int bank = address & 0x3F;
1205	switch (address >> 14) {
1206	case 0x0:
1207		GBMBCSwitchBank0(gb, bank * 2);
1208		GBMBCSwitchBank(gb, bank * 2 + 1);
1209		break;
1210	default:
1211		// TODO
1212		mLOG(GB_MBC, STUB, "Wisdom Tree unknown address: %04X:%02X", address, value);
1213		break;
1214	}
1215}
1216
1217void GBMBCRTCRead(struct GB* gb) {
1218	struct GBMBCRTCSaveBuffer rtcBuffer;
1219	struct VFile* vf = gb->sramVf;
1220	if (!vf) {
1221		return;
1222	}
1223	vf->seek(vf, gb->sramSize, SEEK_SET);
1224	if (vf->read(vf, &rtcBuffer, sizeof(rtcBuffer)) < (ssize_t) sizeof(rtcBuffer) - 4) {
1225		return;
1226	}
1227
1228	LOAD_32LE(gb->memory.rtcRegs[0], 0, &rtcBuffer.latchedSec);
1229	LOAD_32LE(gb->memory.rtcRegs[1], 0, &rtcBuffer.latchedMin);
1230	LOAD_32LE(gb->memory.rtcRegs[2], 0, &rtcBuffer.latchedHour);
1231	LOAD_32LE(gb->memory.rtcRegs[3], 0, &rtcBuffer.latchedDays);
1232	LOAD_32LE(gb->memory.rtcRegs[4], 0, &rtcBuffer.latchedDaysHi);
1233	LOAD_64LE(gb->memory.rtcLastLatch, 0, &rtcBuffer.unixTime);
1234}
1235
1236void GBMBCRTCWrite(struct GB* gb) {
1237	struct VFile* vf = gb->sramVf;
1238	if (!vf) {
1239		return;
1240	}
1241
1242	uint8_t rtcRegs[5];
1243	memcpy(rtcRegs, gb->memory.rtcRegs, sizeof(rtcRegs));
1244	time_t rtcLastLatch = gb->memory.rtcLastLatch;
1245	_latchRtc(gb->memory.rtc, rtcRegs, &rtcLastLatch);
1246
1247	struct GBMBCRTCSaveBuffer rtcBuffer;
1248	STORE_32LE(rtcRegs[0], 0, &rtcBuffer.sec);
1249	STORE_32LE(rtcRegs[1], 0, &rtcBuffer.min);
1250	STORE_32LE(rtcRegs[2], 0, &rtcBuffer.hour);
1251	STORE_32LE(rtcRegs[3], 0, &rtcBuffer.days);
1252	STORE_32LE(rtcRegs[4], 0, &rtcBuffer.daysHi);
1253	STORE_32LE(gb->memory.rtcRegs[0], 0, &rtcBuffer.latchedSec);
1254	STORE_32LE(gb->memory.rtcRegs[1], 0, &rtcBuffer.latchedMin);
1255	STORE_32LE(gb->memory.rtcRegs[2], 0, &rtcBuffer.latchedHour);
1256	STORE_32LE(gb->memory.rtcRegs[3], 0, &rtcBuffer.latchedDays);
1257	STORE_32LE(gb->memory.rtcRegs[4], 0, &rtcBuffer.latchedDaysHi);
1258	STORE_64LE(gb->memory.rtcLastLatch, 0, &rtcBuffer.unixTime);
1259
1260	if ((size_t) vf->size(vf) < gb->sramSize + sizeof(rtcBuffer)) {
1261		// Writing past the end of the file can invalidate the file mapping
1262		vf->unmap(vf, gb->memory.sram, gb->sramSize);
1263		gb->memory.sram = NULL;
1264	}
1265	vf->seek(vf, gb->sramSize, SEEK_SET);
1266	vf->write(vf, &rtcBuffer, sizeof(rtcBuffer));
1267	if (!gb->memory.sram) {
1268		gb->memory.sram = vf->map(vf, gb->sramSize, MAP_WRITE);
1269		GBMBCSwitchSramBank(gb, gb->memory.sramCurrentBank);
1270	}
1271}