cart.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. class Cartridge : public property {
  2. public:
  3. enum Mode {
  4. ModeNormal,
  5. ModeBsxSlotted,
  6. ModeBsx,
  7. ModeSufamiTurbo,
  8. };
  9. enum Type {
  10. TypeNormal,
  11. TypeBsxSlotted,
  12. TypeBsxBios,
  13. TypeBsx,
  14. TypeSufamiTurboBios,
  15. TypeSufamiTurbo,
  16. TypeUnknown,
  17. };
  18. enum Region {
  19. NTSC,
  20. PAL,
  21. };
  22. enum MemoryMapper {
  23. LoROM,
  24. HiROM,
  25. ExLoROM,
  26. ExHiROM,
  27. SPC7110ROM,
  28. BSCLoROM,
  29. BSCHiROM,
  30. BSXROM,
  31. STROM,
  32. };
  33. enum DSP1MemoryMapper {
  34. DSP1Unmapped,
  35. DSP1LoROM1MB,
  36. DSP1LoROM2MB,
  37. DSP1HiROM,
  38. };
  39. //properties can be read via operator(), eg "if(cartridge.loaded() == true)";
  40. //warning: if loaded() == false, no other property is considered valid!
  41. property_t<bool> loaded; //is a base cartridge inserted?
  42. property_t<bool> bsx_flash_loaded; //is a BS-X flash cart connected?
  43. property_t<bool> patched; //has a UPS patch been applied?
  44. property_t<string> name; //display name (filename sans path and extension)
  45. property_t<Mode> mode;
  46. property_t<Region> region;
  47. property_t<MemoryMapper> mapper;
  48. property_t<DSP1MemoryMapper> dsp1_mapper;
  49. property_t<bool> has_bsx_slot;
  50. property_t<bool> has_superfx;
  51. property_t<bool> has_sa1;
  52. property_t<bool> has_srtc;
  53. property_t<bool> has_sdd1;
  54. property_t<bool> has_spc7110, has_spc7110rtc;
  55. property_t<bool> has_cx4;
  56. property_t<bool> has_dsp1, has_dsp2, has_dsp3, has_dsp4;
  57. property_t<bool> has_obc1;
  58. property_t<bool> has_st010, has_st011, has_st018;
  59. //main interface
  60. bool load_normal (const char *base);
  61. bool load_bsx_slotted (const char *base, const char *slot = "");
  62. bool load_bsx (const char *base, const char *slot = "");
  63. bool load_sufami_turbo(const char *base, const char *slotA = "", const char *slotB = "");
  64. void unload();
  65. //utility functions
  66. static string filepath(const char *filename, const char *pathname); //"./bar.ext" -> "/foo/bar.ext"
  67. static string basename(const char *filename); //"/foo/bar.ext" -> "bar"
  68. static string basepath(const char *filename); //"/foo/bar.ext" -> "/foo/bar/"
  69. //this function will load 'filename', decompress it if needed, and determine what type of
  70. //image file 'filename' refers to (eg normal cart, BS-X flash cart, Sufami Turbo cart, etc.)
  71. //warning: this operation is very expensive, use sparingly!
  72. Type detect_image_type(const char *filename) const;
  73. Cartridge();
  74. ~Cartridge();
  75. private:
  76. void load_begin(Mode);
  77. void load_end();
  78. void unload_normal();
  79. void unload_bsx_slotted();
  80. void unload_bsx();
  81. void unload_sufami_turbo();
  82. struct cartinfo_t {
  83. Type type;
  84. Region region;
  85. MemoryMapper mapper;
  86. DSP1MemoryMapper dsp1_mapper;
  87. unsigned rom_size, ram_size;
  88. bool bsx_slot;
  89. bool superfx;
  90. bool sa1;
  91. bool srtc;
  92. bool sdd1;
  93. bool spc7110, spc7110rtc;
  94. bool cx4;
  95. bool dsp1, dsp2, dsp3, dsp4;
  96. bool obc1;
  97. bool st010, st011, st018;
  98. void reset();
  99. cartinfo_t();
  100. };
  101. enum HeaderField {
  102. CartName = 0x00,
  103. Mapper = 0x15,
  104. RomType = 0x16,
  105. RomSize = 0x17,
  106. RamSize = 0x18,
  107. CartRegion = 0x19,
  108. Company = 0x1a,
  109. Version = 0x1b,
  110. Complement = 0x1c, //inverse checksum
  111. Checksum = 0x1e,
  112. ResetVector = 0x3c,
  113. };
  114. void read_header(cartinfo_t &info, const uint8_t *data, unsigned size) const;
  115. unsigned find_header(const uint8_t *data, unsigned size) const;
  116. unsigned score_header(const uint8_t *data, unsigned size, unsigned addr) const;
  117. void set_cartinfo(const cartinfo_t&);
  118. bool load_image(const char *filename, uint8_t *&data, unsigned &size, bool &patched) const;
  119. bool load_ram (const char *filename, uint8_t *&data, unsigned size, uint8_t init_value) const;
  120. enum CompressionMode {
  121. CompressionNone, //always load without compression
  122. CompressionInspect, //use file header inspection
  123. CompressionAuto, //use file extension or file header inspection (configured by user)
  124. };
  125. bool load_file(const char *fn, uint8 *&data, unsigned &size, CompressionMode compression = CompressionNone) const;
  126. bool save_file(const char *fn, uint8 *data, unsigned size) const;
  127. bool apply_patch(const uint8_t *pdata, unsigned psize, uint8_t *&data, unsigned &size) const;
  128. string modify_extension(const char *filename, const char *extension) const;
  129. string get_filename(const char *source, const char *extension, const char *path) const;
  130. struct {
  131. string filename;
  132. uint8_t *rom, *ram, *rtc;
  133. unsigned rom_size, ram_size, rtc_size;
  134. } cart;
  135. struct {
  136. string filename;
  137. uint8_t *ram;
  138. unsigned ram_size;
  139. } bs;
  140. struct {
  141. string filename;
  142. uint8_t *rom, *ram;
  143. unsigned rom_size, ram_size;
  144. } stA, stB;
  145. };
  146. namespace memory {
  147. extern MappedRAM cartrom, cartram, cartrtc;
  148. extern MappedRAM bscram;
  149. extern MappedRAM stArom, stAram;
  150. extern MappedRAM stBrom, stBram;
  151. };
  152. extern Cartridge cartridge;