-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidregs.v
225 lines (204 loc) · 6.74 KB
/
midregs.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
module IF_ID(
input wire clk,
input wire rst,
input wire rdy,
input wire stall_enable,
input wire[`AddrLen - 1: 0] if_pc,
input wire[`InstLen - 1: 0] if_inst,
output reg[`AddrLen - 1: 0] id_pc,
output reg[`InstLen - 1: 0] id_inst
);
always @(posedge clk) begin
if(!rst) begin
if(rdy && !stall_enable) begin
id_pc <= if_pc;
id_inst <= if_inst;
end
end
else begin
id_pc <= `ZERO_WORD;
id_inst <= `ZERO_WORD; // Turn into NOP later in EX case clause
end
end
endmodule
module ID_EX(
input wire clk,
input wire rst,
input wire rdy,
input wire stall_enable,
input wire jump_rst,
// from ID
input wire[`RegLen - 1: 0] id_rs1_data,
input wire[`RegLen - 1: 0] id_rs2_data,
input wire[`AluOpLen - 1: 0] id_aluop,
input wire[`AluSelLen - 1: 0] id_alusel,
input wire[`Funct3Len - 1: 0] id_funct3,
input wire[`RegLen - 1: 0] id_imm,
input wire[`RegAddrLen - 1: 0] id_rd_addr,
input wire id_rd_write_enable,
input wire[`AddrLen - 1: 0] id_next_pc,
input wire[`AddrLen - 1: 0] id_jump_pc,
// to EX
output reg[`RegLen - 1: 0] ex_rs1_data,
output reg[`RegLen - 1: 0] ex_rs2_data,
output reg[`AluOpLen - 1: 0] ex_aluop,
output reg[`AluSelLen - 1: 0] ex_alusel,
output reg[`Funct3Len - 1: 0] ex_funct3,
output reg[`RegLen - 1: 0] ex_imm,
output reg[`RegAddrLen - 1: 0] ex_rd_addr,
output reg ex_rd_write_enable,
output reg[`AddrLen - 1: 0] ex_next_pc,
output reg[`AddrLen - 1: 0] ex_jump_pc,
// to ID (Read after load: must stall)
output reg last_is_load,
output reg[`RegAddrLen - 1: 0] last_load_rd_addr
);
always @(posedge clk) begin
if(!rst) begin
if(rdy && !stall_enable) begin
if(jump_rst) begin // stall unlock & jump rst
// NOP
ex_rs1_data <= `ZERO_WORD;
ex_rs2_data <= `ZERO_WORD;
ex_aluop <= `NOP_ALUOP;
ex_alusel <= `NOP_ALUSEL;
ex_funct3 <= `NFunct3;
ex_imm <= `ZERO_WORD;
ex_rd_addr <= `ZERO_WORD;
ex_rd_write_enable <= `Disable;
ex_next_pc <= `ZERO_WORD;
ex_jump_pc <= `ZERO_WORD;
end
else begin
ex_rs1_data <= id_rs1_data;
ex_rs2_data <= id_rs2_data;
ex_aluop <= id_aluop;
ex_alusel <= id_alusel;
ex_funct3 <= id_funct3;
ex_imm <= id_imm;
ex_rd_addr <= id_rd_addr;
ex_rd_write_enable <= id_rd_write_enable;
ex_next_pc <= id_next_pc;
ex_jump_pc <= id_jump_pc;
end
end
end
else begin
// NOP
ex_rs1_data <= `ZERO_WORD;
ex_rs2_data <= `ZERO_WORD;
ex_aluop <= `NOP_ALUOP;
ex_alusel <= `NOP_ALUSEL;
ex_funct3 <= `NFunct3;
ex_imm <= `ZERO_WORD;
ex_rd_addr <= `ZERO_WORD;
ex_rd_write_enable <= `Disable;
ex_next_pc <= `ZERO_WORD;
ex_jump_pc <= `ZERO_WORD;
end
end
// Read after LOAD assertion.
always @(posedge clk) begin
if(!rst) begin
if(rdy && !stall_enable) begin
if(id_alusel == `LOAD__RES) begin
last_is_load <= `True;
last_load_rd_addr <= id_rd_addr;
end
else begin
last_is_load <= `False;
last_load_rd_addr <= `X0;
end
end
end
else begin
last_is_load <= `False;
last_load_rd_addr <= `X0;
end
end
endmodule
module EX_MEM(
input wire clk,
input wire rst,
input wire rdy,
input wire stall_enable,
// from EX
input wire[`AddrLen - 1: 0] ex_next_pc,
input wire[`RegLen - 1: 0] ex_store_data, // to RAM
input wire ex_load_enable,
input wire ex_store_enable,
input wire[`AddrLen - 1: 0] ex_load_store_addr,
input wire[`Funct3Len - 1: 0] ex_funct3, // for LOAD/STORE in MEM
input wire[`RegLen - 1: 0] ex_rd_data, // to rd
input wire[`RegAddrLen - 1: 0] ex_rd_addr,
input wire ex_rd_write_enable,
// to MEM, then MEM to MEMCTRL
output reg mem_wr_enable,
output reg mem_wr,
output reg[`AddrLen - 1: 0] mem_next_pc,
// to MEM
output reg[`Funct3Len - 1: 0] mem_funct3,
output reg[`RegAddrLen - 1: 0] mem_rd_addr,
output reg mem_rd_write_enable,
output reg[`RegLen - 1: 0] mem_rd_data,
// to MEMCTRL
output reg[`AddrLen - 1: 0] mem_load_store_addr,
output reg[1: 0] mem_load_store_type,
output reg[`RegLen - 1: 0] mem_store_data
);
always @(posedge clk) begin
if(!rst) begin
if(rdy && !stall_enable) begin
mem_next_pc <= ex_next_pc;
mem_funct3 <= ex_funct3;
mem_rd_addr <= ex_rd_addr;
mem_rd_write_enable <= ex_rd_write_enable;
mem_rd_data <= ex_rd_data;
mem_wr_enable <= ex_load_enable | ex_store_enable;
mem_wr <= ex_load_enable? `Read: `Write;
mem_load_store_addr <= ex_load_store_addr;
mem_load_store_type <= ex_funct3[1: 0];
mem_store_data <= ex_store_data;
end
end
else begin
mem_next_pc <= `ZERO_WORD;
mem_funct3 <= `NFunct3;
mem_rd_addr <= `X0;
mem_rd_write_enable <= `Disable;
mem_rd_data <= `ZERO_WORD;
mem_wr_enable <= `Disable;
mem_wr <= `Read;
mem_load_store_addr <= `ZERO_WORD;
mem_load_store_type <= 2'b00;
mem_store_data <= `ZERO_WORD;
end
end
endmodule
module MEM_WB(
input wire clk,
input wire rst,
input wire rdy,
input wire stall_enable,
input wire[`RegLen - 1: 0] mem_rd_data,
input wire[`RegAddrLen - 1: 0] mem_rd_addr,
input wire mem_rd_write_enable,
output reg[`RegLen - 1: 0] wb_rd_data,
output reg[`RegAddrLen - 1: 0] wb_rd_addr,
output reg wb_rd_write_enable
);
always @(posedge clk) begin
if(!rst) begin
if(rdy && !stall_enable) begin
wb_rd_data <= mem_rd_data;
wb_rd_addr <= mem_rd_addr;
wb_rd_write_enable <= mem_rd_write_enable;
end
end
else begin
wb_rd_data <= `ZERO_WORD;
wb_rd_addr <= `X0;
wb_rd_write_enable <= `Disable;
end
end
endmodule