-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.c
More file actions
238 lines (196 loc) · 5.48 KB
/
shell.c
File metadata and controls
238 lines (196 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Copyright (C) 2020
* "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
* Animula is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* Animula is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "shell.h"
static int show_help (int argc, char **argv, vm_t vm);
static int serial_load (int argc, char **argv, vm_t vm);
static int flash_test (int argc, char **argv, vm_t vm);
static int etest (int argc, char **argv, vm_t vm);
static int run_program (int argc, char **argv, vm_t vm);
#define KSC_CNT 10
static const ksc_t kernel_shell_cmd[]
= {{"help", "List all commands", show_help},
{"sload", "Load LEF from serial port", serial_load},
{"ftest", "Flash test", flash_test},
{"etest", "Endian test", etest},
{"run_prog", "Run stored program", run_program},
KSC_END};
static int show_help (int argc, char **argv, vm_t vm)
{
for (int i = 0; i < KSC_CNT; i++)
{
ksc_t ksc = kernel_shell_cmd[i];
if (NULL == ksc.run)
break;
os_printk ("%s - %s\n", ksc.name, ksc.desc);
}
return 0;
}
static int etest (int argc, char **argv, vm_t vm)
{
u32_t n = 0x01234567;
u8_t *ptr = (u8_t *)&n;
os_printk ("start\n");
for (int i = 0; i < 4; i++)
os_printk (" %2x", ptr[i]);
os_printk ("\n");
return 0;
}
static int serial_load (int argc, char **argv, vm_t vm)
{
bool run = false;
bool save = false;
#define SLOAD_HELP() os_printk ("Usage: sload [save | once | run]\n")
if (argc < 2)
{
SLOAD_HELP ();
return 0;
}
else if (!strncmp (argv[1], "save", 5))
{
save = true;
}
else if (!strncmp (argv[1], "once", 5))
{
save = false;
}
else if (!strncmp (argv[1], "run", 4))
{
// save = true;
run = true;
}
else
{
SLOAD_HELP ();
return 0;
}
// Load to RAM
os_printk ("Ready for receiving LEF......\n");
lef_t lef = load_lef_from_uart ();
if (!lef)
{
os_printk ("Invalid LEF file\n");
goto end;
}
if (save)
store_lef (lef, 0);
if (run)
{
uint32_t start_time0;
uint32_t stop_time0;
uint32_t cycles_spent;
uint64_t nanoseconds_spent;
vm_load_lef (vm, lef);
#ifdef ANIMULA_ZEPHYR
start_time0 = k_cycle_get_32 ();
#endif /* ANIMULA_ZEPHYR */
vm_run (vm);
free_lef (lef);
#ifdef ANIMULA_ZEPHYR
stop_time0 = k_cycle_get_32 ();
cycles_spent = stop_time0 - start_time0;
nanoseconds_spent = k_cyc_to_ns_floor64 (cycles_spent);
os_printk ("cycles_spent = %d\n", cycles_spent); // 28800231
os_printk ("nanoseconds_spent = %lld\n", nanoseconds_spent); // 300002406
// affects by CONFIG_SYS_CLOCK_TICKS_PER_SEC
os_printk ("start_time0 = %d ns\n", start_time0); // 910108
os_printk ("stop_time0 = %d ns\n", stop_time0); // 29710362
#endif /* ANIMULA_ZEPHYR */
vm_clean (vm);
vm_init (vm);
}
os_printk ("Free LEF successfully!]\n");
end:
return 0;
}
static int flash_test (int argc, char **argv, vm_t vm)
{
char buf[10] = {0};
int offset = ANIMULA_FLASH_AVAILABLE_OFFSET;
os_printk ("offset: %d\n", offset);
os_flash_write ("hello", offset, 6);
os_flash_read (buf, offset, 6);
os_printk ("flash: %s\n", buf);
return 0;
}
static int run_program (int argc, char **argv, vm_t vm)
{
os_printk ("Loading LEF from flash......\n");
lef_t lef = load_lef_from_flash (0);
vm_load_lef (vm, lef);
if (lef)
{
vm_run (vm);
free_lef (lef);
os_printk ("Free LEF successfully!]\n");
}
return 0;
}
static int run_cmd (char *buf, vm_t vm)
{
int argc = 0;
char *argv[KSC_MAXARGS] = {0};
int i = 0;
// Parse the command buffer into whitespace-separated arguments
argv[argc] = NULL;
while (true)
{
// gobble whitespace
while (*buf && strchr (KSC_WHITESPACE, *buf))
*buf++ = 0;
if (0 == *buf)
break;
// save and scan past next arg
if (KSC_MAXARGS - 1 == argc)
{
os_printk ("Too many arguments (max %d)\n", KSC_MAXARGS);
return 0;
}
argv[argc++] = buf;
while (*buf && !strchr (KSC_WHITESPACE, *buf))
buf++;
}
argv[argc] = 0;
// Lookup and invoke the command
if (0 == argc)
return 0;
for (i = 0; i < KSC_CNT; i++)
{
if (NULL == kernel_shell_cmd[i].run)
break;
else if (0 == strncmp (argv[0], kernel_shell_cmd[i].name, KSC_NAME_LEN))
return kernel_shell_cmd[i].run (argc, argv, vm);
}
os_printk ("Unknown command '%s'\n", argv[0]);
return 0;
}
static void ksc_error_handler (retval_t rv, const char *cmd)
{
os_printk ("retval: %d\n,cmd: %s\n,len-cmd: %d\n", rv, cmd,
(u8_t)os_strnlen (cmd, LINE_BUF_SIZE));
}
void run_shell (vm_t vm)
{
char *cmd = NULL;
retval_t rv = OK;
while (true)
{
cmd = read_line ("^CHIP> ");
if (NULL != cmd && (rv = run_cmd (cmd, vm)) < 0)
{
ksc_error_handler (rv, cmd);
break;
}
}
}