data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v
None: at end of input
1: b'%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:18: 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/hadbadge2019/projects/camera_image_lcd,data/full_repos/permissive/190755935/../../pipe/rtl/pipe_defs.v\n             data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd,data/full_repos/permissive/190755935/../../pipe/rtl/pipe_defs.v.v\n             data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd,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/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:20: 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/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:376: Cannot find include file: ../../drivers/rtl/lcd_defs.v\n    `include "../../drivers/rtl/lcd_defs.v" \n             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:229: Define or directive not defined: \'`IS_w\'\n    localparam [`IS_w-1:0] CameraIS =  `IS( 0, 0, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                ^~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:229: Define or directive not defined: \'`IS\'\n    localparam [`IS_w-1:0] CameraIS =  `IS( 0, 0, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                                       ^~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:229: syntax error, unexpected \',\'\n    localparam [`IS_w-1:0] CameraIS =  `IS( 0, 0, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                                             ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:229: Define or directive not defined: \'`IS_FORMAT_GRAYSCALE\'\n    localparam [`IS_w-1:0] CameraIS =  `IS( 0, 0, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );\n                                                                                 ^~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:231: Define or directive not defined: \'`IS_PIXEL_COUNT\'\n    localparam ImagePixelCount = `IS_PIXEL_COUNT( CameraIS );\n                                 ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:233: Define or directive not defined: \'`IS_WIDTH_WIDTH\'\n    localparam ImageWidthWidth =  `IS_WIDTH_WIDTH(  CameraIS );\n                                  ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:234: Define or directive not defined: \'`IS_HEIGHT_WIDTH\'\n    localparam ImageHeightWidth = `IS_HEIGHT_WIDTH( CameraIS );\n                                  ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:235: Define or directive not defined: \'`IS_DATA_WIDTH\'\n    localparam ImageDataWidth =   `IS_DATA_WIDTH(   CameraIS );\n                                  ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:237: Define or directive not defined: \'`IS_C0_WIDTH\'\n    localparam ImageC0Width =     `IS_C0_WIDTH(    CameraIS );\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:238: Define or directive not defined: \'`IS_C1_WIDTH\'\n    localparam ImageC1Width =     `IS_C1_WIDTH(    CameraIS );\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:239: Define or directive not defined: \'`IS_C2_WIDTH\'\n    localparam ImageC2Width =     `IS_C2_WIDTH(    CameraIS );\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:240: Define or directive not defined: \'`IS_ALPHA_WIDTH\'\n    localparam ImageAlphaWidth =  `IS_ALPHA_WIDTH( CameraIS );\n                                  ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:241: Define or directive not defined: \'`IS_Z_WIDTH\'\n    localparam ImageZWidth =      `IS_Z_WIDTH(     CameraIS );\n                                  ^~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:243: Define or directive not defined: \'`I_w\'\n    wire [`I_w( CameraIS )-1:0 ] camera_image_out;\n          ^~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:257: Define or directive not defined: \'`I_Start\'\n    assign camera_image_out_start = `I_Start( CameraIS, camera_image_out );\n                                    ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:257: syntax error, unexpected \',\'\n    assign camera_image_out_start = `I_Start( CameraIS, camera_image_out );\n                                                      ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:258: Define or directive not defined: \'`I_Stop\'\n    assign camera_image_out_stop  = `I_Stop( CameraIS, camera_image_out );\n                                    ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:258: syntax error, unexpected \',\'\n    assign camera_image_out_stop  = `I_Stop( CameraIS, camera_image_out );\n                                                     ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:259: Define or directive not defined: \'`I_Data\'\n    assign camera_image_out_data  = `I_Data( CameraIS, camera_image_out );\n                                    ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:259: syntax error, unexpected \',\'\n    assign camera_image_out_data  = `I_Data( CameraIS, camera_image_out );\n                                                     ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:260: Define or directive not defined: \'`I_Error\'\n                                                                                                              : ... Suggested alternative: \'`error\'\n    assign camera_image_out_error = `I_Error( CameraIS, camera_image_out );\n                                    ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:260: syntax error, unexpected \',\'\n    assign camera_image_out_error = `I_Error( CameraIS, camera_image_out );\n                                                      ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:261: Define or directive not defined: \'`I_Valid\'\n    assign camera_image_out_valid = `I_Valid( CameraIS, camera_image_out );\n                                    ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:261: syntax error, unexpected \',\'\n    assign camera_image_out_valid = `I_Valid( CameraIS, camera_image_out );\n                                                      ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:263: Define or directive not defined: \'`I_Request\'\n    assign camera_image_out_request = `I_Request( CameraIS, camera_image_out );\n                                      ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:263: syntax error, unexpected \',\'\n    assign camera_image_out_request = `I_Request( CameraIS, camera_image_out );\n                                                          ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:264: Define or directive not defined: \'`I_Cancel\'\n    assign camera_image_out_cancel  = `I_Cancel( CameraIS, camera_image_out );\n                                      ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:264: syntax error, unexpected \',\'\n    assign camera_image_out_cancel  = `I_Cancel( CameraIS, camera_image_out );\n                                                         ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:265: Define or directive not defined: \'`I_Ready\'\n    assign camera_image_out_ready   = `I_Ready( CameraIS, camera_image_out );\n                                      ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:265: syntax error, unexpected \',\'\n    assign camera_image_out_ready   = `I_Ready( CameraIS, camera_image_out );\n                                                        ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:343: Define or directive not defined: \'`I_w\'\n    wire [`I_w( CameraIS )-1:0 ] image_fifo_to_reformat;\n          ^~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:350: Define or directive not defined: \'`I_Valid\'\n    assign image_fifo_out_valid = `I_Valid( CameraIS, image_fifo_to_reformat );\n                                  ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:350: syntax error, unexpected \',\'\n    assign image_fifo_out_valid = `I_Valid( CameraIS, image_fifo_to_reformat );\n                                                    ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:351: Define or directive not defined: \'`I_Ready\'\n    assign image_fifo_out_ready   = `I_Ready( CameraIS, image_fifo_to_reformat );\n                                    ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:351: syntax error, unexpected \',\'\n    assign image_fifo_out_ready   = `I_Ready( CameraIS, image_fifo_to_reformat );\n                                                      ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:357: Define or directive not defined: \'`IS_w\'\n    localparam [`IS_w-1:0] LcdIS =  `IS( ImageXInitial, ImageYInitial, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_RGB, 5, 6, 5, 0, 0 );\n                ^~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:357: Define or directive not defined: \'`IS\'\n    localparam [`IS_w-1:0] LcdIS =  `IS( ImageXInitial, ImageYInitial, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_RGB, 5, 6, 5, 0, 0 );\n                                    ^~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:357: syntax error, unexpected \',\'\n    localparam [`IS_w-1:0] LcdIS =  `IS( ImageXInitial, ImageYInitial, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_RGB, 5, 6, 5, 0, 0 );\n                                                      ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:357: Define or directive not defined: \'`IS_FORMAT_RGB\'\n    localparam [`IS_w-1:0] LcdIS =  `IS( ImageXInitial, ImageYInitial, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_RGB, 5, 6, 5, 0, 0 );\n                                                                                                      ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:359: Define or directive not defined: \'`I_w\'\n    wire [`I_w( LcdIS )-1:0 ] lcd_image_in;\n          ^~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:364: Define or directive not defined: \'`I_Stop\'\n    assign lcd_image_stop = `I_Stop( LcdIS, lcd_image_in );\n                            ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:364: syntax error, unexpected \',\'\n    assign lcd_image_stop = `I_Stop( LcdIS, lcd_image_in );\n                                          ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:369: Define or directive not defined: \'`I_Valid\'\n    assign lcd_image_valid = `I_Valid( LcdIS, lcd_image_in );\n                             ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:369: syntax error, unexpected \',\'\n    assign lcd_image_valid = `I_Valid( LcdIS, lcd_image_in );\n                                            ^\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:370: Define or directive not defined: \'`I_Ready\'\n    assign lcd_image_ready = `I_Ready( LcdIS, lcd_image_in );\n                             ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/hadbadge2019/projects/camera_image_lcd/camera_image_lcd.v:370: syntax error, unexpected \',\'\n    assign lcd_image_ready = `I_Ready( LcdIS, lcd_image_in );\n                                            ^\n%Error: Exiting due to 49 error(s)\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module camera_image_lcd (
		input clk,
		input [7:0] btn,
