cli.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 "fpga.h"
  37. #include "fpga_spi.h"
  38. #include "cic.h"
  39. #include "xmodem.h"
  40. #include "rtc.h"
  41. #include "cli.h"
  42. #define MAX_LINE 250
  43. /* Variables */
  44. static char cmdbuffer[MAX_LINE+1];
  45. static char *curchar;
  46. /* Word lists */
  47. static char command_words[] =
  48. "cd\0reset\0sreset\0dir\0ls\0test\0exit\0loadrom\0loadraw\0saveraw\0put\0rm\0mkdir\0d4\0vmode\0mapper\0settime\0time\0setfeature\0hexdump\0w8\0w16\0memset\0";
  49. 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 };
  50. /* ------------------------------------------------------------------------- */
  51. /* Parse functions */
  52. /* ------------------------------------------------------------------------- */
  53. /* Skip spaces at curchar */
  54. static uint8_t skip_spaces(void) {
  55. uint8_t res = (*curchar == ' ' || *curchar == 0);
  56. while (*curchar == ' ')
  57. curchar++;
  58. return res;
  59. }
  60. /* Parse the string in curchar for an integer with bounds [lower,upper] */
  61. static int32_t parse_unsigned(uint32_t lower, uint32_t upper, uint8_t base) {
  62. char *end;
  63. uint32_t result;
  64. if (strlen(curchar) == 1 && *curchar == '?') {
  65. printf("Number between %ld[0x%lx] and %ld[0x%lx] expected\n",lower,lower,upper,upper);
  66. return -2;
  67. }
  68. result = strtoul(curchar, &end, base);
  69. if ((*end != ' ' && *end != 0) || errno != 0) {
  70. printf("Invalid numeric argument\n");
  71. return -1;
  72. }
  73. curchar = end;
  74. skip_spaces();
  75. if (result < lower || result > upper) {
  76. printf("Numeric argument out of range (%ld..%ld)\n",lower,upper);
  77. return -1;
  78. }
  79. return result;
  80. }
  81. /* Parse the string starting with curchar for a word in wordlist */
  82. static int8_t parse_wordlist(char *wordlist) {
  83. uint8_t i, matched;
  84. unsigned char *cur, *ptr;
  85. unsigned char c;
  86. i = 0;
  87. ptr = (unsigned char *)wordlist;
  88. // Command list on "?"
  89. if (strlen(curchar) == 1 && *curchar == '?') {
  90. printf("Commands available: \n ");
  91. while (1) {
  92. c = *ptr++;
  93. if (c == 0) {
  94. if (*ptr == 0) {
  95. printf("\n");
  96. return -2;
  97. } else {
  98. printf("\n ");
  99. }
  100. } else
  101. uart_putc(c);
  102. }
  103. }
  104. while (1) {
  105. cur = (unsigned char *)curchar;
  106. matched = 1;
  107. c = *ptr;
  108. do {
  109. // If current word list character is \0: No match found
  110. if (c == 0) {
  111. printf("Unknown word: %s\n(use ? for help)",curchar);
  112. return -1;
  113. }
  114. if (tolower((int)c) != tolower((int)*cur)) {
  115. // Check for end-of-word
  116. if (cur != (unsigned char*)curchar && (*cur == ' ' || *cur == 0)) {
  117. // Partial match found, return that
  118. break;
  119. } else {
  120. matched = 0;
  121. break;
  122. }
  123. }
  124. ptr++;
  125. cur++;
  126. c = *ptr;
  127. } while (c != 0);
  128. if (matched) {
  129. char *tmp = curchar;
  130. curchar = (char *)cur;
  131. // Return match only if whitespace or end-of-string follows
  132. // (avoids mismatching partial words)
  133. if (skip_spaces()) {
  134. return i;
  135. } else {
  136. printf("Unknown word: %s\n(use ? for help)\n",tmp);
  137. return -1;
  138. }
  139. } else {
  140. // Try next word in list
  141. i++;
  142. while (*ptr++ != 0) ;
  143. }
  144. }
  145. }
  146. /* Read a line from serial, uses cmdbuffer as storage */
  147. static char *getline(char *prompt) {
  148. int i=0;
  149. char c;
  150. printf("\n%s",prompt);
  151. memset(cmdbuffer,0,sizeof(cmdbuffer));
  152. while (1) {
  153. c = uart_getc();
  154. if (c == 13)
  155. break;
  156. if (c == 27 || c == 3) {
  157. printf("\\\n%s",prompt);
  158. i = 0;
  159. memset(cmdbuffer,0,sizeof(cmdbuffer));
  160. continue;
  161. }
  162. if (c == 127 || c == 8) {
  163. if (i > 0) {
  164. i--;
  165. uart_putc(8); // backspace
  166. uart_putc(' '); // erase character
  167. uart_putc(8); // backspace
  168. } else
  169. continue;
  170. } else {
  171. if (i < sizeof(cmdbuffer)-1) {
  172. cmdbuffer[i++] = c;
  173. uart_putc(c);
  174. }
  175. }
  176. }
  177. cmdbuffer[i] = 0;
  178. return cmdbuffer;
  179. }
  180. /* ------------------------------------------------------------------------- */
  181. /* Command functions */
  182. /* ------------------------------------------------------------------------- */
  183. /* Reset */
  184. static void cmd_reset(void) {
  185. /* force watchdog reset */
  186. LPC_WDT->WDTC = 256; // minimal timeout
  187. LPC_WDT->WDCLKSEL = BV(31); // internal RC, lock register
  188. LPC_WDT->WDMOD = BV(0) | BV(1); // enable watchdog and reset-by-watchdog
  189. LPC_WDT->WDFEED = 0xaa;
  190. LPC_WDT->WDFEED = 0x55; // initial feed to really enable WDT
  191. }
  192. /* Show the contents of the current directory */
  193. static void cmd_show_directory(void) {
  194. FRESULT res;
  195. DIR dh;
  196. FILINFO finfo;
  197. uint8_t *name;
  198. f_getcwd((TCHAR*)file_lfn, 255);
  199. res = f_opendir(&dh, (TCHAR*)file_lfn);
  200. if (res != FR_OK) {
  201. printf("f_opendir failed, result %d\n",res);
  202. return;
  203. }
  204. finfo.lfname = (TCHAR*)file_lfn;
  205. finfo.lfsize = 255;
  206. do {
  207. /* Read the next entry */
  208. res = f_readdir(&dh, &finfo);
  209. if (res != FR_OK) {
  210. printf("f_readdir failed, result %d\n",res);
  211. return;
  212. }
  213. /* Abort if none was found */
  214. if (!finfo.fname[0])
  215. break;
  216. /* Skip volume labels */
  217. if (finfo.fattrib & AM_VOL)
  218. continue;
  219. /* Select between LFN and 8.3 name */
  220. if (finfo.lfname[0])
  221. name = (uint8_t*)finfo.lfname;
  222. else {
  223. name = (uint8_t*)finfo.fname;
  224. strlwr((char *)name);
  225. }
  226. printf("%s [%s] (%ld)",finfo.lfname, finfo.fname, finfo.fsize);
  227. /* Directory indicator (Unix-style) */
  228. if (finfo.fattrib & AM_DIR)
  229. uart_putc('/');
  230. printf("\n");
  231. } while (finfo.fname[0]);
  232. }
  233. static void cmd_loadrom(void) {
  234. uint32_t address = 0;
  235. uint8_t flags = LOADROM_WITH_SRAM | LOADROM_WITH_RESET;
  236. load_rom((uint8_t*)curchar, address, flags);
  237. }
  238. static void cmd_loadraw(void) {
  239. uint32_t address = parse_unsigned(0,16777216,16);
  240. load_sram((uint8_t*)curchar, address);
  241. }
  242. static void cmd_saveraw(void) {
  243. uint32_t address = parse_unsigned(0,16777216,16);
  244. uint32_t length = parse_unsigned(0,16777216,16);
  245. if(address != -1 && length != -1)
  246. save_sram((uint8_t*)curchar, length, address);
  247. }
  248. static void cmd_d4(void) {
  249. int32_t hz;
  250. if(get_cic_state() != CIC_PAIR) {
  251. printf("not in pair mode\n");
  252. } else {
  253. hz = parse_unsigned(50,60,10);
  254. if(hz==50) {
  255. cic_d4(CIC_PAL);
  256. } else {
  257. cic_d4(CIC_NTSC);
  258. }
  259. printf("ok\n");
  260. }
  261. }
  262. static void cmd_vmode(void) {
  263. int32_t hz;
  264. if(get_cic_state() != CIC_PAIR) {
  265. printf("not in pair mode\n");
  266. } else {
  267. hz = parse_unsigned(50,60,10);
  268. if(hz==50) {
  269. cic_videomode(CIC_PAL);
  270. } else {
  271. cic_videomode(CIC_NTSC);
  272. }
  273. printf("ok\n");
  274. }
  275. }
  276. void cmd_put(void) {
  277. if(*curchar != 0) {
  278. file_open((uint8_t*)curchar, FA_CREATE_ALWAYS | FA_WRITE);
  279. if(file_res) {
  280. printf("FAIL: error opening file %s\n", curchar);
  281. } else {
  282. printf("OK, start xmodem transfer now.\n");
  283. xmodem_rxfile(&file_handle);
  284. }
  285. file_close();
  286. } else {
  287. printf("Usage: put <filename>\n");
  288. }
  289. }
  290. void cmd_rm(void) {
  291. FRESULT res = f_unlink(curchar);
  292. if(res) printf("Error %d removing %s\n", res, curchar);
  293. }
  294. void cmd_mkdir(void) {
  295. FRESULT res = f_mkdir(curchar);
  296. if(res) printf("Error %d creating directory %s\n", res, curchar);
  297. }
  298. void cmd_mapper(void) {
  299. int32_t mapper;
  300. mapper = parse_unsigned(0,7,10);
  301. set_mapper((uint8_t)mapper & 0x7);
  302. printf("mapper set to %ld\n", mapper);
  303. }
  304. void cmd_sreset(void) {
  305. if(*curchar != 0) {
  306. int32_t resetstate;
  307. resetstate = parse_unsigned(0,1,10);
  308. snes_reset(resetstate);
  309. } else {
  310. snes_reset_pulse();
  311. }
  312. }
  313. void cmd_settime(void) {
  314. struct tm time;
  315. if(strlen(curchar) != 4+2+2 + 2+2+2) {
  316. printf("invalid time format (need YYYYMMDDhhmmss)\n");
  317. } else {
  318. time.tm_sec = atoi(curchar+4+2+2+2+2);
  319. curchar[4+2+2+2+2] = 0;
  320. time.tm_min = atoi(curchar+4+2+2+2);
  321. curchar[4+2+2+2] = 0;
  322. time.tm_hour = atoi(curchar+4+2+2);
  323. curchar[4+2+2] = 0;
  324. time.tm_mday = atoi(curchar+4+2);
  325. curchar[4+2] = 0;
  326. time.tm_mon = atoi(curchar+4);
  327. curchar[4] = 0;
  328. time.tm_year = atoi(curchar);
  329. set_rtc(&time);
  330. }
  331. }
  332. void cmd_time(void) {
  333. struct tm time;
  334. read_rtc(&time);
  335. printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon,
  336. time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
  337. }
  338. void cmd_setfeature(void) {
  339. uint8_t feat = parse_unsigned(0, 255, 16);
  340. fpga_set_features(feat);
  341. }
  342. void cmd_hexdump(void) {
  343. uint32_t offset = parse_unsigned(0, 16777215, 16);
  344. uint32_t len = parse_unsigned(0, 16777216, 16);
  345. sram_hexdump(offset, len);
  346. }
  347. void cmd_w8(void) {
  348. uint32_t offset = parse_unsigned(0, 16777215, 16);
  349. uint8_t val = parse_unsigned(0, 255, 16);
  350. sram_writebyte(val, offset);
  351. }
  352. void cmd_w16(void) {
  353. uint32_t offset = parse_unsigned(0, 16777215, 16);
  354. uint16_t val = parse_unsigned(0, 65535, 16);
  355. sram_writeshort(val, offset);
  356. }
  357. void cmd_memset(void) {
  358. uint32_t offset = parse_unsigned(0, 16777215, 16);
  359. uint32_t len = parse_unsigned(0, 16777216, 16);
  360. uint8_t val = parse_unsigned(0, 255, 16);
  361. sram_memset(offset, len, val);
  362. }
  363. /* ------------------------------------------------------------------------- */
  364. /* CLI interface functions */
  365. /* ------------------------------------------------------------------------- */
  366. void cli_init(void) {
  367. }
  368. void cli_entrycheck() {
  369. if(uart_gotc() && uart_getc() == 27) {
  370. printf("*** BREAK\n");
  371. cli_loop();
  372. }
  373. }
  374. void cli_loop(void) {
  375. while (1) {
  376. curchar = getline(">");
  377. printf("\n");
  378. /* Process medium changes before executing the command */
  379. if (disk_state != DISK_OK && disk_state != DISK_REMOVED) {
  380. FRESULT res;
  381. printf("Medium changed... ");
  382. res = f_mount(0,&fatfs);
  383. if (res != FR_OK) {
  384. printf("Failed to mount new medium, result %d\n",res);
  385. } else {
  386. printf("Ok\n");
  387. }
  388. }
  389. /* Remove whitespace */
  390. while (*curchar == ' ') curchar++;
  391. while (strlen(curchar) > 0 && curchar[strlen(curchar)-1] == ' ')
  392. curchar[strlen(curchar)-1] = 0;
  393. /* Ignore empty lines */
  394. if (strlen(curchar) == 0)
  395. continue;
  396. /* Parse command */
  397. int8_t command = parse_wordlist(command_words);
  398. if (command < 0)
  399. continue;
  400. FRESULT res;
  401. switch (command) {
  402. case CMD_CD:
  403. #if _FS_RPATH
  404. if (strlen(curchar) == 0) {
  405. f_getcwd((TCHAR*)file_lfn, 255);
  406. printf("%s\n",file_lfn);
  407. break;
  408. }
  409. res = f_chdir((const TCHAR *)curchar);
  410. if (res != FR_OK) {
  411. printf("chdir %s failed with result %d\n",curchar,res);
  412. } else {
  413. printf("Ok.\n");
  414. }
  415. #else
  416. printf("cd not supported.\n");
  417. res;
  418. #endif
  419. break;
  420. case CMD_RESET:
  421. cmd_reset();
  422. break;
  423. case CMD_SRESET:
  424. cmd_sreset();
  425. break;
  426. case CMD_DIR:
  427. case CMD_LS:
  428. cmd_show_directory();
  429. break;
  430. case CMD_EXIT:
  431. return;
  432. break;
  433. case CMD_LOADROM:
  434. cmd_loadrom();
  435. break;
  436. case CMD_LOADRAW:
  437. cmd_loadraw();
  438. break;
  439. case CMD_SAVERAW:
  440. cmd_saveraw();
  441. break;
  442. case CMD_RM:
  443. cmd_rm();
  444. break;
  445. case CMD_MKDIR:
  446. cmd_mkdir();
  447. break;
  448. case CMD_D4:
  449. cmd_d4();
  450. break;
  451. case CMD_VMODE:
  452. cmd_vmode();
  453. break;
  454. case CMD_PUT:
  455. cmd_put();
  456. break;
  457. case CMD_MAPPER:
  458. cmd_mapper();
  459. break;
  460. case CMD_SETTIME:
  461. cmd_settime();
  462. break;
  463. case CMD_TIME:
  464. cmd_time();
  465. break;
  466. case CMD_TEST:
  467. testbattery();
  468. break;
  469. case CMD_SETFEATURE:
  470. cmd_setfeature();
  471. break;
  472. case CMD_HEXDUMP:
  473. cmd_hexdump();
  474. break;
  475. case CMD_W8:
  476. cmd_w8();
  477. break;
  478. case CMD_W16:
  479. cmd_w16();
  480. break;
  481. case CMD_MEMSET:
  482. cmd_memset();
  483. break;
  484. }
  485. }
  486. }