`timescale 1ns / 1ps
module single_fish_top(
input sys_clk_i ,
//input inclk , //share input
//input csi , //share input
//input sck , //share input
//input sdi , //share input
//his interface
output his_sdo , //输出到海思的SDO;
output his_hsync , //输出的HSYNC到海思
output his_vsync , //输出的VSYNC到海思
output [11:0] his_v_data , //视频数据
output his_dclk , //输出到海思的数据时钟
//sensor 1 interface
input sen_1_sdo , //输出到海思的SDO;
input sen_1_hsync , //输出的HSYNC到海思
input sen_1_vsync , //输出的VSYNC到海思
input [11:0] sen_1_v_data , //视频数据
input sen_1_dclk , //输出到海思的数据时钟
//sram interface
output [19:0] sram_address_1 , //
output reg [19:0] sram_address_2 , //
inout [11:0] sram_io_1 , //SRAM1 U6数据
inout [15:0] sram_io_2 , //SRAM2 U7数据
output we_1 , //SRAM1 U6 we
output reg we_2 , //SRAM2 U7 we
output oe_1 , //SRAM1 U6 oe
output reg oe_2 , //SRAM2 U7 oe
//SERIAL FLASH INTERFACE
output sf_sck ,
output sf_cs ,
output sf_sdi ,
input sf_sdo ,
output [15:0] dummy_out
);
wire sen_1_dclk_ibufg ;
wire pll_sen_locked ;
wire pll_sys_locked ;
wire clk_66m ;
wire clk_66m_pin_out ;
wire [11:0] his_gen_vdata_out ;
wire his_gen_vsync_out ;
wire his_gen_hsync_out ;
wire power_up_rst ;
wire dummy_hsync , dummy_vsync ;
wire [11:0] dummy_vdata ;
wire sen_1_wren_0 , sen_1_wren_1 , sen_1_valid_data_period ;
wire sen_1_hsync_fil , sen_1_vsync_fil ;
wire [15:0] valid_dot_cnt_sen_1 ;
wire [2:0] push_static_sen_1 ;
wire [2:0] push_static_sen_2 ;
wire line_valid_static_error_1 , line_valid_static_error_2 ;
wire [15:0] dot_cnt_1 ;
wire [15:0] dot_cnt_2 ;
wire [23:0] dot_cnt_total_1 ;
wire [23:0] dot_cnt_total_2 ;
wire [2 :0] image_save_index_1 , image_save_index_2 ;
wire read_request_1 , read_request_2 ;
wire spi_init_done ;
wire read_request_image ;
wire [29:0] read_request_address_image ;
reg [11:0] sen_1_v_data_d ;
wire [15:0] sram_wdata_2 ;
reg [15:0] sram_wdata_2_d ;
wire [19:0] sram_waddr_2 ;
wire [19:0] sram_raddr_2 ;
wire sram_wren_2 ;
wire sram_rden_2 ;
wire vsync_in_his_out ;
wire hsync_in_his_out ;
wire [11:0] video_data_in_his_out ;
assign sram_io_2 = spi_init_done ? 16'bzzzzzzzzzzzzzzzz : sram_wdata_2_d ;
always @(posedge clk_66m)
begin
sram_wdata_2_d <= sram_wdata_2 ;
we_2 <= spi_init_done ? 1'b1 : sram_wren_2 ;
oe_2 <= spi_init_done ? sram_rden_2 : 1'b1 ;
sram_address_2 <= spi_init_done ? sram_raddr_2 : sram_waddr_2 ;
end
wire his_clk_source_org ;
wire his_clk_source ;
BUFGCE BUFGCE_inst (
.O(his_clk_source), // 1-bit output: Clock buffer output
.CE(1'b1), // 1-bit input: Clock buffer select
.I(his_clk_source_org) // 1-bit input: Clock buffer input (S=0)
);
IBUFG BUFG_inst_sen_dclk (.O(sen_1_dclk_ibufg), .I(sen_1_dclk));
pll_sen_clk pll_sen_clk(.clk_in(sen_1_dclk_ibufg) , .clk_out(his_clk_source_org), .pll_locked(pll_sen_locked));
assign sen_1_dclk_buf = sen_1_dclk_ibufg ;
pll_sys_clk pll_sys_clk(.clk_in(sys_clk_i) , .clk_out(clk_66m), .pll_locked(pll_sys_locked), .clk_66m_pin_out(clk_66m_pin_out));
always @(posedge sen_1_dclk_buf)
sen_1_v_data_d <= sen_1_v_data ;
rst_ctrl RST_CTRL(
.rst_i(1'b1),
.isp_rst(1'b1) ,
.clk(clk_66m),
.power_up_rst(power_up_rst) ,
.rst_o()
);
filter filter_sen_1_hsync(.clk(sen_1_dclk_buf), .pin_in(sen_1_hsync), .filtered_out(sen_1_hsync_fil));
filter filter_sen_1_vsync(.clk(sen_1_dclk_buf), .pin_in(sen_1_vsync), .filtered_out(sen_1_vsync_fil));
spi_flash_mover spi_flash_mover(
.clk_sys (clk_66m) ,
.rst_n (power_up_rst) ,
.spi_cs (sf_cs) ,
.spi_clk (sf_sck) ,
.spi_di (sf_sdi) ,
.spi_do (sf_sdo) ,
.sram_wdata_2 (sram_wdata_2) ,
.sram_waddr_2 (sram_waddr_2) ,
.sram_wren_2 (sram_wren_2) ,
.spi_init_done (spi_init_done)
);
sensor_proc sensor_proc_sen1(
.sen_sdo (), //输出到海思的SDO;
.sen_hsync (sen_1_hsync_fil ), //输出的HSYNC到海思
.sen_vsync (sen_1_vsync_fil ), //输出的VSYNC到海思
.sen_v_data (sen_1_v_data_d), //视频数据
.sen_dclk (sen_1_dclk_buf ), //输出到海思的数据时钟
.valid_data_period (sen_1_valid_data_period),
.wren_0 (sen_1_wren_0),
.wren_1 (sen_1_wren_1),
.valid_dot_cnt (valid_dot_cnt_sen_1),
.line_valid_static_error (line_valid_static_error_1),
.dot_cnt (dot_cnt_1),
.dot_cnt_total (dot_cnt_total_1),
.spi_init_done (spi_init_done)
);
sram_controller sram_controller(
.sen_1_hsync (sen_1_hsync_fil ),
.sen_1_vsync (sen_1_vsync_fil ),
.sen_1_v_data (sen_1_v_data) ,
.sen_1_dclk (sen_1_dclk_buf ),
.valid_data_period_sen_1 (sen_1_valid_data_period),
.valid_dot_cnt_sen_1 (valid_dot_cnt_sen_1),
.spi_init_done_sen_1 (spi_init_done),
.clk_sys (clk_66m) ,
.rst_n (power_up_rst) ,
.sram_addr (sram_address_1) ,
.sram_io_1 (sram_io_1) ,
.sram_wen_1 (we_1) ,
.sram_oen_1 (oe_1) ,
.sram_rden_2 (sram_rden_2) ,
.sram_raddr_2 (sram_raddr_2) ,
.sram_din_2 (sram_io_2) ,
.vsync_out (vsync_in_his_out ) ,
.hsync_out (hsync_in_his_out ) ,
.video_data_out (video_data_in_his_out) ,
.dummy_out (dummy_out)
);
reg vsync_in_d ;
reg hsync_in_d ;
reg [11:0] video_data_in_d ;
reg [15:0] sram_din_2_d ;
always @(posedge clk_66m)
sram_din_2_d <= sram_io_2 ;
//assign dummy_out = sram_io_2 ; //|sram_din_2_d ;
always @(posedge sen_1_dclk_buf)
begin
vsync_in_d <= sen_1_vsync ;
hsync_in_d <= sen_1_hsync ;
video_data_in_d <= sen_1_v_data ;
end
assign his_sdo = sen_1_sdo ;
assign his_dclk = his_clk_source ; //clk_66m_pin_out ; //sen_1_dclk_buf ;
//assign his_hsync = sen_1_hsync ;
//assign his_vsync = sen_1_vsync ;
//assign his_v_data = sen_1_v_data ;
assign his_hsync = hsync_in_his_out ;
assign his_vsync = vsync_in_his_out ;
assign his_v_data = video_data_in_his_out ;
endmodule
module sensor_proc(
sen_sdo , //输出到海思的SDO;
sen_hsync , //输出的HSYNC到海思
sen_vsync , //输出的VSYNC到海思
sen_v_data , //视频数据
sen_dclk, //输出到海思的数据时钟
valid_data_period ,
wren_0 ,
wren_1 ,
valid_dot_cnt ,
line_valid_static_error ,
dot_cnt ,
dot_cnt_total ,
spi_init_done
);
input sen_sdo ; //输出到海思的SDO;
input sen_hsync ; //输出的HSYNC到海思
input sen_vsync ; //输出的VSYNC到海思
input [11:0] sen_v_data ; //视频数据
input sen_dclk ; //输出到海思的数据时钟
output valid_data_period ;
output wren_0 , wren_1 ;
output [15:0] valid_dot_cnt ;
output line_valid_static_error ;
output [15:0] dot_cnt ;
output [23:0] dot_cnt_total ;
input spi_init_done ;
parameter INEFFECTIVE_OB_LEN = 16 ;
parameter IGNORED_AREA_LEN = 24 ;
parameter COLOR_PROCESS_LEH = 8 ;
parameter IMAGE_WIDTH = 1920 ;
parameter FRONT_INVALID_LEN = INEFFECTIVE_OB_LEN + IGNORED_AREA_LEN + COLOR_PROCESS_LEH ;
reg [11:0] sen_v_data_d1 ;
reg [11:0] sen_v_data_d2 ;
reg [11:0] sen_v_data_d3 ;
reg [11:0] sen_v_data_d4 ;
reg [11:0] sen_v_data_d5 ;
reg [11:0] sen_v_data_d6 ;
reg [11:0] sen_v_data_d7 ;
reg active_data_period ; //SAV和EAV之间的区域
reg valid_data_period ; //真正有效的数据区域,不包括前后的INEFFECTIVE区域
reg [15:0] line_cnt ;
reg [15:0] dot_cnt ;
reg [15:0] valid_dot_cnt ;
reg sensor_valid_frame ; //表示一帧中开始有数据了
reg sen_hsync_d1 , sen_hsync_d2 ;
reg sen_vsync_d1 , sen_vsync_d2 ;
wire hsync_r = sen_hsync_d1 & (~sen_hsync_d2) ;
wire hsync_f = sen_hsync_d2 & (~sen_hsync_d1) ;
wire vsync_r = sen_vsync_d1 & (~sen_vsync_d2) ;
wire vsync_f = sen_vsync_d2 & (~sen_vsync_d1) ;
reg wren_0 , wren_1 ;
reg active_data_period_d ;
wire active_data_period_r = active_data_period & (~active_data_period_d) ;
reg [15:0] active_line_cnt ; //统计SAV有效的行数计数器。
reg [7:0] sen_hsync_cnt ;
reg line_valid_static_error ;
reg [23:0] dot_cnt_total ;
always @(posedge sen_dclk)
line_valid_static_error <= (sen_hsync_cnt == 20) & (dot_cnt > 100) & (dot_cnt != 2004) ;
always @(posedge sen_dclk)
if (~sen_hsync)
sen_hsync_cnt <= 16'd0 ;
else if (sen_hsync_cnt != 150)
sen_hsync_cnt <= sen_hsync_cnt + 16'd1 ;
always @(posedge sen_dclk)
if (sen_hsync_cnt == 8'd60)
valid_dot_cnt <= 16'd0 ;
else if (valid_data_period)
begin
if (valid_dot_cnt != (IMAGE_WIDTH - 1))
valid_dot_cnt <= valid_dot_cnt + 16'd1 ;
end
always @(posedge sen_dclk)
if ((~sen_vsync)|(sen_hsync_cnt == 8'd60))
wren_0 <= 1'b0 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1))
wren_0 <= 1'b1 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 480))
wren_0 <= 1'b0 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 960))
wren_0 <= 1'b1 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 1440))
wren_0 <= 1'b0 ;
always @(posedge sen_dclk)
if ((~sen_vsync)|(sen_hsync_cnt == 8'd60))
wren_1 <= 1'b0 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 480))
wren_1 <= 1'b1 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 960))
wren_1 <= 1'b0 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 1440))
wren_1 <= 1'b1 ;
else if (dot_cnt == (FRONT_INVALID_LEN - 1 + 1920))
wren_1 <= 1'b0 ;
always @(posedge sen_dclk)
if ((~sen_vsync)|(sen_hsync_cnt == 8'd60)|(~spi_init_done))
valid_data_period <= 1'b0 ;
else if ((dot_cnt == (FRONT_INVALID_LEN - 1))&(active_line_cnt > 16'd25)&(active_line_cnt <= 16'd25 + 1080) & spi_init_done)
valid_data_period <= 1'b1 ;
else if(valid_dot_cnt == (IMAGE_WIDTH - 1))
valid_data_period <= 1'b0 ;
always @(posedge sen_dclk)
begin
sen_vsync_d2 <= sen_vsync_d1 ;
sen_vsync_d1 <= sen_vsync ;
sen_hsync_d2 <= sen_hsync_d1 ;
sen_hsync_d1 <= sen_hsync ;
end
always @(posedge sen_dclk)
if (~sen_vsync)
line_cnt <= 16'd0 ;
else if (sensor_valid_frame & hsync_f)
line_cnt <= line_cnt + 16'd1 ;
always @(posedge sen_dclk)
if (~sen_vsync)
sensor_valid_frame <= 1'b0 ;
else if (active_data_period)
sensor_valid_frame <= 1'b1 ;
always @(posedge sen_dclk)
if(~sen_vsync_d2)
dot_cnt_total <= 24'd0 ;
else if(sen_hsync_cnt == 8'd60)
dot_cnt_total <= dot_cnt_total + dot_cnt ;
always @(posedge sen_dclk)
if(sen_hsync_cnt == 8'd60)
dot_cnt <= 16'd0 ;
else if (active_data_period)
dot_cnt <= dot_cnt + 16'd1 ;
wire [3:0] sen_v_data_d7_sum = sen_v_data_d7[11]+sen_v_data_d7[10]+sen_v_data_d7[9]+sen_v_data_d7[8]+sen_v_data_d7[7]+sen_v_data_d7[6]+sen_v_data_d7[5]+sen_v_data_d7[4]+sen_v_data_d7[3]+sen_v_data_d7[2]+sen_v_data_d7[1]+sen_v_data_d7[0];
wire [3:0] sen_v_data_d6_sum = sen_v_data_d6[11]+sen_v_data_d6[10]+sen_v_data_d6[9]+sen_v_data_d6[8]+sen_v_data_d6[7]+sen_v_data_d6[6]+sen_v_data_d6[5]+sen_v_data_d6[4]+sen_v_data_d6[3]+sen_v_data_d6[2]+sen_v_data_d6[1]+sen_v_data_d6[0];
wire [3:0] sen_v_data_d5_sum = sen_v_data_d5[11]+sen_v_data_d5[10]+sen_v_data_d5[9]+sen_v_data_d5[8]+sen_v_data_d5[7]+sen_v_data_d5[6]+sen_v_data_d5[5]+sen_v_data_d5[4]+sen_v_data_d5[3]+sen_v_data_d5[2]+sen_v_data_d5[1]+sen_v_data_d5[0];
wire [3:0] sen_v_data_d4_sum = sen_v_data_d4[11]+sen_v_data_d4[10]+sen_v_data_d4[9]+sen_v_data_d4[8]+sen_v_data_d4[7]+sen_v_data_d4[6]+sen_v_data_d4[5]+sen_v_data_d4[4]+sen_v_data_d4[3]+sen_v_data_d4[2]+sen_v_data_d4[1]+sen_v_data_d4[0];
wire [3:0] sen_v_data_d3_sum = sen_v_data_d3[11]+sen_v_data_d3[10]+sen_v_data_d3[9]+sen_v_data_d3[8]+sen_v_data_d3[7]+sen_v_data_d3[6]+sen_v_data_d3[5]+sen_v_data_d3[4]+sen_v_data_d3[3]+sen_v_data_d3[2]+sen_v_data_d3[1]+sen_v_data_d3[0];
wire [3:0] sen_v_data_d2_sum = sen_v_data_d2[11]+sen_v_data_d2[10]+sen_v_data_d2[9]+sen_v_data_d2[8]+sen_v_data_d2[7]+sen_v_data_d2[6]+sen_v_data_d2[5]+sen_v_data_d2[4]+sen_v_data_d2[3]+sen_v_data_d2[2]+sen_v_data_d2[1]+sen_v_data_d2[0];
wire [3:0] sen_v_data_d1_sum = sen_v_data_d1[11]+sen_v_data_d1[10]+sen_v_data_d1[9]+sen_v_data_d1[8]+sen_v_data_d1[7]+sen_v_data_d1[6]+sen_v_data_d1[5]+sen_v_data_d1[4]+sen_v_data_d1[3]+sen_v_data_d1[2]+sen_v_data_d1[1]+sen_v_data_d1[0];
wire [3:0] sen_v_data_sum = sen_v_data[11]+sen_v_data[10]+sen_v_data[9]+sen_v_data[8]+sen_v_data[7]+sen_v_data[6]+sen_v_data[5]+sen_v_data[4]+sen_v_data[3]+sen_v_data[2]+sen_v_data[1]+sen_v_data[0];
always @(posedge sen_dclk)
if (~sen_vsync)
active_data_period <= 1'b0 ;
//else if ((sen_v_data == 12'h800) & (sen_v_data_d1 == 12'h000) & (sen_v_data_d2 == 12'h000) & (sen_v_data_d3 == 12'hfff))
else if ((sen_v_data_sum <= 4'd2) & (sen_v_data_d1_sum <= 4'd1) & (sen_v_data_d2_sum <= 4'd1) & (sen_v_data_d3_sum >= 4'd10) & (sen_v_data_d4_sum <= 4'd1) & (sen_v_data_d5_sum <= 4'd1))
active_data_period <= 1'b1 ;
//else if ((sen_v_data == 12'h9d0) & (sen_v_data_d1 == 12'h000) & (sen_v_data_d2 == 12'h000) & (sen_v_data_d3 == 12'hfff))
else if ((sen_v_data_d1_sum <= 4'd1) & (sen_v_data_d2_sum <= 4'd1) & (sen_v_data_d3_sum >= 4'd10))
active_data_period <= 1'b0 ;
always @(posedge sen_dclk)
active_data_period_d <= active_data_period ;
always @(posedge sen_dclk)
if (~sen_vsync)
active_line_cnt <= 16'd0 ;
else if (active_data_period_r)
active_line_cnt <= active_line_cnt + 16'd1 ;
always @(posedge sen_dclk)
begin
sen_v_data_d7 <= sen_v_data_d6 ;
sen_v_data_d6 <= sen_v_data_d5 ;
sen_v_data_d5 <= sen_v_data_d4 ;
sen_v_data_d4 <= sen_v_data_d3 ;
sen_v_data_d3 <= sen_v_data_d2 ;
sen_v_data_d2 <= sen_v_data_d1 ;
sen_v_data_d1 <= sen_v_data ;
end
endmodule
|
|