-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslc3.c
462 lines (424 loc) · 15 KB
/
slc3.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
/**
* LC-3 Simulator
* Final Project (Project #6)
* TCSS 372 - Computer Architecture
* Spring 2018
*
* LC-3 Simulator Module File
*
* This is a simulator of the LC-3 (Little Computer) machine using an
* object-oriented approach in C. The simulator includes all standard LC-3
* functionality based on the finite state machine approach and the corresponding
* opcode tables for the machine, with an additional push-pop stack feature utilized
* on the previously reserved (1101) opcode.
*
* Group Members:
* Michael Fulton
* Enoch Chan
* Logan Stafford
*
* Base Code Contributors:
* Sam Brendel
* Michael Josten
* Sam Anderson
* Tyler Schupack
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "display.h"
#include "lc3.h"
#include "memory.h"
#include "slc3.h"
/** Allows the display to edit memory */
void prompt_edit_mem(lc3_p, display_p);
/** Returns a non-null pointer to a (hopefully) hex file */
void prompt_load_file_display(lc3_p);
/** Returns a non-null pointer to a (hopefully) hex file */
void prompt_save_file_display(lc3_p);
/** Prompt from the terminal for a file if one wasn't specified in the arguments */
void prompt_load_file_terminal(lc3_p, int, char *[]);
/** The main instruction cycle control flow */
void controller(lc3_p, display_p);
/** Coordinates TRAP functionality between the Display and LC3 */
void trap(display_p, lc3_p, word_t);
/** Opens a file with the given file name */
FILE *open_file(char *);
/** Saves a file with the given file name */
bool_t save_memory_to_file(char *, lc3_snapshot_t);
/** This function allows for the loading of hex files into memory. */
void load_file_to_memory(lc3_p, FILE *);
/** Main method for the LC-3 Emulator.
*
* The command line argument passed in is what will be populated into
* the LC-3's memory at runtime. For example, if the program is run with
* input "0x1694", that hexadecimal value will be stored into the instruction
* register (IR) and can be thought of in binary as 0001 0110 1001 0100, which
* (based on the four highest-order bits) is an ADD instruction for the LC-3
* (from its instruction set). */
int main(int argc, char *argv[]) {
/** Create and initialze the LC3 object */
lc3_p lc3 = lc3_create();
/** Prompt from the terminal for a file if one wasn't specified in the arguments */
prompt_load_file_terminal(lc3, argc, argv);
/** Create and initialize the Display object */
display_p disp = display_create();
/** Create a snapshot of LC3's components' current parameters and pass this struct by
* value into the Display component for displaying. This ensures the display only
* receieves a copy of all values in the LC3 and makes it impossible for Display to
* modify the LC3 without calling the appropriate methods. */
lc3_snapshot_t lc3_snapshot = lc3_get_snapshot(lc3);
display_update(disp, lc3_snapshot);
display_result_t result = display_loop(disp, lc3_snapshot);
/* If the user has selected MONITOR_STEP, then lets continue executing the
* LC-3 */
while (result != DISPLAY_QUIT) {
switch (result) {
case DISPLAY_EDIT_MEM:
prompt_edit_mem(lc3, disp);
break;
case DISPLAY_LOAD:
prompt_load_file_display(lc3);
lc3_snapshot = lc3_get_snapshot(lc3);
display_update(disp, lc3_snapshot);
break;
case DISPLAY_SAVE:
prompt_save_file_display(lc3);
break;
case DISPLAY_STEP:
if (lc3_is_halted(lc3) == FALSE) {
controller(lc3, disp);
lc3_snapshot = lc3_get_snapshot(lc3);
display_update(disp, lc3_snapshot);
}
break;
case DISPLAY_RUN:
do {
/** Run through the controller for this instruction */
controller(lc3, disp);
/** Update the display with new information (but don't wait for a
* keystroke) */
lc3_snapshot = lc3_get_snapshot(lc3);
display_update(disp, lc3_snapshot);
/** Sleep for 5 milliseconds */
usleep(5000);
} while (lc3_is_halted(lc3) == FALSE &&
display_has_breakpoint(disp, lc3_get_pc(lc3)) == FALSE);
}
lc3_snapshot = lc3_get_snapshot(lc3);
result = display_loop(disp, lc3_snapshot);
}
/* Memory cleanup. */
display_destroy(disp);
lc3_destroy(lc3);
return 0;
}
/** Prompt from and loads a hex file using the regular terminal before Display is loaded */
void prompt_load_file_terminal(lc3_p lc3, int argc, char *argv[]) {
/** The char array used to store the hex file's name */
char input_file_name[80];
/** The pointer to the file on disk */
FILE *file_ptr;
/*
* If there is an argument, attempt to use it first as the file name.
* Example file name: "/hex/HW3.hex"
*/
if (argc > 1) {
if (argc > 2) {
printf("Too many arguments supplied. The first argument will be treated as a file "
"name.\n");
}
file_ptr = open_file(argv[1]);
while (file_ptr == NULL) {
printf("File not found. Enter a file name: ");
scanf("%s", input_file_name);
file_ptr = open_file(input_file_name);
}
load_file_to_memory(lc3, file_ptr);
}
}
/** Prompts for and loads a hex file. */
void prompt_load_file_display(lc3_p lc3) {
char user_input[64];
display_get_file_name(user_input, sizeof(user_input) / sizeof(user_input[0]));
FILE *file_ptr = open_file(user_input);
while (file_ptr == NULL) {
display_get_file_error(user_input, sizeof(user_input) / sizeof(user_input[0]));
file_ptr = open_file(user_input);
}
load_file_to_memory(lc3, open_file(user_input));
display_get_file_success(user_input);
}
/** Prompts for and saves to a hex file. */
void prompt_save_file_display(lc3_p lc3) {
lc3_snapshot_t snapshot = lc3_get_snapshot(lc3);
char user_input[64];
display_save_file_name(user_input, sizeof(user_input) / sizeof(user_input[0]));
bool_t success = save_memory_to_file(user_input, snapshot);
while (success == FALSE) {
display_save_file_error(user_input, sizeof(user_input) / sizeof(user_input[0]));
success = save_memory_to_file(user_input, snapshot);
}
display_save_file_success(user_input);
}
/** Allows the display to edit memory */
void prompt_edit_mem(lc3_p lc3, display_p disp) {
char address_input[6];
display_edit_mem_get_address(address_input);
word_t address = get_word_from_string(address_input);
char data_input[6];
display_edit_mem_get_data(data_input, address_input);
word_t data = get_word_from_string(data_input);
lc3_set_memory(lc3, address, data);
lc3_snapshot_t snapshot = lc3_get_snapshot(lc3);
display_update(disp, snapshot);
display_edit_mem_success(disp, address_input, address);
}
/*
* The controller method of the LC-3. This contains much of the complete
* instruction cycle of the LC-3 */
void controller(lc3_p lc3, display_p disp) {
/** This is set to true at the end of the STORE phase and allows execution control to
* be passed back to the main loop */
bool_t is_cycle_complete = FALSE;
/** Ensuring that CPU pointer being passed into the controller is valid. */
if (!lc3) {
exit(1);
}
/** Beginning instruction cycle. */
lc3_set_state(lc3, STATE_FETCH);
while (!is_cycle_complete) {
switch (lc3_get_state(lc3)) {
/* The first state of the instruction cycle, the "fetch" state. */
case STATE_FETCH:
lc3_fetch(lc3);
lc3_set_state(lc3, STATE_DECODE);
break;
/* The second state of the instruction cycle, the "decode" state. */
case STATE_DECODE:
/* Corresponding to FSM state 32. */
lc3_decode(lc3);
lc3_set_state(lc3, STATE_EVAL_ADDR);
break;
/* The third state of the instruction cycle, the "evaluate address" state.
*/
case STATE_EVAL_ADDR:
switch (lc3_get_opcode(lc3)) {
case OPCODE_LD:
lc3_eval_addr_ld(lc3);
break;
case OPCODE_LDI:
lc3_eval_addr_ldi(lc3);
break;
case OPCODE_LDR:
lc3_eval_addr_ldr(lc3);
break;
case OPCODE_LEA:
lc3_eval_addr_lea(lc3);
break;
case OPCODE_ST:
lc3_eval_addr_st(lc3);
break;
case OPCODE_STI:
lc3_eval_addr_sti(lc3);
break;
case OPCODE_STR:
lc3_eval_addr_str(lc3);
break;
case OPCODE_JMP:
lc3_eval_addr_jmp(lc3);
break;
case OPCODE_JSR:
lc3_eval_addr_jsr(lc3);
break;
case OPCODE_BR:
lc3_eval_addr_br(lc3);
break;
}
lc3_set_state(lc3, STATE_FETCH_OP);
break;
/* The fourth state of the instruction cycle, the "fetch operands" state. */
case STATE_FETCH_OP:
switch (lc3_get_opcode(lc3)) {
case OPCODE_ADD:
lc3_fetch_op_add(lc3);
break;
case OPCODE_AND:
lc3_fetch_op_and(lc3);
break;
case OPCODE_NOT:
lc3_fetch_op_not(lc3);
break;
case OPCODE_TRAP:
lc3_fetch_op_trap(lc3);
break;
case OPCODE_LD:
lc3_fetch_op_ld(lc3);
break;
case OPCODE_LDI:
lc3_fetch_op_ldi(lc3);
break;
case OPCODE_LDR:
lc3_fetch_op_ldr(lc3);
break;
case OPCODE_ST:
lc3_fetch_op_st(lc3);
break;
case OPCODE_STI:
lc3_fetch_op_sti(lc3);
break;
case OPCODE_STR:
lc3_fetch_op_str(lc3);
break;
case OPCODE_STACK:
lc3_fetch_op_stack(lc3);
}
lc3_set_state(lc3, STATE_EXECUTE);
break;
/* The fifth state of the instruction cycle, the "execute" state. */
case STATE_EXECUTE:
switch (lc3_get_opcode(lc3)) {
case OPCODE_ADD:
lc3_execute_add(lc3);
break;
case OPCODE_AND:
lc3_execute_and(lc3);
break;
case OPCODE_NOT:
lc3_execute_not(lc3);
break;
case OPCODE_TRAP:
trap(disp, lc3, lc3_execute_trap(lc3));
break;
case OPCODE_BR:
lc3_execute_br(lc3);
break;
}
lc3_set_state(lc3, STATE_STORE);
break;
/* The sixth state of the instruction cycle, the "store" state. */
case STATE_STORE:
/* Corresponding to FSM state 16. */
switch (lc3_get_opcode(lc3)) {
// write back to register or store MDR into memory
case OPCODE_ADD:
lc3_store_add(lc3);
break;
case OPCODE_AND:
lc3_store_and(lc3);
break;
case OPCODE_JMP:
lc3_store_jmp(lc3);
break;
case OPCODE_JSR:
lc3_store_jsr(lc3);
break;
case OPCODE_LD:
lc3_store_ld(lc3);
break;
case OPCODE_LDI:
lc3_store_ldi(lc3);
break;
case OPCODE_LDR:
lc3_store_ldr(lc3);
break;
case OPCODE_NOT:
lc3_store_not(lc3);
break;
case OPCODE_ST:
lc3_store_st(lc3);
break;
case OPCODE_STI:
lc3_store_sti(lc3);
break;
case OPCODE_STR:
lc3_store_str(lc3);
break;
case OPCODE_LEA:
lc3_store_lea(lc3);
break;
case OPCODE_STACK:
lc3_store_stack(lc3);
}
is_cycle_complete = TRUE;
lc3_set_state(lc3, STATE_FETCH);
break;
} // end switch (state)
} // end while (isCycleComplete)
} // end controller()
/*
* This function that determines and executes the appropriate trap routine based
* on the trap vector passed.
*/
void trap(display_p disp, lc3_p lc3, word_t vector) {
char c;
switch (vector) {
case TRAP_VECTOR_X25:
/** HALT */
lc3_trap_x25(lc3);
break;
case TRAP_VECTOR_X20:
/** GETC */
c = display_get_input(disp);
lc3_trap_x20(lc3, c);
break;
case TRAP_VECTOR_X21:
/** OUT */
c = lc3_trap_x21(lc3);
display_print_output(disp, c);
break;
case TRAP_VECTOR_X22:
/** PUTS */
c = lc3_trap_x22(lc3);
while (c != '\0') {
display_print_output(disp, c);
c = lc3_trap_x22(lc3);
}
break;
}
}
/** This function allows for the opening of .hex files. */
FILE *open_file(char *file_name) {
/* Attempt to open file. If file isn't found or otherwise null, allow user to
press enter to return to main program of the menu. */
FILE *input_file_pointer;
input_file_pointer = fopen(file_name, "r");
return input_file_pointer;
}
/** This functions allows for the saving of .hex files. */
bool_t save_memory_to_file(char *file_name, lc3_snapshot_t lc3_snapshot) {
/* Attempt to save file. If file isn't found or otherwise null, allow user to
press enter to return to main program of the menu. */
FILE *output_file_pointer;
output_file_pointer = fopen(file_name, "w+");
if (output_file_pointer == NULL) {
return FALSE;
}
fprintf(output_file_pointer, "%04X\n", lc3_snapshot.starting_address);
int i;
// char string[6];
for (i = 0; i < MEMORY_SIZE; i++) {
// sprintf(string, "%04X\n", lc3_snapshot.memory_snapshot.data[i]);
fprintf(output_file_pointer, "%04X\n", lc3_snapshot.memory_snapshot.data[i]);
}
fclose(output_file_pointer);
return TRUE;
}
/** This function allows for the loading of hex files into memory. */
void load_file_to_memory(lc3_p lc3, FILE *file) {
char line[8];
fgets(line, sizeof(line), file);
/** Set the starting address */
word_t data = strtol(line, NULL, 16);
lc3_set_starting_address(lc3, data);
/* Read through file line by line and store to CPU memory. */
int i = 0;
while (fscanf(file, "%hx", &data) != EOF) {
lc3_set_memory(lc3, lc3_get_starting_address(lc3) + i, data);
i += 1;
}
if (lc3_has_file_loaded(lc3) == FALSE) {
lc3_toggle_file_loaded(lc3);
}
}