-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
104 lines (89 loc) · 2.92 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "mtypes.h"
#include "instructions.h"
#include "main.h"
void load_program(int fd, proc_t *p) {
char *f;
ssize_t fsize;
fsize = lseek(fd, 0, SEEK_END);
f = mmap(0, fsize, PROT_READ, MAP_SHARED, fd, 0);
for (int i = 0; *f != '\n'; f += 33, i++) {
char n[33];
memcpy(n, f, 32);
n[32] = 0;
p->text_32[i] = atoi_binary(n);
}
f++;
for (int i = 0; *f != '\0'; f += 33, i++) {
char n[33];
memcpy(n, f, 32);
n[32] = 0;
p->data_32[i] = atoi_binary(n);
}
}
void print_machine(proc_t *p) {
int i = 0;
printf("%d.\t%s\t 0x%08x\n", i+1, "$zero", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$at", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$v0", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$v1", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$a0", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$a1", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$a2", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$a3", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t0", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t1", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t2", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t3", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t4", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t5", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t6", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t7", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s0", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s1", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s2", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s3", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s4", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s5", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s6", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$s7", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t8", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$t9", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$k0", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "$k1", p->GPR[i]); i++;
printf("%d.\t%s\t 0x%08x\n", i+1, "pc", p->pc);
}
int main(int argc, char *argv[]) {
int input;
proc_t p;
input = open(argv[1], O_RDONLY);
memset(&p, 0, sizeof(proc_t));
load_program(input, &p);
for (p.pc = 0; p.pc < MEM_SIZE; p.pc += 4) {
instruction_t i;
i.data = p.mem_32[p.pc/4];
if (i.data == 0) {
p.pc += 4;
print_machine(&p);
return 0;
}
#ifdef DEBUG
printf("Line num: %d\t", p.pc/4 + 1);
#endif
void (*op)(instruction_t, proc_t*) = get_instruction(i);
op(i, &p);
#ifdef DEBUG
printf("\n");
print_machine(&p);
printf("xx.\t$ra\t 0x%08x\n", p.ra);
getchar();
#endif
}
return 0;
}