mcd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. mcd.c - Mike Pavone's Genesis/Sega CD transfer cable 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 <stdlib.h>
  20. #include "misc/archive.h"
  21. #include "misc/misc.h"
  22. #include "misc/parallel.h"
  23. #include "misc/term.h"
  24. #include "ucon64.h"
  25. #include "ucon64_misc.h"
  26. #include "backup/mcd.h"
  27. #ifdef USE_PARALLEL
  28. static st_ucon64_obj_t mcd_obj[] =
  29. {
  30. {UCON64_GEN, WF_DEFAULT | WF_STOP | WF_NO_SPLIT | WF_NO_ROM}
  31. };
  32. #endif
  33. const st_getopt2_t mcd_usage[] =
  34. {
  35. {
  36. NULL, 0, 0, 0,
  37. NULL, "Mike Pavone's Genesis/Sega CD transfer cable",
  38. NULL
  39. },
  40. #ifdef USE_PARALLEL
  41. {
  42. "xmcd", 0, 0, UCON64_XMCD,
  43. NULL, "receive ROM from Genesis/Sega CD; " OPTION_LONG_S "port=PORT",
  44. &mcd_obj[0]
  45. },
  46. #endif
  47. {NULL, 0, 0, 0, NULL, NULL, NULL}
  48. };
  49. #ifdef USE_PARALLEL
  50. #define BUFFERSIZE 8192
  51. static void
  52. checkabort (int status)
  53. {
  54. if ((!ucon64.frontend ? kbhit () : 0) && getch () == 'q')
  55. {
  56. puts ("\nProgram aborted");
  57. exit (status);
  58. }
  59. }
  60. static void
  61. read_block (unsigned char *buffer, int size, unsigned short parport)
  62. {
  63. int i;
  64. for (i = 0; i < size; i++)
  65. {
  66. while (inportb (parport + PARPORT_STATUS) & 0x08)
  67. ;
  68. outportb (parport + PARPORT_CONTROL, 0xa2);
  69. while (!(inportb (parport + PARPORT_STATUS) & 0x08))
  70. ;
  71. buffer[i] = inportb (parport + PARPORT_DATA) & 0x0f;
  72. outportb (parport + PARPORT_CONTROL, 0xa0);
  73. while (inportb (parport + PARPORT_STATUS) & 0x08)
  74. ;
  75. outportb (parport + PARPORT_CONTROL, 0xa2);
  76. while (!(inportb (parport + PARPORT_STATUS) & 0x08))
  77. ;
  78. buffer[i] |= (inportb (parport + PARPORT_DATA) & 0x0f) << 4;
  79. outportb (parport + PARPORT_CONTROL, 0xa0);
  80. }
  81. }
  82. int
  83. mcd_read_rom (const char *filename, unsigned short parport)
  84. {
  85. FILE *file;
  86. unsigned char buffer[BUFFERSIZE];
  87. int n_bytes = 0, size;
  88. time_t starttime;
  89. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  90. init_conio ();
  91. if (register_func (deinit_conio) == -1)
  92. {
  93. fputs ("ERROR: Could not register function with register_func()\n", stderr);
  94. exit (1);
  95. }
  96. #endif
  97. parport_print_info ();
  98. if ((file = fopen (filename, "wb")) == NULL)
  99. {
  100. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  101. exit (1);
  102. }
  103. read_block (buffer, 1, parport);
  104. size = (buffer[0] + 1) * 64 * 1024;
  105. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  106. puts ("Press q to abort\n");
  107. starttime = time (NULL);
  108. while (n_bytes < size)
  109. {
  110. read_block (buffer, BUFFERSIZE, parport);
  111. fwrite (buffer, 1, BUFFERSIZE, file);
  112. n_bytes += BUFFERSIZE;
  113. ucon64_gauge (starttime, n_bytes, size);
  114. checkabort (2);
  115. }
  116. fclose (file);
  117. return 0;
  118. }
  119. #endif // USE_PARALLEL