openMSX
RomFactory.cc
Go to the documentation of this file.
1#include "RomFactory.hh"
2#include "RomTypes.hh"
3#include "RomInfo.hh"
4#include "RomPageNN.hh"
5#include "RomPlain.hh"
6#include "RomDRAM.hh"
7#include "RomGeneric8kB.hh"
8#include "RomGeneric16kB.hh"
9#include "RomKonami.hh"
10#include "RomKonamiSCC.hh"
12#include "RomAscii8kB.hh"
13#include "RomAscii8_8.hh"
14#include "RomAscii16kB.hh"
15#include "RomMSXWrite.hh"
16#include "RomPadial8kB.hh"
17#include "RomPadial16kB.hh"
18#include "RomSuperLodeRunner.hh"
19#include "RomSuperSwangi.hh"
20#include "RomMitsubishiMLTS2.hh"
21#include "RomMSXDOS2.hh"
22#include "RomAscii16_2.hh"
23#include "RomRType.hh"
24#include "RomCrossBlaim.hh"
25#include "RomHarryFox.hh"
26#include "RomPanasonic.hh"
27#include "RomNational.hh"
28#include "RomMajutsushi.hh"
29#include "RomSynthesizer.hh"
30#include "RomPlayBall.hh"
31#include "RomNettouYakyuu.hh"
32#include "RomGameMaster2.hh"
33#include "RomHalnote.hh"
34#include "RomZemina25in1.hh"
35#include "RomZemina80in1.hh"
36#include "RomZemina90in1.hh"
37#include "RomZemina126in1.hh"
38#include "RomHolyQuran.hh"
39#include "RomHolyQuran2.hh"
40#include "RomFSA1FM.hh"
41#include "RomManbow2.hh"
42#include "RomMatraInk.hh"
44#include "RomArc.hh"
45#include "ROMHunterMk2.hh"
47#include "ReproCartridgeV1.hh"
48#include "ReproCartridgeV2.hh"
50#include "RomDooly.hh"
51#include "RomMSXtra.hh"
52#include "RomRamFile.hh"
53#include "RomColecoMegaCart.hh"
54#include "RomMultiRom.hh"
55#include "Rom.hh"
56#include "Reactor.hh"
57#include "MSXMotherBoard.hh"
58#include "RomDatabase.hh"
59#include "DeviceConfig.hh"
60#include "XMLElement.hh"
61#include "MSXException.hh"
62#include "enumerate.hh"
63#include "one_of.hh"
64#include "xrange.hh"
65#include <memory>
66
67using std::make_unique;
68
70
71[[nodiscard]] static RomType guessRomType(const Rom& rom)
72{
73 auto size = rom.size();
74 if (size == 0) {
75 return ROM_NORMAL;
76 }
77 //std::span data = rom; // TODO error with clang-13/libc++
78 std::span data{&*rom.begin(), size};
79
80 if (size < 0x10000) {
81 if ((size <= 0x4000) &&
82 (data[0] == 'A') && (data[1] == 'B')) {
83 word initAddr = data[2] + 256 * data[3];
84 word textAddr = data[8] + 256 * data[9];
85 if ((textAddr & 0xC000) == 0x8000) {
86 if ((initAddr == 0) ||
87 (((initAddr & 0xC000) == 0x8000) &&
88 (data[initAddr & (size - 1)] == 0xC9))) {
89 return ROM_PAGE2;
90 }
91 }
92 }
93 // not correct for Konami-DAC, but does this really need
94 // to be correct for _every_ rom?
95 return ROM_MIRRORED;
96 } else if (size == 0x10000 && !((data[0] == 'A') && (data[1] == 'B'))) {
97 // 64 kB ROMs can be plain or memory mapped...
98 // check here for plain, if not, try the auto detection
99 // (thanks for the hint, hap)
100 return ROM_MIRRORED;
101 } else {
102 // GameCartridges do their bank switching by using the Z80
103 // instruction ld(nn),a in the middle of program code. The
104 // address nn depends upon the GameCartridge mapper type used.
105 // To guess which mapper it is, we will look how much writes
106 // with this instruction to the mapper-registers-addresses
107 // occur.
108
109 std::array<unsigned, ROM_LAST> typeGuess = {}; // 0-initialized
110 for (auto i : xrange(size - 3)) {
111 if (data[i] == 0x32) {
112 word value = data[i + 1] + (data[i + 2] << 8);
113 switch (value) {
114 case 0x5000:
115 case 0x9000:
116 case 0xb000:
117 typeGuess[ROM_KONAMI_SCC]++;
118 break;
119 case 0x4000:
120 case 0x8000:
121 case 0xa000:
122 typeGuess[ROM_KONAMI]++;
123 break;
124 case 0x6800:
125 case 0x7800:
126 typeGuess[ROM_ASCII8]++;
127 break;
128 case 0x6000:
129 typeGuess[ROM_KONAMI]++;
130 typeGuess[ROM_ASCII8]++;
131 typeGuess[ROM_ASCII16]++;
132 break;
133 case 0x7000:
134 typeGuess[ROM_KONAMI_SCC]++;
135 typeGuess[ROM_ASCII8]++;
136 typeGuess[ROM_ASCII16]++;
137 break;
138 case 0x77ff:
139 typeGuess[ROM_ASCII16]++;
140 break;
141 }
142 }
143 }
144 if (typeGuess[ROM_ASCII8]) typeGuess[ROM_ASCII8]--; // -1 -> max_int
146 for (auto [i, tg] : enumerate(typeGuess)) {
147 // debug: fprintf(stderr, "%d: %d\n", i, tg);
148 if (tg && (tg >= typeGuess[type])) {
149 type = static_cast<RomType>(i);
150 }
151 }
152 return type;
153 }
154}
155
156std::unique_ptr<MSXDevice> create(const DeviceConfig& config)
157{
158 Rom rom(std::string(config.getAttributeValue("id")), "rom", config);
159
160 // Get specified mapper type from the config.
161 RomType type = [&] {
162 // if no type is mentioned, we assume 'mirrored' which works for most
163 // plain ROMs...
164 std::string_view typeStr = config.getChildData("mappertype", "Mirrored");
165 if (typeStr == "auto") {
166 // First check whether the (possibly patched) SHA1 is in the DB
167 const RomInfo* romInfo = config.getReactor().getSoftwareDatabase().fetchRomInfo(rom.getSHA1());
168 // If not found, try the original SHA1 in the DB
169 if (!romInfo) {
171 }
172 // If still not found, guess the mapper type
173 if (!romInfo) {
174 auto machineType = config.getMotherBoard().getMachineType();
175 if (machineType == "Coleco") {
176 if (rom.size() == one_of(128*1024u, 256*1024u, 512*1024u, 1024*1024u)) {
177 return ROM_COLECOMEGACART;
178 } else {
179 return ROM_PAGE23;
180 }
181 } else {
182 return guessRomType(rom);
183 }
184 } else {
185 return romInfo->getRomType();
186 }
187 } else {
188 // Use mapper type from config, even if this overrides DB.
189 auto t = RomInfo::nameToRomType(typeStr);
190 if (t == ROM_UNKNOWN) {
191 throw MSXException("Unknown mappertype: ", typeStr);
192 }
193 return t;
194 }
195 }();
196
197 // Store actual detected mapper type in config (override the possible
198 // 'auto' value). This way we're sure that on savestate/loadstate we're
199 // using the same mapper type (for example when the user's rom-database
200 // was updated).
201 // We do it at this point so that constructors used below can use this
202 // information for warning messages etc.
203 auto& doc = const_cast<DeviceConfig&>(config).getXMLDocument();
204 doc.setChildData(const_cast<XMLElement&>(*config.getXML()),
205 "mappertype", RomInfo::romTypeToName(type).data());
206
207 std::unique_ptr<MSXRom> result;
208 switch (type) {
209 case ROM_MIRRORED:
210 case ROM_MIRRORED0000:
211 case ROM_MIRRORED4000:
212 case ROM_MIRRORED8000:
213 case ROM_MIRROREDC000:
214 case ROM_NORMAL:
215 case ROM_NORMAL0000:
216 case ROM_NORMAL4000:
217 case ROM_NORMAL8000:
218 case ROM_NORMALC000:
219 result = make_unique<RomPlain>(config, std::move(rom), type);
220 break;
221 case ROM_PAGE0:
222 case ROM_PAGE1:
223 case ROM_PAGE01:
224 case ROM_PAGE2:
225 case ROM_PAGE12:
226 case ROM_PAGE012:
227 case ROM_PAGE3:
228 case ROM_PAGE23:
229 case ROM_PAGE123:
230 case ROM_PAGE0123:
231 result = make_unique<RomPageNN>(config, std::move(rom), type);
232 break;
233 case ROM_DRAM:
234 result = make_unique<RomDRAM>(config, std::move(rom));
235 break;
236 case ROM_GENERIC_8KB:
237 result = make_unique<RomGeneric8kB>(config, std::move(rom));
238 break;
239 case ROM_GENERIC_16KB:
240 result = make_unique<RomGeneric16kB>(config, std::move(rom));
241 break;
242 case ROM_KONAMI_SCC:
243 result = make_unique<RomKonamiSCC>(config, std::move(rom));
244 break;
245 case ROM_KONAMI:
246 result = make_unique<RomKonami>(config, std::move(rom));
247 break;
248 case ROM_KBDMASTER:
249 result = make_unique<RomKonamiKeyboardMaster>(config, std::move(rom));
250 break;
251 case ROM_ASCII8:
252 result = make_unique<RomAscii8kB>(config, std::move(rom));
253 break;
254 case ROM_ASCII16:
255 result = make_unique<RomAscii16kB>(config, std::move(rom));
256 break;
257 case ROM_MSXWRITE:
258 result = make_unique<RomMSXWrite>(config, std::move(rom));
259 break;
260 case ROM_PADIAL8:
261 result = make_unique<RomPadial8kB>(config, std::move(rom));
262 break;
263 case ROM_PADIAL16:
264 result = make_unique<RomPadial16kB>(config, std::move(rom));
265 break;
267 result = make_unique<RomSuperLodeRunner>(config, std::move(rom));
268 break;
269 case ROM_SUPERSWANGI:
270 result = make_unique<RomSuperSwangi>(config, std::move(rom));
271 break;
273 result = make_unique<RomMitsubishiMLTS2>(config, std::move(rom));
274 break;
275 case ROM_MSXDOS2:
276 result = make_unique<RomMSXDOS2>(config, std::move(rom));
277 break;
278 case ROM_R_TYPE:
279 result = make_unique<RomRType>(config, std::move(rom));
280 break;
281 case ROM_CROSS_BLAIM:
282 result = make_unique<RomCrossBlaim>(config, std::move(rom));
283 break;
284 case ROM_HARRY_FOX:
285 result = make_unique<RomHarryFox>(config, std::move(rom));
286 break;
287 case ROM_ASCII8_8:
288 result = make_unique<RomAscii8_8>(
289 config, std::move(rom), RomAscii8_8::ASCII8_8);
290 break;
291 case ROM_ASCII8_32:
292 result = make_unique<RomAscii8_8>(
293 config, std::move(rom), RomAscii8_8::ASCII8_32);
294 break;
295 case ROM_ASCII8_2:
296 result = make_unique<RomAscii8_8>(
297 config, std::move(rom), RomAscii8_8::ASCII8_2);
298 break;
299 case ROM_KOEI_8:
300 result = make_unique<RomAscii8_8>(
301 config, std::move(rom), RomAscii8_8::KOEI_8);
302 break;
303 case ROM_KOEI_32:
304 result = make_unique<RomAscii8_8>(
305 config, std::move(rom), RomAscii8_8::KOEI_32);
306 break;
307 case ROM_WIZARDRY:
308 result = make_unique<RomAscii8_8>(
309 config, std::move(rom), RomAscii8_8::WIZARDRY);
310 break;
311 case ROM_ASCII16_2:
312 result = make_unique<RomAscii16_2>(config, std::move(rom), RomAscii16_2::ASCII16_2);
313 break;
314 case ROM_ASCII16_8:
315 result = make_unique<RomAscii16_2>(config, std::move(rom), RomAscii16_2::ASCII16_8);
316 break;
317 case ROM_GAME_MASTER2:
318 result = make_unique<RomGameMaster2>(config, std::move(rom));
319 break;
320 case ROM_PANASONIC:
321 result = make_unique<RomPanasonic>(config, std::move(rom));
322 break;
323 case ROM_NATIONAL:
324 result = make_unique<RomNational>(config, std::move(rom));
325 break;
326 case ROM_MAJUTSUSHI:
327 result = make_unique<RomMajutsushi>(config, std::move(rom));
328 break;
329 case ROM_SYNTHESIZER:
330 result = make_unique<RomSynthesizer>(config, std::move(rom));
331 break;
332 case ROM_PLAYBALL:
333 result = make_unique<RomPlayBall>(config, std::move(rom));
334 break;
336 result = make_unique<RomNettouYakyuu>(config, std::move(rom));
337 break;
338 case ROM_HALNOTE:
339 result = make_unique<RomHalnote>(config, std::move(rom));
340 break;
341 case ROM_ZEMINA25IN1:
342 result = make_unique<RomZemina25in1>(config, std::move(rom));
343 break;
344 case ROM_ZEMINA80IN1:
345 result = make_unique<RomZemina80in1>(config, std::move(rom));
346 break;
347 case ROM_ZEMINA90IN1:
348 result = make_unique<RomZemina90in1>(config, std::move(rom));
349 break;
350 case ROM_ZEMINA126IN1:
351 result = make_unique<RomZemina126in1>(config, std::move(rom));
352 break;
353 case ROM_HOLY_QURAN:
354 result = make_unique<RomHolyQuran>(config, std::move(rom));
355 break;
356 case ROM_HOLY_QURAN2:
357 result = make_unique<RomHolyQuran2>(config, std::move(rom));
358 break;
359 case ROM_FSA1FM1:
360 result = make_unique<RomFSA1FM1>(config, std::move(rom));
361 break;
362 case ROM_FSA1FM2:
363 result = make_unique<RomFSA1FM2>(config, std::move(rom));
364 break;
365 case ROM_MANBOW2:
366 case ROM_MANBOW2_2:
370 result = make_unique<RomManbow2>(config, std::move(rom), type);
371 break;
372 case ROM_MATRAINK:
373 result = make_unique<RomMatraInk>(config, std::move(rom));
374 break;
376 result = make_unique<RomMatraCompilation>(config, std::move(rom));
377 break;
378 case ROM_ARC:
379 result = make_unique<RomArc>(config, std::move(rom));
380 break;
381 case ROM_ROMHUNTERMK2:
382 result = make_unique<ROMHunterMk2>(config, std::move(rom));
383 break;
385 result = make_unique<MegaFlashRomSCCPlus>(config, std::move(rom));
386 break;
388 result = make_unique<ReproCartridgeV1>(config, std::move(rom));
389 break;
391 result = make_unique<ReproCartridgeV2>(config, std::move(rom));
392 break;
394 result = make_unique<KonamiUltimateCollection>(config, std::move(rom));
395 break;
396 case ROM_DOOLY:
397 result = make_unique<RomDooly>(config, std::move(rom));
398 break;
399 case ROM_MSXTRA:
400 result = make_unique<RomMSXtra>(config, std::move(rom));
401 break;
402 case ROM_MULTIROM:
403 result = make_unique<RomMultiRom>(config, std::move(rom));
404 break;
405 case ROM_RAMFILE:
406 result = make_unique<RomRamFile>(config, std::move(rom));
407 break;
409 result = make_unique<RomColecoMegaCart>(config, std::move(rom));
410 break;
411 default:
412 throw MSXException("Unknown ROM type");
413 }
414
415 return result;
416}
417
418} // namespace openmsx::RomFactory
TclObject t
Definition: one_of.hh:7
Reactor & getReactor() const
Definition: DeviceConfig.cc:30
MSXMotherBoard & getMotherBoard() const
Definition: DeviceConfig.cc:13
std::string_view getChildData(std::string_view name) const
Definition: DeviceConfig.cc:48
const XMLElement * getXML() const
Definition: DeviceConfig.hh:48
std::string_view getAttributeValue(std::string_view attName) const
Definition: DeviceConfig.cc:70
std::string_view getMachineType() const
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:305
const RomInfo * fetchRomInfo(const Sha1Sum &sha1sum) const
Lookup an entry in the database by sha1sum.
Definition: RomDatabase.cc:618
static std::string_view romTypeToName(RomType type)
Definition: RomInfo.cc:185
RomType getRomType() const
Definition: RomInfo.hh:61
static RomType nameToRomType(std::string_view name)
Definition: RomInfo.cc:174
const Sha1Sum & getSHA1() const
Definition: Rom.cc:357
auto begin() const
Definition: Rom.hh:37
const Sha1Sum & getOriginalSHA1() const
Definition: Rom.cc:349
auto size() const
Definition: Rom.hh:36
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition: enumerate.hh:28
std::unique_ptr< MSXDevice > create(const DeviceConfig &config)
Definition: RomFactory.cc:156
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
@ ROM_HALNOTE
Definition: RomTypes.hh:25
@ ROM_COLECOMEGACART
Definition: RomTypes.hh:16
@ ROM_MATRACOMPILATION
Definition: RomTypes.hh:43
@ ROM_DRAM
Definition: RomTypes.hh:19
@ ROM_PADIAL8
Definition: RomTypes.hh:63
@ ROM_ASCII16_2
Definition: RomTypes.hh:14
@ ROM_FSA1FM1
Definition: RomTypes.hh:20
@ ROM_FSA1FM2
Definition: RomTypes.hh:21
@ ROM_ZEMINA126IN1
Definition: RomTypes.hh:87
@ ROM_PADIAL16
Definition: RomTypes.hh:64
@ ROM_NORMAL0000
Definition: RomTypes.hh:59
@ ROM_KOEI_8
Definition: RomTypes.hh:31
@ ROM_MIRRORED8000
Definition: RomTypes.hh:49
@ ROM_UNKNOWN
Definition: RomTypes.hh:90
@ ROM_ZEMINA90IN1
Definition: RomTypes.hh:86
@ ROM_PAGE1
Definition: RomTypes.hh:66
@ ROM_SYNTHESIZER
Definition: RomTypes.hh:82
@ ROM_PAGE2
Definition: RomTypes.hh:68
@ ROM_PANASONIC
Definition: RomTypes.hh:75
@ ROM_GENERIC_16KB
Definition: RomTypes.hh:24
@ ROM_REPRO_CARTRIDGE2
Definition: RomTypes.hh:36
@ ROM_PAGE01
Definition: RomTypes.hh:67
@ ROM_MANBOW2_2
Definition: RomTypes.hh:40
@ ROM_ZEMINA80IN1
Definition: RomTypes.hh:85
@ ROM_ROMHUNTERMK2
Definition: RomTypes.hh:79
@ ROM_HAMARAJANIGHT
Definition: RomTypes.hh:26
@ ROM_MIRRORED0000
Definition: RomTypes.hh:47
@ ROM_CROSS_BLAIM
Definition: RomTypes.hh:17
@ ROM_MANBOW2
Definition: RomTypes.hh:39
@ ROM_NETTOU_YAKYUU
Definition: RomTypes.hh:57
@ ROM_MULTIROM
Definition: RomTypes.hh:55
@ ROM_GAME_MASTER2
Definition: RomTypes.hh:22
@ ROM_MEGAFLASHROMSCC
Definition: RomTypes.hh:44
@ ROM_PAGE3
Definition: RomTypes.hh:71
@ ROM_MIRRORED4000
Definition: RomTypes.hh:48
@ ROM_PAGE12
Definition: RomTypes.hh:69
@ ROM_MEGAFLASHROMSCCPLUS
Definition: RomTypes.hh:45
@ ROM_MSXDOS2
Definition: RomTypes.hh:52
@ ROM_ASCII16
Definition: RomTypes.hh:13
@ ROM_ZEMINA25IN1
Definition: RomTypes.hh:84
@ ROM_MAJUTSUSHI
Definition: RomTypes.hh:38
@ ROM_ARC
Definition: RomTypes.hh:8
@ ROM_SUPERLODERUNNER
Definition: RomTypes.hh:80
@ ROM_SUPERSWANGI
Definition: RomTypes.hh:81
@ ROM_PAGE123
Definition: RomTypes.hh:73
@ ROM_ASCII8_32
Definition: RomTypes.hh:11
@ ROM_PAGE23
Definition: RomTypes.hh:72
@ ROM_KONAMI_ULTIMATE_COLLECTION
Definition: RomTypes.hh:37
@ ROM_NATIONAL
Definition: RomTypes.hh:56
@ ROM_NORMALC000
Definition: RomTypes.hh:62
@ ROM_NORMAL4000
Definition: RomTypes.hh:60
@ ROM_KBDMASTER
Definition: RomTypes.hh:30
@ ROM_RBSC_FLASH_KONAMI_SCC
Definition: RomTypes.hh:41
@ ROM_MIRRORED
Definition: RomTypes.hh:46
@ ROM_ASCII16_8
Definition: RomTypes.hh:15
@ ROM_HARRY_FOX
Definition: RomTypes.hh:27
@ ROM_R_TYPE
Definition: RomTypes.hh:77
@ ROM_NORMAL8000
Definition: RomTypes.hh:61
@ ROM_WIZARDRY
Definition: RomTypes.hh:83
@ ROM_NORMAL
Definition: RomTypes.hh:58
@ ROM_KONAMI
Definition: RomTypes.hh:33
@ ROM_MATRAINK
Definition: RomTypes.hh:42
@ ROM_PLAYBALL
Definition: RomTypes.hh:76
@ ROM_PAGE0123
Definition: RomTypes.hh:74
@ ROM_RAMFILE
Definition: RomTypes.hh:78
@ ROM_MITSUBISHIMLTS2
Definition: RomTypes.hh:51
@ ROM_ASCII8_2
Definition: RomTypes.hh:10
@ ROM_PAGE012
Definition: RomTypes.hh:70
@ ROM_PAGE0
Definition: RomTypes.hh:65
@ ROM_MSXTRA
Definition: RomTypes.hh:53
@ ROM_MIRROREDC000
Definition: RomTypes.hh:50
@ ROM_GENERIC_8KB
Definition: RomTypes.hh:23
@ ROM_KOEI_32
Definition: RomTypes.hh:32
@ ROM_HOLY_QURAN2
Definition: RomTypes.hh:29
@ ROM_MSXWRITE
Definition: RomTypes.hh:54
@ ROM_ASCII8_8
Definition: RomTypes.hh:12
@ ROM_DOOLY
Definition: RomTypes.hh:18
@ ROM_KONAMI_SCC
Definition: RomTypes.hh:34
@ ROM_REPRO_CARTRIDGE1
Definition: RomTypes.hh:35
@ ROM_HOLY_QURAN
Definition: RomTypes.hh:28
@ ROM_ASCII8
Definition: RomTypes.hh:9
size_t size(std::string_view utf8)
constexpr auto xrange(T e)
Definition: xrange.hh:133