data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v
[Errno 2] No such file or directory: 'preprocess.output'
1: b'%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:213: Cannot find include file: ../../pipe/rtl/pipe_defs.v\n`include "../../pipe/rtl/pipe_defs.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/190755935/src/drivers/rtl,data/full_repos/permissive/190755935/../../pipe/rtl/pipe_defs.v\n             data/full_repos/permissive/190755935/src/drivers/rtl,data/full_repos/permissive/190755935/../../pipe/rtl/pipe_defs.v.v\n             data/full_repos/permissive/190755935/src/drivers/rtl,data/full_repos/permissive/190755935/../../pipe/rtl/pipe_defs.v.sv\n             ../../pipe/rtl/pipe_defs.v\n             ../../pipe/rtl/pipe_defs.v.v\n             ../../pipe/rtl/pipe_defs.v.sv\n             obj_dir/../../pipe/rtl/pipe_defs.v\n             obj_dir/../../pipe/rtl/pipe_defs.v.v\n             obj_dir/../../pipe/rtl/pipe_defs.v.sv\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:214: Cannot find include file: ../../image/rtl/image_defs.v\n`include "../../image/rtl/image_defs.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:217: Define or directive not defined: \'`IS_w\'\n        parameter [`IS_w-1:0] IS = `IS_CAMERA,\n                   ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:217: Define or directive not defined: \'`IS_CAMERA\'\n        parameter [`IS_w-1:0] IS = `IS_CAMERA,\n                                   ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:217: syntax error, unexpected \',\', expecting TYPE-IDENTIFIER\n        parameter [`IS_w-1:0] IS = `IS_CAMERA,\n                                             ^\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:220: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter CameraHeight = 482,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:222: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter ImageXInitial = 0,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:223: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter ImageYInitial = 0,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:225: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter CameraHorizontalBlanking = 600,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:226: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter CameraVerticalBlanking = 750,    \n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:228: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter BlankingWidth = 10,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:229: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter CoordinateWidth = 10,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:230: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter CameraPixelWidth = 10,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:232: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter I2CClockCount = 200,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:233: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter I2CGapCount = ( 1 << 8 )\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:234: syntax error, unexpected \')\', expecting \';\'\n    ) (\n    ^\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:257: Define or directive not defined: \'`I_w\'\n        inout [`I_w( IS )-1:0 ] image_out,\n               ^~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:282: Define or directive not defined: \'`IS_WIDTH\'\n    localparam ImageWidth  = `IS_WIDTH( IS );\n                             ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:283: Define or directive not defined: \'`IS_HEIGHT\'\n    localparam ImageHeight = `IS_HEIGHT( IS );\n                             ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:285: Define or directive not defined: \'`IS_WIDTH_WIDTH\'\n    localparam ImageWidthWidth  = `IS_WIDTH_WIDTH( IS );\n                                  ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:286: Define or directive not defined: \'`IS_HEIGHT_WIDTH\'\n    localparam ImageHeightWidth = `IS_HEIGHT_WIDTH( IS );\n                                  ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:287: Define or directive not defined: \'`IS_DATA_WIDTH\'\n    localparam ImageDataWidth   = `IS_DATA_WIDTH( IS );\n                                  ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:289: Define or directive not defined: \'`I_C0_w\'\n    localparam ImageC0Width = `I_C0_w( IS );\n                              ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:290: Define or directive not defined: \'`I_C1_w\'\n    localparam ImageC1Width = `I_C1_w( IS );\n                              ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:291: Define or directive not defined: \'`I_C2_w\'\n    localparam ImageC2Width = `I_C2_w( IS );\n                              ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:301: syntax error, unexpected assign\n    assign configuring = ci_configuring;\n    ^~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:343: syntax error, unexpected assign\n    assign debug = {  image_out_request, busy,  camera_start, start, camera_running, camera_idle, camera_configuring, camera_configure };\n    ^~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:424: syntax error, unexpected assign\n    assign `I_Start( IS, image_out ) = image_out_start;\n    ^~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:424: Define or directive not defined: \'`I_Start\'\n    assign `I_Start( IS, image_out ) = image_out_start;\n           ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:425: Define or directive not defined: \'`I_Stop\'\n    assign `I_Stop( IS, image_out )  = image_out_stop;\n           ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:426: Define or directive not defined: \'`I_Data\'\n    assign `I_Data( IS, image_out )  = image_out_data;\n           ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:427: Define or directive not defined: \'`I_Valid\'\n    assign `I_Valid( IS, image_out ) = image_out_valid;\n           ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:428: Define or directive not defined: \'`I_Error\'\n                                                                               : ... Suggested alternative: \'`error\'\n    assign `I_Error( IS, image_out ) = image_out_error;\n           ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:431: Define or directive not defined: \'`I_Request\'\n    assign image_out_request = `I_Request( IS, image_out );\n                               ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:432: Define or directive not defined: \'`I_Cancel\'\n    assign image_out_cancel  = `I_Cancel(  IS, image_out );\n                               ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:433: Define or directive not defined: \'`I_Ready\'\n    assign image_out_ready   = `I_Ready(   IS, image_out );\n                               ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/camera_image.v:457: syntax error, unexpected always\n    always @( posedge clock ) begin\n    ^~~~~~\n%Error: Exiting due to 37 error(s)\n'
module camera_image #(
        parameter [`IS_w-1:0] IS = `IS_CAMERA,

        parameter CameraWidth = 752,
        parameter CameraHeight = 482,

        parameter ImageXInitial = 0,
        parameter ImageYInitial = 0,

        parameter CameraHorizontalBlanking = 600,
        parameter CameraVerticalBlanking = 750,

        parameter BlankingWidth = 10,
        parameter CoordinateWidth = 10,
        parameter CameraPixelWidth = 10,

        parameter I2CClockCount = 200,
        parameter I2CGapCount = ( 1 << 8 )
    ) (
        input clock,
        input reset,


        input  configure,
        input  start,
        input  stop,


        output configuring,
        output error,
        output idle,
        output running,
        output busy,
        output image_transfer,


        input [CoordinateWidth-1:0] image_x,
        input [CoordinateWidth-1:0] image_y,
        input image_origin_update,


        inout [`I_w( IS )-1:0 ] image_out,
        input                   out_request_external,


        output scl_out,
        input  scl_in,
        output sda_out,
        input  sda_in,
        input  vs,
        input  hs,
        input  pclk,
        input  [CameraPixelWidth-1:0] d,
        output rst,
        output pwdn,
        input  led,
        output trigger,

        output [7:0] debug
    );






    localparam ImageWidth  = `IS_WIDTH( IS );
    localparam ImageHeight = `IS_HEIGHT( IS );

    localparam ImageWidthWidth  = `IS_WIDTH_WIDTH( IS );
    localparam ImageHeightWidth = `IS_HEIGHT_WIDTH( IS );
    localparam ImageDataWidth   = `IS_DATA_WIDTH( IS );

    localparam ImageC0Width = `I_C0_w( IS );
    localparam ImageC1Width = `I_C1_w( IS );
    localparam ImageC2Width = `I_C2_w( IS );





    reg ci_configuring;
    reg ci_image_transfer;
    reg ci_busy;

    assign configuring = ci_configuring;
    assign image_transfer = ci_image_transfer;
    assign busy = ci_busy || camera_busy;
    assign running = camera_running;
    assign idle = camera_idle;






    reg camera_configure;
    reg camera_start;
    reg camera_stop;


    wire camera_idle;
    wire camera_running;
    wire camera_configuring;
    wire camera_busy;

    reg [CoordinateWidth-1:0] column_start;
    reg [CoordinateWidth-1:0] row_start;
    reg [CoordinateWidth-1:0] window_width;
    reg [CoordinateWidth-1:0] window_height;
    reg set_window;
    reg set_origin;

    reg [BlankingWidth-1:0]   horizontal_blanking;
    reg [BlankingWidth-1:0]   vertical_blanking;
    reg set_blanking;

    reg snapshot_mode;
    reg set_snapshot_mode;
    reg snapshot;

    wire       out_vs;
    wire       out_hs;
    wire       out_valid;
    wire [CameraPixelWidth-1:0] out_d;


    assign debug = {  image_out_request, busy,  camera_start, start, camera_running, camera_idle, camera_configuring, camera_configure };

    camera_core #(
            .Width( CameraWidth ),
            .Height( CameraHeight ),
            .BlankingWidth( BlankingWidth ),
            .CameraPixelWidth( CameraPixelWidth ),
            .I2CClockCount( I2CClockCount ),
            .I2CGapCount( I2CGapCount )
        ) cam (
            .clock( clock ),
            .reset( reset ),


            .configure( camera_configure ),
            .start( camera_start ),
            .stop( camera_stop ),


            .configuring( camera_configuring ),
            .idle( camera_idle ),
            .running( camera_running ),
            .busy( camera_busy ),
            .error( error ),


            .column_start( column_start ),
            .row_start( row_start),
            .window_width( window_width ),
            .window_height( window_height ),
            .set_origin( set_origin ),
            .set_window( set_window ),


            .horizontal_blanking( horizontal_blanking ),
            .vertical_blanking( vertical_blanking ),
            .set_blanking( set_blanking ),


            .snapshot_mode( snapshot_mode ),
            .set_snapshot_mode( set_snapshot_mode ),

            .snapshot( snapshot ),


            .out_vs( out_vs ),
            .out_hs( out_hs ),
            .out_valid( out_valid ),
            .out_d( out_d ),


            .scl_in( scl_in ),
            .scl_out( scl_out ),
            .sda_in( sda_in ),
            .sda_out( sda_out ),
            .vs( vs ),
            .hs( hs ),
            .pclk( pclk ),
            .d( d ),
            .rst( rst ),
            .pwdn( pwdn ),
            .led( led ),
            .trigger( trigger )
        );






    reg                      image_out_start;
    reg                      image_out_stop;
    reg [ImageDataWidth-1:0] image_out_data;
    reg                      image_out_valid;
    reg                      image_out_error;

    wire                     image_out_request;
    wire                     image_out_cancel;
    wire                     image_out_ready;


    assign `I_Start( IS, image_out ) = image_out_start;
    assign `I_Stop( IS, image_out )  = image_out_stop;
    assign `I_Data( IS, image_out )  = image_out_data;
    assign `I_Valid( IS, image_out ) = image_out_valid;
    assign `I_Error( IS, image_out ) = image_out_error;


    assign image_out_request = `I_Request( IS, image_out );
    assign image_out_cancel  = `I_Cancel(  IS, image_out );
    assign image_out_ready   = `I_Ready(   IS, image_out );

    localparam CIM_STATE_POWERUP            = 0,
               CIM_STATE_CONFIGURING        = 1,
               CIM_STATE_WAIT_CONFIGURING   = 2,
               CIM_STATE_CONFIGURE_WINDOW   = 3,
               CIM_STATE_CONFIGURE_BLANKING = 4,
               CIM_STATE_RUNNABLE           = 5,
               CIM_STATE_FRAME_IDLE         = 6,
               CIM_STATE_FRAME_START        = 7,
               CIM_STATE_FRAME_DATA         = 8,
               CIM_STATE_FRAME_END          = 9;

    reg [3:0] cim_state;

    reg [ImageWidthWidth+ImageHeightWidth:0] image_pixel_counter;


    localparam CommandTimerWidth = 4;
    localparam CommandTimerShortCount = 10;

    reg [ CommandTimerWidth:0 ] command_timer;
    wire command_timer_expired = command_timer[ CommandTimerWidth ];

    always @( posedge clock ) begin
        if ( reset ) begin
            cim_state <= CIM_STATE_POWERUP;

            camera_start <= 0;
            camera_stop <= 0;

            command_timer <= -1;


            ci_configuring <= 0;
            ci_image_transfer <= 0;


            column_start <= 0;
            row_start <= 0;
            window_width <= 0;
            window_height <= 0;
            set_window <= 0;
            set_origin <= 0;
            horizontal_blanking <= 0;
            vertical_blanking <= 0;
            set_blanking <= 0;
            snapshot_mode <= 0;
            set_snapshot_mode <= 0;
            snapshot <= 0;


            image_out_start <= 0;
            image_out_stop <= 0;
            image_out_data <= 0;
            image_out_valid <= 0;
            image_out_error <= 0;

            image_pixel_counter <= 0;

        end else begin
            case ( cim_state )
                CIM_STATE_POWERUP: begin
                        if ( configure ) begin
                            camera_configure <= 1;
                            ci_busy <= 1;
                            cim_state <= CIM_STATE_WAIT_CONFIGURING;
                            ci_configuring <= 1;
                            command_timer <= CommandTimerShortCount;
                        end
                    end
                CIM_STATE_WAIT_CONFIGURING: begin
                        if ( command_timer_expired ) begin
                            if ( camera_configuring ) begin
                                cim_state <= CIM_STATE_CONFIGURING;
                                command_timer <= CommandTimerShortCount;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_CONFIGURING: begin
                        camera_configure <= 0;
                        if ( command_timer_expired ) begin
                            if ( camera_idle && ~camera_busy ) begin
                                column_start <= ImageXInitial;
                                row_start <= ImageYInitial;
                                window_width <= ImageWidth;
                                window_height <= ImageHeight;
                                set_window <= 1;
                                command_timer <= CommandTimerShortCount;
                                cim_state <= CIM_STATE_CONFIGURE_WINDOW;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_CONFIGURE_WINDOW: begin
                        set_window <= 0;
                        if ( command_timer_expired ) begin
                            column_start <= 0;
                            row_start <= 0;
                            window_width <= 0;
                            window_height <= 0;
                            if ( camera_idle && ~camera_busy ) begin
                                horizontal_blanking <= CameraHorizontalBlanking;
                                vertical_blanking <= CameraVerticalBlanking;
                                set_blanking <= 1;
                                command_timer <= CommandTimerShortCount;
                                cim_state <= CIM_STATE_CONFIGURE_BLANKING;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_CONFIGURE_BLANKING: begin
                        set_blanking <= 0;
                        horizontal_blanking <= 0;
                        vertical_blanking <= 0;
                        if ( command_timer_expired ) begin
                            if ( camera_idle && ~camera_busy ) begin
                                ci_configuring <= 0;
                                command_timer <= CommandTimerShortCount;
                                ci_busy <= 0;
                                cim_state <= CIM_STATE_RUNNABLE;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_RUNNABLE: begin


                        if ( start )
                            camera_start <= 1;
                        else
                            camera_start <= 0;
                        if ( stop )
                            camera_stop <= 1;
                        else
                            camera_stop <= 0;

                        if ( camera_running && ( out_request_external || image_out_request ) ) begin
                            ci_busy <= 1;
                            cim_state <= CIM_STATE_FRAME_IDLE;
                            image_pixel_counter <= 0;
                        end
                    end
                CIM_STATE_FRAME_IDLE: begin
                        if ( image_out_cancel ) begin
                            ci_busy <= 0;
                            cim_state <= CIM_STATE_RUNNABLE;
                        end else begin
                            if ( ~out_vs ) begin
                                cim_state <= CIM_STATE_FRAME_START;
                            end
                        end
                    end
                CIM_STATE_FRAME_START: begin
                        if ( image_out_cancel ) begin
                            ci_busy <= 0;
                            cim_state <= CIM_STATE_RUNNABLE;
                        end else begin
                            if ( out_vs ) begin
                                cim_state <= CIM_STATE_FRAME_DATA;
                            end
                        end
                    end
                CIM_STATE_FRAME_DATA: begin
                        if ( image_out_cancel ) begin
                            cim_state <= CIM_STATE_FRAME_END;
                        end else begin
                            if ( out_valid ) begin
                                image_out_valid <= 1;
                                image_out_data <= out_d;
                                image_out_start <= ( image_pixel_counter == 0 );
                                if ( image_pixel_counter == 0 )
                                    ci_image_transfer <= 1;
                                if ( ( image_pixel_counter == ( ( ImageWidth * ImageHeight ) - 1  ) ) ) begin
                                    image_out_stop <= 1;
                                    cim_state <= CIM_STATE_FRAME_END;
                                    ci_image_transfer <= 0;
                                end
                                image_pixel_counter <= image_pixel_counter + 1;


                            end else begin
                                image_out_valid <= 0;
                                image_out_data <= 0;
                                image_out_start <= 0;
                                image_out_stop <= 0;
                            end
                        end
                    end
                CIM_STATE_FRAME_END: begin
                        image_pixel_counter <= 0;
                        image_out_valid <= 0;
                        image_out_data <= 0;
                        image_out_start <= 0;
                        image_out_stop <= 0;
                        ci_busy <= 0;
                        if ( !out_vs ) begin
                            cim_state <= CIM_STATE_RUNNABLE;
                        end
                    end
            endcase
        end
    end

endmodule
module camera_image #(
        parameter [`IS_w-1:0] IS = `IS_CAMERA,

        parameter CameraWidth = 752,
        parameter CameraHeight = 482,

        parameter ImageXInitial = 0,
        parameter ImageYInitial = 0,

        parameter CameraHorizontalBlanking = 600,
        parameter CameraVerticalBlanking = 750,

        parameter BlankingWidth = 10,
        parameter CoordinateWidth = 10,
        parameter CameraPixelWidth = 10,

        parameter I2CClockCount = 200,
        parameter I2CGapCount = ( 1 << 8 )
    ) (
        input clock,
        input reset,


        input  configure,
        input  start,
        input  stop,


        output configuring,
        output error,
        output idle,
        output running,
        output busy,
        output image_transfer,


        input [CoordinateWidth-1:0] image_x,
        input [CoordinateWidth-1:0] image_y,
        input image_origin_update,


        inout [`I_w( IS )-1:0 ] image_out,
        input                   out_request_external,


        output scl_out,
        input  scl_in,
        output sda_out,
        input  sda_in,
        input  vs,
        input  hs,
        input  pclk,
        input  [CameraPixelWidth-1:0] d,
        output rst,
        output pwdn,
        input  led,
        output trigger,

        output [7:0] debug
    );







    localparam ImageWidth  = `IS_WIDTH( IS );
    localparam ImageHeight = `IS_HEIGHT( IS );

    localparam ImageWidthWidth  = `IS_WIDTH_WIDTH( IS );
    localparam ImageHeightWidth = `IS_HEIGHT_WIDTH( IS );
    localparam ImageDataWidth   = `IS_DATA_WIDTH( IS );

    localparam ImageC0Width = `I_C0_w( IS );
    localparam ImageC1Width = `I_C1_w( IS );
    localparam ImageC2Width = `I_C2_w( IS );





    reg ci_configuring;
    reg ci_image_transfer;
    reg ci_busy;

    assign configuring = ci_configuring;
    assign image_transfer = ci_image_transfer;
    assign busy = ci_busy || camera_busy;
    assign running = camera_running;
    assign idle = camera_idle;






    reg camera_configure;
    reg camera_start;
    reg camera_stop;


    wire camera_idle;
    wire camera_running;
    wire camera_configuring;
    wire camera_busy;

    reg [CoordinateWidth-1:0] column_start;
    reg [CoordinateWidth-1:0] row_start;
    reg [CoordinateWidth-1:0] window_width;
    reg [CoordinateWidth-1:0] window_height;
    reg set_window;
    reg set_origin;

    reg [BlankingWidth-1:0]   horizontal_blanking;
    reg [BlankingWidth-1:0]   vertical_blanking;
    reg set_blanking;

    reg snapshot_mode;
    reg set_snapshot_mode;
    reg snapshot;

    wire       out_vs;
    wire       out_hs;
    wire       out_valid;
    wire [CameraPixelWidth-1:0] out_d;


    assign debug = {  image_out_request, busy,  camera_start, start, camera_running, camera_idle, camera_configuring, camera_configure };

    camera_core #(
            .Width( CameraWidth ),
            .Height( CameraHeight ),
            .BlankingWidth( BlankingWidth ),
            .CameraPixelWidth( CameraPixelWidth ),
            .I2CClockCount( I2CClockCount ),
            .I2CGapCount( I2CGapCount )
        ) cam (
            .clock( clock ),
            .reset( reset ),


            .configure( camera_configure ),
            .start( camera_start ),
            .stop( camera_stop ),


            .configuring( camera_configuring ),
            .idle( camera_idle ),
            .running( camera_running ),
            .busy( camera_busy ),
            .error( error ),


            .column_start( column_start ),
            .row_start( row_start),
            .window_width( window_width ),
            .window_height( window_height ),
            .set_origin( set_origin ),
            .set_window( set_window ),


            .horizontal_blanking( horizontal_blanking ),
            .vertical_blanking( vertical_blanking ),
            .set_blanking( set_blanking ),


            .snapshot_mode( snapshot_mode ),
            .set_snapshot_mode( set_snapshot_mode ),

            .snapshot( snapshot ),


            .out_vs( out_vs ),
            .out_hs( out_hs ),
            .out_valid( out_valid ),
            .out_d( out_d ),


            .scl_in( scl_in ),
            .scl_out( scl_out ),
            .sda_in( sda_in ),
            .sda_out( sda_out ),
            .vs( vs ),
            .hs( hs ),
            .pclk( pclk ),
            .d( d ),
            .rst( rst ),
            .pwdn( pwdn ),
            .led( led ),
            .trigger( trigger )
        );






    reg                      image_out_start;
    reg                      image_out_stop;
    reg [ImageDataWidth-1:0] image_out_data;
    reg                      image_out_valid;
    reg                      image_out_error;

    wire                     image_out_request;
    wire                     image_out_cancel;
    wire                     image_out_ready;


    assign `I_Start( IS, image_out ) = image_out_start;
    assign `I_Stop( IS, image_out )  = image_out_stop;
    assign `I_Data( IS, image_out )  = image_out_data;
    assign `I_Valid( IS, image_out ) = image_out_valid;
    assign `I_Error( IS, image_out ) = image_out_error;


    assign image_out_request = `I_Request( IS, image_out );
    assign image_out_cancel  = `I_Cancel(  IS, image_out );
    assign image_out_ready   = `I_Ready(   IS, image_out );

    localparam CIM_STATE_POWERUP            = 0,
               CIM_STATE_CONFIGURING        = 1,
               CIM_STATE_WAIT_CONFIGURING   = 2,
               CIM_STATE_CONFIGURE_WINDOW   = 3,
               CIM_STATE_CONFIGURE_BLANKING = 4,
               CIM_STATE_RUNNABLE           = 5,
               CIM_STATE_FRAME_IDLE         = 6,
               CIM_STATE_FRAME_START        = 7,
               CIM_STATE_FRAME_DATA         = 8,
               CIM_STATE_FRAME_END          = 9;

    reg [3:0] cim_state;

    reg [ImageWidthWidth+ImageHeightWidth:0] image_pixel_counter;


    localparam CommandTimerWidth = 4;
    localparam CommandTimerShortCount = 10;

    reg [ CommandTimerWidth:0 ] command_timer;
    wire command_timer_expired = command_timer[ CommandTimerWidth ];

    always @( posedge clock ) begin
        if ( reset ) begin
            cim_state <= CIM_STATE_POWERUP;

            camera_start <= 0;
            camera_stop <= 0;

            command_timer <= -1;


            ci_configuring <= 0;
            ci_image_transfer <= 0;


            column_start <= 0;
            row_start <= 0;
            window_width <= 0;
            window_height <= 0;
            set_window <= 0;
            set_origin <= 0;
            horizontal_blanking <= 0;
            vertical_blanking <= 0;
            set_blanking <= 0;
            snapshot_mode <= 0;
            set_snapshot_mode <= 0;
            snapshot <= 0;


            image_out_start <= 0;
            image_out_stop <= 0;
            image_out_data <= 0;
            image_out_valid <= 0;
            image_out_error <= 0;

            image_pixel_counter <= 0;

        end else begin
            case ( cim_state )
                CIM_STATE_POWERUP: begin
                        if ( configure ) begin
                            camera_configure <= 1;
                            ci_busy <= 1;
                            cim_state <= CIM_STATE_WAIT_CONFIGURING;
                            ci_configuring <= 1;
                            command_timer <= CommandTimerShortCount;
                        end
                    end
                CIM_STATE_WAIT_CONFIGURING: begin
                        if ( command_timer_expired ) begin
                            if ( camera_configuring ) begin
                                cim_state <= CIM_STATE_CONFIGURING;
                                command_timer <= CommandTimerShortCount;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_CONFIGURING: begin
                        camera_configure <= 0;
                        if ( command_timer_expired ) begin
                            if ( camera_idle && ~camera_busy ) begin
                                column_start <= ImageXInitial;
                                row_start <= ImageYInitial;
                                window_width <= ImageWidth;
                                window_height <= ImageHeight;
                                set_window <= 1;
                                command_timer <= CommandTimerShortCount;
                                cim_state <= CIM_STATE_CONFIGURE_WINDOW;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_CONFIGURE_WINDOW: begin
                        set_window <= 0;
                        if ( command_timer_expired ) begin
                            column_start <= 0;
                            row_start <= 0;
                            window_width <= 0;
                            window_height <= 0;
                            if ( camera_idle && ~camera_busy ) begin
                                horizontal_blanking <= CameraHorizontalBlanking;
                                vertical_blanking <= CameraVerticalBlanking;
                                set_blanking <= 1;
                                command_timer <= CommandTimerShortCount;
                                cim_state <= CIM_STATE_CONFIGURE_BLANKING;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_CONFIGURE_BLANKING: begin
                        set_blanking <= 0;
                        horizontal_blanking <= 0;
                        vertical_blanking <= 0;
                        if ( command_timer_expired ) begin
                            if ( camera_idle && ~camera_busy ) begin
                                ci_configuring <= 0;
                                command_timer <= CommandTimerShortCount;
                                ci_busy <= 0;
                                cim_state <= CIM_STATE_RUNNABLE;
                            end
                        end else begin
                            command_timer <= command_timer - 1;
                        end
                    end
                CIM_STATE_RUNNABLE: begin


                        if ( start )
                            camera_start <= 1;
                        else
                            camera_start <= 0;
                        if ( stop )
                            camera_stop <= 1;
                        else
                            camera_stop <= 0;

                        if ( camera_running && ( out_request_external || image_out_request ) ) begin
                            ci_busy <= 1;
                            cim_state <= CIM_STATE_FRAME_IDLE;
                            image_pixel_counter <= 0;
                        end
                    end
                CIM_STATE_FRAME_IDLE: begin
                        if ( image_out_cancel ) begin
                            ci_busy <= 0;
                            cim_state <= CIM_STATE_RUNNABLE;
                        end else begin
                            if ( ~out_vs ) begin
                                cim_state <= CIM_STATE_FRAME_START;
                            end
                        end
                    end
                CIM_STATE_FRAME_START: begin
                        if ( image_out_cancel ) begin
                            ci_busy <= 0;
                            cim_state <= CIM_STATE_RUNNABLE;
                        end else begin
                            if ( out_vs ) begin
                                cim_state <= CIM_STATE_FRAME_DATA;
                            end
                        end
                    end
                CIM_STATE_FRAME_DATA: begin
                        if ( image_out_cancel ) begin
                            cim_state <= CIM_STATE_FRAME_END;
                        end else begin
                            if ( out_valid ) begin
                                image_out_valid <= 1;
                                image_out_data <= out_d;
                                image_out_start <= ( image_pixel_counter == 0 );
                                if ( image_pixel_counter == 0 )
                                    ci_image_transfer <= 1;
                                if ( ( image_pixel_counter == ( ( ImageWidth * ImageHeight ) - 1  ) ) ) begin
                                    image_out_stop <= 1;
                                    cim_state <= CIM_STATE_FRAME_END;
                                    ci_image_transfer <= 0;
                                end
                                image_pixel_counter <= image_pixel_counter + 1;


                            end else begin
                                image_out_valid <= 0;
                                image_out_data <= 0;
                                image_out_start <= 0;
                                image_out_stop <= 0;
                            end
                        end
                    end
                CIM_STATE_FRAME_END: begin
                        image_pixel_counter <= 0;
                        image_out_valid <= 0;
                        image_out_data <= 0;
                        image_out_start <= 0;
                        image_out_stop <= 0;
                        ci_busy <= 0;
                        if ( !out_vs ) begin
                            cim_state <= CIM_STATE_RUNNABLE;
                        end
                    end
            endcase
        end
    end

endmodule