-
Notifications
You must be signed in to change notification settings - Fork 0
/
decodeStage.c
executable file
·240 lines (228 loc) · 5.88 KB
/
decodeStage.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
#include "bool.h"
#include "tools.h"
#include <stdio.h>
#include "instructions.h"
#include "status.h"
#include "forwarding.h"
#include "bubbling.h"
#include "decodeStage.h"
#include "executeStage.h"
#include "registers.h"
/* prototypes of Functions only called within this file */
static unsigned int getSrcA();
static unsigned int getSrcB();
static unsigned int getDstE();
static unsigned int getDstM();
static unsigned int selectFwdA(forwardType *forward, unsigned int d_srcA);
static unsigned int selectFwdB(forwardType *forward, unsigned int d_srcB);
static bool E_bubble(bubbleType bubble, unsigned int srcA, unsigned int srcB);
static void printDregister();
//D register holds the input for the decode stage.
//It is only accessible from this file. (static)
static dregister D;
/*
unsigned int d_srcA;
unsigned int d_srcB;
unsigned int d_dstE;
unsigned int d_dstM;
*/
// Function: getDregister
// Description: Returns a copy of the D register
// Params: none
// Returns: dregister
// Modifies: none
dregister getDregister()
{
return D;
}
void decodeStage(forwardType forward, bubbleType *bubble)
{
bubble->D_icode = D.icode;
unsigned int d_srcA;
unsigned int d_srcB;
unsigned int d_dstE;
unsigned int d_dstM;
unsigned int srcA = getSrcA();
unsigned int srcB = getSrcB();
unsigned int valA = selectFwdA(&forward, srcA);
unsigned int valB = selectFwdB(&forward, srcB);
unsigned int dstE = getDstE();
unsigned int dstM = getDstM();
bubble->d_srcA = srcA;
bubble->d_srcB = srcB;
if(!E_bubble(*bubble, srcA, srcB))
{
updateEregister(D.stat, D.icode, D.ifun, D.valC, valA, valB, dstE, dstM, srcA, srcB);
}
else
{
updateEregister(1, NOP, 0, 0, 0, 0, RNONE, RNONE, RNONE, RNONE);
}
}
// Function: E_bubble
// Description: Determines if the E Register needs bubbled
// Params: bubble- Bubbling struct
// Returns: bool- TRUE if needs to be bubblefd
// Modifies: none
bool E_bubble(bubbleType bubble, unsigned int srcA, unsigned int srcB)
{
bool first = bubble.E_icode == JXX && !bubble.e_Cnd;
bool sec = bubble.E_icode == MRMOVL || bubble.E_icode == POPL;
bool third = bubble.E_dstM == srcA || bubble.E_dstM == srcB;
return first || (sec && third);
}
// Function: getSrcA
// Description: decides srcA register
// Params: none
// Returns: register to use for SrcA
// Modifies: none
unsigned int getSrcA()
{
switch(D.icode)
{
case RRMOVL:
case RMMOVL:
case OPL:
case PUSHL:
return D.rA;
case POPL:
case RET:
return ESP;
}
return RNONE;
}
// Function: getSrcB
// Description: decides srcB register
// Params: none
// Returns: register to use for srcB
// Modifes: none
unsigned int getSrcB()
{
switch(D.icode)
{
case OPL:
case RMMOVL:
case MRMOVL:
return D.rB;
case PUSHL:
case POPL:
case CALL:
case RET:
return ESP;
}
return RNONE;
}
// Function: getDstE
// Description: decides dstE register
// Params: none
// Returns: register to use for dstE
// Modifies: none
unsigned int getDstE()
{
switch(D.icode)
{
case RRMOVL:
case IRMOVL:
case OPL:
return D.rB;
case PUSHL:
case POPL:
case CALL:
case RET:
return ESP;
}
return RNONE;
}
// Function: getDstM
// Description: decides dstM register
// Params: none
// Returns: register to use for dstM
// Modifies: none
unsigned int getDstM()
{
if(D.icode == MRMOVL || D.icode == POPL)
{
return D.rA;
}
return RNONE;
}
// Function: selectFwdA
// Description: decides if data needs forwarded through pipeline
// Params: forwardType- *forward uint- srcA
// Returns: int -value to forward
// Modifies: none
unsigned int selectFwdA(forwardType *forward, unsigned int d_srcA)
{
switch (D.icode)
{
case CALL:
case JXX:
return D.valP;
}
if(d_srcA == RNONE)
{
return 0;
}
if(d_srcA == forward->e_dstE)
return forward->e_valE;
if(d_srcA == forward->M_dstM)
return forward->m_valM;
if(d_srcA == forward->M_dstE)
return forward->M_valE;
if(d_srcA == forward->W_dstM)
return forward->W_valM;
if(d_srcA == forward->W_dstE)
return forward->W_valE;
return getRegister(d_srcA);
}
// Function: selectFwdB
// Description: decides if data needs forwarded through pipeline
// Params: forwardType- *forward uint- srcB
// Returns: int -value to forward
// Modifies: none
unsigned int selectFwdB(forwardType *forward, unsigned int d_srcB)
{
if(d_srcB == RNONE)
return 0;
if(d_srcB == forward->e_dstE)
return forward->e_valE;
if(d_srcB == forward->M_dstM)
return forward->m_valM;
if(d_srcB == forward->M_dstE)
return forward->M_valE;
if(d_srcB == forward->W_dstM)
return forward->W_valM;
if(d_srcB == forward->W_dstE)
return forward->W_valE;
return getRegister(d_srcB);
}
// Function: updateDRegister
// Description: Sets D Register to specified values
// Params: stat -current pipeline status icode -current instr
// ifun -additional info on instr rA -rA of current instr
// rB -rB of current instr valC -constant word of current instr
// valP -valP of current instr
// Returns: none
// Modifies: D Register
void updateDRegister(unsigned int stat, unsigned int icode, unsigned int ifun,
unsigned int rA, unsigned int rB, unsigned int valC, unsigned int valP)
{
D.stat = stat;
D.icode = icode;
D.ifun = ifun;
D.rA = rA;
D.rB = rB;
D.valC = valC;
D.valP = valP;
}
// Function: clearDregister
// Description: Clears the D register
// Params: none
// Returns: void
// Modifies: D
void clearDregister()
{
D.stat = AOK;
D.icode = NOP;
clearBuffer((char *) &D, sizeof(D));
}