| `timescale 1ns / 1ps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| module conv3_out_buffer#( |
| parameter bits = 16 , |
| parameter bits_shift = 4 , |
| parameter channel_num = 16 , |
| parameter length = 18 , |
| parameter length_2 = 5 , |
| parameter height = 57 , |
| parameter height_2 = 7 , |
| parameter filter_size = 2 , |
| parameter filter_size_2 = 2 ,
|
| parameter stride_in = 1 ,
|
| parameter stride_in_2 = 1 , |
| parameter stride = 2 , |
| parameter stride_2 = 2 , |
| parameter zero_pad = 0 , |
| parameter first_height = 40 , |
| parameter first_length = 1 , |
| parameter mem_length = 18 , |
| parameter mem_height = 3 , |
| parameter mem_length_2 = 5 , |
| parameter mem_height_2 = 2 , |
| parameter state_in_reset = 0 , |
| parameter state_in_wait = 1 , |
| parameter state_in_input = 2 , |
| parameter state_out_reset = 0 , |
| parameter state_out_wait = 1 , |
| parameter state_out_output = 2 |
| ) |
| ( |
| input clk_in , |
| input rst_n , |
| input start , |
| input [(channel_num<<bits_shift)-1:0] data_in , |
| output [(channel_num<<bits_shift)-1:0] data_out , |
| output reg ready |
| ); |
|
|
| |
| |
| |
| |
|
|
| reg [1:0] state_in ; |
| reg start_flag ; |
| reg start_output ; |
| reg [stride_in_2-1:0] cnt_start_output ; |
| reg start_out_first ; |
| reg [length_2-1:0] cnt_in_first_length ; |
| reg [height_2-1:0] cnt_in_depth_height ; |
| reg [height_2-1:0] cnt_in_offset_height ; |
| reg [length_2-1:0] cnt_in_offset_length ; |
| reg [mem_height_2-1:0] cnt_in_mem_height ; |
| reg [mem_length_2-1:0] cnt_in_mem_length ; |
| reg start_output_flag ; |
| always@(posedge clk_in or negedge rst_n) |
| begin |
| if(!rst_n) |
| begin |
| state_in <= state_in_reset ; |
| end |
| else |
| begin |
| if(state_in == state_in_reset) |
| begin |
| cnt_in_first_length <= first_length - 1 ; |
| cnt_in_depth_height <= first_height - 1 ; |
| cnt_in_offset_height <= 0 ; |
| cnt_in_offset_length <= first_length - 1 ; |
| cnt_in_mem_height <= 0 ; |
| cnt_in_mem_length <= 0 ; |
| start_out_first <= 1'b0 ; |
| start_output <= 1'b0 ; |
| state_in <= state_in_wait ; |
| cnt_start_output <= stride ; |
| end |
| else if(state_in == state_in_wait) |
| begin
|
| start_flag <= start ; |
| if(start&(~start_flag)) |
| begin |
| state_in <= state_in_input ; |
| end
|
| if(cnt_in_offset_height[0] == 1)
|
| start_out_first <= 1'b1 ;
|
| else
|
| start_out_first <= 1'b0 ;
|
|
|
| end |
| else if(state_in == state_in_input) |
| begin
|
| if(start_out_first&(cnt_in_mem_length==filter_size-1))
|
| start_output <= 1'b1 ;
|
| else if(start_out_first&(cnt_in_mem_length>filter_size-1))
|
| start_output <= !start_output ;
|
| else
|
| start_output <= 1'b0 ;
|
|
|
| if(cnt_in_mem_length > length - stride_in - 1'b1) |
| begin
|
| cnt_in_mem_length <= 0 ;
|
| if(cnt_in_offset_height > height - stride_in) |
| begin |
| cnt_in_mem_height <= 0 ;
|
| state_in <= state_in_reset ; |
| end |
| else |
| begin
|
| cnt_in_offset_height <= cnt_in_offset_height + stride_in;
|
| if(cnt_in_mem_height + stride_in < mem_height) |
| cnt_in_mem_height <= cnt_in_mem_height + stride_in ;
|
| else
|
| cnt_in_mem_height <= cnt_in_mem_height + stride_in - mem_height; |
| end |
| end |
| else |
| begin |
| cnt_in_mem_length <= cnt_in_mem_length + stride_in ;
|
| end |
| state_in <= state_in_wait ;
|
| end |
| else |
| begin |
| state_in <= state_in_reset ; |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| reg [1:0] state_out ; |
| reg [height_2-1:0] cnt_out_offset_height ; |
| reg [length_2-1:0] cnt_out_offset_length ; |
| reg [height_2-1:0] cnt_out_data_height ; |
| reg [length_2-1:0] cnt_out_data_length ; |
| reg [mem_height_2-1:0] cnt_out_mem_height ; |
| reg [mem_length_2-1:0] cnt_out_mem_length ; |
| |
| reg cnt_out_speed ; |
| |
| always@(posedge clk_in or negedge rst_n) |
| begin |
| if(!rst_n) |
| begin |
| state_out <= state_out_reset ; |
| end |
| else |
| begin |
| if(state_out == state_out_reset) |
| begin |
| cnt_out_offset_height <= 0 ; |
| cnt_out_offset_length <= 0 ; |
| cnt_out_data_height <= 0 ; |
| cnt_out_data_length <= 0 ; |
| cnt_out_mem_height <= 0 ; |
| cnt_out_mem_length <= first_length - 1 ; |
| state_out <= state_out_wait ; |
| ready <= 1'b0 ; |
| cnt_out_speed <= 1'b1 ; |
| end |
| else if(state_out == state_out_wait) |
| begin |
| start_output_flag <= start_output ; |
| if(start_output&(!start_output_flag)) |
| state_out <= state_out_output ; |
| end |
| else if(state_out == state_out_output) |
| begin |
| |
| |
| |
| |
| if(cnt_out_offset_height == 0 && cnt_out_offset_length == 0) |
| ready <= 1'b1 ; |
| else |
| ready <= 1'b0 ; |
| if(cnt_out_speed) |
| begin |
| if(cnt_out_offset_height == filter_size-1) |
| begin |
| cnt_out_offset_height <= 0 ; |
| if(cnt_out_offset_length == filter_size-1) |
| begin |
| cnt_out_offset_length <= 0 ;
|
| if(cnt_out_data_length > length - filter_size - stride)
|
| begin
|
| if(cnt_out_data_height > height - filter_size - stride)
|
| begin
|
| state_out <= state_out_reset ;
|
| end
|
| else
|
| begin |
| state_out <= state_out_wait ;
|
| if(cnt_out_data_height + stride < mem_height)
|
| begin
|
| cnt_out_data_height <= cnt_out_data_height + stride;
|
| cnt_out_mem_height <= cnt_out_data_height + stride;
|
| end
|
| else
|
| begin
|
| cnt_out_data_height <= cnt_out_data_height + stride - mem_height;
|
| cnt_out_mem_height <= cnt_out_data_height + stride - mem_height;
|
| end
|
| cnt_out_data_length <= 0 ;
|
| cnt_out_mem_length <= 0 ;
|
| end
|
| end
|
| else
|
| begin
|
| state_out <= state_out_wait ;
|
| cnt_out_mem_height <= cnt_out_data_height ;
|
| cnt_out_mem_length <= cnt_out_data_length + stride ;
|
| cnt_out_data_length <= cnt_out_data_length + stride ;
|
| end |
| end |
| else |
| begin |
| cnt_out_offset_length <= cnt_out_offset_length + 1'b1 ;
|
| cnt_out_mem_height <= cnt_out_data_height ;
|
| cnt_out_mem_length <= cnt_out_mem_length + 1'b1 ;
|
| end |
| end |
| else |
| begin |
| cnt_out_offset_height <= cnt_out_offset_height + 1'b1 ;
|
| if(cnt_out_mem_height == mem_height-1) |
| cnt_out_mem_height <= 0 ;
|
| else |
| cnt_out_mem_height <= cnt_out_mem_height + 1'b1 ; |
| end |
| end |
| end |
| else |
| begin |
| state_out <= state_out_reset ; |
| end |
| end |
| end |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| wire [8:0] addra ; |
| assign addra = ((cnt_in_mem_height<<4)+(cnt_in_mem_height<<1))+cnt_in_mem_length ; |
| wire [8:0] addrb ; |
| wire [64:0] data_out_temp[0:3] ; |
| assign addrb = ((cnt_out_mem_height<<4)+(cnt_out_mem_height<<1))+cnt_out_mem_length ; |
|
|
| l3cb1 L3CB1 ( |
| .clka(clk_in), |
| .wea(start&(!start_flag)), |
| .addra(addra), |
| .dina(data_in), |
| .clkb(clk_in), |
| .addrb(addrb), |
| .doutb(data_out) |
| ); |
| endmodule |
|
|