-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdcache_control.sv
executable file
·269 lines (218 loc) · 5.96 KB
/
dcache_control.sv
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
import lc3b_types::*; /* Import types defined in lc3b_types.sv */
module dcache_control
(
input clk,
/* Datapath controls */
input lru,
input hit_A, hit_B,
input valid_A_dataout, valid_B_dataout,
output logic valid_A_write, valid_B_write,
output logic valid_A_datain, valid_B_datain,
input dirty_A_dataout, dirty_B_dataout,
output logic dirty_A_write, dirty_B_write,
output logic dirty_A_datain, dirty_B_datain,
output logic tag_A_write, tag_B_write,
output logic data_A_write, data_B_write,
output logic lru_datain,
output logic lru_write,
output logic replacemux_sel,
/* Memory signals */
input dcache_write,
input dcache_read,
output logic dcache_resp,
input L2_resp,
output logic L2_write,
output logic L2_read,
output logic pmemaddressmux_sel
);
enum int unsigned {
HIT,
MISS,
WRITE_BACK,
SET_STATUS_MISS,
SET_STATUS_WRITE_BACK
} state, next_state;
always_comb
begin : state_actions
/* Default assignments */
lru_datain = 1'b0;
lru_write = 1'b0;
valid_A_write = 1'b0;
valid_B_write = 1'b0;
valid_A_datain = 1'b0;
valid_B_datain = 1'b0;
dirty_A_write = 1'b0;
dirty_B_write = 1'b0;
dirty_A_datain = 1'b0;
dirty_B_datain = 1'b0;
tag_A_write = 1'b0;
tag_B_write = 1'b0;
data_A_write = 1'b0;
data_B_write = 1'b0;
L2_write = 1'b0;
L2_read = 1'b0;
dcache_resp = 1'b0;
pmemaddressmux_sel = 1'b0;
replacemux_sel = 1'b0;
case(state)
HIT: begin
if ((hit_A || hit_B) && (dcache_read || dcache_write))
begin
dcache_resp = 1'b1;
if (hit_A && valid_A_dataout)
begin
pmemaddressmux_sel = 1'b0;
/* should be taken care of by hardware wire-y stuff */
// data_write = 1'b1; // write dat data
lru_write = 1'b1;
lru_datain = 1'b0; // wrote to A, least recent is now B
if (dcache_write)
begin
replacemux_sel = 1'b1;
dirty_A_write = 1'b1;
dirty_A_datain = 1'b1; // A is dirty (gross!)
end
end
else if (hit_B && valid_B_dataout)
begin
pmemaddressmux_sel = 1'b0;
/* should be taken care of by hardware wire-y stuff */
// data_write = 1'b1; // write dat data
lru_write = 1'b1;
lru_datain = 1'b1; // wrote to B, least recent is now A
if (dcache_write)
begin
replacemux_sel = 1'b1;
dirty_B_write = 1'b1;
dirty_B_datain = 1'b1; // B is dirty (gross!)
end
end
end
end
MISS: begin
pmemaddressmux_sel = 1'b0;
L2_read = 1'b1; // we want to read the new and exciting stuff from physical memory
if (lru == 1'b1)
begin
data_A_write = 1'b1; // write the data...
tag_A_write = 1'b1; // and the tag
end
if (lru == 1'b0)
begin
data_B_write = 1'b1; // write the data...
tag_B_write = 1'b1; // and the tag
end
end
WRITE_BACK: begin
pmemaddressmux_sel = 1'b1; // set mux to write to address specified by tag arras and part of original memory address
L2_write = 1'b1; // we want to write the dirty shit to physical memory
end
SET_STATUS_WRITE_BACK: begin
pmemaddressmux_sel = 1'b1;
if (lru == 1'b1) // if least recently used is A...
begin
dirty_A_write= 1'b1;
dirty_A_datain = 1'b0; // no longer dirty, we wrote dirty stuff to physical memory
end
else if (lru == 1'b0) // if least recently used is B...
begin
dirty_B_write = 1'b1;
dirty_B_datain = 1'b0; // no longer dirty, we wrote dirty stuff to physical memory
end
end
SET_STATUS_MISS: begin
if (lru == 1'b1)
begin
valid_A_write = 1'b1; // set valid bit of least recently used
valid_A_datain = 1'b1;
if (dcache_write)
begin
dirty_A_write = 1'b1; // set dirty bit if it's a write
dirty_A_datain = 1'b1;
end
//data_A_write = 1'b1; // write the data...
//tag_A_write = 1'b1; // and the tag
end
if (lru == 1'b0)
begin
valid_B_write = 1'b1; // set valid bit of least recently used
valid_B_datain = 1'b1;
if (dcache_write)
begin
dirty_B_write = 1'b1; // set dirty bit if it's a write
dirty_B_datain = 1'b1;
end
//data_B_write = 1'b1; // write the data...
//tag_B_write = 1'b1; // and the tag
end
// lru_write = 1'b1;
// lru_datain = !lru; // set lru to opposite
end
default: /* Do nothing */;
endcase
end
always_comb
begin : next_state_logic
next_state = state;
case(state)
HIT: begin
if (lru == 1'b1 && !hit_B && (dcache_read|dcache_write)) // if we messed with B last...
begin
if (!hit_A && dirty_A_dataout == 1'b0) // if there's no hit on A and if the block isn't dirty...
begin
next_state = MISS; // It's a MISS
end
// otherwise if we have a dirty block
else if (!hit_A && dirty_A_dataout == 1'b1 && valid_A_dataout == 1'b1)
begin
next_state = WRITE_BACK; // we'll have to write it to memory
end
end
else if (lru == 1'b0 && !hit_A && (dcache_read|dcache_write)) // if we messed with A last...
begin
if (!hit_B && dirty_B_dataout == 1'b0)
begin // if there's no hit on B and if the block isn't dirty...
next_state = MISS; // It's a MISS
end
// otherwise if we have a dirty block
else if (!hit_B && dirty_B_dataout == 1'b1 && valid_B_dataout == 1'b1)
begin
next_state = WRITE_BACK; // we'll have to write it to memory
end
end
else next_state = HIT;
end
MISS: begin
if (L2_resp)
begin
next_state = SET_STATUS_MISS;
end
else
begin
next_state = MISS;
end
end
WRITE_BACK: begin
if (L2_resp) // after the data is written to physical memory
begin
next_state = SET_STATUS_WRITE_BACK;
end
else
begin
next_state = WRITE_BACK;
end
end
SET_STATUS_MISS: begin
next_state = HIT;
end
SET_STATUS_WRITE_BACK: begin
next_state = MISS;
end
default: begin next_state = HIT; end
endcase
end
always_ff @(posedge clk)
begin : next_state_assignment
state <= next_state;
end
endmodule : dcache_control