smc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. smc.c - Super Magic Card support for uCON64
  3. Copyright (c) 2003 dbjh
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <string.h>
  23. #include "misc/misc.h"
  24. #include "misc/itypes.h"
  25. #ifdef USE_ZLIB
  26. #include "misc/archive.h"
  27. #endif
  28. #include "misc/getopt2.h" // st_getopt2_t
  29. #include "misc/file.h"
  30. #include "ucon64.h"
  31. #include "ucon64_misc.h"
  32. #include "ffe.h"
  33. #include "smc.h"
  34. const st_getopt2_t smc_usage[] =
  35. {
  36. {
  37. NULL, 0, 0, 0,
  38. NULL, "Super Magic Card"/*"1993/1994/1995/19XX Front Far East/FFE http://www.front.com.tw"*/,
  39. NULL
  40. },
  41. #ifdef USE_PARALLEL
  42. {
  43. "xsmc", 0, 0, UCON64_XSMC, // send only
  44. NULL, "send ROM (in FFE format) to Super Magic Card; " OPTION_LONG_S "port=PORT",
  45. &ucon64_wf[WF_OBJ_NES_DEFAULT_STOP_NO_SPLIT]
  46. },
  47. {
  48. "xsmcr", 0, 0, UCON64_XSMCR,
  49. NULL, "send/receive RTS data to/from Super Magic Card; " OPTION_LONG_S "port=PORT\n"
  50. "receives automatically when RTS file does not exist",
  51. &ucon64_wf[WF_OBJ_NES_STOP_NO_ROM]
  52. },
  53. #endif
  54. {NULL, 0, 0, 0, NULL, NULL, NULL}
  55. };
  56. #ifdef USE_PARALLEL
  57. #define BUFFERSIZE 8192
  58. static int get_blocks1 (unsigned char *header);
  59. static int get_blocks2 (unsigned char *header);
  60. static int get_blocks3 (unsigned char *header);
  61. int
  62. get_blocks1 (unsigned char *header)
  63. {
  64. if (header[7] == 0xaa)
  65. return header[3];
  66. if (header[0] & 0x30)
  67. return header[0] & 0x20 ? 32 : 16; // 0x10 => 16; 0x20/0x30 => 32
  68. else
  69. switch (header[1] >> 5)
  70. {
  71. case 0:
  72. case 4:
  73. return 16;
  74. case 1:
  75. case 2:
  76. case 3:
  77. return 32;
  78. default: // 5/6/7
  79. return 4;
  80. }
  81. }
  82. int
  83. get_blocks2 (unsigned char *header)
  84. {
  85. if (header[0] & 0x30)
  86. return header[0] & 0x10 ? 32 : 16; // 0x10/0x30 => 32; 0x20 => 16
  87. else
  88. return 0;
  89. }
  90. int
  91. get_blocks3 (unsigned char *header)
  92. {
  93. if (header[7] == 0xaa)
  94. return header[4];
  95. if (header[0] & 0x30)
  96. return 0;
  97. else
  98. switch (header[1] >> 5)
  99. {
  100. default: // 0/1/2/3
  101. return 0;
  102. case 4:
  103. case 5:
  104. return 4;
  105. case 6:
  106. return 2;
  107. case 7:
  108. return 1;
  109. }
  110. }
  111. int
  112. smc_write_rom (const char *filename, unsigned int parport)
  113. {
  114. FILE *file;
  115. unsigned char *buffer;
  116. int bytesread, bytessend, size, offset, n_blocks1, n_blocks2, n_blocks3, n;
  117. time_t starttime;
  118. ffe_init_io (parport);
  119. if ((file = fopen (filename, "rb")) == NULL)
  120. {
  121. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  122. exit (1);
  123. }
  124. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  125. {
  126. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  127. exit (1);
  128. }
  129. ffe_send_command0 (0x4500, 2);
  130. ffe_send_command0 (0x42fd, 0x20);
  131. ffe_send_command0 (0x43fc, 0);
  132. fread (buffer, 1, SMC_HEADER_LEN, file);
  133. n_blocks1 = get_blocks1 (buffer);
  134. n_blocks2 = get_blocks2 (buffer);
  135. n_blocks3 = get_blocks3 (buffer);
  136. size = (n_blocks1 + n_blocks2 + n_blocks3) * 8 * 1024 + 8 +
  137. (buffer[0] & SMC_TRAINER ? 512 : 0);
  138. printf ("Send: %d Bytes (%.4f Mb)\n", size, (float) size / MBIT);
  139. ffe_send_block (0x5020, buffer, 8); // send "file control block"
  140. bytessend = 8;
  141. if (buffer[1] >> 5 > 4)
  142. offset = 12;
  143. else
  144. offset = 0;
  145. if (buffer[0] & SMC_TRAINER) // send trainer if present
  146. {
  147. fread (buffer, 1, 512, file);
  148. ffe_send_block (0x600, buffer, 512);
  149. bytessend += 512;
  150. }
  151. printf ("Press q to abort\n\n");
  152. starttime = time (NULL);
  153. for (n = 0; n < n_blocks1; n++)
  154. {
  155. ffe_send_command0 (0x4507, (unsigned char) (n + offset));
  156. if ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) == 0)
  157. break;
  158. ffe_send_block (0x6000, buffer, bytesread);
  159. bytessend += bytesread;
  160. ucon64_gauge (starttime, bytessend, size);
  161. ffe_checkabort (2);
  162. }
  163. for (n = 0; n < n_blocks2; n++)
  164. {
  165. ffe_send_command0 (0x4507, (unsigned char) (n + 32));
  166. if ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) == 0)
  167. break;
  168. ffe_send_block (0x6000, buffer, bytesread);
  169. bytessend += bytesread;
  170. ucon64_gauge (starttime, bytessend, size);
  171. ffe_checkabort (2);
  172. }
  173. ffe_send_command0 (0x2001, 0);
  174. for (n = 0; n < n_blocks3; n++)
  175. {
  176. if (n == 0)
  177. {
  178. ffe_send_command0 (0x4500, 0x22);
  179. ffe_send_command0 (0x42ff, 0x30);
  180. if ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) == 0)
  181. break;
  182. ffe_send_block (0x6000, buffer, bytesread);
  183. }
  184. else
  185. {
  186. int m;
  187. ffe_send_command0 (0x4500, 7);
  188. for (m = 0; m < 8; m++)
  189. ffe_send_command0 ((unsigned short) (0x4510 + m), (unsigned char) (n * 8 + m));
  190. if ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) == 0)
  191. break;
  192. ffe_send_block2 (0, buffer, bytesread);
  193. }
  194. bytessend += bytesread;
  195. ucon64_gauge (starttime, bytessend, size);
  196. ffe_checkabort (2);
  197. }
  198. for (n = 0x4504; n < 0x4508; n++)
  199. ffe_send_command0 ((unsigned short) n, 0);
  200. for (n = 0x4510; n < 0x451c; n++)
  201. ffe_send_command0 ((unsigned short) n, 0);
  202. ffe_send_command (5, 1, 0);
  203. free (buffer);
  204. fclose (file);
  205. ffe_deinit_io ();
  206. return 0;
  207. }
  208. int
  209. smc_read_rts (const char *filename, unsigned int parport)
  210. {
  211. FILE *file;
  212. unsigned char *buffer;
  213. int bytesreceived = 0, size, n;
  214. time_t starttime;
  215. ffe_init_io (parport);
  216. if ((file = fopen (filename, "wb")) == NULL)
  217. {
  218. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  219. exit (1);
  220. }
  221. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  222. {
  223. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  224. exit (1);
  225. }
  226. size = 0x68 + 4 * 1024 + 5 * 8 * 1024;
  227. printf ("Receive: %d Bytes\n", size);
  228. memset (buffer, 0, SMC_HEADER_LEN);
  229. buffer[8] = 0xaa;
  230. buffer[9] = 0xbb;
  231. buffer[10] = 1;
  232. printf ("Press q to abort\n\n");
  233. starttime = time (NULL);
  234. ffe_send_command (5, 3, 0);
  235. ffe_receive_block (0x5840, buffer + 0x100, 0x68);
  236. fwrite (buffer, 1, SMC_HEADER_LEN, file);
  237. bytesreceived += 0x68;
  238. ffe_send_command0 (0x4500, 0x32);
  239. ffe_send_command0 (0x42ff, 0x30);
  240. ffe_receive_block (0x6000, buffer, BUFFERSIZE / 2); // 0x1000
  241. fwrite (buffer, 1, BUFFERSIZE / 2, file);
  242. bytesreceived += BUFFERSIZE / 2;
  243. ucon64_gauge (starttime, bytesreceived, size);
  244. ffe_checkabort (2);
  245. for (n = 2; n <= 0x22; n += 0x20)
  246. {
  247. ffe_send_command0 (0x4500, (unsigned char) n);
  248. ffe_receive_block (0x6000, buffer, BUFFERSIZE); // 0x2000
  249. fwrite (buffer, 1, BUFFERSIZE, file);
  250. bytesreceived += BUFFERSIZE;
  251. ucon64_gauge (starttime, bytesreceived, size);
  252. ffe_checkabort (2);
  253. }
  254. for (n = 1; n <= 3; n++)
  255. {
  256. ffe_send_command0 (0x43fc, (unsigned char) n);
  257. if (n == 1)
  258. ffe_send_command0 (0x2001, 0);
  259. ffe_receive_block2 (0, buffer, BUFFERSIZE); // 0x2000
  260. fwrite (buffer, 1, BUFFERSIZE, file);
  261. bytesreceived += BUFFERSIZE;
  262. ucon64_gauge (starttime, bytesreceived, size);
  263. ffe_checkabort (2);
  264. }
  265. ffe_send_command0 (0x43fc, 0);
  266. ffe_send_command0 (0x2001, 0x6b);
  267. free (buffer);
  268. fclose (file);
  269. ffe_deinit_io ();
  270. return 0;
  271. }
  272. int
  273. smc_write_rts (const char *filename, unsigned int parport)
  274. {
  275. FILE *file;
  276. unsigned char *buffer;
  277. int bytessend = 0, size, n;
  278. time_t starttime;
  279. ffe_init_io (parport);
  280. if ((file = fopen (filename, "rb")) == NULL)
  281. {
  282. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  283. exit (1);
  284. }
  285. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  286. {
  287. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  288. exit (1);
  289. }
  290. size = 0x68 + 4 * 1024 + 5 * 8 * 1024;
  291. printf ("Send: %d Bytes\n", size);
  292. fread (buffer, 1, SMC_HEADER_LEN, file);
  293. printf ("Press q to abort\n\n");
  294. starttime = time (NULL);
  295. ffe_send_command (5, 3, 0);
  296. ffe_send_block (0x5840, buffer + 0x100, 0x68);
  297. bytessend += 0x68;
  298. ffe_send_command0 (0x4500, 0x32);
  299. ffe_send_command0 (0x42ff, 0x30);
  300. fread (buffer, 1, BUFFERSIZE / 2, file);
  301. ffe_send_block (0x6000, buffer, BUFFERSIZE / 2); // 0x1000
  302. bytessend += BUFFERSIZE / 2;
  303. ucon64_gauge (starttime, bytessend, size);
  304. ffe_checkabort (2);
  305. for (n = 2; n <= 0x22; n += 0x20)
  306. {
  307. ffe_send_command0 (0x4500, (unsigned char) n);
  308. fread (buffer, 1, BUFFERSIZE, file);
  309. ffe_send_block (0x6000, buffer, BUFFERSIZE); // 0x2000
  310. bytessend += BUFFERSIZE;
  311. ucon64_gauge (starttime, bytessend, size);
  312. ffe_checkabort (2);
  313. }
  314. for (n = 1; n <= 3; n++)
  315. {
  316. ffe_send_command0 (0x43fc, (unsigned char) n);
  317. if (n == 1)
  318. ffe_send_command0 (0x2001, 0);
  319. fread (buffer, 1, BUFFERSIZE, file);
  320. ffe_send_block2 (0, buffer, BUFFERSIZE); // 0x2000
  321. bytessend += BUFFERSIZE;
  322. ucon64_gauge (starttime, bytessend, size);
  323. ffe_checkabort (2);
  324. }
  325. ffe_send_command0 (0x43fc, 0);
  326. ffe_send_command0 (0x2001, 0x6b);
  327. free (buffer);
  328. fclose (file);
  329. ffe_deinit_io ();
  330. return 0;
  331. }
  332. #endif // USE_PARALLEL