all repos — mgba @ 98ff2fb5348d0c4145b03ba380dd512949e78b52

mGBA Game Boy Advance Emulator

src/gb/overrides.c (view raw)

  1/* Copyright (c) 2013-2015 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/overrides.h>
  7
  8#include <mgba/internal/gb/gb.h>
  9#include <mgba/internal/gb/mbc.h>
 10
 11#include <mgba-util/configuration.h>
 12#include <mgba-util/crc32.h>
 13
 14static const struct GBCartridgeOverride _overrides[] = {
 15	// Pokemon Gold Spaceworld 1997 demo
 16	{ 0x232a067d, GB_MODEL_AUTODETECT, GB_MBC3_RTC, { 0 } },
 17
 18	{ 0, 0, 0, { 0 } }
 19};
 20
 21bool GBOverrideFind(const struct Configuration* config, struct GBCartridgeOverride* override) {
 22	override->model = GB_MODEL_AUTODETECT;
 23	override->mbc = GB_MBC_AUTODETECT;
 24	memset(override->gbColors, 0, sizeof(override->gbColors));
 25	bool found = false;
 26
 27	int i;
 28	for (i = 0; _overrides[i].headerCrc32; ++i) {
 29		if (override->headerCrc32 == _overrides[i].headerCrc32) {
 30			*override = _overrides[i];
 31			found = true;
 32			break;
 33		}
 34	}
 35
 36	if (config) {
 37		char sectionName[24] = "";
 38		snprintf(sectionName, sizeof(sectionName), "gb.override.%08X", override->headerCrc32);
 39		const char* model = ConfigurationGetValue(config, sectionName, "model");
 40		const char* mbc = ConfigurationGetValue(config, sectionName, "mbc");
 41		const char* pal[12] = {
 42			ConfigurationGetValue(config, sectionName, "pal[0]"),
 43			ConfigurationGetValue(config, sectionName, "pal[1]"),
 44			ConfigurationGetValue(config, sectionName, "pal[2]"),
 45			ConfigurationGetValue(config, sectionName, "pal[3]"),
 46			ConfigurationGetValue(config, sectionName, "pal[4]"),
 47			ConfigurationGetValue(config, sectionName, "pal[5]"),
 48			ConfigurationGetValue(config, sectionName, "pal[6]"),
 49			ConfigurationGetValue(config, sectionName, "pal[7]"),
 50			ConfigurationGetValue(config, sectionName, "pal[8]"),
 51			ConfigurationGetValue(config, sectionName, "pal[9]"),
 52			ConfigurationGetValue(config, sectionName, "pal[10]"),
 53			ConfigurationGetValue(config, sectionName, "pal[11]")
 54		};
 55
 56		if (model) {
 57			override->model = GBNameToModel(model);
 58			found = override->model != GB_MODEL_AUTODETECT;
 59		}
 60
 61		if (mbc) {
 62			char* end;
 63			long type = strtoul(mbc, &end, 0);
 64			if (end && !*end) {
 65				override->mbc = type;
 66				found = true;
 67			}
 68		}
 69
 70		for (i = 0; i < 12; ++i) {
 71			if (!pal[i]) {
 72				continue;
 73			}
 74			char* end;
 75			unsigned long value = strtoul(pal[i], &end, 10);
 76			if (end == &pal[i][1] && *end == 'x') {
 77				value = strtoul(pal[i], &end, 16);
 78			}
 79			if (*end) {
 80				continue;
 81			}
 82			value |= 0xFF000000;
 83			override->gbColors[i] = value;
 84			if (i < 8) {
 85				override->gbColors[i + 4] = value;
 86			}
 87			if (i < 4) {
 88				override->gbColors[i + 8] = value;
 89			}
 90		}
 91	}
 92	return found;
 93}
 94
 95void GBOverrideSave(struct Configuration* config, const struct GBCartridgeOverride* override) {
 96	char sectionName[24] = "";
 97	snprintf(sectionName, sizeof(sectionName), "gb.override.%08X", override->headerCrc32);
 98	const char* model = GBModelToName(override->model);
 99	ConfigurationSetValue(config, sectionName, "model", model);
100
101	if (override->gbColors[0] & 0xFF000000) {
102		ConfigurationSetIntValue(config, sectionName, "pal[0]", override->gbColors[0] & ~0xFF000000);
103	}
104	if (override->gbColors[1] & 0xFF000000) {
105		ConfigurationSetIntValue(config, sectionName, "pal[1]", override->gbColors[1] & ~0xFF000000);
106	}
107	if (override->gbColors[2] & 0xFF000000) {
108		ConfigurationSetIntValue(config, sectionName, "pal[2]", override->gbColors[2] & ~0xFF000000);
109	}
110	if (override->gbColors[3] & 0xFF000000) {
111		ConfigurationSetIntValue(config, sectionName, "pal[3]", override->gbColors[3] & ~0xFF000000);
112	}
113	if (override->gbColors[4] & 0xFF000000) {
114		ConfigurationSetIntValue(config, sectionName, "pal[4]", override->gbColors[4] & ~0xFF000000);
115	}
116	if (override->gbColors[5] & 0xFF000000) {
117		ConfigurationSetIntValue(config, sectionName, "pal[5]", override->gbColors[5] & ~0xFF000000);
118	}
119	if (override->gbColors[6] & 0xFF000000) {
120		ConfigurationSetIntValue(config, sectionName, "pal[6]", override->gbColors[6] & ~0xFF000000);
121	}
122	if (override->gbColors[7] & 0xFF000000) {
123		ConfigurationSetIntValue(config, sectionName, "pal[7]", override->gbColors[7] & ~0xFF000000);
124	}
125	if (override->gbColors[8] & 0xFF000000) {
126		ConfigurationSetIntValue(config, sectionName, "pal[8]", override->gbColors[8] & ~0xFF000000);
127	}
128	if (override->gbColors[9] & 0xFF000000) {
129		ConfigurationSetIntValue(config, sectionName, "pal[9]", override->gbColors[9] & ~0xFF000000);
130	}
131	if (override->gbColors[10] & 0xFF000000) {
132		ConfigurationSetIntValue(config, sectionName, "pal[10]", override->gbColors[10] & ~0xFF000000);
133	}
134	if (override->gbColors[11] & 0xFF000000) {
135		ConfigurationSetIntValue(config, sectionName, "pal[11]", override->gbColors[11] & ~0xFF000000);
136	}
137
138	if (override->mbc != GB_MBC_AUTODETECT) {
139		ConfigurationSetIntValue(config, sectionName, "mbc", override->mbc);
140	} else {
141		ConfigurationClearValue(config, sectionName, "mbc");
142	}
143}
144
145void GBOverrideApply(struct GB* gb, const struct GBCartridgeOverride* override) {
146	if (override->model != GB_MODEL_AUTODETECT) {
147		gb->model = override->model;
148	}
149
150	if (override->mbc != GB_MBC_AUTODETECT) {
151		gb->memory.mbcType = override->mbc;
152		GBMBCInit(gb);
153	}
154
155	int i;
156	for (i = 0; i < 12; ++i) {
157		if (!(override->gbColors[i] & 0xFF000000)) {
158			continue;
159		}
160		GBVideoSetPalette(&gb->video, i, override->gbColors[i]);
161		if (i < 8) {
162			GBVideoSetPalette(&gb->video, i + 4, override->gbColors[i]);
163		}
164		if (i < 4) {
165			GBVideoSetPalette(&gb->video, i + 8, override->gbColors[i]);
166		}
167	}
168}
169
170void GBOverrideApplyDefaults(struct GB* gb) {
171	struct GBCartridgeOverride override;
172	override.headerCrc32 = doCrc32(&gb->memory.rom[0x100], sizeof(struct GBCartridge));
173	if (GBOverrideFind(0, &override)) {
174		GBOverrideApply(gb, &override);
175	}
176}