-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvUnit
51 lines (39 loc) · 1.53 KB
/
convUnit
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
`timescale 100 ns / 10 ps
module convUnit(clk,reset,image,filter,result);
parameter DATA_WIDTH = 16;
parameter D = 3; //depth of the filter
parameter F = 3; //size of the filter
input clk, reset;
input [D*F*F*DATA_WIDTH-1:0]image;
input [D*F*F*DATA_WIDTH-1:0]filter;
output reg [DATA_WIDTH-1:0] result;
reg [DATA_WIDTH-1:0] selectedInput1, selectedInput2;
wire [DATA_WIDTH-1:0] multResult;
wire [DATA_WIDTH-1:0] addResult;
integer i;
floatMult FM (selectedInput1,selectedInput2,multResult);
floatAdd FADD (multResult,result,addResult);
// The convolution is calculated in a sequential process to save hardware
// The result of the element wise matrix multiplication is finished after (F*F+2) cycles (2 cycles to reset the processing element and F*F cycles to accumulate the result of the F*F multiplications)
always @ (posedge clk, posedge reset) begin
if (reset == 1'b1) begin // reset
i = 1'b0;
selectedInput1 = 1'b0;
selectedInput2 = 1'b0;
end else if (i >= D*F*F) begin // if the convolution is finished but we still wait for other blocks to finsih, send zeros to the conv unit (in case of pipelining)
selectedInput1 = 1'b0;
selectedInput2 = 1'b0;
end else begin // send one element of the image part and one element of the filter to be multiplied and accumulated
selectedInput1 = image[DATA_WIDTH*i+:DATA_WIDTH];
selectedInput2 = filter[DATA_WIDTH*i+:DATA_WIDTH];
i = i + 1;
end
end
always @ (posedge clk or posedge reset) begin
if (reset == 1'b1) begin
result = 1'b0;
end else begin
result = addResult;
end
end
endmodule