pce-pro.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. pce-pro.c - PCE-PRO flash card programmer support for uCON64
  3. Copyright (c) 2004 - 2005 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 <stdlib.h>
  23. #include <string.h>
  24. #include "misc/archive.h"
  25. #include "ucon64.h"
  26. #include "ucon64_misc.h"
  27. #include "backup/tototek.h"
  28. #include "backup/pce-pro.h"
  29. #ifdef USE_PARALLEL
  30. static st_ucon64_obj_t pcepro_obj[] =
  31. {
  32. {UCON64_PCE, WF_DEFAULT | WF_STOP | WF_NO_SPLIT | WF_NO_ROM}
  33. };
  34. #endif
  35. const st_getopt2_t pcepro_usage[] =
  36. {
  37. {
  38. NULL, 0, 0, 0,
  39. NULL, "PCE-PRO flash card programmer"/*"2004 ToToTEK Multi Media http://www.tototek.com"*/,
  40. NULL
  41. },
  42. #ifdef USE_PARALLEL
  43. {
  44. "xpce", 0, 0, UCON64_XPCE,
  45. NULL, "send/receive ROM to/from PCE-PRO flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  46. "receives automatically (32 Mbits) when ROM does not exist",
  47. &pcepro_obj[0]
  48. },
  49. #endif
  50. {NULL, 0, 0, 0, NULL, NULL, NULL}
  51. };
  52. #ifdef USE_PARALLEL
  53. static void eep_reset (void);
  54. static unsigned short int check_card (void);
  55. static void write_rom_by_byte (int *addr, unsigned char *buf);
  56. static void write_rom_by_page (int *addr, unsigned char *buf);
  57. static void
  58. eep_reset (void)
  59. {
  60. ttt_rom_enable ();
  61. ttt_write_mem (0x000000, 0xff); // reset EEP
  62. ttt_write_mem (0x200000, 0xff); // reset EEP
  63. ttt_rom_disable ();
  64. }
  65. static unsigned short int
  66. check_card (void)
  67. {
  68. unsigned short int id;
  69. eep_reset ();
  70. id = ttt_get_id ();
  71. if (id != 0xb0d0)
  72. {
  73. fprintf (stderr, "ERROR: PCE-PRO flash card (programmer) not detected (ID: 0x%02hx)\n", id);
  74. return 0;
  75. }
  76. else
  77. return id;
  78. }
  79. static void
  80. write_rom_by_byte (int *addr, unsigned char *buf)
  81. {
  82. int x;
  83. for (x = 0; x < 0x4000; x++)
  84. {
  85. ttt_write_byte_sharp (*addr, buf[*addr & 0x3fff]);
  86. (*addr)++;
  87. }
  88. }
  89. static void
  90. write_rom_by_page (int *addr, unsigned char *buf)
  91. {
  92. int x;
  93. for (x = 0; x < 0x200; x++)
  94. {
  95. ttt_write_page_rom (*addr, buf);
  96. (*addr) += 0x20;
  97. }
  98. }
  99. int
  100. pce_read_rom (const char *filename, unsigned short parport, int size)
  101. {
  102. FILE *file;
  103. unsigned char buffer[0x100];
  104. int blocksleft, address = 0;
  105. time_t starttime;
  106. void (*read_block) (int, unsigned char *) = ttt_read_rom_w; // ttt_read_rom_b
  107. if ((file = fopen (filename, "wb")) == NULL)
  108. {
  109. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  110. exit (1);
  111. }
  112. ttt_init_io (parport);
  113. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  114. if (check_card () == 0)
  115. {
  116. fclose (file);
  117. remove (filename);
  118. exit (1);
  119. }
  120. blocksleft = size >> 8;
  121. eep_reset ();
  122. ttt_rom_enable ();
  123. if (read_block == ttt_read_rom_w)
  124. ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  125. starttime = time (NULL);
  126. while (blocksleft-- > 0)
  127. {
  128. read_block (address, buffer); // 0x100 bytes read
  129. fwrite (buffer, 1, 0x100, file);
  130. address += 0x100;
  131. if ((address & 0x3fff) == 0)
  132. ucon64_gauge (starttime, address, size);
  133. }
  134. // original code doesn't call ttt_rom_disable() when byte-size function is
  135. // used (ttt_read_rom_b() calls it)
  136. if (read_block == ttt_read_rom_w)
  137. ttt_rom_disable ();
  138. fclose (file);
  139. return 0;
  140. }
  141. int
  142. pce_write_rom (const char *filename, unsigned short parport)
  143. {
  144. FILE *file;
  145. unsigned char buffer[0x4000], game_table[32 * 0x20];
  146. int game_no, size, romsize = 0, startaddress, address = 0, bytesread,
  147. bytessent = 0, bytesleft, multi_game;
  148. time_t starttime;
  149. void (*write_block) (int *, unsigned char *) = write_rom_by_page; // write_rom_by_byte
  150. (void) write_rom_by_byte;
  151. if ((file = fopen (filename, "rb")) == NULL)
  152. {
  153. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  154. exit (1);
  155. }
  156. ttt_init_io (parport);
  157. fseek (file, 0xb3f4, SEEK_SET);
  158. buffer[0] = 0;
  159. fread (buffer, 1, 12, file); // it's OK to not verify if we can read
  160. // currently we ignore the version string (full string is "uCON64 2.0.1")
  161. multi_game = strncmp ((char *) buffer, "uCON64", 6) ? 0 : 1;
  162. if (multi_game)
  163. {
  164. fseek (file, 0xb000, SEEK_SET);
  165. bytesread = fread (game_table, 1, 32 * 0x20, file);
  166. if (bytesread != 32 * 0x20)
  167. {
  168. fputs ("ERROR: Could not read game table from file\n", stderr);
  169. fclose (file);
  170. return -1;
  171. }
  172. }
  173. size = ucon64.file_size;
  174. // 4 Mbit games need the last 2 Mbit to be mirrored (so, they need 6 Mbit)
  175. if (multi_game)
  176. {
  177. game_no = 0;
  178. while (game_table[game_no * 0x20] && game_no < 31)
  179. {
  180. if (game_table[game_no * 0x20 + 0x1e] == 4)
  181. size += 2 * MBIT;
  182. game_no++;
  183. }
  184. }
  185. else
  186. {
  187. romsize = size; // one file (no multi-game)
  188. if (size == 4 * MBIT)
  189. size += 2 * MBIT;
  190. }
  191. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  192. if (check_card () == 0)
  193. {
  194. fclose (file);
  195. exit (1);
  196. }
  197. fseek (file, 0, SEEK_SET);
  198. starttime = time (NULL);
  199. eep_reset ();
  200. game_no = -1;
  201. do
  202. {
  203. if (game_no >= 0) // a game of a multi-game file
  204. romsize = game_table[game_no * 0x20 + 0x1e] * MBIT;
  205. else if (multi_game)
  206. romsize = PCE_PRO_LOADER_SIZE; // the loader
  207. bytesleft = romsize;
  208. if (bytesleft == 4 * MBIT)
  209. bytesleft += 2 * MBIT;
  210. startaddress = address;
  211. while (bytesleft > 0 && (bytesread = fread (buffer, 1, 0x4000, file)) != 0)
  212. {
  213. if ((address & 0xffff) == 0)
  214. ttt_erase_block (address);
  215. write_block (&address, buffer);
  216. if ((romsize == 3 * MBIT) && (address - startaddress == 2 * MBIT))
  217. address += 2 * MBIT;
  218. else if ((romsize == 4 * MBIT) && (address - startaddress == 4 * MBIT))
  219. fseek (file, -2 * MBIT, SEEK_CUR);
  220. bytessent += bytesread;
  221. ucon64_gauge (starttime, bytessent, size);
  222. bytesleft -= bytesread;
  223. }
  224. // Games have to be aligned to a Mbit boundary.
  225. address = (address + MBIT - 1) & ~(MBIT - 1);
  226. game_no++;
  227. }
  228. while (multi_game ? (game_table[game_no * 0x20] && game_no < 31) : 0);
  229. fclose (file);
  230. return 0;
  231. }
  232. #endif // USE_PARALLEL