smc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 countAllASCII(uint8_t* data, int size) {
  29. uint8_t res = 0;
  30. do {
  31. size--;
  32. if(data[size] >= 0x20 && data[size] <= 0x7e) {
  33. res++;
  34. }
  35. } while (size);
  36. return res;
  37. }
  38. uint8_t countAllJISX0201(uint8_t* data, int size) {
  39. uint8_t res = 0;
  40. do {
  41. size--;
  42. if((data[size] >= 0x20 && data[size] <= 0x7e)
  43. ||(data[size] >= 0xa1 && data[size] <= 0xdf)) {
  44. res++;
  45. }
  46. } while (size);
  47. return res;
  48. }
  49. uint8_t isFixed(uint8_t* data, int size, uint8_t value) {
  50. uint8_t res = 1;
  51. do {
  52. size--;
  53. if(data[size] != value) {
  54. res = 0;
  55. }
  56. } while (size);
  57. return res;
  58. }
  59. uint8_t checkChksum(uint16_t cchk, uint16_t chk) {
  60. uint32_t sum = cchk + chk;
  61. uint8_t res = 0;
  62. if(sum==0x0000ffff) {
  63. res = 0x10;
  64. }
  65. return res;
  66. }
  67. void smc_id(snes_romprops_t* props) {
  68. uint8_t score, maxscore=1, score_idx=2; /* assume LoROM */
  69. snes_header_t* header = &(props->header);
  70. props->has_dspx = 0;
  71. props->has_st0010 = 0;
  72. props->has_cx4 = 0;
  73. props->fpga_features = 0;
  74. props->fpga_conf = NULL;
  75. for(uint8_t num = 0; num < 6; num++) {
  76. if(!file_readblock(header, hdr_addr[num], sizeof(snes_header_t))
  77. || file_res) {
  78. score = 0;
  79. } else {
  80. score = smc_headerscore(header)/(1+(num&1));
  81. if((file_handle.fsize & 0x2ff) == 0x200) {
  82. if(num&1) {
  83. score+=20;
  84. } else {
  85. score=0;
  86. }
  87. } else {
  88. if(!(num&1)) {
  89. score+=20;
  90. } else {
  91. score=0;
  92. }
  93. }
  94. }
  95. //printf("%d: offset = %lX; score = %d\n", num, hdr_addr[num], score); // */
  96. if(score>=maxscore) {
  97. score_idx=num;
  98. maxscore=score;
  99. }
  100. }
  101. if(score_idx & 1) {
  102. props->offset = 0x200;
  103. } else {
  104. props->offset = 0;
  105. }
  106. /* restore the chosen one */
  107. /*dprintf("winner is %d\n", score_idx); */
  108. file_readblock(header, hdr_addr[score_idx], sizeof(snes_header_t));
  109. if(header->name[0x13] == 0x00 || header->name[0x13] == 0xff) {
  110. if(header->name[0x14] == 0x00) {
  111. const uint8_t n15 = header->map;
  112. if(n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c
  113. || n15 == 0xbc || n15 == 0xfc) {
  114. if(header->licensee == 0x33 || header->licensee == 0xff) {
  115. props->mapper_id = 0;
  116. /*XXX do this properly */
  117. props->ramsize_bytes = 0x8000;
  118. props->romsize_bytes = 0x100000;
  119. props->expramsize_bytes = 0;
  120. props->mapper_id = 3; /* BS-X Memory Map */
  121. return;
  122. }
  123. }
  124. }
  125. }
  126. switch(header->map & 0xef) {
  127. case 0x21: /* HiROM */
  128. props->mapper_id = 0;
  129. if(header->map == 0x31 && (header->carttype == 0x03 || header->carttype == 0x05)) {
  130. props->has_dspx = 1;
  131. props->dsp_fw = DSPFW_1B;
  132. props->fpga_features |= FEAT_DSPX;
  133. }
  134. break;
  135. case 0x20: /* LoROM */
  136. props->mapper_id = 1;
  137. if (header->map == 0x20 && header->carttype == 0xf3) {
  138. props->has_cx4 = 1;
  139. props->dsp_fw = CX4FW;
  140. props->fpga_conf = FPGA_CX4;
  141. props->fpga_features |= FEAT_CX4;
  142. }
  143. else if ((header->map == 0x20 && header->carttype == 0x03) ||
  144. (header->map == 0x30 && header->carttype == 0x05 && header->licensee != 0xb2)) {
  145. props->has_dspx = 1;
  146. props->fpga_features |= FEAT_DSPX;
  147. // Pilotwings uses DSP1 instead of DSP1B
  148. if(!memcmp(header->name, "PILOTWINGS", 10)) {
  149. props->dsp_fw = DSPFW_1;
  150. } else {
  151. props->dsp_fw = DSPFW_1B;
  152. }
  153. } else if (header->map == 0x20 && header->carttype == 0x05) {
  154. props->has_dspx = 1;
  155. props->dsp_fw = DSPFW_2;
  156. props->fpga_features |= FEAT_DSPX;
  157. } else if (header->map == 0x30 && header->carttype == 0x05 && header->licensee == 0xb2) {
  158. props->has_dspx = 1;
  159. props->dsp_fw = DSPFW_3;
  160. props->fpga_features |= FEAT_DSPX;
  161. } else if (header->map == 0x30 && header->carttype == 0x03) {
  162. props->has_dspx = 1;
  163. props->dsp_fw = DSPFW_4;
  164. props->fpga_features |= FEAT_DSPX;
  165. } else if (header->map == 0x30 && header->carttype == 0xf6 && header->romsize >= 0xa) {
  166. props->has_dspx = 1;
  167. props->has_st0010 = 1;
  168. props->dsp_fw = DSPFW_ST0010;
  169. props->fpga_features |= FEAT_ST0010;
  170. header->ramsize = 2;
  171. }
  172. break;
  173. case 0x25: /* ExHiROM */
  174. props->mapper_id = 2;
  175. break;
  176. case 0x22: /* ExLoROM */
  177. if(file_handle.fsize > 0x400200) {
  178. props->mapper_id = 6; /* SO96 */
  179. } else {
  180. props->mapper_id = 1;
  181. }
  182. break;
  183. default: /* invalid/unsupported mapper, use header location */
  184. switch(score_idx) {
  185. case 0:
  186. case 1:
  187. props->mapper_id = 0;
  188. break;
  189. case 2:
  190. case 3:
  191. if(file_handle.fsize > 0x800200) {
  192. props->mapper_id = 6; /* SO96 interleaved */
  193. } else if(file_handle.fsize > 0x400200) {
  194. props->mapper_id = 1; /* ExLoROM */
  195. } else {
  196. props->mapper_id = 1; /* LoROM */
  197. }
  198. break;
  199. case 4:
  200. case 5:
  201. props->mapper_id = 2;
  202. break;
  203. default:
  204. props->mapper_id = 1; // whatever
  205. }
  206. }
  207. if(header->romsize == 0 || header->romsize > 13) {
  208. header->romsize = 13;
  209. }
  210. props->ramsize_bytes = (uint32_t)1024 << header->ramsize;
  211. props->romsize_bytes = (uint32_t)1024 << header->romsize;
  212. props->expramsize_bytes = (uint32_t)1024 << header->expramsize;
  213. /*dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes); */
  214. if(props->ramsize_bytes > 32768 || props->ramsize_bytes < 2048) {
  215. props->ramsize_bytes = 0;
  216. }
  217. /*dprintf("ramsize_bytes: %ld\n", props->ramsize_bytes); */
  218. }
  219. uint8_t smc_headerscore(snes_header_t* header) {
  220. uint8_t score=0;
  221. score += countAllASCII(header->maker, sizeof(header->maker));
  222. score += countAllASCII(header->gamecode, sizeof(header->gamecode));
  223. score += isFixed(header->fixed_00, sizeof(header->fixed_00), 0x00);
  224. score += countAllJISX0201(header->name, sizeof(header->name));
  225. score += 3*isFixed(&header->licensee, sizeof(header->licensee), 0x33);
  226. score += checkChksum(header->cchk, header->chk);
  227. return score;
  228. }