-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvolution_engine.v
More file actions
215 lines (198 loc) · 4.38 KB
/
convolution_engine.v
File metadata and controls
215 lines (198 loc) · 4.38 KB
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
`timescale 1ns / 1ps
module mac #(
parameter n = 16
)(
input clk,rst,ce,
input [n-1:0] a,
input [n-1:0] b,
input [n-1:0] c,
output reg [n-1:0] p
);
always@(posedge clk,posedge rst)
begin
if(rst)
begin
p<=0;
end
else if(ce)
begin
p <= (a*b+c);
end
end
endmodule
module variable_shift_reg #(parameter width = 8, parameter size = 3) (
input clk,
input ce,
input rst,
input [width-1:0] data_in,
output [width-1:0] data_out
);
reg [width-1:0] sr [size-1:0];
generate
genvar i;
for(i = 0;i < size;i = i + 1)
begin
always@(posedge clk or posedge rst)
begin
if(rst)
begin
sr[i] <= 'd0;
end
else
begin
if(ce)
begin
if(i == 'd0)
begin
sr[i] <= data_in;
end
else
begin
sr[i] <= sr[i-1];
end
end
end
end
end
endgenerate
assign data_out = sr[size-1];
endmodule
module convolver #(
parameter n = 9'h00a,
parameter k = 9'h003,
parameter s = 1,
parameter n = 16
)(
input clk,
input ce,
input global_rst,
input [n-1:0] activation,
input [(k*k)*16-1:0] weight1,
output[n-1:0] conv_op,
output valid_conv,
output end_conv
);
reg [31:0] count,count2,count3,row_count;
reg en1,en2,en3;
wire [15:0] tmp [k*k+1:0];
wire [15:0] weight [0:k*k-1];
generate
genvar l;
for(l=0;l<k*k;l=l+1)
begin
assign weight [l][n-1:0] = weight1[n*l +: n];
end
endgenerate
assign tmp[0] = 32'h0000000;
generate
genvar i;
for(i = 0;i<k*k;i=i+1)
begin: MAC
if((i+1)%k ==0)
begin
if(i==k*k-1)
begin
(* use_dsp = "yes" *)
mac #(.n(n)) mac(
.clk(clk),
.ce(ce),
.rst(global_rst),
.a(activation),
.b(weight[i]),
.c(tmp[i]),
.p(conv_op)
);
end
else
begin
wire [n-1:0] tmp2;
mac #(.n(n)) mac(
.clk(clk),
.ce(ce),
.rst(global_rst),
.a(activation),
.b(weight[i]),
.c(tmp[i]),
.p(tmp2)
);
variable_shift_reg #(.width(32),.size(n-k)) SR (
.clk(clk),
.ce(ce),
.rst(global_rst),
.data_in(tmp2),
.data_out(tmp[i+1])
);
end
end
else
begin
(* use_dsp = "yes" *)
mac #(.n(n)) mac2(
.clk(clk),
.ce(ce),
.rst(global_rst),
.a(activation),
.b(weight[i]),
.c(tmp[i]),
.p(tmp[i+1])
);
end
end
endgenerate
always@(posedge clk)
begin
if(global_rst)
begin
count <=0;
count2<=0;
count3<=0;
row_count <= 0;
en1<=0;
en2<=1;
en3<=0;
end
else if(ce)
begin
if(count == (k-1)*n+k-1)
begin
en1 <= 1'b1;
count <= count+1'b1;
end
else
begin
count<= count+1'b1;
end
end
if(en1 && en2)
begin
if(count2 == n-k)
begin
count2 <= 0;
en2 <= 0 ;
row_count <= row_count + 1'b1;
end
else
begin
count2 <= count2 + 1'b1;
end
end
if(~en2)
begin
if(count3 == k-2)
begin
count3<=0;
en2 <= 1'b1;
end
else
count3 <= count3 + 1'b1;
end
if((((count2 + 1) % s == 0) && (row_count % s == 0))||(count3 == k-2)&&(row_count % s == 0)||(count == (k-1)*n+k-1))
begin
en3 <= 1;
end
else
en3 <= 0;
end
assign end_conv = (count>= n*n+2) ? 1'b1 : 1'b0;
assign valid_conv = (en1&&en2&&en3);
endmodule