-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop.v
408 lines (346 loc) · 12.4 KB
/
top.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
module SNN (
input clk,
input reset,
input start_en,
input reset
);
parameter CHANNELS = 32;
reg fc_start[CHANNELS-1 : 0], conv_start[CHANNELS-1 : 0], LIF_start[CHANNELS-1 : 0], mp_start[CHANNELS-1 : 0], fc_start[CHANNELS-1 : 0];
reg start_en [CHANNELS-1 : 0];
reg conv_done [CHANNELS-1 : 0];
reg LIF_done [CHANNELS-1 : 0];
reg mp_done [CHANNELS-1 : 0];
reg fc_done [CHANNELS-1 : 0];
reg start_en_prev[CHANNELS-1 : 0];
reg conv_done_prev[CHANNELS-1 : 0];
reg LIF_done_prev[CHANNELS-1 : 0];
reg mp_done_prev[CHANNELS-1 : 0];
reg fc_done_prev[CHANNELS-1 : 0];
wire conv_src1_address[CHANNELS-1 : 0];
wire conv_src1_readdata[CHANNELS-1 : 0];
wire conv_src1_writedata[CHANNELS-1 : 0];
wire conv_src1_write[CHANNELS-1 : 0];
wire conv_src2_address[CHANNELS-1 : 0];
wire conv_src2_readdata[CHANNELS-1 : 0];
wire conv_src2_writedata[CHANNELS-1 : 0];
wire conv_src2_write[CHANNELS-1 : 0];
wire conv_dest_address[CHANNELS-1 : 0];
wire conv_dest_readdata[CHANNELS-1 : 0];
wire conv_dest_writedata[CHANNELS-1 : 0];
wire conv_dest_write[CHANNELS-1 : 0];
wire LIF_src1_address[CHANNELS-1 : 0];
wire LIF_src1_readdata[CHANNELS-1 : 0];
wire LIF_src1_writedata[CHANNELS-1 : 0];
wire LIF_src1_write[CHANNELS-1 : 0];
wire LIF_src2_address[CHANNELS-1 : 0];
wire LIF_src2_readdata[CHANNELS-1 : 0];
wire LIF_src2_writedata[CHANNELS-1 : 0];
wire LIF_src2_write[CHANNELS-1 : 0];
wire LIF_dest_address[CHANNELS-1 : 0];
wire LIF_dest_readdata[CHANNELS-1 : 0];
wire LIF_dest_writedata[CHANNELS-1 : 0];
wire LIF_dest_write[CHANNELS-1 : 0];
wire mp_src1_address[CHANNELS-1 : 0];
wire mp_src1_readdata[CHANNELS-1 : 0];
wire mp_src1_writedata[CHANNELS-1 : 0];
wire mp_src1_write[CHANNELS-1 : 0];
wire mp_src2_address[CHANNELS-1 : 0];
wire mp_src2_readdata[CHANNELS-1 : 0];
wire mp_src2_writedata[CHANNELS-1 : 0];
wire mp_src2_write[CHANNELS-1 : 0];
wire mp_dest_address[CHANNELS-1 : 0];
wire mp_dest_readdata[CHANNELS-1 : 0];
wire mp_dest_writedata[CHANNELS-1 : 0];
wire mp_dest_write[CHANNELS-1 : 0];
wire fc_src1_address[CHANNELS-1 : 0];
wire fc_src1_readdata[CHANNELS-1 : 0];
wire fc_src1_writedata[CHANNELS-1 : 0];
wire fc_src1_write[CHANNELS-1 : 0];
wire fc_src2_address[CHANNELS-1 : 0];
wire fc_src2_readdata[CHANNELS-1 : 0];
wire fc_src2_writedata[CHANNELS-1 : 0];
wire fc_src2_write[CHANNELS-1 : 0];
wire fc_dest_address[CHANNELS-1 : 0];
wire fc_dest_readdata[CHANNELS-1 : 0];
wire fc_dest_writedata[CHANNELS-1 : 0];
wire fc_dest_write[CHANNELS-1 : 0];
// wire conv_done[i], fc_done, mp_done;
// wire conv_we, conv_we[i], conv_we_1, mp_we_0, mp_we_1, mp_we, fc_we; //write enable
// wire [11:0] conv_address[i], conv_address_1, conv_address;
// wire [11:0] fc_address_0, fc_address_1, fc_address;
// wire [11:0] mp_address, mp_address_0, mp_address_1;
// wire [7:0] final_result;
// wire [15:0] conv_writedata[i], conv_writedata_1, conv_writedata, fc_writedata, fc_writedata_0, fc_writedata_1, mp_writedata;
genvar i;
generate
for (i = 0; i < 32; i = i + 1) begin
always @ (posedge clk) begin
if(reset) begin
start_en_prev[i] <= 1'b0;
conv_done_prev[i] <= 1'b0;
LIF_done_prev[i] <= 1'b0;
mp_done_prev[i] <= 1'b0;
fc_done_prev[i] <= 1'b0;
end
else begin
start_en_prev[i] <= start_en;
conv_done_prev[i] <= conv_done[i];
LIF_done_prev[i] <= LIF_done[i];
mp_done_prev[i] <= mp_done[i];
fc_done_prev[i] <= fc_done[i];
end
end
always @ (posedge clk) begin
if(reset) begin
conv_start[i] <= 1'b0;
end
else begin
if(start_en&&~start_en_prev[i]) begin
conv_start[i] <= 1'b1;
end
else begin
conv_start[i] <= 1'b0;
end
end
end
always @ (posedge clk) begin
if(reset) begin
LIF_start[i] <= 1'b0;
end
else begin
if(conv_done[i]&&~conv_done_prev[i]) begin
LIF_start[i] <= 1'b1;
end
else begin
LIF_start[i] <= 1'b0;
end
end
end
always @ (posedge clk) begin
if(reset) begin
mp_start[i] <= 1'b0;
end
else begin
if(LIF_done&&~LIF_done_prev[i]) begin
mp_start[i] <= 1'b1;
end
else begin
mp_start[i] <= 1'b0;
end
end
end
always @ (posedge clk) begin
if(reset) begin
fc_start[i] <= 1'b0;
end
else begin
if(mp_done&&~mp_done_prev[i]) begin
fc_start[i] <= 1'b1;
end
else begin
fc_start[i] <= 1'b0;
end
end
end
conv_unit conv_layer(
.clk(clk),
.reset(reset),
.start(conv_start[i]),
.done(conv_done[i]),
.src1_start_address(12'd0),
.src2_start_address(12'd0),
.src1_address(conv_src1_address[i]),
.src1_readdata(conv_src1_readdata[i]),
.src1_writedata(conv_src1_writedata[i]),
.src1_write_en(conv_src1_write[i]),
.src2_address(conv_src2_address[i]),
.src2_readdata(conv_src2_readdata[i]),
.src2_writedata(conv_src2_writedata[i]),
.src2_write_en(conv_src2_write[i]),
.src1_row_size(9'd32),
.src1_col_size(9'd32),
.src2_row_size(6'd3),
.src2_col_size(6'd3),
.dest_start_address(12'd0),
.dest_address(conv_dest_address[i]),
.dest_writedata(conv_dest_writedata[i]),
.dest_readdata(conv_dest_readdata[i]),
.dest_write_en(conv_dest_write[i])
);
ram conv_src1_ram(
.q(conv_src1_readdata[i]),
.d(conv_src1_writedata[i]),
.address(conv_src1_address[i]),
.we(conv_src1_write[i]),
.clk(clk)
);
ram conv_src2_ram(
.q(conv_src2_readdata[i]),
.d(conv_src2_writedata[i]),
.address(conv_src2_address[i]),
.we(conv_src2_write[i]),
.clk(clk)
);
always @(*) begin
if(reset) begin
LIF_sel[i] = 0;
end
else begin
if(conv_done[i]) begin
LIF_sel[i] = 0;
end
else begin
LIF_sel[i] = 1;
end
end
end
always @(*) begin
if(LIF_sel) begin
conv_dest_ram_q[i] = conv_dest_readdata[i];
conv_dest_ram_d[i] = conv_dest_writedata[i];
conv_dest_ram_addr[i] = conv_dest_address[i];
conv_dest_ram_we[i] = conv_dest_write[i];
end
else begin
conv_dest_ram_q[i] = LIF_src1_readdata[i];
conv_dest_ram_d[i] = LIF_src1_writedata[i];
conv_dest_ram_addr[i] = LIF_src1_address[i];
conv_dest_ram_we[i] = LIF_src1_write[i];
end
end
ram conv_dest_ram(
.q(conv_dest_ram_q[i]),
.d(conv_dest_ram_d[i]),
.address(conv_dest_ram_addr[i]),
.we(conv_dest_ram_we[i]),
.clk(clk)
);
matrix_LIF LIF_layer(
.clk(clk),
.reset(reset),
.start(LIF_start[i]),
.done(LIF_done),
.src1_start_address(12'd0),
.src2_start_address(12'd0),
.src1_address(LIF_src1_address[i]),
.src1_readdata(LIF_src1_readdata[i]),
.src1_writedata(LIF_src1_writedata[i]),
.src1_write_en(LIF_src1_write[i]),
.src2_address(LIF_src2_address[i]),
.src2_readdata(LIF_src2_readdata[i]),
.src2_writedata(LIF_src2_writedata[i]),
.src2_write_en(LIF_src2_write[i]),
.src1_row_size(9'd30),
.src1_col_size(9'd30),
.src2_row_size(9'd30),
.src2_col_size(9'd30),
.dest_start_address(12'd0),
.dest_address(LIF_dest_address[i]),
.dest_writedata(LIF_dest_writedata[i]),
.dest_readdata(LIF_dest_readdata[i]),
.dest_write_en(LIF_dest_write[i])
);
always @(*) begin
if(reset) begin
mp_sel[i] = 0;
end
else begin
if(conv_done[i]) begin
mp_sel[i] = 0;
end
else begin
mp_sel[i] = 1;
end
end
end
always @(*) begin
if(mp_sel) begin
LIF_dest_ram_q[i] = LIF_dest_readdata[i];
LIF_dest_ram_d[i] = LIF_dest_writedata[i];
LIF_dest_ram_addr[i] = LIF_dest_address[i];
LIF_dest_ram_we[i] = LIF_dest_write[i];
end
else begin
LIF_dest_ram_q[i] = mp_src2_readdata[i];
LIF_dest_ram_d[i] = mp_src2_writedata[i];
LIF_dest_ram_addr[i] = mp_src2_address[i];
LIF_dest_ram_we[i] = mp_src2_write[i];
end
end
ram conv_dest_ram(
.q(LIF_dest_ram_q[i]),
.d(LIF_dest_ram_d[i]),
.address(LIF_dest_ram_addr[i]),
.we(LIF_dest_ram_we[i]),
.clk(clk)
);
max_pooling pooling_layer(
.clk(clk),
.reset(reset),
.start(mp_start[i]),
.done(mp_done),
.src1_start_address(12'd0),
.src1_address(mp_src1_address[i]),
.src1_readdata(mp_src1_readdata[i]),
.src1_write_en(mp_src1_write[i]),
.src1_row_size(9'd30),
.src1_col_size(9'd30),
.src2_row_size(5'd2),
.src2_col_size(5'd2),
.dest_start_address(12'd0),
.dest_address(mp_dest_address[i]),
.dest_writedata(mp_dest_writedata[i]),
.dest_write_en(mp_dest_en[i])
);
matrix_fc matrix_fc_instance(
.clk(clk),
.reset(reset),
.start(fc_start),
.done(fc_done),
.src1_start_address(12'd0),
.src2_start_address(12'd0),
.src1_address(fc_src1_address[i]),
.src1_readdata(fc_src1_readdata[i]),
.src1_write_en(fc_src1_write_en[i]),
.src2_address(fc_src2_address[i]),
.src2_readdata(fc_src2_readdata[i]),
.src2_write_en(fc_src2_write_en[i]),
.src1_row_size(13'd7200),
.src1_col_size(13'd10),
.src2_row_size(13'd1),
.src2_col_size(13'd7200),
.dest_start_address(12'd0),
.dest_address(fc_dest_address[i]),
.dest_writedata(fc_dest_writedata[i]),
.dest_write_en(fc_dest_write_en[i])
);
ram fc_src1_ram(
.q(fc_src1_readdata[i]),
.d(fc_src1_writedata[i]),
.address(fc_src1_address[i]),
.we(fc_src1_write[i]),
.clk(clk)
);
always @(*) begin
if(reset) begin
fc_sel[i] = 0;
end
else begin
if(conv_done[i]) begin
fc_sel[i] = 0;
end
else begin
fc_sel[i] = 1;
end
end
end
ram conv_dest_ram(
.q(fc_dest_ram_q[i]),
.d(fc_dest_ram_d[i]),
.address(fc_dest_ram_addr[i]),
.we(fc_dest_ram_we[i]),
.clk(clk)
);
end
endgenerate
endmodule // end top level