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