-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc3.c
More file actions
95 lines (87 loc) · 2.15 KB
/
Copy pathlc3.c
File metadata and controls
95 lines (87 loc) · 2.15 KB
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
// gcc main.c opcodes.c -o program
#include <stdlib.h>
#include "enums.h"
#include "opcodes.h"
#define MEMORY_MAX (1<<16)
uint16 memory[MEMORY_MAX];
uint16 reg[R_COUNT];
int main (int argc, const char* argv[])
{
if(argc<2)
{
// show usage string
printf("lc3 [image-file1] ...\n");
exit(2);
}
for(int j = 1; j<argc;++j)
{
if(!read_image(argv[j]))
{
printf("failed to load image: %s\n", argv[j]);
exit(1);
}
}
//setup
signal(SIGINT, handle_interrupt);
disable_input_buffering();
reg[R_COND] = FL_ZRO; // set the Z flag
reg[R_PC] = PC_START; // 0x3000 is the default starting position
int running = 1;
while(running)
{
uint16 instr = mem_read(reg[R_PC]++); // fetch instruction
uint16 op = instr>>12;
switch(op)
{
case OP_ADD:
ADD(instr);
break;
case OP_AND:
AND(instr);
break;
case OP_NOT:
NOT(instr);
break;
case OP_BR:
BR(instr);
break;
case OP_JMP:
JMP(instr);
break;
case OP_JSR:
JSR(instr);
break;
case OP_LD:
LD(instr);
break;
case OP_LDI:
LDI(instr);
break;
case OP_LDR:
LDR(instr);
break;
case OP_LEA:
LEA(instr);
break;
case OP_ST:
ST(instr);
break;
case OP_STI:
STI(instr);
break;
case OP_STR:
STR(instr);
break;
case OP_TRAP:
TRAP(instr, &running);
break;
case OP_RES:
case OP_RTI:
default:
BAD();
break;
}
}
// shutdown
restore_input_buffering();
}