-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxpool
145 lines (136 loc) · 3.7 KB
/
maxpool
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
`timescale 1 ns / 10 ps
module MaxPoolSingle(clk,reset,relu_out,mPoolOut,max_to_pad_en,max_end,row,column,depth,i,j,mPoolIn);
parameter DATA_WIDTH = 16;
parameter H = 28;
parameter W = 28;
parameter D = 4;
parameter MAX = 2;
parameter MAXD = 4;
parameter ME = 7;
input clk,reset,relu_out;
input [DATA_WIDTH -1:0]mPoolIn;
output reg max_to_pad_en,max_end;
output reg[DATA_WIDTH -1:0]mPoolOut;
output reg[5:0]row,column,depth;
output reg[2:0]i,j;
///////////////////////////////////////////////////////////
reg [ME*MAXD*DATA_WIDTH -1:0]max_num;
reg [ME*MAXD*DATA_WIDTH -1:0]image_to_max;
//reg [DATA_WIDTH -1:0]image_Buff[0:H*W*D-1];
reg [DATA_WIDTH -1:0]MaxOut1[0:(H>>1)*(W>>1)*D -1];
wire [DATA_WIDTH*ME -1:0]output_max;
reg [DATA_WIDTH*ME -1:0]output_max_to_ram;
reg [9:0]k,l,buff_counter;
reg [4:0]MAX_counter,counter;
/////////////////////////////////////////////////////test
reg unit_en,en,ram_en,ram_end;
/////////////////////////////////////////////////////image_to_ram
/*
always@(posedge clk or posedge reset)begin
if(reset == 1'b1)begin
input_end <= 1'b0;
end else if(relu_out == 1'b1)begin
input_end <= 1'b1;
end else if(max_en == 1'b1 && relu_out == 1'b0)begin
image_Buff [covdata_to_max] <= mPoolIn;
end
end*/
always@(posedge clk or posedge reset)begin
if(reset == 1'b1)begin
row <= 1'b0;
column <= 1'b0;
j <= 1'b0;
i <=1'b0;
MAX_counter<=1'b0;
max_end <= 1'b0;
depth <= 1'b0;
unit_en <=1'b0;
end else if(relu_out == 1'b1 && max_end == 1'b0) begin
if(i < 2 && j < 2 && column < H && row < H && depth <D && max_end == 1'b0 && MAX_counter < ME*MAXD)begin
image_to_max[MAX_counter*DATA_WIDTH+:DATA_WIDTH] <= mPoolIn;
i <= i +1'b1;
MAX_counter <= MAX_counter +1'b1;
unit_en <= 1'b0;
end else if(i == 2) begin
j <= j + 1'b1;
i <= 1'b0;
end else if(j == 2)begin
column <= column+2'b10;
j <= 1'b0;
end else if(column == H)begin
column <= 1'b0;
row <= row +2'b10;
end else if(row == H)begin
row <= 1'b0;
depth <= depth +1'b1;
end else if(depth == D && MAX_counter != ME*MAXD)begin
max_end <= 1'b1;
depth <= 1'b0;
unit_en <= 1'b0;
end else if(MAX_counter == ME*MAXD)begin
max_num <= image_to_max;
MAX_counter <= 1'b0;
unit_en <= 1'b1;
end
end
end
always@(posedge clk or posedge reset)begin
if(reset == 1'b1)begin
en <= 1'b0;
counter <= 1'b0;
ram_en <= 1'b0;
k <=1'b0;
l <=1'b0;
ram_end<=1'b0;
output_max_to_ram <= 1'b0;
end else if(unit_en == 1'b1)begin
en <= 1'b1;
end else if(en ==1'b1 && counter < 2'b11)begin
counter <= counter +1'b1;
end else if(counter == 2'b11)begin
counter <= 1'b0;
en <= 1'b0;
output_max_to_ram <= output_max;
ram_en <= 1'b1;
end else if(ram_en == 1'b1 && k < ME)begin
MaxOut1[l] <= output_max_to_ram[k*DATA_WIDTH+:DATA_WIDTH];
k <= k+1'b1;
l <= l+1'b1;
end else if(k == ME)begin
k <= 1'b0;
ram_en <= 1'b0;
end else if(l == (H>>1)*(W>>1)*D)begin
ram_end <=1'b1;
end
end
always@(posedge clk or posedge reset)begin
if(reset == 1'b1)begin
max_to_pad_en <= 1'b0;
buff_counter <= 1'b0;
end else if(ram_end == 1'b1)begin
if(buff_counter < (H>>1)*(W>>1)*D)begin
max_to_pad_en <= 1'b1;
mPoolOut <= MaxOut1[buff_counter];
buff_counter <= buff_counter+1'b1;
end else if(buff_counter == (H>>1)*(W>>1)*D)begin
max_to_pad_en <= 1'b0;
end
end
end
genvar n;
generate //generating n convolution units where n is half the number of pixels in one row of the output image
for (n = 0; n < 7; n = n + 1) begin :Maxpooling
Maxuint
#(
.DATA_WIDTH(DATA_WIDTH)
)max
(
.clk(clk),
.reset(reset),
.en(unit_en),
.num(max_num[n*MAXD*DATA_WIDTH+:MAXD*DATA_WIDTH]),
.MaxOut(output_max[n*DATA_WIDTH+:DATA_WIDTH])
);
end
endgenerate
endmodule