gd.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /*
  2. gd.c - Game Doctor support for uCON64
  3. Copyright (c) 2002 - 2003 John Weidman
  4. Copyright (c) 2002 - 2006 dbjh
  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 <ctype.h>
  21. #include <stdlib.h>
  22. #include "misc/archive.h"
  23. #include "misc/file.h"
  24. #include "misc/misc.h"
  25. #include "misc/parallel.h"
  26. #include "misc/string.h"
  27. #include "misc/term.h"
  28. #include "ucon64_misc.h"
  29. #include "console/snes.h" // for snes_make_gd_names() &
  30. #include "backup/gd.h" // snes_get_snes_hirom()
  31. #ifdef USE_PARALLEL
  32. static st_ucon64_obj_t gd_obj[] =
  33. {
  34. {UCON64_SNES, WF_DEFAULT | WF_STOP | WF_NO_ROM},
  35. {UCON64_SNES, WF_STOP | WF_NO_ROM}
  36. };
  37. #endif
  38. const st_getopt2_t gd_usage[] =
  39. {
  40. {
  41. NULL, 0, 0, 0,
  42. NULL, "Game Doctor SF3(SF6/SF7)/Professor SF(SF II)"/*"19XX Bung Enterprises Ltd http://www.bung.com.hk"*/,
  43. NULL
  44. },
  45. #ifdef USE_PARALLEL
  46. {
  47. "xgd3", 0, 0, UCON64_XGD3, // supports split files
  48. NULL, "send ROM to Game Doctor SF3/SF6/SF7; " OPTION_LONG_S "port=PORT\n"
  49. "this option uses the Game Doctor SF3 protocol",
  50. &gd_obj[0]
  51. },
  52. {
  53. "xgd6", 0, 0, UCON64_XGD6,
  54. #if 1 // dumping is not yet supported
  55. NULL, "send ROM to Game Doctor SF6/SF7; " OPTION_LONG_S "port=PORT\n" // supports split files
  56. #else
  57. NULL, "send/receive ROM to/from Game Doctor SF6/SF7; " OPTION_LONG_S "port=PORT\n"
  58. "receives automatically when ROM does not exist\n"
  59. #endif
  60. "this option uses the Game Doctor SF6 protocol",
  61. &gd_obj[0]
  62. },
  63. {
  64. "xgd3s", 0, 0, UCON64_XGD3S,
  65. NULL, "send SRAM to Game Doctor SF3/SF6/SF7; " OPTION_LONG_S "port=PORT",
  66. &gd_obj[1]
  67. },
  68. // --xgd3r should remain hidden until receiving works
  69. {
  70. "xgd3r", 0, 0, UCON64_XGD3R,
  71. NULL, NULL,
  72. &gd_obj[1]
  73. },
  74. {
  75. "xgd6s", 0, 0, UCON64_XGD6S,
  76. NULL, "send/receive SRAM to/from Game Doctor SF6/SF7; " OPTION_LONG_S "port=PORT\n"
  77. "receives automatically when SRAM does not exist",
  78. &gd_obj[1]
  79. },
  80. {
  81. "xgd6r", 0, 0, UCON64_XGD6R,
  82. NULL, "send/receive saver (RTS) data to/from Game Doctor SF6/SF7;\n" OPTION_LONG_S "port=PORT\n"
  83. "receives automatically when saver file does not exist",
  84. &gd_obj[1]
  85. },
  86. #endif // USE_PARALLEL
  87. {NULL, 0, 0, 0, NULL, NULL, NULL}
  88. };
  89. #ifdef USE_PARALLEL
  90. #define BUFFERSIZE 8192
  91. #define GD_OK 0
  92. #define GD_ERROR 1
  93. #define GD3_PROLOG_STRING "DSF3"
  94. #define GD6_READ_PROLOG_STRING "GD6R" // GD reading, PC writing
  95. #define GD6_WRITE_PROLOG_STRING "GD6W" // GD writing, PC reading
  96. #define GD6_TIMEOUT_ATTEMPTS 0x4000
  97. #define GD6_RX_SYNC_TIMEOUT_ATTEMPTS 0x2000
  98. #define GD6_SYNC_RETRIES 16
  99. #ifdef _MSC_VER
  100. // Visual C++ doesn't allow inline in C source code
  101. #define inline __inline
  102. #endif
  103. static void init_io (unsigned short port);
  104. static void deinit_io (void);
  105. static void io_error (void);
  106. static void gd_checkabort (int status);
  107. static void remove_destfile (void);
  108. static int gd3_send_prolog_byte (unsigned char data);
  109. static int gd3_send_prolog_bytes (unsigned char *data, int len);
  110. static void gd3_send_byte (unsigned char data);
  111. static int gd3_send_bytes (unsigned char *data, int len);
  112. static int gd6_sync_hardware (void);
  113. static int gd6_sync_receive_start (void);
  114. static inline int gd6_send_byte_helper (unsigned char data, unsigned int timeout);
  115. static int gd6_send_prolog_byte (unsigned char data);
  116. static int gd6_send_prolog_bytes (unsigned char *data, int len);
  117. static int gd6_send_bytes (unsigned char *data, int len);
  118. static int gd6_receive_bytes (unsigned char *buffer, int len);
  119. static int gd_send_unit_prolog (int header, unsigned size);
  120. static int gd_write_rom (const char *filename, unsigned short parport,
  121. st_ucon64_nfo_t *rominfo, const char *prolog_str);
  122. static int gd_write_sram (const char *filename, unsigned short parport,
  123. const char *prolog_str);
  124. static int gd_write_saver (const char *filename, unsigned short parport,
  125. const char *prolog_str);
  126. typedef struct st_gd3_memory_unit
  127. {
  128. char name[12]; // Exact size is 11 chars but I'll
  129. // unsigned char *data; // add one extra for string terminator
  130. unsigned int size; // Usually either 0x100000 or 0x80000
  131. } st_gd3_memory_unit_t;
  132. static int (*gd_send_prolog_byte) (unsigned char data);
  133. static int (*gd_send_prolog_bytes) (unsigned char *data, int len);
  134. static int (*gd_send_bytes) (unsigned char *data, int len);
  135. static st_gd3_memory_unit_t gd3_dram_unit[GD3_MAX_UNITS];
  136. static unsigned short gd_port;
  137. static int gd_bytessent, gd_fsize, gd_name_i = 0;
  138. static time_t gd_starttime;
  139. static char **gd_names;
  140. static unsigned char gd6_send_toggle;
  141. static const char *gd_destfname = NULL;
  142. static FILE *gd_destfile;
  143. static void
  144. init_io (unsigned short port)
  145. /*
  146. - sets global `gd_port'. Then the send/receive functions don't need to pass
  147. `gd_port' to all the I/O functions.
  148. - calls init_conio(). Necessary for kbhit() and DOS-like behaviour of getch().
  149. */
  150. {
  151. gd_port = port;
  152. #if 0 // we want to support non-standard parallel port addresses
  153. if (gd_port != 0x3bc && gd_port != 0x378 && gd_port != 0x278)
  154. {
  155. fputs ("ERROR: PORT must be 0x3bc, 0x378 or 0x278\n", stderr);
  156. exit (1);
  157. }
  158. #endif
  159. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  160. init_conio ();
  161. #endif
  162. if (register_func (deinit_io) == -1)
  163. {
  164. fputs ("ERROR: Could not register function with register_func()\n", stderr);
  165. exit (1);
  166. }
  167. parport_print_info ();
  168. }
  169. static void
  170. deinit_io (void)
  171. {
  172. // Put possible transfer cleanup stuff here
  173. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  174. deinit_conio ();
  175. #endif
  176. }
  177. static void
  178. io_error (void)
  179. // This function could be changed to take a string argument that describes the
  180. // error. Or take an integer code that we can interpret here.
  181. {
  182. fflush (stdout);
  183. fputs ("ERROR: Communication with Game Doctor failed\n", stderr);
  184. fflush (stderr);
  185. // calling fflush() seems to be necessary in Msys in order to make the error
  186. // message be displayed before the "Removing: <filename>" message
  187. exit (1);
  188. }
  189. static void
  190. gd_checkabort (int status)
  191. {
  192. if ((!ucon64.frontend ? kbhit () : 0) && getch () == 'q')
  193. {
  194. puts ("\nProgram aborted");
  195. exit (status);
  196. }
  197. }
  198. static void
  199. remove_destfile (void)
  200. {
  201. if (gd_destfname)
  202. {
  203. printf ("Removing: %s\n", gd_destfname);
  204. fclose (gd_destfile);
  205. remove (gd_destfname);
  206. gd_destfname = NULL;
  207. }
  208. }
  209. static int
  210. gd3_send_prolog_byte (unsigned char data)
  211. /*
  212. Prolog specific data output routine
  213. We could probably get away with using the general routine but the
  214. transfer program I (JW) traced to analyze the protocol did this for
  215. the bytes used to set up the transfer so here it is.
  216. */
  217. {
  218. // Wait until SF3 is not busy
  219. do
  220. {
  221. if ((inportb (gd_port + PARPORT_STATUS) & 0x08) == 0)
  222. return GD_ERROR;
  223. }
  224. while ((inportb (gd_port + PARPORT_STATUS) & 0x80) == 0);
  225. outportb (gd_port, data); // Set data
  226. outportb (gd_port + PARPORT_CONTROL, 5); // Clock data out to SF3
  227. inportb (gd_port + PARPORT_CONTROL); // Let data "settle down"
  228. outportb (gd_port + PARPORT_CONTROL, 4);
  229. return GD_OK;
  230. }
  231. static int
  232. gd3_send_prolog_bytes (unsigned char *data, int len)
  233. {
  234. int i;
  235. for (i = 0; i < len; i++)
  236. if (gd3_send_prolog_byte (data[i]) == GD_ERROR)
  237. return GD_ERROR;
  238. return GD_OK;
  239. }
  240. static int
  241. gd_send_unit_prolog (int header, unsigned size)
  242. {
  243. if (gd_send_prolog_byte (0x00) == GD_ERROR)
  244. return GD_ERROR;
  245. if (gd_send_prolog_byte ((unsigned char) ((header != 0) ? 0x02 : 0x00)) == GD_ERROR)
  246. return GD_ERROR;
  247. if (gd_send_prolog_byte ((unsigned char) (size >> 16)) == GD_ERROR) // 0x10 = 8 Mbit
  248. return GD_ERROR;
  249. if (gd_send_prolog_byte (0x00) == GD_ERROR)
  250. return GD_ERROR;
  251. return GD_OK;
  252. }
  253. static void
  254. gd3_send_byte (unsigned char data)
  255. /*
  256. General data output routine
  257. Use this routine for sending ROM data bytes to the Game Doctor SF3 (SF6/SF7
  258. too).
  259. */
  260. {
  261. // Wait until SF3 is not busy
  262. while ((inportb (gd_port + PARPORT_STATUS) & 0x80) == 0)
  263. ;
  264. outportb (gd_port, data); // Set data
  265. outportb (gd_port + PARPORT_CONTROL, 5); // Clock data out to SF3
  266. inportb (gd_port + PARPORT_CONTROL); // Let data "settle down"
  267. outportb (gd_port + PARPORT_CONTROL, 4);
  268. }
  269. static int
  270. gd3_send_bytes (unsigned char *data, int len)
  271. {
  272. int i;
  273. for (i = 0; i < len; i++)
  274. {
  275. gd3_send_byte (data[i]);
  276. gd_bytessent++;
  277. if ((gd_bytessent - GD_HEADER_LEN) % 8192 == 0)
  278. {
  279. ucon64_gauge (gd_starttime, gd_bytessent, gd_fsize);
  280. gd_checkabort (2); // 2 to return something other than 1
  281. }
  282. }
  283. return GD_OK;
  284. }
  285. static int
  286. gd6_sync_hardware (void)
  287. // Sets the SF7 up for an SF6/SF7 protocol transfer
  288. {
  289. int timeout, retries;
  290. volatile int delay;
  291. for (retries = GD6_SYNC_RETRIES; retries > 0; retries--)
  292. {
  293. timeout = GD6_TIMEOUT_ATTEMPTS;
  294. outportb (gd_port + PARPORT_CONTROL, 4);
  295. outportb (gd_port, 0);
  296. outportb (gd_port + PARPORT_CONTROL, 4);
  297. for (delay = 0x1000; delay > 0; delay--) // A delay may not be necessary here
  298. ;
  299. outportb (gd_port, 0xaa);
  300. outportb (gd_port + PARPORT_CONTROL, 0);
  301. while ((inportb (gd_port + PARPORT_CONTROL) & 0x08) == 0)
  302. if (--timeout == 0)
  303. break;
  304. if (timeout == 0)
  305. continue;
  306. outportb (gd_port + PARPORT_CONTROL, 4);
  307. while ((inportb (gd_port + PARPORT_CONTROL) & 0x08) != 0)
  308. if (--timeout == 0)
  309. break;
  310. if (timeout == 0)
  311. continue;
  312. outportb (gd_port, 0x55);
  313. outportb (gd_port + PARPORT_CONTROL, 0);
  314. while ((inportb (gd_port + PARPORT_CONTROL) & 0x08) == 0)
  315. if (--timeout == 0)
  316. break;
  317. if (timeout == 0)
  318. continue;
  319. outportb (gd_port + PARPORT_CONTROL, 4);
  320. while ((inportb (gd_port + PARPORT_CONTROL) & 0x08) != 0)
  321. if (--timeout == 0)
  322. break;
  323. if (timeout == 0)
  324. continue;
  325. return GD_OK;
  326. }
  327. return GD_ERROR;
  328. }
  329. static int
  330. gd6_sync_receive_start (void)
  331. // Sync with the start of the received data
  332. {
  333. int timeout = GD6_RX_SYNC_TIMEOUT_ATTEMPTS;
  334. for (;;)
  335. {
  336. if (((inportb (gd_port + PARPORT_CONTROL) & 0x03) == 0x03) ||
  337. ((inportb (gd_port + PARPORT_CONTROL) & 0x03) == 0))
  338. break;
  339. if (--timeout == 0)
  340. return GD_ERROR;
  341. }
  342. outportb (gd_port, 0);
  343. timeout = GD6_RX_SYNC_TIMEOUT_ATTEMPTS;
  344. while ((inportb (gd_port + PARPORT_STATUS) & 0x80) != 0)
  345. if (--timeout == 0)
  346. return GD_ERROR;
  347. return GD_OK;
  348. }
  349. static inline int
  350. gd6_send_byte_helper (unsigned char data, unsigned int timeout)
  351. {
  352. while ((inportb (gd_port + PARPORT_CONTROL) & 0x02) != gd6_send_toggle)
  353. if (--timeout == 0)
  354. return GD_ERROR;
  355. gd6_send_toggle = ~gd6_send_toggle & 0x02;
  356. outportb (gd_port, data);
  357. outportb (gd_port + PARPORT_CONTROL, 4 | (gd6_send_toggle >> 1));
  358. return GD_OK;
  359. }
  360. static int
  361. gd6_send_prolog_byte (unsigned char data)
  362. {
  363. unsigned int timeout = 0x100000;
  364. gd6_send_toggle = (inportb (gd_port + PARPORT_CONTROL) & 0x01) << 1;
  365. return gd6_send_byte_helper (data, timeout);
  366. }
  367. static int
  368. gd6_send_prolog_bytes (unsigned char *data, int len)
  369. {
  370. int i;
  371. unsigned int timeout = 0x1e00000;
  372. gd6_send_toggle = (inportb (gd_port + PARPORT_CONTROL) & 0x01) << 1;
  373. for (i = 0; i < len; i++)
  374. if (gd6_send_byte_helper (data[i], timeout) != GD_OK)
  375. return GD_ERROR;
  376. return GD_OK;
  377. }
  378. static int
  379. gd6_send_bytes (unsigned char *data, int len)
  380. {
  381. int i;
  382. unsigned int timeout = 0x1e0000;
  383. gd6_send_toggle = (inportb (gd_port + PARPORT_CONTROL) & 0x01) << 1;
  384. for (i = 0; i < len; i++)
  385. {
  386. if (gd6_send_byte_helper (data[i], timeout) != GD_OK)
  387. return GD_ERROR;
  388. gd_bytessent++;
  389. if ((gd_bytessent - GD_HEADER_LEN) % 8192 == 0)
  390. {
  391. ucon64_gauge (gd_starttime, gd_bytessent, gd_fsize);
  392. gd_checkabort (2); // 2 to return something other than 1
  393. }
  394. }
  395. return GD_OK;
  396. }
  397. static int
  398. gd6_receive_bytes (unsigned char *buffer, int len)
  399. {
  400. int i;
  401. unsigned char nibble1, nibble2;
  402. unsigned int timeout = 0x1e0000;
  403. outportb (gd_port, 0x80); // Signal the SF6/SF7 to send the next nibble
  404. for (i = 0; i < len; i++)
  405. {
  406. while ((inportb (gd_port + PARPORT_STATUS) & 0x80) == 0)
  407. if (--timeout == 0)
  408. return GD_ERROR;
  409. nibble1 = (inportb (gd_port + PARPORT_STATUS) >> 3) & 0x0f;
  410. outportb (gd_port, 0x00); // Signal the SF6/SF7 to send the next nibble
  411. while ((inportb (gd_port + PARPORT_STATUS) & 0x80) != 0)
  412. if (--timeout == 0)
  413. return GD_ERROR;
  414. nibble2 = (inportb (gd_port + PARPORT_STATUS) << 1) & 0xf0;
  415. buffer[i] = nibble2 | nibble1;
  416. outportb (gd_port, 0x80);
  417. }
  418. return GD_OK;
  419. }
  420. int
  421. gd_add_filename (const char *filename)
  422. {
  423. char buf[FILENAME_MAX], *p;
  424. if (gd_name_i < GD3_MAX_UNITS)
  425. {
  426. strcpy (buf, filename);
  427. p = strrchr (buf, '.');
  428. if (p)
  429. *p = 0;
  430. strncpy (gd_names[gd_name_i], basename2 (buf), 11);
  431. gd_names[gd_name_i][11] = 0;
  432. gd_name_i++;
  433. }
  434. return 0;
  435. }
  436. int
  437. gd3_read_rom (const char *filename, unsigned short parport)
  438. {
  439. (void) filename; // warning remover
  440. (void) parport; // warning remover
  441. return fputs ("ERROR: The function for dumping a cartridge is not yet implemented for the SF3\n", stderr);
  442. }
  443. int
  444. gd6_read_rom (const char *filename, unsigned short parport)
  445. {
  446. #if 0
  447. FILE *file;
  448. unsigned char *buffer;
  449. init_io (parport);
  450. if ((file = fopen (filename, "wb")) == NULL)
  451. {
  452. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  453. exit (1);
  454. }
  455. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  456. {
  457. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  458. exit (1);
  459. }
  460. free (buffer);
  461. fclose (file);
  462. return 0;
  463. #else
  464. (void) filename; // warning remover
  465. (void) parport; // warning remover
  466. return fputs ("ERROR: The function for dumping a cartridge is not yet implemented for the SF6\n", stderr);
  467. #endif
  468. }
  469. int
  470. gd3_write_rom (const char *filename, unsigned short parport, st_ucon64_nfo_t *rominfo)
  471. {
  472. gd_send_prolog_byte = gd3_send_prolog_byte; // for gd_send_unit_prolog()
  473. gd_send_prolog_bytes = gd3_send_prolog_bytes;
  474. gd_send_bytes = gd3_send_bytes;
  475. return gd_write_rom (filename, parport, rominfo, GD3_PROLOG_STRING);
  476. }
  477. int
  478. gd6_write_rom (const char *filename, unsigned short parport, st_ucon64_nfo_t *rominfo)
  479. {
  480. gd_send_prolog_byte = gd6_send_prolog_byte; // for gd_send_unit_prolog()
  481. gd_send_prolog_bytes = gd6_send_prolog_bytes;
  482. gd_send_bytes = gd6_send_bytes;
  483. return gd_write_rom (filename, parport, rominfo, GD6_READ_PROLOG_STRING);
  484. }
  485. /*
  486. Note: On most Game Doctor's the way you enter link mode to be able to upload
  487. the ROM to the unit is to hold down the R key on the controller while
  488. resetting the SNES. You will see the Game Doctor menu has a message that
  489. says "LINKING.."
  490. */
  491. static int
  492. gd_write_rom (const char *filename, unsigned short parport, st_ucon64_nfo_t *rominfo,
  493. const char *prolog_str)
  494. {
  495. FILE *file = NULL;
  496. unsigned char *buffer;
  497. char *names[GD3_MAX_UNITS], names_mem[GD3_MAX_UNITS][12],
  498. *filenames[GD3_MAX_UNITS], dir[FILENAME_MAX];
  499. int num_units, i, send_header, x, split = 1, hirom = snes_get_snes_hirom();
  500. init_io (parport);
  501. // We don't want to malloc() ridiculously small chunks (of 12 bytes)
  502. for (i = 0; i < GD3_MAX_UNITS; i++)
  503. names[i] = names_mem[i];
  504. gd_names = (char **) names;
  505. num_units = ucon64.split = ucon64_testsplit (filename, gd_add_filename);
  506. if (!ucon64.split)
  507. {
  508. split = 0;
  509. num_units = snes_make_gd_names (filename, rominfo, (char **) names);
  510. }
  511. dirname2 (filename, dir);
  512. gd_fsize = 0;
  513. for (i = 0; i < num_units; i++)
  514. {
  515. // No suffix is necessary but the name entry must be upper case and MUST
  516. // be 11 characters long, padded at the end with spaces if necessary.
  517. memset (gd3_dram_unit[i].name, ' ', 11); // "pad" with spaces
  518. gd3_dram_unit[i].name[11] = 0; // terminate string so we can print it (debug)
  519. // Use memcpy() instead of strcpy() so that the string terminator in
  520. // names[i] won't be copied.
  521. memcpy (gd3_dram_unit[i].name, strupr (names[i]), strlen (names[i]));
  522. x = strlen (dir) + strlen (names[i]) + 6; // file sep., suffix, ASCII-z => 6
  523. if ((filenames[i] = (char *) malloc (x)) == NULL)
  524. {
  525. fprintf (stderr, ucon64_msg[BUFFER_ERROR], x);
  526. exit (1);
  527. }
  528. sprintf (filenames[i], "%s" DIR_SEPARATOR_S "%s.078", dir, names[i]); // should match with what code of -s does
  529. if (split)
  530. {
  531. x = fsizeof (filenames[i]);
  532. gd_fsize += x;
  533. gd3_dram_unit[i].size = x;
  534. if (i == 0) // Correct for header of first file
  535. gd3_dram_unit[i].size -= GD_HEADER_LEN;
  536. }
  537. else
  538. {
  539. if (!gd_fsize)
  540. gd_fsize = ucon64.file_size;
  541. if (hirom)
  542. gd3_dram_unit[i].size = (gd_fsize - GD_HEADER_LEN) / num_units;
  543. else
  544. {
  545. if ((i + 1) * 8 * MBIT <= gd_fsize - GD_HEADER_LEN)
  546. gd3_dram_unit[i].size = 8 * MBIT;
  547. else
  548. gd3_dram_unit[i].size = gd_fsize - GD_HEADER_LEN - i * 8 * MBIT;
  549. }
  550. }
  551. }
  552. if ((buffer = (unsigned char *) malloc (8 * MBIT)) == NULL)
  553. { // a DRAM unit can hold 8 Mbit at maximum
  554. fprintf (stderr, ucon64_msg[ROM_BUFFER_ERROR], 8 * MBIT);
  555. exit (1);
  556. }
  557. printf ("Send: %d Bytes (%.4f Mb)\n", gd_fsize, (float) gd_fsize / MBIT);
  558. // Put this just before the real transfer begins or else the ETA won't be
  559. // correct.
  560. gd_starttime = time (NULL);
  561. // Send the ROM to the hardware
  562. if (memcmp (prolog_str, GD6_READ_PROLOG_STRING, 4) == 0)
  563. if (gd6_sync_hardware () == GD_ERROR)
  564. io_error ();
  565. memcpy (buffer, prolog_str, 4);
  566. buffer[4] = (unsigned char) num_units;
  567. if (gd_send_prolog_bytes (buffer, 5) == GD_ERROR)
  568. io_error ();
  569. puts ("Press q to abort\n");
  570. for (i = 0; i < num_units; i++)
  571. {
  572. #ifdef DEBUG
  573. printf ("\nfilename (%d): \"%s\", ", split, (split ? (char *) filenames[i] : filename));
  574. printf ("name: \"%s\", size: %d\n", gd3_dram_unit[i].name, gd3_dram_unit[i].size);
  575. #endif
  576. if (split)
  577. {
  578. if ((file = fopen (filenames[i], "rb")) == NULL)
  579. {
  580. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filenames[i]);
  581. exit (1);
  582. }
  583. }
  584. else
  585. if (file == NULL) // don't open the file more than once
  586. if ((file = fopen (filename, "rb")) == NULL)
  587. {
  588. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  589. exit (1);
  590. }
  591. /*
  592. When sending a "pre-split" file, opening the next file often causes
  593. enough of a delay for the GDSF7. When sending an unsplit file without
  594. an extra delay often results in the infamous "File Size Error !".
  595. Adding the delay also results in the transfer code catching an I/O
  596. error more often, instead of simply hanging (while the GDSF7 displays
  597. "File Size Error !"). The delay seems *much* more important for HiROM
  598. games than for LoROM games.
  599. There's more to the "File Size Error !" than this as the transfer
  600. reliability is still far from 100%, but the delay helps me a lot.
  601. Note that this applies to the SF3 protocol. Using the SF6 protocol does
  602. not work for me. - dbjh
  603. */
  604. wait2 (100); // 100 ms seems a good value
  605. send_header = i == 0 ? 1 : 0;
  606. if (gd_send_unit_prolog (send_header, gd3_dram_unit[i].size) == GD_ERROR)
  607. io_error ();
  608. if (gd_send_prolog_bytes ((unsigned char *) gd3_dram_unit[i].name, 11) == GD_ERROR)
  609. io_error ();
  610. if (send_header)
  611. {
  612. // Send the Game Doctor 512 byte header
  613. fread (buffer, 1, GD_HEADER_LEN, file);
  614. if (gd_send_prolog_bytes (buffer, GD_HEADER_LEN) == GD_ERROR)
  615. io_error ();
  616. gd_bytessent += GD_HEADER_LEN;
  617. }
  618. if (split == 0) // Not pre-split -- have to split it ourselves
  619. {
  620. if (hirom)
  621. fseek (file, i * gd3_dram_unit[0].size + GD_HEADER_LEN, SEEK_SET);
  622. else
  623. fseek (file, i * 8 * MBIT + GD_HEADER_LEN, SEEK_SET);
  624. }
  625. fread (buffer, 1, gd3_dram_unit[i].size, file);
  626. if (gd_send_bytes (buffer, gd3_dram_unit[i].size) == GD_ERROR)
  627. io_error ();
  628. if (split || i == num_units - 1)
  629. fclose (file);
  630. }
  631. for (i = 0; i < num_units; i++)
  632. free (filenames[i]);
  633. free (buffer);
  634. return 0;
  635. }
  636. int
  637. gd3_read_sram (const char *filename, unsigned short parport)
  638. {
  639. (void) filename; // warning remover
  640. (void) parport; // warning remover
  641. return fputs ("ERROR: The function for dumping SRAM is not yet implemented for the SF3\n", stderr);
  642. }
  643. int
  644. gd6_read_sram (const char *filename, unsigned short parport)
  645. {
  646. FILE *file;
  647. unsigned char *buffer, gdfilename[12];
  648. int len, bytesreceived = 0, transfer_size;
  649. time_t starttime;
  650. init_io (parport);
  651. if ((file = fopen (filename, "wb")) == NULL)
  652. {
  653. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  654. exit (1);
  655. }
  656. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  657. {
  658. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  659. exit (1);
  660. }
  661. // Be nice to the user and automatically remove the file on an error (or abortion)
  662. gd_destfname = filename;
  663. gd_destfile = file;
  664. register_func (remove_destfile);
  665. if (gd6_sync_hardware () == GD_ERROR)
  666. io_error ();
  667. if (gd6_send_prolog_bytes ((unsigned char *) GD6_WRITE_PROLOG_STRING, 4) == GD_ERROR)
  668. io_error ();
  669. /*
  670. The BRAM (SRAM) filename doesn't have to exactly match any game loaded in
  671. the SF7. It needs to match any valid Game Doctor filename AND have an
  672. extension of .B## (where # is a digit from 0-9)
  673. */
  674. strcpy ((char *) gdfilename, "SF16497 B00"); // TODO: We might need to make a GD filename from the real one
  675. if (gd6_send_prolog_bytes (gdfilename, 11) == GD_ERROR)
  676. io_error ();
  677. if (gd6_sync_receive_start () == GD_ERROR)
  678. io_error ();
  679. if (gd6_receive_bytes (buffer, 16) == GD_ERROR)
  680. io_error ();
  681. transfer_size = buffer[1] | (buffer[2] << 8) | (buffer[3] << 16) | (buffer[4] << 24);
  682. if (transfer_size != 0x8000)
  683. {
  684. fprintf (stderr, "ERROR: SRAM transfer size from Game Doctor != 0x8000 bytes\n");
  685. exit (1);
  686. }
  687. printf ("Receive: %d Bytes\n", transfer_size);
  688. puts ("Press q to abort\n");
  689. starttime = time (NULL);
  690. while (bytesreceived < transfer_size)
  691. {
  692. if (transfer_size - bytesreceived >= BUFFERSIZE)
  693. len = BUFFERSIZE;
  694. else
  695. len = transfer_size - bytesreceived;
  696. if (gd6_receive_bytes (buffer, len) == GD_ERROR)
  697. io_error ();
  698. fwrite (buffer, 1, len, file);
  699. bytesreceived += len;
  700. ucon64_gauge (starttime, bytesreceived, 32 * 1024);
  701. gd_checkabort (2);
  702. }
  703. unregister_func (remove_destfile);
  704. free (buffer);
  705. fclose (file);
  706. return 0;
  707. }
  708. int
  709. gd3_write_sram (const char *filename, unsigned short parport)
  710. {
  711. gd_send_prolog_bytes = gd3_send_prolog_bytes;
  712. gd_send_bytes = gd3_send_bytes;
  713. return gd_write_sram (filename, parport, GD3_PROLOG_STRING);
  714. }
  715. int
  716. gd6_write_sram (const char *filename, unsigned short parport)
  717. {
  718. gd_send_prolog_bytes = gd6_send_prolog_bytes;
  719. gd_send_bytes = gd6_send_bytes;
  720. return gd_write_sram (filename, parport, GD6_READ_PROLOG_STRING);
  721. }
  722. static int
  723. gd_write_sram (const char *filename, unsigned short parport, const char *prolog_str)
  724. {
  725. FILE *file;
  726. unsigned char *buffer, gdfilename[12];
  727. int bytesread, bytessent = 0, size, header_size;
  728. time_t starttime;
  729. init_io (parport);
  730. if ((file = fopen (filename, "rb")) == NULL)
  731. {
  732. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  733. exit (1);
  734. }
  735. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  736. {
  737. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  738. exit (1);
  739. }
  740. size = ucon64.file_size; // GD SRAM is 4*8 KB, emu SRAM often not
  741. if (size == 0x8000)
  742. header_size = 0;
  743. else if (size == 0x8200)
  744. {
  745. header_size = 0x200;
  746. size = 0x8000;
  747. }
  748. else
  749. {
  750. fputs ("ERROR: GD SRAM file size must be 32768 or 33280 bytes\n", stderr);
  751. exit (1);
  752. }
  753. printf ("Send: %d Bytes\n", size);
  754. fseek (file, header_size, SEEK_SET); // skip the header
  755. if (memcmp (prolog_str, GD6_READ_PROLOG_STRING, 4) == 0)
  756. if (gd6_sync_hardware () == GD_ERROR)
  757. io_error ();
  758. memcpy (buffer, prolog_str, 4);
  759. buffer[4] = 1;
  760. if (gd_send_prolog_bytes (buffer, 5) == GD_ERROR)
  761. io_error ();
  762. buffer[0] = 0x00;
  763. buffer[1] = 0x80;
  764. buffer[2] = 0x00;
  765. buffer[3] = 0x00;
  766. if (gd_send_prolog_bytes (buffer, 4) == GD_ERROR)
  767. io_error ();
  768. /*
  769. The BRAM (SRAM) filename doesn't have to exactly match any game loaded in
  770. the SF7. It needs to match any valid Game Doctor filename AND have an
  771. extension of .B## (where # is a digit from 0-9)
  772. */
  773. strcpy ((char *) gdfilename, "SF8123 B00"); // TODO: We might need to make a GD filename from the real one
  774. if (gd_send_prolog_bytes (gdfilename, 11) == GD_ERROR)
  775. io_error ();
  776. puts ("Press q to abort\n"); // print here, NOT before first GD I/O,
  777. // because if we get here q works ;-)
  778. starttime = time (NULL);
  779. while ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) != 0)
  780. {
  781. if (gd_send_bytes (buffer, bytesread) == GD_ERROR)
  782. io_error ();
  783. bytessent += bytesread;
  784. ucon64_gauge (starttime, bytessent, size);
  785. gd_checkabort (2);
  786. }
  787. free (buffer);
  788. fclose (file);
  789. return 0;
  790. }
  791. int
  792. gd3_read_saver (const char *filename, unsigned short parport)
  793. {
  794. (void) filename; // warning remover
  795. (void) parport; // warning remover
  796. return fputs ("ERROR: The function for dumping saver data is not yet implemented for the SF3\n", stderr);
  797. }
  798. int
  799. gd6_read_saver (const char *filename, unsigned short parport)
  800. {
  801. FILE *file;
  802. unsigned char *buffer, gdfilename[12];
  803. int len, bytesreceived = 0, transfer_size;
  804. time_t starttime;
  805. init_io (parport);
  806. if ((file = fopen (filename, "wb")) == NULL)
  807. {
  808. fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], filename);
  809. exit (1);
  810. }
  811. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  812. {
  813. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  814. exit (1);
  815. }
  816. // Be nice to the user and automatically remove the file on an error (or abortion)
  817. gd_destfname = filename;
  818. gd_destfile = file;
  819. register_func (remove_destfile);
  820. if (gd6_sync_hardware () == GD_ERROR)
  821. io_error ();
  822. if (gd6_send_prolog_bytes ((unsigned char *) GD6_WRITE_PROLOG_STRING, 4) == GD_ERROR)
  823. io_error ();
  824. /*
  825. TODO: Graceful handling of an abort because of a name error?
  826. Currently we fail with a generic error.
  827. TODO: We could make a GD filename from the real one but a valid dummy name
  828. seems to work OK here. The user must have the proper game selected in
  829. the SF7 menu even if the real name is used.
  830. */
  831. strcpy ((char *) gdfilename, "SF16497 S00");
  832. if (gd6_send_prolog_bytes (gdfilename, 11) == GD_ERROR)
  833. io_error ();
  834. if (gd6_sync_receive_start () == GD_ERROR)
  835. io_error ();
  836. if (gd6_receive_bytes (buffer, 16) == GD_ERROR)
  837. io_error ();
  838. transfer_size = buffer[1] | (buffer[2] << 8) | (buffer[3] << 16) | (buffer[4] << 24);
  839. if (transfer_size != 0x38000)
  840. {
  841. fputs ("ERROR: Saver transfer size from Game Doctor != 0x38000 bytes\n", stderr);
  842. exit (1);
  843. }
  844. printf ("Receive: %d Bytes\n", transfer_size);
  845. puts ("Press q to abort\n");
  846. starttime = time (NULL);
  847. while (bytesreceived < transfer_size)
  848. {
  849. if (transfer_size - bytesreceived >= BUFFERSIZE)
  850. len = BUFFERSIZE;
  851. else
  852. len = transfer_size - bytesreceived;
  853. if (gd6_receive_bytes (buffer, len) == GD_ERROR)
  854. io_error ();
  855. fwrite (buffer, 1, len, file);
  856. bytesreceived += len;
  857. ucon64_gauge (starttime, bytesreceived, 32 * 1024);
  858. gd_checkabort (2);
  859. }
  860. unregister_func (remove_destfile);
  861. free (buffer);
  862. fclose (file);
  863. return 0;
  864. }
  865. int
  866. gd3_write_saver (const char *filename, unsigned short parport)
  867. {
  868. gd_send_prolog_bytes = gd3_send_prolog_bytes;
  869. gd_send_bytes = gd3_send_bytes;
  870. return gd_write_saver (filename, parport, GD3_PROLOG_STRING);
  871. }
  872. int
  873. gd6_write_saver (const char *filename, unsigned short parport)
  874. {
  875. gd_send_prolog_bytes = gd6_send_prolog_bytes;
  876. gd_send_bytes = gd6_send_bytes;
  877. return gd_write_saver (filename, parport, GD6_READ_PROLOG_STRING);
  878. }
  879. static int
  880. gd_write_saver (const char *filename, unsigned short parport, const char *prolog_str)
  881. {
  882. FILE *file;
  883. unsigned char *buffer, gdfilename[12];
  884. const char *p;
  885. int bytesread, bytessent = 0, size, fn_length;
  886. time_t starttime;
  887. init_io (parport);
  888. /*
  889. Check that filename is a valid Game Doctor saver filename.
  890. It should start with SF, followed by the game ID, followed by the extension.
  891. The extension is of the form .S## (where # is a digit from 0-9).
  892. E.g., SF16123.S00
  893. The saver base filename must match the base name of the game (loaded in the
  894. Game Doctor) that you are loading the saver data for.
  895. */
  896. // Strip the path out of filename for use in the GD
  897. p = basename2 (filename);
  898. fn_length = strlen (p);
  899. if (fn_length < 6 || fn_length > 11 // 7 ("base") + 1 (period) + 3 (extension)
  900. || toupper ((int) p[0]) != 'S' || toupper ((int) p[1]) != 'F'
  901. || p[fn_length - 4] != '.' || toupper ((int) p[fn_length - 3]) != 'S')
  902. {
  903. fprintf (stderr, "ERROR: Filename (%s) is not a saver filename (SF*.S##)\n", p);
  904. exit (1);
  905. }
  906. if ((file = fopen (filename, "rb")) == NULL)
  907. {
  908. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  909. exit (1);
  910. }
  911. if ((buffer = (unsigned char *) malloc (BUFFERSIZE)) == NULL)
  912. {
  913. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  914. exit (1);
  915. }
  916. size = ucon64.file_size;
  917. if (size != 0x38000) // GD saver size is always 0x38000 bytes -- no header
  918. {
  919. fputs ("ERROR: GD saver file size must be 229376 bytes\n", stderr);
  920. exit (1);
  921. }
  922. // Make a GD filename from the real one
  923. memset (gdfilename, ' ', 11); // "pad" with spaces
  924. gdfilename[11] = 0; // terminate string
  925. memcpy (gdfilename, p, fn_length - 4); // copy name except extension
  926. memcpy (&gdfilename[8], "S00", 3); // copy extension S00
  927. strupr ((char *) gdfilename);
  928. printf ("Send: %d Bytes\n", size);
  929. fseek (file, 0, SEEK_SET);
  930. if (memcmp (prolog_str, GD6_READ_PROLOG_STRING, 4) == 0)
  931. if (gd6_sync_hardware () == GD_ERROR)
  932. io_error ();
  933. memcpy (buffer, prolog_str, 4);
  934. buffer[4] = 1;
  935. if (gd_send_prolog_bytes (buffer, 5) == GD_ERROR)
  936. io_error ();
  937. // Transfer 0x38000 bytes
  938. buffer[0] = 0x00;
  939. buffer[1] = 0x80;
  940. buffer[2] = 0x03;
  941. buffer[3] = 0x00;
  942. if (gd_send_prolog_bytes (buffer, 4) == GD_ERROR)
  943. io_error ();
  944. if (gd_send_prolog_bytes (gdfilename, 11) == GD_ERROR)
  945. io_error ();
  946. puts ("Press q to abort\n"); // print here, NOT before first GD I/O,
  947. // because if we get here q works ;-)
  948. starttime = time (NULL);
  949. while ((bytesread = fread (buffer, 1, BUFFERSIZE, file)) != 0)
  950. {
  951. if (gd_send_bytes (buffer, bytesread) == GD_ERROR)
  952. io_error ();
  953. bytessent += bytesread;
  954. ucon64_gauge (starttime, bytessent, size);
  955. gd_checkabort (2);
  956. }
  957. free (buffer);
  958. fclose (file);
  959. return 0;
  960. }
  961. #endif // USE_PARALLEL