-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuffer_b_4x.v
323 lines (291 loc) · 8.6 KB
/
buffer_b_4x.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
module buffer_b_4x #(parameter VAR_SIZE = 8, parameter MMU_SIZE = 10)
(
output reg signed [(VAR_SIZE*MMU_SIZE-1):0] B1,
input wire signed [(VAR_SIZE-1):0] A,
input wire clk,
input wire rst_n,
input wire stop,
input wire [1:0] cmd,
input wire [4:0] buffer,
input wire [7:0] dim_x_in,
input wire [7:0] dim_y_in,
output reg [7:0] dim_x_out,
output reg [7:0] dim_y_out
);
localparam STATE_IDLE = 2'b00;
localparam STATE_LOAD = 2'b01;
localparam STATE_SEND = 2'b10;
localparam STATE_CLEAR = 2'b11;
localparam CMD_NONE = 2'b00;
localparam CMD_LOAD = 2'b01;
localparam CMD_SEND = 2'b10;
localparam CMD_CLEAR = 2'b11;
reg [1:0] state, state_nxt;
reg [3:0] buffer_pointer, buffer_pointer_nxt;
reg [7:0] row_pointer, row_pointer_nxt;
reg [7:0] column_pointer, column_pointer_nxt;
reg [(MMU_SIZE-1):0] write;
reg [7:0] dim_x;
reg [7:0] dim_y;
always @(posedge clk) begin
if (!rst_n) begin
state <= STATE_IDLE;
buffer_pointer <= 0;
row_pointer <= 0;
column_pointer <= 0;
end
else if (stop) begin
state <= (cmd == CMD_CLEAR) ? STATE_CLEAR : state_nxt;
buffer_pointer <= buffer_pointer_nxt;
row_pointer <= row_pointer;
column_pointer <= column_pointer;
end
else begin
state <= state_nxt;
buffer_pointer <= buffer_pointer_nxt;
row_pointer <= row_pointer_nxt;
column_pointer <= column_pointer_nxt;
end
end
always @* begin
case (state)
STATE_IDLE: begin
state_nxt = cmd;
if (cmd == CMD_NONE)
buffer_pointer_nxt = buffer_pointer;
else
buffer_pointer_nxt = buffer;
row_pointer_nxt = (state_nxt == STATE_SEND) ? 1 : 0;
column_pointer_nxt = 0;
end
STATE_LOAD: begin
if (row_pointer >= (dim_x - 1) && column_pointer >= (dim_y - 1)) begin
state_nxt = STATE_IDLE;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = 0;
column_pointer_nxt = 0;
end
else if (row_pointer >= (dim_x - 1)) begin
state_nxt = state;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = 0;
column_pointer_nxt = column_pointer + 1;
end
else begin
state_nxt = state;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = row_pointer + 1;
column_pointer_nxt = column_pointer;
end
end
STATE_SEND: begin
if (row_pointer >= dim_x) begin
state_nxt = STATE_IDLE;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = 0;
column_pointer_nxt = column_pointer;
end
else begin
state_nxt = state;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = row_pointer + 1;
column_pointer_nxt = column_pointer;
end
end
STATE_CLEAR: begin
if (row_pointer >= (MMU_SIZE - 1)) begin
state_nxt = STATE_IDLE;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = 0;
column_pointer_nxt = column_pointer;
end
else begin
state_nxt = state;
buffer_pointer_nxt = buffer_pointer;
row_pointer_nxt = row_pointer + 1;
column_pointer_nxt = column_pointer;
end
end
endcase
end
always @* begin
case(state)
STATE_IDLE: write = 0;
STATE_LOAD: write = stop ? 0 : (1 << column_pointer);
STATE_SEND: write = 0;
STATE_CLEAR: write = -1;
endcase
end
wire signed [(VAR_SIZE*MMU_SIZE-1):0] buffer0_B1;
wire signed [(VAR_SIZE-1):0] buffer0_A;
wire [(MMU_SIZE-1):0] buffer0_write;
mem_1to2 #(.VAR_SIZE(VAR_SIZE), .MMU_SIZE(MMU_SIZE)) m_buffer0
(
.B1(buffer0_B1),
.A(buffer0_A),
.address(row_pointer),
.write(buffer0_write),
.clk(clk)
);
assign buffer0_A = (buffer_pointer == 0 && state != STATE_CLEAR) ? A : 0;
assign buffer0_write = (buffer_pointer == 0) ? write : 0;
wire signed [(VAR_SIZE*MMU_SIZE-1):0] buffer1_B1;
wire signed [(VAR_SIZE-1):0] buffer1_A;
wire [(MMU_SIZE-1):0] buffer1_write;
mem_1to2 #(.VAR_SIZE(VAR_SIZE), .MMU_SIZE(MMU_SIZE)) m_buffer1
(
.B1(buffer1_B1),
.A(buffer1_A),
.address(row_pointer),
.write(buffer1_write),
.clk(clk)
);
assign buffer1_A = (buffer_pointer == 1 && state != STATE_CLEAR) ? A : 0;
assign buffer1_write = (buffer_pointer == 1) ? write : 0;
wire signed [(VAR_SIZE*MMU_SIZE-1):0] buffer2_B1;
wire signed [(VAR_SIZE-1):0] buffer2_A;
wire [(MMU_SIZE-1):0] buffer2_write;
mem_1to2 #(.VAR_SIZE(VAR_SIZE), .MMU_SIZE(MMU_SIZE)) m_buffer2
(
.B1(buffer2_B1),
.A(buffer2_A),
.address(row_pointer),
.write(buffer2_write),
.clk(clk)
);
assign buffer2_A = (buffer_pointer == 2 && state != STATE_CLEAR) ? A : 0;
assign buffer2_write = (buffer_pointer == 2) ? write : 0;
wire signed [(VAR_SIZE*MMU_SIZE-1):0] buffer3_B1;
wire signed [(VAR_SIZE-1):0] buffer3_A;
wire [(MMU_SIZE-1):0] buffer3_write;
mem_1to2 #(.VAR_SIZE(VAR_SIZE), .MMU_SIZE(MMU_SIZE)) m_buffer3
(
.B1(buffer3_B1),
.A(buffer3_A),
.address(row_pointer),
.write(buffer3_write),
.clk(clk)
);
assign buffer3_A = (buffer_pointer == 3 && state != STATE_CLEAR) ? A : 0;
assign buffer3_write = (buffer_pointer == 3) ? write : 0;
always @* begin
if (state == STATE_SEND) begin
case (buffer_pointer)
0: B1 = buffer0_B1;
1: B1 = buffer1_B1;
2: B1 = buffer2_B1;
3: B1 = buffer3_B1;
default: B1 = 0;
endcase
end
else B1 = 0;
end
reg [7:0] buffer0_dim_x, buffer0_dim_x_nxt, buffer0_dim_y, buffer0_dim_y_nxt;
reg [7:0] buffer1_dim_x, buffer1_dim_x_nxt, buffer1_dim_y, buffer1_dim_y_nxt;
reg [7:0] buffer2_dim_x, buffer2_dim_x_nxt, buffer2_dim_y, buffer2_dim_y_nxt;
reg [7:0] buffer3_dim_x, buffer3_dim_x_nxt, buffer3_dim_y, buffer3_dim_y_nxt;
always @(posedge clk) begin
if (!rst_n) begin
buffer0_dim_x <= 0;
buffer0_dim_y <= 0;
buffer1_dim_x <= 0;
buffer1_dim_y <= 0;
buffer2_dim_x <= 0;
buffer2_dim_y <= 0;
buffer3_dim_x <= 0;
buffer3_dim_y <= 0;
end
else begin
buffer0_dim_x <= buffer0_dim_x_nxt;
buffer0_dim_y <= buffer0_dim_y_nxt;
buffer1_dim_x <= buffer1_dim_x_nxt;
buffer1_dim_y <= buffer1_dim_y_nxt;
buffer2_dim_x <= buffer2_dim_x_nxt;
buffer2_dim_y <= buffer2_dim_y_nxt;
buffer3_dim_x <= buffer3_dim_x_nxt;
buffer3_dim_y <= buffer3_dim_y_nxt;
end
end
always @* begin
buffer0_dim_x_nxt = buffer0_dim_x;
buffer0_dim_y_nxt = buffer0_dim_y;
buffer1_dim_x_nxt = buffer1_dim_x;
buffer1_dim_y_nxt = buffer1_dim_y;
buffer2_dim_x_nxt = buffer2_dim_x;
buffer2_dim_y_nxt = buffer2_dim_y;
buffer3_dim_x_nxt = buffer3_dim_x;
buffer3_dim_y_nxt = buffer3_dim_y;
if (state == STATE_IDLE) begin
if (state_nxt == STATE_LOAD) begin
case(buffer)
0: begin
buffer0_dim_x_nxt = dim_x_in;
buffer0_dim_y_nxt = dim_y_in;
end
1: begin
buffer1_dim_x_nxt = dim_x_in;
buffer1_dim_y_nxt = dim_y_in;
end
2: begin
buffer2_dim_x_nxt = dim_x_in;
buffer2_dim_y_nxt = dim_y_in;
end
3: begin
buffer3_dim_x_nxt = dim_x_in;
buffer3_dim_y_nxt = dim_y_in;
end
endcase
end
else if (state_nxt == STATE_CLEAR) begin
case(buffer)
0: begin
buffer0_dim_x_nxt = 0;
buffer0_dim_y_nxt = 0;
end
1: begin
buffer1_dim_x_nxt = 0;
buffer1_dim_y_nxt = 0;
end
2: begin
buffer2_dim_x_nxt = 0;
buffer2_dim_y_nxt = 0;
end
3: begin
buffer3_dim_x_nxt = 0;
buffer3_dim_y_nxt = 0;
end
endcase
end
end
end
always @(posedge clk) begin
if (!rst_n) begin
dim_x_out <= 0;
dim_y_out <= 0;
end
else begin
dim_x_out <= dim_x;
dim_y_out <= dim_y;
end
end
always @* begin
case (buffer)
0: begin
dim_x = buffer0_dim_x;
dim_y = buffer0_dim_y;
end
1: begin
dim_x = buffer1_dim_x;
dim_y = buffer1_dim_y;
end
2: begin
dim_x = buffer2_dim_x;
dim_y = buffer2_dim_y;
end
3: begin
dim_x = buffer3_dim_x;
dim_y = buffer3_dim_y;
end
endcase
end
endmodule