-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFSM_DIRECTORY_cpu.v
254 lines (231 loc) · 8.39 KB
/
FSM_DIRECTORY_cpu.v
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
module directoryStateMachineCPU(
input operation, hit, input[1:0] cpuInitialState,
output reg readMissOnBus, writeMissOnBus, invalidateOnBus, dataWriteBack,
output reg [1:0] cpuNewState
);
//
//invalidate
//shared
//modified
//escrita 1, leitura 0
localparam INVALID = 2'b01, SHARED = 2'b10, MODIFIED = 2'b11;
initial begin
readMissOnBus <= 1'b0;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
always @(*) begin
case(cpuInitialState)
INVALID: begin
if(operation == 1'b0) begin //read
cpuNewState <= SHARED;
readMissOnBus <= 1'b1;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
else if(operation==1'b1) begin //write
cpuNewState <= MODIFIED;
writeMissOnBus <= 1'b1;
readMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
end
SHARED: begin
case({operation, hit})
2'b00: begin //read miss PRA ESCRITA SEMPRE MODIFIED
cpuNewState <= SHARED;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b1;
dataWriteBack <= 1'b0;
end
2'b01: begin //read hit
cpuNewState <= SHARED;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
2'b10: begin // write miss
cpuNewState <= MODIFIED;
writeMissOnBus <= 1'b1;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
2'b11: begin // write hit
cpuNewState <= MODIFIED;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b1;
readMissOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
endcase
end
MODIFIED: begin
case({operation, hit})
2'b00: begin //read miss PRA ESCRITA SEMPRE MODIFIED
cpuNewState <= SHARED;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b1;
dataWriteBack <= 1'b1;
end
2'b01: begin //read hit
cpuNewState <= MODIFIED;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
2'b10: begin // write miss
cpuNewState <= MODIFIED;
writeMissOnBus <= 1'b1;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b0;
dataWriteBack <= 1'b1;
end
2'b11: begin // write hit
cpuNewState <= MODIFIED;
writeMissOnBus <= 1'b0;
invalidateOnBus <= 1'b0;
readMissOnBus <= 1'b0;
dataWriteBack <= 1'b0;
end
endcase
end
endcase
end
endmodule
module directoryStateMachineBus(
input fetch, invalidate,
input[1:0] initialState,
output reg [1:0] busNewState, output reg busWriteBack, abortMemAccess);
localparam NONEMESSAGE = 2'b00, INVALID = 2'b01, SHARED = 2'b10, MODIFIED = 2'b11;
always @(*) begin
case (initialState)
SHARED: begin
if(invalidate == 1'b1)begin
busNewState <= INVALID;
busWriteBack <= 1'b0;
end
end
INVALID: begin
busNewState <= INVALID;
busWriteBack<= 1'b0;
end
MODIFIED: begin
if(fetch == 1'b1 & invalidate == 1'b1)begin
busNewState <= INVALID;
busWriteBack<= 1'b1;
end
if(fetch == 1'b1) begin
busNewState <= MODIFIED;
busWriteBack <= 1'b1;
end
end
endcase
end
endmodule
module directoryFSM(
input requester, readMiss, invalidate, writeMiss, dataWriteBack,
input[1:0] initialState, input[3:0] ownersSharers,
output reg[1:0] directoryNewState, output reg directoryFetch,
directoryInvalidate, dataValueReply, output reg [3:0] dataSharers
);
localparam NONEMESSAGE = 2'b00, UNCACHED = 2'b01, SHARED = 2'b10, MODIFIED = 2'b11;
always @(*) begin
case (initialState)
SHARED: begin
if(readMiss == 1'b1) begin //readmiss
dataValueReply <= 1'b1;
directoryFetch <= 1'b0;
directoryInvalidate <= 1'b0;
directoryNewState <= SHARED;
if (requester == 1'b1) begin
dataSharers <= {ownersSharers[3:2],1'b1,ownersSharers[0]};
end
else if(requester == 1'b0) begin
dataSharers <= {ownersSharers[3:1],1'b1};
end
end
if(writeMiss == 1'b1) begin //write miss
dataValueReply <= 1'b1;
directoryFetch <= 1'b0;
directoryInvalidate <= 1'b1;
directoryNewState <= MODIFIED;
if (requester == 1'b1) begin
dataSharers <= {ownersSharers[3:2],2'b10};
end
else if(requester == 1'b0) begin
dataSharers <= {ownersSharers[3:2],2'b01};
end
end
end
UNCACHED: begin //uncached
if(readMiss == 1'b1) begin //readmiss
dataValueReply <= 1'b1;
directoryFetch <= 1'b0;
directoryInvalidate <= 1'b0;
directoryNewState <= SHARED;
if (requester == 1'b1) begin
dataSharers <= {ownersSharers[3:2],2'b10};
end
else if(requester == 1'b0) begin
dataSharers <= {ownersSharers[3:2],2'b01};
end
end
if(writeMiss == 1'b1) begin
dataValueReply <= 1'b1;
directoryFetch <= 1'b0;
directoryInvalidate <= 1'b0;
directoryNewState <= MODIFIED;
if (requester == 1'b1) begin
dataSharers <= {ownersSharers[3:2],2'b10};
end
else if(requester == 1'b0) begin
dataSharers <= {ownersSharers[3:2],2'b01};
end
end
end
MODIFIED: begin
if(readMiss == 1'b1) begin //readmiss
dataValueReply <= 1'b1;
directoryFetch <= 1'b1;
directoryInvalidate <= 1'b0;
directoryNewState <= SHARED;
if (requester == 1'b1) begin
dataSharers <= {ownersSharers[3:2],1'b1,ownersSharers[0]};
end
else if(requester == 1'b0) begin
dataSharers <= {ownersSharers[3:1],1'b1};
end
end
else if(writeMiss == 1'b1) begin
dataValueReply <= 1'b1;
directoryFetch <= 1'b1;
directoryInvalidate <= 1'b1;
directoryNewState <= MODIFIED;
if (requester == 1'b1) begin
dataSharers <= {ownersSharers[3:2],2'b10};
end
else if(requester == 1'b0) begin
dataSharers <= {ownersSharers[3:2],2'b01};
end
end
else if (dataWriteBack == 1'b1) begin
dataValueReply <= 1'b0;
directoryFetch <= 1'b0;
directoryInvalidate <= 1'b0;
directoryNewState <= UNCACHED;
dataSharers <= {ownersSharers[3:2],2'b00};
end
end
default: begin
end
endcase
end
endmodule