-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.c
207 lines (196 loc) · 6.53 KB
/
execute.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
#include <stdio.h>
#include <stdlib.h>
#include "execute.h"
#define R 0
#define J 2
#define JAL 3
#define MASK 31
//{"and","or","add","addu","sll","srl","sra","sub","sltu","jr","syscall"};
const int R_CODES[] = {0x24,0x25,0x20,0x21,0x00,0x02,0x03,0x22,0x2B,0x08,0xC};
//{"addi", "addiu", "sltiu", "beq", "bne", "lw", "sw", "ori", "lui"};
const int I_CODES[] = {0x08,0x09,0x0B,4,5,0x23,0x2B,0x0D,0x0F};
Execute *ExecuteInit(int **pc, int *mem, DecodeExecute *inBucket,
ExecuteMemAccess *outBucket) {
Execute *res = malloc(sizeof(Decode));
res->count = 0;
res->inBucket = inBucket;
res->outBucket = outBucket;
res->pc = pc;
res->mem = mem;
return res;
}
/**
* Now that all values have been resolved, do the operation.
* If it's a memory read or write, you can't do that here. You need to
* notify the memory access phase that something needs to be done by
* writing to the out bucket.
*/
void ExecutePhase(Execute *execute, int *halt, int *flush) {
DecodeExecute *instr = execute->inBucket;
ExecuteMemAccess *out = execute->outBucket;
if (instr->ready == 0)
return;
instr->ready = 0;
int func, br;
execute->count++;
switch (instr->opcode){
case R:
for(func = 0, br = 1; br;)
if(R_CODES[func] == instr->funct)
br = 0;
else func++;
switch (func){
case 0: //and
out->value = instr->rs & instr->rt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 1: //or
out->value = instr->rs | instr->rt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 2: //add
out->value = instr->rs + instr->rt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 3: //addu
out->value = instr->rs + instr->rt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 4: //sll
out->value = instr->rt << instr->shamt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 5: //srl
out->value = (unsigned int)instr->rt >> instr->shamt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 6: //sra
out->value = instr->rt >> instr->shamt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 7: //sub
out->value = instr->rs - instr->rt;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 8: //sltu
out->value = ((unsigned int)instr->rs < (unsigned int)instr->rt) ? 1 : 0;
out->reg = instr->rd;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 9: //jr
out->value = (long) *(execute->pc);
out->reg = 31;
*flush = 1;
*execute->pc = (int *) instr->rs;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 10: //syscall (assume halt)
*halt = 1;
break;
}
break;
case J:
*execute->pc = execute->mem + (unsigned int) instr->imm;
*flush = 1;
break;
case JAL:
out->value = (long) *(execute->pc);
out->reg = 31;
*flush = 1;
*execute->pc = execute->mem + (unsigned int) instr->imm;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
default:
for(func = 0, br = 1; br;)
if(I_CODES[func] == instr->opcode)
br = 0;
else func++;
switch (func){
case 0: //addi
if(instr->imm >> 15) instr->imm |= 0xFFFF0000;
out->value = instr->rs + instr->imm;
out->reg = instr->rt;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 1: //addiu
out->value = (unsigned int)instr->rs + (unsigned int)instr->imm;
out->reg = instr->rt;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 2: //sltiu
out->value = instr->rs < instr->imm ? 1 : 0;
out->reg = instr->rt;
out->operation = OPERATION_NONE;
out->ready = 1;
break;
case 3: //beq
if(instr->imm >> 15)
instr->imm |= 0xFFFF0000;
instr->rs &= 0x0FFFFFFFF;
instr->rtval &= 0x0FFFFFFFF;
if((int) instr->rs == (int) instr->rtval) {
*(execute->pc) += (int) instr->imm - 2;
*flush = 1;
}
break;
case 4: //bne
if(instr->imm >> 15)
instr->imm |= 0xFFFF0000;
instr->rs &= 0x0FFFFFFFF;
instr->rtval &= 0x0FFFFFFFF;
if((int) instr->rs != (int) instr->rtval) {
*(execute->pc) += (int) instr->imm - 2;
*flush = 1;
}
break;
case 5: //lw
if(instr->imm >> 15)
instr->imm |= 0xFFFF0000;
out->address = instr->rs + instr->imm;
out->reg = instr->rt;
out->operation = OPERATION_READ;
out->ready = 1;
break;
case 6: //sw
if(instr->imm >> 15)
instr->imm |= 0xFFFF0000;
out->address = instr->rs + instr->imm;
out->value = instr->rt;
out->operation = OPERATION_WRITE;
out->ready = 1;
break;
case 7: //ori
out->value = instr->rs | instr->imm;
out->reg = instr->rt;
out->ready = 1;
break;
case 8: //lui
out->value = instr->imm << 16;
out->reg = instr->rt;
out->ready = 1;
break;
}
break;
}
}