basic heap implementation
[cortex-from-scratch] / term.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/8/14 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Small terminal with some built-in debug commands
9  * 
10  * */
11
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include <sys/robsys.h>
17 #include <sys/mmap.h>
18
19 #include <lib/stdio.h>
20 #include <lib/string.h>
21 #include <lib/regfunc.h>
22 #include <lib/tinyprintf.h>
23
24 #include <lib/pool.h>
25
26 #include <drivers/led.h>
27
28 #define SERIAL 1
29 #define BUFSIZE 200
30 #define MAXARGS 5
31 #define WHITESPACE "\t\r\n "
32 #define BUILTINCMDS 4
33
34 /* 
35  * Built in commands
36  *      info -- shows basic info of system
37  *      uptime -- uptime; read from the RTC register
38  *      reset -- software reset TODO
39  *      showmem xxxxxxxx -- shows address value
40  *      led -- led on/off
41  *      switchmode -- switch to unprivileged mode TODO
42  * */
43
44 static char buf[BUFSIZE];
45
46 struct cmd {
47         char * name;
48         int (*function)(int argc, char ** argsv);
49 };
50
51 struct cmd builtincmds[BUILTINCMDS];
52
53 int info(int argc, char ** argsv) {
54         sysinfo();
55         return 0;
56 }
57
58 int uptime(int arg, char ** argsv) {
59         printf("CURRENT UPTIME: %d seconds \n", *RTC_CNTL);
60 }
61
62 int led(int argc, char ** argsv) {
63
64         if (argsv[1] != NULL) {
65                 if (strcmp(argsv[1], "on")) {
66                         printf("LED ON\n");
67                         led_on();
68                         }
69                 else if (strcmp(argsv[1], "off")) {
70                         printf("LED OFF\n");
71                         led_off();
72                         }
73         }
74         return 0;
75 }
76
77 int showmem(int argc, char ** argsv) {
78
79         if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) {
80         
81                 uint32_t * check = (uint32_t *) hextoreg(argsv[1]);
82                 printf("LOCATION 0x%s, VALUE: %#x\n", argsv[1], *check);
83                 return 1;
84         
85         }
86
87         extern mem_pool_t kheap_pool;
88         int * a = kalloc(&kheap_pool);
89         *a = argc;
90         printf("%d\n", *a);
91         printf("%p\n", a);
92
93         return 0;
94 }
95
96 int exec_cmd(char * buf) {
97
98         int argc;
99         char *argv[MAXARGS];
100         int i;
101
102         // Parse the command buffer into whitespace-separated arguments
103         argc = 0;
104         argv[argc] = 0;
105         while (1) {
106                 // gobble whitespace
107                 while (*buf && strchr(WHITESPACE, *buf))
108                         *buf++ = 0;
109                 if (*buf == 0)
110                         break;
111
112                 // save and scan past next arg
113                 if (argc == MAXARGS-1) {
114                         printf("Too many arguments\n");
115                         return 0;
116                 }
117                 argv[argc++] = buf;
118                 while (*buf && !strchr(WHITESPACE, *buf))
119                         buf++;
120         }
121         argv[argc] = 0;
122
123         // Lookup and invoke the command
124         if (argc == 0)
125                 return 0;
126         for (i = 0; i < BUILTINCMDS; i++) {
127                 if (strcmp(argv[0], builtincmds[i].name))
128                         return builtincmds[i].function(argc, argv);
129         }
130         printf("Unknown command\n");
131         return 0;
132
133
134
135 void terminal() {
136
137         builtincmds[0].name = "info";
138         builtincmds[0].function = info;
139
140         builtincmds[1].name = "led";
141         builtincmds[1].function = led;
142
143         builtincmds[2].name = "showmem";
144         builtincmds[2].function = showmem;
145
146         builtincmds[3].name = "uptime";
147         builtincmds[3].function = uptime;
148
149
150         char *buf;
151  
152          while (1) {
153                  buf = readline("root# ");
154                         if (buf != NULL)
155                          if (exec_cmd(buf) < 0)
156                                  break;
157          }
158 }
159