-
Notifications
You must be signed in to change notification settings - Fork 13
/
mcxxxx-microcontroller-simulation.c
282 lines (232 loc) · 9.3 KB
/
mcxxxx-microcontroller-simulation.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MACRO
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#define BUFF_LEN 256
#define MAX_VAL 999
#define MIN_VAL -MAX_VAL
#define ALLOC(p, n) \
do \
{ \
if ( !( (p) = malloc((n) * sizeof(*(p)) ) ) ) \
{ \
fprintf(stderr, "Memory allocation failed"); \
exit(EXIT_FAILURE); \
} \
memset( (p), 0, (n) * sizeof(*(p)) ); \
} \
while(0)
#define ALLOC_ARR(p, n) \
do \
{ \
if ( !( (p) = malloc((n) * sizeof(*(p)) ) ) ) \
{ \
fprintf(stderr, "Memory allocation failed"); \
exit(EXIT_FAILURE); \
} \
for ( int i = 0; i < (n); ++i ) \
{ \
if ( !( *(p + i) = malloc(BUFF_LEN * sizeof(*(p)))))\
{ \
fprintf(stderr, "Memory allocation failed"); \
exit(EXIT_FAILURE); \
} \
memset( *(p + i), 0, BUFF_LEN * sizeof(*(p)) ); \
} \
} \
while(0)
#define TK strtok(NULL, " ")
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STRUCTS
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typedef struct LblMap {
uint _capacity;
uint _count;
int* _lines; /*< Lines containing labels */
unsigned long* _hashs; /*< Hashed values of the corresponding labels */
} LblMap;
unsigned long lbl_hash(const char *str) {
unsigned long ret = 5381;
int c;
while (c = *str++) ret = ((ret << 5) + ret) + c;
return ret;
}
LblMap* lbl_new(uint capacity) {
LblMap* ret;
ALLOC(ret, 1);
ret->_capacity = capacity;
ret->_count = 0;
ALLOC(ret->_lines, ret->_capacity);
ALLOC(ret->_hashs, ret->_capacity);
return ret;
}
int lbl_del(LblMap* map) {
// TODO
}
int lbl_add(LblMap* map, const char* label, int line) {
if ( NULL == map || NULL == label ) return 0;
if ( map->_count >= map->_capacity ) return 0;
++map->_count;
fprintf(stderr, "\tlbl_add(%s, %d)\n", label, line);
unsigned long hash = lbl_hash(label);
map->_lines[map->_count - 1] = line;
map->_hashs[map->_count - 1] = hash;
}
int lbl_get(LblMap* map, const char* label, int* ret) {
if ( NULL == map || NULL == label ) return 0;
unsigned long hash = lbl_hash(label);
for ( int i = 0; i < map->_count; ++i ) {
if ( hash == map->_hashs[i] ) {
*ret = map->_lines[i];
return 1;
}
}
return 0;
}
int clmp(int ret) {
return (ret > MAX_VAL) ? MAX_VAL : (ret < MIN_VAL) ? MIN_VAL : ret;
}
char *trimwhitespace(char *str) {
char *end;
while (isspace((unsigned char)*str)) str++;
if (*str == 0) return str;
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}
int get_digit(int val, int n) {
static int po10[] = {1, 10, 100};
return ((val / po10[n]) % 10);
}
void set_digit(int* val, int n, int new_digit) {
static int po10[] = {1, 10, 100};
*val = *val - ((*val / po10[n]) % 10) * po10[n] + new_digit * po10[n];
}
typedef enum {
RET_FAILURE,
RET_SUCCESS
} RET_VAL;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// GLOBAL VARIABLES
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool BPLUS = false, BMINUS = false;
int k, n, code_size = 0;
int *acc, *dat, *x0;
char** code = NULL;
LblMap* labels = NULL;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FUNCTIONS
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int buff_read(const char* reg) {
fprintf(stderr, "\tbuff_read(%s)\n", reg);
if ( 0 == strncmp("acc", reg, BUFF_LEN) ) return *acc;
else if ( 0 == strncmp("dat", reg, BUFF_LEN) ) return *dat;
else if ( 0 == strncmp("x0" , reg, BUFF_LEN) ) return *x0++;
else return strtol(reg, NULL, 10);
}
RET_VAL buff_nwrite(const char* reg, int val) {
fprintf(stderr, "\tbuff_nwrite(%s,%d)\n", reg, val);
static bool first = true;
if ( 0 == strncmp("acc", reg, BUFF_LEN) ) *acc = val;
else if ( 0 == strncmp("dat", reg, BUFF_LEN) ) *dat = val;
else if ( 0 == strncmp("x1" , reg, BUFF_LEN) ) {
if ( first ) fprintf(stdout, "%d" , val), first = false;
else fprintf(stdout, " %d", val);
}
return RET_SUCCESS;
}
RET_VAL buff_vwrite(int* reg, int* val) {
if ( NULL == reg || NULL == val ) return RET_FAILURE;
*reg = *val;
return RET_SUCCESS;
}
RET_VAL label_get(const char* label, int* ret) {
return (RET_VAL)(lbl_get(labels, label, ret));
}
RET_VAL label_add(const char* label, int line) {
return (RET_VAL)(lbl_add(labels, label, line));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MAIN
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int main() {
int i;
// --------------------------
// MEMORY INITIALIZATION
// --------------------------
ALLOC (x0 , 100);
ALLOC (acc , 1);
ALLOC (dat , 1);
ALLOC_ARR(code, 40);
labels = lbl_new(20);
scanf("%d", &k);
for (i = 0; i < k; ++i) scanf("%d", x0 + i);
// --------------------------
// PREPROCESS CODE
// --------------------------
scanf("%d", &n); fgetc(stdin);
for (i = 0; i < n; ++i) {
char* line, *jmp_label, *jmp_inst;
scanf("%[^\n]", code[code_size]), fgetc(stdin);
line = code[code_size];
if ( '#' == line[0] ) continue;
if ( NULL == strchr(line, ':') ) { ++code_size; continue; }
jmp_label = strtok(line, ":");
jmp_inst = strtok(NULL, ":");
label_add(jmp_label, code_size);
if ( NULL != jmp_inst ) code[code_size++] = trimwhitespace(jmp_inst);
}
// --------------------------
// EXECUTE CODE
// --------------------------
i = 0;
while ( i < code_size ) {
int val;
char* line = strdup(code[i]);
char* tkn = strtok(line, " ");
// Instruction modifiers
if ( NULL == tkn ) goto next;
if ( '#' == *tkn || '$' == *tkn ) goto next;
if ( '+' == *tkn && !BPLUS ) goto next;
if ( '-' == *tkn && !BMINUS ) goto next;
if ( '@' == *tkn ) { tkn = TK; code[i][0] = '$'; }
if ( '-' == *tkn || '+' == *tkn ) tkn = TK;
// Arithmetic instructions
if ( 0 == strncmp("add", tkn, 3) ) *acc = clmp( *acc + buff_read( TK ) );
else if ( 0 == strncmp("sub", tkn, 3) ) *acc = clmp( *acc - buff_read( TK ) );
else if ( 0 == strncmp("mul", tkn, 3) ) *acc = clmp( *acc * buff_read( TK ) );
else if ( 0 == strncmp("not", tkn, 3) ) *acc = (0 == *acc) ? 100 : 0;
else if ( 0 == strncmp("dgt", tkn, 3) ) *acc = get_digit(*acc, buff_read( TK ));
else if ( 0 == strncmp("dst", tkn, 3) ) val = buff_read( TK ), set_digit(acc,
val, buff_read( TK ) );
// Basic instructions
else if ( 0 == strncmp("mov", tkn, 3) ) val = buff_read(TK), buff_nwrite(TK,
val);
else if ( 0 == strncmp("jmp", tkn, 3) ) { label_get(TK, &i); goto free_data; }
// Branching instructions
else if ( 0 == strncmp("teq", tkn, 3) ) {
if ( buff_read(TK) == buff_read(TK) ) BPLUS = true , BMINUS = false;
else BPLUS = false, BMINUS = true ;
} else if ( 0 == strncmp("tgt", tkn, 3) ) {
if ( buff_read(TK) > buff_read(TK) ) BPLUS = true , BMINUS = false;
else BPLUS = false, BMINUS = true ;
} else if ( 0 == strncmp("tlt", tkn, 3) ) {
if ( buff_read(TK) < buff_read(TK) ) BPLUS = true , BMINUS = false;
else BPLUS = false, BMINUS = true ;
} else if ( 0 == strncmp("tcp", tkn, 3) ) {
int A = buff_read(TK), B = buff_read(TK);
if ( A == B ) BPLUS = false, BMINUS = false;
else if ( A > B ) BPLUS = true , BMINUS = false;
else if ( A < B ) BPLUS = false, BMINUS = true ;
}
next:
++i;
free_data:
free(line);
}
return EXIT_SUCCESS;
}