-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsole.c
252 lines (230 loc) · 6.32 KB
/
Console.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*****************************************************************************
* Project Name: CKOS
* Authors: Casey Artner & Kenny Deardorff
* Class: CST 352 - Operating Systems
* File: Console.c - CKOS Shell Console
*****************************************************************************/
#include "serial.h"
#include "ports_d256.h"
#include "CKLib.h"
#include "Console.h"
#include "Threads.h"
#include "MemMgr.h"
//MemMgr Test Variables
char * point = 0;
char * point2 = 0;
char * point3 = 0;
/*****************************************************************************
* Function Name: InitConsole
* Params:
* void
* Returns:
* void
* Discription:
* Initializes CKOS console and displays version info
*****************************************************************************/
void InitConsole()
{
puts("\n\nCKOS Version ");
puts(CKOS_VERSION);
puts("\n");
puts("Stage: ");
puts(CKOS_STAGE);
puts("\n");
}
/*****************************************************************************
* Function Name: RunConsole
* Params:
* void
* Returns:
* void
* Discription:
* Prompts user for input and runs commands
*****************************************************************************/
void RunConsole()
{
BOOL run = TRUE;
unsigned char buffer[C_BUFFER_MAX];
while(1)
{
//Display Prompt
puts("\n[CKOS]: ");
//Get input string
GetLine(buffer);
puts("\n");
//Run command
if(RunCommand(buffer))
run = FALSE;
//Yield();
}
}
/*****************************************************************************
* Function Name: Run
* Params:
* char * buffer - Command string entered by user
* Returns:
* char - TRUE if the user signaled to close the console, FALSE otherwise
* Discription:
* Runs the command string entered by the user
*****************************************************************************/
char RunCommand(unsigned char * buffer)
{
unsigned char cmd[C_BUFFER_MAX] = "";
unsigned char args[C_BUFFER_MAX] = "";
//Split the entered string into command and argument strings
SplitCommand(buffer, cmd, args);
//Checks for known commands
if(!SStrcmp(cmd, "help"))
{
puts("[Console Commands]\n");
puts("hello - HELLO WORLD!\n");
puts("echo [string] - Prints [string] back.\n");
puts("memdump - Shows all memory.\n");
puts("malloc [1-3] - Allocate memory and show dump\n");
puts("memset [1-3] - Sets allocated memory\n");
puts("free [1-3] - Free memory and show dump\n");
puts("end - Terminates the console.\n");
}
else if(!SStrcmp(cmd, "hello"))
{
puts("HELLO WORLD!\n");
}
else if(!SStrcmp(cmd, "echo"))
{
puts(UCtoC(args));
putch('\n');
}
else if(!SStrcmp(cmd, "memdump"))
{
MemDump(0, 16);
}
else if(!SStrcmp(cmd, "malloc"))
{
if(!SStrcmp(args, "1"))
point = Malloc(8);
else if(!SStrcmp(args, "2"))
point2 = Malloc(11);
else if(!SStrcmp(args, "3"))
point3 = Malloc(16);
MemDump(0, 16);
}
else if(!SStrcmp(cmd, "free"))
{
if(!SStrcmp(args, "1"))
Free(point);
else if(!SStrcmp(args, "2"))
Free(point2);
else if(!SStrcmp(args, "3"))
Free(point3);
MemDump(0, 16);
}
else if(!SStrcmp(cmd, "memset"))
{
if(!SStrcmp(args, "1"))
Strcpy("8 Bytes", point);
else if(!SStrcmp(args, "2"))
Strcpy("11Bytes", point2);
else if(!SStrcmp(args, "3"))
Strcpy("16Bytes", point3);
MemDump(0, 16);
}
else if(!SStrcmp(cmd, "end"))
{
return TRUE;
}
else
{
puts("Unknown Command - Type 'help' for a list of commands\n");
}
return FALSE;
}
/*****************************************************************************
* Function Name: SplitCommand
* Params:
* char * src - Source command string
* char * cmd - String to store the command string
* char * args - String to store the argument string
* Returns:
* void
* Discription:
* Puts everything before the first space in [cmd] and everything after in [args]
*****************************************************************************/
void SplitCommand(unsigned char * src, unsigned char * cmd, unsigned char * args)
{
int i;
for(i = 0; src[i]; i++)
{
if(src[i] == ' ')
{
int j;
i++;
for(j = 0; src[i]; i++, j++)
{
args[j] = src[i];
args[j + 1] = '\0';
}
}
else
{
cmd[i] = src[i];
cmd[i + 1] = '\0';
}
}
}
/*****************************************************************************
* Function Name: GetLine
* Params:
* char * buffer - Buffer to store user input
* Returns:
* void
* Discription:
* Waits for user input which it displays to console and adds to buffer
*****************************************************************************/
void GetLine(unsigned char * buffer)
{
int i = 0;
while(i < (C_BUFFER_MAX - 1))
{
//Wait for input character
char tmp = GetChar();
//Check input character
switch(tmp)
{
case '\n':
case '\r':
buffer[i] = '\0';
putch('\n');
return;
case '\b':
if(i > 0)
{
i--;
putch(tmp);
}
buffer[i] = '\0';
break;
default:
buffer[i] = tmp;
buffer[i + 1] = '\0';
putch(tmp);
i++;
break;
}
}
buffer[C_BUFFER_MAX - 1] = '\0';
putch('\n');
}
/*****************************************************************************
* Function Name: GetChar
* Params:
* void
* Returns:
* char - A single user entered char
* Discription:
* Waits for user input to be ready and returns entered character
*****************************************************************************/
char GetChar()
{
while(!(SC0SR1 & 0x20));
return SC0DRL;
}