`ifdef BADGE_V3
		output [10:0] ledc,
		output [2:0] leda,
		inout [29:0] genio,
`else
		output [8:0] led,
		inout [27:0] genio,
`endif
		output uart_tx,
		input uart_rx,
`ifdef BADGE_V3
		output irda_tx,
		input irda_rx,
		output irda_sd,
`endif
		output pwmout,

		output [17:0] lcd_db,
		output lcd_rd,
		output lcd_wr,
		output lcd_rs,
		output lcd_cs,
		input lcd_id,
		output lcd_rst,
		input lcd_fmark,
		output lcd_blen,

		output psrama_nce,
		output psrama_sclk,
		inout [3:0] psrama_sio,
		output psramb_nce,
		output psramb_sclk,
		inout [3:0] psramb_sio,
		output flash_cs,
		inout flash_miso,
		inout flash_mosi,
		inout flash_wp,
		inout flash_hold,
		output fsel_d,
		output fsel_c,
		output programn,

		output [3:0] gpdi_dp, gpdi_dn,
		inout usb_dp,
		inout usb_dm,
		output usb_pu,
		input usb_vdet,

		inout [5:0] sao1,
		inout [5:0] sao2,

		inout [7:0] pmod,

		output adcrefout,
		input adcref4
	);





    wire clock_48mhz;
    clock_pll c_pll (.CLKI( clk ), .CLKOP( clock_48mhz ));






    reg [5:0] reset_counter = 0;
    wire reset = ~reset_counter[5];
    always @(posedge clock_48mhz)
        reset_counter <= reset_counter + reset;





	reg fpga_reload=0;
	assign programn = ~fpga_reload;
	always @( posedge clock_48mhz )
		if ( ~btn[4] )
			fpga_reload <= 1;





	reg [28:0] led_counter = 0;
	always @( posedge clock_48mhz ) begin
		if ( reset ) begin
		end else begin
			led_counter <= led_counter + 1'H1;
		end
	end

    `ifdef BADGE_V2
	    assign led[ 8 ] = led_counter[ 25:19 ] == 0;
    `else
	    assign ledc[ 8 ] = led_counter[ 25:19 ] == 0;
    `endif











    localparam LcdWidth = 480;
    localparam LcdHeight = 320;

    localparam CameraWidth = 752;
    localparam CameraHeight = 482;


    localparam ImageWidth = 320;
    localparam ImageHeight = 200;
    localparam ImageXInitial = ( LcdWidth - ImageWidth ) / 2;
    localparam ImageYInitial = ( LcdHeight - ImageHeight ) / 2;

    localparam BlankingWidth = 10;
    localparam CoordinateWidth = 10;
    localparam CameraPixelWidth = 10;

    reg  camera_configure;
    reg  camera_start;
    reg  camera_stop;

    wire camera_configuring;
    wire camera_idle;
    wire camera_running;
    wire camera_error;
    wire camera_busy;
    wire camera_image_transfer;










	wire [7:0] debug;




    reg refresh;





    wire sda;
    wire scl;

    wire camera_scl_out;
    wire camera_sda_out;

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

    wire       trigger;

	IB ib_hs( .I( genio[ 17 ] ), .O( hs ) );
	IB ib_vs( .I( genio[ 16 ] ), .O( vs ) );
	IB ib_pclk( .I( genio[ 14 ] ), .O( pclk ) );
	IB ib_led( .I( genio[ 1 ] ), .O( cled ) );

    IB ib_d0( .I( genio[  5 ] ), .O( d[ 0 ] ) );
    IB ib_d1( .I( genio[  4 ] ), .O( d[ 1 ] ) );
    IB ib_d2( .I( genio[  7 ] ), .O( d[ 2 ] ) );
    IB ib_d3( .I( genio[  6 ] ), .O( d[ 3 ] ) );
    IB ib_d4( .I( genio[  9 ] ), .O( d[ 4 ] ) );
    IB ib_d5( .I( genio[  8 ] ), .O( d[ 5 ] ) );
    IB ib_d6( .I( genio[ 11 ] ), .O( d[ 6 ] ) );
    IB ib_d7( .I( genio[ 10 ] ), .O( d[ 7 ] ) );
    IB ib_d8( .I( genio[ 13 ] ), .O( d[ 8 ] ) );
    IB ib_d9( .I( genio[ 12 ] ), .O( d[ 9 ] ) );

    OB ob_xclk( .I( xclk ), .O( genio[ 15 ] ) );
    OB  ob_rst( .I( rst ),  .O( genio[ 2 ] ) );
    OB ob_pwdn( .I( pwdn ),  .O( genio[ 3 ] ) );

    OB ob_trigger( .I( trigger ), .O( genio[ 0 ] ) );


    reg [CoordinateWidth-1:0] camera_image_x;
    reg [CoordinateWidth-1:0] camera_image_y;
    reg camera_image_origin_update;


    localparam [`IS_w-1:0] CameraIS =  `IS( 0, 0, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );

    localparam ImagePixelCount = `IS_PIXEL_COUNT( CameraIS );

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

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

    wire [`I_w( CameraIS )-1:0 ] camera_image_out;
    reg                    camera_out_request_external;


    wire                 camera_image_out_start;
    wire                 camera_image_out_stop;
    wire [ImageDataWidth-1:0] camera_image_out_data;
    wire                 camera_image_out_valid;
    wire                 camera_image_out_error;
    wire                 camera_image_out_ready;
    wire                 camera_image_out_request;
    wire                 camera_image_out_cancel;


    assign camera_image_out_start = `I_Start( CameraIS, camera_image_out );
    assign camera_image_out_stop  = `I_Stop( CameraIS, camera_image_out );
    assign camera_image_out_data  = `I_Data( CameraIS, camera_image_out );
    assign camera_image_out_error = `I_Error( CameraIS, camera_image_out );
    assign camera_image_out_valid = `I_Valid( CameraIS, camera_image_out );

    assign camera_image_out_request = `I_Request( CameraIS, camera_image_out );
    assign camera_image_out_cancel  = `I_Cancel( CameraIS, camera_image_out );
    assign camera_image_out_ready   = `I_Ready( CameraIS, camera_image_out );

    camera_image #(
            .IS( CameraIS ),
            .CameraWidth( CameraWidth ),
            .CameraHeight( CameraHeight ),
            .ImageXInitial( ImageXInitial ),
            .ImageYInitial( ImageYInitial ),
            .I2CClockCount( 400 ),
			.I2CGapCount( ( 1 << 13 ) )
        ) cam_image (
            .clock( clock_48mhz),
            .reset( reset ),


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


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


            .image_x( camera_image_x ),
            .image_y( camera_image_y ),
            .image_origin_update( camera_image_origin_update ),


            .image_out( camera_image_out ),
            .out_request_external( camera_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( cled ),
            .trigger( trigger )


        );



	BB    clock_io( .I( camera_scl_out ), .T( 0 ), .O( scl ), .B( genio[18] ) );
	BBPU   data_io( .I( 0 ), .T( camera_sda_out), .O( sda ), .B( genio[19] ) );

    assign pmod[ 0 ] = refresh;
    assign pmod[ 1 ] = camera_image_out_valid;
    assign pmod[ 2 ] = image_fifo_out_valid;
    assign pmod[ 3 ] = image_fifo_out_ready;



    assign pmod[ 4 ] = debug[ 0 ];
    assign pmod[ 5 ] = debug[ 1 ];
    assign pmod[ 6 ] = debug[ 4 ];

    assign pmod[ 7 ] = lcd_wr;








    wire [`I_w( CameraIS )-1:0 ] image_fifo_to_reformat;

    image_fifo #( .InIS( CameraIS ), .OutIS( CameraIS ), .MemoryWidth( 9 ) ) i_f( clock_48mhz, reset, camera_image_out, image_fifo_to_reformat );

    wire                 image_fifo_out_valid;
    wire                 image_fifo_out_ready;

    assign image_fifo_out_valid = `I_Valid( CameraIS, image_fifo_to_reformat );
    assign image_fifo_out_ready   = `I_Ready( CameraIS, image_fifo_to_reformat );





    localparam [`IS_w-1:0] LcdIS =  `IS( ImageXInitial, ImageYInitial, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_RGB, 5, 6, 5, 0, 0 );

    wire [`I_w( LcdIS )-1:0 ] lcd_image_in;

    image_reformat #( .InIS( CameraIS ), .OutIS( LcdIS ) ) ir( clock_48mhz, reset, image_fifo_to_reformat, lcd_image_in );

    wire lcd_image_stop;
    assign lcd_image_stop = `I_Stop( LcdIS, lcd_image_in );

    wire lcd_image_valid;
    wire lcd_image_ready;

    assign lcd_image_valid = `I_Valid( LcdIS, lcd_image_in );
    assign lcd_image_ready = `I_Ready( LcdIS, lcd_image_in );





    `include "../../drivers/rtl/lcd_defs.v"

    wire lcd_configuring;
    wire lcd_running;
    wire lcd_busy;



    lcd_image #(
            .IS( LcdIS ),
        ) lc (
            .clock( clock_48mhz ),
            .reset( reset ),

            .image( lcd_image_in ),

            .configuring( lcd_configuring ),
            .running( lcd_running ),
            .busy( lcd_busy ),

            .refresh( refresh ),


            .lcd_db(lcd_db),
            .lcd_rd(lcd_rd),
            .lcd_wr(lcd_wr),
            .lcd_rs(lcd_rs),
            .lcd_cs(lcd_cs),
            .lcd_id(lcd_id),
            .lcd_rst(lcd_rst),
            .lcd_fmark(lcd_fmark),
            .lcd_blen(lcd_blen),

            .debug( debug )
        );






















	reg [2:0] clock_divided;

	always @( posedge clock_48mhz ) begin
		if ( reset )
			clock_divided <= 0;
		else
			clock_divided <= clock_divided + 1;
	end

	assign xclk = clock_divided[ 0 ];

	localparam CONTROL_POWER_DOWN = 0,
	           CONTROL_CONFIGURING = 1,
               CONTROL_CAMERA_SETUP = 2,
               CONTROL_CAMERA_SET_WINDOW = 3,
               CONTROL_CAMERA_SET_BLANKING = 4,
			   CONTROL_ERROR = 5,
			   CONTROL_RUNNING = 6;

	reg [3:0] control_state;

    localparam ControlTimerWidth = 24;
	localparam ControlTimerLongCount = 2**22;
	localparam ControlTimerShortCount = 10;

	reg [ ControlTimerWidth:0 ] control_timer;
	wire control_timer_expired = control_timer[ ControlTimerWidth ];

	always @( posedge clock_48mhz ) begin
		if ( reset ) begin
            camera_configure <= 0;
			camera_start <= 0;
			camera_stop <= 0;
			control_timer <= ControlTimerLongCount;
			control_state <= CONTROL_POWER_DOWN;
            camera_out_request_external <= 0;
    `ifdef BADGE_V2
            led[ 7:0 ] <= 0;
    `else
	        ledc[ 7:0 ] <= 0;
    `endif

		end else begin
			case ( control_state )
				CONTROL_POWER_DOWN: begin
						if ( control_timer_expired ) begin
							camera_configure <= 1;
							control_state <= CONTROL_CONFIGURING;
						end else begin
							control_timer <= control_timer - 1;
						end
					end
				CONTROL_CONFIGURING: begin


                        camera_configure <= 0;

                        if ( lcd_running ) begin

                            if ( camera_idle && !camera_busy ) begin
                                if ( control_timer_expired ) begin
                                    control_timer <= ControlTimerLongCount;
                                    control_state <= CONTROL_RUNNING;
                                    camera_start <= 1;
                                end else begin
                                    control_timer <= control_timer - 1;
                                end
                            end else begin

                                if ( camera_error ) begin
                                    camera_start <= 0;
                                    control_state <= CONTROL_ERROR;


        							control_timer <= ControlTimerLongCount;
                                end
                            end
                        end
					end
				CONTROL_RUNNING: begin

                        if ( control_timer_expired ) begin
                            if ( !camera_busy && !lcd_busy && ~btn[ 2 ] && ~camera_image_transfer ) begin
                                refresh <= 1;
                                control_timer <= ControlTimerLongCount;
                            end
                            camera_start <= ~btn[ 1 ];
                        end else begin
                            refresh <= 0;
                            control_timer <= control_timer - 1;
                        end
					end
				CONTROL_ERROR: begin
                        if ( control_timer_expired ) begin

                            if ( ~btn[ 0 ] ) begin
                                control_state <= CONTROL_POWER_DOWN;
                                control_timer <= ControlTimerLongCount;
                            end
                        end else begin
                            control_timer <= control_timer - 1;
                        end
                    end
			endcase
		end
	end

endmodule
module camera_image_lcd (
		input clk,
		input [7:0] btn,
`ifdef BADGE_V3
		output [10:0] ledc,
		output [2:0] leda,
		inout [29:0] genio,
