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