An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

257 lines
6.9 KiB

  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include "rom.h"
  11. #include "logging.h"
  12. #include "util.h"
  13. #define NUM_LOCATIONS 3
  14. #define MAGIC_LEN 8
  15. #define HEADER_SIZE 16
  16. static size_t header_locations[NUM_LOCATIONS] = {0x7FF0, 0x1FF0, 0x3FF0};
  17. static const char header_magic[MAGIC_LEN + 1] = "TMR SEGA";
  18. /*
  19. Return whether or not the given ROM image size is valid.
  20. */
  21. static bool validate_size(off_t size)
  22. {
  23. if (size & (size - 1))
  24. return false; // Ensure size is a power of two
  25. off_t kbytes = size >> 10;
  26. return kbytes >= 8 && kbytes <= 1024;
  27. }
  28. #ifdef DEBUG_MODE
  29. /*
  30. DEBUG FUNCTION: Print out the header to stdout.
  31. */
  32. static void print_header(const uint8_t *header)
  33. {
  34. char header_hex[3 * HEADER_SIZE], header_chr[3 * HEADER_SIZE];
  35. for (int i = 0; i < HEADER_SIZE; i++) {
  36. snprintf(&header_hex[3 * i], 3, "%02X", header[i]);
  37. if (isprint(header[i]))
  38. snprintf(&header_chr[3 * i], 3, "%2c", header[i]);
  39. else {
  40. header_chr[3 * i] = ' ';
  41. header_chr[3 * i + 1] = '?';
  42. }
  43. header_hex[3 * i + 2] = header_chr[3 * i + 2] = ' ';
  44. }
  45. header_hex[3 * HEADER_SIZE - 1] = header_chr[3 * HEADER_SIZE - 1] = '\0';
  46. DEBUG("- header dump (hex): %s", header_hex)
  47. DEBUG("- header dump (chr): %s", header_chr)
  48. }
  49. #endif
  50. /*
  51. Return a string indicating the ROM's size, according to its header.
  52. */
  53. static const char* parse_reported_size(uint8_t value)
  54. {
  55. switch (value) {
  56. case 0x0: return "256 KB";
  57. case 0x1: return "512 KB";
  58. case 0x2: return "1 MB";
  59. case 0xA: return "8 KB";
  60. case 0xB: return "16 KB";
  61. case 0xC: return "32 KB";
  62. case 0xD: return "48 KB";
  63. case 0xE: return "64 KB";
  64. case 0xF: return "128 KB";
  65. default: return "Unknown";
  66. }
  67. }
  68. /*
  69. Parse a ROM image's header, and return whether or not it is valid.
  70. The header is 16 bytes long, consisting of:
  71. byte 0: magic ('T')
  72. byte 1: magic ('M')
  73. byte 2: magic ('R')
  74. byte 3: magic (' ')
  75. byte 4: magic ('S')
  76. byte 5: magic ('E')
  77. byte 6: magic ('G')
  78. byte 7: magic ('A')
  79. byte 8: unused
  80. byte 9: unused
  81. byte A: checksum (LSB)
  82. byte B: checksum (MSB)
  83. byte C: product code (LSB)
  84. byte D: product code (middle byte)
  85. byte E (hi nibble): product code (most-significant nibble)
  86. byte E (lo nibble): version
  87. byte F (hi nibble): region code
  88. byte F (lo nibble): ROM size
  89. (Based on: http://www.smspower.org/Development/ROMHeader)
  90. */
  91. static bool parse_header(ROM *rom, const uint8_t *header)
  92. {
  93. #ifdef DEBUG_MODE
  94. print_header(header);
  95. #endif
  96. rom->checksum = header[0xA] + (header[0xB] << 8);
  97. rom->product_code = bcd_decode(header[0xC]) +
  98. (bcd_decode(header[0xD]) * 100) + ((header[0xE] >> 4) * 10000);
  99. rom->version = header[0xE] & 0x0F;
  100. rom->region_code = header[0xF] >> 4;
  101. const char* region = rom_region(rom);
  102. DEBUG("- header info:")
  103. DEBUG(" - checksum: 0x%04X", rom->checksum)
  104. DEBUG(" - product code: %u", rom->product_code)
  105. DEBUG(" - version: %u", rom->version)
  106. DEBUG(" - region code: %u (%s)", rom->region_code, region ? region : "unknown")
  107. DEBUG(" - reported size: %s", parse_reported_size(header[0xF] & 0xF))
  108. return true;
  109. }
  110. /*
  111. Find and read a ROM image's header, and return whether or not it is valid.
  112. */
  113. static bool find_and_read_header(ROM *rom)
  114. {
  115. size_t location, i;
  116. const uint8_t *header;
  117. DEBUG("- looking for header:")
  118. for (i = 0; i < NUM_LOCATIONS; i++) {
  119. location = header_locations[i];
  120. if (location + HEADER_SIZE > rom->size) {
  121. DEBUG(" - skipping location 0x%zX, out of range", location)
  122. continue;
  123. }
  124. DEBUG(" - trying location 0x%zX:", location)
  125. header = &rom->data[location];
  126. if (memcmp(header, header_magic, MAGIC_LEN)) {
  127. DEBUG(" - magic not present")
  128. }
  129. else {
  130. DEBUG(" - magic found")
  131. return parse_header(rom, header);
  132. }
  133. }
  134. DEBUG(" - could not find header")
  135. return false;
  136. }
  137. /*
  138. Create and load a ROM image located at the given path.
  139. rom_ptr will point to the new object if created successfully, and NULL will
  140. be returned. Otherwise, rom_ptr will not be modified and an error string
  141. will be returned. The error string should not be freed.
  142. */
  143. const char* rom_open(ROM **rom_ptr, const char *path)
  144. {
  145. ROM *rom;
  146. FILE* fp;
  147. struct stat st;
  148. if (!(fp = fopen(path, "r")))
  149. return strerror(errno);
  150. if (fstat(fileno(fp), &st)) {
  151. fclose(fp);
  152. return strerror(errno);
  153. }
  154. if (!(st.st_mode & S_IFREG)) {
  155. fclose(fp);
  156. return (st.st_mode & S_IFDIR) ? rom_err_isdir : rom_err_notfile;
  157. }
  158. if (!(rom = malloc(sizeof(ROM))))
  159. OUT_OF_MEMORY()
  160. // Set defaults:
  161. rom->name = NULL;
  162. rom->data = NULL;
  163. rom->size = 0;
  164. rom->checksum = 0;
  165. rom->product_code = 0;
  166. rom->version = 0;
  167. rom->region_code = 0;
  168. // Set rom->name:
  169. if (!(rom->name = malloc(sizeof(char) * (strlen(path) + 1))))
  170. OUT_OF_MEMORY()
  171. strcpy(rom->name, path);
  172. DEBUG("Loading ROM %s:", rom->name)
  173. // Set rom->size:
  174. DEBUG("- size: %lld", st.st_size)
  175. if (!validate_size(st.st_size)) {
  176. rom_close(rom);
  177. fclose(fp);
  178. return rom_err_badsize;
  179. }
  180. rom->size = st.st_size;
  181. // Set rom->data:
  182. if (!(rom->data = malloc(sizeof(uint8_t) * st.st_size)))
  183. OUT_OF_MEMORY()
  184. if (!(fread(rom->data, st.st_size, 1, fp))) {
  185. rom_close(rom);
  186. fclose(fp);
  187. return rom_err_badread;
  188. }
  189. fclose(fp);
  190. // Parse the header:
  191. if (!find_and_read_header(rom)) {
  192. rom_close(rom);
  193. return rom_err_badheader;
  194. }
  195. *rom_ptr = rom;
  196. return NULL;
  197. }
  198. /*
  199. Free a ROM object previously created with rom_open().
  200. */
  201. void rom_close(ROM *rom)
  202. {
  203. if (rom->name)
  204. free(rom->name);
  205. if (rom->data)
  206. free(rom->data);
  207. free(rom);
  208. }
  209. /*
  210. Return the region this ROM was intended for, based on header information.
  211. NULL is returned if the region code is invalid.
  212. Region code information is taken from:
  213. http://www.smspower.org/Development/ROMHeader
  214. */
  215. const char* rom_region(const ROM *rom)
  216. {
  217. switch (rom->region_code) {
  218. case 3: return "SMS Japan";
  219. case 4: return "SMS Export";
  220. case 5: return "GG Japan";
  221. case 6: return "GG Export";
  222. case 7: return "GG International";
  223. default: return NULL;
  224. }
  225. }