-
Notifications
You must be signed in to change notification settings - Fork 0
/
writebackStage.c
118 lines (113 loc) · 2.8 KB
/
writebackStage.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
#include <stdbool.h>
#include "writebackStage.h"
#include "tools.h"
#include "dump.h"
#include "instructions.h"
#include "forwarding.h"
#include "registers.h"
#include "status.h"
//W register holds the input for the writeback stage.
//It is only accessible from this file. (static)
static wregister W;
// Function: getWregister
// Description: Returns a copy of the W register
// Params: none
// Returns: wregister
// Modifies: none
wregister getWregister()
{
return W;
}
// Function: clearWregister
// Description: Returns a copy of the W register
// Params: none
// Returns: none
// Modifies: W
void clearWregister()
{
clearBuffer((char *) &W, sizeof(W));
W.stat = AOK;
W.icode = nop;
}
// Function: updateWregister
// Description: Returns a copy of the W register
// Params: stat, icode, valE, valM, dstE, dstM
// Returns: None
// Modifies: stat, icode, valE, valM, dstE, dstM
void updateWregister(int stat, int icode, int valE, int valM, int dstE, int dstM)
{
W.stat = stat;
W.icode = icode;
W.valE = valE;
W.valM = valM;
W.dstE = dstE;
W.dstM = dstM;
}
// Function: writebackStage
// Description: Simulates the writebackStage of the y86 pipe
// Params: *forward, *status
// Returns: Bool
// Modifies: None
bool writebackStage(forwardType *forward, statusType *status)
{
forward->W_dstE = W.dstE;
forward->W_valE = W.valE;
forward->W_valM = W.valM;
forward->W_dstM = W.dstM;
status->W_stat = W.stat;
forward->W_icode = W.icode;
//setRegister(W.dstE, W.valE);
//setRegister(W.dstM, W.valM);
/*if (W.icode == mrmovl || W.icode == popl)
{
setRegister(W.dstM, W.valM);
}
if(W.icode == OPL || W.icode == rrmovl || W.icode == irmovl || W.icode == popl || W.icode == pushl)
{
setRegister(W.dstE, W.valE);
}*/
if(W.dstM == none)
{
W.valM = 0;
}
if (W.stat == HLT || W.stat == INS || W.stat == ADR)
{
if(W.stat == INS)
{
printf("Invalid instruction\n");
dumpProgramRegisters();
dumpProcessorRegisters();
dumpMemory();
}
if(W.stat == ADR)
{
printf("Invalid memory address\n");
dumpProgramRegisters();
dumpProcessorRegisters();
dumpMemory();
}
return true;
}
else
{
setRegister(W.dstE, W.valE);
setRegister(W.dstM, W.valM);
if (W.icode == dump)
{
int temp_array[3] = {getBits(0,0,W.valE),getBits(1,1,W.valE),getBits(2,2,W.valE)};
if (temp_array[0])
{
dumpProgramRegisters();
}
if (temp_array[1])
{
dumpProcessorRegisters();
}
if (temp_array[2])
{
dumpMemory();
}
}
return false;
}
}