basic heap implementation
[cortex-from-scratch] / term.c
diff --git a/term.c b/term.c
index 0628e1e..92017e6 100644 (file)
--- a/term.c
+++ b/term.c
@@ -1,3 +1,14 @@
+/* (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 <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/mmap.h>
 
 #include <lib/stdio.h>
+#include <lib/string.h>
+#include <lib/regfunc.h>
+#include <lib/tinyprintf.h>
 
-#define SERIAL 1
-#define BUFSIZE 200
+#include <lib/pool.h>
 
+#include <drivers/led.h>
 
-int help(int, char**);
+#define SERIAL 1
+#define BUFSIZE 200
+#define MAXARGS 5
+#define WHITESPACE "\t\r\n "
+#define BUILTINCMDS 4
 
 /* 
  * Built in commands
  *     info -- shows basic info of system
- *     reset -- software reset
- *     show [ADDRESS-ADDRESS] -- shows SRAM range
- *     switchmode -- switch to unprivileged mode
+ *     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];
 
-
 struct cmd {
        char * name;
-       char * description;
        int (*function)(int argc, char ** argsv);
 };
 
-static struct cmd builtin[] = 
-       { "info", "show info", help};
+struct cmd builtincmds[BUILTINCMDS];
 
-int help(int argc, char ** argsv) {
+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 0;
+}
+
+int showmem(int argc, char ** argsv) {
+
+       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;
+       
+       }
+
+       extern mem_pool_t kheap_pool;
+       int * a = kalloc(&kheap_pool);
+       *a = argc;
+       printf("%d\n", *a);
+       printf("%p\n", a);
+
+       return 0;
+}
+
+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("root# ");
-                 /* if (buf != NULL)
-                         if (runcmd(buf, tf) < 0)
-                                 break; */
+                       if (buf != NULL)
+                         if (exec_cmd(buf) < 0)
+                                 break;
          }
 }
+