pce-pro.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. pce-pro.c - PCE-PRO flash card programmer support for uCON64
  3. Copyright (c) 2004 dbjh
  4. Based on Delphi source code by ToToTEK Multi Media. Information in that source
  5. code has been used with permission. However, ToToTEK Multi Media explicitly
  6. stated that the information in that source code may be freely distributed.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include "misc/parallel.h"
  26. #include "misc/itypes.h"
  27. #ifdef USE_ZLIB
  28. #include "misc/archive.h"
  29. #endif
  30. #include "misc/getopt2.h" // st_getopt2_t
  31. #include "misc/file.h"
  32. #include "misc/misc.h"
  33. #include "ucon64.h"
  34. #include "ucon64_misc.h"
  35. #include "tototek.h"
  36. #include "pce-pro.h"
  37. const st_getopt2_t pcepro_usage[] =
  38. {
  39. {
  40. NULL, 0, 0, 0,
  41. NULL, "PCE-PRO flash card programmer"/*"2004 ToToTEK Multi Media http://www.tototek.com"*/,
  42. NULL
  43. },
  44. #ifdef USE_PARALLEL
  45. {
  46. "xpce", 0, 0, UCON64_XPCE,
  47. NULL, "send/receive ROM to/from PCE-PRO flash card programmer\n" OPTION_LONG_S "port=PORT\n"
  48. "receives automatically (32 Mbits) when ROM does not exist",
  49. &ucon64_wf[WF_OBJ_PCE_DEFAULT_STOP_NO_SPLIT_NO_ROM]
  50. },
  51. #endif
  52. {NULL, 0, 0, 0, NULL, NULL, NULL}
  53. };
  54. #ifdef USE_PARALLEL
  55. static void eep_reset (void);
  56. static void write_rom_by_byte (int *addr, unsigned char *buf);
  57. static void write_rom_by_page (int *addr, unsigned char *buf);
  58. void
  59. eep_reset (void)
  60. {
  61. ttt_rom_enable ();
  62. ttt_write_mem (0x000000, 0xff); // reset EEP
  63. ttt_write_mem (0x200000, 0xff); // reset EEP
  64. ttt_rom_disable ();
  65. }
  66. void
  67. write_rom_by_byte (int *addr, unsigned char *buf)
  68. {
  69. int x;
  70. for (x = 0; x < 0x4000; x++)
  71. {
  72. ttt_write_byte_sharp (*addr, buf[*addr & 0x3fff]);
  73. (*addr)++;
  74. }
  75. }
  76. void
  77. write_rom_by_page (int *addr, unsigned char *buf)
  78. {
  79. int x;
  80. for (x = 0; x < 0x200; x++)
  81. {
  82. ttt_write_page_rom (*addr, buf);
  83. (*addr) += 0x20;
  84. }
  85. }
  86. int
  87. pce_read_rom (const char *filename, unsigned int parport, int size)
  88. {
  89. FILE *file;
  90. unsigned char buffer[0x100];
  91. int blocksleft, address = 0;
  92. time_t starttime;
  93. void (*read_block) (int, unsigned char *) = ttt_read_rom_w; // ttt_read_rom_b
  94. if ((file = fopen (filename, "wb")) == NULL)
  95. {
  96. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  97. exit (1);
  98. }
  99. ttt_init_io (parport);
  100. printf ("Receive: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  101. blocksleft = size >> 8;
  102. eep_reset ();
  103. ttt_rom_enable ();
  104. if (read_block == ttt_read_rom_w)
  105. ttt_set_ai_data (6, 0x94); // rst=1, wei=0(dis.), rdi=0(dis.), inc mode, rom_CS
  106. starttime = time (NULL);
  107. while (blocksleft-- > 0)
  108. {
  109. read_block (address, buffer); // 0x100 bytes read
  110. fwrite (buffer, 1, 0x100, file);
  111. address += 0x100;
  112. if ((address & 0x3fff) == 0)
  113. ucon64_gauge (starttime, address, size);
  114. }
  115. // original code doesn't call ttt_rom_disable() when byte-size function is
  116. // used (ttt_read_rom_b() calls it)
  117. if (read_block == ttt_read_rom_w)
  118. ttt_rom_disable ();
  119. fclose (file);
  120. ttt_deinit_io ();
  121. return 0;
  122. }
  123. int
  124. pce_write_rom (const char *filename, unsigned int parport)
  125. {
  126. FILE *file;
  127. unsigned char buffer[0x4000];
  128. int size, fsize, address = 0, bytesread, bytessend = 0;
  129. time_t starttime;
  130. void (*write_block) (int *, unsigned char *) = write_rom_by_page; // write_rom_by_byte
  131. (void) write_rom_by_byte;
  132. if ((file = fopen (filename, "rb")) == NULL)
  133. {
  134. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  135. exit (1);
  136. }
  137. ttt_init_io (parport);
  138. size = fsize = fsizeof (filename);
  139. if (fsize == 4 * MBIT)
  140. size += 2 * MBIT;
  141. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  142. eep_reset ();
  143. if (ttt_get_id () != 0xb0d0)
  144. {
  145. fputs ("ERROR: PCE-PRO flash card (programmer) not detected\n", stderr);
  146. fclose (file);
  147. ttt_deinit_io ();
  148. exit (1);
  149. }
  150. starttime = time (NULL);
  151. eep_reset ();
  152. while ((bytesread = fread (buffer, 1, 0x4000, file)))
  153. {
  154. if ((address & 0xffff) == 0)
  155. ttt_erase_block (address);
  156. write_block (&address, buffer);
  157. if ((fsize == 3 * MBIT) && (address == 2 * MBIT))
  158. address += 2 * MBIT;
  159. if ((fsize == 4 * MBIT) && (address == 4 * MBIT))
  160. fseek (file, 2 * MBIT, SEEK_SET);
  161. bytessend += bytesread;
  162. ucon64_gauge (starttime, bytessend, size);
  163. }
  164. fclose (file);
  165. ttt_deinit_io ();
  166. return 0;
  167. }
  168. #endif // USE_PARALLEL