smc.c 8.3 KB

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