-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc3_vm.c
526 lines (455 loc) · 11.3 KB
/
lc3_vm.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/mman.h>
/* 65536 locations, RAM is 64K * 16bit / 2 = 128KB */
uint16_t memory[UINT16_MAX];
/* whether the program is running or not */
static bool running;
/* LC-3 have 10 registers, each size is 16 bit
* R0-R7 : general registers
* PC : program couter register
* COND : condition flags register */
enum {
R_R0 = 0,
R_R1,
R_R2,
R_R3,
R_R4,
R_R5,
R_R6,
R_R7,
R_PC, /* program counter */
R_COND,
R_COUNT
};
uint16_t reg[R_COUNT];
/* LC-3 instruction format
* bit0 bit1 bit2 bit3 | bit4 bit5 bit6 bit7 bit8 bit9 bit10 bit11 bit12 bit13 bit14 bit15
* opcode parameter
* LC-3 is RISC */
enum {
OP_BR = 0, /* branch */
OP_ADD, /* add */
OP_LD, /* load */
OP_ST, /* store */
OP_JSR, /* jump register */
OP_AND, /* bitwise and */
OP_LDR, /* load register */
OP_STR, /* store register */
OP_RTI, /* unused */
OP_NOT, /* bitwise not */
OP_LDI, /* load indirect */
OP_STI, /* store indirect */
OP_JMP, /* jump */
OP_RES, /* reserved(unused) */
OP_LEA, /* load effective address */
OP_TRAP /* execute trap */
};
/* Condition flags register -- condition flag */
enum {
FL_POS = 1 << 0, /* positive */
FL_ZRO = 1 << 1, /* zero */
FL_NEG = 1 << 2 /* negative */
};
/* the default program start position : 0x3000*/
enum { PC_START = 0x3000 };
/* trap code */
enum {
TRAP_GETC = 0x20, /* get character from keyboard, not echoed onto the terminal */
TRAP_OUT = 0x21, /* output a character */
TRAP_PUTS = 0x22, /* output a word string */
TRAP_IN = 0x23, /* get character from keyboard, echoed onto the terminal */
TRAP_PUTSP = 0x24, /* output a byte string */
TRAP_HALT = 0x25 /* halt the program */
};
/* device register */
enum {
MR_KBSR = 0xFE00, /* Keyboard status register */
MR_KBDR = 0xFE02 /* Keyboard data register */
};
static struct termios original_tio;
/*****************************************************************************/
bool check_key()
{
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &read_fds, NULL, NULL, &timeout) != 0;
}
void mem_write(uint16_t address, uint16_t val)
{
memory[address] = val;
}
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];
}
uint16_t sign_extend(uint16_t x, int bit_count)
{
if ((x >> (bit_count - 1)) & 0x1)
x |= 0xFFFF << bit_count;
return x;
}
void update_flags(uint16_t reg_index)
{
if (reg[reg_index] == 0)
reg[R_COND] = FL_ZRO;
else if (reg[reg_index] >> 15)
reg[R_COND] = FL_NEG;
else
reg[R_COND] = FL_POS;
}
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(int signal)
{
restore_input_buffering();
printf("\n");
exit(-2);
}
/*****************************************************************************/
/* OP_ADD */
void op_add(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t src_reg1 = (instr >> 6) & 0x7;
bool imm5_flag = (instr >> 5) & 0x1;
if (imm5_flag)
{
uint16_t imm5 = sign_extend(instr & 0x1F, 5);
reg[dst_reg] = reg[src_reg1] + imm5;
}
else
{
uint16_t src_reg2 = instr & 0x7;
reg[dst_reg] = reg[src_reg1] + reg[src_reg2];
}
update_flags(dst_reg);
}
/* OP_AND */
void op_and(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t src_reg1 = (instr >> 6) & 0x7;
bool imm5_flag = (instr >> 5) & 0x1;
if (imm5_flag)
{
uint16_t imm5 = sign_extend(instr & 0x1F, 5);
reg[dst_reg] = reg[src_reg1] & imm5;
}
else
{
uint16_t src_reg2 = instr & 0x7;
reg[dst_reg] = reg[src_reg1] & reg[src_reg2];
}
update_flags(dst_reg);
}
/* OP_NOT */
void op_not(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t src_reg = (instr >> 6) & 0x7;
reg[dst_reg] = ~reg[src_reg];
update_flags(dst_reg);
}
/* OP_BR */
void op_br(uint16_t instr)
{
uint16_t pc_offset9 = sign_extend(instr & 0x1FF, 9);
uint16_t cond_flag = (instr >> 9) & 0x7;
if (cond_flag & reg[R_COND])
reg[R_PC] += pc_offset9;
}
/* OP_JMP */
void op_jmp(uint16_t instr)
{
uint16_t base_reg = (instr >> 6) & 0x7;
reg[R_PC] = reg[base_reg];
}
/* OP_JSR */
void op_jsr(uint16_t instr)
{
uint16_t long_flag = (instr >> 11) & 0x1;
reg[R_R7] = reg[R_PC];
if (long_flag)
{
uint16_t pc_offset11 = sign_extend(instr & 0x7FF, 11);
reg[R_PC] += pc_offset11;
}
else
{
uint16_t base_reg = (instr >> 6) & 0x7;
reg[R_PC] = reg[base_reg];
}
}
/* OP_LD */
void op_ld(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t pc_offset9 = sign_extend(instr & 0x1FF, 9);
reg[dst_reg] = mem_read(reg[R_PC] + pc_offset9);
update_flags(dst_reg);
}
/* OP_LDI */
void op_ldi(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t pc_offset9 = sign_extend(instr & 0x1FF, 9);
uint16_t address = mem_read(reg[R_PC] + pc_offset9);
reg[dst_reg] = mem_read(address);
update_flags(dst_reg);
}
/* OP_LDR */
void op_ldr(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t base_reg = (instr >> 6) & 0x7;
uint16_t pc_offset6 = sign_extend(instr & 0x3F, 6);
reg[dst_reg] = mem_read(reg[base_reg] + pc_offset6);
update_flags(dst_reg);
}
/* OP_LEA */
void op_lea(uint16_t instr)
{
uint16_t dst_reg = (instr >> 9) & 0x7;
uint16_t pc_offset9 = sign_extend(instr & 0x1FF, 9);
reg[dst_reg] = reg[R_PC] + pc_offset9;
update_flags(dst_reg);
}
/* OP_ST */
void op_st(uint16_t instr)
{
uint16_t src_reg = (instr >> 9) & 0x7;
uint16_t pc_offset9 = sign_extend(instr & 0x1FF, 9);
uint16_t address = reg[R_PC] + pc_offset9;
mem_write(address, reg[src_reg]);
}
/* OP_STI */
void op_sti(uint16_t instr)
{
uint16_t src_reg = (instr >> 9) & 0x7;
uint16_t pc_offset9 = sign_extend(instr & 0x1FF, 9);
uint16_t address = mem_read(reg[R_PC] + pc_offset9);
mem_write(address, reg[src_reg]);
}
/* OP_STR */
void op_str(uint16_t instr)
{
uint16_t src_reg = (instr >> 9) & 0x7;
uint16_t base_reg = (instr >> 6) & 0x7;
uint16_t pc_offset6 = sign_extend(instr & 0x3F, 6);
uint16_t address = reg[base_reg] + pc_offset6;
mem_write(address, reg[src_reg]);
}
/****************************** Trap Routine *********************************/
/* TRAP_GETC */
void trap_getc()
{
reg[R_R0] = (uint16_t)getchar();
}
/* TRAP_OUT */
void trap_out()
{
putc((char)reg[R_R0], stdout);
fflush(stdout);
}
/* TRAP_PUTS */
void trap_puts()
{
/* one char per word */
uint16_t *ptr_c = memory + reg[R_R0];
while ( *ptr_c)
{
putc((char)*ptr_c, stdout);
++ptr_c;
}
fflush(stdout);
}
/* TRAP_IN */
void trap_in()
{
printf("Enter a character:\n");
char ch = getchar();
putc(ch, stdout);
reg[R_R0] = (uint16_t)ch;
}
/* TRAP_PUTSP */
void trap_putsp()
{
/* two char per word, here we need to swap back to big endian format */
uint16_t *ptr_c = memory + reg[R_R0];
while ( *ptr_c)
{
char ch1 = (*ptr_c) & 0xFF;
putc(ch1, stdout);
char ch2 = (*ptr_c) >> 8;
if (ch2)
putc(ch2, stdout);
++ptr_c;
}
fflush(stdout);
}
void trap_halt()
{
puts("HATL");
fflush(stdout);
running = false;
}
/* OP_TRAP */
void op_trap(uint16_t instr)
{
switch (instr & 0xFF)
{
case TRAP_GETC:
trap_getc();
break;
case TRAP_OUT:
trap_out();
break;
case TRAP_PUTS:
trap_puts();
break;
case TRAP_IN:
trap_in();
break;
case TRAP_PUTSP:
trap_putsp();
break;
case TRAP_HALT:
trap_halt();
break;
default:
break;
}
}
/*****************************************************************************/
/* swap big-endian to little-endian */
uint16_t swap16(uint16_t x)
{
return (x << 8) | (x >> 8);
}
void read_image_file(FILE *file)
{
/* the first 16 bit tell us where in memory to place the image */
uint16_t origin;
fread(&origin, sizeof(origin), 1, file);
origin = swap16(origin);
/* we know the maximum file size so we only need one fread */
uint16_t max_read = UINT16_MAX - origin;
uint16_t *p_curr = memory + origin;
size_t actual_read = fread(p_curr, sizeof (uint16_t), max_read, file);
while (actual_read-- > 0)
{
*p_curr = swap16(*p_curr);
++p_curr;
}
}
bool read_image(const char *image_path)
{
FILE *image_file = fopen(image_path, "rb");
if (image_file == NULL)
return false;
read_image_file(image_file);
fclose(image_file);
return true;
}
/*****************************************************************************/
int main(int argc, const char* argv[])
{
if (argc < 2)
return 0;
read_image(argv[1]);
signal(SIGINT, handle_interrupt);
disable_input_buffering();
/* Set the PC to starting position */
reg[R_PC] = PC_START;
running = true;
while (running)
{
uint16_t instr = mem_read(reg[R_PC]++);
uint16_t op = instr >> 12;
switch (op)
{
case OP_ADD:
op_add(instr);
break;
case OP_AND:
op_and(instr);
break;
case OP_NOT:
op_not(instr);
break;
case OP_BR:
op_br(instr);
break;
case OP_JMP:
op_jmp(instr);
break;
case OP_JSR:
op_jsr(instr);
break;
case OP_LD:
op_ld(instr);
break;
case OP_LDI:
op_ldi(instr);
break;
case OP_LDR:
op_ldr(instr);
break;
case OP_LEA:
op_lea(instr);
break;
case OP_ST:
op_st(instr);
break;
case OP_STI:
op_sti(instr);
break;
case OP_STR:
op_str(instr);
break;
case OP_TRAP:
op_trap(instr);
break;
case OP_RES:
case OP_RTI:
default:
abort();
break;
}
}
/* Shutdown */
restore_input_buffering();
return 0;
}