smc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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->has_dspx = 0;
  50. props->has_st0010 = 0;
  51. props->has_cx4 = 0;
  52. props->fpga_features = 0;
  53. props->fpga_conf = NULL;
  54. for(uint8_t num = 0; num < 6; num++) {
  55. score = smc_headerscore(hdr_addr[num], header);
  56. printf("%d: offset = %lX; score = %d\n", num, hdr_addr[num], score); // */
  57. if(score>=maxscore) {
  58. score_idx=num;
  59. maxscore=score;
  60. }
  61. }
  62. if(score_idx & 1) {
  63. props->offset = 0x200;
  64. } else {
  65. props->offset = 0;
  66. }
  67. /* restore the chosen one */
  68. /*dprintf("winner is %d\n", score_idx); */
  69. file_readblock(header, hdr_addr[score_idx], sizeof(snes_header_t));
  70. if(header->name[0x13] == 0x00 || header->name[0x13] == 0xff) {
  71. if(header->name[0x14] == 0x00) {
  72. const uint8_t n15 = header->map;
  73. if(n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c
  74. || n15 == 0xbc || n15 == 0xfc) {
  75. if(header->licensee == 0x33 || header->licensee == 0xff) {
  76. props->mapper_id = 0;
  77. /*XXX do this properly */
  78. props->ramsize_bytes = 0x8000;
  79. props->romsize_bytes = 0x100000;
  80. props->expramsize_bytes = 0;
  81. props->mapper_id = 3; /* BS-X Memory Map */
  82. props->region = 0; /* BS-X only existed in Japan */
  83. return;
  84. }
  85. }
  86. }
  87. }
  88. switch(header->map & 0xef) {
  89. case 0x21: /* HiROM */
  90. props->mapper_id = 0;
  91. if(header->map == 0x31 && (header->carttype == 0x03 || header->carttype == 0x05)) {
  92. props->has_dspx = 1;
  93. props->dsp_fw = DSPFW_1B;
  94. props->fpga_features |= FEAT_DSPX;
  95. }
  96. break;
  97. case 0x20: /* LoROM */
  98. props->mapper_id = 1;
  99. if (header->map == 0x20 && header->carttype == 0xf3) {
  100. props->has_cx4 = 1;
  101. props->dsp_fw = CX4FW;
  102. props->fpga_conf = FPGA_CX4;
  103. props->fpga_features |= FEAT_CX4;
  104. }
  105. else if ((header->map == 0x20 && header->carttype == 0x03) ||
  106. (header->map == 0x30 && header->carttype == 0x05 && header->licensee != 0xb2)) {
  107. props->has_dspx = 1;
  108. props->fpga_features |= FEAT_DSPX;
  109. /* Pilotwings uses DSP1 instead of DSP1B */
  110. if(!memcmp(header->name, "PILOTWINGS", 10)) {
  111. props->dsp_fw = DSPFW_1;
  112. } else {
  113. props->dsp_fw = DSPFW_1B;
  114. }
  115. } else if (header->map == 0x20 && header->carttype == 0x05) {
  116. props->has_dspx = 1;
  117. props->dsp_fw = DSPFW_2;
  118. props->fpga_features |= FEAT_DSPX;
  119. } else if (header->map == 0x30 && header->carttype == 0x05 && header->licensee == 0xb2) {
  120. props->has_dspx = 1;
  121. props->dsp_fw = DSPFW_3;
  122. props->fpga_features |= FEAT_DSPX;
  123. } else if (header->map == 0x30 && header->carttype == 0x03) {
  124. props->has_dspx = 1;
  125. props->dsp_fw = DSPFW_4;
  126. props->fpga_features |= FEAT_DSPX;
  127. } else if (header->map == 0x30 && header->carttype == 0xf6 && header->romsize >= 0xa) {
  128. props->has_dspx = 1;
  129. props->has_st0010 = 1;
  130. props->dsp_fw = DSPFW_ST0010;
  131. props->fpga_features |= FEAT_ST0010;
  132. header->ramsize = 2;
  133. }
  134. break;
  135. case 0x25: /* ExHiROM */
  136. props->mapper_id = 2;
  137. break;
  138. case 0x22: /* ExLoROM */
  139. if(file_handle.fsize > 0x400200) {
  140. props->mapper_id = 6; /* SO96 */
  141. } else {
  142. props->mapper_id = 1;
  143. }
  144. break;
  145. default: /* invalid/unsupported mapper, use header location */
  146. switch(score_idx) {
  147. case 0:
  148. case 1:
  149. props->mapper_id = 0;
  150. break;
  151. case 2:
  152. case 3:
  153. if(file_handle.fsize > 0x800200) {
  154. props->mapper_id = 6; /* SO96 interleaved */
  155. } else if(file_handle.fsize > 0x400200) {
  156. props->mapper_id = 1; /* ExLoROM */
  157. } else {
  158. props->mapper_id = 1; /* LoROM */
  159. }
  160. break;
  161. case 4:
  162. case 5:
  163. props->mapper_id = 2;
  164. break;
  165. default:
  166. props->mapper_id = 1; // whatever
  167. }
  168. }
  169. if(header->romsize == 0 || header->romsize > 13) {
  170. props->romsize_bytes = 1024;
  171. header->romsize = 0;
  172. if(file_handle.fsize >= 1024) {
  173. while(props->romsize_bytes < file_handle.fsize-1) {
  174. header->romsize++;
  175. props->romsize_bytes <<= 1;
  176. }
  177. }
  178. }
  179. props->ramsize_bytes = (uint32_t)1024 << header->ramsize;
  180. props->romsize_bytes = (uint32_t)1024 << header->romsize;
  181. props->expramsize_bytes = (uint32_t)1024 << header->expramsize;
  182. //dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes);
  183. if(props->ramsize_bytes > 32768 || props->ramsize_bytes < 2048) {
  184. props->ramsize_bytes = 0;
  185. }
  186. props->region = (header->destcode <= 1 || header->destcode >= 13) ? 0 : 1;
  187. /*dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes); */
  188. }
  189. uint8_t smc_headerscore(uint32_t addr, snes_header_t* header) {
  190. int score=0;
  191. uint8_t reset_inst;
  192. uint16_t header_offset;
  193. if((addr & 0xfff) == 0x1b0) {
  194. header_offset = 0x200;
  195. } else {
  196. header_offset = 0;
  197. }
  198. if((file_readblock(header, addr, sizeof(snes_header_t)) < sizeof(snes_header_t))
  199. || file_res) {
  200. return 0;
  201. }
  202. uint8_t mapper = header->map & ~0x10;
  203. uint16_t resetvector = header->vect_reset; /* not endian safe! */
  204. uint32_t file_addr = (((addr - header_offset) & ~0x7fff) | (resetvector & 0x7fff)) + header_offset;
  205. if(resetvector < 0x8000) return 0;
  206. score += 2*isFixed(&header->licensee, sizeof(header->licensee), 0x33);
  207. score += 4*checkChksum(header->cchk, header->chk);
  208. if(header->carttype < 0x08) score++;
  209. if(header->romsize < 0x10) score++;
  210. if(header->ramsize < 0x08) score++;
  211. if(header->destcode < 0x0e) score++;
  212. if((addr-header_offset) == 0x007fc0 && mapper == 0x20) score += 2;
  213. if((addr-header_offset) == 0x00ffc0 && mapper == 0x21) score += 2;
  214. if((addr-header_offset) == 0x007fc0 && mapper == 0x22) score += 2;
  215. if((addr-header_offset) == 0x40ffc0 && mapper == 0x25) score += 2;
  216. file_readblock(&reset_inst, file_addr, 1);
  217. switch(reset_inst) {
  218. case 0x78: /* sei */
  219. case 0x18: /* clc */
  220. case 0x38: /* sec */
  221. case 0x9c: /* stz abs */
  222. case 0x4c: /* jmp abs */
  223. case 0x5c: /* jml abs */
  224. score += 8;
  225. break;
  226. case 0xc2: /* rep */
  227. case 0xe2: /* sep */
  228. case 0xad: /* lda abs */
  229. case 0xae: /* ldx abs */
  230. case 0xac: /* ldy abs */
  231. case 0xaf: /* lda abs long */
  232. case 0xa9: /* lda imm */
  233. case 0xa2: /* ldx imm */
  234. case 0xa0: /* ldy imm */
  235. case 0x20: /* jsr abs */
  236. case 0x22: /* jsl abs */
  237. score += 4;
  238. break;
  239. case 0x40: /* rti */
  240. case 0x60: /* rts */
  241. case 0x6b: /* rtl */
  242. case 0xcd: /* cmp abs */
  243. case 0xec: /* cpx abs */
  244. case 0xcc: /* cpy abs */
  245. score -= 4;
  246. break;
  247. case 0x00: /* brk */
  248. case 0x02: /* cop */
  249. case 0xdb: /* stp */
  250. case 0x42: /* wdm */
  251. case 0xff: /* sbc abs long indexed */
  252. score -= 8;
  253. break;
  254. }
  255. if(score && addr > 0x400000) score += 4;
  256. if(score < 0) score = 0;
  257. return score;
  258. }