| `timescale 1ns / 1ps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| module pic_in
|
| #(
|
| parameter bits = 16 ,
|
| parameter bits_shift = 4 ,
|
| parameter channel_num = 1 ,
|
| parameter channel_height_num = 4 ,
|
| parameter channel_length_num = 4 ,
|
| parameter channel_paralell_num = 16 ,
|
| parameter channel_all_num = 16 ,
|
| parameter bits_channel = 256 ,
|
|
|
| parameter length = 100 ,
|
| parameter length_2 = 7 ,
|
| parameter height = 252 ,
|
| parameter height_2 = 10 ,
|
| parameter filter_size = 5 ,
|
| parameter filter_size_2 = 3 ,
|
| parameter stride_height = 4 ,
|
| parameter stride_height_2 = 3 ,
|
| parameter stride_length = 4 ,
|
| parameter stride_length_2 = 3 ,
|
| parameter zero_pad = 0 ,
|
| parameter first_height = 157 ,
|
| parameter first_length = 1 ,
|
| parameter weight_num = 25 ,
|
| parameter weight_num_2 = 5 ,
|
| parameter conv_num = 4 ,
|
| parameter state_reset = 0 ,
|
| parameter state_output = 1
|
| )
|
| (
|
| input clk_in,
|
| input rst_n,
|
| input start,
|
| output reg [bits_channel-1:0] map,
|
| output reg ready,
|
| output reg [(conv_num<<bits_shift)-1:0] weight
|
| );
|
|
|
|
|
|
|
|
|
|
|
| reg state ;
|
| reg [length_2-1:0] cnt_length_start ;
|
| reg [filter_size_2-1:0] cnt_length_offset ;
|
| reg [height_2-1:0] cnt_height_first ;
|
| reg [height_2-1:0] cnt_height_start ;
|
| reg [filter_size_2-1:0] cnt_height_offset ;
|
|
|
| reg [weight_num_2-1:0] cnt_weight_output ;
|
| always@(posedge clk_in or negedge rst_n)
|
| begin
|
| if(~rst_n)
|
| begin
|
| state <= state_reset ;
|
| end
|
| else
|
| begin
|
| if(state == state_reset)
|
| begin
|
| cnt_length_start <= 0 ;
|
| cnt_length_offset <= 0 ;
|
| cnt_height_first <= 0 ;
|
| cnt_height_start <= 0 ;
|
| cnt_height_offset <= 0 ;
|
| cnt_weight_output <= 0 ;
|
| ready <= 1'b0 ;
|
| if(start)
|
| state <= state_output ;
|
| end
|
| else if(state == state_output)
|
| begin
|
| ready <= 1'b1 ;
|
|
|
|
|
|
|
| if(cnt_height_offset == filter_size-1)
|
| begin
|
| cnt_height_offset <= 0 ;
|
| if(cnt_length_offset == filter_size-1)
|
| begin
|
| cnt_length_offset <= 0 ;
|
| if(length - filter_size - cnt_length_start < stride_length)
|
| begin
|
| cnt_length_start <= 0 ;
|
| if(height - filter_size - cnt_height_start < stride_height)
|
| begin
|
| state <= state_reset ;
|
| end
|
| else
|
| begin
|
| cnt_height_first <= cnt_height_first + stride_height ;
|
| cnt_height_start <= cnt_height_first + stride_height ;
|
| end
|
| end
|
| else
|
| begin
|
| cnt_length_start <= cnt_length_start + stride_length ;
|
| end
|
| end
|
| else
|
| begin
|
| cnt_length_offset <= cnt_length_offset + 1'b1 ;
|
| end
|
| end
|
| else
|
| begin
|
| cnt_height_offset <= cnt_height_offset + 1'b1 ;
|
| end
|
|
|
|
|
|
|
| if(cnt_weight_output == weight_num-1)
|
| begin
|
| cnt_weight_output <= 0 ;
|
| end
|
| else
|
| begin
|
| cnt_weight_output <= cnt_weight_output + 1'b1 ;
|
| end
|
| end
|
| end
|
| end
|
|
|
|
|
| wire [14:0] addra [0:channel_paralell_num-1] ;
|
| wire [bits-1:0] douta [0:channel_all_num-1] ;
|
| genvar i,j,k;
|
| generate
|
| for(i = 0; i < channel_num; i = i + 1)
|
| begin:outdata_channel
|
| for(j = 0; j < channel_height_num; j = j + 1)
|
| begin:outdata_height
|
| for(k = 0; k < channel_length_num; k = k + 1)
|
| begin:outdata_length
|
| input_data Input_Data (
|
| .clka(clk_in),
|
| .ena(1'b1),
|
| .addra(addra[j*channel_length_num+k]),
|
| .douta(douta[j*channel_length_num+k])
|
| );
|
|
|
| always@(posedge clk_in)
|
| begin
|
| map[((j*channel_length_num+k)<<bits_shift)+bits-1:(j*channel_length_num+k<<bits_shift)] <= douta[j*channel_length_num+k] ;
|
| end
|
| assign addra[j*channel_length_num+k] = ((cnt_height_start+cnt_height_offset+j)<<6) +((cnt_height_start+cnt_height_offset+j)<<5)+((cnt_height_start+cnt_height_offset+j)<<2)+ cnt_length_start+cnt_length_offset + k ;
|
| end
|
| end
|
| end
|
| endgenerate
|
| wire [(conv_num<<bits_shift)-1:0] weight_temp ;
|
| weight1 Weight1 (
|
| .clka(clk_in),
|
| .addra(cnt_weight_output),
|
| .douta(weight_temp)
|
| );
|
|
|
| always@(posedge clk_in)
|
| begin
|
| weight <= weight_temp ;
|
| end
|
| endmodule
|
|
|