-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20141570.c
347 lines (306 loc) · 9.92 KB
/
20141570.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "20141570.h"
int main () {
FILE * fp = fopen("opcode.txt","r");
char input_str[MAX_LEN_INPUT],
command[MAX_LEN_COMMAND],
argv[3][MAX_LEN_INPUT],
*mnemonic = (char *) calloc(LEN_MNEMONIC, sizeof(char));
int i, j,
error_flag, comma_flag, linking_loader_flag = 0,
idx_input_str, idx_argv,
opcode, format;
hist_list * new_hist_elem = NULL;
history_head = (hist_list *) calloc(1, sizeof(hist_list));
/* make opcode table */
for (i = 0; i < 20; i++) {
table_head[i] = (op_list *) calloc(1, sizeof(op_list));
}
// opcode에 관한 정보를 받고 linked list에 저장한다.
i = 0;
while (i < NUM_OF_OPCODES) {
fscanf(fp, "%02X %s %d/%*s\n", &opcode, mnemonic, &format);
// TODO : opcode table에서 linking 시켜 op_list 를 굳이 새로 만들지 않게 할 것 && 이 부분도 함수로 moduling 할 것 && trash -> format && mnemonic 바로 받는 것도 생각해 보기
make_linking_table(&table_head[hash_func(mnemonic)], opcode, mnemonic, format);
opcode_table[i].opcode = opcode;
opcode_table[i].format = format;
strcpy(opcode_table[i].mnemonic, mnemonic);
i ++;
}
while (1) {
/**************************************/
/********** initialization ************/
/**************************************/
error_flag = 0;
comma_flag = 0;
idx_input_str = 0;
linking_loader_flag = 0;
for (i = 0; i < MAX_LEN_COMMAND; i++) { command[i] = 0; }
for (i = 0; i < 3; i++) { for (j = 0; j < MAX_LEN_INPUT; j++) { argv[i][j] = 0; } }
for (i = 0; i < MAX_LEN_INPUT; i++) { input_str[i] = 0; }
/********** initialization end ********/
printf("sicsim> ");
/**************************************/
/****** input string handling *********/
/**************************************/
gets(input_str);
/** get command name **/
while (input_str[idx_input_str] == ' ') { idx_input_str ++; } /* ignore front space */
// input consists of space, enter
if (!input_str[idx_input_str]) {
continue;
}
idx_argv = 0;
while ((input_str[idx_input_str] != ' ' && input_str[idx_input_str] != '\t') && (input_str[idx_input_str] != 0)) {
command[idx_argv++] = input_str[idx_input_str++];
}
command[idx_argv] = 0;
while (input_str[idx_input_str] == ' ' ||
input_str[idx_input_str] == '\t' ||
input_str[idx_input_str] == '\n') {
idx_input_str++;
}
/* ---------- linking loader check and execute command ---------- */
if (!strcmp(command, "progaddr")) {
error_flag = linking_loader_main(1, input_str + idx_input_str);
linking_loader_flag = 1;
}
else if (!strcmp(command, "loader")) {
error_flag = linking_loader_main(2, input_str + idx_input_str);
linking_loader_flag = 1;
}
else if (!strcmp(command, "run")) {
error_flag = linking_loader_main(3, input_str + idx_input_str);
linking_loader_flag = 1;
}
else if (!strcmp(command, "bp")) {
error_flag = linking_loader_main(4, input_str + idx_input_str);
linking_loader_flag = 1;
}
if (error_flag) {
continue;
}
/* ---------- linking loader check end --------- */
if (!linking_loader_flag) {
/* get parameter name */
for (i = 0; i < 3 ;i++) {
comma_flag = 0;
/** i th(second, third, fourth) argument value **/
if (input_str[idx_input_str]) { // not end
/* ignore front space and counting comma */
while (input_str[idx_input_str] == ' ' || input_str[idx_input_str] == ',') {
if (input_str[idx_input_str] == ',') {
comma_flag++;
}
idx_input_str ++;
}
error_flag = error_check_comma(i, comma_flag);
idx_argv = 0;
while (input_str[idx_input_str] != ' ' && (input_str[idx_input_str] != 0) && (input_str[idx_input_str] != ',') && input_str[idx_input_str] != '\n') { // except 0(end of string), ",", space
argv[i][idx_argv++] = input_str[idx_input_str++];
}
argv[i][idx_argv] = 0;
}
}
/* error checking */
if (error_flag) {
SEND_ERROR_MESSAGE("COMMA");
continue;
}
else if(error_check_moreargv(input_str, idx_input_str)) {
SEND_ERROR_MESSAGE("OVERFULL ARGV");
continue;
}
}
/****** input string handling end ********/
/*****************************************/
/*********** execute command *************/
/*****************************************/
if (!strcmp(command, "help") || !strcmp(command, "h")) {
// argv[0] exist - error
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_help();
}
else if (!strcmp(command, "dir") || !strcmp(command, "d")) {
// argv[0] exist - error
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_dir();
}
else if (!strcmp(command, "quit") || !strcmp(command, "q")) {
// argv[0] exist - error
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
break;
}
else if (!strcmp(command, "history") || !strcmp(command, "hi")) {
// argv[0] exist - error
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
// function check
}
else if (!strcmp(command, "dump") || !strcmp(command, "du")) {
// exception handling - boundary error and existing thrid argv
int start = strtoi(argv[0], &error_flag, 16),
end = strtoi(argv[1], &error_flag, 16);
/* error handling */
if (error_flag) { // does not integer
SEND_ERROR_MESSAGE("NOT INTEGER");
continue;
}
if ((start < 0) || (end < 0) || (start > 0xFFFFF) || (argv[1][0] && (start > end)) || start > 0xFFFFF || end > 0xFFFFF) { // boundary error
SEND_ERROR_MESSAGE("BOUNDARY ERROR");
continue;
}
if (argv[2][0]) { // argv[2] exists
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
// params don't exist
if (!argv[0][0]) {
start = -1;
}
else if (!argv[1][0]) {
end = -1;
}
command_dump(start, end);
}
else if (!strcmp(command, "edit") || !strcmp(command, "e")) {
int addr = strtoi(argv[0], &error_flag, 16),
val = strtoi(argv[1], &error_flag, 16);
/* error handling */
if (error_flag) { // does not integer
SEND_ERROR_MESSAGE("NOT INTEGER");
continue;
}
if (addr < 0x0 || addr > 0xFFFFF || val < 0 || val > 0xFF) { // boundary error
SEND_ERROR_MESSAGE("BOUNDARY ERROR");
continue;
}
if (!argv[0][0] || !argv[1][0] || argv[2][0]) { // argv[0] doesn't exist || argv[1] doesn't exist || argv[2] exist
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_edit(addr, val);
}
else if (!strcmp(command, "fill") || !strcmp(command, "f")) {
int start = strtoi(argv[0], &error_flag, 16),
end = strtoi(argv[1], &error_flag, 16),
val = strtoi(argv[2], &error_flag, 16);
/* error handling */
if (error_flag) { // does not integer
SEND_ERROR_MESSAGE("NOT INTEGER");
continue;
}
if ((start < 0) || (end < 0) || (start > 0xFFFFF)|| (argv[1][0] && (start > end)) || end > 0xFFFFF || val < 0 || val > 0xFF) { // boundary error
SEND_ERROR_MESSAGE("BOUNDARY ERROR");
continue;
}
if (!argv[0][0] || !argv[1][0] || !argv[2][0]) { // argv[0] doesn't exist || argv[1] doesn't exist || argv[2] exist
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_fill(start, end, val);
}
else if (!strcmp(command, "reset")) {
// argv[0] exist - error
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_reset();
}
else if (!strcmp(command, "opcode")) {
char input_mnem[LEN_MNEMONIC];
// argv[0] exist - error
if (!argv[0][0] || argv[1][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
strcpy(input_mnem, argv[0]);
command_opcode(input_mnem, &error_flag);
if (error_flag == -1) {
SEND_ERROR_MESSAGE("THERE IS NO MATCHING MNEMONIC");
continue;
}
}
else if (!strcmp(command, "opcodelist")) {
// argv[0] exist - error
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_opcodelist();
}
else if (!strcmp(command, "assemble")) {
if (argv[1][0] || !argv[0][0]) { // argv[1][0] != 0 implies that second argument exists and argv[1][0] == 0 implies that first argument does not exist.
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
// TODO : Make a function deallocating a symbol_table
if (!symbol_table) {
// free symbol_table
// FATAL it needs to be fixed.
symbol_table = NULL;
}
if (strcmp(argv[0] + strlen(argv[0]) - 4, ".asm")) {
SEND_ERROR_MESSAGE("THIS IS NOT .asm file");
}
command_assemble(argv[0], &error_flag);
if (error_flag) {
continue;
}
}
else if (!strcmp(command, "type")) {
if (argv[1][0] || !argv[0][0]) { // argv[1][0] != 0 implies that second argument exists and argv[1][0] == 0 implies that first argument does not exist.
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
command_type(argv[0], &error_flag);
if (error_flag) {
// error is occured
continue;
}
}
else if (!strcmp(command, "symbol")) {
// XXX : error check!
if (argv[0][0]) {
SEND_ERROR_MESSAGE("FORMAT DOES NOT MATCH THIS COMMAND");
continue;
}
if (!complete_table) {
SEND_ERROR_MESSAGE("THERE IS NO SYMBOL TABLE");
continue;
}
command_symbol();
}
else if (linking_loader_flag) {}
else {
SEND_ERROR_MESSAGE("THIS COMMAND DOES NOT EXIST");
continue;
}
hist_list * tmp_hist;
// store in hist_list(linked list for history has command lines of history
new_hist_elem = (hist_list *) calloc(1, sizeof(hist_list));
strcpy(new_hist_elem->command_line, input_str);
tmp_hist = history_head;
while (tmp_hist->next) { tmp_hist = tmp_hist->next; }
tmp_hist->next = new_hist_elem;
if (!strcmp(command, "history") || !strcmp(command, "hi")) {
command_history();
}
}
fclose(fp);
// deallocating memory
deallocate_opcode();
deallocate_history();
return 0;
}