-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
404 lines (373 loc) · 11.1 KB
/
main.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#define MAX_MEMORY (1 << 16)
uint16_t memory[MAX_MEMORY];
/* Registers */
enum {
R_R0 = 0,
R_R1,
R_R2,
R_R3,
R_R4,
R_R5,
R_R6,
R_R7,
R_PC, /* Program Counter */
R_COND, /* Conditional */
R_COUNT /* Number of Registers */
};
/* Register Storage */
uint16_t reg[R_COUNT];
/* Condition Flags */
enum {
FL_POS = 1 << 0, /* POSITIVE */
FL_ZER = 1 << 1, /* ZERO */
FL_NEG = 1 << 2 /* NEGATIVE */
};
/* Opcodes */
enum {
OP_BR = 0x0, /* branch */
OP_ADD = 0x1, /* add */
OP_LD = 0x2, /* load */
OP_ST = 0x3, /* store */
OP_JSR = 0x4, /* jump register */
OP_AND = 0x5, /* bitwise AND */
OP_LDR = 0x6, /* load register */
OP_STR = 0x7, /* store register */
OP_RTI = 0x8, /* unused */
OP_NOT = 0x9, /* bitwise NOT */
OP_LDI = 0xA, /* load indirect */
OP_STI = 0xB, /* store indirect */
OP_JMP = 0xC, /* jump */
OP_RES = 0xD, /* reserved */
OP_LEA = 0xE, /* load effective address */
OP_TRP = 0xF /* trap */
};
/* TRAPS */
enum {
TRP_GETC = 0x20, /* get character from the keyboard */
TRP_OUT = 0x21, /* output a character */
TRP_PUTS = 0x22, /* output a string */
TRP_IN = 0x23, /* get character from the terminal */
TRP_PUTSP = 0x24, /* output a byte string */
TRP_HALT = 0x25 /* halt the execution */
};
enum {
MR_KBSR = 0xFE00, /* keyboard status */
MR_KBDR = 0xFE02 /* keyboard data */
};
uint16_t check_key()
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
}
uint16_t mem_read(uint16_t address) {
if (address == MR_KBSR) {
if (check_key()) {
memory[MR_KBSR] = (1 << 15);
memory[MR_KBDR] = getchar();
} else {
memory[MR_KBSR] = 0;
}
}
return memory[address];
}
void mem_write(uint16_t address, uint16_t val) {
memory[address] = val;
}
uint16_t sign_extend(uint16_t x, int bit_c) {
if ((x >> (bit_c - 1)) & 1) {
x |= (0xFFFF << bit_c);
}
return x;
}
uint16_t zero_extend(uint16_t x, int bit_c) {
return (x &= (0x00FF >> bit_c));
}
uint16_t update_flags(uint16_t r) {
if (reg[r] == 0) {
reg[R_COND] = FL_ZER;
} else if (reg[r] >> 15) {
reg[R_COND] = FL_NEG;
} else {
reg[R_COND] = FL_POS;
}
return reg[r];
}
uint16_t swap16(uint16_t n) {
return ((n >> 8) | (n << 8));
}
void read_image_file(FILE *file) {
uint16_t origin;
fread(&origin, sizeof(origin), 1, file);
origin = swap16(origin);
uint16_t max_read = MAX_MEMORY - origin;
uint16_t *p = memory + origin;
size_t read = fread(p, sizeof(uint16_t), max_read, file);
while (read--) {
*p = swap16(*p);
p++;
}
}
int read_image(char *image_path) {
FILE *file = fopen(image_path, "rb");
if (!file) { return 0; }
read_image_file(file);
fclose(file);
return 1;
}
struct termios original_tio;
void disable_input_buffering()
{
tcgetattr(STDIN_FILENO, &original_tio);
struct termios new_tio = original_tio;
new_tio.c_lflag &= ~ICANON & ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
}
void restore_input_buffering()
{
tcsetattr(STDIN_FILENO, TCSANOW, &original_tio);
}
void handle_interrupt() {
restore_input_buffering();
printf("\n");
exit(-2);
}
int main(int argc, char *argv[]) {
signal(SIGINT, handle_interrupt);
disable_input_buffering();
if (argc < 2) {
printf("lc-3 [image-file] ...\n");
exit(2);
}
for (int i = 1; i < argc; i++) {
if (!read_image(argv[i])) {
printf("failed to load image: %s\n", argv[i]);
exit(1);
}
}
reg[R_COND] = FL_ZER;
enum { PC_START = 0x3000 };
reg[R_PC] = PC_START;
int running = 1;
while (running) {
uint16_t inst = mem_read(reg[R_PC]++);
uint16_t op = inst >> 12;
switch (op) {
case OP_ADD:
/* ADD */
{
uint16_t DR = (inst >> 9) & 0x7;
uint16_t SR1 = (inst >> 6) & 0x7;
uint16_t IMM_FL = (inst >> 5) & 0x1;
if (IMM_FL) {
uint16_t IMM = sign_extend(inst & 0x1F, 5);
reg[DR] = reg[SR1] + IMM;
} else {
uint16_t SR2 = (inst & 0x7);
reg[DR] = reg[SR1] + reg[SR2];
}
update_flags(DR);
}
break;
case OP_AND:
/* AND */
{
uint16_t DR = (inst >> 9) & 0x7;
uint16_t SR1 = (inst >> 6) & 0x7;
uint16_t IMM_FL = (inst >> 5) & 0x1;
if (IMM_FL) {
uint16_t IMM = sign_extend((inst & 0x1F), 5);
reg[DR] = reg[SR1] & IMM;
} else {
uint16_t SR2 = (inst & 0x7);
reg[DR] = reg[SR1] & reg[SR2];
}
update_flags(DR);
}
break;
case OP_BR:
/* BRANCH */
{
uint16_t P = (inst >> 9) & 0x7;
uint16_t PC_OFFSET = sign_extend((inst & 0x1FF), 9);
if (P & reg[R_COND]) {
reg[R_PC] += PC_OFFSET;
}
}
break;
case OP_JMP:
/* JUMP */
{
uint16_t LOC = (inst >> 6) & 0x7;
reg[R_PC] = reg[LOC];
}
break;
case OP_JSR:
/* JUMP TO LOCATION SPECIFIED BY REGISTER */
{
uint16_t SR_FL = (inst >> 11) & 0x1;
reg[R_R7] = reg[R_PC];
if (SR_FL) {
uint16_t PC_OFFSET = (inst & 0x7FF);
reg[R_PC] += sign_extend(PC_OFFSET, 11);
} else {
uint16_t BASE = (inst >> 6) & 0x7;
reg[R_PC] = reg[BASE];
}
}
break;
case OP_NOT:
/* NOT */
{
uint16_t DR = (inst >> 9) & 0x7;
uint16_t SR = (inst >> 6) & 0x7;
reg[DR] = ~reg[SR];
update_flags(DR);
}
break;
case OP_LD:
/* LOAD */
{
uint16_t DR = (inst >> 9) & 0x7;
uint16_t PC_OFFSET = sign_extend((inst & 0x1FF), 9);
reg[DR] = mem_read(reg[R_PC] + PC_OFFSET);
update_flags(DR);
}
break;
case OP_LDI:
/* LOAD INDIRECT */
{
uint16_t PC_OFFSET = sign_extend((inst & 0x1FF), 9);
uint16_t DR = (inst >> 9) & 0x7;
reg[DR] = mem_read(mem_read(reg[R_PC] + PC_OFFSET));
update_flags(DR);
}
break;
case OP_LDR:
/* LOAD REGISTER */
{
uint16_t DR = (inst >> 9) & 0x7;
uint16_t BASE = (inst >> 6) & 0x7;
uint16_t OFFSET6 = sign_extend((inst & 0x3F), 6);
reg[DR] = mem_read(OFFSET6 + reg[BASE]);
update_flags(DR);
}
break;
case OP_LEA:
/* LOAD EFFECTIVE ADDRESS */
{
uint16_t DR = (inst >> 9) & 0x7;
uint16_t PC_OFFSET = sign_extend((inst & 0x1FF), 9);
reg[DR] = PC_OFFSET + reg[R_PC];
update_flags(DR);
}
break;
/* RTI */
// abort();
case OP_ST:
/* STORE */
{
uint16_t SR = (inst >> 9) & 0x7;
uint16_t PC_OFFSET = sign_extend((inst & 0x1FF), 9);
mem_write(reg[R_PC] + PC_OFFSET, reg[SR]);
}
break;
case OP_STI:
/* STORE INDIRECT */
{
uint16_t SR = (inst >> 9) & 0x7;
uint16_t PC_OFFSET = sign_extend((inst & 0x1FF), 9);
mem_write(mem_read(reg[R_PC] + PC_OFFSET), reg[SR]);
}
break;
case OP_STR:
/* STORE IN REGISTER */
{
uint16_t SR = (inst >> 9) & 0x7;
uint16_t BASE = (inst >> 6) & 0x7;
uint16_t OFFSET6 = sign_extend((inst & 0x3F), 6);
mem_write(reg[BASE] + OFFSET6, reg[SR]);
}
break;
case OP_TRP:
/* TRAP */
{
reg[R_R7] = reg[R_PC];
uint16_t TRP_VEC8 = (inst & 0xFF);
switch (TRP_VEC8) {
case TRP_GETC:
{
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);
}
break;
case TRP_PUTS:
{
uint16_t* c = memory + reg[R_R0];
while (*c) {
putc((char) *c, stdout);
++c;
}
fflush(stdout);
}
break;
case TRP_OUT:
{
putc((char) reg[R_R0], stdout);
fflush(stdout);
}
break;
case TRP_IN:
{
printf("> ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t) c;
update_flags(R_R0);
}
break;
case TRP_PUTSP:
{
uint16_t* c = memory + reg[R_R0];
while (*c) {
char c1 = (*c) & 0xFF;
putc(c1, stdout);
char c2 = (*c) >> 8;
if (c2) putc(c2, stdout);
++c;
}
fflush(stdout);
}
break;
case TRP_HALT:
{
printf("Halting The Execution...\n");
running = 0;
}
default:
break;
}
}
case OP_RES:
/* RES */
case OP_RTI:
/* RTI */
default:
break;
}
}
restore_input_buffering();
}