-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_utils.c
130 lines (111 loc) · 3.17 KB
/
user_utils.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
const char *ECHO_COMMAND = "echo\0";
const char *WRITE_MEMORY_COMMAND = "wmem\0";
const char *READ_MEMORY_COMMAND = "rmem\0";
const char *HELP_MEMORY_COMMAND1 = "?\0";
const char *HELP_MEMORY_COMMAND2 = "help\0";
void print(const char *str);
char *base_pointer = 0x00000000;
void print_newline(void);
int get_first_pos(char* str, char ch) {
int i = 0;
while (str[i] != ch) {
i++;
}
return i;
}
void buf_cpy(char *from, char *to, int start, int end) {
int current = start;
int i = 0;
while (current < end) {
to[i++] = from[current++];
}
to[i] = '\0';
}
int compare_str(const char *str1, const char *str2) {
int i = 0;
while (str1[i] != '\0') {
if (str1[i] != str2[i]) {
return 0;
}
i++;
}
return 1;
}
unsigned int str_to_int (char *str){
unsigned int temp = 0;
int i = 0;
while (str[i] >= 0x30 && str[i] <= 0x39) {
temp = temp + (str[i] & 0x0F);
temp = temp * 10;
i++;
}
temp = temp / 10;
return (temp);
}
char* int_to_str(unsigned char i) {
static char buf[255];
char *p = buf + 255;
int sign;
if ((sign = i) < 0) /* записываем знак */
i = -i; /* делаем i положительным числом */
do {
*--p = '0' + (i % 10);
} while ((i /= 10) > 0);
if (sign < 0)
*--p = '-';
return p;
}
void echo_cmd(char *buf_ptr) {
print(buf_ptr);
print_newline();
}
void help_cmd() {
const char* echo = "echo [str] - print str to console\n";
const char* rmem = "rmem [addr] - read a byte from the memory\n";
const char* wmem = "wmem [addr] [byte] - write a byte to the memory";
print(echo);
print(rmem);
print(wmem);
print_newline();
}
char* write_to_mem(char* ptr, char *str) {
unsigned int address = str_to_int(ptr);
unsigned char value = str_to_int(str);
base_pointer[address] = value;
unsigned char result = base_pointer[address];
return int_to_str(result);
}
char* read_from_mem(char* ptr){
unsigned int address = str_to_int(ptr);
unsigned char result = base_pointer[address];
return int_to_str(result);
}
void handle_command(char *buf_ptr) {
char *command[32];
buf_cpy(buf_ptr, command, 0, 5);
int i = 0;
int end = get_first_pos(buf_ptr, '\0');
if (compare_str(ECHO_COMMAND, command)) {
char arg[256];
buf_cpy(buf_ptr, arg, 5, end);
echo_cmd(arg);
} else if (compare_str(WRITE_MEMORY_COMMAND, command)) {
char arg[256];
buf_cpy(buf_ptr, arg, 5, end);
int delimiter = get_first_pos(arg, ' ');
char arg1[32], arg2[32];
buf_cpy(arg, arg1, 0, delimiter);
buf_cpy(arg, arg2, delimiter + 1, end);
char *result = write_to_mem(arg1, arg2);
echo_cmd(result);
} else if (compare_str(READ_MEMORY_COMMAND, command)) {
char arg[32];
buf_cpy(buf_ptr, arg, 5, end);
char *result = read_from_mem(arg);
echo_cmd(result);
} else if (compare_str(HELP_MEMORY_COMMAND1, command) || compare_str(HELP_MEMORY_COMMAND2, command)) {
help_cmd();
} else {
print("Command not found\n");
}
}