-
Notifications
You must be signed in to change notification settings - Fork 0
/
executeStage.c
executable file
·349 lines (326 loc) · 7.76 KB
/
executeStage.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "bool.h"
#include "tools.h"
#include <stdio.h>
#include "instructions.h"
#include "status.h"
#include "bubbling.h"
#include "forwarding.h"
#include "executeStage.h"
#include "memoryStage.h"
#include "registers.h"
/* prototypes of Functions only called within this file */
static int doNothing();
static int performDump();
static int performRrmovl();
static int performRmmovl();
static int performMrmovl();
static int performIrmovl();
static int performOpl();
static int performJXX();
static int performCALL();
static int performRET();
static int performPush();
static int performPop();
static void printEregister();
// E register holds the input for the execute stage.
// It is only accessible from this file. (static)
static eregister E;
int (*funcArr[16])();
// Function: getEregister
// Description: Returns a copy of the E address
// Params: none
// Returns: eregister
// Modifies: none
eregister getEregister()
{
return E;
}
void executeStage(forwardType *forward, statusType *status, bubbleType *bubble)
{
bubble->E_icode = E.icode;
bubble->E_dstM = E.dstM;
unsigned int valE = 0;
unsigned int e_dstE;
bool e_Cnd = FALSE;
if(E.icode == RRMOVL || E.icode == JXX)
{
e_Cnd = calculateCnd();
}
if(E.stat != INS)
{
valE = (*funcArr[E.icode])();
}
if(E.icode == RRMOVL || E.icode == JXX)
{
e_Cnd = calculateCnd();
}
if((E.icode == RRMOVL) && !e_Cnd)
{
e_dstE = RNONE;
}
else
{
e_dstE = E.dstE;
}
forward->e_dstE = E.dstE;
forward->e_valE = valE;
bubble->e_Cnd = e_Cnd;
if(isException(status))
{
setCC(OF, 0);
setCC(ZF, 0);
setCC(SF, 0);
updateMregister(1, NOP, 0, 0, 0, RNONE, RNONE);
}
else
{
updateMregister(E.stat, E.icode, e_Cnd, valE, E.valA, e_dstE, E.dstM);
}
}
// Function: isException
// Description: Determines whether memory stage should be bubbled
// Params: status -Status struct
// Returns: bool -TRUE if memory stage needs stalled
// Modifies: none
bool isException (statusType *status)
{
switch (status->m_stat)
{
case ADR:
case INS:
case HLT:
return TRUE;
}
switch (status->W_stat)
{
case ADR:
case INS:
case HLT:
return TRUE;
}
return 0;
}
// Function: initializeFuncPtrArray
// Description: initializes the function ptr array
// Params: none
// Returns: void
// Modifies: funcArr[]
void initializeFuncPtrArray()
{
funcArr[HALT] = &doNothing;
funcArr[NOP] = &doNothing;
funcArr[RRMOVL] = &performRrmovl;
funcArr[IRMOVL] = &performIrmovl;
funcArr[RMMOVL] = &performRmmovl;
funcArr[MRMOVL] = &performMrmovl;;
funcArr[OPL] = &performOpl;
funcArr[JXX] = &performJXX;
funcArr[CALL] = &performCALL;
funcArr[RET] = &performRET;
funcArr[PUSHL] = &performPush;
funcArr[POPL] = &performPop;
funcArr[DUMP] = &performDump;
}
// Function: doNothing
// Description: does nothing in the array of function ptrs (funcArr[])
// Params: none
// Returns: 0
// Modifies: none
int doNothing()
{
return 0;
}
// Function: performIrmovl
// Description: Simulates IRMOVL instruction
// Params: none
// Returns: E.valC
// Modifies: none
int performIrmovl()
{
return E.valC;
}
// Function: calculateCnd
// Description: Determines Cnd based on condition codes
// Params: none
// Returns: bool
// Modifies: none
bool calculateCnd()
{
unsigned int e_OF = getCC(OF);
unsigned int e_ZF = getCC(ZF);
unsigned int e_SF = getCC(SF);
switch(E.ifun)
{
case RRFUN:
return TRUE;
case CMOVLE:
return ((e_SF^e_OF)|e_ZF);
case CMOVL:
return (e_SF ^ e_OF);
case CMOVE:
return e_ZF;
case CMOVNE:
return !e_ZF;
case CMOVGE:
return (!(e_SF ^ e_OF));
case CMOVG:
return ((!(e_SF^ e_OF)) & (!e_ZF));
default:
return FALSE;
}
}
// Function: performMrmovl
// Description: Simulates the MRMOVL instruction
// Params: none
// Returns: int- E.valB + E.valC
// Modifies: none
int performMrmovl()
{
return E.valB + E.valC;
}
// Function performRmmovl
// Description: Simulates the RMMOVL instruction
// Params: none
// Returns int- E.valB + E.valC
// Modifies: none
int performRmmovl()
{
return E.valB + E.valC;
}
// Function: performRrmovl
// Description: Simulates the RRMOVL instruction
// Params: none
// Returns: int- E.valA
// Modifies: none
int performRrmovl()
{
return E.valA;
}
// Function: performOpl
// Description: Simulates OPL instruction
// Params: none
// Returns: int -result of operation
// Modifies: E.stat
int performOpl(){
int result = 0;
switch(E.ifun)
{
case ADDL:
result = E.valB + E.valA;
if((signed)isNegative(E.valA) == (signed)isNegative(E.valB) && ((signed)isNegative(result) != (signed)isNegative(E.valA)))
setCC(OF, 1);
else
setCC(OF, 0);
break;
case SUBL:
result = E.valB - E.valA;
if(((signed)isNegative(E.valB)) != ((signed) isNegative(E.valA)) && ((signed)isNegative(E.valA)) == ((signed) isNegative(result)))
setCC(OF, 1);
else
setCC(OF, 0);
break;
case ANDL:
result = E.valB & E.valA;
setCC(OF, 0);
break;
case XORL:
result = E.valB ^ E.valA;
setCC(OF, 0);
break;
default:
E.stat = INS;
break;
}
setCC(ZF, result == 0);
setCC(SF, result < 0);
return result;
}
// Function: performJXX
// Description: Simulates the JXX instruction
// Params: none
// Returns: int- E.valC
// Modifies: none
int performJXX()
{
return E.valC;
}
// Function performCALL
// Description: Simulates the CALL instruction
// Params: none
// Returns: int- E.valB + (-4)
// Modifies: none
int performCALL()
{
return E.valB + (-4);
}
// Function: performRET
// Description: Simulates the RET instruction
// Params: none
// Returns: int- E.valB + 4;
// Modifies: none
int performRET()
{
return E.valB + 4;
}
// Function: performPush
// Description: Simulates the PUSH instruction
// Params: none
// Returns: int- E.valB + (-4)
// Modifies: none
int performPush()
{
return E.valB + (-4);
}
// Function: performPop
// Description: Simulates the POP instruction
// Params: none
// Returns: int- E.valB + 4
// Modifes: none
int performPop()
{
return E.valB + 4;
}
// Function: performDump
// Description: simulates DUMP instruction
// Params: none
// Returns: E.valC
// Modifies: none
int performDump()
{
return E.valC;
}
// Function: updateEregister
// Description:
// Params: stat -current pipeline status icode -current instr
// ifun -aditional info on instr valC -valC of current instr
// valA -valA of current instr valB -valB of current instr
// dstE -dstE of current instr dstM -dstM of current instr
// srcA -srcA of current instr srcB -srcB of current instr
// Returns: void
// Modifies: E Register
void updateEregister(unsigned int stat, unsigned int icode, unsigned int ifun,
unsigned int valC, unsigned int valA, unsigned int valB, unsigned int dstE,
unsigned int dstM, unsigned int srcA, unsigned int srcB)
{
E.stat = stat;
E.icode = icode;
E.ifun = ifun;
E.valC = valC;
E.valA = valA;
E.valB = valB;
E.dstE = dstE;
E.dstM = dstM;
E.srcA = srcA;
E.srcB = srcB;
}
// Funstion clearEregister
// Description: Clears contents of the E register
// Params: none
// Returns: void
// Modifies: E
void clearEregister()
{
E.stat = AOK;
E.icode = NOP;
clearBuffer((char *) &E, sizeof(E));
}