smsgg-pro.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. smsgg-pro.c - SMS-PRO/GG-PRO flash card programmer support for uCON64
  3. Copyright (c) 2004 dbjh
  4. Based on Delphi source code by ToToTEK Multi Media. Information in that source
  5. code has been used with permission. However, ToToTEK Multi Media explicitly
  6. stated that the information in that source code may be freely distributed.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include "misc/parallel.h"
  26. #include "misc/itypes.h"
  27. #ifdef USE_ZLIB
  28. #include "misc/archive.h"
  29. #endif
  30. #include "misc/getopt2.h" // st_getopt2_t
  31. #include "misc/file.h"
  32. #include "misc/misc.h"
  33. #include "ucon64.h"
  34. #include "ucon64_misc.h"
  35. #include "tototek.h"
  36. #include "smsgg-pro.h"
  37. const st_getopt2_t smsggpro_usage[] =
  38. {
  39. {
  40. NULL, 0, 0, 0,
  41. NULL, "SMS-PRO/GG-PRO flash card programmer"/*"2004 ToToTEK Multi Media http://www.tototek.com"*/,
  42. NULL
  43. },
  44. #ifdef USE_PARALLEL
  45. {
  46. "xgg", 0, 0, UCON64_XGG,
  47. NULL, "send/receive ROM to/from SMS-PRO/GG-PRO flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  48. "receives automatically (32 Mbits) when ROM does not exist",
  49. &ucon64_wf[WF_OBJ_SMS_DEFAULT_STOP_NO_SPLIT_NO_ROM]
  50. },
  51. {
  52. "xggs", 0, 0, UCON64_XGGS,
  53. NULL, "send/receive SRAM to/from SMS-PRO/GG-PRO flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  54. "receives automatically when SRAM does not exist",
  55. &ucon64_wf[WF_OBJ_SMS_STOP_NO_ROM]
  56. },
  57. {
  58. "xggb", 1, 0, UCON64_XGGB,
  59. "BANK", "send/receive SRAM to/from SMS-PRO/GG-PRO BANK\n"
  60. "BANK can be a number from 1 to 4; " OPTION_LONG_S "port=PORT\n"
  61. "receives automatically when SRAM does not exist",
  62. &ucon64_wf[WF_OBJ_SMS_STOP_NO_ROM]
  63. },
  64. #endif // USE_PARALLEL
  65. {NULL, 0, 0, 0, NULL, NULL, NULL}
  66. };
  67. #ifdef USE_PARALLEL
  68. static void eep_reset (void);
  69. static void write_rom_by_byte (int *addr, unsigned char *buf);
  70. static void write_rom_by_page (int *addr, unsigned char *buf);
  71. static void write_ram_by_byte (int *addr, unsigned char *buf);
  72. static void write_ram_by_page (int *addr, unsigned char *buf);
  73. void
  74. eep_reset (void)
  75. {
  76. ttt_rom_enable ();
  77. ttt_write_mem (0x000000, 0xff); // reset EEP
  78. ttt_write_mem (0x200000, 0xff); // reset EEP
  79. ttt_rom_disable ();
  80. }
  81. void
  82. write_rom_by_byte (int *addr, unsigned char *buf)
  83. {
  84. int x;
  85. for (x = 0; x < 0x4000; x++)
  86. {
  87. ttt_write_byte_sharp (*addr, buf[*addr & 0x3fff]);
  88. (*addr)++;
  89. }
  90. }
  91. void
  92. write_rom_by_page (int *addr, unsigned char *buf)
  93. {
  94. int x;
  95. for (x = 0; x < 0x200; x++)
  96. {
  97. ttt_write_page_rom (*addr, buf);
  98. (*addr) += 0x20;
  99. }
  100. }
  101. void
  102. write_ram_by_byte (int *addr, unsigned char *buf)
  103. {
  104. int x;
  105. for (x = 0; x < 0x4000; x++)
  106. {
  107. ttt_write_byte_ram (*addr, buf[*addr & 0x3fff]);
  108. (*addr)++;
  109. }
  110. }
  111. void
  112. write_ram_by_page (int *addr, unsigned char *buf)
  113. {
  114. int x;
  115. for (x = 0; x < 0x40; x++)
  116. {
  117. ttt_write_page_ram (*addr, buf);
  118. (*addr) += 0x100;
  119. }
  120. }
  121. int
  122. smsgg_read_rom (const char *filename, unsigned int parport, int size)
  123. {
  124. FILE *file;
  125. unsigned char buffer[0x100];
  126. int blocksleft, address = 0;
  127. time_t starttime;
  128. void (*read_block) (int, unsigned char *) = ttt_read_rom_w; // ttt_read_rom_b
  129. if ((file = fopen (filename, "wb")) == NULL)
  130. {
  131. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  132. exit (1);
  133. }
  134. ttt_init_io (parport);
  135. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  136. blocksleft = size >> 8;
  137. eep_reset ();
  138. ttt_rom_enable ();
  139. if (read_block == ttt_read_rom_w)
  140. ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  141. starttime = time (NULL);
  142. while (blocksleft-- > 0)
  143. {
  144. read_block (address, buffer); // 0x100 bytes read
  145. fwrite (buffer, 1, 0x100, file);
  146. address += 0x100;
  147. if ((address & 0x3fff) == 0)
  148. ucon64_gauge (starttime, address, size);
  149. }
  150. // original code doesn't call ttt_rom_disable() when byte-size function is
  151. // used (ttt_read_rom_b() calls it)
  152. if (read_block == ttt_read_rom_w)
  153. ttt_rom_disable ();
  154. fclose (file);
  155. ttt_deinit_io ();
  156. return 0;
  157. }
  158. int
  159. smsgg_write_rom (const char *filename, unsigned int parport)
  160. {
  161. FILE *file;
  162. unsigned char buffer[0x4000];
  163. int size, address = 0, bytesread, bytessend = 0;
  164. time_t starttime;
  165. void (*write_block) (int *, unsigned char *) = write_rom_by_page; // write_rom_by_byte
  166. (void) write_rom_by_byte;
  167. if ((file = fopen (filename, "rb")) == NULL)
  168. {
  169. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  170. exit (1);
  171. }
  172. ttt_init_io (parport);
  173. size = fsizeof (filename);
  174. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  175. eep_reset ();
  176. if (ttt_get_id () != 0xb0d0)
  177. {
  178. fputs ("ERROR: SMS-PRO/GG-PRO flash card (programmer) not detected\n", stderr);
  179. fclose (file);
  180. ttt_deinit_io ();
  181. exit (1);
  182. }
  183. starttime = time (NULL);
  184. eep_reset ();
  185. while ((bytesread = fread (buffer, 1, 0x4000, file)))
  186. {
  187. if ((address & 0xffff) == 0)
  188. ttt_erase_block (address);
  189. write_block (&address, buffer);
  190. bytessend += bytesread;
  191. ucon64_gauge (starttime, bytessend, size);
  192. }
  193. fclose (file);
  194. ttt_deinit_io ();
  195. return 0;
  196. }
  197. int
  198. smsgg_read_sram (const char *filename, unsigned int parport, int start_bank)
  199. {
  200. FILE *file;
  201. unsigned char buffer[0x100];
  202. int blocksleft, address, size, bytesreceived = 0;
  203. time_t starttime;
  204. void (*read_block) (int, unsigned char *) = ttt_read_ram_b; // ttt_read_ram_w
  205. if (start_bank == -1)
  206. {
  207. address = 0;
  208. size = 128 * 1024;
  209. }
  210. else
  211. {
  212. if (start_bank < 1 || start_bank > 4)
  213. {
  214. fputs ("ERROR: Bank must be a value 1 - 4\n", stderr);
  215. exit (1);
  216. }
  217. address = (start_bank - 1) * 32 * 1024;
  218. size = 32 * 1024;
  219. }
  220. if ((file = fopen (filename, "wb")) == NULL)
  221. {
  222. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  223. exit (1);
  224. }
  225. ttt_init_io (parport);
  226. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  227. if (read_block == ttt_read_ram_w)
  228. {
  229. ttt_ram_enable ();
  230. ttt_set_ai_data (6, 0x98); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  231. }
  232. // else
  233. // ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  234. blocksleft = size >> 8;
  235. starttime = time (NULL);
  236. while (blocksleft-- > 0)
  237. {
  238. read_block (address, buffer); // 0x100 bytes read
  239. fwrite (buffer, 1, 0x100, file);
  240. address += 0x100;
  241. bytesreceived += 0x100;
  242. if ((address & 0x3fff) == 0)
  243. ucon64_gauge (starttime, bytesreceived, size);
  244. }
  245. if (read_block == ttt_read_ram_w)
  246. ttt_ram_disable ();
  247. fclose (file);
  248. ttt_deinit_io ();
  249. return 0;
  250. }
  251. int
  252. smsgg_write_sram (const char *filename, unsigned int parport, int start_bank)
  253. {
  254. FILE *file;
  255. unsigned char buffer[0x4000];
  256. int size, bytesread, bytessend = 0, address;
  257. time_t starttime;
  258. void (*write_block) (int *, unsigned char *) = write_ram_by_byte; // write_ram_by_page
  259. (void) write_ram_by_page;
  260. size = fsizeof (filename);
  261. if (start_bank == -1)
  262. address = 0;
  263. else
  264. {
  265. if (start_bank < 1 || start_bank > 4)
  266. {
  267. fputs ("ERROR: Bank must be a value 1 - 4\n", stderr);
  268. exit (1);
  269. }
  270. address = (start_bank - 1) * 32 * 1024;
  271. }
  272. if ((file = fopen (filename, "rb")) == NULL)
  273. {
  274. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  275. exit (1);
  276. }
  277. ttt_init_io (parport);
  278. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  279. starttime = time (NULL);
  280. while ((bytesread = fread (buffer, 1, 0x4000, file)))
  281. {
  282. write_block (&address, buffer);
  283. bytessend += bytesread;
  284. ucon64_gauge (starttime, bytessend, size);
  285. }
  286. fclose (file);
  287. ttt_deinit_io ();
  288. return 0;
  289. }
  290. #endif // USE_PARALLEL