data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v
None: at end of input
1: b'%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:78: 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/lcd_image.v:80: 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/lcd_image.v:126: 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/drivers/rtl/lcd_image.v:85: Define or directive not defined: \'`IS_w\'\n        parameter [`IS_w-1:0] IS = `IS_NULL,\n                   ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:85: Define or directive not defined: \'`IS_NULL\'\n        parameter [`IS_w-1:0] IS = `IS_NULL,\n                                   ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:85: syntax error, unexpected \',\', expecting TYPE-IDENTIFIER\n        parameter [`IS_w-1:0] IS = `IS_NULL,\n                                           ^\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:89: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdHeight          = 320,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:90: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdPixelRedWidth   = 5,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:91: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdPixelGreenWidth = 6,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:92: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdPixelBlueWidth  = 5,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:93: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdCoordinateWidth = 9,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:94: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdDataWidth       = 18,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:95: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdPixelWidth      = 16,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:96: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdCommandWidth    = 3,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:97: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdCommandDataTimerCount = 0,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:98: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter LcdDelayTimerCount = 10000,\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:99: syntax error, unexpected parameter, expecting IDENTIFIER\n        parameter TimeOutCount       = 4000000\n        ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:100: syntax error, unexpected \')\', expecting \';\'\n    ) (\n    ^\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:104: Define or directive not defined: \'`I_w\'\n        inout [`I_w(IS)-1:0] image,\n               ^~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:129: Define or directive not defined: \'`IS_w\'\n    parameter [`IS_w-1:0] LcdIs = `IS( 0, 0, LcdWidth, LcdHeight, 0, 1,`IS_FORMAT_RGB, LcdPixelRedWidth, LcdPixelGreenWidth, LcdPixelBlueWidth, 0, 0 );\n               ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:129: Define or directive not defined: \'`IS\'\n    parameter [`IS_w-1:0] LcdIs = `IS( 0, 0, LcdWidth, LcdHeight, 0, 1,`IS_FORMAT_RGB, LcdPixelRedWidth, LcdPixelGreenWidth, LcdPixelBlueWidth, 0, 0 );\n                                  ^~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:129: syntax error, unexpected \',\'\n    parameter [`IS_w-1:0] LcdIs = `IS( 0, 0, LcdWidth, LcdHeight, 0, 1,`IS_FORMAT_RGB, LcdPixelRedWidth, LcdPixelGreenWidth, LcdPixelBlueWidth, 0, 0 );\n                                        ^\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:129: Define or directive not defined: \'`IS_FORMAT_RGB\'\n    parameter [`IS_w-1:0] LcdIs = `IS( 0, 0, LcdWidth, LcdHeight, 0, 1,`IS_FORMAT_RGB, LcdPixelRedWidth, LcdPixelGreenWidth, LcdPixelBlueWidth, 0, 0 );\n                                                                       ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:131: Define or directive not defined: \'`IS_WIDTH_WIDTH\'\n    localparam LcdWidthWidth = `IS_WIDTH_WIDTH( LcdIs );\n                               ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:132: Define or directive not defined: \'`IS_HEIGHT_WIDTH\'\n    localparam LcdHeightWidth = `IS_HEIGHT_WIDTH( LcdIs );\n                                ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:134: Define or directive not defined: \'`I_Data_w\'\n    localparam NullDataWidth = `I_Data_w( `IS_NULL );\n                               ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:134: Define or directive not defined: \'`IS_NULL\'\n    localparam NullDataWidth = `I_Data_w( `IS_NULL );\n                                          ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:134: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n    localparam NullDataWidth = `I_Data_w( `IS_NULL );\n                                                   ^\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:140: Define or directive not defined: \'`I_Data_w\'\n    localparam ImageDataWidth = `I_Data_w( IS );\n                                ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:141: Define or directive not defined: \'`IS_PIXEL_COUNT\'\n    localparam ImageTransferSize = `IS_PIXEL_COUNT( IS );\n                                   ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:142: Define or directive not defined: \'`IS_PIXEL_COUNT_WIDTH\'\n    localparam ImageTransferSizeWidth = `IS_PIXEL_COUNT_WIDTH( IS );\n                                        ^~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:144: Define or directive not defined: \'`IS_X\'\n    localparam ImageRectX0 = `IS_X( IS );\n                             ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:145: Define or directive not defined: \'`IS_X\'\n    localparam ImageRectX1 = `IS_X( IS ) + `IS_WIDTH( IS ) - 1;\n                             ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:145: Define or directive not defined: \'`IS_WIDTH\'\n    localparam ImageRectX1 = `IS_X( IS ) + `IS_WIDTH( IS ) - 1;\n                                           ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:146: Define or directive not defined: \'`IS_Y\'\n    localparam ImageRectY0 = `IS_Y( IS );\n                             ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:147: Define or directive not defined: \'`IS_Y\'\n    localparam ImageRectY1 = `IS_Y( IS ) + `IS_HEIGHT( IS ) - 1;\n                             ^~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:147: Define or directive not defined: \'`IS_HEIGHT\'\n    localparam ImageRectY1 = `IS_Y( IS ) + `IS_HEIGHT( IS ) - 1;\n                                           ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:159: syntax error, unexpected assign\n    assign image_in_start   = `I_Start( IS, image );\n    ^~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:159: Define or directive not defined: \'`I_Start\'\n    assign image_in_start   = `I_Start( IS, image );\n                              ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:160: Define or directive not defined: \'`I_Stop\'\n    assign image_in_stop    = `I_Stop( IS, image );\n                              ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:161: Define or directive not defined: \'`I_Data\'\n    assign image_in_data    = `I_Data( IS, image );\n                              ^~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:162: Define or directive not defined: \'`I_Error\'\n                                                                            : ... Suggested alternative: \'`error\'\n    assign image_in_error   = `I_Error( IS, image );\n                              ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:163: Define or directive not defined: \'`I_Valid\'\n    assign image_in_valid   = `I_Valid( IS, image );\n                              ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:165: Define or directive not defined: \'`I_Request\'\n    assign `I_Request( IS, image ) = image_in_request;\n           ^~~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:166: Define or directive not defined: \'`I_Cancel\'\n    assign `I_Cancel( IS, image )  = image_in_cancel;\n           ^~~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:167: Define or directive not defined: \'`I_Ready\'\n    assign `I_Ready( IS, image )   = image_in_ready;\n           ^~~~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:195: syntax error, unexpected IDENTIFIER\n    lcd #(\n    ^~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:250: syntax error, unexpected assign\n    assign configuring = li_configuring;\n    ^~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:269: syntax error, unexpected assign\n    assign debug[3:0] = li_state;\n    ^~~~~~\n%Error: data/full_repos/permissive/190755935/src/drivers/rtl/lcd_image.v:284: syntax error, unexpected always\n    always @( posedge clock ) begin\n    ^~~~~~\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module lcd_image #(


        parameter [`IS_w-1:0] IS = `IS_NULL,


        parameter LcdWidth           = 480,
        parameter LcdHeight          = 320,
        parameter LcdPixelRedWidth   = 5,
        parameter LcdPixelGreenWidth = 6,
        parameter LcdPixelBlueWidth  = 5,
        parameter LcdCoordinateWidth = 9,
        parameter LcdDataWidth       = 18,
        parameter LcdPixelWidth      = 16,
        parameter LcdCommandWidth    = 3,
        parameter LcdCommandDataTimerCount = 0,
        parameter LcdDelayTimerCount = 10000,
        parameter TimeOutCount       = 4000000
    ) (
        input clock,
        input reset,

        inout [`I_w(IS)-1:0] image,

        output configuring,
        output running,
        output busy,

        input refresh,


        output  [LcdDataWidth-1: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 [7:0] debug
    );

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


    parameter [`IS_w-1:0] LcdIs = `IS( 0, 0, LcdWidth, LcdHeight, 0, 1,`IS_FORMAT_RGB, LcdPixelRedWidth, LcdPixelGreenWidth, LcdPixelBlueWidth, 0, 0 );

    localparam LcdWidthWidth = `IS_WIDTH_WIDTH( LcdIs );
    localparam LcdHeightWidth = `IS_HEIGHT_WIDTH( LcdIs );

    localparam NullDataWidth = `I_Data_w( `IS_NULL );





    localparam ImageDataWidth = `I_Data_w( IS );
    localparam ImageTransferSize = `IS_PIXEL_COUNT( IS );
    localparam ImageTransferSizeWidth = `IS_PIXEL_COUNT_WIDTH( IS );

    localparam ImageRectX0 = `IS_X( IS );
    localparam ImageRectX1 = `IS_X( IS ) + `IS_WIDTH( IS ) - 1;
    localparam ImageRectY0 = `IS_Y( IS );
    localparam ImageRectY1 = `IS_Y( IS ) + `IS_HEIGHT( IS ) - 1;

    wire                      image_in_start;
    wire                      image_in_stop;
    wire [ImageDataWidth-1:0] image_in_data;
    wire                      image_in_valid;
    wire                      image_in_error;

    reg                       image_in_ready;
    reg                       image_in_request;
    reg                       image_in_cancel;

    assign image_in_start   = `I_Start( IS, image );
    assign image_in_stop    = `I_Stop( IS, image );
    assign image_in_data    = `I_Data( IS, image );
    assign image_in_error   = `I_Error( IS, image );
    assign image_in_valid   = `I_Valid( IS, image );

    assign `I_Request( IS, image ) = image_in_request;
    assign `I_Cancel( IS, image )  = image_in_cancel;
    assign `I_Ready( IS, image )   = image_in_ready;

    reg [LcdWidthWidth-1:0]  image_rect_x0;
    reg [LcdWidthWidth-1:0]  image_rect_x1;
    reg [LcdHeightWidth-1:0] image_rect_y0;
    reg [LcdHeightWidth-1:0] image_rect_y1;







    reg [LcdCommandWidth-1:0]     lcd_command;
    reg                           lcd_abort;
    wire                          lcd_ready;
    reg [LcdPixelWidth-1:0]       lcd_fill_pixel;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_x0;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_x1;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_y0;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_y1;
    reg [LcdPixelWidth-1:0]       lcd_rect_pixel_write;
    reg                           lcd_rect_pixel_write_valid;
    wire                          lcd_rect_pixel_write_ready;
    wire [LcdPixelWidth-1:0]      lcd_rect_pixel_read;
    wire                          lcd_rect_pixel_read_valid;
    reg                           lcd_rect_pixel_read_ready;

    lcd #(
            .Width( LcdWidth ),
            .Height( LcdHeight ),
            .CoordinateWidth( LcdCoordinateWidth ),
            .CommandWidth( LcdCommandWidth ),
            .DataWidth( LcdDataWidth ),
            .PixelWidth( LcdPixelWidth ),
            .PixelRedWidth( LcdPixelRedWidth ),
            .PixelGreenWidth( LcdPixelGreenWidth ),
            .PixelBlueWidth( LcdPixelBlueWidth ),
            .CommandDataTimerCount( LcdCommandDataTimerCount ),
            .DelayTimerCount( LcdDelayTimerCount )
        ) l (
            .clock( clock ),
            .reset( reset ),


            .command( lcd_command ),
            .abort( lcd_abort ),
            .ready( lcd_ready ),
            .fill_pixel( lcd_fill_pixel ),
            .rect_x0( lcd_rect_x0 ),
            .rect_x1( lcd_rect_x1 ),
            .rect_y0( lcd_rect_y0 ),
            .rect_y1( lcd_rect_y1 ),


            .rect_pixel_write( lcd_rect_pixel_write ),
            .rect_pixel_write_valid( lcd_rect_pixel_write_valid ),
            .rect_pixel_write_ready( lcd_rect_pixel_write_ready ),
            .rect_pixel_read( lcd_rect_pixel_read ),
            .rect_pixel_read_valid( lcd_rect_pixel_read_valid ),
            .rect_pixel_read_ready( lcd_rect_pixel_read_ready ),


            .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)
        );






    reg li_configuring;
    reg li_running;
    reg li_busy;

    assign configuring = li_configuring;
    assign running = li_running;
    assign busy = li_busy;

    localparam LI_STATE_START          = 0,
               LI_STATE_CONFIGURE      = 1,
               LI_STATE_IDLE           = 2,
               LI_STATE_REFRESH        = 3,
               LI_STATE_START_TRANSFER = 4,
               LI_STATE_TRANSFER       = 5,
               LI_STATE_STARVE         = 6,
               LI_STATE_STALL          = 7,
               LI_STATE_OVERFLOW       = 8,
               LI_STATE_END_TRANSFER   = 9,
               LI_STATE_ABORT_TRANSFER = 10,
               LI_STATE_BUSY           = 11;

    reg [3:0]  li_state;

    assign debug[3:0] = li_state;
    assign debug[4] = ( image_transfer_count_next == ImageTransferSize );
    assign debug[5] = lcd_rect_pixel_write_ready;

    reg  [ImageTransferSizeWidth:0] image_transfer_count;
    wire [ImageTransferSizeWidth:0] image_transfer_count_next = image_transfer_count + 1;

    localparam TimeOutCountWidth = $clog2( TimeOutCount + 1 ) + 1;
    localparam TimeOutCountShort = 4;

    reg [TimeOutCountWidth:0] li_timeout_counter;
    wire li_timeout_expired = li_timeout_counter[ TimeOutCountWidth ];

    reg [ImageDataWidth-1:0] image_in_data_overflow;

    always @( posedge clock ) begin

        if ( reset ) begin

            li_configuring <= 0;
            li_running <= 0;
            li_busy <= 0;

            li_state <= LI_STATE_START;
            lcd_command <= LCD_COMMAND_NONE;
            lcd_abort <= 0;

            lcd_fill_pixel <= 0;

            lcd_rect_x0 <= 0;
            lcd_rect_x1 <= 0;
            lcd_rect_y0 <= 0;
            lcd_rect_y1 <= 0;
            lcd_rect_pixel_write <= 0;
            lcd_rect_pixel_write_valid <= 0;

            lcd_rect_pixel_read_ready <= 0;

            image_in_ready <= 0;
            image_in_request <= 0;
            image_in_cancel <= 0;

            image_transfer_count <= 0;

            li_timeout_counter <= 0;

        end else begin
            case ( li_state )
                LI_STATE_START: begin
                        lcd_command <= LCD_COMMAND_CONFIGURE;
                        li_state <= LI_STATE_CONFIGURE;
                        li_configuring <= 1;
                        li_busy <= 1;
                        li_timeout_counter <= TimeOutCountShort;
                    end
                LI_STATE_CONFIGURE: begin
                        if ( li_timeout_expired ) begin
                            if ( lcd_ready ) begin
                                lcd_fill_pixel <= 1;
                                lcd_rect_x0 <= 0;
                                lcd_rect_x1 <= LcdWidth - 1;
                                lcd_rect_y0 <= 0;
                                lcd_rect_y1 <= LcdHeight - 1;
                                lcd_command <= LCD_COMMAND_FILL_RECT;
                                li_timeout_counter <= TimeOutCountShort;
                                li_state <= LI_STATE_BUSY;
                            end else begin
                                lcd_command <= LCD_COMMAND_NONE;
                            end
                        end else begin
                            li_timeout_counter <= li_timeout_counter - 1;
                        end
                    end
                LI_STATE_IDLE: begin
                        li_configuring <= 0;
                        li_running <= 1;

                        lcd_abort <= 0;
                        image_in_cancel <= 0;

                        if ( refresh ) begin
                            li_busy <= 1;
                            li_state <= LI_STATE_REFRESH;
                        end else begin
                            li_busy <= 0;
                        end
                    end
                LI_STATE_REFRESH: begin
                        lcd_rect_x0 <= ImageRectX0;
                        lcd_rect_x1 <= ImageRectX1;
                        lcd_rect_y0 <= ImageRectY0;
                        lcd_rect_y1 <= ImageRectY1;
                        lcd_command <= LCD_COMMAND_WRITE_RECT;
                        image_in_ready <= 0;
                        image_in_request <= 1;
                        li_state <= LI_STATE_START_TRANSFER;
                    end
                LI_STATE_START_TRANSFER: begin
                        lcd_command <=  LCD_COMMAND_NONE;
                        image_in_request <= 0;

                        if ( lcd_rect_pixel_write_ready ) begin
                            image_in_ready <= 1;
                            image_transfer_count <= 0;
                            lcd_rect_pixel_write_valid <= 0;
                            li_timeout_counter <= TimeOutCount;
                            li_state <= LI_STATE_TRANSFER;

                        end
                    end
                LI_STATE_TRANSFER: begin
                        if ( image_in_stop || !li_timeout_expired ) begin
                            if ( image_in_valid && ( ( image_transfer_count != 0 ) || image_in_start ) ) begin
                                li_timeout_counter <= TimeOutCount;
                                if ( lcd_rect_pixel_write_ready || !lcd_rect_pixel_write_valid ) begin
                                    lcd_rect_pixel_write <= image_in_data;
                                    lcd_rect_pixel_write_valid <= 1;
                                    if ( image_transfer_count_next == ImageTransferSize ) begin
                                        image_transfer_count <= 0;
                                        image_in_ready <= 0;
                                        li_state <= LI_STATE_END_TRANSFER;
                                    end else begin
                                        image_transfer_count <= image_transfer_count_next;
                                    end
                                end else begin
                                    image_in_ready <= 0;
                                    image_in_data_overflow <= image_in_data;
                                    li_state <= LI_STATE_OVERFLOW;
                                end
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                                image_in_ready <= 0;
                                if ( ~lcd_rect_pixel_write_ready ) begin
                                    li_state <= LI_STATE_STALL;
                                end else begin
                                    lcd_rect_pixel_write <= 0;
                                    lcd_rect_pixel_write_valid <= 0;
                                    li_state <= LI_STATE_STARVE;
                                end
                            end
                        end else begin

                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_STARVE: begin
                        if ( !li_timeout_expired ) begin
                            if ( image_in_valid ) begin
                                image_in_ready <= 1;
                                li_timeout_counter <= TimeOutCount;
                                li_state <= LI_STATE_TRANSFER;
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end
                        end else begin
                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_STALL: begin
                        if ( !li_timeout_expired ) begin
                            if ( lcd_rect_pixel_write_ready ) begin
                                li_timeout_counter <= TimeOutCount;
                                li_state <= LI_STATE_TRANSFER;
                                image_in_ready <= 1;
                                lcd_rect_pixel_write <= 0;
                                lcd_rect_pixel_write_valid <= 0;
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end
                        end else begin
                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_OVERFLOW: begin
                        if ( !li_timeout_expired ) begin
                            if ( lcd_rect_pixel_write_ready ) begin
                                lcd_rect_pixel_write <= image_in_data_overflow;
                                if ( image_transfer_count_next == ImageTransferSize ) begin
                                    image_transfer_count <= 0;
                                    image_in_ready <= 0;
                                    li_state <= LI_STATE_END_TRANSFER;
                                end else begin
                                    image_in_ready <= 1;
                                    image_transfer_count <= image_transfer_count_next;
                                    li_state <= LI_STATE_TRANSFER;
                                end
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end
                        end else begin
                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_END_TRANSFER: begin
                        if ( lcd_rect_pixel_write_ready ) begin
                            li_timeout_counter <= TimeOutCount;
                            lcd_rect_pixel_write <= 0;
                            image_in_ready <= 0;
                            lcd_rect_pixel_write_valid <= 0;
                            li_busy <= 0;
                            li_state <= LI_STATE_IDLE;
                        end else begin
                            if ( !li_timeout_expired ) begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end else begin
                                li_state <= LI_STATE_ABORT_TRANSFER;
                            end
                        end
                    end
                LI_STATE_ABORT_TRANSFER: begin
                        lcd_rect_pixel_write <= 0;
                        lcd_rect_pixel_write_valid <= 0;
                        image_transfer_count <= 0;
                        image_in_ready <= 0;
                        lcd_abort <= 1;
                        image_in_cancel <= 1;
                        li_busy <= 0;
                        li_state <= LI_STATE_IDLE;
                    end
                LI_STATE_BUSY: begin
                        lcd_command <= LCD_COMMAND_NONE;
                        if ( li_timeout_expired ) begin
                            if ( lcd_ready ) begin
                                li_state <= LI_STATE_IDLE;
                            end
                        end else begin
                            li_timeout_counter <= li_timeout_counter - 1;
                        end
                    end
            endcase
        end
    end

endmodule
module lcd_image #(


        parameter [`IS_w-1:0] IS = `IS_NULL,


        parameter LcdWidth           = 480,
        parameter LcdHeight          = 320,
        parameter LcdPixelRedWidth   = 5,
        parameter LcdPixelGreenWidth = 6,
        parameter LcdPixelBlueWidth  = 5,
        parameter LcdCoordinateWidth = 9,
        parameter LcdDataWidth       = 18,
        parameter LcdPixelWidth      = 16,
        parameter LcdCommandWidth    = 3,
        parameter LcdCommandDataTimerCount = 0,
        parameter LcdDelayTimerCount = 10000,
        parameter TimeOutCount       = 4000000
    ) (
        input clock,
        input reset,

        inout [`I_w(IS)-1:0] image,

        output configuring,
        output running,
        output busy,

        input refresh,


        output  [LcdDataWidth-1: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 [7:0] debug
    );


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


    parameter [`IS_w-1:0] LcdIs = `IS( 0, 0, LcdWidth, LcdHeight, 0, 1,`IS_FORMAT_RGB, LcdPixelRedWidth, LcdPixelGreenWidth, LcdPixelBlueWidth, 0, 0 );

    localparam LcdWidthWidth = `IS_WIDTH_WIDTH( LcdIs );
    localparam LcdHeightWidth = `IS_HEIGHT_WIDTH( LcdIs );

    localparam NullDataWidth = `I_Data_w( `IS_NULL );





    localparam ImageDataWidth = `I_Data_w( IS );
    localparam ImageTransferSize = `IS_PIXEL_COUNT( IS );
    localparam ImageTransferSizeWidth = `IS_PIXEL_COUNT_WIDTH( IS );

    localparam ImageRectX0 = `IS_X( IS );
    localparam ImageRectX1 = `IS_X( IS ) + `IS_WIDTH( IS ) - 1;
    localparam ImageRectY0 = `IS_Y( IS );
    localparam ImageRectY1 = `IS_Y( IS ) + `IS_HEIGHT( IS ) - 1;

    wire                      image_in_start;
    wire                      image_in_stop;
    wire [ImageDataWidth-1:0] image_in_data;
    wire                      image_in_valid;
    wire                      image_in_error;

    reg                       image_in_ready;
    reg                       image_in_request;
    reg                       image_in_cancel;

    assign image_in_start   = `I_Start( IS, image );
    assign image_in_stop    = `I_Stop( IS, image );
    assign image_in_data    = `I_Data( IS, image );
    assign image_in_error   = `I_Error( IS, image );
    assign image_in_valid   = `I_Valid( IS, image );

    assign `I_Request( IS, image ) = image_in_request;
    assign `I_Cancel( IS, image )  = image_in_cancel;
    assign `I_Ready( IS, image )   = image_in_ready;

    reg [LcdWidthWidth-1:0]  image_rect_x0;
    reg [LcdWidthWidth-1:0]  image_rect_x1;
    reg [LcdHeightWidth-1:0] image_rect_y0;
    reg [LcdHeightWidth-1:0] image_rect_y1;







    reg [LcdCommandWidth-1:0]     lcd_command;
    reg                           lcd_abort;
    wire                          lcd_ready;
    reg [LcdPixelWidth-1:0]       lcd_fill_pixel;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_x0;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_x1;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_y0;
    reg [LcdCoordinateWidth-1:0]  lcd_rect_y1;
    reg [LcdPixelWidth-1:0]       lcd_rect_pixel_write;
    reg                           lcd_rect_pixel_write_valid;
    wire                          lcd_rect_pixel_write_ready;
    wire [LcdPixelWidth-1:0]      lcd_rect_pixel_read;
    wire                          lcd_rect_pixel_read_valid;
    reg                           lcd_rect_pixel_read_ready;

    lcd #(
            .Width( LcdWidth ),
            .Height( LcdHeight ),
            .CoordinateWidth( LcdCoordinateWidth ),
            .CommandWidth( LcdCommandWidth ),
            .DataWidth( LcdDataWidth ),
            .PixelWidth( LcdPixelWidth ),
            .PixelRedWidth( LcdPixelRedWidth ),
            .PixelGreenWidth( LcdPixelGreenWidth ),
            .PixelBlueWidth( LcdPixelBlueWidth ),
            .CommandDataTimerCount( LcdCommandDataTimerCount ),
            .DelayTimerCount( LcdDelayTimerCount )
        ) l (
            .clock( clock ),
            .reset( reset ),


            .command( lcd_command ),
            .abort( lcd_abort ),
            .ready( lcd_ready ),
            .fill_pixel( lcd_fill_pixel ),
            .rect_x0( lcd_rect_x0 ),
            .rect_x1( lcd_rect_x1 ),
            .rect_y0( lcd_rect_y0 ),
            .rect_y1( lcd_rect_y1 ),


            .rect_pixel_write( lcd_rect_pixel_write ),
            .rect_pixel_write_valid( lcd_rect_pixel_write_valid ),
            .rect_pixel_write_ready( lcd_rect_pixel_write_ready ),
            .rect_pixel_read( lcd_rect_pixel_read ),
            .rect_pixel_read_valid( lcd_rect_pixel_read_valid ),
            .rect_pixel_read_ready( lcd_rect_pixel_read_ready ),


            .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)
        );






    reg li_configuring;
    reg li_running;
    reg li_busy;

    assign configuring = li_configuring;
    assign running = li_running;
    assign busy = li_busy;

    localparam LI_STATE_START          = 0,
               LI_STATE_CONFIGURE      = 1,
               LI_STATE_IDLE           = 2,
               LI_STATE_REFRESH        = 3,
               LI_STATE_START_TRANSFER = 4,
               LI_STATE_TRANSFER       = 5,
               LI_STATE_STARVE         = 6,
               LI_STATE_STALL          = 7,
               LI_STATE_OVERFLOW       = 8,
               LI_STATE_END_TRANSFER   = 9,
               LI_STATE_ABORT_TRANSFER = 10,
               LI_STATE_BUSY           = 11;

    reg [3:0]  li_state;

    assign debug[3:0] = li_state;
    assign debug[4] = ( image_transfer_count_next == ImageTransferSize );
    assign debug[5] = lcd_rect_pixel_write_ready;

    reg  [ImageTransferSizeWidth:0] image_transfer_count;
    wire [ImageTransferSizeWidth:0] image_transfer_count_next = image_transfer_count + 1;

    localparam TimeOutCountWidth = $clog2( TimeOutCount + 1 ) + 1;
    localparam TimeOutCountShort = 4;

    reg [TimeOutCountWidth:0] li_timeout_counter;
    wire li_timeout_expired = li_timeout_counter[ TimeOutCountWidth ];

    reg [ImageDataWidth-1:0] image_in_data_overflow;

    always @( posedge clock ) begin

        if ( reset ) begin

            li_configuring <= 0;
            li_running <= 0;
            li_busy <= 0;

            li_state <= LI_STATE_START;
            lcd_command <= LCD_COMMAND_NONE;
            lcd_abort <= 0;

            lcd_fill_pixel <= 0;

            lcd_rect_x0 <= 0;
            lcd_rect_x1 <= 0;
            lcd_rect_y0 <= 0;
            lcd_rect_y1 <= 0;
            lcd_rect_pixel_write <= 0;
            lcd_rect_pixel_write_valid <= 0;

            lcd_rect_pixel_read_ready <= 0;

            image_in_ready <= 0;
            image_in_request <= 0;
            image_in_cancel <= 0;

            image_transfer_count <= 0;

            li_timeout_counter <= 0;

        end else begin
            case ( li_state )
                LI_STATE_START: begin
                        lcd_command <= LCD_COMMAND_CONFIGURE;
                        li_state <= LI_STATE_CONFIGURE;
                        li_configuring <= 1;
                        li_busy <= 1;
                        li_timeout_counter <= TimeOutCountShort;
                    end
                LI_STATE_CONFIGURE: begin
                        if ( li_timeout_expired ) begin
                            if ( lcd_ready ) begin
                                lcd_fill_pixel <= 1;
                                lcd_rect_x0 <= 0;
                                lcd_rect_x1 <= LcdWidth - 1;
                                lcd_rect_y0 <= 0;
                                lcd_rect_y1 <= LcdHeight - 1;
                                lcd_command <= LCD_COMMAND_FILL_RECT;
                                li_timeout_counter <= TimeOutCountShort;
                                li_state <= LI_STATE_BUSY;
                            end else begin
                                lcd_command <= LCD_COMMAND_NONE;
                            end
                        end else begin
                            li_timeout_counter <= li_timeout_counter - 1;
                        end
                    end
                LI_STATE_IDLE: begin
                        li_configuring <= 0;
                        li_running <= 1;

                        lcd_abort <= 0;
                        image_in_cancel <= 0;

                        if ( refresh ) begin
                            li_busy <= 1;
                            li_state <= LI_STATE_REFRESH;
                        end else begin
                            li_busy <= 0;
                        end
                    end
                LI_STATE_REFRESH: begin
                        lcd_rect_x0 <= ImageRectX0;
                        lcd_rect_x1 <= ImageRectX1;
                        lcd_rect_y0 <= ImageRectY0;
                        lcd_rect_y1 <= ImageRectY1;
                        lcd_command <= LCD_COMMAND_WRITE_RECT;
                        image_in_ready <= 0;
                        image_in_request <= 1;
                        li_state <= LI_STATE_START_TRANSFER;
                    end
                LI_STATE_START_TRANSFER: begin
                        lcd_command <=  LCD_COMMAND_NONE;
                        image_in_request <= 0;

                        if ( lcd_rect_pixel_write_ready ) begin
                            image_in_ready <= 1;
                            image_transfer_count <= 0;
                            lcd_rect_pixel_write_valid <= 0;
                            li_timeout_counter <= TimeOutCount;
                            li_state <= LI_STATE_TRANSFER;

                        end
                    end
                LI_STATE_TRANSFER: begin
                        if ( image_in_stop || !li_timeout_expired ) begin
                            if ( image_in_valid && ( ( image_transfer_count != 0 ) || image_in_start ) ) begin
                                li_timeout_counter <= TimeOutCount;
                                if ( lcd_rect_pixel_write_ready || !lcd_rect_pixel_write_valid ) begin
                                    lcd_rect_pixel_write <= image_in_data;
                                    lcd_rect_pixel_write_valid <= 1;
                                    if ( image_transfer_count_next == ImageTransferSize ) begin
                                        image_transfer_count <= 0;
                                        image_in_ready <= 0;
                                        li_state <= LI_STATE_END_TRANSFER;
                                    end else begin
                                        image_transfer_count <= image_transfer_count_next;
                                    end
                                end else begin
                                    image_in_ready <= 0;
                                    image_in_data_overflow <= image_in_data;
                                    li_state <= LI_STATE_OVERFLOW;
                                end
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                                image_in_ready <= 0;
                                if ( ~lcd_rect_pixel_write_ready ) begin
                                    li_state <= LI_STATE_STALL;
                                end else begin
                                    lcd_rect_pixel_write <= 0;
                                    lcd_rect_pixel_write_valid <= 0;
                                    li_state <= LI_STATE_STARVE;
                                end
                            end
                        end else begin

                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_STARVE: begin
                        if ( !li_timeout_expired ) begin
                            if ( image_in_valid ) begin
                                image_in_ready <= 1;
                                li_timeout_counter <= TimeOutCount;
                                li_state <= LI_STATE_TRANSFER;
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end
                        end else begin
                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_STALL: begin
                        if ( !li_timeout_expired ) begin
                            if ( lcd_rect_pixel_write_ready ) begin
                                li_timeout_counter <= TimeOutCount;
                                li_state <= LI_STATE_TRANSFER;
                                image_in_ready <= 1;
                                lcd_rect_pixel_write <= 0;
                                lcd_rect_pixel_write_valid <= 0;
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end
                        end else begin
                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_OVERFLOW: begin
                        if ( !li_timeout_expired ) begin
                            if ( lcd_rect_pixel_write_ready ) begin
                                lcd_rect_pixel_write <= image_in_data_overflow;
                                if ( image_transfer_count_next == ImageTransferSize ) begin
                                    image_transfer_count <= 0;
                                    image_in_ready <= 0;
                                    li_state <= LI_STATE_END_TRANSFER;
                                end else begin
                                    image_in_ready <= 1;
                                    image_transfer_count <= image_transfer_count_next;
                                    li_state <= LI_STATE_TRANSFER;
                                end
                            end else begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end
                        end else begin
                            li_state <= LI_STATE_ABORT_TRANSFER;
                        end
                    end
                LI_STATE_END_TRANSFER: begin
                        if ( lcd_rect_pixel_write_ready ) begin
                            li_timeout_counter <= TimeOutCount;
                            lcd_rect_pixel_write <= 0;
                            image_in_ready <= 0;
                            lcd_rect_pixel_write_valid <= 0;
                            li_busy <= 0;
                            li_state <= LI_STATE_IDLE;
                        end else begin
                            if ( !li_timeout_expired ) begin
                                li_timeout_counter <= li_timeout_counter - 1;
                            end else begin
                                li_state <= LI_STATE_ABORT_TRANSFER;
                            end
                        end
                    end
                LI_STATE_ABORT_TRANSFER: begin
                        lcd_rect_pixel_write <= 0;
                        lcd_rect_pixel_write_valid <= 0;
                        image_transfer_count <= 0;
                        image_in_ready <= 0;
                        lcd_abort <= 1;
                        image_in_cancel <= 1;
                        li_busy <= 0;
                        li_state <= LI_STATE_IDLE;
                    end
                LI_STATE_BUSY: begin
                        lcd_command <= LCD_COMMAND_NONE;
                        if ( li_timeout_expired ) begin
                            if ( lcd_ready ) begin
                                li_state <= LI_STATE_IDLE;
                            end
                        end else begin
                            li_timeout_counter <= li_timeout_counter - 1;
                        end
                    end
            endcase
        end
    end

endmodule