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