cli.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /* tapplay - TAP file playback for sd2iec hardware
  2. Copyright (C) 2009 Ingo Korb <ingo@akana.de>
  3. Inspiration and low-level SD/MMC access based on code from MMC2IEC
  4. by Lars Pontoppidan et al., see sdcard.c|h and config.h.
  5. FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; version 2 of the License only.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. cli.c: The command line interface
  17. */
  18. #include <arm/NXP/LPC17xx/LPC17xx.h>
  19. #include <arm/bits.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include "config.h"
  29. #include "diskio.h"
  30. #include "ff.h"
  31. #include "timer.h"
  32. #include "uart.h"
  33. #include "fileops.h"
  34. #include "memory.h"
  35. #include "snes.h"
  36. #include "tests.h"
  37. #include "fpga.h"
  38. #include "fpga_spi.h"
  39. #include "cic.h"
  40. #include "xmodem.h"
  41. #include "rtc.h"
  42. #include "cli.h"
  43. #define MAX_LINE 250
  44. /* Variables */
  45. static char cmdbuffer[MAX_LINE + 1];
  46. static char *curchar;
  47. /* Word lists */
  48. static char command_words[] =
  49. "cd\0reset\0sreset\0dir\0ls\0test\0exit\0loadrom\0loadraw\0saveraw\0put\0rm\0mkdir\0d4\0vmode\0mapper\0settime\0time\0setfeature\0hexdump\0w8\0w16\0memset\0memtest\0";
  50. enum { CMD_CD = 0, CMD_RESET, CMD_SRESET, CMD_DIR, CMD_LS, CMD_TEST, CMD_EXIT, CMD_LOADROM, CMD_LOADRAW, CMD_SAVERAW, CMD_PUT, CMD_RM, CMD_MKDIR, CMD_D4, CMD_VMODE, CMD_MAPPER, CMD_SETTIME, CMD_TIME, CMD_SETFEATURE, CMD_HEXDUMP, CMD_W8, CMD_W16, CMD_MEMSET, CMD_MEMTEST };
  51. /* ------------------------------------------------------------------------- */
  52. /* Parse functions */
  53. /* ------------------------------------------------------------------------- */
  54. /* Skip spaces at curchar */
  55. static uint8_t skip_spaces( void )
  56. {
  57. uint8_t res = ( *curchar == ' ' || *curchar == 0 );
  58. while ( *curchar == ' ' )
  59. {
  60. curchar++;
  61. }
  62. return res;
  63. }
  64. /* Parse the string in curchar for an integer with bounds [lower,upper] */
  65. static int32_t parse_unsigned( uint32_t lower, uint32_t upper, uint8_t base )
  66. {
  67. char *end;
  68. uint32_t result;
  69. if ( strlen( curchar ) == 1 && *curchar == '?' )
  70. {
  71. printf( "Number between %ld[0x%lx] and %ld[0x%lx] expected\n", lower, lower, upper, upper );
  72. return -2;
  73. }
  74. result = strtoul( curchar, &end, base );
  75. if ( ( *end != ' ' && *end != 0 ) || errno != 0 )
  76. {
  77. printf( "Invalid numeric argument\n" );
  78. return -1;
  79. }
  80. curchar = end;
  81. skip_spaces();
  82. if ( result < lower || result > upper )
  83. {
  84. printf( "Numeric argument out of range (%ld..%ld)\n", lower, upper );
  85. return -1;
  86. }
  87. return result;
  88. }
  89. /* Parse the string starting with curchar for a word in wordlist */
  90. static int8_t parse_wordlist( char *wordlist )
  91. {
  92. uint8_t i, matched;
  93. unsigned char *cur, *ptr;
  94. unsigned char c;
  95. i = 0;
  96. ptr = ( unsigned char * )wordlist;
  97. // Command list on "?"
  98. if ( strlen( curchar ) == 1 && *curchar == '?' )
  99. {
  100. printf( "Commands available: \n " );
  101. while ( 1 )
  102. {
  103. c = *ptr++;
  104. if ( c == 0 )
  105. {
  106. if ( *ptr == 0 )
  107. {
  108. printf( "\n" );
  109. return -2;
  110. }
  111. else
  112. {
  113. printf( "\n " );
  114. }
  115. }
  116. else
  117. {
  118. uart_putc( c );
  119. }
  120. }
  121. }
  122. while ( 1 )
  123. {
  124. cur = ( unsigned char * )curchar;
  125. matched = 1;
  126. c = *ptr;
  127. do
  128. {
  129. // If current word list character is \0: No match found
  130. if ( c == 0 )
  131. {
  132. printf( "Unknown word: %s\n(use ? for help)", curchar );
  133. return -1;
  134. }
  135. if ( tolower( ( int )c ) != tolower( ( int )*cur ) )
  136. {
  137. // Check for end-of-word
  138. if ( cur != ( unsigned char * )curchar && ( *cur == ' ' || *cur == 0 ) )
  139. {
  140. // Partial match found, return that
  141. break;
  142. }
  143. else
  144. {
  145. matched = 0;
  146. break;
  147. }
  148. }
  149. ptr++;
  150. cur++;
  151. c = *ptr;
  152. }
  153. while ( c != 0 );
  154. if ( matched )
  155. {
  156. char *tmp = curchar;
  157. curchar = ( char * )cur;
  158. // Return match only if whitespace or end-of-string follows
  159. // (avoids mismatching partial words)
  160. if ( skip_spaces() )
  161. {
  162. return i;
  163. }
  164. else
  165. {
  166. printf( "Unknown word: %s\n(use ? for help)\n", tmp );
  167. return -1;
  168. }
  169. }
  170. else
  171. {
  172. // Try next word in list
  173. i++;
  174. while ( *ptr++ != 0 ) ;
  175. }
  176. }
  177. }
  178. /* Read a line from serial, uses cmdbuffer as storage */
  179. static char *getline( char *prompt )
  180. {
  181. int i = 0;
  182. char c;
  183. printf( "\n%s", prompt );
  184. memset( cmdbuffer, 0, sizeof( cmdbuffer ) );
  185. while ( 1 )
  186. {
  187. c = uart_getc();
  188. if ( c == 13 )
  189. {
  190. break;
  191. }
  192. if ( c == 27 || c == 3 )
  193. {
  194. printf( "\\\n%s", prompt );
  195. i = 0;
  196. memset( cmdbuffer, 0, sizeof( cmdbuffer ) );
  197. continue;
  198. }
  199. if ( c == 127 || c == 8 )
  200. {
  201. if ( i > 0 )
  202. {
  203. i--;
  204. uart_putc( 8 ); // backspace
  205. uart_putc( ' ' ); // erase character
  206. uart_putc( 8 ); // backspace
  207. }
  208. else
  209. {
  210. continue;
  211. }
  212. }
  213. else
  214. {
  215. if ( i < sizeof( cmdbuffer ) - 1 )
  216. {
  217. cmdbuffer[i++] = c;
  218. uart_putc( c );
  219. }
  220. }
  221. }
  222. cmdbuffer[i] = 0;
  223. return cmdbuffer;
  224. }
  225. /* ------------------------------------------------------------------------- */
  226. /* Command functions */
  227. /* ------------------------------------------------------------------------- */
  228. /* Reset */
  229. static void cmd_reset( void )
  230. {
  231. /* force watchdog reset */
  232. LPC_WDT->WDTC = 256; // minimal timeout
  233. LPC_WDT->WDCLKSEL = BV( 31 ); // internal RC, lock register
  234. LPC_WDT->WDMOD = BV( 0 ) | BV( 1 ); // enable watchdog and reset-by-watchdog
  235. LPC_WDT->WDFEED = 0xaa;
  236. LPC_WDT->WDFEED = 0x55; // initial feed to really enable WDT
  237. }
  238. /* Show the contents of the current directory */
  239. static void cmd_show_directory( void )
  240. {
  241. FRESULT res;
  242. DIR dh;
  243. FILINFO finfo;
  244. uint8_t *name;
  245. f_getcwd( ( TCHAR * )file_lfn, 255 );
  246. res = f_opendir( &dh, ( TCHAR * )file_lfn );
  247. if ( res != FR_OK )
  248. {
  249. printf( "f_opendir failed, result %d\n", res );
  250. return;
  251. }
  252. finfo.lfname = ( TCHAR * )file_lfn;
  253. finfo.lfsize = 255;
  254. do
  255. {
  256. /* Read the next entry */
  257. res = f_readdir( &dh, &finfo );
  258. if ( res != FR_OK )
  259. {
  260. printf( "f_readdir failed, result %d\n", res );
  261. return;
  262. }
  263. /* Abort if none was found */
  264. if ( !finfo.fname[0] )
  265. {
  266. break;
  267. }
  268. /* Skip volume labels */
  269. if ( finfo.fattrib & AM_VOL )
  270. {
  271. continue;
  272. }
  273. /* Select between LFN and 8.3 name */
  274. if ( finfo.lfname[0] )
  275. {
  276. name = ( uint8_t * )finfo.lfname;
  277. }
  278. else
  279. {
  280. name = ( uint8_t * )finfo.fname;
  281. strlwr( ( char * )name );
  282. }
  283. printf( "%s [%s] (%ld)", finfo.lfname, finfo.fname, finfo.fsize );
  284. /* Directory indicator (Unix-style) */
  285. if ( finfo.fattrib & AM_DIR )
  286. {
  287. uart_putc( '/' );
  288. }
  289. printf( "\n" );
  290. }
  291. while ( finfo.fname[0] );
  292. }
  293. static void cmd_loadrom( void )
  294. {
  295. uint32_t address = 0;
  296. uint8_t flags = LOADROM_WITH_SRAM | LOADROM_WITH_RESET;
  297. load_rom( ( uint8_t * )curchar, address, flags );
  298. }
  299. static void cmd_loadraw( void )
  300. {
  301. uint32_t address = parse_unsigned( 0, 16777216, 16 );
  302. load_sram( ( uint8_t * )curchar, address );
  303. }
  304. static void cmd_saveraw( void )
  305. {
  306. uint32_t address = parse_unsigned( 0, 16777216, 16 );
  307. uint32_t length = parse_unsigned( 0, 16777216, 16 );
  308. if ( address != -1 && length != -1 )
  309. {
  310. save_sram( ( uint8_t * )curchar, length, address );
  311. }
  312. }
  313. static void cmd_d4( void )
  314. {
  315. int32_t hz;
  316. if ( get_cic_state() != CIC_PAIR )
  317. {
  318. printf( "not in pair mode\n" );
  319. }
  320. else
  321. {
  322. hz = parse_unsigned( 50, 60, 10 );
  323. if ( hz == 50 )
  324. {
  325. cic_d4( CIC_PAL );
  326. }
  327. else
  328. {
  329. cic_d4( CIC_NTSC );
  330. }
  331. printf( "ok\n" );
  332. }
  333. }
  334. static void cmd_vmode( void )
  335. {
  336. int32_t hz;
  337. if ( get_cic_state() != CIC_PAIR )
  338. {
  339. printf( "not in pair mode\n" );
  340. }
  341. else
  342. {
  343. hz = parse_unsigned( 50, 60, 10 );
  344. if ( hz == 50 )
  345. {
  346. cic_videomode( CIC_PAL );
  347. }
  348. else
  349. {
  350. cic_videomode( CIC_NTSC );
  351. }
  352. printf( "ok\n" );
  353. }
  354. }
  355. void cmd_put( void )
  356. {
  357. if ( *curchar != 0 )
  358. {
  359. file_open( ( uint8_t * )curchar, FA_CREATE_ALWAYS | FA_WRITE );
  360. if ( file_res )
  361. {
  362. printf( "FAIL: error opening file %s\n", curchar );
  363. }
  364. else
  365. {
  366. printf( "OK, start xmodem transfer now.\n" );
  367. xmodem_rxfile( &file_handle );
  368. }
  369. file_close();
  370. }
  371. else
  372. {
  373. printf( "Usage: put <filename>\n" );
  374. }
  375. }
  376. void cmd_rm( void )
  377. {
  378. FRESULT res = f_unlink( curchar );
  379. if ( res )
  380. {
  381. printf( "Error %d removing %s\n", res, curchar );
  382. }
  383. }
  384. void cmd_mkdir( void )
  385. {
  386. FRESULT res = f_mkdir( curchar );
  387. if ( res )
  388. {
  389. printf( "Error %d creating directory %s\n", res, curchar );
  390. }
  391. }
  392. void cmd_mapper( void )
  393. {
  394. int32_t mapper;
  395. mapper = parse_unsigned( 0, 7, 10 );
  396. set_mapper( ( uint8_t )mapper & 0x7 );
  397. printf( "mapper set to %ld\n", mapper );
  398. }
  399. void cmd_sreset( void )
  400. {
  401. if ( *curchar != 0 )
  402. {
  403. int32_t resetstate;
  404. resetstate = parse_unsigned( 0, 1, 10 );
  405. snes_reset( resetstate );
  406. }
  407. else
  408. {
  409. snes_reset_pulse();
  410. }
  411. }
  412. void cmd_settime( void )
  413. {
  414. struct tm time;
  415. if ( strlen( curchar ) != 4 + 2 + 2 + 2 + 2 + 2 )
  416. {
  417. printf( "invalid time format (need YYYYMMDDhhmmss)\n" );
  418. }
  419. else
  420. {
  421. time.tm_sec = atoi( curchar + 4 + 2 + 2 + 2 + 2 );
  422. curchar[4 + 2 + 2 + 2 + 2] = 0;
  423. time.tm_min = atoi( curchar + 4 + 2 + 2 + 2 );
  424. curchar[4 + 2 + 2 + 2] = 0;
  425. time.tm_hour = atoi( curchar + 4 + 2 + 2 );
  426. curchar[4 + 2 + 2] = 0;
  427. time.tm_mday = atoi( curchar + 4 + 2 );
  428. curchar[4 + 2] = 0;
  429. time.tm_mon = atoi( curchar + 4 );
  430. curchar[4] = 0;
  431. time.tm_year = atoi( curchar );
  432. set_rtc( &time );
  433. }
  434. }
  435. void cmd_time( void )
  436. {
  437. struct tm time;
  438. read_rtc( &time );
  439. printf( "%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon,
  440. time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec );
  441. }
  442. void cmd_setfeature( void )
  443. {
  444. uint8_t feat = parse_unsigned( 0, 255, 16 );
  445. fpga_set_features( feat );
  446. }
  447. void cmd_hexdump( void )
  448. {
  449. uint32_t offset = parse_unsigned( 0, 16777215, 16 );
  450. uint32_t len = parse_unsigned( 0, 16777216, 16 );
  451. sram_hexdump( offset, len );
  452. }
  453. void cmd_w8( void )
  454. {
  455. uint32_t offset = parse_unsigned( 0, 16777215, 16 );
  456. uint8_t val = parse_unsigned( 0, 255, 16 );
  457. sram_writebyte( val, offset );
  458. }
  459. void cmd_w16( void )
  460. {
  461. uint32_t offset = parse_unsigned( 0, 16777215, 16 );
  462. uint16_t val = parse_unsigned( 0, 65535, 16 );
  463. sram_writeshort( val, offset );
  464. }
  465. void cmd_memset( void )
  466. {
  467. uint32_t offset = parse_unsigned( 0, 16777215, 16 );
  468. uint32_t len = parse_unsigned( 0, 16777216, 16 );
  469. uint8_t val = parse_unsigned( 0, 255, 16 );
  470. sram_memset( offset, len, val );
  471. }
  472. /* ------------------------------------------------------------------------- */
  473. /* CLI interface functions */
  474. /* ------------------------------------------------------------------------- */
  475. void cli_init( void )
  476. {
  477. }
  478. void cli_entrycheck()
  479. {
  480. if ( uart_gotc() && uart_getc() == 27 )
  481. {
  482. printf( "*** BREAK\n" );
  483. cli_loop();
  484. }
  485. }
  486. void cli_loop( void )
  487. {
  488. while ( 1 )
  489. {
  490. curchar = getline( ">" );
  491. printf( "\n" );
  492. /* Process medium changes before executing the command */
  493. if ( disk_state != DISK_OK && disk_state != DISK_REMOVED )
  494. {
  495. FRESULT res;
  496. printf( "Medium changed... " );
  497. res = f_mount( 0, &fatfs );
  498. if ( res != FR_OK )
  499. {
  500. printf( "Failed to mount new medium, result %d\n", res );
  501. }
  502. else
  503. {
  504. printf( "Ok\n" );
  505. }
  506. }
  507. /* Remove whitespace */
  508. while ( *curchar == ' ' )
  509. {
  510. curchar++;
  511. }
  512. while ( strlen( curchar ) > 0 && curchar[strlen( curchar ) - 1] == ' ' )
  513. {
  514. curchar[strlen( curchar ) - 1] = 0;
  515. }
  516. /* Ignore empty lines */
  517. if ( strlen( curchar ) == 0 )
  518. {
  519. continue;
  520. }
  521. /* Parse command */
  522. int8_t command = parse_wordlist( command_words );
  523. if ( command < 0 )
  524. {
  525. continue;
  526. }
  527. FRESULT res;
  528. switch ( command )
  529. {
  530. case CMD_CD:
  531. #if _FS_RPATH
  532. if ( strlen( curchar ) == 0 )
  533. {
  534. f_getcwd( ( TCHAR * )file_lfn, 255 );
  535. printf( "%s\n", file_lfn );
  536. break;
  537. }
  538. res = f_chdir( ( const TCHAR * )curchar );
  539. if ( res != FR_OK )
  540. {
  541. printf( "chdir %s failed with result %d\n", curchar, res );
  542. }
  543. else
  544. {
  545. printf( "Ok.\n" );
  546. }
  547. #else
  548. printf( "cd not supported.\n" );
  549. res;
  550. #endif
  551. break;
  552. case CMD_RESET:
  553. cmd_reset();
  554. break;
  555. case CMD_SRESET:
  556. cmd_sreset();
  557. break;
  558. case CMD_DIR:
  559. case CMD_LS:
  560. cmd_show_directory();
  561. break;
  562. case CMD_EXIT:
  563. return;
  564. break;
  565. case CMD_LOADROM:
  566. cmd_loadrom();
  567. break;
  568. case CMD_LOADRAW:
  569. cmd_loadraw();
  570. break;
  571. case CMD_SAVERAW:
  572. cmd_saveraw();
  573. break;
  574. case CMD_RM:
  575. cmd_rm();
  576. break;
  577. case CMD_MKDIR:
  578. cmd_mkdir();
  579. break;
  580. case CMD_D4:
  581. cmd_d4();
  582. break;
  583. case CMD_VMODE:
  584. cmd_vmode();
  585. break;
  586. case CMD_PUT:
  587. cmd_put();
  588. break;
  589. case CMD_MAPPER:
  590. cmd_mapper();
  591. break;
  592. case CMD_SETTIME:
  593. cmd_settime();
  594. break;
  595. case CMD_TIME:
  596. cmd_time();
  597. break;
  598. case CMD_TEST:
  599. testbattery();
  600. break;
  601. case CMD_SETFEATURE:
  602. cmd_setfeature();
  603. break;
  604. case CMD_HEXDUMP:
  605. cmd_hexdump();
  606. break;
  607. case CMD_W8:
  608. cmd_w8();
  609. break;
  610. case CMD_W16:
  611. cmd_w16();
  612. break;
  613. case CMD_MEMSET:
  614. cmd_memset();
  615. break;
  616. case CMD_MEMTEST:
  617. test_mem();
  618. break;
  619. }
  620. }
  621. }