123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- /*
- cd64.c - CD64 support for uCON64
- Copyright (c) 2004 dbjh
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdarg.h>
- #include <stdlib.h>
- #include <ultra64/host/cd64lib.h>
- #include "misc/archive.h"
- #include "misc/misc.h"
- #include "misc/parallel.h"
- #include "ucon64.h"
- #include "ucon64_misc.h"
- #include "backup/cd64.h"
- #if defined USE_PARALLEL && defined USE_LIBCD64
- static st_ucon64_obj_t cd64_obj[] =
- {
- {UCON64_N64, WF_DEFAULT | WF_STOP | WF_NO_ROM},
- {UCON64_N64, WF_STOP | WF_NO_ROM},
- {UCON64_N64, WF_DEFAULT | WF_STOP},
- {UCON64_N64, WF_SWITCH}
- };
- #endif
- const st_getopt2_t cd64_usage[] =
- {
- {
- NULL, 0, 0, 0,
- NULL, "CD64"/*"19XX UFO http://www.cd64.com"*/,
- NULL
- },
- #if defined USE_PARALLEL && defined USE_LIBCD64
- {
- "xcd64", 0, 0, UCON64_XCD64,
- NULL, "send/receive ROM to/from CD64; " OPTION_LONG_S "port=PORT\n"
- "receives automatically (64 Mbits) when ROM does not exist",
- &cd64_obj[0]
- },
- {
- "xcd64c", 1, 0, UCON64_XCD64C,
- "N", "receive N Mbits of ROM from CD64; " OPTION_LONG_S "port=PORT",
- &cd64_obj[1]
- },
- {
- "xcd64b", 0, 0, UCON64_XCD64B,
- NULL, "send boot emu to CD64; " OPTION_LONG_S "port=PORT",
- &cd64_obj[2]
- },
- {
- "xcd64s", 0, 0, UCON64_XCD64S,
- NULL, "send/receive SRAM to/from CD64; " OPTION_LONG_S "port=PORT\n"
- "receives automatically when SRAM file does not exist",
- &cd64_obj[1]
- },
- {
- "xcd64f", 0, 0, UCON64_XCD64F,
- NULL, "send/receive flash RAM to/from CD64; " OPTION_LONG_S "port=PORT\n"
- "receives automatically when flash RAM file does not exist",
- &cd64_obj[1]
- },
- {
- "xcd64e", 0, 0, UCON64_XCD64E,
- NULL, "send/receive EEPROM data to/from CD64; " OPTION_LONG_S "port=PORT\n"
- "receives automatically when EEPROM file does not exist",
- &cd64_obj[1]
- },
- {
- "xcd64m", 1, 0, UCON64_XCD64M,
- "INDEX", "send/receive memory pack data to/from CD64; " OPTION_LONG_S "port=PORT\n"
- "INDEX is ignored for CD64 BIOS protocol\n"
- "receives automatically when memory pack file does not exist",
- &cd64_obj[1]
- },
- {
- "xcd64p", 1, 0, UCON64_XCD64P,
- "PROT", "use protocol PROT when communicating with CD64; " OPTION_LONG_S "port=PORT\n"
- "PROT=0 CD64 BIOS\n"
- "PROT=1 Ghemor\n"
- "PROT=2 UltraLink",
- &cd64_obj[3]
- },
- #endif // USE_PARALLEL && USE_LIBCD64
- {NULL, 0, 0, 0, NULL, NULL, NULL}
- };
- #if defined USE_PARALLEL && defined USE_LIBCD64
- static time_t cd64_starttime;
- static void
- cd64_progress (uint32_t current, uint32_t total)
- {
- ucon64_gauge (cd64_starttime, current, total);
- }
- static int
- cd64_notice_helper (FILE *file, const char *prefix, const char *format,
- va_list argptr)
- {
- int n_chars;
- fputs (prefix, file);
- n_chars = vfprintf (file, format, argptr);
- fputc ('\n', file);
- fflush (file);
- return n_chars;
- }
- static int
- cd64_notice (const char *format, ...)
- {
- va_list argptr;
- int n_chars;
- va_start (argptr, format);
- n_chars = cd64_notice_helper (stdout, "NOTE (libcd64): ", format, argptr);
- va_end (argptr);
- return n_chars;
- }
- static int
- cd64_notice2 (const char *format, ...)
- {
- va_list argptr;
- int n_chars;
- va_start (argptr, format);
- n_chars = cd64_notice_helper (stderr, "ERROR (libcd64): ", format, argptr);
- va_end (argptr);
- return n_chars;
- }
- static int
- fread_wrapper (void *io_id, void *buffer, uint32_t size)
- {
- return fread (buffer, 1, size, (FILE *) io_id);
- }
- static int
- fwrite_wrapper (void *io_id, void *buffer, uint32_t size)
- {
- return fwrite (buffer, 1, size, (FILE *) io_id);
- }
- static int32_t
- ftell_wrapper (void *io_id)
- {
- return (int32_t) ftell ((FILE *) io_id);
- }
- static int
- fseek_wrapper (void *io_id, int32_t offset, int whence)
- {
- return fseek ((FILE *) io_id, offset, whence);
- }
- static struct cd64_t *
- cd64_init (void)
- {
- struct cd64_t *cd64;
- #ifdef USE_PPDEV
- uint16_t port = strtol (&ucon64.parport_dev[strlen (ucon64.parport_dev) - 1], NULL, 10);
- method_t method = PPDEV;
- #else
- uint16_t port = ucon64.parport;
- method_t method = RAWIO;
- #endif
- int is_parallel = 1;
- if ((cd64 = (struct cd64_t *) calloc (1, sizeof (struct cd64_t))) == NULL)
- {
- fprintf (stderr, ucon64_msg[BUFFER_ERROR], sizeof (struct cd64_t));
- exit (1);
- }
- #ifndef USE_PPDEV
- if (ucon64.parport == (uint16_t) UCON64_UNKNOWN)
- {
- fputs ("ERROR: No port or invalid port specified\n"
- "TIP: Specify one with --port or in the configuration file\n", stderr);
- exit (1);
- }
- if (port >= 0x300 && port <= 0x330)
- is_parallel = 0;
- #endif
- cd64->notice_callback = cd64_notice;
- cd64->notice_callback2 = cd64_notice2;
- if (!cd64_create (cd64, method, port, (protocol_t) ucon64.io_mode, is_parallel))
- {
- fputs ("ERROR: Could not initialise libcd64\n", stderr);
- exit (1);
- }
- cd64->read_callback = fread_wrapper; // actually f*2(), if zlib
- cd64->write_callback = fwrite_wrapper; // support is enabled
- cd64->tell_callback = ftell_wrapper;
- cd64->seek_callback = fseek_wrapper;
- cd64->progress_callback = cd64_progress;
- strcpy (cd64->io_driver_dir, ucon64.configdir);
- // parport_print_info() displays a reasonable message (even if we're using a
- // comms link)
- parport_print_info ();
- if (!cd64->devopen (cd64))
- {
- fputs ("ERROR: Could not open I/O device for CD64\n", stderr);
- exit (1);
- }
- #if defined __unix__ && !defined __MSDOS__
- drop_privileges ();
- #endif
- return cd64;
- }
- int
- cd64_read_rom (const char *filename, int size)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "w+b")) == NULL) // cd64_download_cart() also
- { // reads from file
- fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_download_cart (cd64, file, size * MBIT, NULL);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_write_rom (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "rb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_upload_dram (cd64, file, ucon64.file_size, NULL, 1);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_write_bootemu (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "rb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_upload_bootemu (cd64, file, ucon64.file_size, NULL);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_read_sram (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "wb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_download_sram (cd64, file);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_write_sram (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "rb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_upload_sram (cd64, file);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_read_flashram (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "wb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_download_flashram (cd64, file);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_write_flashram (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "rb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_upload_flashram (cd64, file);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_read_eeprom (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "wb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_download_eeprom (cd64, file);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_write_eeprom (const char *filename)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "rb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
- exit (1);
- }
- cd64_starttime = time (NULL);
- cd64_upload_eeprom (cd64, file);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_read_mempack (const char *filename, int index)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "wb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
- exit (1);
- }
- if (ucon64.io_mode == CD64BIOS)
- index = -1;
- cd64_starttime = time (NULL);
- cd64_download_mempak (cd64, file, (int8_t) index);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- int
- cd64_write_mempack (const char *filename, int index)
- {
- FILE *file;
- struct cd64_t *cd64 = cd64_init ();
- if ((file = fopen (filename, "rb")) == NULL)
- {
- fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
- exit (1);
- }
- if (ucon64.io_mode == CD64BIOS)
- index = -1;
- cd64_starttime = time (NULL);
- cd64_upload_mempak (cd64, file, (int8_t) index);
- cd64->devclose (cd64);
- fclose (file);
- free (cd64);
- return 0;
- }
- #endif // USE_PARALLEL && USE_LIBCD64
|