Evdev joystick plugin
[lsnes.git] / generic / rom.hpp
blob57b3c1fc20990165feaf5d6b2a4e5455045ad372
1 #ifndef _rom__hpp__included__
2 #define _rom__hpp__included__
4 #include <string>
5 #include <map>
6 #include <vector>
7 #include <stdexcept>
8 #include "misc.hpp"
10 /**
11 * Region of ROM.
13 enum rom_region
15 /**
16 * Autodetect region
18 REGION_AUTO = 0,
19 /**
20 * (force) PAL region
22 REGION_PAL,
23 /**
24 * (force) NTSC region
26 REGION_NTSC
29 /**
30 * Major type of ROM
32 enum rom_type
34 /**
35 * Ordinary SNES ROM
37 ROMTYPE_SNES,
38 /**
39 * BS-X Slotted ROM.
41 ROMTYPE_BSXSLOTTED,
42 /**
43 * BS-X (non-slotted) ROM.
45 ROMTYPE_BSX,
46 /**
47 * Sufami Turbo ROM.
49 ROMTYPE_SUFAMITURBO,
50 /**
51 * Super Game Boy ROM.
53 ROMTYPE_SGB,
54 /**
55 * No ROM.
57 ROMTYPE_NONE
60 /**
61 * This enumeration enumerates possible ROM types and regions for those.
63 enum gametype_t
65 /**
66 * NTSC-region SNES game
68 GT_SNES_NTSC = 0,
69 /**
70 * PAL-region SNES game
72 GT_SNES_PAL = 1,
73 /**
74 * NTSC-region BSX slotted game
76 GT_BSX_SLOTTED = 2,
77 /**
78 * NTSC-region BSX (non-slotted) game
80 GT_BSX = 3,
81 /**
82 * NTSC-region sufami turbo game
84 GT_SUFAMITURBO = 4,
85 /**
86 * NTSC-region Super Game Boy game
88 GT_SGB_NTSC = 5,
89 /**
90 * PAL-region Super Game Boy game
92 GT_SGB_PAL = 6,
93 /**
94 * Invalid game type
96 GT_INVALID = 7
99 /**
100 * Translations between diffrent representations of type.
102 class gtype
104 public:
106 * Translate from major ROM type and region to string representation of the type.
108 * parameter rtype: The major ROM type.
109 * parameter region: Region.
110 * returns: String representation of combined type/region.
111 * throws std::bad_alloc: Not enough memory.
112 * throws std::runtime_error: Invalid type.
114 static std::string tostring(rom_type rtype, rom_region region) throw(std::bad_alloc, std::runtime_error);
116 * Translate major/region combination to string representation.
118 * This function produces the same IDs as the other tostring(), except that it can't produce arbitrary-region ones.
120 * parameter gametype: Type of the game.
121 * returns: String representation of the type.
122 * throws std::bad_alloc: Not enough memory.
123 * throws std::runtime_error: Invalid type.
125 static std::string tostring(gametype_t gametype) throw(std::bad_alloc, std::runtime_error);
127 * Combine major/region into game type.
129 * For arbitrary-region types, this gives NTSC types.
131 * parameter rtype: Major type.
132 * parameter region: The region.
133 * returns: The combined game type.
134 * throws std::bad_alloc: Not enough memory.
135 * throws std::runtime_error: Invalid type.
137 static gametype_t togametype(rom_type rtype, rom_region region) throw(std::bad_alloc, std::runtime_error);
139 * Parse string representation to game type.
141 * parameter gametype: The game type to parse.
142 * returns: The parsed game type.
143 * throws std::bad_alloc: Not enough memory.
144 * throws std::runtime_error: Invalid type.
146 static gametype_t togametype(const std::string& gametype) throw(std::bad_alloc, std::runtime_error);
148 * Parse string representation into major type.
150 * parameter gametype: The game type to parse.
151 * returns: The major type.
152 * throws std::bad_alloc: Not enough memory.
153 * throws std::runtime_error: Invalid type.
155 static rom_type toromtype(const std::string& gametype) throw(std::bad_alloc, std::runtime_error);
157 * Extract major type out of game type.
159 * parameter gametype: the game type to parse.
160 * returns: The major type.
162 static rom_type toromtype(gametype_t gametype) throw();
164 * Extract region out of game type.
166 * parameter gametype: the game type to parse.
167 * returns: The region.
168 * throws std::bad_alloc: Not enough memory.
169 * throws std::runtime_error: Invalid type.
171 static rom_region toromregion(const std::string& gametype) throw(std::bad_alloc, std::runtime_error);
173 * Extract region out of game type.
175 * parameter gametype: the game type to parse.
176 * returns: The region.
178 static rom_region toromregion(gametype_t gametype) throw();
182 * This structure gives all files associated with given ROM image.
184 struct rom_files
187 * Construct defaults
189 rom_files() throw();
192 * Reads the filenames out of command line arguments given.
194 * parameter cmdline: The commmand line
195 * throws std::bad_alloc: Not enough memory
196 * throws std::runtime_error: Failed to load ROM filenames.
198 rom_files(const std::vector<std::string>& cmdline) throw(std::bad_alloc, std::runtime_error);
201 * Resolve relative references.
203 * throws std::bad_alloc: Not enough memory
204 * throws std::runtime_error: Bad path.
206 void resolve_relative() throw(std::bad_alloc, std::runtime_error);
209 * The file to look other ROM files relative to. May be blank.
211 std::string base_file;
213 * Major ROM type.
215 enum rom_type rtype;
217 * Game region (the region ROM is to be loaded as)
219 enum rom_region region;
221 * Relative filename of main ROM file.
223 std::string rom;
225 * Relative filename of main ROM XML file.
227 std::string rom_xml;
229 * Relative filename of slot A ROM file (non-SNES only).
231 std::string slota;
233 * Relative filename of slot A XML file (non-SNES only).
235 std::string slota_xml;
237 * Relative filename of slot B ROM file (Sufami Turbo only).
239 std::string slotb;
241 * Relative filename of slot B XML file (Sufami Turbo only).
243 std::string slotb_xml;
247 * Some loaded data or indication of no data.
249 struct loaded_slot
252 * Construct empty slot.
254 * throws std::bad_alloc: Not enough memory.
256 loaded_slot() throw(std::bad_alloc);
259 * This constructor construct slot by reading data from file. If filename is "", constructs an empty slot.
261 * parameter filename: The filename to read. If "", empty slot is constructed.
262 * parameter base: Base filename to interpret the filename against. If "", no base filename is used.
263 * parameter xml_flag: If set, always keep trailing NUL.
264 * throws std::bad_alloc: Not enough memory.
265 * throws std::runtime_error: Can't load the data.
267 loaded_slot(const std::string& filename, const std::string& base, bool xml_flag = false) throw(std::bad_alloc,
268 std::runtime_error);
271 * This method patches this slot using specified IPS patch.
273 * parameter patch: The patch to apply
274 * parameter offset: The amount to add to the offsets in the IPS file. Parts with offsets below zero are not patched.
275 * throws std::bad_alloc: Not enough memory.
276 * throws std::runtime_error: Bad IPS patch.
278 void patch(const std::vector<char>& patch, int32_t offset) throw(std::bad_alloc, std::runtime_error);
281 * Is this slot XML slot?
283 bool xml;
285 * If this slot is blank, this is set to false, data is undefined and sha256 is "". Otherwise this is set to true,
286 * data to apporiate data, and sha256 to hash of data.
288 bool valid;
290 * The actual data for this slot.
292 std::vector<char> data;
294 * SHA-256 for the data in this slot if data is valid. If no valid data, this field is "".
296 std::string sha256;
298 * Get pointer to loaded data
300 * returns: Pointer to loaded data, or NULL if slot is blank.
302 operator const char*() const throw()
304 return valid ? reinterpret_cast<const char*>(&data[0]) : NULL;
307 * Get pointer to loaded data
309 * returns: Pointer to loaded data, or NULL if slot is blank.
311 operator const uint8_t*() const throw()
313 return valid ? reinterpret_cast<const uint8_t*>(&data[0]) : NULL;
316 * Get size of slot
318 * returns: The number of bytes in slot, or 0 if slot is blank.
320 operator unsigned() const throw()
322 return valid ? data.size() : 0;
327 * ROM loaded into memory.
329 struct loaded_rom
332 * Create blank ROM
334 loaded_rom() throw();
336 * Takes in collection of ROM filenames and loads them into memory.
338 * parameter files: The files to load
339 * throws std::bad_alloc: Not enough memory.
340 * throws std::runtime_error: Loading ROM files failed.
342 loaded_rom(const rom_files& files) throw(std::bad_alloc, std::runtime_error);
344 * ROM type
346 enum rom_type rtype;
348 * ROM region (this is the currently active region).
350 enum rom_region region;
352 * ROM original region (this is the region ROM is loaded as).
354 enum rom_region orig_region;
356 * Loaded main ROM
358 loaded_slot rom;
360 * Loaded main ROM XML
362 loaded_slot rom_xml;
364 * Loaded slot A ROM (.bs, .st or .dmg)
366 loaded_slot slota;
368 * Loaded slot A XML (.bs, .st or .dmg)
370 loaded_slot slota_xml;
372 * Loaded slot B ROM (.st)
374 loaded_slot slotb;
376 * Loaded slot B XML (.st).
378 loaded_slot slotb_xml;
381 * Patch the ROM.
383 * parameter cmdline: The command line.
384 * throws std::bad_alloc: Not enough memory.
385 * throws std::runtime_error: Failed to patch the ROM.
387 void do_patch(const std::vector<std::string>& cmdline) throw(std::bad_alloc, std::runtime_error);
390 * Switches the active cartridge to this cartridge. The compatiblity between selected region and original region
391 * is checked. Region is updated after cartridge has been loaded.
393 * throws std::bad_alloc: Not enough memory
394 * throws std::runtime_error: Switching cartridges failed.
396 void load() throw(std::bad_alloc, std::runtime_error);
400 * Recognize the slot this ROM goes to.
402 * parameter major: The major type.
403 * parameter romname: Name of the ROM type.
404 * returns: Even if this is main rom, odd if XML. 0/1 for main slot, 2/3 for slot A, 4/5 for slot B. -1 if not valid
405 * rom type.
406 * throws std::bad_alloc: Not enough memory
408 int recognize_commandline_rom(enum rom_type major, const std::string& romname) throw(std::bad_alloc);
411 * Recognize major type from flags.
413 * parameter flags: Flags telling what ROM parameters are present.
414 * returns: The recognzed major type.
415 * throws std::bad_alloc: Not enough memory
416 * throws std::runtime_error: Illegal flags.
418 rom_type recognize_platform(unsigned long flags) throw(std::bad_alloc, std::runtime_error);
421 * Name a sub-ROM.
423 * parameter major: The major type.
424 * parameter romnumber: ROM number to name (as returned by recognize_commandline_rom).
425 * throws std::bad_alloc: Not enough memory
427 std::string name_subrom(enum rom_type major, unsigned romnumber) throw(std::bad_alloc);
430 * Get major type and region of loaded ROM.
432 * returns: Tuple (ROM type, ROM region) of currently loaded ROM.
434 std::pair<enum rom_type, enum rom_region> get_current_rom_info() throw();
437 * Take current values of all SRAMs in current system and save their contents.
439 * returns: Saved SRAM contents.
440 * throws std::bad_alloc: Out of memory.
442 std::map<std::string, std::vector<char>> save_sram() throw(std::bad_alloc);
445 * Write contents of saved SRAMs into current system SRAMs.
447 * parameter sram: Saved SRAM contents.
448 * throws std::bad_alloc: Out of memory.
450 void load_sram(std::map<std::string, std::vector<char>>& sram) throw(std::bad_alloc);
453 * Read SRAMs from command-line and and load the files.
455 * parameter cmdline: Command line
456 * returns: The loaded SRAM contents.
457 * throws std::bad_alloc: Out of memory.
458 * throws std::runtime_error: Failed to load.
460 std::map<std::string, std::vector<char>> load_sram_commandline(const std::vector<std::string>& cmdline)
461 throw(std::bad_alloc, std::runtime_error);
464 * Saves core state into buffer. WARNING: This takes emulated time.
466 * returns: The saved state.
467 * throws std::bad_alloc: Not enough memory.
469 std::vector<char> save_core_state() throw(std::bad_alloc);
472 * Loads core state from buffer.
474 * parameter buf: The buffer containing the state.
475 * throws std::runtime_error: Loading state failed.
477 void load_core_state(const std::vector<char>& buf) throw(std::runtime_error);
480 * Read index of ROMs and add ROMs found to content-searchable storage.
482 * parameter filename: The filename of index file.
483 * throws std::bad_alloc: Not enough memory.
484 * throws std::runtime_error: Loading index failed.
486 void load_index_file(const std::string& filename) throw(std::bad_alloc, std::runtime_error);
489 * Search all indices, looking for file with specified SHA-256 (specifying hash of "" results "").
491 * parameter hash: The hash of file.
492 * returns: Absolute filename.
493 * throws std::bad_alloc: Not enough memory.
494 * throws std::runtime_error: Not found.
496 std::string lookup_file_by_sha256(const std::string& hash) throw(std::bad_alloc, std::runtime_error);
498 #endif