An emulator, assembler, and disassembler for the Sega Game Gear
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

292 lines
8.3 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 <stdio.h>
  6. #include <stdbool.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 SIZE_CODE_BUF 8
  16. static size_t header_locations[NUM_LOCATIONS] = {0x7FF0, 0x3FF0, 0x1FF0};
  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. Compute the correct ROM data checksum.
  52. If the summable region (as specified by the range parameter) is too large,
  53. we'll compute the checksum over the default range (0x0000-0x7FF0), or the
  54. largest possible range.
  55. */
  56. static uint16_t compute_checksum(const uint8_t *data, size_t size, uint8_t range)
  57. {
  58. size_t low_region, high_region;
  59. switch (range & 0xF) {
  60. case 0xA: low_region = 0x1FEF; high_region = 0; break;
  61. case 0xB: low_region = 0x3FEF; high_region = 0; break;
  62. case 0xC: low_region = 0x7FEF; high_region = 0; break;
  63. case 0xD: low_region = 0xBFEF; high_region = 0; break;
  64. case 0xE: low_region = 0x7FEF; high_region = 0x0FFFF; break;
  65. case 0xF: low_region = 0x7FEF; high_region = 0x1FFFF; break;
  66. case 0x0: low_region = 0x7FEF; high_region = 0x3FFFF; break;
  67. case 0x1: low_region = 0x7FEF; high_region = 0x7FFFF; break;
  68. case 0x2: low_region = 0x7FEF; high_region = 0xFFFFF; break;
  69. default: low_region = 0x7FEF; high_region = 0; break;
  70. }
  71. if (low_region >= size)
  72. low_region = (size >= 0x4000) ? 0x3FEF : 0x1FEF;
  73. if (high_region >= size)
  74. high_region = 0;
  75. uint16_t sum = 0;
  76. for (size_t index = 0; index <= low_region; index++)
  77. sum += data[index];
  78. if (high_region) {
  79. for (size_t index = 0x08000; index <= high_region; index++)
  80. sum += data[index];
  81. }
  82. return sum;
  83. }
  84. #ifdef DEBUG_MODE
  85. /*
  86. DEBUG FUNCTION: Return the ROM's size as a string, according to its header.
  87. */
  88. static const char* parse_reported_size(uint8_t value)
  89. {
  90. static char buffer[SIZE_CODE_BUF];
  91. size_t size = size_code_to_bytes(value);
  92. if (!size)
  93. strncpy(buffer, "Unknown", SIZE_CODE_BUF);
  94. else if (size >= (1 << 20))
  95. snprintf(buffer, SIZE_CODE_BUF, "%zu MB", size >> 20);
  96. else
  97. snprintf(buffer, SIZE_CODE_BUF, "%zu KB", size >> 10);
  98. return buffer;
  99. }
  100. #endif
  101. /*
  102. Parse a ROM image's header, and return whether or not it is valid.
  103. The header is 16 bytes long, consisting of:
  104. byte 0: magic ('T')
  105. byte 1: magic ('M')
  106. byte 2: magic ('R')
  107. byte 3: magic (' ')
  108. byte 4: magic ('S')
  109. byte 5: magic ('E')
  110. byte 6: magic ('G')
  111. byte 7: magic ('A')
  112. byte 8: unused
  113. byte 9: unused
  114. byte A: checksum (LSB)
  115. byte B: checksum (MSB)
  116. byte C: product code (LSB)
  117. byte D: product code (middle byte)
  118. byte E (hi nibble): product code (most-significant nibble)
  119. byte E (lo nibble): version
  120. byte F (hi nibble): region code
  121. byte F (lo nibble): ROM size
  122. (Based on: http://www.smspower.org/Development/ROMHeader)
  123. */
  124. static bool parse_header(ROM *rom, const uint8_t *header)
  125. {
  126. #ifdef DEBUG_MODE
  127. print_header(header);
  128. #endif
  129. rom->reported_checksum = header[0xA] + (header[0xB] << 8);
  130. rom->expected_checksum = compute_checksum(rom->data, rom->size, header[0xF]);
  131. rom->product_code = bcd_decode(header[0xC]) +
  132. (bcd_decode(header[0xD]) * 100) + ((header[0xE] >> 4) * 10000);
  133. rom->version = header[0xE] & 0x0F;
  134. rom->region_code = header[0xF] >> 4;
  135. DEBUG("- header info:")
  136. if (rom->reported_checksum == rom->expected_checksum)
  137. DEBUG(" - checksum: 0x%04X (valid)", rom->reported_checksum)
  138. else
  139. DEBUG(" - checksum: 0x%04X (invalid, expected 0x%04X)",
  140. rom->reported_checksum, rom->expected_checksum)
  141. DEBUG(" - product code: %u", rom->product_code)
  142. DEBUG(" - version: %u", rom->version)
  143. DEBUG(" - region code: %u (%s)", rom->region_code,
  144. rom_region(rom) ? rom_region(rom) : "unknown")
  145. DEBUG(" - reported size: %s", parse_reported_size(header[0xF] & 0xF))
  146. return true;
  147. }
  148. /*
  149. Find and read a ROM image's header, and return whether or not it is valid.
  150. */
  151. static bool find_and_read_header(ROM *rom)
  152. {
  153. size_t location, i;
  154. const uint8_t *header;
  155. DEBUG("- looking for header:")
  156. for (i = 0; i < NUM_LOCATIONS; i++) {
  157. location = header_locations[i];
  158. if (location + HEADER_SIZE > rom->size) {
  159. DEBUG(" - skipping location 0x%zX, out of range", location)
  160. continue;
  161. }
  162. DEBUG(" - trying location 0x%zX:", location)
  163. header = &rom->data[location];
  164. if (memcmp(header, header_magic, MAGIC_LEN)) {
  165. DEBUG(" - magic not present")
  166. }
  167. else {
  168. DEBUG(" - magic found")
  169. return parse_header(rom, header);
  170. }
  171. }
  172. DEBUG(" - could not find header")
  173. return false;
  174. }
  175. /*
  176. Create and load a ROM image located at the given path.
  177. rom_ptr will point to the new object if created successfully, and NULL will
  178. be returned. Otherwise, rom_ptr will not be modified and an error string
  179. will be returned. The error string should not be freed.
  180. */
  181. const char* rom_open(ROM **rom_ptr, const char *path)
  182. {
  183. ROM *rom;
  184. FILE *fp;
  185. struct stat st;
  186. if (!(fp = fopen(path, "rb")))
  187. return strerror(errno);
  188. if (fstat(fileno(fp), &st)) {
  189. fclose(fp);
  190. return strerror(errno);
  191. }
  192. if (!(st.st_mode & S_IFREG)) {
  193. fclose(fp);
  194. return (st.st_mode & S_IFDIR) ? rom_err_isdir : rom_err_notfile;
  195. }
  196. if (!(rom = malloc(sizeof(ROM))))
  197. OUT_OF_MEMORY()
  198. // Set defaults:
  199. rom->name = NULL;
  200. rom->data = NULL;
  201. rom->size = 0;
  202. rom->reported_checksum = 0;
  203. rom->expected_checksum = 0;
  204. rom->product_code = 0;
  205. rom->version = 0;
  206. rom->region_code = 0;
  207. // Set rom->name:
  208. if (!(rom->name = malloc(sizeof(char) * (strlen(path) + 1))))
  209. OUT_OF_MEMORY()
  210. strcpy(rom->name, path);
  211. DEBUG("Loading ROM %s:", rom->name)
  212. // Set rom->size:
  213. DEBUG("- size: %lld", st.st_size)
  214. if (!validate_size(st.st_size)) {
  215. rom_close(rom);
  216. fclose(fp);
  217. return rom_err_badsize;
  218. }
  219. rom->size = st.st_size;
  220. // Set rom->data:
  221. if (!(rom->data = malloc(sizeof(uint8_t) * st.st_size)))
  222. OUT_OF_MEMORY()
  223. if (!(fread(rom->data, st.st_size, 1, fp))) {
  224. rom_close(rom);
  225. fclose(fp);
  226. return rom_err_badread;
  227. }
  228. fclose(fp);
  229. // Parse the header:
  230. if (!find_and_read_header(rom)) {
  231. rom_close(rom);
  232. return rom_err_badheader;
  233. }
  234. *rom_ptr = rom;
  235. return NULL;
  236. }
  237. /*
  238. Free a ROM object previously created with rom_open().
  239. */
  240. void rom_close(ROM *rom)
  241. {
  242. if (rom->name)
  243. free(rom->name);
  244. if (rom->data)
  245. free(rom->data);
  246. free(rom);
  247. }
  248. /*
  249. Return the region this ROM was intended for, based on header information.
  250. NULL is returned if the region code is invalid.
  251. */
  252. const char* rom_region(const ROM *rom)
  253. {
  254. return region_code_to_string(rom->region_code);
  255. }