md-pro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. md-pro.c - MD-PRO flash card programmer support for uCON64
  3. Copyright (c) 2003 - 2004 dbjh
  4. Copyright (c) 2003 NoisyB <noisyb@gmx.net>
  5. Based on Delphi source code by ToToTEK Multi Media. Information in that source
  6. code has been used with permission. However, ToToTEK Multi Media explicitly
  7. stated that the information in that source code may be freely distributed.
  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; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #include "misc/parallel.h"
  27. #include "misc/itypes.h"
  28. #include "misc/misc.h"
  29. #ifdef USE_ZLIB
  30. #include "misc/archive.h"
  31. #endif
  32. #include "misc/getopt2.h" // st_getopt2_t
  33. #include "misc/file.h"
  34. #include "ucon64.h"
  35. #include "ucon64_misc.h"
  36. #include "tototek.h"
  37. #include "md-pro.h"
  38. const st_getopt2_t mdpro_usage[] =
  39. {
  40. {
  41. NULL, 0, 0, 0,
  42. NULL, "MD-PRO flash card programmer"/*"2003 ToToTEK Multi Media http://www.tototek.com"*/,
  43. NULL
  44. },
  45. #ifdef USE_PARALLEL
  46. {
  47. "xmd", 0, 0, UCON64_XMD,
  48. NULL, "send/receive ROM to/from MD-PRO flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  49. "receives automatically (32/64 Mbits) when ROM does not exist",
  50. &ucon64_wf[WF_OBJ_GEN_DEFAULT_STOP_NO_SPLIT_NO_ROM]
  51. },
  52. {
  53. "xmds", 0, 0, UCON64_XMDS,
  54. NULL, "send/receive SRAM to/from MD-PRO flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  55. "receives automatically when SRAM does not exist",
  56. &ucon64_wf[WF_OBJ_GEN_STOP_NO_ROM]
  57. },
  58. {
  59. "xmdb", 1, 0, UCON64_XMDB,
  60. "BANK", "send/receive SRAM to/from MD-PRO BANK\n"
  61. "BANK can be a number from 1 to 4; " OPTION_LONG_S "port=PORT\n"
  62. "receives automatically when SRAM does not exist",
  63. &ucon64_wf[WF_OBJ_GEN_STOP_NO_ROM]
  64. },
  65. #endif
  66. {NULL, 0, 0, 0, NULL, NULL, NULL}
  67. };
  68. #ifdef USE_PARALLEL
  69. static void eep_reset (void);
  70. static void write_rom_by_byte (int *addr, unsigned char *buf);
  71. static void write_rom_by_page (int *addr, unsigned char *buf);
  72. static void write_ram_by_byte (int *addr, unsigned char *buf);
  73. static void write_ram_by_page (int *addr, unsigned char *buf);
  74. static unsigned short int md_id;
  75. void
  76. eep_reset (void)
  77. {
  78. ttt_rom_enable ();
  79. ttt_write_mem (0x400000, 0xff); // reset EEP chip 2
  80. ttt_write_mem (0, 0xff); // reset EEP chip 1
  81. ttt_write_mem (0x600000, 0xff); // reset EEP chip 2
  82. ttt_write_mem (0x200000, 0xff); // reset EEP chip 1
  83. ttt_rom_disable ();
  84. }
  85. void
  86. write_rom_by_byte (int *addr, unsigned char *buf)
  87. {
  88. int x;
  89. for (x = 0; x < 0x4000; x++)
  90. {
  91. if (md_id == 0xb0d0)
  92. ttt_write_byte_sharp (*addr, buf[*addr & 0x3fff]);
  93. else if (md_id == 0x8916 || md_id == 0x8917)
  94. ttt_write_byte_intel (*addr, buf[*addr & 0x3fff]);
  95. (*addr)++;
  96. }
  97. }
  98. void
  99. write_rom_by_page (int *addr, unsigned char *buf)
  100. {
  101. int x;
  102. for (x = 0; x < 0x200; x++)
  103. {
  104. ttt_write_page_rom (*addr, buf);
  105. (*addr) += 0x20;
  106. }
  107. }
  108. void
  109. write_ram_by_byte (int *addr, unsigned char *buf)
  110. {
  111. int x, i = *addr & 0x3fff;
  112. for (x = 0; x < 0x4000; x++, i = (i + 1) & 0x3fff)
  113. {
  114. ttt_write_byte_ram (*addr, buf[i]);
  115. (*addr)++;
  116. // Send the same byte again => SRAM files needn't store redundant data
  117. ttt_write_byte_ram (*addr, buf[i]);
  118. (*addr)++;
  119. }
  120. }
  121. void
  122. write_ram_by_page (int *addr, unsigned char *buf)
  123. {
  124. int x;
  125. for (x = 0; x < 0x80; x++)
  126. {
  127. ttt_write_page_ram2 (*addr, buf);
  128. (*addr) += 0x80;
  129. }
  130. }
  131. int
  132. md_read_rom (const char *filename, unsigned int parport, int size)
  133. {
  134. FILE *file;
  135. unsigned char buffer[0x100];
  136. int blocksleft, address = 0, id;
  137. time_t starttime;
  138. void (*read_block) (int, unsigned char *) = ttt_read_rom_w; // ttt_read_rom_b
  139. if ((file = fopen (filename, "wb")) == NULL)
  140. {
  141. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  142. exit (1);
  143. }
  144. ttt_init_io (parport);
  145. eep_reset ();
  146. id = ttt_get_id ();
  147. if ((id == 0xb0d0 || id == 0x8916) && size > 32 * MBIT)
  148. size = 32 * MBIT; // Sharp or Intel 32 Mbit flash card
  149. #if 0
  150. // size is set to 64 * MBIT "by default" (in ucon64_opts.c)
  151. else if (id == 0x8917 && size > 64 * MBIT)
  152. size = 64 * MBIT; // Intel 64 Mbit flash card
  153. #endif
  154. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  155. blocksleft = size >> 8;
  156. eep_reset ();
  157. ttt_rom_enable ();
  158. if (read_block == ttt_read_rom_w)
  159. ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  160. starttime = time (NULL);
  161. while (blocksleft-- > 0)
  162. {
  163. read_block (address, buffer); // 0x100 bytes read
  164. if (read_block == ttt_read_rom_b)
  165. ucon64_bswap16_n (buffer, 0x100);
  166. fwrite (buffer, 1, 0x100, file);
  167. address += 0x100;
  168. if ((address & 0x3fff) == 0)
  169. ucon64_gauge (starttime, address, size);
  170. }
  171. // original code doesn't call ttt_rom_disable() when byte-size function is
  172. // used (ttt_read_rom_b() calls it)
  173. if (read_block == ttt_read_rom_w)
  174. ttt_rom_disable ();
  175. fclose (file);
  176. ttt_deinit_io ();
  177. return 0;
  178. }
  179. int
  180. md_write_rom (const char *filename, unsigned int parport)
  181. {
  182. FILE *file;
  183. unsigned char buffer[0x4000];
  184. int size, address = 0, bytesread, bytessend = 0;
  185. time_t starttime;
  186. void (*write_block) (int *, unsigned char *) = write_rom_by_page; // write_rom_by_byte
  187. (void) write_rom_by_byte;
  188. if ((file = fopen (filename, "rb")) == NULL)
  189. {
  190. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  191. exit (1);
  192. }
  193. ttt_init_io (parport);
  194. size = fsizeof (filename);
  195. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  196. eep_reset ();
  197. md_id = ttt_get_id ();
  198. if ((md_id != 0xb0d0) && (md_id != 0x8916) && (md_id != 0x8917)) // Sharp 32M, Intel 64J3
  199. {
  200. fputs ("ERROR: MD-PRO flash card (programmer) not detected\n", stderr);
  201. fclose (file);
  202. ttt_deinit_io ();
  203. exit (1);
  204. }
  205. starttime = time (NULL);
  206. eep_reset ();
  207. while ((bytesread = fread (buffer, 1, 0x4000, file)))
  208. {
  209. ucon64_bswap16_n (buffer, 0x4000);
  210. if ((((address & 0xffff) == 0) && (md_id == 0xb0d0)) ||
  211. (((address & 0x1ffff) == 0) && (md_id == 0x8916 || md_id == 0x8917)))
  212. ttt_erase_block (address);
  213. write_block (&address, buffer);
  214. bytessend += bytesread;
  215. ucon64_gauge (starttime, bytessend, size);
  216. }
  217. fclose (file);
  218. ttt_deinit_io ();
  219. return 0;
  220. }
  221. int
  222. md_read_sram (const char *filename, unsigned int parport, int start_bank)
  223. /*
  224. The MD-PRO has 256 kB of SRAM. However, the SRAM dumps of all games that have
  225. been tested had each byte doubled. In order to make it possible to easily
  226. obtain the SRAM data for use in an emulator, or to send an emulator SRAM file
  227. to the MD-PRO, we remove the redundant data when receiving/dumping and double
  228. the data when sending.
  229. It could be that this approach causes trouble for some games. However, when
  230. looking at ToToTEK's own code in ttt_write_page_ram2() this seems unlikely
  231. (data is doubled in that function). Note that write_sram_by_byte() is a
  232. function written by us, and so does the doubling of data, but it doesn't mean
  233. it should work for all games.
  234. */
  235. {
  236. FILE *file;
  237. unsigned char buffer[0x100];
  238. int blocksleft, address, bytesreceived = 0, size, i;
  239. time_t starttime;
  240. void (*read_block) (int, unsigned char *) = ttt_read_ram_b; // ttt_read_ram_w
  241. // This function does not seem to work if ttt_read_ram_w() is used, but see
  242. // note below
  243. if (start_bank == -1)
  244. {
  245. address = 0;
  246. size = 128 * 1024;
  247. }
  248. else
  249. {
  250. if (start_bank < 1 || start_bank > 4)
  251. {
  252. fputs ("ERROR: Bank must be a value 1 - 4\n", stderr);
  253. exit (1);
  254. }
  255. address = (start_bank - 1) * 32 * 1024;
  256. size = 32 * 1024;
  257. }
  258. if ((file = fopen (filename, "wb")) == NULL)
  259. {
  260. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  261. exit (1);
  262. }
  263. ttt_init_io (parport);
  264. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  265. if (read_block == ttt_read_ram_w)
  266. {
  267. // address *= 2;
  268. ttt_ram_enable ();
  269. #if 0
  270. // According to JohnDie, disabling this statement should make it possible
  271. // to use ttt_read_ram_w().
  272. ttt_set_ai_data (6, 0x98); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  273. #endif
  274. }
  275. // else
  276. // ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  277. blocksleft = size >> 7;
  278. starttime = time (NULL);
  279. while (blocksleft-- > 0)
  280. {
  281. read_block (address, buffer); // 0x100 bytes read
  282. for (i = 0; i < 0x80; i++)
  283. buffer[i] = buffer[2 * i]; // data is doubled => no problems with endianess
  284. fwrite (buffer, 1, 0x80, file);
  285. address += 0x100;
  286. bytesreceived += 0x80;
  287. if ((address & 0x3fff) == 0)
  288. ucon64_gauge (starttime, bytesreceived, size);
  289. }
  290. if (read_block == ttt_read_ram_w)
  291. ttt_ram_disable ();
  292. fclose (file);
  293. ttt_deinit_io ();
  294. return 0;
  295. }
  296. int
  297. md_write_sram (const char *filename, unsigned int parport, int start_bank)
  298. {
  299. FILE *file;
  300. unsigned char buffer[0x4000];
  301. int size, bytesread, bytessend = 0, address;
  302. time_t starttime;
  303. void (*write_block) (int *, unsigned char *) = write_ram_by_byte; // write_ram_by_page
  304. (void) write_ram_by_page;
  305. size = fsizeof (filename);
  306. if (start_bank == -1)
  307. address = 0;
  308. else
  309. {
  310. if (start_bank < 1 || start_bank > 4)
  311. {
  312. fputs ("ERROR: Bank must be a value 1 - 4\n", stderr);
  313. exit (1);
  314. }
  315. address = (start_bank - 1) * 32 * 1024;
  316. }
  317. if ((file = fopen (filename, "rb")) == NULL)
  318. {
  319. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  320. exit (1);
  321. }
  322. ttt_init_io (parport);
  323. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  324. starttime = time (NULL);
  325. while ((bytesread = fread (buffer, 1, 0x4000, file)))
  326. {
  327. write_block (&address, buffer);
  328. bytessend += bytesread;
  329. ucon64_gauge (starttime, bytessend, size);
  330. }
  331. fclose (file);
  332. ttt_deinit_io ();
  333. return 0;
  334. }
  335. #endif // USE_PARALLEL