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_32:
277  result = make_unique<RomAscii8_8>(
278  config, move(rom), RomAscii8_8::ASCII8_32);
279  break;
280  case ROM_ASCII8_2:
281  result = make_unique<RomAscii8_8>(
282  config, move(rom), RomAscii8_8::ASCII8_2);
283  break;
284  case ROM_KOEI_8:
285  result = make_unique<RomAscii8_8>(
286  config, move(rom), RomAscii8_8::KOEI_8);
287  break;
288  case ROM_KOEI_32:
289  result = make_unique<RomAscii8_8>(
290  config, move(rom), RomAscii8_8::KOEI_32);
291  break;
292  case ROM_WIZARDRY:
293  result = make_unique<RomAscii8_8>(
294  config, move(rom), RomAscii8_8::WIZARDRY);
295  break;
296  case ROM_ASCII16_2:
297  result = make_unique<RomAscii16_2>(config, move(rom), RomAscii16_2::ASCII16_2);
298  break;
299  case ROM_ASCII16_8:
300  result = make_unique<RomAscii16_2>(config, move(rom), RomAscii16_2::ASCII16_8);
301  break;
302  case ROM_GAME_MASTER2:
303  result = make_unique<RomGameMaster2>(config, move(rom));
304  break;
305  case ROM_PANASONIC:
306  result = make_unique<RomPanasonic>(config, move(rom));
307  break;
308  case ROM_NATIONAL:
309  result = make_unique<RomNational>(config, move(rom));
310  break;
311  case ROM_MAJUTSUSHI:
312  result = make_unique<RomMajutsushi>(config, move(rom));
313  break;
314  case ROM_SYNTHESIZER:
315  result = make_unique<RomSynthesizer>(config, move(rom));
316  break;
317  case ROM_PLAYBALL:
318  result = make_unique<RomPlayBall>(config, move(rom));
319  break;
320  case ROM_NETTOU_YAKYUU:
321  result = make_unique<RomNettouYakyuu>(config, move(rom));
322  break;
323  case ROM_HALNOTE:
324  result = make_unique<RomHalnote>(config, move(rom));
325  break;
326  case ROM_ZEMINA80IN1:
327  result = make_unique<RomZemina80in1>(config, move(rom));
328  break;
329  case ROM_ZEMINA90IN1:
330  result = make_unique<RomZemina90in1>(config, move(rom));
331  break;
332  case ROM_ZEMINA126IN1:
333  result = make_unique<RomZemina126in1>(config, move(rom));
334  break;
335  case ROM_HOLY_QURAN:
336  result = make_unique<RomHolyQuran>(config, move(rom));
337  break;
338  case ROM_HOLY_QURAN2:
339  result = make_unique<RomHolyQuran2>(config, move(rom));
340  break;
341  case ROM_FSA1FM1:
342  result = make_unique<RomFSA1FM1>(config, move(rom));
343  break;
344  case ROM_FSA1FM2:
345  result = make_unique<RomFSA1FM2>(config, move(rom));
346  break;
347  case ROM_MANBOW2:
348  case ROM_MANBOW2_2:
349  case ROM_HAMARAJANIGHT:
350  case ROM_MEGAFLASHROMSCC:
351  result = make_unique<RomManbow2>(config, move(rom), type);
352  break;
353  case ROM_MATRAINK:
354  result = make_unique<RomMatraInk>(config, move(rom));
355  break;
356  case ROM_ARC:
357  result = make_unique<RomArc>(config, move(rom));
358  break;
360  result = make_unique<MegaFlashRomSCCPlus>(config, move(rom));
361  break;
363  result = make_unique<MegaFlashRomSCCPlusSD>(config, move(rom));
364  break;
366  result = make_unique<KonamiUltimateCollection>(config, move(rom));
367  break;
368  case ROM_DOOLY:
369  result = make_unique<RomDooly>(config, move(rom));
370  break;
371  case ROM_MSXTRA:
372  result = make_unique<RomMSXtra>(config, move(rom));
373  break;
374  case ROM_MULTIROM:
375  result = make_unique<RomMultiRom>(config, move(rom));
376  break;
377  default:
378  throw MSXException("Unknown ROM type");
379  }
380 
381  // Store actual detected mapper type in config (override the possible
382  // 'auto' value). This way we're sure that on savestate/loadstate we're
383  // using the same mapper type (for example when the user's rom-database
384  // was updated).
385  auto& writableConfig = const_cast<XMLElement&>(*config.getXML());
386  writableConfig.setChildData("mappertype", RomInfo::romTypeToName(type));
387 
388  return move(result);
389 }
390 
391 } // namespace RomFactory
392 } // 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
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
Reactor & getReactor() const
Definition: DeviceConfig.cc:30
RomDatabase & getSoftwareDatabase()
Definition: Reactor.hh:85
const Sha1Sum & getOriginalSHA1() const
Definition: Rom.cc:360
RomType getRomType() const
Definition: RomInfo.hh:50
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
size_t size() const
const RomInfo * fetchRomInfo(const Sha1Sum &sha1sum) const
Lookup an entry in the database by sha1sum.
Definition: RomDatabase.cc:620
const std::string & getChildData(string_ref name) const
Definition: DeviceConfig.cc:43
unsigned getSize() const
Definition: Rom.hh:32
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
uint8_t * data()
const XMLElement * getXML() const
Definition: DeviceConfig.hh:54
unique_ptr< MSXDevice > create(const DeviceConfig &config)
Definition: RomFactory.cc:158
const std::string & getAttribute(string_ref attName) const
Definition: DeviceConfig.cc:65
static string_ref romTypeToName(RomType type)
Definition: RomInfo.cc:190
static RomType nameToRomType(string_ref name)
Definition: RomInfo.cc:183