`else
		output [8:0] led,
		inout [27:0] genio,
`endif
		output uart_tx,
		input uart_rx,
`ifdef BADGE_V3
		output irda_tx,
		input irda_rx,
		output irda_sd,
`endif
		output pwmout,

		output [17:0] lcd_db,
		output lcd_rd,
		output lcd_wr,
		output lcd_rs,
		output lcd_cs,
		input lcd_id,
		output lcd_rst,
		input lcd_fmark,
		output lcd_blen,

		output psrama_nce,
		output psrama_sclk,
		inout [3:0] psrama_sio,
		output psramb_nce,
		output psramb_sclk,
		inout [3:0] psramb_sio,
		output flash_cs,
		inout flash_miso,
		inout flash_mosi,
		inout flash_wp,
		inout flash_hold,
		output fsel_d,
		output fsel_c,
		output programn,

		output [3:0] gpdi_dp, gpdi_dn,
		inout usb_dp,
		inout usb_dm,
		output usb_pu,
		input usb_vdet,

		inout [5:0] sao1,
		inout [5:0] sao2,

		inout [7:0] pmod,

		output adcrefout,
		input adcref4
	);






    wire clock_48mhz;
    clock_pll c_pll (.CLKI( clk ), .CLKOP( clock_48mhz ));






    reg [5:0] reset_counter = 0;
    wire reset = ~reset_counter[5];
    always @(posedge clock_48mhz)
        reset_counter <= reset_counter + reset;





	reg fpga_reload=0;
	assign programn = ~fpga_reload;
	always @( posedge clock_48mhz )
		if ( ~btn[4] )
			fpga_reload <= 1;





	reg [28:0] led_counter = 0;
	always @( posedge clock_48mhz ) begin
		if ( reset ) begin
		end else begin
			led_counter <= led_counter + 1'H1;
		end
	end

    `ifdef BADGE_V2
	    assign led[ 8 ] = led_counter[ 25:19 ] == 0;
    `else
	    assign ledc[ 8 ] = led_counter[ 25:19 ] == 0;
    `endif











    localparam LcdWidth = 480;
    localparam LcdHeight = 320;

    localparam CameraWidth = 752;
    localparam CameraHeight = 482;


    localparam ImageWidth = 320;
    localparam ImageHeight = 200;
    localparam ImageXInitial = ( LcdWidth - ImageWidth ) / 2;
    localparam ImageYInitial = ( LcdHeight - ImageHeight ) / 2;

    localparam BlankingWidth = 10;
    localparam CoordinateWidth = 10;
    localparam CameraPixelWidth = 10;

    reg  camera_configure;
    reg  camera_start;
    reg  camera_stop;

    wire camera_configuring;
    wire camera_idle;
    wire camera_running;
    wire camera_error;
    wire camera_busy;
    wire camera_image_transfer;










	wire [7:0] debug;




    reg refresh;





    wire sda;
    wire scl;

    wire camera_scl_out;
    wire camera_sda_out;

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

    wire       trigger;

	IB ib_hs( .I( genio[ 17 ] ), .O( hs ) );
	IB ib_vs( .I( genio[ 16 ] ), .O( vs ) );
	IB ib_pclk( .I( genio[ 14 ] ), .O( pclk ) );
	IB ib_led( .I( genio[ 1 ] ), .O( cled ) );

    IB ib_d0( .I( genio[  5 ] ), .O( d[ 0 ] ) );
    IB ib_d1( .I( genio[  4 ] ), .O( d[ 1 ] ) );
    IB ib_d2( .I( genio[  7 ] ), .O( d[ 2 ] ) );
    IB ib_d3( .I( genio[  6 ] ), .O( d[ 3 ] ) );
    IB ib_d4( .I( genio[  9 ] ), .O( d[ 4 ] ) );
    IB ib_d5( .I( genio[  8 ] ), .O( d[ 5 ] ) );
    IB ib_d6( .I( genio[ 11 ] ), .O( d[ 6 ] ) );
    IB ib_d7( .I( genio[ 10 ] ), .O( d[ 7 ] ) );
    IB ib_d8( .I( genio[ 13 ] ), .O( d[ 8 ] ) );
    IB ib_d9( .I( genio[ 12 ] ), .O( d[ 9 ] ) );

    OB ob_xclk( .I( xclk ), .O( genio[ 15 ] ) );
    OB  ob_rst( .I( rst ),  .O( genio[ 2 ] ) );
    OB ob_pwdn( .I( pwdn ),  .O( genio[ 3 ] ) );

    OB ob_trigger( .I( trigger ), .O( genio[ 0 ] ) );


    reg [CoordinateWidth-1:0] camera_image_x;
    reg [CoordinateWidth-1:0] camera_image_y;
    reg camera_image_origin_update;


    localparam [`IS_w-1:0] CameraIS =  `IS( 0, 0, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_GRAYSCALE, 10, 0, 0, 0, 0 );

    localparam ImagePixelCount = `IS_PIXEL_COUNT( CameraIS );

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

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

    wire [`I_w( CameraIS )-1:0 ] camera_image_out;
    reg                    camera_out_request_external;


    wire                 camera_image_out_start;
    wire                 camera_image_out_stop;
    wire [ImageDataWidth-1:0] camera_image_out_data;
    wire                 camera_image_out_valid;
    wire                 camera_image_out_error;
    wire                 camera_image_out_ready;
    wire                 camera_image_out_request;
    wire                 camera_image_out_cancel;


    assign camera_image_out_start = `I_Start( CameraIS, camera_image_out );
    assign camera_image_out_stop  = `I_Stop( CameraIS, camera_image_out );
    assign camera_image_out_data  = `I_Data( CameraIS, camera_image_out );
    assign camera_image_out_error = `I_Error( CameraIS, camera_image_out );
    assign camera_image_out_valid = `I_Valid( CameraIS, camera_image_out );

    assign camera_image_out_request = `I_Request( CameraIS, camera_image_out );
    assign camera_image_out_cancel  = `I_Cancel( CameraIS, camera_image_out );
    assign camera_image_out_ready   = `I_Ready( CameraIS, camera_image_out );

    camera_image #(
            .IS( CameraIS ),
            .CameraWidth( CameraWidth ),
            .CameraHeight( CameraHeight ),
            .ImageXInitial( ImageXInitial ),
            .ImageYInitial( ImageYInitial ),
            .I2CClockCount( 400 ),
			.I2CGapCount( ( 1 << 13 ) )
        ) cam_image (
            .clock( clock_48mhz),
            .reset( reset ),


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


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


            .image_x( camera_image_x ),
            .image_y( camera_image_y ),
            .image_origin_update( camera_image_origin_update ),


            .image_out( camera_image_out ),
            .out_request_external( camera_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( cled ),
            .trigger( trigger )


        );



	BB    clock_io( .I( camera_scl_out ), .T( 0 ), .O( scl ), .B( genio[18] ) );
	BBPU   data_io( .I( 0 ), .T( camera_sda_out), .O( sda ), .B( genio[19] ) );

    assign pmod[ 0 ] = refresh;
    assign pmod[ 1 ] = camera_image_out_valid;
    assign pmod[ 2 ] = image_fifo_out_valid;
    assign pmod[ 3 ] = image_fifo_out_ready;



    assign pmod[ 4 ] = debug[ 0 ];
    assign pmod[ 5 ] = debug[ 1 ];
    assign pmod[ 6 ] = debug[ 4 ];

    assign pmod[ 7 ] = lcd_wr;








    wire [`I_w( CameraIS )-1:0 ] image_fifo_to_reformat;

    image_fifo #( .InIS( CameraIS ), .OutIS( CameraIS ), .MemoryWidth( 9 ) ) i_f( clock_48mhz, reset, camera_image_out, image_fifo_to_reformat );

    wire                 image_fifo_out_valid;
    wire                 image_fifo_out_ready;

    assign image_fifo_out_valid = `I_Valid( CameraIS, image_fifo_to_reformat );
    assign image_fifo_out_ready   = `I_Ready( CameraIS, image_fifo_to_reformat );





    localparam [`IS_w-1:0] LcdIS =  `IS( ImageXInitial, ImageYInitial, ImageWidth, ImageHeight, 0, 1, `IS_FORMAT_RGB, 5, 6, 5, 0, 0 );

    wire [`I_w( LcdIS )-1:0 ] lcd_image_in;

    image_reformat #( .InIS( CameraIS ), .OutIS( LcdIS ) ) ir( clock_48mhz, reset, image_fifo_to_reformat, lcd_image_in );

    wire lcd_image_stop;
    assign lcd_image_stop = `I_Stop( LcdIS, lcd_image_in );

    wire lcd_image_valid;
    wire lcd_image_ready;

    assign lcd_image_valid = `I_Valid( LcdIS, lcd_image_in );
    assign lcd_image_ready = `I_Ready( LcdIS, lcd_image_in );





    `include "../../drivers/rtl/lcd_defs.v"

    wire lcd_configuring;
    wire lcd_running;
    wire lcd_busy;



    lcd_image #(
            .IS( LcdIS ),
        ) lc (
            .clock( clock_48mhz ),
            .reset( reset ),

            .image( lcd_image_in ),

            .configuring( lcd_configuring ),
            .running( lcd_running ),
            .busy( lcd_busy ),

            .refresh( refresh ),


            .lcd_db(lcd_db),
            .lcd_rd(lcd_rd),
            .lcd_wr(lcd_wr),
            .lcd_rs(lcd_rs),
            .lcd_cs(lcd_cs),
            .lcd_id(lcd_id),
            .lcd_rst(lcd_rst),
            .lcd_fmark(lcd_fmark),
            .lcd_blen(lcd_blen),

            .debug( debug )
        );






















	reg [2:0] clock_divided;

	always @( posedge clock_48mhz ) begin
		if ( reset )
			clock_divided <= 0;
		else
			clock_divided <= clock_divided + 1;
	end

	assign xclk = clock_divided[ 0 ];

	localparam CONTROL_POWER_DOWN = 0,
	           CONTROL_CONFIGURING = 1,
               CONTROL_CAMERA_SETUP = 2,
               CONTROL_CAMERA_SET_WINDOW = 3,
               CONTROL_CAMERA_SET_BLANKING = 4,
			   CONTROL_ERROR = 5,
			   CONTROL_RUNNING = 6;

	reg [3:0] control_state;

    localparam ControlTimerWidth = 24;
	localparam ControlTimerLongCount = 2**22;
	localparam ControlTimerShortCount = 10;

	reg [ ControlTimerWidth:0 ] control_timer;
	wire control_timer_expired = control_timer[ ControlTimerWidth ];

	always @( posedge clock_48mhz ) begin
		if ( reset ) begin
            camera_configure <= 0;
			camera_start <= 0;
			camera_stop <= 0;
			control_timer <= ControlTimerLongCount;
			control_state <= CONTROL_POWER_DOWN;
            camera_out_request_external <= 0;
    `ifdef BADGE_V2
            led[ 7:0 ] <= 0;
    `else
	        ledc[ 7:0 ] <= 0;
    `endif

		end else begin
			case ( control_state )
				CONTROL_POWER_DOWN: begin
						if ( control_timer_expired ) begin
							camera_configure <= 1;
							control_state <= CONTROL_CONFIGURING;
						end else begin
							control_timer <= control_timer - 1;
						end
					end
				CONTROL_CONFIGURING: begin


                        camera_configure <= 0;

                        if ( lcd_running ) begin

                            if ( camera_idle && !camera_busy ) begin
                                if ( control_timer_expired ) begin
                                    control_timer <= ControlTimerLongCount;
                                    control_state <= CONTROL_RUNNING;
                                    camera_start <= 1;
                                end else begin
                                    control_timer <= control_timer - 1;
                                end
                            end else begin

                                if ( camera_error ) begin
                                    camera_start <= 0;
                                    control_state <= CONTROL_ERROR;


        							control_timer <= ControlTimerLongCount;
                                end
                            end
                        end
					end
				CONTROL_RUNNING: begin

                        if ( control_timer_expired ) begin
                            if ( !camera_busy && !lcd_busy && ~btn[ 2 ] && ~camera_image_transfer ) begin
                                refresh <= 1;
                                control_timer <= ControlTimerLongCount;
                            end
                            camera_start <= ~btn[ 1 ];
                        end else begin
                            refresh <= 0;
                            control_timer <= control_timer - 1;
                        end
					end
				CONTROL_ERROR: begin
                        if ( control_timer_expired ) begin

                            if ( ~btn[ 0 ] ) begin
                                control_state <= CONTROL_POWER_DOWN;
                                control_timer <= ControlTimerLongCount;
                            end
                        end else begin
                            control_timer <= control_timer - 1;
                        end
                    end
			endcase
		end
	end

endmodule