doctor64jr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. doctor64jr.c - Bung Doctor V64 Junior support for uCON64
  3. Copyright (c) 1999 - 2002 NoisyB
  4. Copyright (c) 2004, 2015 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. /*
  18. drjr transfer protocol
  19. DB25 pin name
  20. p2~p9 pd[7:0] XXXXXXXXXXX ai XXXXX data XXXX
  21. p1 nwrite ~~~~~~~~~|_____________________|~~
  22. p14 ndstb ~~~~~~~~~~~~~~~~~~~~~~~~~|_|~~~~~~
  23. p17 nastb ~~~~~~~~~~~~~|_|~~~~~~~~~~~~~~~~~~
  24. ai[]=0 r/w a[7..0]
  25. ai[]=1 r/w a[15..8]
  26. ai[]=2 r/w a[23..16]
  27. ai[]=3 w a[28..24]
  28. ai[]=3 r (rst,wdf,wcf,a[28..24])
  29. ai[]=4 r/w data
  30. ai[]=5 w mode
  31. ai[]=6 w en_1
  32. ai[]=7 w en_0
  33. *remark
  34. a[8..1] support page count up
  35. ai[3]d7:0=N64 power off, 1=N64 power on
  36. d6:0=no dram data written, 1=dram data written
  37. d5:0=no data write in b4000000~b7ffffff, 1=some data written in b4000000~b7ffffff
  38. mode d0:0=dram read only and clear wdf, 1=dram write enable
  39. d1:0=disable cartridge read and clear wcf flag,
  40. 1=enable cartridge read(write b4000000~b7ffffff will switch off dram and cartridge will present at b0000000~b3ffffff)
  41. en_0=05 and en_1=0a is enable port control
  42. mode:q0 0 1 0 1
  43. mode:q1 0 0 1 1
  44. b7ff ffff
  45. b400 0000 dram read only dram r/w cartridge read cartridge read(* write this area will switch off dram)
  46. b3ff ffff
  47. b000 0000 dram read only dram r/w dram read only dram r/w
  48. eg:enable port control
  49. DB25 pin name
  50. p2~p9 pd[7:0] XXXXXXXXXXX 07 XX 05 XXXX 06 XX 0a XXXXXXXXXXXX
  51. p1 nwrite ~~~~~~~~~|_____________________________|~~~~~~~
  52. p14 ndstb ~~~~~~~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~
  53. p17 nastb ~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~~~~~~~
  54. en_0=05 en_1=0a
  55. eg:write adr $b0123456, data $a55a,$1234..
  56. DB25 pin name
  57. p2~p9 pd[7:0] XXXXXXXXXXX 00 XX 56 XXXX 01 XX 34 XXXX 02 XX 12 XXXX 03 XX b0 XXXXXX 04 XX 5a XX a5 XX 34 XX 12 XXXXXXXXXXX
  58. p1 nwrite ~~~~~~~~~|_______________________________________________________________________________________|~~~~~~~~~~
  59. p14 ndstb ~~~~~~~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~~|_|~~~|_|~~~|_|~~~|_|~~~~~~~~~~~
  60. p17 nastb ~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~~|_|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61. set adr word low set adr word high wdata a55a wdata 1234 (after write adr=b012345a)
  62. eg:read adr $b0123400~$b01235ff, 512 data
  63. DB25 pin name
  64. p2~p9 pd[7:0] XXXXXXXXXXX 00 XX 00 XXXX 01 XX 34 XXXX 02 XX 12 XXXX 03 XX b0 XXXXXX 04 XX data0 XX data1 X ... X data510 XX data511 XXXXX
  65. p1 nwrite ~~~~~~~~~|________________________________________________________________~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. p14 ndstb ~~~~~~~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~~~|_|~~~~~~|_|~~~ ~~~ ~~~~|_|~~~~~~~~|_|~~~~~~~~
  67. p17 nastb ~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~~|_|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. set adr word low set adr word high (after 512 read adr=b0123400)
  69. eg:dram write protect, disable N64 access to cartridge and disable port control
  70. DB25 pin name
  71. p2~p9 pd[7:0] XXXXXXXXXXX 05 XX 00 XXXX 07 XX 00 XXXX 06 XX 00 XXXXXXXXXXXX
  72. p1 nwrite ~~~~~~~~~|________________________________________|~~~~~~~~~~
  73. p14 ndstb ~~~~~~~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~
  74. p17 nastb ~~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~|_|~~~~~~~~~~~~~~~~~~
  75. mode=00 en_0=00 en_1=00
  76. simple backup rountine for N64
  77. void writePI(unsigned long addr, unsigned long value)
  78. {
  79. do {} while (*(volatile unsigned long *) (0xa4600010) & 3); // check parallel interface not busy
  80. addr &=0xbffffffc;
  81. *(unsigned long *)(addr)=value;
  82. }
  83. unsigned long readPI(unsigned long addr)
  84. {
  85. do {} while (*(volatile unsigned long *) (0xa4600010) & 3); // check parallel interface not busy
  86. addr &=0xbffffffc;
  87. return *(unsigned long *)(addr);
  88. }
  89. // MAIN -- START OF USER CODE
  90. void mainproc(void *arg) {
  91. u32 base_adr;
  92. for (base_adr=0;base_adr<0x1000000;base_adr++){ // backup 128Mbits
  93. writePI(0xb0000000+base_adr,readPI(0xb4000000 + base_adr)); // write data
  94. }
  95. }
  96. */
  97. #ifdef HAVE_CONFIG_H
  98. #include "config.h"
  99. #endif
  100. #include <stdlib.h>
  101. #include "misc/archive.h"
  102. #include "misc/parallel.h"
  103. #include "ucon64.h"
  104. #include "ucon64_misc.h"
  105. #include "backup/doctor64jr.h"
  106. #ifdef USE_PARALLEL
  107. static st_ucon64_obj_t doctor64jr_obj[] =
  108. {
  109. {UCON64_N64, WF_DEFAULT | WF_STOP | WF_NO_ROM}
  110. };
  111. #endif
  112. const st_getopt2_t doctor64jr_usage[] =
  113. {
  114. {
  115. NULL, 0, 0, 0,
  116. NULL, "Doctor V64 Junior"/*"19XX Bung Enterprises Ltd http://www.bung.com.hk"*/,
  117. NULL
  118. },
  119. #ifdef USE_PARALLEL
  120. {
  121. "xdjr", 0, 0, UCON64_XDJR,
  122. NULL, "send ROM to Doctor V64 Junior; " OPTION_LONG_S "port=PORT",
  123. &doctor64jr_obj[0]
  124. },
  125. #if 0
  126. {
  127. "xdjrs", 0, 0, UCON64_XDJRS,
  128. NULL, "send/receive SRAM to/from Doctor V64 Junior; " OPTION_LONG_S "port=PORT\n"
  129. "receives automatically when SRAM does not exist",
  130. &doctor64jr_obj[0]
  131. },
  132. #endif
  133. #endif // USE_PARALLEL
  134. {NULL, 0, 0, 0, NULL, NULL, NULL}
  135. };
  136. #ifdef USE_PARALLEL
  137. #define BUFFERSIZE 32768
  138. //#define set_ai_write outportb (port_a, 5); // ninit=1, nwrite=0
  139. #define set_data_write outportb (port_a, 1); // ninit=0, nwrite=0
  140. #define set_data_read outportb (port_a, 0); // ninit=0, nwrite=1
  141. //#define set_normal outportb (port_a, 4); // ninit=1, nwrite=1
  142. static unsigned short int port_8, port_9, port_a, port_b, port_c,
  143. *buffer;
  144. static int wv_mode;
  145. static void
  146. set_ai (unsigned char ai)
  147. {
  148. outportb (port_a, 5); // ninit=1, nwrite=0
  149. outportb (port_b, ai);
  150. }
  151. static void
  152. set_ai_data (unsigned char ai, unsigned char data)
  153. {
  154. set_ai (ai);
  155. set_data_write // ninit=0, nwrite=0
  156. outportb (port_c, data);
  157. }
  158. static void
  159. init_port (int enable_write)
  160. {
  161. #ifndef USE_PPDEV
  162. outportb (port_9, 1); // clear EPP time flag
  163. #endif
  164. set_ai_data (6, 0x0a);
  165. set_ai_data (7, 0x05); // 6==0x0a, 7==0x05 is pc_control mode
  166. // set_ai (5);
  167. // set_data_read
  168. // enable_write = inportb (port_c);
  169. set_ai_data (5, (unsigned char) enable_write); // d0=0 is write protect mode
  170. }
  171. static void
  172. end_port (int enable_write)
  173. {
  174. set_ai_data (5, (unsigned char) enable_write); // d0=0 is write protect mode
  175. set_ai_data (7, 0); // release pc mode
  176. set_ai_data (6, 0); // 6==0x0a, 7==0x05 is pc_control mode
  177. outportb (port_a, 4); // ninit=1, nwrite=1
  178. }
  179. static int
  180. check_card (void)
  181. {
  182. set_ai_data (3, 0x12);
  183. set_ai_data (2, 0x34);
  184. set_ai_data (1, 0x56);
  185. set_ai_data (0, 0x78);
  186. set_ai (3);
  187. set_data_read // ninit=0, nwrite=1
  188. if ((inportb (port_c) & 0x1f) != 0x12)
  189. return 1;
  190. set_ai (2);
  191. set_data_read
  192. if (inportb (port_c) != 0x34)
  193. return 1;
  194. set_ai (1);
  195. set_data_read
  196. if (inportb (port_c) != 0x56)
  197. return 1;
  198. set_ai (0);
  199. set_data_read
  200. if (inportb (port_c) != 0x78)
  201. return 1;
  202. return 0;
  203. }
  204. static int
  205. write_32k (unsigned short int hi_word, unsigned short int lo_word)
  206. {
  207. unsigned char unpass, pass1;
  208. unsigned short int i, j, fix;
  209. set_ai_data (3, (unsigned char) (0x10 | (hi_word >> 8)));
  210. set_ai_data (2, (unsigned char) hi_word);
  211. for (i = 0; i < 0x40; i++)
  212. {
  213. unpass = 3;
  214. while (unpass)
  215. {
  216. set_ai_data (1, (unsigned char) ((i << 1) | lo_word));
  217. set_ai_data (0, 0);
  218. set_ai (4); // set address index=4
  219. set_data_write // ninit=0, nwrite=0
  220. fix = i << 8;
  221. for (j = 0; j < 256; j++)
  222. outportw (port_c, buffer[j + fix]);
  223. set_data_read // ninit=0, nwrite=1
  224. if (wv_mode)
  225. {
  226. for (j = 0; j < 256; j++)
  227. if (inportw (port_c) != buffer[j + fix])
  228. break;
  229. }
  230. else
  231. {
  232. pass1 = 1;
  233. for (j = 0; j < 4; j++)
  234. if (inportw (port_c) != buffer[j + fix])
  235. {
  236. pass1 = 0;
  237. break;
  238. }
  239. if (pass1)
  240. {
  241. set_ai_data (1, (unsigned char) ((i << 1) | lo_word | 1));
  242. set_ai_data (0, 0xf8);
  243. set_ai (4);
  244. set_data_read // ninit=0, nwrite=1
  245. for (j = 252; j < 256; j++)
  246. if (inportw (port_c) != buffer[j + fix])
  247. break;
  248. }
  249. }
  250. set_ai (0);
  251. set_data_read // ninit=0, nwrite=1
  252. if (inportb (port_c) != 0)
  253. {
  254. unpass--;
  255. outportb (port_a, 0x0b); // set all pin=0 for debug
  256. set_ai_data (3, (unsigned char) (0x10 | (hi_word >> 8)));
  257. set_ai_data (2, (unsigned char) hi_word);
  258. if (unpass == 0)
  259. return 1;
  260. }
  261. else
  262. unpass = 0;
  263. }
  264. }
  265. /*
  266. outportb (ai, 0);
  267. printf ("\na[7..0]=%02x\n", inportb (data));
  268. outportb (ai, 1);
  269. printf ("a[15..8]=%02x\n", inportb (data));
  270. */
  271. return 0;
  272. }
  273. #if 0 // not used
  274. static int
  275. verify_32k (unsigned short int hi_word, unsigned short int lo_word)
  276. {
  277. char unpass;
  278. unsigned short int i, j, fix;
  279. set_ai_data (3, (unsigned char) (0x10 | (hi_word >> 8)));
  280. set_ai_data (2, (unsigned char) hi_word);
  281. for (i = 0; i < 0x40; i++)
  282. {
  283. unpass = 3;
  284. while (unpass)
  285. {
  286. set_ai_data (1, (unsigned char) ((i << 1) | lo_word));
  287. set_ai_data (0, 0);
  288. set_ai (4);
  289. set_data_read // ninit=0, nwrite=1
  290. fix = i << 8;
  291. for (j = 0; j < 256; j++)
  292. {
  293. if (inportw (port_c) != buffer[j + fix])
  294. {
  295. outportb (port_a, 0x0b); // all pin=0 for debug
  296. set_ai_data (3, (unsigned char) (0x10 | (hi_word >> 8)));
  297. set_ai_data (2, (unsigned char) hi_word);
  298. unpass--;
  299. if (unpass == 0)
  300. return 1;
  301. else
  302. break;
  303. }
  304. }
  305. if (j == 256)
  306. break;
  307. }
  308. }
  309. /*
  310. outportb (ai,0);
  311. printf ("\na[7..0]=%02x\n", inportb (data));
  312. outportb (ai, 1);
  313. printf ("a[15..8]=%02x\n", inportb (data));
  314. */
  315. return 0;
  316. }
  317. static void
  318. gen_pat_32k (unsigned short int offset)
  319. {
  320. int i;
  321. for (i = 0; i < 0x4000; i++)
  322. buffer[i] = i + offset;
  323. }
  324. static unsigned short int
  325. test_dram (void)
  326. {
  327. int n_pages = 0, page;
  328. gen_pat_32k (0x0000);
  329. write_32k (0, 0);
  330. gen_pat_32k (0x8000);
  331. write_32k (0x100, 0);
  332. gen_pat_32k (0x0000);
  333. if (verify_32k (0, 0) == 0) // find lower 128 Mbits
  334. n_pages = 0x100;
  335. gen_pat_32k (0x8000);
  336. if (verify_32k (0x100, 0) == 0) // find upper 128 Mbits
  337. n_pages = 0x200;
  338. printf ("Testing DRAM...\n");
  339. for (page = 0; page < n_pages; page++)
  340. {
  341. gen_pat_32k ((unsigned short int) (page * 2));
  342. if (write_32k (page, 0))
  343. return 0;
  344. else
  345. {
  346. fputc ('w', stdout);
  347. fflush (stdout);
  348. }
  349. gen_pat_32k ((unsigned short int) (page * 2 + 1));
  350. if (write_32k (page, 0x80))
  351. return 0;
  352. else
  353. {
  354. fputc ('w', stdout);
  355. fflush (stdout);
  356. }
  357. }
  358. fputc ('\n', stdout);
  359. for (page = 0; page < n_pages; page++)
  360. {
  361. gen_pat_32k ((unsigned short int) (page * 2));
  362. if (verify_32k (page, 0))
  363. return 0;
  364. else
  365. {
  366. fputc ('v', stdout);
  367. fflush (stdout);
  368. }
  369. gen_pat_32k ((unsigned short int) (page * 2 + 1));
  370. if (verify_32k (page, 0x80))
  371. return 0;
  372. else
  373. {
  374. fputc ('v', stdout);
  375. fflush (stdout);
  376. }
  377. }
  378. return n_pages;
  379. }
  380. #endif
  381. static unsigned long int
  382. get_address (void)
  383. {
  384. unsigned long int address;
  385. set_ai_data (6, 0x0a); // enable pc mode
  386. set_ai_data (7, 0x05); // enable pc mode
  387. set_ai (3);
  388. set_data_read // ninit=0, nwrite=1
  389. address = inportb (port_c) << 24;
  390. set_ai (2);
  391. set_data_read
  392. address |= inportb (port_c) << 16;
  393. set_ai (1);
  394. set_data_read
  395. address |= inportb (port_c) << 8;
  396. set_ai (0);
  397. set_data_read
  398. address |= inportb (port_c);
  399. return address;
  400. }
  401. int
  402. doctor64jr_read (const char *filename, unsigned short parport)
  403. {
  404. (void) filename;
  405. (void) parport;
  406. return fprintf (stderr, "ERROR: The function for dumping a cartridge is not yet implemented for the\n"
  407. " Doctor V64 Junior\n");
  408. }
  409. int
  410. doctor64jr_write (const char *filename, unsigned short parport)
  411. {
  412. unsigned int enable_write = 0, size, bytesread, bytessent = 0, n_pages;
  413. time_t init_time;
  414. unsigned short int page;
  415. FILE *file;
  416. parport_print_info ();
  417. port_8 = parport;
  418. port_9 = parport + 1;
  419. port_a = parport + 2;
  420. port_b = parport + 3;
  421. port_c = parport + 4;
  422. init_port (enable_write);
  423. if (check_card () != 0)
  424. {
  425. fprintf (stderr, "ERROR: No Doctor V64 Junior card present\n");
  426. end_port (enable_write);
  427. exit (1);
  428. }
  429. wv_mode = 0;
  430. if ((file = fopen (filename, "rb")) == NULL)
  431. {
  432. fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], filename);
  433. exit (1);
  434. }
  435. if ((buffer = (unsigned short int *) malloc (BUFFERSIZE)) == NULL)
  436. {
  437. fprintf (stderr, ucon64_msg[FILE_BUFFER_ERROR], BUFFERSIZE);
  438. exit (1);
  439. }
  440. size = ucon64.file_size;
  441. printf ("Send: %d Bytes (%.4f Mb)\n\n", size, (float) size / MBIT);
  442. #if 0
  443. if (dram_test)
  444. {
  445. dram_size = test_dram ();
  446. if (dram_size)
  447. printf ("\nDRAM size=%dMbits\n", (dram_size / 2));
  448. else
  449. fprintf (stderr, "\nERROR: DRAM test failed\n");
  450. return 0;
  451. }
  452. #endif
  453. n_pages = (size + (64 * 1024 - 1)) / (64 * 1024); // "+ (64 * 1024 - 1)" to round up
  454. init_time = time (0);
  455. for (page = 0; page < n_pages; page++)
  456. {
  457. bytesread = fread ((unsigned char *) buffer, 1, BUFFERSIZE, file);
  458. if (write_32k (page, 0))
  459. {
  460. fprintf (stderr, "ERROR: Transfer failed at address 0x%8lx", get_address ());
  461. break;
  462. }
  463. bytesread += fread ((unsigned char *) buffer, 1, BUFFERSIZE, file);
  464. if (write_32k (page, 0x80))
  465. {
  466. fprintf (stderr, "ERROR: Transfer failed at address 0x%8lx", get_address ());
  467. break;
  468. }
  469. bytessent += bytesread;
  470. ucon64_gauge (init_time, bytessent, size);
  471. }
  472. fputc ('\n', stdout);
  473. if (enable_write) // 1 or 3
  474. printf ("DRAM write protect disabled\n");
  475. if (enable_write & 2) // 3
  476. printf ("Run cartridge enabled\n");
  477. // set_ai_data(5, enable_write); // d0=0 is write protect mode
  478. end_port (enable_write);
  479. return 0;
  480. }
  481. #endif // USE_PARALLEL