ffe.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. ffe.c - General Front Far East copier routines for uCON64
  3. Copyright (c) 2002 - 2004 dbjh
  4. Copyright (c) 2003 JohnDie
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <stdlib.h>
  21. #include "misc/misc.h"
  22. #include "misc/parallel.h"
  23. #include "misc/term.h"
  24. #include "ucon64.h"
  25. #include "backup/ffe.h"
  26. #ifdef USE_PARALLEL
  27. #define N_TRY_MAX 65536 // # times to test if copier ready
  28. static void ffe_sendb (unsigned char byte);
  29. static unsigned char ffe_wait_while_busy (void);
  30. static unsigned short ffe_port;
  31. void
  32. ffe_init_io (unsigned short port)
  33. /*
  34. - sets global `ffe_port'. Then the send/receive functions don't need to pass
  35. `ffe_port' all the way to ffe_sendb()/ffe_receiveb().
  36. - calls init_conio(). Necessary for kbhit() and DOS-like behaviour of getch().
  37. */
  38. {
  39. ffe_port = port;
  40. #if 0 // we want to support non-standard parallel port addresses
  41. if (ffe_port != 0x3bc && ffe_port != 0x378 && ffe_port != 0x278)
  42. {
  43. fprintf (stderr, "ERROR: PORT must be 0x3bc, 0x378 or 0x278\n");
  44. exit (1);
  45. }
  46. #endif
  47. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  48. init_conio ();
  49. #endif
  50. if (register_func (ffe_deinit_io) == -1)
  51. {
  52. fputs ("ERROR: Could not register function with register_func()\n", stderr);
  53. exit (1);
  54. }
  55. parport_print_info ();
  56. }
  57. void
  58. ffe_deinit_io (void)
  59. {
  60. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  61. deinit_conio ();
  62. #endif
  63. }
  64. void
  65. ffe_send_block (unsigned short address, unsigned char *buffer, unsigned short len)
  66. {
  67. int n;
  68. unsigned char checksum = 0x81;
  69. ffe_send_command (0, address, len);
  70. for (n = 0; n < len; n++)
  71. {
  72. ffe_sendb (buffer[n]);
  73. checksum ^= buffer[n];
  74. }
  75. ffe_sendb (checksum);
  76. }
  77. void
  78. ffe_send_block2 (unsigned short address, unsigned char *buffer, unsigned short len)
  79. {
  80. int n;
  81. unsigned char checksum = 0x81;
  82. ffe_send_command (2, address, len);
  83. for (n = 0; n < len; n++)
  84. {
  85. ffe_sendb (buffer[n]);
  86. checksum ^= buffer[n];
  87. }
  88. ffe_sendb (checksum);
  89. }
  90. void
  91. ffe_send_command0 (unsigned short address, unsigned char byte)
  92. // command 0 for 1 byte
  93. {
  94. ffe_send_command (0, address, 1);
  95. ffe_sendb (byte);
  96. ffe_sendb (0x81 ^ byte);
  97. }
  98. unsigned char
  99. ffe_send_command1 (unsigned short address)
  100. // command 1 for 1 byte
  101. {
  102. unsigned char byte;
  103. ffe_send_command (1, address, 1);
  104. byte = ffe_receiveb ();
  105. if ((0x81 ^ byte) != ffe_receiveb ())
  106. puts ("received data is corrupt");
  107. return byte;
  108. }
  109. void
  110. ffe_send_command (unsigned char command_code, unsigned short a, unsigned short l)
  111. {
  112. ffe_sendb (0xd5);
  113. ffe_sendb (0xaa);
  114. ffe_sendb (0x96);
  115. ffe_sendb (command_code);
  116. ffe_sendb ((unsigned char) a); // low byte
  117. ffe_sendb ((unsigned char) (a >> 8)); // high byte
  118. ffe_sendb ((unsigned char) l); // low byte
  119. ffe_sendb ((unsigned char) (l >> 8)); // high byte
  120. ffe_sendb ((unsigned char) (0x81 ^ command_code ^ a ^ (a >> 8) ^ l ^ (l >> 8))); // checksum
  121. }
  122. void
  123. ffe_sendb (unsigned char byte)
  124. {
  125. ffe_wait_for_ready ();
  126. outportb (ffe_port + PARPORT_DATA, byte);
  127. outportb (ffe_port + PARPORT_CONTROL,
  128. inportb (ffe_port + PARPORT_CONTROL) ^ PARPORT_STROBE); // invert strobe
  129. ffe_wait_for_ready (); // necessary if followed by ffe_receiveb()
  130. }
  131. void
  132. ffe_receive_block (unsigned short address, unsigned char *buffer, unsigned short len)
  133. {
  134. volatile int n;
  135. int n_try = 0;
  136. unsigned char checksum1, checksum2;
  137. do
  138. {
  139. checksum1 = 0x81;
  140. ffe_send_command (1, address, len);
  141. for (n = 0; n < len; n++)
  142. {
  143. buffer[n] = ffe_receiveb ();
  144. checksum1 ^= buffer[n];
  145. }
  146. checksum2 = ffe_receiveb ();
  147. for (n = 0; n < 65536; n++) // a delay is necessary here
  148. ;
  149. n_try++;
  150. }
  151. while ((checksum1 != checksum2) && (n_try < N_TRY_MAX));
  152. if (checksum1 != checksum2)
  153. puts ("\nreceived data is corrupt");
  154. }
  155. void
  156. ffe_receive_block2 (unsigned short address, unsigned char *buffer, unsigned short len)
  157. {
  158. volatile int n;
  159. int n_try = 0;
  160. unsigned char checksum1, checksum2;
  161. do
  162. {
  163. checksum1 = 0x81;
  164. ffe_send_command (3, address, len);
  165. for (n = 0; n < len; n++)
  166. {
  167. buffer[n] = ffe_receiveb ();
  168. checksum1 ^= buffer[n];
  169. }
  170. checksum2 = ffe_receiveb ();
  171. for (n = 0; n < 65536; n++) // a delay is necessary here
  172. ;
  173. n_try++;
  174. }
  175. while ((checksum1 != checksum2) && (n_try < N_TRY_MAX));
  176. if (checksum1 != checksum2)
  177. puts ("\nreceived data is corrupt");
  178. }
  179. unsigned char
  180. ffe_receiveb (void)
  181. {
  182. unsigned char byte;
  183. byte = (ffe_wait_while_busy () & PARPORT_INPUT_MASK) >> 3; // receive low nibble
  184. outportb (ffe_port + PARPORT_CONTROL,
  185. inportb (ffe_port + PARPORT_CONTROL) ^ PARPORT_STROBE); // invert strobe
  186. byte |= (ffe_wait_while_busy () & PARPORT_INPUT_MASK) << 1; // receive high nibble
  187. outportb (ffe_port + PARPORT_CONTROL,
  188. inportb (ffe_port + PARPORT_CONTROL) ^ PARPORT_STROBE); // invert strobe
  189. return byte;
  190. }
  191. unsigned char
  192. ffe_wait_while_busy (void)
  193. {
  194. unsigned char input;
  195. int n_try = 0;
  196. do
  197. {
  198. input = inportb (ffe_port + PARPORT_STATUS);
  199. n_try++;
  200. }
  201. while (input & PARPORT_IBUSY && n_try < N_TRY_MAX);
  202. #if 0
  203. /*
  204. VGS doesn't check for this, and it seems to happen quite regularly, so it
  205. is currently commented out
  206. */
  207. if (n_try >= N_TRY_MAX)
  208. {
  209. fputs ("ERROR: The copier is not ready\n" // yes, "ready" :-)
  210. " Turn it off for a few seconds then turn it on and try again\n",
  211. stderr);
  212. exit (1);
  213. }
  214. #endif
  215. // read port again to let data settle down and to delay a little bit - JohnDie
  216. return inportb (ffe_port + PARPORT_STATUS);
  217. }
  218. void
  219. ffe_wait_for_ready (void)
  220. {
  221. unsigned char input;
  222. int n_try = 0;
  223. do
  224. {
  225. input = inportb (ffe_port + PARPORT_STATUS);
  226. n_try++;
  227. }
  228. while (!(input & PARPORT_IBUSY) && n_try < N_TRY_MAX);
  229. #if 0
  230. if (n_try >= N_TRY_MAX)
  231. {
  232. fputs ("ERROR: The copier is not ready\n"
  233. " Turn it off for a few seconds then turn it on and try again\n",
  234. stderr);
  235. exit (1);
  236. }
  237. #endif
  238. }
  239. void
  240. ffe_checkabort (int status)
  241. {
  242. if ((!ucon64.frontend ? kbhit () : 0) && getch () == 'q')
  243. {
  244. // ffe_send_command (5, 0, 0); // VGS: when sending/receiving a SNES ROM
  245. puts ("\nProgram aborted");
  246. exit (status);
  247. }
  248. }
  249. #endif // USE_PARALLEL