sflash.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. sflash.h - Super Flash flash card programmer support for uCON64
  3. Copyright (c) 2004 JohnDie
  4. Copyright (c) 2005 dbjh
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <stdlib.h>
  21. #include "misc/archive.h"
  22. #include "ucon64.h"
  23. #include "ucon64_misc.h"
  24. #include "backup/tototek.h"
  25. #include "backup/sflash.h"
  26. #ifdef USE_PARALLEL
  27. static st_ucon64_obj_t sflash_obj[] =
  28. {
  29. {UCON64_SNES, WF_DEFAULT | WF_STOP | WF_NO_SPLIT | WF_NO_ROM},
  30. {UCON64_SNES, WF_STOP | WF_NO_ROM}
  31. };
  32. #endif
  33. const st_getopt2_t sflash_usage[] =
  34. {
  35. {
  36. NULL, 0, 0, 0,
  37. NULL, "Super Flash flash card programmer"/*"2004 ToToTEK Multi Media http://www.tototek.com"*/,
  38. NULL
  39. },
  40. #ifdef USE_PARALLEL
  41. {
  42. "xsf", 0, 0, UCON64_XSF,
  43. NULL, "send/receive ROM to/from Super Flash flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  44. "receives automatically (64 Mbits) when ROM does not exist",
  45. &sflash_obj[0]
  46. },
  47. {
  48. "xsfs", 0, 0, UCON64_XSFS,
  49. NULL, "send/receive SRAM to/from Super Flash flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  50. "receives automatically when SRAM does not exist",
  51. &sflash_obj[1]
  52. },
  53. #endif
  54. {NULL, 0, 0, 0, NULL, NULL, NULL}
  55. };
  56. #ifdef USE_PARALLEL
  57. static void eep_reset (void);
  58. static unsigned short int check_card (void);
  59. static void write_rom_by_byte (int *addr, unsigned char *buf);
  60. static void write_rom_by_page (int *addr, unsigned char *buf);
  61. static void write_ram_by_byte (int *addr, unsigned char *buf);
  62. static void write_ram_by_page (int *addr, unsigned char *buf);
  63. void
  64. eep_reset (void)
  65. {
  66. ttt_rom_enable ();
  67. ttt_write_mem (0x400000, 0xff); // reset EEP chip 2
  68. ttt_write_mem (0, 0xff); // reset EEP chip 1
  69. ttt_write_mem (0x600000, 0xff); // reset EEP chip 2
  70. ttt_write_mem (0x200000, 0xff); // reset EEP chip 1
  71. ttt_rom_disable ();
  72. }
  73. unsigned short int
  74. check_card (void)
  75. {
  76. unsigned short int id;
  77. eep_reset ();
  78. id = ttt_get_id ();
  79. if (id != 0x8917) // Intel 64J3
  80. {
  81. fprintf (stderr, "ERROR: Super Flash flash card (programmer) not detected (ID: %02hx)\n", id);
  82. return 0;
  83. }
  84. else
  85. return id;
  86. }
  87. void
  88. write_rom_by_byte (int *addr, unsigned char *buf)
  89. {
  90. int x;
  91. for (x = 0; x < 0x4000; x++)
  92. {
  93. ttt_write_byte_intel (*addr, buf[*addr & 0x3fff]);
  94. (*addr)++;
  95. }
  96. }
  97. void
  98. write_rom_by_page (int *addr, unsigned char *buf)
  99. {
  100. int x;
  101. for (x = 0; x < 0x200; x++)
  102. {
  103. ttt_write_page_rom (*addr, buf);
  104. (*addr) += 0x20;
  105. }
  106. }
  107. void
  108. write_ram_by_byte (int *addr, unsigned char *buf)
  109. {
  110. int x, i = *addr & 0x3fff;
  111. for (x = 0; x < 0x4000; x++, i = (i + 1) & 0x3fff)
  112. {
  113. ttt_write_byte_ram (*addr, buf[i]);
  114. (*addr)++;
  115. }
  116. }
  117. void
  118. write_ram_by_page (int *addr, unsigned char *buf)
  119. {
  120. int x;
  121. for (x = 0; x < 0x40; x++)
  122. {
  123. ttt_write_page_ram (*addr, buf);
  124. (*addr) += 0x100;
  125. }
  126. }
  127. int
  128. sf_read_rom (const char *filename, unsigned short parport, int size)
  129. {
  130. FILE *file;
  131. unsigned char buffer[0x100];
  132. int blocksleft, address = 0;
  133. time_t starttime;
  134. void (*read_block) (int, unsigned char *) = ttt_read_rom_w; // ttt_read_rom_b
  135. if ((file = fopen (filename, "wb")) == NULL)
  136. {
  137. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  138. exit (1);
  139. }
  140. ttt_init_io (parport);
  141. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  142. if (check_card () == 0)
  143. {
  144. fclose (file);
  145. remove (filename);
  146. exit (1);
  147. }
  148. blocksleft = size >> 8;
  149. eep_reset ();
  150. ttt_rom_enable ();
  151. if (read_block == ttt_read_rom_w)
  152. ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  153. starttime = time (NULL);
  154. while (blocksleft-- > 0)
  155. {
  156. read_block (address, buffer); // 0x100 bytes read
  157. if (read_block == ttt_read_rom_b)
  158. ucon64_bswap16_n (buffer, 0x100);
  159. fwrite (buffer, 1, 0x100, file);
  160. address += 0x100;
  161. if ((address & 0x3fff) == 0)
  162. ucon64_gauge (starttime, address, size);
  163. }
  164. // ttt_read_rom_b() calls ttt_rom_disable()
  165. if (read_block == ttt_read_rom_w)
  166. ttt_rom_disable ();
  167. fclose (file);
  168. return 0;
  169. }
  170. int
  171. sf_write_rom (const char *filename, unsigned short parport)
  172. {
  173. FILE *file;
  174. unsigned char buffer[0x4000], game_table[0x80];
  175. int game_no, size, address = 0, bytesread, bytessent = 0, bytesleft;
  176. time_t starttime;
  177. void (*write_block) (int *, unsigned char *) = write_rom_by_page; // write_rom_by_byte
  178. (void) write_rom_by_byte;
  179. if ((file = fopen (filename, "rb")) == NULL)
  180. {
  181. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  182. exit (1);
  183. }
  184. ttt_init_io (parport);
  185. fseek (file, 0x4000, SEEK_SET);
  186. bytesread = fread (game_table, 1, 0x80, file);
  187. if (bytesread != 0x80)
  188. {
  189. fputs ("ERROR: Could not read game table from file\n", stderr);
  190. fclose (file);
  191. return -1;
  192. }
  193. size = ucon64.file_size;
  194. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  195. if (check_card () == 0)
  196. {
  197. fclose (file);
  198. exit (1);
  199. }
  200. fseek (file, 0x8000, SEEK_SET);
  201. starttime = time (NULL);
  202. // Erase last block now, because we have to write to it anyway later. Erasing
  203. // it later could erase part of a game.
  204. ttt_erase_block (0x7e0000);
  205. for (game_no = 0; game_no < 4; game_no++)
  206. {
  207. if (game_table[game_no * 0x20] == 0)
  208. continue;
  209. bytesleft = game_table[game_no * 0x20 + 0x1f] * 0x8000;
  210. switch (game_table[game_no * 0x20 + 0x1d] & 0x60)
  211. {
  212. case 0x00:
  213. address = 0x000000;
  214. break;
  215. case 0x40:
  216. address = 0x200000;
  217. break;
  218. case 0x20:
  219. address = 0x400000;
  220. break;
  221. case 0x60:
  222. address = 0x600000;
  223. break;
  224. // no default case because we handled all possible cases
  225. }
  226. eep_reset ();
  227. while (bytesleft > 0 && (bytesread = fread (buffer, 1, 0x4000, file)) != 0)
  228. {
  229. if ((address & 0x1ffff) == 0)
  230. ttt_erase_block (address);
  231. if (address < 0x7f8000) // We mustn't write to the loader space
  232. write_block (&address, buffer);
  233. bytessent += bytesread;
  234. ucon64_gauge (starttime, bytessent, size);
  235. bytesleft -= bytesread;
  236. }
  237. }
  238. fseek (file, 0, SEEK_SET);
  239. bytesleft = 0x8000;
  240. address = 0x7f8000;
  241. while (bytesleft > 0 && (bytesread = fread (buffer, 1, 0x4000, file)) != 0)
  242. {
  243. write_block (&address, buffer);
  244. bytessent += bytesread;
  245. ucon64_gauge (starttime, bytessent, size);
  246. bytesleft -= bytesread;
  247. }
  248. fclose (file);
  249. return 0;
  250. }
  251. int
  252. sf_read_sram (const char *filename, unsigned short parport)
  253. {
  254. FILE *file;
  255. unsigned char buffer[0x100];
  256. int blocksleft, address, bytesreceived = 0, size;
  257. time_t starttime;
  258. void (*read_block) (int, unsigned char *) = ttt_read_ram_w; // ttt_read_ram_w
  259. address = 0xfe0000; // SRAM is stored at 0xfe0000
  260. size = 0x020000; // SRAM size is 1024 kbit
  261. if ((file = fopen (filename, "wb")) == NULL)
  262. {
  263. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  264. exit (1);
  265. }
  266. ttt_init_io (parport);
  267. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  268. if (check_card () == 0)
  269. {
  270. fclose (file);
  271. remove (filename);
  272. exit (1);
  273. }
  274. // ttt_ram_enable (); // The next ttt_set_ai_data also enables ram access
  275. ttt_set_ai_data (6, 0x98); // Enable cartridge access, auto address increment
  276. blocksleft = size >> 8;
  277. starttime = time (NULL);
  278. while (blocksleft-- > 0)
  279. {
  280. read_block (address, buffer); // 0x100 bytes read
  281. fwrite (buffer, 1, 0x100, file);
  282. address += 0x100;
  283. bytesreceived += 0x100;
  284. if ((address & 0x3fff) == 0)
  285. ucon64_gauge (starttime, bytesreceived, size);
  286. }
  287. ttt_ram_disable ();
  288. fclose (file);
  289. return 0;
  290. }
  291. int
  292. sf_write_sram (const char *filename, unsigned short parport)
  293. {
  294. FILE *file;
  295. unsigned char buffer[0x4000];
  296. int size, bytesread, bytessent = 0, address;
  297. time_t starttime;
  298. void (*write_block) (int *, unsigned char *) = write_ram_by_byte; // write_ram_by_page
  299. (void) write_ram_by_page;
  300. size = ucon64.file_size;
  301. address = 0xfe0000;
  302. if ((file = fopen (filename, "rb")) == NULL)
  303. {
  304. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  305. exit (1);
  306. }
  307. ttt_init_io (parport);
  308. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  309. if (check_card () == 0)
  310. {
  311. fclose (file);
  312. exit (1);
  313. }
  314. ttt_ram_enable ();
  315. starttime = time (NULL);
  316. while ((bytesread = fread (buffer, 1, 0x4000, file)) != 0)
  317. {
  318. write_block (&address, buffer); // 0x4000 bytes write
  319. bytessent += bytesread;
  320. if ((address & 0x3fff) == 0)
  321. ucon64_gauge (starttime, bytessent, size);
  322. }
  323. fclose (file);
  324. return 0;
  325. }
  326. #endif // USE_PARALLEL