-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstructions.c
146 lines (124 loc) · 3.08 KB
/
instructions.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "mtypes.h"
#include "instructions.h"
void add(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("add $%d $%d $%d", i.rd, i.rs, i.rt);
#endif
p->GPR[i.rd] = p->GPR[i.rs] + p->GPR[i.rt];
}
void sub(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("sub $%d $%d $%d", i.rd, i.rs, i.rt);
#endif
p->GPR[i.rd] = p->GPR[i.rs] - p->GPR[i.rt];
}
void or(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("or $%d $%d $%d", i.rd, i.rs, i.rt);
#endif
p->GPR[i.rd] = p->GPR[i.rs] | p->GPR[i.rt];
}
void and(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("and $%d $%d $%d", i.rd, i.rs, i.rt);
#endif
p->GPR[i.rd] = p->GPR[i.rs] & p->GPR[i.rt];
}
void slt(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("slt $%d $%d $%d", i.rd, i.rs, i.rt);
#endif
p->GPR[i.rd] = (p->GPR[i.rs] < p->GPR[i.rt]);
}
void sll(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("sll $%d $%d %d", i.rd, i.rt, i.sa);
#endif
p->GPR[i.rd] = p->GPR[i.rt] << i.sa;
}
void srl(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("srl $%d $%d %d", i.rd, i.rt, i.sa);
#endif
p->GPR[i.rd] = p->GPR[i.rt] >> i.sa;
}
void jr(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("jr $%d", i.rs);
#endif
p->pc = p->GPR[i.rs] - 4;
}
void lw(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("lw $%d 0x%X($%d)", i.rt, i.s_immediate, i.rs);
#endif
p->GPR[i.rt] = p->mem_32[(p->GPR[i.rs] + i.s_immediate)/4];
}
void sw(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("sw $%d 0x%X($%d)", i.rt, i.s_immediate, i.rs);
#endif
p->mem_32[(p->GPR[i.rs] + i.s_immediate)/4] = p->GPR[i.rt];
}
void addi(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("addi $%d $%d 0x%X", i.rt, i.rs, i.s_immediate);
#endif
p->GPR[i.rt] = p->GPR[i.rs] + i.s_immediate;
}
void ori(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("ori $%d $%d 0x%X", i.rt, i.rs, i.z_immediate);
#endif
p->GPR[i.rt] = p->GPR[i.rs] | i.z_immediate;
}
void andi(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("andi $%d $%d 0x%X", i.rt, i.rs, i.s_immediate);
#endif
p->GPR[i.rt] = p->GPR[i.rs] & i.s_immediate;
}
void slti(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("slti $%d $%d 0x%X", i.rt, i.rs, i.s_immediate);
#endif
p->GPR[i.rt] = (p->GPR[i.rs] < i.s_immediate);
}
void beq(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("beq $%d $%d 0x%X", i.rt, i.rs, i.s_immediate);
#endif
if (p->GPR[i.rs] == p->GPR[i.rt]) {
p->pc = p->pc + (i.s_immediate << 2);
}
}
void bne(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("bne $%d $%d 0x%X", i.rt, i.rs, i.s_immediate);
#endif
if (p->GPR[i.rs] != p->GPR[i.rt]) {
p->pc = p->pc + (i.s_immediate << 2);
}
}
void lui(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("lui $%d 0x%X", i.rt, i.z_immediate);
#endif
p->GPR[i.rt] = (i.z_immediate << 16);
}
void j(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("j 0x%X", i.instr_index);
#endif
p->pc = ((p->pc >> 28) | (i.instr_index << 2)) - 4;
}
void jal(instruction_t i, proc_t *p) {
#ifdef DEBUG
printf("jal 0x%X", i.instr_index);
#endif
p->GPR[31] = p->pc + 4;
p->pc = ((p->pc >> 28) | (i.instr_index << 2)) - 4;
}