openMSX
RomInfo.cc
Go to the documentation of this file.
1 #include "RomInfo.hh"
2 #include "StringOp.hh"
3 #include "hash_map.hh"
4 #include "ranges.hh"
5 #include "stl.hh"
6 #include "unreachable.hh"
7 #include "xxhash.hh"
8 #include <cassert>
9 
10 using std::string;
11 using std::string_view;
12 using std::vector;
13 
14 namespace openmsx {
15 
16 static inline RomType makeAlias(RomType type)
17 {
18  return static_cast<RomType>(ROM_ALIAS | type);
19 }
20 static inline RomType removeAlias(RomType type)
21 {
22  return static_cast<RomType>(type & ~ROM_ALIAS);
23 }
24 static inline bool isAlias(RomType type)
25 {
26  return (type & ROM_ALIAS) != 0;
27 }
28 
29 
30 static bool isInit = false;
31 
32 // This maps a name to a RomType. There can be multiple names (aliases) for the
33 // same type.
35 static RomTypeMap romTypeMap(256); // initial hashtable size
36  // (big enough so that no rehash is needed)
37 
38 // This contains extra information for each RomType. This structure only
39 // contains the primary (non-alias) romtypes.
40 using RomTypeInfo = std::tuple<
41  RomType, // rom type
42  string_view, // description
43  unsigned>; // blockSize
44 using RomTypeInfoMap = vector<RomTypeInfo>;
46 static RomTypeInfoMap romTypeInfoMap;
47 
48 static void init(RomType type, string_view name,
49  unsigned blockSize, string_view description)
50 {
51  romTypeMap.emplace_noCapacityCheck_noDuplicateCheck(name, type);
52  romTypeInfoMap.emplace_back(type, description, blockSize);
53 }
54 static void initAlias(RomType type, string_view name)
55 {
56  romTypeMap.emplace_noCapacityCheck_noDuplicateCheck(name, makeAlias(type));
57 }
58 static void init()
59 {
60  if (isInit) return;
61  isInit = true;
62 
63  // Generic ROM types that don't exist in real ROMs
64  // (should not occur in any database!)
65  init(ROM_GENERIC_8KB, "8kB", 0x2000, "Generic 8kB");
66  init(ROM_GENERIC_16KB, "16kB", 0x4000, "Generic 16kB");
67 
68  // ROM mapper types for normal software (mainly games)
69  init(ROM_KONAMI, "Konami", 0x2000, "Konami MegaROM");
70  init(ROM_KONAMI_SCC, "KonamiSCC", 0x2000, "Konami with SCC");
71  init(ROM_KBDMASTER, "KeyboardMaster", 0x4000, "Konami Keyboard Master with VLM5030"); // officially plain 16K
72  init(ROM_ASCII8, "ASCII8", 0x2000, "ASCII 8kB");
73  init(ROM_ASCII16, "ASCII16", 0x4000, "ASCII 16kB");
74  init(ROM_R_TYPE, "R-Type", 0x4000, "R-Type");
75  init(ROM_CROSS_BLAIM, "CrossBlaim", 0x4000, "Cross Blaim");
76  init(ROM_HARRY_FOX, "HarryFox", 0x4000, "Harry Fox");
77  init(ROM_HALNOTE, "Halnote", 0x2000, "Halnote");
78  init(ROM_ZEMINA80IN1, "Zemina80in1", 0x2000, "Zemina 80 in 1");
79  init(ROM_ZEMINA90IN1, "Zemina90in1", 0x2000, "Zemina 90 in 1");
80  init(ROM_ZEMINA126IN1, "Zemina126in1", 0x2000, "Zemina 126 in 1");
81  init(ROM_ASCII16_2, "ASCII16SRAM2", 0x4000, "ASCII 16kB with 2kB SRAM");
82  init(ROM_ASCII16_8, "ASCII16SRAM8", 0x4000, "ASCII 16kB with 8kB SRAM");
83  init(ROM_ASCII8_8, "ASCII8SRAM8", 0x2000, "ASCII 8kB with 8kB SRAM");
84  init(ROM_ASCII8_32, "ASCII8SRAM32", 0x2000, "ASCII 8kB with 32kB SRAM");
85  init(ROM_ASCII8_2, "ASCII8SRAM2", 0x2000, "ASCII 8kB with 2kB SRAM");
86  init(ROM_KOEI_8, "KoeiSRAM8", 0x2000, "Koei with 8kB SRAM");
87  init(ROM_KOEI_32, "KoeiSRAM32", 0x2000, "Koei with 32kB SRAM");
88  init(ROM_WIZARDRY, "Wizardry", 0x2000, "Wizardry");
89  init(ROM_GAME_MASTER2, "GameMaster2", 0x1000, "Konami's Game Master 2");
90  init(ROM_MAJUTSUSHI, "Majutsushi", 0x2000, "Hai no Majutsushi");
91  init(ROM_SYNTHESIZER, "Synthesizer", 0x4000, "Konami's Synthesizer"); // officially plain 32K
92  init(ROM_PLAYBALL, "PlayBall", 0x4000, "Sony's PlayBall"); // officially plain 32K
93  init(ROM_NETTOU_YAKYUU, "NettouYakyuu", 0x2000, "Nettou Yakuu");
94  init(ROM_HOLY_QURAN, "AlQuranDecoded", 0x2000, "Holy Qu'ran (pre-decrypted)");
95  init(ROM_HOLY_QURAN2, "AlQuran", 0x2000, "Holy Qu'ran");
96  init(ROM_PADIAL8, "Padial8", 0x2000, "Padial 8kB");
97  init(ROM_PADIAL16, "Padial16", 0x4000, "Padial 16kB");
98  init(ROM_SUPERLODERUNNER,"SuperLodeRunner",0x4000, "Super Lode Runner");
99  init(ROM_SUPERSWANGI, "SuperSwangi" ,0x4000, "Super Swangi");
100  init(ROM_MSXDOS2, "MSXDOS2", 0x4000, "MSX-DOS2");
101  init(ROM_MITSUBISHIMLTS2,"MitsubishiMLTS2",0x2000, "Mitsubishi ML-TS2 firmware");
102  init(ROM_MANBOW2, "Manbow2", 0x2000, "Manbow2");
103  init(ROM_MANBOW2_2, "Manbow2_2", 0x2000, "Manbow2 - Second Release");
104  init(ROM_HAMARAJANIGHT, "HamarajaNight", 0x2000, "Best of Hamaraja Night");
105  init(ROM_MEGAFLASHROMSCC,"MegaFlashRomScc",0x2000, "Mega Flash ROM SCC");
106  init(ROM_MATRAINK, "MatraInk", 0x0000, "Matra Ink");
107  init(ROM_MATRACOMPILATION,"MatraCompilation",0x2000, "Matra Compilation");
108  init(ROM_ARC, "Arc", 0x4000, "Parallax' ARC"); // officially plain 32K
109  init(ROM_ROMHUNTERMK2, "ROMHunterMk2", 0x0000, "ROM Hunter Mk2");
110  init(ROM_DOOLY, "Dooly", 0x4000, "Baby Dinosaur Dooly"); // officially 32K blocksize, but spread over 2 pages
111  init(ROM_MSXTRA, "MSXtra", 0x0000, "PTC MSXtra");
112  init(ROM_MSXWRITE, "MSXWrite", 0x4000, "Japanese MSX Write");
113  init(ROM_MULTIROM, "MultiRom", 0x0000, "MultiRom Collection");
114  init(ROM_RAMFILE, "RAMFILE", 0x0000, "Tecall MSX RAMFILE");
115  init(ROM_COLECOMEGACART, "ColecoMegaCart", 0x4000, "ColecoVision MegaCart");
116  init(ROM_MEGAFLASHROMSCCPLUS,"MegaFlashRomSccPlus",0x0000, "Mega Flash ROM SCC Plus");
117  init(ROM_REPRO_CARTRIDGE1,"ReproCartridgeV1",0x0000, "Repro Cartridge V1");
118  init(ROM_REPRO_CARTRIDGE2,"ReproCartridgeV2",0x0000, "Repro Cartridge V2");
119  init(ROM_KONAMI_ULTIMATE_COLLECTION,"KonamiUltimateCollection",0x0000, "Konami Ultimate Collection");
120 
121  // ROM mapper types used for system ROMs in machines
122  init(ROM_PANASONIC, "Panasonic", 0x2000, "Panasonic internal mapper");
123  init(ROM_NATIONAL, "National", 0x4000, "National internal mapper");
124  init(ROM_FSA1FM1, "FSA1FM1", 0x0000, "Panasonic FS-A1FM internal mapper 1"); // TODO: romblocks debuggable?
125  init(ROM_FSA1FM2, "FSA1FM2", 0x2000, "Panasonic FS-A1FM internal mapper 2");
126  init(ROM_DRAM, "DRAM", 0x2000, "MSXturboR DRAM");
127 
128  // Non-mapper ROM types
129  init(ROM_MIRRORED, "Mirrored", 0x2000, "Plain rom, mirrored (any size)");
130  init(ROM_MIRRORED0000,"Mirrored0000",0x2000, "Plain rom, mirrored start at 0x0000");
131  init(ROM_MIRRORED4000,"Mirrored4000",0x2000, "Plain rom, mirrored start at 0x4000");
132  init(ROM_MIRRORED8000,"Mirrored8000",0x2000, "Plain rom, mirrored start at 0x8000");
133  init(ROM_MIRROREDC000,"MirroredC000",0x2000, "Plain rom, mirrored start at 0xC000");
134  init(ROM_NORMAL, "Normal", 0x2000, "Plain rom (any size)");
135  init(ROM_NORMAL0000, "Normal0000", 0x2000, "Plain rom start at 0x0000");
136  init(ROM_NORMAL4000, "Normal4000", 0x2000, "Plain rom start at 0x4000");
137  init(ROM_NORMAL8000, "Normal8000", 0x2000, "Plain rom start at 0x8000");
138  init(ROM_NORMALC000, "NormalC000", 0x2000, "Plain rom start at 0xC000");
139  init(ROM_PAGE0, "Page0", 0x2000, "Plain 16kB page 0");
140  init(ROM_PAGE1, "Page1", 0x2000, "Plain 16kB page 1");
141  init(ROM_PAGE2, "Page2", 0x2000, "Plain 16kB page 2 (BASIC)");
142  init(ROM_PAGE3, "Page3", 0x2000, "Plain 16kB page 3");
143  init(ROM_PAGE01, "Page01", 0x2000, "Plain 32kB page 0-1");
144  init(ROM_PAGE12, "Page12", 0x2000, "Plain 32kB page 1-2");
145  init(ROM_PAGE23, "Page23", 0x2000, "Plain 32kB page 2-3");
146  init(ROM_PAGE012, "Page012", 0x2000, "Plain 48kB page 0-2");
147  init(ROM_PAGE123, "Page123", 0x2000, "Plain 48kB page 1-3");
148  init(ROM_PAGE0123, "Page0123", 0x2000, "Plain 64kB");
149 
150  // Alternative names for rom types, mainly for backwards compatibility
151  initAlias(ROM_GENERIC_8KB, "0");
152  initAlias(ROM_GENERIC_8KB, "GenericKonami"); // probably actually used in a Zemina Box
153  initAlias(ROM_GENERIC_16KB,"1");
154  initAlias(ROM_KONAMI_SCC, "2");
155  initAlias(ROM_KONAMI_SCC, "SCC");
156  initAlias(ROM_KONAMI_SCC, "KONAMI5");
157  initAlias(ROM_KONAMI, "KONAMI4");
158  initAlias(ROM_KONAMI, "3");
159  initAlias(ROM_ASCII8, "4");
160  initAlias(ROM_ASCII16, "5");
161  initAlias(ROM_MIRRORED, "64kB");
162  initAlias(ROM_MIRRORED, "Plain");
163  initAlias(ROM_NORMAL0000, "0x0000");
164  initAlias(ROM_NORMAL4000, "0x4000");
165  initAlias(ROM_NORMAL8000, "0x8000");
166  initAlias(ROM_NORMALC000, "0xC000");
167  initAlias(ROM_ASCII16_2, "HYDLIDE2");
168  initAlias(ROM_GAME_MASTER2,"RC755");
169  initAlias(ROM_NORMAL8000, "ROMBAS");
170  initAlias(ROM_R_TYPE, "RTYPE");
171  initAlias(ROM_ZEMINA80IN1, "KOREAN80IN1");
172  initAlias(ROM_ZEMINA90IN1, "KOREAN90IN1");
173  initAlias(ROM_ZEMINA126IN1,"KOREAN126IN1");
174  initAlias(ROM_HOLY_QURAN, "HolyQuran");
175 
176  ranges::sort(romTypeInfoMap, RomTypeInfoMapLess());
177 }
178 static const RomTypeMap& getRomTypeMap()
179 {
180  init();
181  return romTypeMap;
182 }
183 static const RomTypeInfoMap& getRomTypeInfoMap()
184 {
185  init();
186  return romTypeInfoMap;
187 }
188 
189 RomType RomInfo::nameToRomType(string_view name)
190 {
191  auto v = lookup(getRomTypeMap(), name);
192  return v ? removeAlias(*v) : ROM_UNKNOWN;
193 }
194 
195 string_view RomInfo::romTypeToName(RomType type)
196 {
197  assert(!isAlias(type));
198  for (const auto& [name, romType] : getRomTypeMap()) {
199  if (romType == type) {
200  return name;
201  }
202  }
203  UNREACHABLE; return {};
204 }
205 
206 vector<string_view> RomInfo::getAllRomTypes()
207 {
208  vector<string_view> result;
209  for (const auto& [name, romType] : getRomTypeMap()) {
210  if (!isAlias(romType)) {
211  result.push_back(name);
212  }
213  }
214  return result;
215 }
216 
217 string_view RomInfo::getDescription(RomType type)
218 {
219  auto& m = getRomTypeInfoMap();
220  auto it = ranges::lower_bound(m, type, RomTypeInfoMapLess());
221  assert(it != end(m));
222  assert(std::get<0>(*it) == type);
223  return std::get<1>(*it);
224 }
225 
226 unsigned RomInfo::getBlockSize(RomType type)
227 {
228  auto& m = getRomTypeInfoMap();
229  auto it = ranges::lower_bound(m, type, RomTypeInfoMapLess());
230  assert(it != end(m));
231  assert(std::get<0>(*it) == type);
232  return std::get<2>(*it);
233 }
234 
235 } // namespace openmsx
iterator emplace_noCapacityCheck_noDuplicateCheck(Args &&... args)
Definition: hash_set.hh:473
vector< RomTypeInfo > RomTypeInfoMap
Definition: RomInfo.cc:44
const Value * lookup(const hash_map< Key, Value, Hasher, Equal > &map, const Key2 &key)
Definition: hash_map.hh:91
static unsigned getBlockSize(RomType type)
Definition: RomInfo.cc:226
static std::string_view romTypeToName(RomType type)
Definition: RomInfo.cc:195
static RomType nameToRomType(std::string_view name)
Definition: RomInfo.cc:189
LessTupleElement< 0 > RomTypeInfoMapLess
Definition: RomInfo.cc:45
static std::vector< std::string_view > getAllRomTypes()
Definition: RomInfo.cc:206
void sort(RandomAccessRange &&range)
Definition: ranges.hh:35
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
std::tuple< RomType, string_view, unsigned > RomTypeInfo
Definition: RomInfo.cc:43
auto lower_bound(ForwardRange &&range, const T &value)
Definition: ranges.hh:71
static std::string_view getDescription(RomType type)
Definition: RomInfo.cc:217
#define UNREACHABLE
Definition: unreachable.hh:38