-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemctrl.v
302 lines (279 loc) · 10.6 KB
/
memctrl.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
module MemCtrl(
input wire clk,
input wire rst,
input wire rdy,
// connect to RAM
input wire[`ByteLen - 1: 0] ram_data_i,
output reg[`ByteLen - 1: 0] ram_data_o,
output reg[`AddrLen - 1: 0] ram_addr_o,
output reg ram_wr_o,
// from IF
input wire if_from_ram_enable_i,
input wire[`AddrLen - 1: 0] if_pc_i,
input wire if_pc_jump_enable_i,
// to IF
output reg is_if_output_o,
output reg if_inst_ready_o,
output reg[`RegLen - 1: 0] if_inst_o,
input wire id_stall_req_i, // for Read after LOAD stall
// from ID
input wire[`AddrLen - 1: 0] id_stall_pc_i, // used for id_stall_resume check
// to ID
output reg id_stall_req_resume_o, // Enabled after LOAD is ready
// from MEM
input wire mem_wr_enable_i,
input wire mem_wr_i, // write/read signal (1 for write)
input wire[`AddrLen - 1: 0] mem_next_pc_i, // used for id_stall_resume check
// from EX_MEM
input wire[`AddrLen - 1: 0] load_store_addr_i,
input wire[1: 0] load_store_type_i, // how many bytes are required
input wire[`RegLen - 1: 0] store_data_i,
// to MEM
output reg is_mem_output_o,
output reg load_store_ready_o,
output reg[`RegLen - 1: 0] load_data_o
);
// finite state machine
parameter RW0 = 1, RW1 = 2, RW2 = 3, RW3 = 4, RW4 = 5, OFF = 0;
reg[2: 0] state, next_state;
parameter NOout = 0, LOADout = 1, STOREout = 2, IFout = 3;
reg[1: 0] output_state, next_output_state; // which output
reg[`AddrLen - 1: 0] current_addr, next_addr;
reg already_jumped, next_already_jumped;
// ----------- state transition logic -------------
// state
always @(*) begin
if(!rst) begin
if(output_state == IFout && if_pc_jump_enable_i && !already_jumped) begin // if pc jump, reset RW
if(!if_from_ram_enable_i) next_state = OFF;
else next_state = RW0;
end
else begin
case(state)
OFF: begin
if(output_state != NOout) next_state = OFF;
else if(mem_wr_enable_i) next_state = RW0;
else if(if_from_ram_enable_i) next_state = RW0;
else next_state = OFF;
end
RW0: begin
if(output_state == STOREout && load_store_type_i == `LSB) next_state = OFF;
else next_state = RW1;
end
RW1: begin
if((output_state == LOADout && load_store_type_i == `LSB)
|| (output_state == STOREout && load_store_type_i == `LSH)) next_state = OFF;
else next_state = RW2;
end
RW2: begin
if(output_state == LOADout && load_store_type_i == `LSH) next_state = OFF;
else next_state = RW3;
end
RW3: begin
if(output_state == STOREout) next_state = OFF;
else next_state = RW4;
end
RW4: next_state = OFF;
default: next_state = OFF;
endcase
end
end
else next_state = OFF;
end
// already jumped state
always @(*) begin
if(!rst) begin
if(!already_jumped) begin
if(output_state == IFout && if_pc_jump_enable_i) next_already_jumped = `True;
else next_already_jumped = `False;
end
else begin
if(state == OFF) next_already_jumped = `False;
else next_already_jumped = `True;
end
end
else next_already_jumped = `False;
end
// output state
always @(*) begin
if(!rst) begin
if(output_state == IFout && if_pc_jump_enable_i && !already_jumped && !if_from_ram_enable_i)
next_output_state = NOout;
else begin
case(state)
OFF: begin
if(output_state != NOout) next_output_state = NOout;
else if(mem_wr_enable_i) next_output_state = mem_wr_i? STOREout: LOADout;
else if(if_from_ram_enable_i) next_output_state = IFout;
else next_output_state = NOout;
end
default: next_output_state = output_state;
endcase
end
end
else next_output_state = NOout;
end
// addr
always @(*) begin
if(!rst) begin
if(output_state == IFout && if_pc_jump_enable_i && !already_jumped) begin // if pc jump, update addr
if(!if_from_ram_enable_i) next_addr = `ZERO_WORD;
else next_addr = if_pc_i;
end
else begin
case(state)
OFF: begin
if(mem_wr_enable_i) next_addr = load_store_addr_i;
else if(if_from_ram_enable_i) next_addr = if_pc_i;
else next_addr = `ZERO_WORD;
end
default: next_addr = current_addr + 1;
endcase
end
end
else next_addr = `ZERO_WORD;
end
// output state to IF/MEM
always @(*) begin
is_if_output_o = `False;
is_mem_output_o = `False;
if(output_state == IFout || output_state == NOout) is_if_output_o = `True;
if(output_state != IFout) is_mem_output_o = `True;
end
// ---------------- state flip-flops --------------------
// LOAD/IF output data
always @(posedge clk) begin
if(!rst) begin
if(rdy) begin
case(state) // old value
RW1: begin
case(output_state) // old value(same)
LOADout: load_data_o[7: 0] <= ram_data_i;
IFout: if_inst_o[7: 0] <= ram_data_i;
endcase
end
RW2: begin
case(output_state)
LOADout: load_data_o[15: 8] <= ram_data_i;
IFout: if_inst_o[15: 8] <= ram_data_i;
endcase
end
RW3: begin
case(output_state)
LOADout: load_data_o[23: 16] <= ram_data_i;
IFout: if_inst_o[23: 16] <= ram_data_i;
endcase
end
RW4: begin
case(output_state)
LOADout: load_data_o[31: 24] <= ram_data_i;
IFout: if_inst_o[31: 24] <= ram_data_i;
endcase
end
endcase
end
end
else begin
load_data_o <= `ZERO_WORD;
if_inst_o <= `ZERO_WORD;
end
end
// state/output/addr/already_jumped
always @(posedge clk) begin
if(!rst) begin
if(rdy) begin
state <= next_state;
output_state <= next_output_state;
current_addr <= next_addr;
already_jumped <= next_already_jumped;
end
end
else begin
state <= OFF;
output_state <= NOout;
current_addr <= `ZERO_WORD;
already_jumped <= `False;
// next_state <= OFF;
// next_output_state <= NOout;
// next_addr <= `ZERO_WORD;
// next_already_jumped <= `False;
end
end
// resume ex/id/if when LOAD is ready (id stall caused by Read after LOAD)
always @(posedge clk) begin
if(!rst) begin
if(rdy) begin
if(id_stall_req_i && next_output_state == LOADout && next_state == OFF && id_stall_pc_i == mem_next_pc_i)
// LOAD off && is the last load.
id_stall_req_resume_o <= `Enable;
else id_stall_req_resume_o <= `Disable;
end
end
else id_stall_req_resume_o <= `Disable;
end
// LOAD/IF ready or not
always @(posedge clk) begin
if(!rst) begin
if(rdy) begin
load_store_ready_o <= `Disable;
if_inst_ready_o <= `Disable;
if(next_state == OFF) begin
if(next_output_state == LOADout || next_output_state == STOREout)
load_store_ready_o <= `Enable;
else if(next_output_state == IFout)
if_inst_ready_o <= `Enable;
end
end
end
else begin
load_store_ready_o <= `Disable;
if_inst_ready_o <= `Disable;
end
end
// LOAD/IF/STORE addr_o/wr_o
always @(posedge clk) begin
if(!rst) begin
if(rdy) begin
if(next_state != OFF && next_state != RW4) begin // OFF/RW4: do not request read from RAM
case(next_output_state)
LOADout: begin
ram_addr_o <= next_addr;
ram_wr_o <= `Read;
end
IFout: begin
ram_addr_o <= next_addr;
ram_wr_o <= `Read;
end
STOREout: begin
ram_addr_o <= next_addr;
ram_wr_o <= `Write;
end
default: begin
ram_addr_o <= `ZERO_WORD;
ram_wr_o <= `Read;
end
endcase
end
else if(next_state == OFF) begin // when OFF, just read 0x0. hci has a bug causing consecutive write on 0x30000 failed.
ram_addr_o <= `ZERO_WORD;
ram_wr_o <= `Read;
end
end
end
end
// STORE data out
always @(posedge clk) begin
if(!rst) begin
if(rdy) begin
if(next_output_state == STOREout) begin
case(next_state)
RW0: ram_data_o <= store_data_i[7: 0];
RW1: ram_data_o <= store_data_i[15: 8];
RW2: ram_data_o <= store_data_i[23: 16];
RW3: ram_data_o <= store_data_i[31: 24];
endcase
end
end
end
end
endmodule