smc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. {
  30. uint8_t res = 1;
  31. do
  32. {
  33. size--;
  34. if ( data[size] != value )
  35. {
  36. res = 0;
  37. }
  38. }
  39. while ( size );
  40. return res;
  41. }
  42. uint8_t checkChksum( uint16_t cchk, uint16_t chk )
  43. {
  44. uint32_t sum = cchk + chk;
  45. uint8_t res = 0;
  46. if ( sum == 0x0000ffff )
  47. {
  48. res = 1;
  49. }
  50. return res;
  51. }
  52. void smc_id( snes_romprops_t *props )
  53. {
  54. uint8_t score, maxscore = 1, score_idx = 2; /* assume LoROM */
  55. snes_header_t *header = &( props->header );
  56. props->has_dspx = 0;
  57. props->has_st0010 = 0;
  58. props->has_cx4 = 0;
  59. props->fpga_features = 0;
  60. props->fpga_conf = NULL;
  61. for ( uint8_t num = 0; num < 6; num++ )
  62. {
  63. score = smc_headerscore( hdr_addr[num], header );
  64. printf( "%d: offset = %lX; score = %d\n", num, hdr_addr[num], score ); // */
  65. if ( score >= maxscore )
  66. {
  67. score_idx = num;
  68. maxscore = score;
  69. }
  70. }
  71. if ( score_idx & 1 )
  72. {
  73. props->offset = 0x200;
  74. }
  75. else
  76. {
  77. props->offset = 0;
  78. }
  79. /* restore the chosen one */
  80. /*dprintf("winner is %d\n", score_idx); */
  81. file_readblock( header, hdr_addr[score_idx], sizeof( snes_header_t ) );
  82. if ( header->name[0x13] == 0x00 || header->name[0x13] == 0xff )
  83. {
  84. if ( header->name[0x14] == 0x00 )
  85. {
  86. const uint8_t n15 = header->map;
  87. if ( n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c
  88. || n15 == 0xbc || n15 == 0xfc )
  89. {
  90. if ( header->licensee == 0x33 || header->licensee == 0xff )
  91. {
  92. props->mapper_id = 0;
  93. /*XXX do this properly */
  94. props->ramsize_bytes = 0x8000;
  95. props->romsize_bytes = 0x100000;
  96. props->expramsize_bytes = 0;
  97. props->mapper_id = 3; /* BS-X Memory Map */
  98. props->region = 0; /* BS-X only existed in Japan */
  99. return;
  100. }
  101. }
  102. }
  103. }
  104. switch ( header->map & 0xef )
  105. {
  106. case 0x21: /* HiROM */
  107. props->mapper_id = 0;
  108. if ( header->map == 0x31 && ( header->carttype == 0x03 || header->carttype == 0x05 ) )
  109. {
  110. props->has_dspx = 1;
  111. props->dsp_fw = DSPFW_1B;
  112. props->fpga_features |= FEAT_DSPX;
  113. }
  114. break;
  115. case 0x20: /* LoROM */
  116. props->mapper_id = 1;
  117. if ( header->map == 0x20 && header->carttype == 0xf3 )
  118. {
  119. props->has_cx4 = 1;
  120. props->dsp_fw = CX4FW;
  121. props->fpga_conf = FPGA_CX4;
  122. props->fpga_features |= FEAT_CX4;
  123. }
  124. else if ( ( header->map == 0x20 && header->carttype == 0x03 ) ||
  125. ( header->map == 0x30 && header->carttype == 0x05 && header->licensee != 0xb2 ) )
  126. {
  127. props->has_dspx = 1;
  128. props->fpga_features |= FEAT_DSPX;
  129. /* Pilotwings uses DSP1 instead of DSP1B */
  130. if ( !memcmp( header->name, "PILOTWINGS", 10 ) )
  131. {
  132. props->dsp_fw = DSPFW_1;
  133. }
  134. else
  135. {
  136. props->dsp_fw = DSPFW_1B;
  137. }
  138. }
  139. else if ( header->map == 0x20 && header->carttype == 0x05 )
  140. {
  141. props->has_dspx = 1;
  142. props->dsp_fw = DSPFW_2;
  143. props->fpga_features |= FEAT_DSPX;
  144. }
  145. else if ( header->map == 0x30 && header->carttype == 0x05 && header->licensee == 0xb2 )
  146. {
  147. props->has_dspx = 1;
  148. props->dsp_fw = DSPFW_3;
  149. props->fpga_features |= FEAT_DSPX;
  150. }
  151. else if ( header->map == 0x30 && header->carttype == 0x03 )
  152. {
  153. props->has_dspx = 1;
  154. props->dsp_fw = DSPFW_4;
  155. props->fpga_features |= FEAT_DSPX;
  156. }
  157. else if ( header->map == 0x30 && header->carttype == 0xf6 && header->romsize >= 0xa )
  158. {
  159. props->has_dspx = 1;
  160. props->has_st0010 = 1;
  161. props->dsp_fw = DSPFW_ST0010;
  162. props->fpga_features |= FEAT_ST0010;
  163. header->ramsize = 2;
  164. }
  165. break;
  166. case 0x25: /* ExHiROM */
  167. props->mapper_id = 2;
  168. break;
  169. case 0x22: /* ExLoROM */
  170. if ( file_handle.fsize > 0x400200 )
  171. {
  172. props->mapper_id = 6; /* SO96 */
  173. }
  174. else
  175. {
  176. props->mapper_id = 1;
  177. }
  178. break;
  179. default: /* invalid/unsupported mapper, use header location */
  180. switch ( score_idx )
  181. {
  182. case 0:
  183. case 1:
  184. props->mapper_id = 0;
  185. break;
  186. case 2:
  187. case 3:
  188. if ( file_handle.fsize > 0x800200 )
  189. {
  190. props->mapper_id = 6; /* SO96 interleaved */
  191. }
  192. else if ( file_handle.fsize > 0x400200 )
  193. {
  194. props->mapper_id = 1; /* ExLoROM */
  195. }
  196. else
  197. {
  198. props->mapper_id = 1; /* LoROM */
  199. }
  200. break;
  201. case 4:
  202. case 5:
  203. props->mapper_id = 2;
  204. break;
  205. default:
  206. props->mapper_id = 1; // whatever
  207. }
  208. }
  209. if ( header->romsize == 0 || header->romsize > 13 )
  210. {
  211. props->romsize_bytes = 1024;
  212. header->romsize = 0;
  213. if ( file_handle.fsize >= 1024 )
  214. {
  215. while ( props->romsize_bytes < file_handle.fsize - 1 )
  216. {
  217. header->romsize++;
  218. props->romsize_bytes <<= 1;
  219. }
  220. }
  221. }
  222. props->ramsize_bytes = ( uint32_t )1024 << header->ramsize;
  223. props->romsize_bytes = ( uint32_t )1024 << header->romsize;
  224. props->expramsize_bytes = ( uint32_t )1024 << header->expramsize;
  225. //dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes);
  226. if ( props->ramsize_bytes > 32768 || props->ramsize_bytes < 2048 )
  227. {
  228. props->ramsize_bytes = 0;
  229. }
  230. props->region = ( header->destcode <= 1 || header->destcode >= 13 ) ? 0 : 1;
  231. /*dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes); */
  232. }
  233. uint8_t smc_headerscore( uint32_t addr, snes_header_t *header )
  234. {
  235. int score = 0;
  236. uint8_t reset_inst;
  237. uint16_t header_offset;
  238. if ( ( addr & 0xfff ) == 0x1b0 )
  239. {
  240. header_offset = 0x200;
  241. }
  242. else
  243. {
  244. header_offset = 0;
  245. }
  246. if ( ( file_readblock( header, addr, sizeof( snes_header_t ) ) < sizeof( snes_header_t ) )
  247. || file_res )
  248. {
  249. return 0;
  250. }
  251. uint8_t mapper = header->map & ~0x10;
  252. uint16_t resetvector = header->vect_reset; /* not endian safe! */
  253. uint32_t file_addr = ( ( ( addr - header_offset ) & ~0x7fff ) | ( resetvector & 0x7fff ) ) + header_offset;
  254. if ( resetvector < 0x8000 )
  255. {
  256. return 0;
  257. }
  258. score += 2 * isFixed( &header->licensee, sizeof( header->licensee ), 0x33 );
  259. score += 4 * checkChksum( header->cchk, header->chk );
  260. if ( header->carttype < 0x08 )
  261. {
  262. score++;
  263. }
  264. if ( header->romsize < 0x10 )
  265. {
  266. score++;
  267. }
  268. if ( header->ramsize < 0x08 )
  269. {
  270. score++;
  271. }
  272. if ( header->destcode < 0x0e )
  273. {
  274. score++;
  275. }
  276. if ( ( addr - header_offset ) == 0x007fc0 && mapper == 0x20 )
  277. {
  278. score += 2;
  279. }
  280. if ( ( addr - header_offset ) == 0x00ffc0 && mapper == 0x21 )
  281. {
  282. score += 2;
  283. }
  284. if ( ( addr - header_offset ) == 0x007fc0 && mapper == 0x22 )
  285. {
  286. score += 2;
  287. }
  288. if ( ( addr - header_offset ) == 0x40ffc0 && mapper == 0x25 )
  289. {
  290. score += 2;
  291. }
  292. file_readblock( &reset_inst, file_addr, 1 );
  293. switch ( reset_inst )
  294. {
  295. case 0x78: /* sei */
  296. case 0x18: /* clc */
  297. case 0x38: /* sec */
  298. case 0x9c: /* stz abs */
  299. case 0x4c: /* jmp abs */
  300. case 0x5c: /* jml abs */
  301. score += 8;
  302. break;
  303. case 0xc2: /* rep */
  304. case 0xe2: /* sep */
  305. case 0xad: /* lda abs */
  306. case 0xae: /* ldx abs */
  307. case 0xac: /* ldy abs */
  308. case 0xaf: /* lda abs long */
  309. case 0xa9: /* lda imm */
  310. case 0xa2: /* ldx imm */
  311. case 0xa0: /* ldy imm */
  312. case 0x20: /* jsr abs */
  313. case 0x22: /* jsl abs */
  314. score += 4;
  315. break;
  316. case 0x40: /* rti */
  317. case 0x60: /* rts */
  318. case 0x6b: /* rtl */
  319. case 0xcd: /* cmp abs */
  320. case 0xec: /* cpx abs */
  321. case 0xcc: /* cpy abs */
  322. score -= 4;
  323. break;
  324. case 0x00: /* brk */
  325. case 0x02: /* cop */
  326. case 0xdb: /* stp */
  327. case 0x42: /* wdm */
  328. case 0xff: /* sbc abs long indexed */
  329. score -= 8;
  330. break;
  331. }
  332. if ( score && addr > 0x400000 )
  333. {
  334. score += 4;
  335. }
  336. if ( score < 0 )
  337. {
  338. score = 0;
  339. }
  340. return score;
  341. }