data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v
None: at end of input
1: b'%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:15: Cannot find include file: ../../utils/sim/sim.v\n`include "../../utils/sim/sim.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/190755935/src/drivers/sim,data/full_repos/permissive/190755935/../../utils/sim/sim.v\n             data/full_repos/permissive/190755935/src/drivers/sim,data/full_repos/permissive/190755935/../../utils/sim/sim.v.v\n             data/full_repos/permissive/190755935/src/drivers/sim,data/full_repos/permissive/190755935/../../utils/sim/sim.v.sv\n             ../../utils/sim/sim.v\n             ../../utils/sim/sim.v.v\n             ../../utils/sim/sim.v.sv\n             obj_dir/../../utils/sim/sim.v\n             obj_dir/../../utils/sim/sim.v.v\n             obj_dir/../../utils/sim/sim.v.sv\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:17: Cannot find include file: ../../pipe/rtl/pipe_defs.v\n`include "../../pipe/rtl/pipe_defs.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:19: 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/sim/camera_image_tb.v:23: Define or directive not defined: \'`OutputDebug\'\n    parameter Output=`OutputDebug;\n                     ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:23: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n    parameter Output=`OutputDebug;\n                                 ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:28: Unsupported or unknown PLI call: $dumpfile\n        $dumpfile("camera_image_tb.vcd");\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:29: Unsupported or unknown PLI call: $dumpvars\n        $dumpvars( 1, camera_image_tb );\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:30: Unsupported or unknown PLI call: $dumpvars\n        $dumpvars( 1, camera_image_tb.cam_image );\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:31: Unsupported or unknown PLI call: $dumpvars\n        $dumpvars( 1, camera_image_tb.cam_image.cam );\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:32: Unsupported or unknown PLI call: $dumpvars\n        $dumpvars( 1, camera_image_tb.buffer );\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:33: Unsupported or unknown PLI call: $dumpvars\n        $dumpvars( 1, camera_image_tb.cam_proxy );\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:34: Unsupported or unknown PLI call: $dumpvars\n        $dumpvars( 0, camera_image_tb.cam_proxy.i2c_s );\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:38: Define or directive not defined: \'`Realtime\'\n    `Realtime\n    ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:41: Define or directive not defined: \'`Clock10MHz\'\n    `Clock10MHz\n    ^~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:43: Define or directive not defined: \'`AssertSetup\'\n    `AssertSetup\n    ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:110: Define or directive not defined: \'`IS\'\n    localparam IS =  `IS( 0, 0, CameraWidth - 1, CameraHeight - 4, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                     ^~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:110: syntax error, unexpected \',\'\n    localparam IS =  `IS( 0, 0, CameraWidth - 1, CameraHeight - 4, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                           ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:110: Define or directive not defined: \'`IS_FORMAT_GRAYSCALE\'\n    localparam IS =  `IS( 0, 0, CameraWidth - 1, CameraHeight - 4, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                                                                         ^~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:112: Define or directive not defined: \'`IS_WIDTH\'\n    localparam ImageWidth = `IS_WIDTH( IS );\n                            ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:113: Define or directive not defined: \'`IS_HEIGHT\'\n    localparam ImageHeight = `IS_HEIGHT( IS );\n                             ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:115: Define or directive not defined: \'`IS_PIXEL_COUNT\'\n    localparam ImagePixelCount = `IS_PIXEL_COUNT( IS );\n                                 ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:117: 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/sim/camera_image_tb.v:118: 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/sim/camera_image_tb.v:119: 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/sim/camera_image_tb.v:121: Define or directive not defined: \'`IS_C0_WIDTH\'\n    localparam ImageC0Width =     `IS_C0_WIDTH( IS );\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:122: Define or directive not defined: \'`IS_C1_WIDTH\'\n    localparam ImageC1Width =     `IS_C1_WIDTH( IS );\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:123: Define or directive not defined: \'`IS_C2_WIDTH\'\n    localparam ImageC2Width =     `IS_C2_WIDTH( IS );\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:124: Define or directive not defined: \'`IS_ALPHA_WIDTH\'\n    localparam ImageAlphaWidth =  `IS_ALPHA_WIDTH( IS );\n                                  ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:125: Define or directive not defined: \'`IS_Z_WIDTH\'\n    localparam ImageZWidth =      `IS_Z_WIDTH( IS );\n                                  ^~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:127: Define or directive not defined: \'`I_w\'\n    wire [`I_w( IS )-1:0 ] image_out;\n          ^~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:141: Define or directive not defined: \'`I_Start\'\n    assign image_out_start = `I_Start( IS, image_out );\n                             ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:141: syntax error, unexpected \',\'\n    assign image_out_start = `I_Start( IS, image_out );\n                                         ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:142: Define or directive not defined: \'`I_Stop\'\n    assign image_out_stop = `I_Stop( IS, image_out );\n                            ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:142: syntax error, unexpected \',\'\n    assign image_out_stop = `I_Stop( IS, image_out );\n                                       ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:143: Define or directive not defined: \'`I_Data\'\n    assign image_out_data = `I_Data( IS, image_out );\n                            ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:143: syntax error, unexpected \',\'\n    assign image_out_data = `I_Data( IS, image_out );\n                                       ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:144: Define or directive not defined: \'`I_Error\'\n                                                                                  : ... Suggested alternative: \'`error\'\n    assign image_out_error = `I_Error( IS, image_out );\n                             ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:144: syntax error, unexpected \',\'\n    assign image_out_error = `I_Error( IS, image_out );\n                                         ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:145: Define or directive not defined: \'`I_Valid\'\n    assign image_out_valid = `I_Valid( IS, image_out );\n                             ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:145: syntax error, unexpected \',\'\n    assign image_out_valid = `I_Valid( IS, image_out );\n                                         ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:147: Define or directive not defined: \'`I_Valid\'\n    assign image_out_request = `I_Valid( IS, image_out );\n                               ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:147: syntax error, unexpected \',\'\n    assign image_out_request = `I_Valid( IS, image_out );\n                                           ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:148: 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/sim/camera_image_tb.v:148: syntax error, unexpected \',\'\n    assign image_out_cancel = `I_Cancel( IS, image_out );\n                                           ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:149: 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/sim/camera_image_tb.v:149: syntax error, unexpected \',\'\n    assign image_out_ready = `I_Ready( IS, image_out );\n                                         ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:214: Define or directive not defined: \'`I_w\'\n    wire [`I_w( IS )-1:0 ] buffer_image_out;\n          ^~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:227: Define or directive not defined: \'`I_Start\'\n    assign buffer_image_out_start = `I_Start( IS, buffer_image_out );\n                                    ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:227: syntax error, unexpected \',\'\n    assign buffer_image_out_start = `I_Start( IS, buffer_image_out );\n                                                ^\n%Error: data/full_repos/permissive/190755935/src/drivers/sim/camera_image_tb.v:228: Define or directive not defined: \'`I_Stop\'\n    assign buffer_image_out_stop = `I_Stop( IS, buffer_image_out );\n                                   ^~~~~~~\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module camera_image_tb();

    parameter Output=`OutputDebug;

    reg  reset;

    initial begin
        $dumpfile("camera_image_tb.vcd");
        $dumpvars( 1, camera_image_tb );
        $dumpvars( 1, camera_image_tb.cam_image );
        $dumpvars( 1, camera_image_tb.cam_image.cam );
        $dumpvars( 1, camera_image_tb.buffer );
        $dumpvars( 1, camera_image_tb.cam_proxy );
        $dumpvars( 0, camera_image_tb.cam_proxy.i2c_s );
    end


    `Realtime


    `Clock10MHz

    `AssertSetup





    localparam CameraWidth = 32;
    localparam CameraHeight = 16;

    localparam CoordinateWidth = 10;
    localparam BlankingWidth = 16;





    wire sda;
    wire scl;

    wire camera_scl_out;
    wire camera_sda_out;

    wire camera_proxy_scl_out;
    wire camera_proxy_sda_out;

    assign scl = ( camera_scl_out && camera_proxy_scl_out );
    assign sda = ( camera_sda_out && camera_proxy_sda_out );







    wire       out_vs;
    wire       out_hs;
    wire       out_valid;
    wire [9:0] out_d;

    wire       vs;
    wire       hs;
    wire       pclk;
    wire       xclk;
    wire [9:0] d;
    wire       rst;
    wire       led;
    wire       pwdn;

    wire       trigger;

    reg configure;
    reg start;
    reg stop;

    wire configuring;
    wire idle;
    wire running;
    wire error;
    wire busy;
    wire image_transfer;


    reg [CoordinateWidth-1:0] image_x;
    reg [CoordinateWidth-1:0] image_y;
    reg image_origin_update;


    localparam IS =  `IS( 0, 0, CameraWidth - 1, CameraHeight - 4, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );

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

    localparam ImagePixelCount = `IS_PIXEL_COUNT( IS );

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

    localparam ImageC0Width =     `IS_C0_WIDTH( IS );
    localparam ImageC1Width =     `IS_C1_WIDTH( IS );
    localparam ImageC2Width =     `IS_C2_WIDTH( IS );
    localparam ImageAlphaWidth =  `IS_ALPHA_WIDTH( IS );
    localparam ImageZWidth =      `IS_Z_WIDTH( IS );

    wire [`I_w( IS )-1:0 ] image_out;
    reg                    out_request_external;


    wire                 image_out_start;
    wire                 image_out_stop;
    wire [ImageDataWidth-1:0] image_out_data;
    wire                 image_out_valid;
    wire                 image_out_error;
    wire                 image_out_ready;
    wire                 image_out_request;
    wire                 image_out_cancel;


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

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

    camera_image #(
            .IS( IS ),
            .CameraWidth( CameraWidth ),
            .CameraHeight( CameraHeight ),
            .CameraHorizontalBlanking( 10 ),
            .CameraVerticalBlanking( 10 ),
            .I2CClockCount( 20 ),
            .I2CGapCount( 1 << 6  )
        ) cam_image (
            .clock( clock ),
            .reset( reset ),


            .configure( configure ),
            .start( start ),
            .stop( stop ),


            .configuring( configuring ),
            .error( error ),
            .idle( idle ),
            .running( running ),
            .busy( busy ),
            .image_transfer( image_transfer ),


            .image_x( image_x ),
            .image_y( image_y ),
            .image_origin_update( image_origin_update ),


            .image_out( image_out ),
            .out_request_external( out_request_external ),


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



    reg buffer_in_request_external;
    reg buffer_out_request_external;

    wire buffer_in_receiving;
    wire buffer_out_sending;


    reg [ImageWidthWidth-1:0]  buffer_out_x;
    reg [ImageHeightWidth-1:0] buffer_out_y;
    wire [ImageDataWidth-1:0]  buffer_out_data;


    wire [`I_w( IS )-1:0 ] buffer_image_out;


    wire                 buffer_image_out_start;
    wire                 buffer_image_out_stop;
    wire [ImageDataWidth-1:0] buffer_image_out_data;
    wire                 buffer_image_out_valid;
    wire                 buffer_image_out_error;

    reg                  buffer_image_out_ready;
    reg                  buffer_image_out_request;
    reg                  buffer_image_out_cancel;

    assign buffer_image_out_start = `I_Start( IS, buffer_image_out );
    assign buffer_image_out_stop = `I_Stop( IS, buffer_image_out );
    assign buffer_image_out_data = `I_Data( IS, buffer_image_out );
    assign buffer_image_out_error = `I_Error( IS, buffer_image_out );
    assign buffer_image_out_valid = `I_Valid( IS, buffer_image_out );

    assign `I_Request( IS, buffer_image_out ) = buffer_image_out_request;
    assign `I_Cancel( IS, buffer_image_out ) = buffer_image_out_cancel;
    assign `I_Ready( IS, buffer_image_out ) = buffer_image_out_ready;

    image_buffer #(
            .IS( IS ),
            .ImplementAccessPort( 1 )
    ) buffer (
            .clock( clock ),
            .reset( reset ),

            .in_request_external( buffer_in_request_external ),
            .out_request_external( buffer_out_request_external ),

            .image_in( image_out ),
            .image_out( buffer_image_out ),

            .in_receiving( buffer_in_receiving ),
            .out_sending( buffer_out_sending ),

            .buffer_out_x( buffer_out_x ),
            .buffer_out_y( buffer_out_y ),
            .buffer_out_data( buffer_out_data )
        );

    camera_proxy #(
            .Width( CameraWidth ),
            .Height( CameraHeight )
        ) cam_proxy (
            .clock( clock ),
            .reset( reset ),

            .scl_in( scl ),
            .scl_out( camera_proxy_scl_out ),
            .sda_in( sda ),
            .sda_out( camera_proxy_sda_out ),

            .vs( vs ),
            .hs( hs ),
            .pclk( pclk ),
            .xclk( xclk ),
            .d( d ),

            .rst( rst ),
            .pwdn( pwdn ),

            .led( led ),
            .trigger( trigger )
        );

    assign xclk = ctb_xclk;

    integer i, j, k;
    integer c;
    integer timeout;
    integer count;

    integer long_c;
    integer long_count;

    reg [8*50:1] test_name;

    task camtb_init;
        begin

            configure = 0;
            start = 0;
            stop = 0;
            ctb_xclk = 0;

            image_x = 0;
            image_y = 0;
            image_origin_update = 0;

            buffer_in_request_external = 0;
            buffer_out_request_external = 0;

            buffer_image_out_cancel = 0;
            buffer_image_out_request = 0;
            buffer_image_out_ready = 0;

            c = 0;
            column_count = 0;
            row_count = 0;

            pixel_count = 0;

            test_height = 0;
            test_width = 0;

        end
    endtask

    reg ctb_xclk;

    task  camtb_clock;
        begin
            #2
            @( posedge clock );

            ctb_xclk = !ctb_xclk;
            #2
            ;
        end
    endtask

    task camtb_clock_multiple( input integer  n );
        begin
            for ( i = 0; i < n; i = i + 1 ) begin
                camtb_clock;
            end
        end
    endtask

    task  camtb_reset;
        begin
            reset = 1;
            camtb_clock;
            `Info( "    Reset");
            reset = 0;
            camtb_clock;
        end
    endtask

    task camtb_check_write_rect( input integer x0, input integer y0, input integer x1, input integer y1 );
        begin
            `InfoDo $display( "    Check Write [%3d,%3d]-[%3d,%3d]", x0, y0, x1, y1 );
        end
    endtask

    task camtb_wait_not_busy;
        begin
            `InfoDo $display( "        Wait Not Busy" );

            while ( busy )
                camtb_clock;
        end
    endtask

    integer out_frame_x;
    integer out_frame_y;
    integer out_frame_pixel;

    localparam out_frame_pixel_max_total = ( (2**ImageDataWidth) - 1 );

    function [2:0] out_brightness( input reg[ ImageDataWidth-1:0 ] pixel ); begin

            out_frame_pixel = ( ImageC0Width ) ? `I_C0( IS, pixel ) : 0;

            out_brightness = ( out_frame_pixel == 0 ) ? 0 :
                                  ( ( out_frame_pixel <= ( out_frame_pixel_max_total / 4 ) ? 1 :
                                       ( out_frame_pixel <= ( out_frame_pixel_max_total / 2 ) ? 2 : 3 ) ) );



        end
    endfunction

    integer b;

    task out_frame( input reg full );
        begin


            $write( "        /" );
            for ( out_frame_x = 0; out_frame_x < ImageWidth; out_frame_x = out_frame_x + 1 ) begin
                $write( "--" );
                if ( !full && ( out_frame_x == ImageWidth / 8 ) ) begin
                    $write( "..." );
                    out_frame_x = 15 * ImageWidth / 16;
                end
            end
            $write( "\\\n" );

            for ( out_frame_y = 0; out_frame_y < ImageHeight; out_frame_y = out_frame_y + 1 ) begin
                $write( "        |" );

                for ( out_frame_x = 0; out_frame_x < ImageWidth; out_frame_x = out_frame_x + 1 ) begin
                    buffer_out_x = out_frame_x;
                    buffer_out_y = out_frame_y;

                    #1

                    b = out_brightness( buffer_out_data );


                    $write( "%s", ( b == 0 ) ? "  " : ( ( b == 1 ) ? ". " : ( ( b == 2 ) ? ".." : "oo" ) ) );
                    if ( !full && ( out_frame_x == ImageWidth / 8 ) ) begin
                        $write( "..." );
                        out_frame_x = 15 * ImageWidth / 16;
                    end
                end
                $write( "|\n" );
                if ( !full && ( out_frame_y == ImageHeight / 8 ) ) begin
                    $write( "        ...\n" );
                    out_frame_y = 15 * ImageHeight / 16;
                end
            end


            $write( "        \\" );
            for ( out_frame_x = 0; out_frame_x < ImageWidth; out_frame_x = out_frame_x + 1 ) begin
                $write( "--" );
                if ( !full && ( out_frame_x == ImageWidth / 8 ) ) begin
                    $write( "..." );
                    out_frame_x = 15 * ImageWidth / 16;
                end
            end
            $write( "/\n" );

        end
    endtask















































    integer data_count;
    integer command_count;

    integer column_count;
    integer row_count;
    integer pixel_count;

    integer test_width;
    integer test_height;

    reg pclk_previous;

    initial begin
        $display( "\nCAMERA Test %s", `__FILE__ );

        camtb_init;
        camtb_reset;

        camtb_clock_multiple( 100 );

        `Info( "    Checking Idle State" );

        `Info( "    Configure" );

        configure = 1;

        camtb_clock;

        configure = 0;

        `Assert( configuring, "Configuring" );

        while ( configuring )
            camtb_clock;

        `Info( "    Idle" );

        `Assert( idle, "Idle" );

        camtb_clock_multiple( 1000 );









        camtb_wait_not_busy;








        camtb_wait_not_busy;


        `Info( "    Running" );

        start = 1;

        camtb_clock;

        start = 0;

        while ( running == 0 ) begin
            camtb_clock;
            $display( "        %d", running );
        end

        `Assert( running, "Running" );





        `Info( "    Image Request (via Buffer)" );

        buffer_in_request_external = 1;
        camtb_clock;
        buffer_in_request_external = 0;

        `Info( "    Waiting for image transfer" );

        c = 0;
        count = 100000;
        while ( ( image_transfer == 0 ) && ( c < count ) ) begin
            c = c + 1;
            camtb_clock;
        end

        `Info( "    Image Transfer" );

        `Assert( c < count, "Image Transfer taking too long" );

        pixel_count = 0;

        $write( "        " );

        while ( ( c < count ) && ( image_transfer == 1) && ( pixel_count < (ImageWidth * ImageHeight)  ) ) begin

            c = 0;
            count = 10000;
            while ( ( image_out_valid == 0 ) && ( c < count ) ) begin
                camtb_clock;
                c = c + 1;
            end

            `Assert( c < count, "Image Pixel taking too long" );
            `Assert( image_out_valid, "Pixel" );

            if ( pixel_count == 0 )
                `Assert( image_out_start, "Start" );

            if ( pixel_count == ( ( ImageWidth * ImageHeight ) -1 ) )
                `Assert( image_out_stop, "Stop" );

            $write( " %-d", pixel_count  );
            pixel_count = pixel_count + 1;

            camtb_clock;

        end

        `Assert( c < count, "Timeout" );
        `AssertEqual( pixel_count, (ImageWidth * ImageHeight), "Pixel Count"  );

        `InfoDo $display( "        P %-d", pixel_count );

        camtb_clock_multiple( 100 );

        out_frame( 1 );

        `AssertSummary

        $finish;
    end

endmodule
module camera_image_tb();


    parameter Output=`OutputDebug;

    reg  reset;

    initial begin
        $dumpfile("camera_image_tb.vcd");
        $dumpvars( 1, camera_image_tb );
        $dumpvars( 1, camera_image_tb.cam_image );
        $dumpvars( 1, camera_image_tb.cam_image.cam );
        $dumpvars( 1, camera_image_tb.buffer );
        $dumpvars( 1, camera_image_tb.cam_proxy );
        $dumpvars( 0, camera_image_tb.cam_proxy.i2c_s );
    end


    `Realtime


    `Clock10MHz

    `AssertSetup





    localparam CameraWidth = 32;
    localparam CameraHeight = 16;

    localparam CoordinateWidth = 10;
    localparam BlankingWidth = 16;





    wire sda;
    wire scl;

    wire camera_scl_out;
    wire camera_sda_out;

    wire camera_proxy_scl_out;
    wire camera_proxy_sda_out;

    assign scl = ( camera_scl_out && camera_proxy_scl_out );
    assign sda = ( camera_sda_out && camera_proxy_sda_out );







    wire       out_vs;
    wire       out_hs;
    wire       out_valid;
    wire [9:0] out_d;

    wire       vs;
    wire       hs;
    wire       pclk;
    wire       xclk;
    wire [9:0] d;
    wire       rst;
    wire       led;
    wire       pwdn;

    wire       trigger;

    reg configure;
    reg start;
    reg stop;

    wire configuring;
    wire idle;
    wire running;
    wire error;
    wire busy;
    wire image_transfer;


    reg [CoordinateWidth-1:0] image_x;
    reg [CoordinateWidth-1:0] image_y;
    reg image_origin_update;


    localparam IS =  `IS( 0, 0, CameraWidth - 1, CameraHeight - 4, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );

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

    localparam ImagePixelCount = `IS_PIXEL_COUNT( IS );

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

    localparam ImageC0Width =     `IS_C0_WIDTH( IS );
    localparam ImageC1Width =     `IS_C1_WIDTH( IS );
    localparam ImageC2Width =     `IS_C2_WIDTH( IS );
    localparam ImageAlphaWidth =  `IS_ALPHA_WIDTH( IS );
    localparam ImageZWidth =      `IS_Z_WIDTH( IS );

    wire [`I_w( IS )-1:0 ] image_out;
    reg                    out_request_external;


    wire                 image_out_start;
    wire                 image_out_stop;
    wire [ImageDataWidth-1:0] image_out_data;
    wire                 image_out_valid;
    wire                 image_out_error;
    wire                 image_out_ready;
    wire                 image_out_request;
    wire                 image_out_cancel;


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

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

    camera_image #(
            .IS( IS ),
            .CameraWidth( CameraWidth ),
            .CameraHeight( CameraHeight ),
            .CameraHorizontalBlanking( 10 ),
            .CameraVerticalBlanking( 10 ),
            .I2CClockCount( 20 ),
            .I2CGapCount( 1 << 6  )
        ) cam_image (
            .clock( clock ),
            .reset( reset ),


            .configure( configure ),
            .start( start ),
            .stop( stop ),


            .configuring( configuring ),
            .error( error ),
            .idle( idle ),
            .running( running ),
            .busy( busy ),
            .image_transfer( image_transfer ),


            .image_x( image_x ),
            .image_y( image_y ),
            .image_origin_update( image_origin_update ),


            .image_out( image_out ),
            .out_request_external( out_request_external ),


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



    reg buffer_in_request_external;
    reg buffer_out_request_external;

    wire buffer_in_receiving;
    wire buffer_out_sending;


    reg [ImageWidthWidth-1:0]  buffer_out_x;
    reg [ImageHeightWidth-1:0] buffer_out_y;
    wire [ImageDataWidth-1:0]  buffer_out_data;


    wire [`I_w( IS )-1:0 ] buffer_image_out;


    wire                 buffer_image_out_start;
    wire                 buffer_image_out_stop;
    wire [ImageDataWidth-1:0] buffer_image_out_data;
    wire                 buffer_image_out_valid;
    wire                 buffer_image_out_error;

    reg                  buffer_image_out_ready;
    reg                  buffer_image_out_request;
    reg                  buffer_image_out_cancel;

    assign buffer_image_out_start = `I_Start( IS, buffer_image_out );
    assign buffer_image_out_stop = `I_Stop( IS, buffer_image_out );
    assign buffer_image_out_data = `I_Data( IS, buffer_image_out );
    assign buffer_image_out_error = `I_Error( IS, buffer_image_out );
    assign buffer_image_out_valid = `I_Valid( IS, buffer_image_out );

    assign `I_Request( IS, buffer_image_out ) = buffer_image_out_request;
    assign `I_Cancel( IS, buffer_image_out ) = buffer_image_out_cancel;
    assign `I_Ready( IS, buffer_image_out ) = buffer_image_out_ready;

    image_buffer #(
            .IS( IS ),
            .ImplementAccessPort( 1 )
    ) buffer (
            .clock( clock ),
            .reset( reset ),

            .in_request_external( buffer_in_request_external ),
            .out_request_external( buffer_out_request_external ),

            .image_in( image_out ),
            .image_out( buffer_image_out ),

            .in_receiving( buffer_in_receiving ),
            .out_sending( buffer_out_sending ),

            .buffer_out_x( buffer_out_x ),
            .buffer_out_y( buffer_out_y ),
            .buffer_out_data( buffer_out_data )
        );

    camera_proxy #(
            .Width( CameraWidth ),
            .Height( CameraHeight )
        ) cam_proxy (
            .clock( clock ),
            .reset( reset ),

            .scl_in( scl ),
            .scl_out( camera_proxy_scl_out ),
            .sda_in( sda ),
            .sda_out( camera_proxy_sda_out ),

            .vs( vs ),
            .hs( hs ),
            .pclk( pclk ),
            .xclk( xclk ),
            .d( d ),

            .rst( rst ),
            .pwdn( pwdn ),

            .led( led ),
            .trigger( trigger )
        );

    assign xclk = ctb_xclk;

    integer i, j, k;
    integer c;
    integer timeout;
    integer count;

    integer long_c;
    integer long_count;

    reg [8*50:1] test_name;

    task camtb_init;
        begin

            configure = 0;
            start = 0;
            stop = 0;
            ctb_xclk = 0;

            image_x = 0;
            image_y = 0;
            image_origin_update = 0;

            buffer_in_request_external = 0;
            buffer_out_request_external = 0;

            buffer_image_out_cancel = 0;
            buffer_image_out_request = 0;
            buffer_image_out_ready = 0;

            c = 0;
            column_count = 0;
            row_count = 0;

            pixel_count = 0;

            test_height = 0;
            test_width = 0;

        end
    endtask

    reg ctb_xclk;

    task  camtb_clock;
        begin
            #2
            @( posedge clock );

            ctb_xclk = !ctb_xclk;
            #2
            ;
        end
    endtask

    task camtb_clock_multiple( input integer  n );
        begin
            for ( i = 0; i < n; i = i + 1 ) begin
                camtb_clock;
            end
        end
    endtask

    task  camtb_reset;
        begin
            reset = 1;
            camtb_clock;
            `Info( "    Reset");
            reset = 0;
            camtb_clock;
        end
    endtask

    task camtb_check_write_rect( input integer x0, input integer y0, input integer x1, input integer y1 );
        begin
            `InfoDo $display( "    Check Write [%3d,%3d]-[%3d,%3d]", x0, y0, x1, y1 );
        end
    endtask

    task camtb_wait_not_busy;
        begin
            `InfoDo $display( "        Wait Not Busy" );

            while ( busy )
                camtb_clock;
        end
    endtask

    integer out_frame_x;
    integer out_frame_y;
    integer out_frame_pixel;

    localparam out_frame_pixel_max_total = ( (2**ImageDataWidth) - 1 );

    function [2:0] out_brightness( input reg[ ImageDataWidth-1:0 ] pixel ); begin

            out_frame_pixel = ( ImageC0Width ) ? `I_C0( IS, pixel ) : 0;

            out_brightness = ( out_frame_pixel == 0 ) ? 0 :
                                  ( ( out_frame_pixel <= ( out_frame_pixel_max_total / 4 ) ? 1 :
                                       ( out_frame_pixel <= ( out_frame_pixel_max_total / 2 ) ? 2 : 3 ) ) );



        end
    endfunction

    integer b;

    task out_frame( input reg full );
        begin


            $write( "        /" );
            for ( out_frame_x = 0; out_frame_x < ImageWidth; out_frame_x = out_frame_x + 1 ) begin
                $write( "--" );
                if ( !full && ( out_frame_x == ImageWidth / 8 ) ) begin
                    $write( "..." );
                    out_frame_x = 15 * ImageWidth / 16;
                end
            end
            $write( "\\\n" );

            for ( out_frame_y = 0; out_frame_y < ImageHeight; out_frame_y = out_frame_y + 1 ) begin
                $write( "        |" );

                for ( out_frame_x = 0; out_frame_x < ImageWidth; out_frame_x = out_frame_x + 1 ) begin
                    buffer_out_x = out_frame_x;
                    buffer_out_y = out_frame_y;

                    #1

                    b = out_brightness( buffer_out_data );


                    $write( "%s", ( b == 0 ) ? "  " : ( ( b == 1 ) ? ". " : ( ( b == 2 ) ? ".." : "oo" ) ) );
                    if ( !full && ( out_frame_x == ImageWidth / 8 ) ) begin
                        $write( "..." );
                        out_frame_x = 15 * ImageWidth / 16;
                    end
                end
                $write( "|\n" );
                if ( !full && ( out_frame_y == ImageHeight / 8 ) ) begin
                    $write( "        ...\n" );
                    out_frame_y = 15 * ImageHeight / 16;
                end
            end


            $write( "        \\" );
            for ( out_frame_x = 0; out_frame_x < ImageWidth; out_frame_x = out_frame_x + 1 ) begin
                $write( "--" );
                if ( !full && ( out_frame_x == ImageWidth / 8 ) ) begin
                    $write( "..." );
                    out_frame_x = 15 * ImageWidth / 16;
                end
            end
            $write( "/\n" );

        end
    endtask















































    integer data_count;
    integer command_count;

    integer column_count;
    integer row_count;
    integer pixel_count;

    integer test_width;
    integer test_height;

    reg pclk_previous;

    initial begin
        $display( "\nCAMERA Test %s", `__FILE__ );

        camtb_init;
        camtb_reset;

        camtb_clock_multiple( 100 );

        `Info( "    Checking Idle State" );

        `Info( "    Configure" );

        configure = 1;

        camtb_clock;

        configure = 0;

        `Assert( configuring, "Configuring" );

        while ( configuring )
            camtb_clock;

        `Info( "    Idle" );

        `Assert( idle, "Idle" );

        camtb_clock_multiple( 1000 );









        camtb_wait_not_busy;








        camtb_wait_not_busy;


        `Info( "    Running" );

        start = 1;

        camtb_clock;

        start = 0;

        while ( running == 0 ) begin
            camtb_clock;
            $display( "        %d", running );
        end

        `Assert( running, "Running" );





        `Info( "    Image Request (via Buffer)" );

        buffer_in_request_external = 1;
        camtb_clock;
        buffer_in_request_external = 0;

        `Info( "    Waiting for image transfer" );

        c = 0;
        count = 100000;
        while ( ( image_transfer == 0 ) && ( c < count ) ) begin
            c = c + 1;
            camtb_clock;
        end

        `Info( "    Image Transfer" );

        `Assert( c < count, "Image Transfer taking too long" );

        pixel_count = 0;

        $write( "        " );

        while ( ( c < count ) && ( image_transfer == 1) && ( pixel_count < (ImageWidth * ImageHeight)  ) ) begin

            c = 0;
            count = 10000;
            while ( ( image_out_valid == 0 ) && ( c < count ) ) begin
                camtb_clock;
                c = c + 1;
            end

            `Assert( c < count, "Image Pixel taking too long" );
            `Assert( image_out_valid, "Pixel" );

            if ( pixel_count == 0 )
                `Assert( image_out_start, "Start" );

            if ( pixel_count == ( ( ImageWidth * ImageHeight ) -1 ) )
                `Assert( image_out_stop, "Stop" );

            $write( " %-d", pixel_count  );
            pixel_count = pixel_count + 1;

            camtb_clock;

        end

        `Assert( c < count, "Timeout" );
        `AssertEqual( pixel_count, (ImageWidth * ImageHeight), "Pixel Count"  );

        `InfoDo $display( "        P %-d", pixel_count );

        camtb_clock_multiple( 100 );

        out_frame( 1 );

        `AssertSummary

        $finish;
    end

endmodule