cd64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. cd64.c - CD64 support for uCON64
  3. Copyright (c) 2004 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 <stdarg.h>
  20. #include <stdlib.h>
  21. #include <ultra64/host/cd64lib.h>
  22. #include "misc/archive.h"
  23. #include "misc/misc.h"
  24. #include "misc/parallel.h"
  25. #include "ucon64.h"
  26. #include "ucon64_misc.h"
  27. #include "backup/cd64.h"
  28. #if defined USE_PARALLEL && defined USE_LIBCD64
  29. static st_ucon64_obj_t cd64_obj[] =
  30. {
  31. {UCON64_N64, WF_DEFAULT | WF_STOP | WF_NO_ROM},
  32. {UCON64_N64, WF_STOP | WF_NO_ROM},
  33. {UCON64_N64, WF_DEFAULT | WF_STOP},
  34. {UCON64_N64, WF_SWITCH}
  35. };
  36. #endif
  37. const st_getopt2_t cd64_usage[] =
  38. {
  39. {
  40. NULL, 0, 0, 0,
  41. NULL, "CD64"/*"19XX UFO http://www.cd64.com"*/,
  42. NULL
  43. },
  44. #if defined USE_PARALLEL && defined USE_LIBCD64
  45. {
  46. "xcd64", 0, 0, UCON64_XCD64,
  47. NULL, "send/receive ROM to/from CD64; " OPTION_LONG_S "port=PORT\n"
  48. "receives automatically (64 Mbits) when ROM does not exist",
  49. &cd64_obj[0]
  50. },
  51. {
  52. "xcd64c", 1, 0, UCON64_XCD64C,
  53. "N", "receive N Mbits of ROM from CD64; " OPTION_LONG_S "port=PORT",
  54. &cd64_obj[1]
  55. },
  56. {
  57. "xcd64b", 0, 0, UCON64_XCD64B,
  58. NULL, "send boot emu to CD64; " OPTION_LONG_S "port=PORT",
  59. &cd64_obj[2]
  60. },
  61. {
  62. "xcd64s", 0, 0, UCON64_XCD64S,
  63. NULL, "send/receive SRAM to/from CD64; " OPTION_LONG_S "port=PORT\n"
  64. "receives automatically when SRAM file does not exist",
  65. &cd64_obj[1]
  66. },
  67. {
  68. "xcd64f", 0, 0, UCON64_XCD64F,
  69. NULL, "send/receive flash RAM to/from CD64; " OPTION_LONG_S "port=PORT\n"
  70. "receives automatically when flash RAM file does not exist",
  71. &cd64_obj[1]
  72. },
  73. {
  74. "xcd64e", 0, 0, UCON64_XCD64E,
  75. NULL, "send/receive EEPROM data to/from CD64; " OPTION_LONG_S "port=PORT\n"
  76. "receives automatically when EEPROM file does not exist",
  77. &cd64_obj[1]
  78. },
  79. {
  80. "xcd64m", 1, 0, UCON64_XCD64M,
  81. "INDEX", "send/receive memory pack data to/from CD64; " OPTION_LONG_S "port=PORT\n"
  82. "INDEX is ignored for CD64 BIOS protocol\n"
  83. "receives automatically when memory pack file does not exist",
  84. &cd64_obj[1]
  85. },
  86. {
  87. "xcd64p", 1, 0, UCON64_XCD64P,
  88. "PROT", "use protocol PROT when communicating with CD64; " OPTION_LONG_S "port=PORT\n"
  89. "PROT=0 CD64 BIOS\n"
  90. "PROT=1 Ghemor\n"
  91. "PROT=2 UltraLink",
  92. &cd64_obj[3]
  93. },
  94. #endif // USE_PARALLEL && USE_LIBCD64
  95. {NULL, 0, 0, 0, NULL, NULL, NULL}
  96. };
  97. #if defined USE_PARALLEL && defined USE_LIBCD64
  98. static time_t cd64_starttime;
  99. static void
  100. cd64_progress (uint32_t current, uint32_t total)
  101. {
  102. ucon64_gauge (cd64_starttime, current, total);
  103. }
  104. static int
  105. cd64_notice_helper (FILE *file, const char *prefix, const char *format,
  106. va_list argptr)
  107. {
  108. int n_chars;
  109. fputs (prefix, file);
  110. n_chars = vfprintf (file, format, argptr);
  111. fputc ('\n', file);
  112. fflush (file);
  113. return n_chars;
  114. }
  115. static int
  116. cd64_notice (const char *format, ...)
  117. {
  118. va_list argptr;
  119. int n_chars;
  120. va_start (argptr, format);
  121. n_chars = cd64_notice_helper (stdout, "NOTE (libcd64): ", format, argptr);
  122. va_end (argptr);
  123. return n_chars;
  124. }
  125. static int
  126. cd64_notice2 (const char *format, ...)
  127. {
  128. va_list argptr;
  129. int n_chars;
  130. va_start (argptr, format);
  131. n_chars = cd64_notice_helper (stderr, "ERROR (libcd64): ", format, argptr);
  132. va_end (argptr);
  133. return n_chars;
  134. }
  135. static int
  136. fread_wrapper (void *io_id, void *buffer, uint32_t size)
  137. {
  138. return fread (buffer, 1, size, (FILE *) io_id);
  139. }
  140. static int
  141. fwrite_wrapper (void *io_id, void *buffer, uint32_t size)
  142. {
  143. return fwrite (buffer, 1, size, (FILE *) io_id);
  144. }
  145. static int32_t
  146. ftell_wrapper (void *io_id)
  147. {
  148. return (int32_t) ftell ((FILE *) io_id);
  149. }
  150. static int
  151. fseek_wrapper (void *io_id, int32_t offset, int whence)
  152. {
  153. return fseek ((FILE *) io_id, offset, whence);
  154. }
  155. static struct cd64_t *
  156. cd64_init (void)
  157. {
  158. struct cd64_t *cd64;
  159. #ifdef USE_PPDEV
  160. uint16_t port = strtol (&ucon64.parport_dev[strlen (ucon64.parport_dev) - 1], NULL, 10);
  161. method_t method = PPDEV;
  162. #else
  163. uint16_t port = ucon64.parport;
  164. method_t method = RAWIO;
  165. #endif
  166. int is_parallel = 1;
  167. if ((cd64 = (struct cd64_t *) calloc (1, sizeof (struct cd64_t))) == NULL)
  168. {
  169. fprintf (stderr, ucon64_msg[BUFFER_ERROR], sizeof (struct cd64_t));
  170. exit (1);
  171. }
  172. #ifndef USE_PPDEV
  173. if (ucon64.parport == (uint16_t) UCON64_UNKNOWN)
  174. {
  175. fputs ("ERROR: No port or invalid port specified\n"
  176. "TIP: Specify one with --port or in the configuration file\n", stderr);
  177. exit (1);
  178. }
  179. if (port >= 0x300 && port <= 0x330)
  180. is_parallel = 0;
  181. #endif
  182. cd64->notice_callback = cd64_notice;
  183. cd64->notice_callback2 = cd64_notice2;
  184. if (!cd64_create (cd64, method, port, (protocol_t) ucon64.io_mode, is_parallel))
  185. {
  186. fputs ("ERROR: Could not initialise libcd64\n", stderr);
  187. exit (1);
  188. }
  189. cd64->read_callback = fread_wrapper; // actually f*2(), if zlib
  190. cd64->write_callback = fwrite_wrapper; // support is enabled
  191. cd64->tell_callback = ftell_wrapper;
  192. cd64->seek_callback = fseek_wrapper;
  193. cd64->progress_callback = cd64_progress;
  194. strcpy (cd64->io_driver_dir, ucon64.configdir);
  195. // parport_print_info() displays a reasonable message (even if we're using a
  196. // comms link)
  197. parport_print_info ();
  198. if (!cd64->devopen (cd64))
  199. {
  200. fputs ("ERROR: Could not open I/O device for CD64\n", stderr);
  201. exit (1);
  202. }
  203. #if defined __unix__ && !defined __MSDOS__
  204. drop_privileges ();
  205. #endif
  206. return cd64;
  207. }
  208. int
  209. cd64_read_rom (const char *filename, int size)
  210. {
  211. FILE *file;
  212. struct cd64_t *cd64 = cd64_init ();
  213. if ((file = fopen (filename, "w+b")) == NULL) // cd64_download_cart() also
  214. { // reads from file
  215. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  216. exit (1);
  217. }
  218. cd64_starttime = time (NULL);
  219. cd64_download_cart (cd64, file, size * MBIT, NULL);
  220. cd64->devclose (cd64);
  221. fclose (file);
  222. free (cd64);
  223. return 0;
  224. }
  225. int
  226. cd64_write_rom (const char *filename)
  227. {
  228. FILE *file;
  229. struct cd64_t *cd64 = cd64_init ();
  230. if ((file = fopen (filename, "rb")) == NULL)
  231. {
  232. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  233. exit (1);
  234. }
  235. cd64_starttime = time (NULL);
  236. cd64_upload_dram (cd64, file, ucon64.file_size, NULL, 1);
  237. cd64->devclose (cd64);
  238. fclose (file);
  239. free (cd64);
  240. return 0;
  241. }
  242. int
  243. cd64_write_bootemu (const char *filename)
  244. {
  245. FILE *file;
  246. struct cd64_t *cd64 = cd64_init ();
  247. if ((file = fopen (filename, "rb")) == NULL)
  248. {
  249. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  250. exit (1);
  251. }
  252. cd64_starttime = time (NULL);
  253. cd64_upload_bootemu (cd64, file, ucon64.file_size, NULL);
  254. cd64->devclose (cd64);
  255. fclose (file);
  256. free (cd64);
  257. return 0;
  258. }
  259. int
  260. cd64_read_sram (const char *filename)
  261. {
  262. FILE *file;
  263. struct cd64_t *cd64 = cd64_init ();
  264. if ((file = fopen (filename, "wb")) == NULL)
  265. {
  266. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  267. exit (1);
  268. }
  269. cd64_starttime = time (NULL);
  270. cd64_download_sram (cd64, file);
  271. cd64->devclose (cd64);
  272. fclose (file);
  273. free (cd64);
  274. return 0;
  275. }
  276. int
  277. cd64_write_sram (const char *filename)
  278. {
  279. FILE *file;
  280. struct cd64_t *cd64 = cd64_init ();
  281. if ((file = fopen (filename, "rb")) == NULL)
  282. {
  283. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  284. exit (1);
  285. }
  286. cd64_starttime = time (NULL);
  287. cd64_upload_sram (cd64, file);
  288. cd64->devclose (cd64);
  289. fclose (file);
  290. free (cd64);
  291. return 0;
  292. }
  293. int
  294. cd64_read_flashram (const char *filename)
  295. {
  296. FILE *file;
  297. struct cd64_t *cd64 = cd64_init ();
  298. if ((file = fopen (filename, "wb")) == NULL)
  299. {
  300. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  301. exit (1);
  302. }
  303. cd64_starttime = time (NULL);
  304. cd64_download_flashram (cd64, file);
  305. cd64->devclose (cd64);
  306. fclose (file);
  307. free (cd64);
  308. return 0;
  309. }
  310. int
  311. cd64_write_flashram (const char *filename)
  312. {
  313. FILE *file;
  314. struct cd64_t *cd64 = cd64_init ();
  315. if ((file = fopen (filename, "rb")) == NULL)
  316. {
  317. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  318. exit (1);
  319. }
  320. cd64_starttime = time (NULL);
  321. cd64_upload_flashram (cd64, file);
  322. cd64->devclose (cd64);
  323. fclose (file);
  324. free (cd64);
  325. return 0;
  326. }
  327. int
  328. cd64_read_eeprom (const char *filename)
  329. {
  330. FILE *file;
  331. struct cd64_t *cd64 = cd64_init ();
  332. if ((file = fopen (filename, "wb")) == NULL)
  333. {
  334. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  335. exit (1);
  336. }
  337. cd64_starttime = time (NULL);
  338. cd64_download_eeprom (cd64, file);
  339. cd64->devclose (cd64);
  340. fclose (file);
  341. free (cd64);
  342. return 0;
  343. }
  344. int
  345. cd64_write_eeprom (const char *filename)
  346. {
  347. FILE *file;
  348. struct cd64_t *cd64 = cd64_init ();
  349. if ((file = fopen (filename, "rb")) == NULL)
  350. {
  351. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  352. exit (1);
  353. }
  354. cd64_starttime = time (NULL);
  355. cd64_upload_eeprom (cd64, file);
  356. cd64->devclose (cd64);
  357. fclose (file);
  358. free (cd64);
  359. return 0;
  360. }
  361. int
  362. cd64_read_mempack (const char *filename, int index)
  363. {
  364. FILE *file;
  365. struct cd64_t *cd64 = cd64_init ();
  366. if ((file = fopen (filename, "wb")) == NULL)
  367. {
  368. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  369. exit (1);
  370. }
  371. if (ucon64.io_mode == CD64BIOS)
  372. index = -1;
  373. cd64_starttime = time (NULL);
  374. cd64_download_mempak (cd64, file, (int8_t) index);
  375. cd64->devclose (cd64);
  376. fclose (file);
  377. free (cd64);
  378. return 0;
  379. }
  380. int
  381. cd64_write_mempack (const char *filename, int index)
  382. {
  383. FILE *file;
  384. struct cd64_t *cd64 = cd64_init ();
  385. if ((file = fopen (filename, "rb")) == NULL)
  386. {
  387. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  388. exit (1);
  389. }
  390. if (ucon64.io_mode == CD64BIOS)
  391. index = -1;
  392. cd64_starttime = time (NULL);
  393. cd64_upload_mempak (cd64, file, (int8_t) index);
  394. cd64->devclose (cd64);
  395. fclose (file);
  396. free (cd64);
  397. return 0;
  398. }
  399. #endif // USE_PARALLEL && USE_LIBCD64