-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmu.v
378 lines (315 loc) · 13.3 KB
/
mmu.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
`include "inc/state.v"
module MMU
#(
parameter SADDR=64,
parameter SPCID=12
)
(
input clk
// input [SADDR-1:0] incoming_dtlb_va,
// input [SADDR-1:0] incoming_dtlb_pa,
// input [SPCID-1:0] incoming_dtlb_pcid,
// input [SADDR-1:0] incoming_itlb_va,
// input [SADDR-1:0] incoming_itlb_pa,
// input [SPCID-1:0] incoming_itlb_pcid,
// input [SADDR-1:0] prefetching_stlb_va,
// input [SADDR-1:0] prefetching_stlb_pa,
// input [SPCID-1:0] prefetching_stlb_pcid,
// input prefetch_stlb,
// input dtlb_req,
// input itlb_req,
// output out_dtlb_hit,
// output out_dtlb_miss,
// output out_dtlb_insert,
// output out_itlb_hit,
// output out_itlb_miss,
// output out_itlb_insert,
// output out_stlb_hit,
// output out_stlb_miss,
// output out_stlb_insert,
// output out_stlb_prefetch
// input in_shutdown,
// input in_validate
);
wire dtlb_hit, itlb_hit, stlb_hit,
dtlb_miss, itlb_miss, stlb_miss,
judged_dtlb_miss;
wire [63:0] stat_hit;
wire [63:0] stat_miss;
wire [63:0] stat_prefetch;
reg tlb_insert;
reg stlb_insert;
reg [63:0] pa;
reg ctrl = 1'b0;
reg [SADDR-1:0] dtlb_req_va = {SADDR{1'b0}};
reg [SPCID-1:0] dtlb_req_pcid = {SADDR{1'b0}};
wire [SADDR-1:0] dtlb_req_ta;
reg [`STATE_RANGE] dtlb_state = {6{1'b0}};
reg [SADDR-1:0] itlb_req_va = {SADDR{1'b0}};
reg [SPCID-1:0] itlb_req_pcid = {SADDR{1'b0}};
wire [SADDR-1:0] itlb_req_ta;
reg [`STATE_RANGE] itlb_state = {6{1'b0}};
wire [SADDR-1:0] stlb_req_ta;
reg [`STATE_RANGE] stlb_state = {{5{1'b0}}, 1'b0};
TLB dtlb(// inputs
.clk(clk), .state(dtlb_state), .req_va(dtlb_req_va), .req_pcid(dtlb_req_pcid),
.insert_va(stlb_piping_va[3]), .insert_pa(stlb_piping_pa[3]), .insert_pcid(stlb_piping_pcid[3]),
// outputs
.req_ta(dtlb_req_ta), .hit(dtlb_hit), .miss(dtlb_miss));
TLB itlb(// inputs
.clk(clk), .state(itlb_state), .req_va(itlb_req_va), .req_pcid(itlb_req_pcid),
.insert_va(stlb_piping_va[3]), .insert_pa(stlb_piping_pa[3]), .insert_pcid(stlb_piping_pcid[3]),
// outputs
.req_ta(itlb_req_ta), .hit(itlb_hit), .miss(itlb_miss));
STLB stlb(// inputs
.clk(clk),
.state_bank0(stlb_state), .req_va_bank0(stlb_piping_va[1]), .req_pcid_bank0(stlb_piping_pcid[1]),
.insert_va_bank0(stlb_piping_va[3]), .insert_pa_bank0(stlb_piping_pa[3]), .insert_pcid_bank0(stlb_piping_pcid[3]),
//outputs
.req_ta_bank0(stlb_req_ta), .hit_bank0(stlb_hit), .miss_bank0(stlb_miss));
PMU pmu(.clk(clk),
.dtlb_hit(dtlb_hit), .dtlb_miss(judged_dtlb_miss), .dtlb_insert(dtlb_state`insert_bit),
.itlb_hit(itlb_hit), .itlb_miss(itlb_miss), .itlb_insert(itlb_state`insert_bit),
.stlb_hit(stlb_hit), .stlb_miss(stlb_miss), .stlb_prefetch(prefetch_stlb), .stlb_insert(stlb_state`insert_bit));
/********************************************************************
PIPELINE MACHINE
********************************************************************/
reg [SADDR-1:0] stlb_piping_va [5:0]; // virtual address
reg [SADDR-1:0] stlb_piping_pa [5:0]; // physical address
reg [SPCID-1:0] stlb_piping_pcid [5:0]; // process-context identifier
reg [3:1] piping_marker; // iTLB or dTLB info
reg [SADDR-1:0] incoming_dtlb_va;
wire [SADDR-1:0] incoming_dtlb_pa = incoming_dtlb_va;
reg [SPCID-1:0] incoming_dtlb_pcid = 0;
reg [SADDR-1:0] incoming_itlb_va;
wire [SADDR-1:0] incoming_itlb_pa = incoming_itlb_va;
wire [SPCID-1:0] incoming_itlb_pcid = 0;
reg [SADDR-1:0] prefetching_stlb_va;
wire [SADDR-1:0] prefetching_stlb_pa = prefetching_stlb_va;
reg [SPCID-1:0] prefetching_stlb_pcid = 0;
reg prefetch_stlb;
wire [SADDR-1:0] itlb_ta = (dtlb_hit)? dtlb_req_ta: {SADDR{1'bx}};
wire [SADDR-1:0] dtlb_ta = (itlb_hit)? itlb_req_ta: {SADDR{1'bx}};
reg dtlb_req;
reg itlb_req;
/* verilator lint_off STMTDLY */
initial begin
incoming_dtlb_va = 64'h0;
incoming_itlb_va = 64'h0;
dtlb_req = 1;
itlb_req = 1;
prefetch_stlb = 1'b1;
prefetching_stlb_va = 64'hd1ffffffffffffff;
#2
dtlb_req = 0;
itlb_req = 0;
prefetching_stlb_va = 64'he1ffffffffffffff;
#2
prefetch_stlb = 1'b0;
incoming_dtlb_va = 64'hd1ffffffffffffff;
incoming_itlb_va = 64'he1ffffffffffffff;
dtlb_req = 1;
itlb_req = 1;
#2
dtlb_req = 0;
itlb_req = 0;
#4 // double insert from L2 to L1
#2
incoming_dtlb_va = 64'hd1ffffffffffffff;
incoming_itlb_va = 64'he1ffffffffffffff;
dtlb_req = 1;
itlb_req = 1;
#2
dtlb_req = 0;
itlb_req = 0;
#2
incoming_dtlb_va = 64'hd1ffffffffffffff;
incoming_itlb_va = 64'he1ffffffffffffff;
dtlb_req = 1;
itlb_req = 1;
#2
incoming_dtlb_va = 64'hd2ffffffffffffff;
incoming_itlb_va = 64'he2ffffffffffffff;
#2
dtlb_req = 0;
itlb_req = 0;
#4 // double insert to both L1, L2
#2
incoming_itlb_va = 64'he2ffffffffffffff;
itlb_req = 1;
#2
incoming_dtlb_va = 64'hd2ffffffffffffff;
itlb_req = 0;
dtlb_req = 1;
#2
dtlb_req = 0;
itlb_req = 0;
end
/* verilator lint_off STMTDLY */
reg [SADDR-1:0] dtlb_pre_pipe_pa;
reg [SADDR-1:0] itlb_pre_pipe_pa;
wire dtlb_itlb_miss_conflict = (itlb_miss & dtlb_miss)? 1'b1 : 1'b0;
wire stop_itlb_miss = (itlb_miss & (prefetch_stlb | stlb_squeeze_insert))? 1'b1 : 1'b0;
wire stop_dtlb_miss = (dtlb_miss & (prefetch_stlb | stlb_squeeze_insert))? 1'b1 : 1'b0;
wire stlb_squeeze_insert = (stlb_hit & stlb_state`miss_bit)? 1'b1 : 1'b0;
assign judged_dtlb_miss = dtlb_miss & ~(itlb_miss & dtlb_miss);
always @(negedge clk) begin
// -------------- iTLB & dTLB STATE PIPELINE --------------
$display("dreq - %b, ireq - %b, conf - %b, - prefetch conflict %b", dtlb_req, itlb_req, dtlb_itlb_miss_conflict, stop_dtlb_miss);
if (dtlb_req || dtlb_itlb_miss_conflict || stop_dtlb_miss) begin
dtlb_state`req_bit <= 1'b1;
end else if (dtlb_hit || dtlb_miss) begin
dtlb_state`req_bit <= 1'b0;
end
if (itlb_req || stop_itlb_miss) begin
itlb_state`req_bit <= 1'b1;
end else if (itlb_hit || itlb_miss) begin
itlb_state`req_bit <= 1'b0;
end
if (~prefetch_stlb) begin
if(stlb_squeeze_insert) begin
stlb_state`insert_bit <= 1'b1;
stlb_state`miss_bit <= 1'b0;
if (piping_marker[2]) begin
itlb_state`insert_bit <= 1'b1;
dtlb_state`insert_bit <= 1'b0;
end else begin
dtlb_state`insert_bit <= 1'b1;
itlb_state`insert_bit <= 1'b0;
end
//------------------------------------------
stlb_piping_va[3] <= stlb_piping_va[2];
stlb_piping_pa[3] <= stlb_piping_pa[2];
stlb_piping_pcid[3] <= stlb_piping_pcid[2];
//------------------------------------------
if (~dtlb_itlb_miss_conflict) begin
if (~dtlb_miss) begin
dtlb_req_va <= incoming_dtlb_va;
dtlb_pre_pipe_pa <= incoming_dtlb_pa;
dtlb_req_pcid <= incoming_dtlb_pcid;
end
if (~itlb_miss) begin
itlb_req_va <= incoming_itlb_va;
itlb_pre_pipe_pa <= incoming_itlb_pa;
itlb_req_pcid <= incoming_itlb_pcid;
end
end
end else begin
if (~itlb_miss) begin
if (dtlb_miss) begin
dtlb_state`miss_bit <= 1'b1;
end else if (dtlb_state`miss_bit == 1'b1) begin
dtlb_state`miss_bit <= 1'b0;
end
end
if (itlb_miss) begin
itlb_state`miss_bit <= 1'b1;
end else if (itlb_state`miss_bit == 1'b1) begin
itlb_state`miss_bit <= 1'b0;
end
// -------------- INSERT RESET --------------
if (dtlb_state`insert_bit == 1'b1 && stlb_state`miss_bit == 1'b0) begin
dtlb_state`insert_bit <= 1'b0;
end
if (itlb_state`insert_bit == 1'b1 && stlb_state`miss_bit == 1'b0) begin
itlb_state`insert_bit <= 1'b0;
end
// -------------- STLB STATE PIPELINE --------------
if (dtlb_miss || itlb_miss) begin
stlb_state`req_bit <= 1'b1;
end else if (stlb_hit || stlb_miss) begin
stlb_state`req_bit <= 1'b0;
end
if (stlb_state`insert_bit == 1'b1) begin
stlb_state`insert_bit <= 1'b0;
end
if (stlb_hit) begin
// ta <= dtlb_req_ta;
if (piping_marker[1]) begin
itlb_state`insert_bit <= 1'b1;
end else begin
dtlb_state`insert_bit <= 1'b1;
end
end
if (stlb_state`miss_bit == 1'b1) begin
stlb_state`insert_bit <= 1'b1;
if (piping_marker[2]) begin
itlb_state`insert_bit <= 1'b1;
end else begin
dtlb_state`insert_bit <= 1'b1;
end
end
if (stlb_miss) begin
stlb_state`miss_bit <= 1'b1;
end else if (stlb_state`miss_bit == 1'b1) begin
stlb_state`miss_bit <= 1'b0;
end
// -------------- DATA PIPING --------------
piping_marker <= piping_marker << 1'b1;
if (itlb_miss) begin
stlb_piping_va[1] <= itlb_req_va;
stlb_piping_pa[1] <= itlb_pre_pipe_pa;
stlb_piping_pcid[1] <= itlb_req_pcid;
piping_marker[1] <= 1'b1;
end else begin
stlb_piping_va[1] <= dtlb_req_va;
stlb_piping_pa[1] <= dtlb_pre_pipe_pa;
stlb_piping_pcid[1] <= dtlb_req_pcid;
piping_marker[1] <= 1'b0;
end
itlb_req_va <= incoming_itlb_va;
itlb_pre_pipe_pa <= incoming_itlb_pa;
itlb_req_pcid <= incoming_itlb_pcid;
//------------------------------------------
if (~dtlb_itlb_miss_conflict) begin
dtlb_req_va <= incoming_dtlb_va;
dtlb_pre_pipe_pa <= incoming_dtlb_pa;
dtlb_req_pcid <= incoming_dtlb_pcid;
end
if (stlb_hit) begin
stlb_piping_va[3] <= stlb_piping_va[1];
//------------------------------------------
stlb_piping_pa[3] <= stlb_piping_pa[1];
//------------------------------------------
stlb_piping_pcid[3] <= stlb_piping_pcid[1];
end
else begin
//------------------------------------------
stlb_piping_va[3] <= stlb_piping_va[2];
stlb_piping_va[2] <= stlb_piping_va[1];
//------------------------------------------
stlb_piping_pa[3] <= stlb_piping_pa[2];
stlb_piping_pa[2] <= stlb_piping_pa[1];
//------------------------------------------
stlb_piping_pcid[3] <= stlb_piping_pcid[2];
stlb_piping_pcid[2] <= stlb_piping_pcid[1];
end
// stlb_piping_ta[3] <= stlb_piping_ta[2];
// stlb_piping_ta[2] <= stlb_piping_ta[1];
// stlb_piping_ta[1] <= stlb_piping_ta[0];
// stlb_piping_ta[0] <= {SADDR{1'bz}};
end
end else begin
dtlb_state`insert_bit <= 1'b0;
itlb_state`insert_bit <= 1'b0;
stlb_state`insert_bit <= 1'b1;
//------------------------------------------
stlb_piping_va[3] <= prefetching_stlb_va;
stlb_piping_pa[3] <= prefetching_stlb_pa;
stlb_piping_pcid[3] <= prefetching_stlb_pcid;
//------------------------------------------
if (~dtlb_itlb_miss_conflict) begin
if (~dtlb_miss) begin
dtlb_req_va <= incoming_dtlb_va;
dtlb_pre_pipe_pa <= incoming_dtlb_pa;
dtlb_req_pcid <= incoming_dtlb_pcid;
end
if (~itlb_miss) begin
itlb_req_va <= incoming_itlb_va;
itlb_pre_pipe_pa <= incoming_itlb_pa;
itlb_req_pcid <= incoming_itlb_pcid;
end
end
end
end
endmodule