-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
186 lines (150 loc) · 5.19 KB
/
main.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
/*
* CS 261: Main driver
*
* Name: Sayemum Hassan
*/
#include "p1-check.h"
#include "p2-load.h"
#include "p3-disas.h"
#include "p4-interp.h"
int main (int argc, char **argv)
{
//<<-- PART 1 -->>
// parse command-line options
bool print_header = false;
bool print_phdrs = false;
bool print_membrief = false;
bool print_memfull = false;
bool disas_code = false;
bool disas_data = false;
bool exec_normal = false;
bool exec_debug = false;
char *filename = NULL;
if (!parse_command_line_p4(argc, argv, &print_header, &print_phdrs,
&print_membrief, &print_memfull, &disas_code, &disas_data, &exec_normal, &exec_debug, &filename)) {
exit(EXIT_FAILURE);
}
if (filename != NULL) {
// open Mini-ELF binary
FILE *openFile = fopen(filename, "r");
if (!openFile) {
printf("Failed to read file\n");
exit(EXIT_FAILURE);
}
// P1: load and check Mini-ELF header
elf_hdr_t hdr;
if (!read_header(openFile, &hdr)) {
printf("Failed to read file\n");
exit(EXIT_FAILURE);
}
// P1 output
if (print_header) {
dump_header(&hdr);
}
//<<-- PART 2 -->>
// Read all phdrs
elf_phdr_t phdrs[hdr.e_num_phdr];
for (uint16_t i = 0; i < hdr.e_num_phdr; i++) {
// Read each phdr from file and load into elements
if (!read_phdr(openFile, hdr.e_phdr_start + (i * 20), &phdrs[i])) {
printf("Failed to read file\n");
exit(EXIT_FAILURE);
}
}
// Print Headers
if (print_phdrs) {
dump_phdrs(hdr.e_num_phdr, phdrs);
}
// Load Segments after Creating Memory
byte_t* memory = (byte_t*)calloc(MEMSIZE, 1);
for (uint16_t i = 0; i < hdr.e_num_phdr; i++) {
// Load each phdr into memory
if (!load_segment(openFile, memory, &phdrs[i])) {
printf("Failed to read file\n");
exit(EXIT_FAILURE);
}
}
// Print contents of memory (if requested)
if (print_membrief) {
for (uint16_t i = 0; i < hdr.e_num_phdr; i++) {
dump_memory(memory, phdrs[i].p_vaddr, phdrs[i].p_vaddr + phdrs[i].p_size);
}
} else if (print_memfull) {
dump_memory(memory, 0, MEMSIZE);
}
//<<-- PART 3 -->>
// Disasemble code contents (if requested)
if (disas_code) {
printf("Disassembly of executable contents:\n");
for (uint16_t i = 0; i < hdr.e_num_phdr; i++) {
disassemble_code(memory, &phdrs[i], &hdr);
}
}
if (disas_data) {
printf("Disassembly of data contents:\n");
for (uint16_t i = 0; i < hdr.e_num_phdr; i++) {
disassemble_data(memory, &phdrs[i]);
}
}
//<<-- PART 4 -->>
y86_t cpu; /* main cpu struct */
bool cnd = 0; /* condition signal */
y86_reg_t valA = 0; /* intermediate register */
y86_reg_t valE = 0; /* intermediate register */
uint64_t totalExecutionCount = 0;
/* initilize cpu */
memset(&cpu, 0, sizeof(y86_t));
cpu.stat = AOK;
cpu.pc = hdr.e_entry;
cpu.zf = 0;
cpu.sf = 0;
cpu.of = 0;
// initilize each register as 0
for (uint64_t i = 0; i < NUMREGS; i++) {
cpu.reg[i] = 0;
}
// Print beginning cpu state if exec_debug
if (exec_debug) {
printf("Beginning execution at 0x%04x\n", hdr.e_entry);
dump_cpu_state(&cpu);
printf("\n");
}
/* main cpu cycle */
while (cpu.stat == AOK) {
y86_inst_t ins = fetch(&cpu, memory);
if (cpu.stat != INS && cpu.stat != ADR && ins.icode != INVALID) {
valE = decode_execute(&cpu, ins, &cnd, &valA);
// ERROR CHECK: decode/execute failed
memory_wb_pc(&cpu, ins, memory, cnd, valA, valE);
totalExecutionCount++;
/* trace output (if requested) */
if (exec_debug) {
printf("Executing: ");
disassemble(&ins);
printf("\n");
dump_cpu_state(&cpu);
}
if (exec_debug && cpu.stat == AOK) {
printf("\n");
}
} else {
if (exec_debug) {
printf("Invalid instruction at 0x%04x\n", memory[cpu.pc]);
}
}
}
if (exec_normal) {
printf("Beginning execution at 0x%04x\n", hdr.e_entry);
dump_cpu_state(&cpu);
printf("Total execution count: %lu\n", totalExecutionCount);
}
if (exec_debug) {
printf("Total execution count: %lu\n\n", totalExecutionCount);
dump_memory(memory, 0, MEMSIZE);
}
// cleanup
free(memory);
fclose(openFile);
}
return EXIT_SUCCESS;
}