smc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. smc.c: SMC file related operations
  19. */
  20. #include "fileops.h"
  21. #include "config.h"
  22. #include "uart.h"
  23. #include "smc.h"
  24. #include "string.h"
  25. #include "fpga_spi.h"
  26. snes_romprops_t romprops;
  27. uint32_t hdr_addr[6] = {0xffb0, 0x101b0, 0x7fb0, 0x81b0, 0x40ffb0, 0x4101b0};
  28. uint8_t isFixed(uint8_t* data, int size, uint8_t value) {
  29. uint8_t res = 1;
  30. do {
  31. size--;
  32. if(data[size] != value) {
  33. res = 0;
  34. }
  35. } while (size);
  36. return res;
  37. }
  38. uint8_t checkChksum(uint16_t cchk, uint16_t chk) {
  39. uint32_t sum = cchk + chk;
  40. uint8_t res = 0;
  41. if(sum==0x0000ffff) {
  42. res = 1;
  43. }
  44. return res;
  45. }
  46. void smc_id(snes_romprops_t* props) {
  47. uint8_t score, maxscore=1, score_idx=2; /* assume LoROM */
  48. snes_header_t* header = &(props->header);
  49. props->load_address = 0;
  50. props->has_dspx = 0;
  51. props->has_st0010 = 0;
  52. props->has_cx4 = 0;
  53. props->fpga_features = 0;
  54. props->fpga_conf = NULL;
  55. for(uint8_t num = 0; num < 6; num++) {
  56. score = smc_headerscore(hdr_addr[num], header);
  57. printf("%d: offset = %lX; score = %d\n", num, hdr_addr[num], score); // */
  58. if(score>=maxscore) {
  59. score_idx=num;
  60. maxscore=score;
  61. }
  62. }
  63. if(score_idx & 1) {
  64. props->offset = 0x200;
  65. } else {
  66. props->offset = 0;
  67. }
  68. /* restore the chosen one */
  69. /*dprintf("winner is %d\n", score_idx); */
  70. file_readblock(header, hdr_addr[score_idx], sizeof(snes_header_t));
  71. if(header->name[0x13] == 0x00 || header->name[0x13] == 0xff) {
  72. if(header->name[0x14] == 0x00) {
  73. const uint8_t n15 = header->map;
  74. if(n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c
  75. || n15 == 0xbc || n15 == 0xfc) {
  76. if(header->licensee == 0x33 || header->licensee == 0xff) {
  77. props->mapper_id = 0;
  78. /*XXX do this properly */
  79. props->ramsize_bytes = 0x8000;
  80. props->romsize_bytes = 0x100000;
  81. props->expramsize_bytes = 0;
  82. props->mapper_id = 3; /* BS-X Memory Map */
  83. props->region = 0; /* BS-X only existed in Japan */
  84. uint8_t alloc = header->name[0x10];
  85. if(alloc) {
  86. while(!(alloc & 0x01)) {
  87. props->load_address += 0x20000;
  88. alloc >>= 1;
  89. }
  90. }
  91. printf("load address: %lx\n", props->load_address);
  92. return;
  93. }
  94. }
  95. }
  96. }
  97. switch(header->map & 0xef) {
  98. case 0x21: /* HiROM */
  99. props->mapper_id = 0;
  100. if(header->map == 0x31 && (header->carttype == 0x03 || header->carttype == 0x05)) {
  101. props->has_dspx = 1;
  102. props->dsp_fw = DSPFW_1B;
  103. props->fpga_features |= FEAT_DSPX;
  104. }
  105. break;
  106. case 0x20: /* LoROM */
  107. props->mapper_id = 1;
  108. if (header->map == 0x20 && header->carttype == 0xf3) {
  109. props->has_cx4 = 1;
  110. props->dsp_fw = CX4FW;
  111. props->fpga_conf = FPGA_CX4;
  112. props->fpga_features |= FEAT_CX4;
  113. }
  114. else if ((header->map == 0x20 && header->carttype == 0x03) ||
  115. (header->map == 0x30 && header->carttype == 0x05 && header->licensee != 0xb2)) {
  116. props->has_dspx = 1;
  117. props->fpga_features |= FEAT_DSPX;
  118. /* Pilotwings uses DSP1 instead of DSP1B */
  119. if(!memcmp(header->name, "PILOTWINGS", 10)) {
  120. props->dsp_fw = DSPFW_1;
  121. } else {
  122. props->dsp_fw = DSPFW_1B;
  123. }
  124. } else if (header->map == 0x20 && header->carttype == 0x05) {
  125. props->has_dspx = 1;
  126. props->dsp_fw = DSPFW_2;
  127. props->fpga_features |= FEAT_DSPX;
  128. } else if (header->map == 0x30 && header->carttype == 0x05 && header->licensee == 0xb2) {
  129. props->has_dspx = 1;
  130. props->dsp_fw = DSPFW_3;
  131. props->fpga_features |= FEAT_DSPX;
  132. } else if (header->map == 0x30 && header->carttype == 0x03) {
  133. props->has_dspx = 1;
  134. props->dsp_fw = DSPFW_4;
  135. props->fpga_features |= FEAT_DSPX;
  136. } else if (header->map == 0x30 && header->carttype == 0xf6 && header->romsize >= 0xa) {
  137. props->has_dspx = 1;
  138. props->has_st0010 = 1;
  139. props->dsp_fw = DSPFW_ST0010;
  140. props->fpga_features |= FEAT_ST0010;
  141. header->ramsize = 2;
  142. }
  143. break;
  144. case 0x25: /* ExHiROM */
  145. props->mapper_id = 2;
  146. break;
  147. case 0x22: /* ExLoROM */
  148. if(file_handle.fsize > 0x400200) {
  149. props->mapper_id = 6; /* SO96 */
  150. } else {
  151. props->mapper_id = 1;
  152. }
  153. break;
  154. default: /* invalid/unsupported mapper, use header location */
  155. switch(score_idx) {
  156. case 0:
  157. case 1:
  158. props->mapper_id = 0;
  159. break;
  160. case 2:
  161. case 3:
  162. if(file_handle.fsize > 0x800200) {
  163. props->mapper_id = 6; /* SO96 interleaved */
  164. } else if(file_handle.fsize > 0x400200) {
  165. props->mapper_id = 1; /* ExLoROM */
  166. } else {
  167. props->mapper_id = 1; /* LoROM */
  168. }
  169. break;
  170. case 4:
  171. case 5:
  172. props->mapper_id = 2;
  173. break;
  174. default:
  175. props->mapper_id = 1; // whatever
  176. }
  177. }
  178. if(header->romsize == 0 || header->romsize > 13) {
  179. props->romsize_bytes = 1024;
  180. header->romsize = 0;
  181. if(file_handle.fsize >= 1024) {
  182. while(props->romsize_bytes < file_handle.fsize-1) {
  183. header->romsize++;
  184. props->romsize_bytes <<= 1;
  185. }
  186. }
  187. }
  188. props->ramsize_bytes = (uint32_t)1024 << header->ramsize;
  189. props->romsize_bytes = (uint32_t)1024 << header->romsize;
  190. props->expramsize_bytes = (uint32_t)1024 << header->expramsize;
  191. /*dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes); */
  192. if(props->ramsize_bytes < 2048) {
  193. props->ramsize_bytes = 0;
  194. }
  195. props->region = (header->destcode <= 1 || header->destcode >= 13) ? 0 : 1;
  196. /*dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes); */
  197. }
  198. uint8_t smc_headerscore(uint32_t addr, snes_header_t* header) {
  199. int score=0;
  200. uint8_t reset_inst;
  201. uint16_t header_offset;
  202. if((addr & 0xfff) == 0x1b0) {
  203. header_offset = 0x200;
  204. } else {
  205. header_offset = 0;
  206. }
  207. if((file_readblock(header, addr, sizeof(snes_header_t)) < sizeof(snes_header_t))
  208. || file_res) {
  209. return 0;
  210. }
  211. uint8_t mapper = header->map & ~0x10;
  212. uint16_t resetvector = header->vect_reset; /* not endian safe! */
  213. uint32_t file_addr = (((addr - header_offset) & ~0x7fff) | (resetvector & 0x7fff)) + header_offset;
  214. if(resetvector < 0x8000) return 0;
  215. score += 2*isFixed(&header->licensee, sizeof(header->licensee), 0x33);
  216. score += 4*checkChksum(header->cchk, header->chk);
  217. if(header->carttype < 0x08) score++;
  218. if(header->romsize < 0x10) score++;
  219. if(header->ramsize < 0x08) score++;
  220. if(header->destcode < 0x0e) score++;
  221. if((addr-header_offset) == 0x007fc0 && mapper == 0x20) score += 2;
  222. if((addr-header_offset) == 0x00ffc0 && mapper == 0x21) score += 2;
  223. if((addr-header_offset) == 0x007fc0 && mapper == 0x22) score += 2;
  224. if((addr-header_offset) == 0x40ffc0 && mapper == 0x25) score += 2;
  225. file_readblock(&reset_inst, file_addr, 1);
  226. switch(reset_inst) {
  227. case 0x78: /* sei */
  228. case 0x18: /* clc */
  229. case 0x38: /* sec */
  230. case 0x9c: /* stz abs */
  231. case 0x4c: /* jmp abs */
  232. case 0x5c: /* jml abs */
  233. score += 8;
  234. break;
  235. case 0xc2: /* rep */
  236. case 0xe2: /* sep */
  237. case 0xad: /* lda abs */
  238. case 0xae: /* ldx abs */
  239. case 0xac: /* ldy abs */
  240. case 0xaf: /* lda abs long */
  241. case 0xa9: /* lda imm */
  242. case 0xa2: /* ldx imm */
  243. case 0xa0: /* ldy imm */
  244. case 0x20: /* jsr abs */
  245. case 0x22: /* jsl abs */
  246. score += 4;
  247. break;
  248. case 0x40: /* rti */
  249. case 0x60: /* rts */
  250. case 0x6b: /* rtl */
  251. case 0xcd: /* cmp abs */
  252. case 0xec: /* cpx abs */
  253. case 0xcc: /* cpy abs */
  254. score -= 4;
  255. break;
  256. case 0x00: /* brk */
  257. case 0x02: /* cop */
  258. case 0xdb: /* stp */
  259. case 0x42: /* wdm */
  260. case 0xff: /* sbc abs long indexed */
  261. score -= 8;
  262. break;
  263. }
  264. if(score && addr > 0x400000) score += 4;
  265. if(score < 0) score = 0;
  266. return score;
  267. }