cd64.c 11 KB

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