X-Git-Url: https://robinkrens.nl/gitweb/?a=blobdiff_plain;f=term.c;h=d1a643151af420e2d61f450953895aeff4f9438b;hb=b59a54e78d05a5deea9c60ee6416fcc94bca2d71;hp=8b8125584829600ac9daaa63644369970e91e1c9;hpb=41d25ed67df8b6acd6126c41c8b0882586db0b0d;p=cortex-from-scratch diff --git a/term.c b/term.c index 8b81255..d1a6431 100644 --- a/term.c +++ b/term.c @@ -1,82 +1,152 @@ +/* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL + * + * $LOG$ + * 2019/8/14 - ROBIN KRENS + * Initial version + * + * $DESCRIPTION$ + * Small terminal with some built-in debug commands + * + * */ + #include #include #include -#include -#include + +#include +#include + +#include +#include +#include +#include + +#include #define SERIAL 1 -#define BUFSIZE 256 +#define BUFSIZE 200 +#define MAXARGS 5 +#define WHITESPACE "\t\r\n " +#define BUILTINCMDS 4 + +/* + * Built in commands + * info -- shows basic info of system + * uptime -- uptime; read from the RTC register + * reset -- software reset TODO + * showmem xxxxxxxx -- shows address value + * led -- led on/off + * switchmode -- switch to unprivileged mode TODO + * */ static char buf[BUFSIZE]; -/* Abstraction layer for I/O communication */ -char getchar(void) { - char c; - if (SERIAL) { - while ((c = uart_getc()) == 0); - /* do nothing */ +struct cmd { + char * name; + int (*function)(int argc, char ** argsv); +}; + +struct cmd builtincmds[BUILTINCMDS]; + +int info(int argc, char ** argsv) { + sysinfo(); + return 0; +} + +int uptime(int arg, char ** argsv) { + printf("CURRENT UPTIME: %d seconds \n", *RTC_CNTL); +} + +int led(int argc, char ** argsv) { + + if (argsv[1] != NULL) { + if (strcmp(argsv[1], "on")) { + printf("LED ON\n"); + led_on(); + } + else if (strcmp(argsv[1], "off")) { + printf("LED OFF\n"); + led_off(); + } } - return c; + return 0; } -void cputchar(char c) { +int showmem(int argc, char ** argsv) { - if (SERIAL) { - uart_putc(c); + if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) { + + uint32_t * check = (uint32_t *) hextoreg(argsv[1]); + printf("LOCATION 0x%s, VALUE: %#x\n", argsv[1], *check); + return 1; + } + return 0; } -void cputs(unsigned char *str) { - - int i; - for (i = 0; i < strlen(str); i++) { - cputchar(str[i]); - } +int exec_cmd(char * buf) { -} + int argc; + char *argv[MAXARGS]; + int i; + + // Parse the command buffer into whitespace-separated arguments + argc = 0; + argv[argc] = 0; + while (1) { + // gobble whitespace + while (*buf && strchr(WHITESPACE, *buf)) + *buf++ = 0; + if (*buf == 0) + break; + + // save and scan past next arg + if (argc == MAXARGS-1) { + printf("Too many arguments\n"); + return 0; + } + argv[argc++] = buf; + while (*buf && !strchr(WHITESPACE, *buf)) + buf++; + } + argv[argc] = 0; + + // Lookup and invoke the command + if (argc == 0) + return 0; + for (i = 0; i < BUILTINCMDS; i++) { + if (strcmp(argv[0], builtincmds[i].name)) + return builtincmds[i].function(argc, argv); + } + printf("Unknown command\n"); + return 0; + +} void terminal() { - + + builtincmds[0].name = "info"; + builtincmds[0].function = info; + + builtincmds[1].name = "led"; + builtincmds[1].function = led; + + builtincmds[2].name = "showmem"; + builtincmds[2].function = showmem; + + builtincmds[3].name = "uptime"; + builtincmds[3].function = uptime; + + char *buf; - cputs("Terminal running!\n"); while (1) { - buf = readline("> "); - /* if (buf != NULL) - if (runcmd(buf, tf) < 0) - break; */ + buf = readline("root# "); + if (buf != NULL) + if (exec_cmd(buf) < 0) + break; } } -char * readline(char *prompt) -{ - int i, c, echoing; - - if (prompt != NULL) - cputs(prompt); - - i = 0; - echoing = 1; - while (1) { - c = getchar(); - if (c < 0) { - cputs("read error"); - return NULL; - } else if ((c == '\b' || c == '\x7f') && i > 0) { - if (echoing) - cputchar('\b'); - i--; - } else if (c >= ' ' && i < BUFSIZE-1) { - if (echoing) - cputchar(c); - buf[i++] = c; - } else if (c == '\n' || c == '\r') { - if (echoing) - cputchar('\n'); - buf[i] = 0; - return buf; - } - } -}