-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc3.c
More file actions
219 lines (199 loc) · 4.39 KB
/
lc3.c
File metadata and controls
219 lines (199 loc) · 4.39 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
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
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/mman.h>
#include "core/bit-utilities.h"
#include "core/core.h"
#include "core/input-buffering.h"
#include "core/read-image.h"
#include "instruction-set.h"
// Standard fetch/execute cycle using switch statement
void fetchExecute() {
/* FETCH */
uint16_t instruction = mem_read(registers[R_PC]++);
uint16_t opcode = instruction >> 12;
switch (opcode) {
case OP_ADD:
add(instruction);
break;
case OP_AND:
and(instruction);
break;
case OP_NOT:
not(instruction);
break;
case OP_BR:
branch(instruction);
break;
case OP_JMP:
jump(instruction);
break;
case OP_JSR:
jumpToSubroutine(instruction);
break;
case OP_LD:
load(instruction);
break;
case OP_LDI:
loadIndirect(instruction);
break;
case OP_LDR:
loadRegister(instruction);
break;
case OP_LEA:
loadEffectiveAddress(instruction);
break;
case OP_ST:
store(instruction);
break;
case OP_STI:
storeIndirect(instruction);
break;
case OP_STR:
storeRegister(instruction);
break;
case OP_TRAP:
trap(instruction);
break;
case OP_RES:
abort();
break;
case OP_RTI:
abort();
break;
default:
// Bad opcode
printf("BAD OPCODE\n");
break;
}
}
// Alternate fetch/execute using computed GOTO
// This method supposedly uses less branching by
// eliminating the outer while loop
// Each instruction should use only one JMP instruction
// instead of two, which should make the execution faster
// However, compiler optimizations may make the
// execution times of both approaches approximately the same.
// I have not personally instrumented the code to determine
// if the computed GOTO method offers any advantages
// See: https://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables
// Also: https://news.ycombinator.com/item?id=18678699
#define DISPATCH() {\
currentInstruction = mem_read(registers[R_PC]++);\
uint16_t opcode = currentInstruction >> 12;\
goto *dispatch_table[opcode];\
}
void fetchExecuteComputedGoto() {
// NOTE: THE ORDER OF THIS TABLE
// MUST MATCH THE ORDER OF THE INSTRUCTIONS
// IN instruction-set.h
// i.e. OP_BR must be at index 0 etc.
static void *dispatch_table[] = {
&&OP_BR,
&&OP_ADD,
&&OP_LD,
&&OP_ST,
&&OP_JSR,
&&OP_AND,
&&OP_LDR,
&&OP_STR,
&&OP_RTI,
&&OP_NOT,
&&OP_LDI,
&&OP_STI,
&&OP_JMP,
&&OP_RES,
&&OP_LEA,
&&OP_TRAP
};
uint16_t currentInstruction;
DISPATCH();
OP_ADD:
add(currentInstruction);
DISPATCH();
OP_AND:
and(currentInstruction);
DISPATCH();
OP_NOT:
not(currentInstruction);
DISPATCH();
OP_BR:
branch(currentInstruction);
DISPATCH();
OP_JMP:
jump(currentInstruction);
DISPATCH();
OP_JSR:
jumpToSubroutine(currentInstruction);
DISPATCH();
OP_LD:
load(currentInstruction);
DISPATCH();
OP_LDI:
loadIndirect(currentInstruction);
DISPATCH();
OP_LDR:
loadRegister(currentInstruction);
DISPATCH();
OP_LEA:
loadEffectiveAddress(currentInstruction);
DISPATCH();
OP_ST:
store(currentInstruction);
DISPATCH();
OP_STI:
storeIndirect(currentInstruction);
DISPATCH();
OP_STR:
storeRegister(currentInstruction);
DISPATCH();
OP_TRAP:
trap(currentInstruction);
DISPATCH();
OP_RES:
abort();
DISPATCH();
OP_RTI:
abort();
DISPATCH();
}
/* MAIN */
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], memory)) {
printf("failed to load image: %s\n", argv[j]);
exit(1);
}
}
signal(SIGINT, handle_interrupt);
disable_input_buffering();
/* Set the Program Counter to the default address:
0x3000
Lower addresses are left empty to leave space
for trap routines
*/
enum { PC_START = 0x3000 };
registers[R_PC] = PC_START;
// Fetch/Execute using switch statements
/*
while (1) {
fetchExecute();
}// end while
//*/
// Fetch/Execute using computed GOTO
fetchExecuteComputedGoto();
restore_input_buffering();
}