cli.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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\0d4\0vmode\0mapper\0settime\0time\0setfeature\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_D4, CMD_VMODE, CMD_MAPPER, CMD_SETTIME, CMD_TIME, CMD_SETFEATURE };
  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) {
  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, 10);
  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",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. set_mcu_ovr(1);
  237. load_rom((uint8_t*)curchar, address, flags);
  238. set_mcu_ovr(0);
  239. }
  240. static void cmd_saveraw(void) {
  241. uint32_t address = parse_unsigned(0,16777216);
  242. uint32_t length = parse_unsigned(0,16777216);
  243. set_mcu_ovr(0);
  244. save_sram((uint8_t*)curchar, length, address);
  245. }
  246. static void cmd_d4(void) {
  247. int32_t hz;
  248. if(get_cic_state() != CIC_PAIR) {
  249. printf("not in pair mode\n");
  250. } else {
  251. hz = parse_unsigned(50,60);
  252. if(hz==50) {
  253. cic_d4(CIC_PAL);
  254. } else {
  255. cic_d4(CIC_NTSC);
  256. }
  257. printf("ok\n");
  258. }
  259. }
  260. static void cmd_vmode(void) {
  261. int32_t hz;
  262. if(get_cic_state() != CIC_PAIR) {
  263. printf("not in pair mode\n");
  264. } else {
  265. hz = parse_unsigned(50,60);
  266. if(hz==50) {
  267. cic_videomode(CIC_PAL);
  268. } else {
  269. cic_videomode(CIC_NTSC);
  270. }
  271. printf("ok\n");
  272. }
  273. }
  274. void cmd_put(void) {
  275. if(*curchar != 0) {
  276. file_open((uint8_t*)curchar, FA_CREATE_ALWAYS | FA_WRITE);
  277. if(file_res) {
  278. printf("FAIL: error opening file %s\n", curchar);
  279. } else {
  280. printf("OK, start xmodem transfer now.\n");
  281. xmodem_rxfile(&file_handle);
  282. }
  283. file_close();
  284. } else {
  285. printf("Usage: put <filename>\n");
  286. }
  287. }
  288. void cmd_mapper(void) {
  289. int32_t mapper;
  290. mapper = parse_unsigned(0,7);
  291. set_mapper((uint8_t)mapper & 0x7);
  292. printf("mapper set to %ld\n", mapper);
  293. }
  294. void cmd_sreset(void) {
  295. if(*curchar != 0) {
  296. int32_t resetstate;
  297. resetstate = parse_unsigned(0,1);
  298. snes_reset(resetstate);
  299. } else {
  300. snes_reset(1);
  301. delay_ms(20);
  302. snes_reset(0);
  303. }
  304. }
  305. void cmd_settime(void) {
  306. struct tm time;
  307. if(strlen(curchar) != 4+2+2 + 2+2+2) {
  308. printf("invalid time format (need YYYYMMDDhhmmss)\n");
  309. } else {
  310. time.tm_sec = atoi(curchar+4+2+2+2+2);
  311. curchar[4+2+2+2+2] = 0;
  312. time.tm_min = atoi(curchar+4+2+2+2);
  313. curchar[4+2+2+2] = 0;
  314. time.tm_hour = atoi(curchar+4+2+2);
  315. curchar[4+2+2] = 0;
  316. time.tm_mday = atoi(curchar+4+2);
  317. curchar[4+2] = 0;
  318. time.tm_mon = atoi(curchar+4);
  319. curchar[4] = 0;
  320. time.tm_year = atoi(curchar);
  321. set_rtc(&time);
  322. }
  323. }
  324. void cmd_time(void) {
  325. struct tm time;
  326. read_rtc(&time);
  327. printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon,
  328. time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
  329. }
  330. void cmd_setfeature(void) {
  331. uint8_t feat = parse_unsigned(0, 255);
  332. fpga_set_features(feat);
  333. }
  334. /* ------------------------------------------------------------------------- */
  335. /* CLI interface functions */
  336. /* ------------------------------------------------------------------------- */
  337. void cli_init(void) {
  338. }
  339. void cli_entrycheck() {
  340. if(uart_gotc() && uart_getc() == 27) {
  341. printf("*** BREAK\n");
  342. cli_loop();
  343. }
  344. }
  345. void cli_loop(void) {
  346. while (1) {
  347. curchar = getline(">");
  348. printf("\n");
  349. /* Process medium changes before executing the command */
  350. if (disk_state != DISK_OK && disk_state != DISK_REMOVED) {
  351. FRESULT res;
  352. printf("Medium changed... ");
  353. res = f_mount(0,&fatfs);
  354. if (res != FR_OK) {
  355. printf("Failed to mount new medium, result %d\n",res);
  356. } else {
  357. printf("Ok\n");
  358. }
  359. }
  360. /* Remove whitespace */
  361. while (*curchar == ' ') curchar++;
  362. while (strlen(curchar) > 0 && curchar[strlen(curchar)-1] == ' ')
  363. curchar[strlen(curchar)-1] = 0;
  364. /* Ignore empty lines */
  365. if (strlen(curchar) == 0)
  366. continue;
  367. /* Parse command */
  368. int8_t command = parse_wordlist(command_words);
  369. if (command < 0)
  370. continue;
  371. FRESULT res;
  372. switch (command) {
  373. case CMD_CD:
  374. #if _FS_RPATH
  375. if (strlen(curchar) == 0) {
  376. f_getcwd((TCHAR*)file_lfn, 255);
  377. printf("%s\n",file_lfn);
  378. break;
  379. }
  380. res = f_chdir((const TCHAR *)curchar);
  381. if (res != FR_OK) {
  382. printf("chdir %s failed with result %d\n",curchar,res);
  383. } else {
  384. printf("Ok.\n");
  385. }
  386. #else
  387. printf("cd not supported.\n");
  388. res;
  389. #endif
  390. break;
  391. case CMD_RESET:
  392. cmd_reset();
  393. break;
  394. case CMD_SRESET:
  395. cmd_sreset();
  396. break;
  397. case CMD_DIR:
  398. case CMD_LS:
  399. cmd_show_directory();
  400. break;
  401. case CMD_RESUME:
  402. return;
  403. break;
  404. case CMD_LOADROM:
  405. cmd_loadrom();
  406. break;
  407. case CMD_SAVERAW:
  408. cmd_saveraw();
  409. break;
  410. case CMD_D4:
  411. cmd_d4();
  412. break;
  413. case CMD_VMODE:
  414. cmd_vmode();
  415. break;
  416. case CMD_PUT:
  417. cmd_put();
  418. break;
  419. case CMD_MAPPER:
  420. cmd_mapper();
  421. break;
  422. case CMD_SETTIME:
  423. cmd_settime();
  424. break;
  425. case CMD_TIME:
  426. cmd_time();
  427. break;
  428. case CMD_TEST:
  429. testbattery();
  430. break;
  431. case CMD_SETFEATURE:
  432. cmd_setfeature();
  433. break;
  434. }
  435. }
  436. }