-
Notifications
You must be signed in to change notification settings - Fork 31
/
maxpooling2.v
200 lines (166 loc) · 4.13 KB
/
maxpooling2.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 14:27:37 07/14/2017
// Design Name:
// Module Name: maxpooling
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module maxpooling2(
input cnn_clk,
input wr_en,
input conv1_done,
input [11:0] norm_result1,
input [11:0] norm_result2,
input [3:0] feature_map_counter,
output reg buffer_en,
output reg [11:0] max
);
wire [15:0] bias [11:0];
assign bias[0] = 16'h067e;
assign bias[1] = 16'h01e9;
assign bias[2] = 16'h04f1;
assign bias[3] = 16'h02db;
assign bias[4] = 16'h0627;
assign bias[5] = 16'h0449;
assign bias[6] = 16'h00bc;
assign bias[7] = 16'h82d3;
assign bias[8] = 16'h060a;
assign bias[9] = 16'h0560;
assign bias[10] = 16'h0266;
assign bias[11] = 16'h0344;
wire [11:0] bias_amend;
assign bias_amend = bias[feature_map_counter][15] ? {4'b1111,~bias[feature_map_counter][14:7]}+1'b1 : bias[feature_map_counter][15:7];
reg relu_en;
reg [5:0] addra;
reg [1:0] adder_counter;
reg [11:0] buffer1_6x10 [59:0]; //»º´æ4¸ö
reg [11:0] buffer2_6x10 [59:0];
reg [11:0] buffer3_6x10 [59:0];
reg [11:0] buffer4_6x10 [59:0];
reg [11:0] buffer_6x10 [59:0];
reg [5:0] relu_row; //0-40
reg [2:0] relu_column; //0-5
always @ ( posedge cnn_clk )
begin
if(wr_en && conv1_done) begin
addra <= addra+1'b1;
if(adder_counter == 2'd0) begin
buffer1_6x10[addra] <= norm_result1;
buffer3_6x10[addra] <= norm_result2;
end
else if(adder_counter == 2'd1) begin
buffer2_6x10[addra] <= norm_result1;
buffer4_6x10[addra] <= norm_result2;
end
else if(adder_counter == 2'd2) begin
buffer_6x10[addra] <= norm_result1+norm_result2+buffer1_6x10[addra]+
buffer2_6x10[addra]+buffer3_6x10[addra]+buffer4_6x10[addra]+bias_amend;
end
if(addra == 6'd59) begin
addra <= 6'd0;
if(adder_counter < 2'd2) begin
adder_counter <= adder_counter+1'b1;
end
else begin
adder_counter <= 2'd0;
relu_en <= 1'b1;
end
end
end
else if(relu_en) begin
if(relu_column < 3'd4) begin
relu_column <= relu_column+2;
end
else begin
relu_column <= 3'd0;
if(relu_row < 6'd40) begin
relu_row <= relu_row+20;
end
else begin
relu_en <= 1'b0;
relu_row <= 6'd0;
end
end
end
end
reg [11:0] matrix1_1,matrix1_2;
reg [11:0] matrix2_1,matrix2_2;
//clk1
always @ ( posedge cnn_clk )
begin
if(relu_en) begin
matrix1_1 <= buffer_6x10[relu_column+relu_row];
matrix1_2 <= buffer_6x10[relu_column+relu_row+1];
matrix2_1 <= buffer_6x10[relu_column+relu_row+10];
matrix2_2 <= buffer_6x10[relu_column+relu_row+11];
end
end
reg [11:0] max0,max1;
//clk2
always @ ( posedge cnn_clk )
begin
if(matrix1_1[11] && matrix1_2[11]) begin
max0 <= (matrix1_1 > matrix1_2) ? matrix1_2 : matrix1_1;
end
else if(!matrix1_1[11] && matrix1_2[11]) begin
max0 <= matrix1_1;
end
else if(matrix1_1[11] && !matrix1_2[11]) begin
max0 <= matrix1_2;
end
else if(!matrix1_1[11] && !matrix1_2[11]) begin
max0 <= (matrix1_1 > matrix1_2) ? matrix1_1 : matrix1_2;
end
end
always @ ( posedge cnn_clk )
begin
if(matrix2_1[11] && matrix2_2[11]) begin
max1 <= (matrix2_1 > matrix2_2) ? matrix2_2 : matrix2_1;
end
else if(!matrix2_1[11] && matrix2_2[11]) begin
max1 <= matrix2_1;
end
else if(matrix2_1[11] && !matrix2_2[11]) begin
max1 <= matrix2_2;
end
else if(!matrix2_1[11] && !matrix2_2[11]) begin
max1 <= (matrix2_1 > matrix2_2) ? matrix2_1 : matrix2_2;
end
end
//clk3
always @ ( posedge cnn_clk )
begin
if(max0[11] && max1[11]) begin
max <= (max0 > max1) ? max1 : max0;
end
else if(!max0[11] && max1[11]) begin
max <= max0;
end
else if(max0[11] && !max1[11]) begin
max <= max1;
end
else if(!max0[11] && !max1[11]) begin
max <= (max0 > max1) ? max0 : max1;
end
end
reg relu_en1,relu_en2;
always @ ( posedge cnn_clk ) //reluÑÓ³Ù
begin
relu_en1 <= relu_en;
relu_en2 <= relu_en1;
buffer_en <= relu_en2;
end
endmodule