module oc0ta1b_controller (
    input clk,                              // 24Mhz clock signal
    input rstn,
    output config_finished,                 // Flag to indicate that the configuration is finished
    output sioc,                            // SCCB interface - clock signal
    output siod,                             // SCCB interface - data signal
    output siod_ioen                       // SCC interface - data output enable signal (active low)

    // Overwrite signals - 
    // Used to send data after configuration is finished
    ,input ovw_send,                         // initiate an overwrite transaction
    input [15:0] ovw_reg_addr,              // 16-bit register location
    input [7:0]  ovw_reg_value,             // value to set register
    output ovw_busy                         // overwrite transaction in progress
);

//=============================================================
    // Internal signals
    wire [23:0] command;
    wire finished;
    wire taken;
    wire waitnull;
    
    reg send;
    
    reg [31:0] cnt;
    
    reg resend;// = initial reset, high active

    wire i2c_if_busy;

//=================================================================   
    // Signal for testing
    assign config_finished = finished;
    assign ovw_busy = finished & i2c_if_busy;
//=====================================================================    


	always @ (posedge clk or negedge rstn)
    begin
    	if(!rstn)
    		cnt <= 32'd0;
    	else if(cnt != 32'hffff_ffff)
    		cnt <= cnt + 1'b1;
    end

    
    //resend > (5+1+20)ms
    always @ (posedge clk or negedge rstn)
    begin
    	if(!rstn)
    		resend <= 1'b1;
`ifdef SIM
    	else if(cnt >= 32'd16_000)//32us
    		resend <= 1'b0;
    	else
    		resend <= resend;
`else
    	else if(cnt >= 32'd16_000_000)//32ms
    		resend <= 1'b0;
    	else
    		resend <= resend;
`endif
    end
   
//=======================================================================    
    // Signal to indicate that the configuration is finshied    
    // always @ (finished) begin
    //     send = ~finished;
    // end
    
    always @(*) begin
        if(~finished)
            send = 1'b1; // as long as configuration isn't finished, send stays high
        else begin
            send = ovw_send;
        end
    end

    // Create an instance of a LUT table 
    oc0ta1b_registers Regs(
        .clk(clk),                          // 50Mhz clock signal
        .taken(taken),                      // Flag to advance to take next register
        .waitnull(waitnull),
        .command(command),                  // register value and data for OV5647
        .finished(finished),                // Flag to indicate the configuration is finshed
        .resend(resend)                     // Re-configure flag for OV5647
    );
    
    // Create an instance of a SCCB interface
    I2C_Interface #(
        .SID (8'hC0) 
    ) I2C (
        .resend(resend),
        .clk(clk),                          // 50Mhz clock signal
        .taken(taken),                      // Flag to advance to next register
        .siod(siod),                        // Clock signal for SCCB interface
        .sioc(sioc),                        // Data signal for SCCB interface 
        .siod_ioen(siod_ioen),
        .send(send),                        // Continue to configure OV5647
        .waitnull(waitnull),
        .regah(finished ? ovw_reg_addr[15:8] : command[23:16]),             // Register address  high 8 bit
        .regal(finished ? ovw_reg_addr[7:0]  : command[15:8]),              // Register address  low 8 bit
        .value(finished ? ovw_reg_value : command[7:0]),                // Data to write into register
        .busy(i2c_if_busy)
    );
    
endmodule