-
Notifications
You must be signed in to change notification settings - Fork 0
/
IF_stage.v
407 lines (378 loc) · 10.4 KB
/
IF_stage.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
`include "mycpu.h"
module if_stage(
input clk ,
input reset ,
// allwoin
input ds_allowin ,
// br_bus
input [`BR_BUS_WD -1:0] br_bus ,
// to ds
output fs_to_ds_valid ,
output [`FS_TO_DS_BUS_WD -1:0] fs_to_ds_bus ,
// inst sram interface
// output inst_sram_en ,
// output [ 3:0] inst_sram_wen ,
// output [31:0] inst_sram_addr ,
// output [31:0] inst_sram_wdata,
// input [31:0] inst_sram_rdata,
// sram like interface
output inst_sram_req,
output inst_sram_wr,
output [ 1:0] inst_sram_size,
output [ 3:0] inst_sram_wstrb,
output [31:0] inst_sram_addr,
output [31:0] inst_sram_wdata,
input inst_sram_addr_ok,
input inst_sram_data_ok,
input [31:0] inst_sram_rdata,
input [31:0] cp0_epc,
input ws_eret,
input ws_ex ,
input ws_tlb_refill,
// refetch
input refetch,
input start_refetch,
// search port 1
output [18:0] s0_vpn2 ,
output s0_odd_page,
// output [ 7:0] s0_asid,
input s0_found ,
input [ 3:0] s0_index ,
input [19:0] s0_pfn ,
input [ 2:0] s0_c ,
input s0_d ,
input s0_v
);
reg fs_valid;
wire fs_ready_go;
reg fs_ready_go_r;
wire fs_allowin;
wire pf_ready_go;
reg pf_ready_go_r;
wire to_fs_valid;
wire [31:0] seq_pc;
wire [31:0] nextpc;
reg [31:0] nextpc_r;
reg inst_sram_req_r;
reg inst_sram_rdata_r_valid;
reg [31:0] inst_sram_rdata_r;
reg bd_done;
wire ds_br_or_jump_op;
reg ds_br_or_jump_op_r;
wire br_stall;
wire br_taken;
reg br_taken_r;
wire [31:0] br_target;
reg [31:0] br_target_r;
assign {ds_br_or_jump_op, br_stall, br_taken, br_target} = br_bus;
reg fs_tlb_refill;
wire fs_ex;
wire fs_bd;
wire [ 4:0] fs_excode;
wire [31:0] fs_inst;
reg [31:0] fs_pc;
assign fs_to_ds_bus = {
fs_tlb_refill, // 71:71
fs_bd, // 70:70
fs_ex, // 69:69
fs_excode, // 68:64
fs_inst, // 63:32
fs_pc // 31:0
};
reg ws_ex_r;
reg ws_eret_r;
reg ws_tlb_refill_r;
reg cancel;
// tlbwi & tlbr refetch
reg refetch_r;
reg [31:0] refetch_pc;
reg start_refetch_r;
wire pc_mapped;
wire [31:0] physical_pc;
wire tlb_refill;
wire tlb_invalid;
reg fs_tlb_ex;
always @(posedge clk) begin
if (reset) begin
refetch_r <= 1'b0;
refetch_pc <= 32'b0;
end
else if (refetch) begin
refetch_r <= 1'b1;
refetch_pc <= fs_pc;
end
else if (start_refetch) begin
refetch_r <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
start_refetch_r <= 1'b0;
end
else if (start_refetch) begin
start_refetch_r <= 1'b1;
end
else if (to_fs_valid && fs_allowin) begin
start_refetch_r <= 1'b0;
end
end
// pre-IF stage
always @(posedge clk) begin
if (reset) begin
bd_done <= 1'b1;
end
else if (ws_ex || ws_eret || start_refetch) begin
bd_done <= 1'b1;
end
else if (ds_br_or_jump_op_r && fs_valid) begin
bd_done <= 1'b1;
end
else if (ds_br_or_jump_op) begin
bd_done <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
ds_br_or_jump_op_r = 1'b0;
end
else if (ws_ex || ws_eret || start_refetch) begin
ds_br_or_jump_op_r = 1'b0;
end
else if (fs_to_ds_valid && ds_allowin) begin
ds_br_or_jump_op_r = 1'b0;
end
else if (ds_br_or_jump_op) begin
ds_br_or_jump_op_r = 1'b1;
end
end
always @(posedge clk) begin
if (reset) begin
br_taken_r <= 1'b0;
end
else if (ws_ex || ws_eret || start_refetch) begin
br_taken_r <= 1'b0;
end
else if (br_taken && !br_stall) begin
br_taken_r <= 1'b1;
end
else if (to_fs_valid && fs_allowin) begin
br_taken_r <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
br_target_r <= 32'b0;
end
else if (br_taken && !br_stall) begin
br_target_r <= br_target;
end
end
always @(posedge clk) begin
if (reset) begin
ws_ex_r <= 1'b0;
end
else if (ws_ex) begin
ws_ex_r <= 1'b1;
end
else if (to_fs_valid && fs_allowin) begin
ws_ex_r <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
ws_eret_r <= 1'b0;
end
else if (ws_eret) begin
ws_eret_r <= 1'b1;
end
else if (to_fs_valid && fs_allowin) begin
ws_eret_r <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
ws_tlb_refill_r <= 1'b0;
end
else if (ws_tlb_refill) begin
ws_tlb_refill_r <= 1'b1;
end
else if (to_fs_valid && fs_allowin) begin
ws_tlb_refill_r <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
nextpc_r <= seq_pc;
end
else if (start_refetch) begin
nextpc_r <= refetch_pc;
end
else if (ws_tlb_refill) begin
nextpc_r <= 32'hbfc00200;
end
else if (ws_ex && !ws_tlb_refill) begin
nextpc_r <= 32'hbfc00380;
end
else if (ws_eret) begin
nextpc_r <= cp0_epc;
end
else if (br_taken && !br_stall && bd_done && !ds_br_or_jump_op) begin
nextpc_r <= br_target;
end
else if (to_fs_valid && fs_allowin) begin
nextpc_r <= nextpc;
end
end
always @(posedge clk) begin
if (reset) begin
pf_ready_go_r <= 1'b1;
end
else if (inst_sram_req && inst_sram_addr_ok)begin
pf_ready_go_r <= 1'b1;
end
else if (to_fs_valid && fs_allowin)begin
pf_ready_go_r <= 1'b0;
end
end
assign pf_ready_go = pf_ready_go_r;
assign to_fs_valid = ~reset && pf_ready_go;
assign seq_pc = fs_pc + 3'h4;
assign nextpc = start_refetch_r ? refetch_pc :
ws_tlb_refill_r ? 32'hbfc00200 :
(ws_ex_r && !ws_tlb_refill_r) ? 32'hbfc00380 :
ws_eret_r ? cp0_epc :
br_taken_r ? br_target_r :
seq_pc;
// assign inst_sram_en = to_fs_valid && fs_allowin;
// assign inst_sram_wen = 4'h0;
// assign inst_sram_addr = nextpc;
// assign inst_sram_wdata = 32'b0;
always @(posedge clk) begin
if (reset) begin
inst_sram_req_r <= 1'b0;
end
else if (to_fs_valid && fs_allowin) begin
inst_sram_req_r <= 1'b1;
end
else if (inst_sram_req && inst_sram_addr_ok)begin
inst_sram_req_r <= 1'b0;
end
end
assign pc_mapped = !(nextpc_r[31:30] == 2'b10);
assign s0_vpn2 = nextpc_r[31:13];
assign s0_odd_page = nextpc_r[12];
assign physical_pc = (pc_mapped && s0_found) ? {s0_pfn, nextpc_r[11:0]}
: {3'b0, nextpc_r[28:0]};
assign tlb_refill = pc_mapped && !s0_found;
assign tlb_invalid = pc_mapped && s0_found && !s0_v;
assign inst_sram_req = inst_sram_req_r && ~br_stall;
assign inst_sram_wr = 1'b0;
assign inst_sram_size = 2'h2;
assign inst_sram_wstrb = 4'b0;
assign inst_sram_addr = physical_pc;
assign inst_sram_wdata = 32'b0;
// IF stage
always @(posedge clk) begin
if (reset) begin
fs_ready_go_r <= 1'b0;
end
else if (fs_to_ds_valid && ds_allowin) begin
fs_ready_go_r <= 1'b0;
end
else if (inst_sram_data_ok && !cancel && !ws_ex && !ws_eret && !start_refetch) begin
fs_ready_go_r <= 1'b1;
end
end
assign fs_ready_go = fs_ready_go_r && !refetch_r;
assign fs_allowin = !fs_valid || fs_ready_go && ds_allowin;
assign fs_to_ds_valid = fs_valid && fs_ready_go;
always@(posedge clk) begin
if (reset) begin
cancel <= 1'b0;
end
else if (inst_sram_data_ok) begin
cancel <= 1'b0;
end
else if (ws_ex || ws_eret || start_refetch) begin
if (to_fs_valid || fs_allowin == 1'b0 && fs_ready_go == 1'b0) begin
cancel <= 1'b1;
end
end
end
always @(posedge clk) begin
if (reset) begin
fs_valid <= 1'b0;
end
else if (ws_ex || ws_eret || start_refetch) begin
fs_valid <= 1'b0;
end
else if (fs_allowin) begin
fs_valid <= to_fs_valid;
end
if (reset) begin
fs_pc <= 32'hbfbffffc; // trick: to make nextpc be 0xbfc00000 during reset
end
else if (to_fs_valid && fs_allowin) begin
fs_pc <= nextpc;
end
end
always @(posedge clk) begin
if (reset) begin
inst_sram_rdata_r <= 32'b0;
end
else if (inst_sram_data_ok) begin
inst_sram_rdata_r <= inst_sram_rdata;
end
end
always@(posedge clk)begin
if(reset)begin
inst_sram_rdata_r_valid <= 1'b0;
end
else if(ws_eret || ws_ex || start_refetch) begin
inst_sram_rdata_r_valid <= 1'b0;
end
else if(fs_valid && inst_sram_data_ok)begin
inst_sram_rdata_r_valid <= 1'b1;
end
else if(fs_to_ds_valid && ds_allowin)begin
inst_sram_rdata_r_valid <= 1'b0;
end
end
assign fs_inst = (inst_sram_rdata_r_valid ? inst_sram_rdata_r : inst_sram_rdata) & {32{!fs_tlb_ex}};
// exception judge
always @(posedge clk) begin
if (reset) begin
fs_tlb_ex <= 1'b0;
end
else if (ws_ex || ws_eret || start_refetch) begin
fs_tlb_ex <= 1'b0;
end
else if (tlb_refill || tlb_invalid) begin
fs_tlb_ex <= 1'b1;
end
else if (fs_to_ds_valid && ds_allowin) begin
fs_tlb_ex <= 1'b0;
end
end
always @(posedge clk) begin
if (reset) begin
fs_tlb_refill <= 1'b0;
end
else if (ws_ex || ws_eret || start_refetch) begin
fs_tlb_refill <= 1'b0;
end
else if (tlb_refill) begin
fs_tlb_refill <= 1'b1;
end
else if (fs_to_ds_valid && ds_allowin) begin
fs_tlb_refill <= 1'b0;
end
end
wire addr_error;
assign addr_error = (fs_pc[1:0] != 2'b0);
assign fs_ex = fs_valid && (addr_error || fs_tlb_ex);
assign fs_bd = ds_br_or_jump_op_r;
assign fs_excode = {5{fs_ex}} & (addr_error ? `EX_ADEL :
fs_tlb_ex ? `EX_TLBL :
5'b0);
endmodule