Lua: Fix type confusion between signed and unsigned
[lsnes.git] / include / interface / romtype.hpp
blobeb5c86fde5b384f2a4610c0aed1057e39a10fef4
1 #ifndef _interface__romtype__hpp__included__
2 #define _interface__romtype__hpp__included__
4 #include <list>
5 #include <string>
6 #include <cstdint>
7 #include <vector>
8 #include "interface/controller.hpp"
9 #include "interface/setting.hpp"
10 #include "library/framebuffer.hpp"
11 #include "library/threads.hpp"
12 #include "library/loadlib.hpp"
14 struct core_region;
15 struct core_type;
16 struct core_sysregion;
17 struct core_romimage;
18 struct core_romimage_info;
19 struct core_core;
21 /**
22 * The module currently being loaded.
24 extern thread_local const loadlib::module* module_loading;
26 /**
27 * Interface device register.
29 struct interface_device_reg
31 const char* name;
32 uint64_t (*read)();
33 void (*write)(uint64_t v);
34 bool boolean;
37 /**
38 * An parameter for action in interface.
40 struct interface_action_param
42 //Name of the action parameter.
43 const char* name;
44 //Model of the action parameter.
45 //bool: Boolean.
46 //int:<val>,<val>: Integer in specified range.
47 //string[:<regex>]: String with regex.
48 //enum:<json-array>: Enumeration.
49 //toggle: Not a real parameter, boolean toggle, must be the first.
50 const char* model;
53 /**
54 * Value for action in interface.
56 struct interface_action_paramval
58 std::string s;
59 int64_t i;
60 bool b;
63 /**
64 * An action for interface.
66 struct interface_action
68 unsigned id;
69 const char* _title;
70 const char* _symbol;
71 std::list<interface_action_param> params;
72 std::string get_title() const { return _title; }
73 std::string get_symbol() const { return _symbol; }
74 bool is_toggle() const;
78 /**
79 * Parameters about a region (e.g. NTSC, PAL, World).
81 * Both movies ("run region") and ROMs ("cart region") have regions. The set of regions allowed for latter is superset
82 * of former, since autodetection regions are allowed for carts but not runs.
84 struct core_region_params
86 /**
87 * Internal name of the region. Should be all-lowercase.
89 const char* iname;
90 /**
91 * Human-readable name of the region.
93 const char* hname;
94 /**
95 * Priority of region. Higher numbers are more preferred when tiebreaking.
97 unsigned priority;
98 /**
99 * ID number of region. Unique among regions allowed for given core type.
101 unsigned handle;
103 * Multi-region flag. If set, this region gets further resolved by autodetection.
105 bool multi;
107 * Nominal framerate. The first number is numerator, the second is denominator.
109 uint64_t framemagic[2];
111 * ID numbers of regions of runs compatible with this cartridge region.
113 std::vector<unsigned> compatible_runs;
117 * Parameters about individual ROM image.
119 struct core_romimage_info_params
122 * Internal name of ROM image.
124 const char* iname;
126 * Human-readable name of ROM image.
128 const char* hname;
130 * Upon loading a set of ROM images, the OR of mandatory fields of all present ROM images must equal OR of mandatory
131 * fields of all possible ROM images.
133 unsigned mandatory;
135 * The way image is passed:
136 * 0 => Pass by content.
137 * 1 => Pass by filename.
138 * 2 => Pass by directory.
140 int pass_mode;
142 * Size of optional copier header to remove. 0 means there never is copier header.
144 unsigned headersize;
146 * Standard extensions (split by ;).
148 const char* extensions;
151 struct core_romimage_info
153 core_romimage_info(const core_romimage_info_params& params);
154 std::string iname;
155 std::string hname;
156 unsigned mandatory;
157 int pass_mode;
158 unsigned headersize;
159 std::set<std::string> extensions;
160 size_t get_headnersize(size_t imagesize);
164 * Collection of ROM images.
166 struct core_romimage_info_collection
168 core_romimage_info_collection();
169 core_romimage_info_collection(std::initializer_list<core_romimage_info_params> idata);
170 core_romimage_info_collection(std::vector<core_romimage_info_params> idata);
171 std::vector<core_romimage_info> get() const { return data; }
172 private:
173 std::vector<core_romimage_info> data;
177 * A Virtual Memory Area (VMA), which is a chunk of lsnes memory space.
179 * Mapping stuff above 4PB should be avoided.
181 struct core_vma_info
183 core_vma_info()
185 backing_ram = NULL;
186 readonly = false;
187 volatile_flag = false;
188 special = false;
189 read = NULL;
190 write = NULL;
193 * Name of the VMA.
195 std::string name;
197 * Base address of the VMA.
199 uint64_t base;
201 * Size of the VMA.
203 uint64_t size;
205 * Direct backing RAM for the VMA. May be NULL.
207 void* backing_ram;
209 * If true, the VMA can't be written to.
211 bool readonly;
213 * If true, the VMA has special read/write semantics (no consistency assumed).
215 bool special;
217 * Default endianess. -1 => Little endian, 0 => The same as host system, 1 => Big endian.
219 int endian;
221 * If true, the VMA is volatile.
223 bool volatile_flag;
225 * If backing_ram is NULL, this routine is used to access the memory one byte at a time.
227 * Parameter offset: The offset into VMA to access.
228 * Returns: The read value.
230 uint8_t (*read)(uint64_t offset);
232 * If backing_ram is NULL, this routine is used to access the memory one byte at a time.
234 * Parameter offset: The offset into VMA to access.
235 * Parameter data: Byte to write.
237 void (*write)(uint64_t offset, uint8_t data);
241 * Parameters about system type.
243 * Each system type may have its own regions, image slots and controller configuration.
245 struct core_type_params
248 * Internal name of the system type.
250 const char* iname;
252 * Human-readable name of the system type.
254 const char* hname;
256 * ID of system type. Must be unique among core system is valid for.
258 unsigned id;
260 * System menu name.
262 const char* sysname;
264 * The name of BIOS for this system. NULL if there is no bios.
266 const char* bios;
268 * List of regions valid for this system type.
270 std::vector<core_region*> regions;
272 * List of image slots for this system type.
274 core_romimage_info_collection images;
276 * Description of settings for this system type.
278 core_setting_group settings;
280 * Core this system is emulated by.
282 core_core* core;
285 struct core_region
287 public:
288 core_region(const core_region_params& params);
289 core_region(std::initializer_list<core_region_params> p) : core_region(*p.begin()) {}
290 const std::string& get_iname();
291 const std::string& get_hname();
292 unsigned get_priority();
293 unsigned get_handle();
294 bool is_multi();
295 void fill_framerate_magic(uint64_t* magic); //4 elements filled.
296 double approx_framerate();
297 bool compatible_with(core_region& run);
298 private:
299 core_region(const core_region&);
300 core_region& operator=(const core_region&);
301 std::string iname;
302 std::string hname;
303 bool multi;
304 unsigned handle;
305 unsigned priority;
306 uint64_t magic[4];
307 std::vector<unsigned> compatible;
310 struct core_romimage
312 const char* markup;
313 const unsigned char* data;
314 size_t size;
317 struct core_core
319 core_core(std::initializer_list<portctrl::type*> ports, std::initializer_list<interface_action> actions);
320 core_core(std::vector<portctrl::type*> ports, std::vector<interface_action> actions);
321 virtual ~core_core() throw();
322 bool set_region(core_region& region);
323 std::pair<uint32_t, uint32_t> get_video_rate();
324 double get_PAR();
325 std::pair<uint32_t, uint32_t> get_audio_rate();
326 std::string get_core_identifier() const;
327 std::map<std::string, std::vector<char>> save_sram() throw(std::bad_alloc);
328 void load_sram(std::map<std::string, std::vector<char>>& sram) throw(std::bad_alloc);
329 void serialize(std::vector<char>& out);
330 void unserialize(const char* in, size_t insize);
331 core_region& get_region();
332 void power();
333 void unload_cartridge();
334 std::pair<uint32_t, uint32_t> get_scale_factors(uint32_t width, uint32_t height);
335 void install_handler();
336 void uninstall_handler();
337 void emulate();
338 void runtosave();
339 bool get_pflag();
340 void set_pflag(bool pflag);
341 framebuffer::raw& draw_cover();
342 std::vector<portctrl::type*> get_port_types() { return port_types; }
343 std::string get_core_shortname() const;
344 void pre_emulate_frame(portctrl::frame& cf);
345 void execute_action(unsigned id, const std::vector<interface_action_paramval>& p);
346 unsigned action_flags(unsigned id);
347 std::pair<uint64_t, uint64_t> get_bus_map();
348 std::list<core_vma_info> vma_list();
349 std::set<std::string> srams();
350 static std::set<core_core*> all_cores();
351 static void install_all_handlers();
352 static void uninstall_all_handlers();
353 static void initialize_new_cores();
354 void hide() { hidden = true; }
355 bool is_hidden() const { return hidden; }
356 std::set<const interface_action*> get_actions();
357 const interface_device_reg* get_registers();
358 int reset_action(bool hard);
359 std::pair<unsigned, unsigned> lightgun_scale();
360 void set_debug_flags(uint64_t addr, unsigned flags_set, unsigned flags_clear);
361 void set_cheat(uint64_t addr, uint64_t value, bool set);
362 std::vector<std::string> get_trace_cpus();
363 void debug_reset();
364 bool isnull() const;
365 void reset_to_load() { c_reset_to_load(); }
366 bool safe_to_unload(loadlib::module& mod) { return !mod.is_marked(this); }
367 protected:
369 * Get the name of the core.
371 virtual std::string c_core_identifier() const = 0;
373 * Set the current region.
375 * Parameter region: The new region.
376 * Returns: True on success, false on failure (bad region).
378 virtual bool c_set_region(core_region& region) = 0;
380 * Get current video frame rate as (numerator, denominator).
382 virtual std::pair<uint32_t, uint32_t> c_video_rate() = 0;
384 * Get PAR of video output.
386 virtual double c_get_PAR() = 0;
388 * Get audio sampling rate as (numerator, denominator).
390 * Note: This value should not be changed while ROM is running, since video dumper may malfunction.
392 virtual std::pair<uint32_t, uint32_t> c_audio_rate() = 0;
394 * Save all SRAMs.
396 virtual std::map<std::string, std::vector<char>> c_save_sram() throw(std::bad_alloc) = 0;
398 * Load all SRAMs.
400 * Note: Must handle SRAM being missing or shorter or longer than expected.
402 virtual void c_load_sram(std::map<std::string, std::vector<char>>& sram) throw(std::bad_alloc) = 0;
404 * Serialize the system state.
406 virtual void c_serialize(std::vector<char>& out) = 0;
408 * Unserialize the system state.
410 virtual void c_unserialize(const char* in, size_t insize) = 0;
412 * Get current region.
414 virtual core_region& c_get_region() = 0;
416 * Poweron the console.
418 virtual void c_power() = 0;
420 * Unload the cartridge from the console.
422 virtual void c_unload_cartridge() = 0;
424 * Get the current scale factors for screen as (xscale, yscale).
426 virtual std::pair<uint32_t, uint32_t> c_get_scale_factors(uint32_t width, uint32_t height) = 0;
428 * Do basic core initialization. Called on lsnes startup.
430 virtual void c_install_handler() = 0;
432 * Do basic core uninitialization. Called on lsnes shutdown.
434 virtual void c_uninstall_handler() = 0;
436 * Emulate one frame.
438 virtual void c_emulate() = 0;
440 * Get core into state where saving is possible. Must run less than one frame.
442 virtual void c_runtosave() = 0;
444 * Get the polled flag.
446 * The emulator core sets polled flag when the game asks for input.
448 * If polled flag is clear when frame ends, the frame is marked as lag.
450 virtual bool c_get_pflag() = 0;
452 * Set the polled flag.
454 virtual void c_set_pflag(bool pflag) = 0;
456 * Draw run cover screen.
458 * Should display information about the ROM loaded.
460 virtual framebuffer::raw& c_draw_cover() = 0;
462 * Get shortened name of the core.
464 virtual std::string c_get_core_shortname() const = 0;
466 * Set the system controls to appropriate values for next frame.
468 * E.g. if core supports resetting, set the reset button in the frame to pressed if reset is wanted.
470 virtual void c_pre_emulate_frame(portctrl::frame& cf) = 0;
472 * Execute action.
474 virtual void c_execute_action(unsigned id, const std::vector<interface_action_paramval>& p) = 0;
476 * Get set of interface device registers.
478 virtual const struct interface_device_reg* c_get_registers() = 0;
480 * Get action flags on specified action.
482 * Bit 0: Enabled.
483 * Bit 1: Selected.
485 virtual unsigned c_action_flags(unsigned id) = 0;
487 * Get reset action.
489 * Parameter hard: If true, get info for hard reset instead of soft.
490 * Retunrs: The ID of action. -1 if not supported.
492 virtual int c_reset_action(bool hard) = 0;
494 * Get bus mapping.
496 * Returns: The bus mapping (base,size), or (0,0) if this system does not have bus mapping.
498 virtual std::pair<uint64_t, uint64_t> c_get_bus_map() = 0;
500 * Get list of valid VMAs. ROM must be loaded.
502 * Returns: The list of VMAs.
504 virtual std::list<core_vma_info> c_vma_list() = 0;
506 * Get list of valid SRAM names. ROM must be loaded.
508 * Returns: The list of SRAMs.
510 virtual std::set<std::string> c_srams() = 0;
512 * Get lightgun scale (only cores that have lightguns need to define this).
514 virtual std::pair<unsigned, unsigned> c_lightgun_scale();
516 * Set/Clear debug callback flags for address.
518 * Address of 0xFFFFFFFFFFFFFFFF means all addresses.
519 * Flags are 1 for read, 2 for write, 4 for execute, 8 for trace (addr is processor number)
521 virtual void c_set_debug_flags(uint64_t addr, unsigned flags_set, unsigned flags_clear) = 0;
523 * Set/Clear cheat.
525 virtual void c_set_cheat(uint64_t addr, uint64_t value, bool set) = 0;
527 * Get list of trace processor names.
529 virtual std::vector<std::string> c_get_trace_cpus() = 0;
531 * Reset all debug hooks.
533 virtual void c_debug_reset() = 0;
535 * Reset to state equivalent to ROM load.
537 virtual void c_reset_to_load() = 0;
539 * Is null core (only NULL core should define this).
541 virtual bool c_isnull() const;
542 private:
543 std::vector<portctrl::type*> port_types;
544 bool hidden;
545 std::map<std::string, interface_action> actions;
546 threads::lock actions_lock;
549 struct core_type
551 public:
552 core_type(const core_type_params& params);
553 core_type(std::initializer_list<core_type_params> p) : core_type(*p.begin()) {}
554 virtual ~core_type() throw();
555 static std::list<core_type*> get_core_types();
556 core_region& get_preferred_region() const;
557 std::list<core_region*> get_regions() const;
558 core_sysregion& combine_region(core_region& reg) const;
559 const std::string& get_iname() const;
560 const std::string& get_hname() const;
561 std::list<std::string> get_extensions() const;
562 bool is_known_extension(const std::string& ext) const;
563 core_sysregion& lookup_sysregion(const std::string& sysreg) const;
564 std::string get_biosname() const;
565 unsigned get_id() const;
566 unsigned get_image_count() const;
567 core_romimage_info get_image_info(unsigned index) const;
568 bool load(core_romimage* images, std::map<std::string, std::string>& settings, uint64_t rtc_sec,
569 uint64_t rtc_subsec);
570 controller_set controllerconfig(std::map<std::string, std::string>& settings);
571 core_setting_group& get_settings();
572 std::pair<uint64_t, uint64_t> get_bus_map() { return core->get_bus_map(); }
573 std::list<core_vma_info> vma_list() { return core->vma_list(); }
574 std::set<std::string> srams() { return core->srams(); }
575 core_core* get_core() { return core; }
576 bool set_region(core_region& region) { return core->set_region(region); }
577 std::pair<uint32_t, uint32_t> get_video_rate() { return core->get_video_rate(); }
578 std::pair<uint32_t, uint32_t> get_audio_rate() { return core->get_audio_rate(); }
579 std::string get_core_identifier() { return core->get_core_identifier(); }
580 std::string get_core_shortname() const { return core->get_core_shortname(); }
581 std::map<std::string, std::vector<char>> save_sram() throw(std::bad_alloc) { return core->save_sram(); }
582 void load_sram(std::map<std::string, std::vector<char>>& sram) throw(std::bad_alloc)
584 core->load_sram(sram);
586 void serialize(std::vector<char>& out) { core->serialize(out); }
587 void unserialize(const char* in, size_t insize) { core->unserialize(in, insize); }
588 core_region& get_region() { return core->get_region(); }
589 void power() { core->power(); }
590 void unload_cartridge() { core->unload_cartridge(); }
591 std::pair<uint32_t, uint32_t> get_scale_factors(uint32_t width, uint32_t height)
593 return core->get_scale_factors(width, height);
595 void install_handler() { core->install_handler(); }
596 void uninstall_handler() { core->uninstall_handler(); }
597 void emulate() { core->emulate(); }
598 void runtosave() { core->runtosave(); }
599 bool get_pflag() { return core->get_pflag(); }
600 void set_pflag(bool pflag) { core->set_pflag(pflag); }
601 framebuffer::raw& draw_cover() { return core->draw_cover(); }
602 std::string get_systemmenu_name() { return sysname; }
603 void execute_action(unsigned id, const std::vector<interface_action_paramval>& p)
605 return core->execute_action(id, p);
607 unsigned action_flags(unsigned id) { return core->action_flags(id); }
608 bool is_hidden() const { return core->is_hidden(); }
609 double get_PAR() { return core->get_PAR(); }
610 void pre_emulate_frame(portctrl::frame& cf) { return core->pre_emulate_frame(cf); }
611 std::set<const interface_action*> get_actions() { return core->get_actions(); }
612 const interface_device_reg* get_registers() { return core->get_registers(); }
613 int reset_action(bool hard) { return core->reset_action(hard); }
614 std::pair<unsigned, unsigned> lightgun_scale() { return core->lightgun_scale(); }
615 void set_debug_flags(uint64_t addr, unsigned flags_set, unsigned flags_clear)
617 return core->set_debug_flags(addr, flags_set, flags_clear);
619 void set_cheat(uint64_t addr, uint64_t value, bool set)
621 return core->set_cheat(addr, value, set);
623 std::vector<std::string> get_trace_cpus() { return core->get_trace_cpus(); }
624 void debug_reset() { core->debug_reset(); }
625 bool isnull() const { return core->isnull(); }
626 void reset_to_load() { return core->reset_to_load(); }
627 bool safe_to_unload(loadlib::module& mod) const { return core->safe_to_unload(mod); }
628 protected:
630 * Load a ROM slot set. Changes the ROM currently loaded for core.
632 * Parameter images: The set of images to load.
633 * Parameter settings: The settings to use.
634 * Parameter rtc_sec: The initial RTC seconds value.
635 * Parameter rtc_subsec: The initial RTC subseconds value.
636 * Returns: -1 on failure, 0 on success.
638 virtual int t_load_rom(core_romimage* images, std::map<std::string, std::string>& settings, uint64_t rtc_sec,
639 uint64_t rtc_subsec) = 0;
641 * Obtain controller config for given settings.
643 * Parameter settings: The settings to use.
644 * Returns: The controller configuration.
646 virtual controller_set t_controllerconfig(std::map<std::string, std::string>& settings) = 0;
647 private:
648 core_type(const core_type&);
649 core_type& operator=(const core_type&);
650 unsigned id;
651 std::string iname;
652 std::string hname;
653 std::string biosname;
654 std::string sysname;
655 std::list<core_region*> regions;
656 std::vector<core_romimage_info> imageinfo;
657 core_setting_group settings;
658 core_core* core;
662 * System type / region pair.
664 * All run regions for given system must have valid pairs.
666 struct core_sysregion
668 public:
670 * Create a new system type / region pair.
672 * Parameter name: The internal name of the pair.
673 * Parameter type: The system.
674 * Parameter region: The region.
676 core_sysregion(const std::string& name, core_type& type, core_region& region);
677 ~core_sysregion() throw();
678 const std::string& get_name() const;
679 core_region& get_region();
680 core_type& get_type();
681 void fill_framerate_magic(uint64_t* magic); //4 elements filled.
683 * Find all sysregions matching specified name.
685 * Parameter name: The name of system region.
686 * Returns: Sysregions matching the specified.
688 static std::set<core_sysregion*> find_matching(const std::string& name);
689 private:
690 core_sysregion(const core_sysregion&);
691 core_sysregion& operator=(const core_sysregion&);
692 std::string name;
693 core_type& type;
694 core_region& region;
697 //Register a sysregion to name mapping.
698 void register_sysregion_mapping(std::string from, std::string to);
699 std::string lookup_sysregion_mapping(std::string from);
701 //Set to true if new core is detected.
702 extern bool new_core_flag;
704 #endif