-
Notifications
You must be signed in to change notification settings - Fork 0
/
BUS_controller.v
278 lines (254 loc) · 6.37 KB
/
BUS_controller.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2022/06/16 22:14:02
// Design Name:
// Module Name: BUS_controller
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module BUS_controller #(
parameter [6:0] DATA_WIDTH = 32,
parameter [6:0] ADDR_WIDTH = 32
)
(
input clk,
input rst_n,
input mode, // mode 0 read, mode 1 write
output rdata_valid,
output write_done,
input start_transaction,
output [DATA_WIDTH-1:0] rdata,
input [ADDR_WIDTH-1:0] addr,
input [DATA_WIDTH-1:0] wdata,
output [ADDR_WIDTH-1:0] BUS_addr,
output [DATA_WIDTH-1:0] BUS_wdata,
input [DATA_WIDTH-1:0] BUS_rdata,
output BUS_valid,
input BUS_wready,
output BUS_rready,
input BUS_rvalid,
output BUS_mode
);
localparam [1:0] IDLE = 2'd0,
WRITE = 2'd1,
READ = 2'd2;
reg [1:0] cur_state, next_state;
reg start_write, write_active;
reg start_read, read_active;
// reg rdata_valid_r;
reg [DATA_WIDTH-1:0] rdata_r;
reg [ADDR_WIDTH-1:0] BUS_addr_r;
reg [DATA_WIDTH-1:0] BUS_wdata_r;
reg [DATA_WIDTH-1:0] BUS_rdata_r;
reg BUS_wvalid_r;
reg BUS_rready_r;
reg BUS_mode_r;
wire write_done_in;
wire read_done;
assign write_done = write_done_in;
assign rdata_valid = read_done;
// assign rdata_valid = rdata_valid_r;
assign rdata = rdata_r;
assign BUS_addr = BUS_addr_r;
assign BUS_wdata = BUS_wdata_r;
assign BUS_valid = (cur_state == WRITE) ? BUS_wvalid_r : (start_read || read_active);
assign BUS_rready = BUS_rready_r;
assign BUS_mode = BUS_mode_r;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
BUS_addr_r <= 0;
end
else begin
if (start_transaction) begin
BUS_addr_r <= addr;
end
else begin
BUS_addr_r <= BUS_addr_r;
end
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
BUS_mode_r <= 1'b0;
end
else begin
if (start_transaction) begin
BUS_mode_r <= mode;
end
else begin
BUS_mode_r <= BUS_mode_r;
end
end
end
/************** write logic **************/
assign write_done_in = BUS_wvalid_r & BUS_wready;
always @(posedge clk or negedge rst_n) begin
if (!rst_n || cur_state == IDLE) begin
write_active <= 1'b0;
end
else begin
if (start_write && ~write_active) begin
write_active <= 1'b1;
end
else if (write_active && write_done_in) begin
write_active <= 1'b0;
end
else begin
write_active <= write_active;
end
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n || cur_state == IDLE) begin
BUS_wvalid_r <= 1'b0;
end
else begin
if (start_write) begin
BUS_wvalid_r <= 1'b1;
end
else if (BUS_wvalid_r && write_active && BUS_wready) begin
BUS_wvalid_r <= 1'b0;
end
else begin
BUS_wvalid_r <= BUS_wvalid_r;
end
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
BUS_wdata_r <= 0;
end
else if (start_transaction && mode == 1) begin
BUS_wdata_r <= wdata;
end
else begin
BUS_wdata_r <= BUS_wdata_r;
end
end
/************** write logic end **************/
assign read_done = BUS_rvalid & BUS_rready;
always @(posedge clk or negedge rst_n) begin
if (!rst_n || cur_state == IDLE) begin
read_active <= 1'b0;
end
else begin
if (start_read && ~read_active) begin
read_active <= 1'b1;
end
else if (read_active && read_done) begin
read_active <= 1'b0;
end
else begin
read_active <= read_active;
end
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n || cur_state == IDLE) begin
BUS_rready_r <= 1'b0;
end
else begin
if (BUS_rvalid && read_active && ~BUS_rready_r) begin
BUS_rready_r <= 1'b1;
end
else begin
BUS_rready_r <= 1'b0;
end
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
rdata_r <= 0;
end
else begin
if (BUS_rvalid && read_active) begin
rdata_r <= BUS_rdata;
end
else begin
rdata_r <= rdata_r;
end
end
end
/***************** state machine *****************/
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cur_state <= 1'b0;
end
else begin
cur_state <= next_state;
end
end
always @(*) begin
next_state = IDLE;
case (cur_state)
IDLE: begin
if (start_transaction && mode == 0) begin
next_state = READ;
end
else if (start_transaction && mode == 1) begin
next_state = WRITE;
end
else begin
next_state = IDLE;
end
end
WRITE: begin
if (write_active && write_done_in) begin
next_state = IDLE;
end
else begin
next_state = WRITE;
end
end
READ: begin
if (read_active && read_done) begin
next_state = IDLE;
end
else begin
next_state = READ;
end
end
default: next_state = IDLE;
endcase
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cur_state <= 1'b0;
start_write <= 1'b0;
start_read <= 1'b0;
end
else begin
case (cur_state)
WRITE: begin
if (~start_write && ~write_active) begin
start_write <= 1'b1;
end
else begin
start_write <= 1'b0;
end
end
READ: begin
if (~start_read && ~read_active) begin
start_read <= 1'b1;
end
else begin
start_read <= 1'b0;
end
end
endcase
end
end
/***************** state machine end *****************/
endmodule