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 "RomZemina80in1.hh"
35 #include "RomZemina90in1.hh"
36 #include "RomZemina126in1.hh"
37 #include "RomHolyQuran.hh"
38 #include "RomHolyQuran2.hh"
39 #include "RomFSA1FM.hh"
40 #include "RomManbow2.hh"
41 #include "RomMatraInk.hh"
42 #include "RomArc.hh"
43 #include "MegaFlashRomSCCPlus.hh"
44 #include "MegaFlashRomSCCPlusSD.hh"
46 #include "RomDooly.hh"
47 #include "RomMSXtra.hh"
48 #include "RomMultiRom.hh"
49 #include "Rom.hh"
50 #include "Reactor.hh"
51 #include "RomDatabase.hh"
52 #include "DeviceConfig.hh"
53 #include "XMLElement.hh"
54 #include "MSXException.hh"
55 #include "memory.hh"
56 
57 using std::unique_ptr;
58 using std::move;
59 using std::string;
60 
61 namespace openmsx {
62 namespace RomFactory {
63 
64 static RomType guessRomType(const Rom& rom)
65 {
66  int size = rom.getSize();
67  if (size == 0) {
68  return ROM_NORMAL;
69  }
70  const byte* data = &rom[0];
71 
72  if (size < 0x10000) {
73  if ((size <= 0x4000) &&
74  (data[0] == 'A') && (data[1] == 'B')) {
75  word initAddr = data[2] + 256 * data[3];
76  word textAddr = data[8] + 256 * data[9];
77  if ((textAddr & 0xC000) == 0x8000) {
78  if ((initAddr == 0) ||
79  (((initAddr & 0xC000) == 0x8000) &&
80  (data[initAddr & (size - 1)] == 0xC9))) {
81  return ROM_PAGE2;
82  }
83  }
84  }
85  // not correct for Konami-DAC, but does this really need
86  // to be correct for _every_ rom?
87  return ROM_MIRRORED;
88  } else if (size == 0x10000 && !((data[0] == 'A') && (data[1] == 'B'))) {
89  // 64 kB ROMs can be plain or memory mapped...
90  // check here for plain, if not, try the auto detection
91  // (thanks for the hint, hap)
92  return ROM_MIRRORED;
93  } else {
94  // GameCartridges do their bankswitching by using the Z80
95  // instruction ld(nn),a in the middle of program code. The
96  // adress nn depends upon the GameCartridge mappertype used.
97  // To guess which mapper it is, we will look how much writes
98  // with this instruction to the mapper-registers-addresses
99  // occur.
100 
101  unsigned typeGuess[ROM_END_OF_UNORDERED_LIST] = {}; // 0-initialized
102  for (int i = 0; i < size - 3; ++i) {
103  if (data[i] == 0x32) {
104  word value = data[i + 1] + (data[i + 2] << 8);
105  switch (value) {
106  case 0x5000:
107  case 0x9000:
108  case 0xb000:
109  typeGuess[ROM_KONAMI_SCC]++;
110  break;
111  case 0x4000:
112  typeGuess[ROM_KONAMI]++;
113  break;
114  case 0x8000:
115  case 0xa000:
116  typeGuess[ROM_KONAMI]++;
117  break;
118  case 0x6800:
119  case 0x7800:
120  typeGuess[ROM_ASCII8]++;
121  break;
122  case 0x6000:
123  typeGuess[ROM_KONAMI]++;
124  typeGuess[ROM_ASCII8]++;
125  typeGuess[ROM_ASCII16]++;
126  break;
127  case 0x7000:
128  typeGuess[ROM_KONAMI_SCC]++;
129  typeGuess[ROM_ASCII8]++;
130  typeGuess[ROM_ASCII16]++;
131  break;
132  case 0x77ff:
133  typeGuess[ROM_ASCII16]++;
134  break;
135  }
136  }
137  }
138  if (typeGuess[ROM_ASCII8]) typeGuess[ROM_ASCII8]--; // -1 -> max_int
139  RomType type = ROM_GENERIC_8KB;
140  for (int i = 0; i < ROM_END_OF_UNORDERED_LIST; ++i) {
141  // debug: fprintf(stderr, "%d: %d\n", i, typeGuess[i]);
142  if (typeGuess[i] && (typeGuess[i] >= typeGuess[type])) {
143  type = static_cast<RomType>(i);
144  }
145  }
146  // in case of doubt we go for type ROM_GENERIC_8KB
147  // in case of even type ROM_ASCII16 and ROM_ASCII8 we would
148  // prefer ROM_ASCII16 but we would still prefer ROM_GENERIC_8KB
149  // above ROM_ASCII8 or ROM_ASCII16
150  if ((type == ROM_ASCII16) &&
151  (typeGuess[ROM_GENERIC_8KB] == typeGuess[ROM_ASCII16])) {
152  type = ROM_GENERIC_8KB;
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  string_ref typestr = config.getChildData("mappertype", "Mirrored");
167  if (typestr == "auto") {
168  // Guess mapper type, if it was not in DB.
169  const RomInfo* romInfo = config.getReactor().getSoftwareDatabase().fetchRomInfo(rom.getOriginalSHA1());
170  if (!romInfo) {
171  type = guessRomType(rom);
172  } else {
173  type = romInfo->getRomType();
174  }
175  } else {
176  // Use mapper type from config, even if this overrides DB.
177  type = RomInfo::nameToRomType(typestr);
178  }
179 
180  unique_ptr<MSXRom> result;
181  switch (type) {
182  case ROM_MIRRORED:
183  result = make_unique<RomPlain>(
184  config, move(rom), RomPlain::MIRRORED);
185  break;
186  case ROM_NORMAL:
187  result = make_unique<RomPlain>(
188  config, move(rom), RomPlain::NOT_MIRRORED);
189  break;
190  case ROM_MIRRORED0000:
191  case ROM_MIRRORED4000:
192  case ROM_MIRRORED8000:
193  case ROM_MIRROREDC000:
194  result = make_unique<RomPlain>(
195  config, move(rom), RomPlain::MIRRORED,
196  (type & 7) * 0x2000);
197  break;
198  case ROM_NORMAL0000:
199  case ROM_NORMAL4000:
200  case ROM_NORMAL8000:
201  case ROM_NORMALC000:
202  result = make_unique<RomPlain>(
203  config, move(rom), RomPlain::NOT_MIRRORED,
204  (type & 7) * 0x2000);
205  break;
206  case ROM_PAGE0:
207  case ROM_PAGE1:
208  case ROM_PAGE01:
209  case ROM_PAGE2:
210  case ROM_PAGE12:
211  case ROM_PAGE012:
212  case ROM_PAGE3:
213  case ROM_PAGE23:
214  case ROM_PAGE123:
215  case ROM_PAGE0123:
216  result = make_unique<RomPageNN>(config, move(rom), type & 0xF);
217  break;
218  case ROM_DRAM:
219  result = make_unique<RomDRAM>(config, move(rom));
220  break;
221  case ROM_GENERIC_8KB:
222  result = make_unique<RomGeneric8kB>(config, move(rom));
223  break;
224  case ROM_GENERIC_16KB:
225  result = make_unique<RomGeneric16kB>(config, move(rom));
226  break;
227  case ROM_KONAMI_SCC:
228  result = make_unique<RomKonamiSCC>(config, move(rom));
229  break;
230  case ROM_KONAMI:
231  result = make_unique<RomKonami>(config, move(rom));
232  break;
233  case ROM_KBDMASTER:
234  result = make_unique<RomKonamiKeyboardMaster>(config, move(rom));
235  break;
236  case ROM_ASCII8:
237  result = make_unique<RomAscii8kB>(config, move(rom));
238  break;
239  case ROM_ASCII16:
240  result = make_unique<RomAscii16kB>(config, move(rom));
241  break;
242  case ROM_MSXWRITE:
243  result = make_unique<RomMSXWrite>(config, move(rom));
244  break;
245  case ROM_PADIAL8:
246  result = make_unique<RomPadial8kB>(config, move(rom));
247  break;
248  case ROM_PADIAL16:
249  result = make_unique<RomPadial16kB>(config, move(rom));
250  break;
251  case ROM_SUPERLODERUNNER:
252  result = make_unique<RomSuperLodeRunner>(config, move(rom));
253  break;
254  case ROM_SUPERSWANGI:
255  result = make_unique<RomSuperSwangi>(config, move(rom));
256  break;
257  case ROM_MITSUBISHIMLTS2:
258  result = make_unique<RomMitsubishiMLTS2>(config, move(rom));
259  break;
260  case ROM_MSXDOS2:
261  result = make_unique<RomMSXDOS2>(config, move(rom));
262  break;
263  case ROM_R_TYPE:
264  result = make_unique<RomRType>(config, move(rom));
265  break;
266  case ROM_CROSS_BLAIM:
267  result = make_unique<RomCrossBlaim>(config, move(rom));
268  break;
269  case ROM_HARRY_FOX:
270  result = make_unique<RomHarryFox>(config, move(rom));
271  break;
272  case ROM_ASCII8_8:
273  result = make_unique<RomAscii8_8>(
274  config, move(rom), RomAscii8_8::ASCII8_8);
275  break;
276  case ROM_ASCII8_2:
277  result = make_unique<RomAscii8_8>(
278  config, move(rom), RomAscii8_8::ASCII8_2);
279  break;
280  case ROM_KOEI_8:
281  result = make_unique<RomAscii8_8>(
282  config, move(rom), RomAscii8_8::KOEI_8);
283  break;
284  case ROM_KOEI_32:
285  result = make_unique<RomAscii8_8>(
286  config, move(rom), RomAscii8_8::KOEI_32);
287  break;
288  case ROM_WIZARDRY:
289  result = make_unique<RomAscii8_8>(
290  config, move(rom), RomAscii8_8::WIZARDRY);
291  break;
292  case ROM_ASCII16_2:
293  result = make_unique<RomAscii16_2>(config, move(rom), RomAscii16_2::ASCII16_2);
294  break;
295  case ROM_ASCII16_8:
296  result = make_unique<RomAscii16_2>(config, move(rom), RomAscii16_2::ASCII16_8);
297  break;
298  case ROM_GAME_MASTER2:
299  result = make_unique<RomGameMaster2>(config, move(rom));
300  break;
301  case ROM_PANASONIC:
302  result = make_unique<RomPanasonic>(config, move(rom));
303  break;
304  case ROM_NATIONAL:
305  result = make_unique<RomNational>(config, move(rom));
306  break;
307  case ROM_MAJUTSUSHI:
308  result = make_unique<RomMajutsushi>(config, move(rom));
309  break;
310  case ROM_SYNTHESIZER:
311  result = make_unique<RomSynthesizer>(config, move(rom));
312  break;
313  case ROM_PLAYBALL:
314  result = make_unique<RomPlayBall>(config, move(rom));
315  break;
316  case ROM_NETTOU_YAKYUU:
317  result = make_unique<RomNettouYakyuu>(config, move(rom));
318  break;
319  case ROM_HALNOTE:
320  result = make_unique<RomHalnote>(config, move(rom));
321  break;
322  case ROM_ZEMINA80IN1:
323  result = make_unique<RomZemina80in1>(config, move(rom));
324  break;
325  case ROM_ZEMINA90IN1:
326  result = make_unique<RomZemina90in1>(config, move(rom));
327  break;
328  case ROM_ZEMINA126IN1:
329  result = make_unique<RomZemina126in1>(config, move(rom));
330  break;
331  case ROM_HOLY_QURAN:
332  result = make_unique<RomHolyQuran>(config, move(rom));
333  break;
334  case ROM_HOLY_QURAN2:
335  result = make_unique<RomHolyQuran2>(config, move(rom));
336  break;
337  case ROM_FSA1FM1:
338  result = make_unique<RomFSA1FM1>(config, move(rom));
339  break;
340  case ROM_FSA1FM2:
341  result = make_unique<RomFSA1FM2>(config, move(rom));
342  break;
343  case ROM_MANBOW2:
344  case ROM_MANBOW2_2:
345  case ROM_HAMARAJANIGHT:
346  case ROM_MEGAFLASHROMSCC:
347  result = make_unique<RomManbow2>(config, move(rom), type);
348  break;
349  case ROM_MATRAINK:
350  result = make_unique<RomMatraInk>(config, move(rom));
351  break;
352  case ROM_ARC:
353  result = make_unique<RomArc>(config, move(rom));
354  break;
356  result = make_unique<MegaFlashRomSCCPlus>(config, move(rom));
357  break;
359  result = make_unique<MegaFlashRomSCCPlusSD>(config, move(rom));
360  break;
362  result = make_unique<KonamiUltimateCollection>(config, move(rom));
363  break;
364  case ROM_DOOLY:
365  result = make_unique<RomDooly>(config, move(rom));
366  break;
367  case ROM_MSXTRA:
368  result = make_unique<RomMSXtra>(config, move(rom));
369  break;
370  case ROM_MULTIROM:
371  result = make_unique<RomMultiRom>(config, move(rom));
372  break;
373  default:
374  throw MSXException("Unknown ROM type");
375  }
376 
377  // Store actual detected mapper type in config (override the possible
378  // 'auto' value). This way we're sure that on savestate/loadstate we're
379  // using the same mapper type (for example when the user's rom-database
380  // was updated).
381  auto& writableConfig = const_cast<XMLElement&>(*config.getXML());
382  writableConfig.setChildData("mappertype", RomInfo::romTypeToName(type));
383 
384  return move(result);
385 }
386 
387 } // namespace RomFactory
388 } // namespace openmsx
void setChildData(string_ref name, string_ref value)
Definition: XMLElement.cc:217
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
const std::string & getChildData(string_ref name) const
Definition: DeviceConfig.cc:43
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
const Sha1Sum & getOriginalSHA1() const
Definition: Rom.cc:360
const RomInfo * fetchRomInfo(const Sha1Sum &sha1sum) const
Lookup an entry in the database by sha1sum.
Definition: RomDatabase.cc:620
RomDatabase & getSoftwareDatabase()
Definition: Reactor.hh:85
const XMLElement * getXML() const
Definition: DeviceConfig.hh:54
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
const std::string & getAttribute(string_ref attName) const
Definition: DeviceConfig.cc:65
Reactor & getReactor() const
Definition: DeviceConfig.cc:30
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
uint8_t * data()
size_t size() const
unique_ptr< MSXDevice > create(const DeviceConfig &config)
Definition: RomFactory.cc:158
unsigned getSize() const
Definition: Rom.hh:32
static string_ref romTypeToName(RomType type)
Definition: RomInfo.cc:189
RomType getRomType() const
Definition: RomInfo.hh:50
static RomType nameToRomType(string_ref name)
Definition: RomInfo.cc:182