msg.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. msg.c - Magic Super Griffin support for uCON64
  3. Copyright (c) 2003 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 "ucon64.h"
  23. #include "ucon64_misc.h"
  24. #include "backup/ffe.h"
  25. #include "backup/msg.h"
  26. #ifdef USE_PARALLEL
  27. static st_ucon64_obj_t msg_obj[] =
  28. {
  29. {UCON64_PCE, WF_DEFAULT | WF_STOP | WF_NO_SPLIT | WF_NO_ROM}
  30. };
  31. #endif
  32. const st_getopt2_t msg_usage[] =
  33. {
  34. {
  35. NULL, 0, 0, 0,
  36. NULL, "Magic Super Griffin/MSG"/*"1993/1994/1995/19XX Front Far East/FFE http://www.front.com.tw"*/,
  37. NULL
  38. },
  39. #ifdef USE_PARALLEL
  40. {
  41. "xmsg", 0, 0, UCON64_XMSG,
  42. NULL, "send/receive ROM to/from Magic Super Griffin/MSG; " OPTION_LONG_S "port=PORT\n"
  43. "receives automatically when ROM does not exist",
  44. &msg_obj[0]
  45. },
  46. #endif
  47. {NULL, 0, 0, 0, NULL, NULL, NULL}
  48. };
  49. #ifdef USE_PARALLEL
  50. #define BUFFERSIZE 8192 // don't change, only 8192 works!
  51. static void set_header (unsigned char *buffer);
  52. static int check (unsigned char *info_block, int index1, int index2, int size);
  53. #if BUFFERSIZE < 512
  54. #error receive_rom_info() expects BUFFERSIZE to be at least 512 bytes.
  55. #endif
  56. static void
  57. set_header (unsigned char *buffer)
  58. {
  59. unsigned short n;
  60. unsigned char m = 0, sizes[] = "\x10\x20\x30\x30\x40\x40\x60\x80";
  61. for (n = 0; n < 128; n++)
  62. {
  63. ffe_send_command (5, n, 0);
  64. buffer[n] = ffe_send_command1 (0xa0a0);
  65. wait2 (1);
  66. if (buffer[n] != 0xff)
  67. m = 1;
  68. }
  69. if (m == 0)
  70. { // no cartridge in Magic Super Griffin
  71. buffer[0] = 0;
  72. return;
  73. }
  74. m = 0;
  75. if (!check (buffer, 0, 0x40, 0x20))
  76. m |= 2;
  77. if (check (buffer, 0, 0x20, 0x20))
  78. {
  79. if (!check (buffer, 0, 0x10, 0x10))
  80. m |= 1;
  81. }
  82. else
  83. {
  84. m |= 4;
  85. if (!check (buffer, 0x40, 0x60, 0x20))
  86. m |= 1;
  87. }
  88. memset (buffer, 0, MSG_HEADER_LEN);
  89. buffer[0] = sizes[m];
  90. if (buffer[0] == 0x30)
  91. buffer[1] = 1;
  92. buffer[8] = 0xaa;
  93. buffer[9] = 0xbb;
  94. buffer[10] = 2;
  95. }
  96. static int
  97. check (unsigned char *info_block, int index1, int index2, int size)
  98. {
  99. int n;
  100. for (n = 0; n < size; n++)
  101. if (info_block[n + index1] != info_block[n + index2])
  102. return 0;
  103. return 1;
  104. }
  105. int
  106. msg_read_rom (const char *filename, unsigned short parport)
  107. {
  108. FILE *file;
  109. unsigned char *buffer, blocksleft, emu_mode_select;
  110. int size, bytesreceived = 0;
  111. time_t starttime;
  112. unsigned short blocksdone = 0;
  113. ffe_init_io (parport);
  114. if ((file = fopen (filename, "wb")) == NULL)
  115. {
  116. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  117. exit (1);
  118. }
  119. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  120. {
  121. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  122. exit (1);
  123. }
  124. set_header (buffer);
  125. if (buffer[0] == 0)
  126. {
  127. fprintf (stderr, "ERROR: There is no cartridge present in the Magic Super Griffin\n");
  128. fclose (file);
  129. remove (filename);
  130. exit (1);
  131. }
  132. blocksleft = buffer[0];
  133. size = buffer[0] * 8192;
  134. printf ("Receive: %d Bytes (%.4f Mb)\n", size, (float) size / MBIT);
  135. fwrite (buffer, 1, MSG_HEADER_LEN, file); // write header
  136. emu_mode_select = buffer[1];
  137. ffe_send_command (5, 0, 0);
  138. ffe_send_command0 (0xbff0, 0);
  139. printf ("Press q to abort\n\n");
  140. starttime = time (NULL);
  141. while (blocksleft > 0)
  142. {
  143. ffe_send_command (5, blocksdone, 0);
  144. if (emu_mode_select && blocksdone >= 32)
  145. ffe_send_command (5, blocksdone + 32, 0);
  146. ffe_receive_block (0xa000, buffer, BUFFERSIZE);
  147. // vgs aborts if the checksum doesn't match the data, we let the user decide
  148. blocksleft--;
  149. blocksdone++;
  150. fwrite (buffer, 1, BUFFERSIZE, file);
  151. bytesreceived += BUFFERSIZE;
  152. ucon64_gauge (starttime, bytesreceived, size);
  153. ffe_checkabort (2);
  154. }
  155. free (buffer);
  156. fclose (file);
  157. return 0;
  158. }
  159. int
  160. msg_write_rom (const char *filename, unsigned short parport)
  161. {
  162. FILE *file;
  163. unsigned char *buffer, emu_mode_select;
  164. int bytesread, bytessent = 0, size;
  165. time_t starttime;
  166. unsigned short blocksdone = 0;
  167. ffe_init_io (parport);
  168. if ((file = fopen (filename, "rb")) == NULL)
  169. {
  170. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  171. exit (1);
  172. }
  173. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  174. {
  175. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  176. exit (1);
  177. }
  178. size = ucon64.file_size - MSG_HEADER_LEN;
  179. printf ("Send: %d Bytes (%.4f Mb)\n", size, (float) size / MBIT);
  180. fread (buffer, 1, MSG_HEADER_LEN, file);
  181. emu_mode_select = buffer[1]; // this byte is needed later
  182. ffe_send_command0 (0xe008, 0);
  183. printf ("Press q to abort\n\n");
  184. starttime = time (NULL);
  185. while ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) != 0)
  186. {
  187. ffe_send_command (5, blocksdone, 0);
  188. ffe_send_block (0x8000, buffer, (unsigned short) bytesread);
  189. blocksdone++;
  190. bytessent += bytesread;
  191. ucon64_gauge (starttime, bytessent, size);
  192. ffe_checkabort (2);
  193. }
  194. if (emu_mode_select & 1)
  195. ffe_send_command (4, 0xff00, 0);
  196. else
  197. ffe_send_command (4, 0xff03, 0);
  198. free (buffer);
  199. fclose (file);
  200. return 0;
  201. }
  202. #endif // USE_PARALLEL