-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_stage.v
192 lines (174 loc) · 7.27 KB
/
mem_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
module mem_stage(
input wire clk,
input wire reset,
input wire [31:0] mem_pc,
input wire mem_regfile_wren,
input wire [4:0] mem_regfile_wt_addr,
input wire mem_regfile_mem2reg,
input wire [31:0] mem_regfile_wt_val,
input wire mem_cp0_wren,
input wire [4:0] mem_cp0_wt_addr,
input wire [31:0] mem_cp0_wt_val,
input wire [2:0] mem_lw_sw_type,
input wire [31:0] mem_dmm_addr,
input wire [3:0] mem_dmm_byte_enable,
input wire mem_exception_if_exchappen,
input wire [31:0] mem_exception_if_epc,
input wire mem_exception_if_bd,
input wire [31:0] mem_exception_if_badvaddr,
input wire [4:0] mem_exception_if_exccode,
input wire mem_exception_dec_exchappen,
input wire [4:0] mem_exception_dec_exccode,
input wire mem_exception_exe_exchappen,
input wire [4:0] mem_exception_exe_exccode,
input wire cp0_status_exl,
input wire cp0_status_ie,
input wire cp0_status_im0,
input wire cp0_status_im1,
input wire cp0_cause_ip0,
input wire cp0_cause_ip1,
input wire ready,
input wire complete,
input wire [31:0] dmm_load_val,
output wire [31:0] mem_regfile_wt_val_mux,
output wire exception_inst_exchappen,
output wire exception_flush,
output wire exception_inst_interrupt,
output reg wb_exception_inst_exchappen,
output reg [31:0] wb_exception_inst_epc,
output reg wb_exception_inst_bd,
output reg [31:0] wb_exception_inst_badvaddr,
output reg wb_exception_inst_badvaddr_wren,
output reg [4:0] wb_exception_inst_exccode,
output reg [31:0] wb_pc,
output reg wb_regfile_wren,
output reg [4:0] wb_regfile_wt_addr,
output reg wb_regfile_mem2reg,
output reg [31:0] wb_regfile_wt_val,
output reg [31:0] wb_dmm_load_val,
output reg [3:0] wb_dmm_byte_enable,
output reg [2:0] wb_lw_sw_type,
output reg wb_cp0_wren,
output reg [4:0] wb_cp0_wt_addr,
output reg [31:0] wb_cp0_wt_val
);
reg exception_mem_exchappen;
wire [31:0] exception_mem_badvaddr;
reg [4:0] exception_mem_exccode;
wire[31:0] exception_inst_epc;
wire exception_inst_bd;
reg [31:0] exception_inst_badvaddr;
reg exception_inst_badvaddr_wren;
reg [4:0] exception_inst_exccode;
wire [4:0] exceptions;
wire flush;
assign mem_regfile_wt_val_mux = mem_regfile_mem2reg ? dmm_load_val : mem_regfile_wt_val;
//exception handle
assign exception_mem_badvaddr = mem_dmm_addr;
always @(*)
begin
if (mem_lw_sw_type == 3'd6 && mem_dmm_addr[0] != 1'b0) //sh
begin
exception_mem_exchappen = 1;
exception_mem_exccode = 5'd5;
end
else if (mem_lw_sw_type == 3'd7 && mem_dmm_addr[1:0] != 2'b00) //sw
begin
exception_mem_exchappen = 1;
exception_mem_exccode = 5'd5;
end
else if ( (mem_lw_sw_type == 3'd2 || mem_lw_sw_type == 3'd3) && mem_dmm_addr[0] != 1'b0) // lh, lhu
begin
exception_mem_exchappen = 1;
exception_mem_exccode = 5'd4;
end
else if (mem_lw_sw_type == 3'd4 && mem_dmm_addr[1:0] != 2'b00) //lw
begin
exception_mem_exchappen = 1;
exception_mem_exccode = 5'd4;
end
else
begin
exception_mem_exchappen = 0;
exception_mem_exccode = 0;
end
end
// exception handle
assign exception_flush = exception_inst_exchappen || exception_inst_interrupt;
assign exception_inst_interrupt = cp0_status_ie && ~cp0_status_exl ?
((cp0_status_im0 & cp0_cause_ip0) || (cp0_status_im1 & cp0_cause_ip1)) : 0;
assign exceptions = {mem_exception_if_exchappen,
mem_exception_dec_exchappen, mem_exception_exe_exchappen, exception_mem_exchappen};
assign exception_inst_exchappen = | exceptions;
assign exception_inst_epc = exception_inst_interrupt ? wb_pc : mem_exception_if_epc;
assign exception_inst_bd = mem_exception_if_bd;
always @(*)
casex (exceptions) // synopsys full_case parallel_case
4'b1xxx:
begin
exception_inst_badvaddr = mem_exception_if_badvaddr;
exception_inst_badvaddr_wren = 1;
exception_inst_exccode = mem_exception_if_exccode;
end
4'b01xx:
begin
exception_inst_badvaddr = 0;
exception_inst_badvaddr_wren = 0;
exception_inst_exccode = mem_exception_dec_exccode;
end
4'b001x:
begin
exception_inst_badvaddr = 0;
exception_inst_badvaddr_wren = 0;
exception_inst_exccode = mem_exception_exe_exccode;
end
4'b0001:
begin
exception_inst_badvaddr = exception_mem_badvaddr;
exception_inst_badvaddr_wren = 1;
exception_inst_exccode = exception_mem_exccode;
end
default:
begin
exception_inst_badvaddr = 0;
exception_inst_badvaddr_wren = 0;
exception_inst_exccode = 0;
end
endcase
assign flush = exception_inst_exchappen || exception_inst_interrupt;
always @(posedge clk)
if (reset)
begin
wb_exception_inst_exchappen <= 0;
wb_regfile_wren <= 0;
wb_regfile_wt_addr <= 0;
wb_regfile_mem2reg <= 0;
wb_cp0_wren <= 0;
wb_cp0_wt_addr <= 0;
wb_lw_sw_type <= 0;
end
else if (ready && complete)
begin
wb_exception_inst_exchappen <= exception_inst_exchappen;
wb_regfile_wren <= flush ? 0 : mem_regfile_wren;
wb_regfile_wt_addr <= flush ? 0 : mem_regfile_wt_addr;
wb_regfile_mem2reg <= flush ? 0 : mem_regfile_mem2reg;
wb_cp0_wren <= flush ? 0 : mem_cp0_wren;
wb_cp0_wt_addr <= flush ? 0 : mem_cp0_wt_addr;
wb_lw_sw_type <= flush ? 0 : mem_lw_sw_type;
end
always @(posedge clk)
if (ready && complete)
begin
wb_exception_inst_epc <= exception_inst_epc;
wb_exception_inst_bd <= exception_inst_bd;
wb_exception_inst_badvaddr <= exception_inst_badvaddr;
wb_exception_inst_badvaddr_wren <= exception_inst_badvaddr_wren;
wb_exception_inst_exccode <= exception_inst_exccode;
wb_pc <= flush ? 0 : mem_pc;
wb_regfile_wt_val <= flush ? 0 : mem_regfile_wt_val;
wb_dmm_load_val <= flush ? 0 : dmm_load_val;
wb_dmm_byte_enable <= flush ? 0 : mem_dmm_byte_enable;
wb_cp0_wt_val <= flush ? 0 : mem_cp0_wt_val;
end
endmodule