smc.c 9.7 KB

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