-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrace.c
65 lines (57 loc) · 2.08 KB
/
trace.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
#include <stdlib.h>
#include <stdlib.h>
#include "ObjectFiles.h"
int main(int argc, char** argv) {
//initialize 3 basic fields
MachineState *theMachineState;
ControlSignals *theControlSignals;
DatapathSignals *theDatapath;
//other variables
FILE *output_file;
char *obj_file_name;
int i, num_obj_files;
unsigned short instruction;
unsigned int exception_check;
unsigned short curr_PC;
unsigned int valid_instruction_check;
theMachineState = malloc(sizeof(MachineState));
theControlSignals = malloc(sizeof(ControlSignals));
theDatapath = malloc(sizeof(DatapathSignals));
//initialize state (reset)
Reset(theMachineState);
//read object files and load into memory
output_file = fopen(argv[1], "w");
num_obj_files = argc - 2;
for (i = 0; i < num_obj_files; i++) {
obj_file_name = argv[i+2];
ReadObjectFile(obj_file_name, theMachineState);
}
//simulate one instruction at a time and print instruction / PC
while (theMachineState->PC != 0x80FF) {
instruction = theMachineState->memory[theMachineState->PC];
curr_PC = theMachineState->PC;
valid_instruction_check = DecodeCurrentInstruction(instruction, theControlSignals);
if (valid_instruction_check == 1) {
break;
}
SimulateDatapath(theControlSignals, theMachineState, theDatapath);
exception_check = UpdateMachineState(theControlSignals, theMachineState, theDatapath);
if (exception_check > 0) {
break;
}
//print to output
fwrite(&curr_PC, sizeof(unsigned short), 1, output_file);
fwrite(&instruction, sizeof(unsigned short), 1, output_file);
printf("-----------------------------------------------\n");
}
//write last PC and instruction values if the loop did NOT end with an exception or error
if (exception_check == 0 && valid_instruction_check == 0) {
fwrite(&theMachineState->PC, sizeof(unsigned short), 1, output_file);
instruction = theMachineState->memory[theMachineState->PC];
fwrite(&instruction, sizeof(unsigned short), 1, output_file);
}
free(theMachineState);
free(theControlSignals);
free(theDatapath);
return 0;
}