cli.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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\0resume\0loadrom\0loadraw\0saveraw\0put\0rm\0d4\0vmode\0mapper\0settime\0time\0setfeature\0hexdump\0w8\0w16\0";
  49. enum { CMD_CD = 0, CMD_RESET, CMD_SRESET, CMD_DIR, CMD_LS, CMD_TEST, CMD_RESUME, CMD_LOADROM, CMD_LOADRAW, CMD_SAVERAW, CMD_PUT, CMD_RM, CMD_D4, CMD_VMODE, CMD_MAPPER, CMD_SETTIME, CMD_TIME, CMD_SETFEATURE, CMD_HEXDUMP, CMD_W8, CMD_W16 };
  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. char *cur, *ptr;
  85. char c;
  86. i = 0;
  87. ptr = 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 = 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(c) != tolower(*cur)) {
  115. // Check for end-of-word
  116. if (cur != 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 = 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",name);
  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_mapper(void) {
  295. int32_t mapper;
  296. mapper = parse_unsigned(0,7,10);
  297. set_mapper((uint8_t)mapper & 0x7);
  298. printf("mapper set to %ld\n", mapper);
  299. }
  300. void cmd_sreset(void) {
  301. if(*curchar != 0) {
  302. int32_t resetstate;
  303. resetstate = parse_unsigned(0,1,10);
  304. snes_reset(resetstate);
  305. } else {
  306. snes_reset(1);
  307. delay_ms(20);
  308. snes_reset(0);
  309. }
  310. }
  311. void cmd_settime(void) {
  312. struct tm time;
  313. if(strlen(curchar) != 4+2+2 + 2+2+2) {
  314. printf("invalid time format (need YYYYMMDDhhmmss)\n");
  315. } else {
  316. time.tm_sec = atoi(curchar+4+2+2+2+2);
  317. curchar[4+2+2+2+2] = 0;
  318. time.tm_min = atoi(curchar+4+2+2+2);
  319. curchar[4+2+2+2] = 0;
  320. time.tm_hour = atoi(curchar+4+2+2);
  321. curchar[4+2+2] = 0;
  322. time.tm_mday = atoi(curchar+4+2);
  323. curchar[4+2] = 0;
  324. time.tm_mon = atoi(curchar+4);
  325. curchar[4] = 0;
  326. time.tm_year = atoi(curchar);
  327. set_rtc(&time);
  328. }
  329. }
  330. void cmd_time(void) {
  331. struct tm time;
  332. read_rtc(&time);
  333. printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon,
  334. time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
  335. }
  336. void cmd_setfeature(void) {
  337. uint8_t feat = parse_unsigned(0, 255, 16);
  338. fpga_set_features(feat);
  339. }
  340. void cmd_hexdump(void) {
  341. uint32_t offset = parse_unsigned(0, 16777215, 16);
  342. uint32_t len = parse_unsigned(0, 16777216, 16);
  343. sram_hexdump(offset, len);
  344. }
  345. void cmd_w8(void) {
  346. uint32_t offset = parse_unsigned(0, 16777215, 16);
  347. uint8_t val = parse_unsigned(0, 255, 16);
  348. sram_writebyte(val, offset);
  349. }
  350. void cmd_w16(void) {
  351. uint32_t offset = parse_unsigned(0, 16777215, 16);
  352. uint16_t val = parse_unsigned(0, 65535, 16);
  353. sram_writeshort(val, offset);
  354. }
  355. /* ------------------------------------------------------------------------- */
  356. /* CLI interface functions */
  357. /* ------------------------------------------------------------------------- */
  358. void cli_init(void) {
  359. }
  360. void cli_entrycheck() {
  361. if(uart_gotc() && uart_getc() == 27) {
  362. printf("*** BREAK\n");
  363. cli_loop();
  364. }
  365. }
  366. void cli_loop(void) {
  367. while (1) {
  368. curchar = getline(">");
  369. printf("\n");
  370. /* Process medium changes before executing the command */
  371. if (disk_state != DISK_OK && disk_state != DISK_REMOVED) {
  372. FRESULT res;
  373. printf("Medium changed... ");
  374. res = f_mount(0,&fatfs);
  375. if (res != FR_OK) {
  376. printf("Failed to mount new medium, result %d\n",res);
  377. } else {
  378. printf("Ok\n");
  379. }
  380. }
  381. /* Remove whitespace */
  382. while (*curchar == ' ') curchar++;
  383. while (strlen(curchar) > 0 && curchar[strlen(curchar)-1] == ' ')
  384. curchar[strlen(curchar)-1] = 0;
  385. /* Ignore empty lines */
  386. if (strlen(curchar) == 0)
  387. continue;
  388. /* Parse command */
  389. int8_t command = parse_wordlist(command_words);
  390. if (command < 0)
  391. continue;
  392. FRESULT res;
  393. switch (command) {
  394. case CMD_CD:
  395. #if _FS_RPATH
  396. if (strlen(curchar) == 0) {
  397. f_getcwd((TCHAR*)file_lfn, 255);
  398. printf("%s\n",file_lfn);
  399. break;
  400. }
  401. res = f_chdir((const TCHAR *)curchar);
  402. if (res != FR_OK) {
  403. printf("chdir %s failed with result %d\n",curchar,res);
  404. } else {
  405. printf("Ok.\n");
  406. }
  407. #else
  408. printf("cd not supported.\n");
  409. res;
  410. #endif
  411. break;
  412. case CMD_RESET:
  413. cmd_reset();
  414. break;
  415. case CMD_SRESET:
  416. cmd_sreset();
  417. break;
  418. case CMD_DIR:
  419. case CMD_LS:
  420. cmd_show_directory();
  421. break;
  422. case CMD_RESUME:
  423. return;
  424. break;
  425. case CMD_LOADROM:
  426. cmd_loadrom();
  427. break;
  428. case CMD_LOADRAW:
  429. cmd_loadraw();
  430. break;
  431. case CMD_SAVERAW:
  432. cmd_saveraw();
  433. break;
  434. case CMD_RM:
  435. cmd_rm();
  436. break;
  437. case CMD_D4:
  438. cmd_d4();
  439. break;
  440. case CMD_VMODE:
  441. cmd_vmode();
  442. break;
  443. case CMD_PUT:
  444. cmd_put();
  445. break;
  446. case CMD_MAPPER:
  447. cmd_mapper();
  448. break;
  449. case CMD_SETTIME:
  450. cmd_settime();
  451. break;
  452. case CMD_TIME:
  453. cmd_time();
  454. break;
  455. case CMD_TEST:
  456. testbattery();
  457. break;
  458. case CMD_SETFEATURE:
  459. cmd_setfeature();
  460. break;
  461. case CMD_HEXDUMP:
  462. cmd_hexdump();
  463. break;
  464. case CMD_W8:
  465. cmd_w8();
  466. break;
  467. case CMD_W16:
  468. cmd_w16();
  469. break;
  470. }
  471. }
  472. }