mcd.c 3.6 KB

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