data/full_repos/permissive/163728837/src/sprite.v
 line:381: before: "signed"
1: b'%Error: data/full_repos/permissive/163728837/src/sprite.v:46: Cannot find file containing module: \'simple_dual_ram\'\nsimple_dual_ram #(.SIZE(40), .DEPTH(SPRITE_COUNT)) smeta_ram (\n^~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/163728837/src,data/full_repos/permissive/163728837/simple_dual_ram\n             data/full_repos/permissive/163728837/src,data/full_repos/permissive/163728837/simple_dual_ram.v\n             data/full_repos/permissive/163728837/src,data/full_repos/permissive/163728837/simple_dual_ram.sv\n             simple_dual_ram\n             simple_dual_ram.v\n             simple_dual_ram.sv\n             obj_dir/simple_dual_ram\n             obj_dir/simple_dual_ram.v\n             obj_dir/simple_dual_ram.sv\n%Error: data/full_repos/permissive/163728837/src/sprite.v:63: Cannot find file containing module: \'simple_dual_ram\'\nsimple_dual_ram #(.SIZE(120), .DEPTH(SPRITE_COUNT)) stmeta_ram (\n^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/163728837/src/sprite.v:77: Cannot find file containing module: \'idist_rom\'\nidist_rom idist_rom (\n^~~~~~~~~\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:185: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s VARREF \'map_pos_x\' generates 16 bits.\n                                                                     : ... In instance sprite\n                map_posx_d = map_pos_x <<< 8;\n                                       ^~~\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:186: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s VARREF \'map_pos_y\' generates 16 bits.\n                                                                     : ... In instance sprite\n                map_posy_d = map_pos_y <<< 8;\n                                       ^~~\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:209: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s SEL generates 16 bits.\n                                                                     : ... In instance sprite\n            spritex_d = (smeta_read_data[31:16] << 8) - map_posx_q;\n                                                ^~\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:210: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s SEL generates 16 bits.\n                                                                     : ... In instance sprite\n            spritey_d = (smeta_read_data[15:0] << 8) - map_posy_q;\n                                               ^~\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:280: Operator ASSIGN expects 10 bits on the Assign RHS, but Assign RHS\'s SEL generates 12 bits.\n                                                                     : ... In instance sprite\n            idist_addr = mul_res_q[21:10];\n                       ^\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:285: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s VARREF \'idist\' generates 16 bits.\n                                                                     : ... In instance sprite\n            mul_arg2_d = idist << 8;  \n                               ^~\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:299: Operator ASSIGN expects 16 bits on the Assign RHS, but Assign RHS\'s SHIFTRS generates 32 bits.\n                                                                     : ... In instance sprite\n            sprite_screenx_d = mul_res_q >>> 16;\n                             ^\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:311: Operator ASSIGN expects 16 bits on the Assign RHS, but Assign RHS\'s SHIFTR generates 32 bits.\n                                                                     : ... In instance sprite\n                sprite_height_d = (~mul_res_q) + 1\'b1 >> 16;\n                                ^\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:313: Operator ASSIGN expects 16 bits on the Assign RHS, but Assign RHS\'s SHIFTR generates 32 bits.\n                                                                     : ... In instance sprite\n                sprite_height_d = mul_res_q >> 16;\n                                ^\n%Warning-WIDTH: data/full_repos/permissive/163728837/src/sprite.v:332: Operator EQ expects 32 or 8 bits on the LHS, but LHS\'s VARREF \'count_q\' generates 7 bits.\n                                                                     : ... In instance sprite\n            if (count_q == SPRITE_COUNT-1) begin\n                        ^~\n%Error: Exiting due to 3 error(s), 10 warning(s)\n'
module sprite (
    input clk,
    input rst,

    input start,
    output reg done,

    input [79:0] angle,
    input [15:0] map_pos_x,
    input [15:0] map_pos_y,
    

    input [6:0] smeta_waddr,
    input [39:0] smeta_write_data,
    input smeta_write_en,

    input [6:0] stmeta_raddr,
    output [119:0] stmeta_read_data
);

localparam HEIGHT = 240,
    WIDTH = 320,
    HALF_HEIGHT = 120,
    HALF_WIDTH = 160,
    SPRITE_COUNT = 128;


reg [6:0] smeta_raddr;
wire [39:0] smeta_read_data;


simple_dual_ram #(.SIZE(40), .DEPTH(SPRITE_COUNT)) smeta_ram (
    .wclk(clk),
    .waddr(smeta_waddr),
    .write_data(smeta_write_data),
    .write_en(smeta_write_en),

    .rclk(clk),
    .raddr(smeta_raddr),
    .read_data(smeta_read_data)
);


reg [6:0] stmeta_waddr;
reg [119:0] stmeta_write_data;
reg stmeta_write_en;


simple_dual_ram #(.SIZE(120), .DEPTH(SPRITE_COUNT)) stmeta_ram (
    .wclk(clk),
    .waddr(stmeta_waddr),
    .write_data(stmeta_write_data),
    .write_en(stmeta_write_en),

    .rclk(clk),
    .raddr(stmeta_raddr),
    .read_data(stmeta_read_data)
);


reg [9:0] idist_addr;
wire [15:0] idist;
idist_rom idist_rom (
    .clk(clk),
    .addr(idist_addr),
    .idist(idist)
);

reg [4:0] state_d, state_q;
reg [4:0] next_state_d, next_state_q;
localparam IDLE = 0,
    MULTIPLY = 1,
    TRANSFORM_START = 2,
    TRANSFORMX_1 = 3,
    TRANSFORMX_2 = 4,
    TRANSFORMX_3 = 5,
    TRANSFORMX_4 = 6,
    TRANSFORMY_1 = 7,
    TRANSFORMY_2 = 8,
    TRANSFORMY_3 = 9,
    SPRITE_SCREENX = 10,
    SPRITE_SCREENX_2 = 11,
    SPRITE_SCREENX_3 = 12,
    SPRITE_HEIGHT = 13,
    SPRITE_DRAWBOUNDS = 14,
    SAVE_TRANSFORMS = 15;


reg signed [31:0] mul_res_d, mul_res_q;
reg signed [31:0] mul_arg1_d, mul_arg1_q;
reg signed [31:0] mul_arg2_d, mul_arg2_q;
reg signed [31:0] math_hold_d, math_hold_q;


reg signed [31:0] dirx_d, dirx_q;
reg signed [31:0] diry_d, diry_q;
reg signed [31:0] planex_d, planex_q;
reg signed [31:0] planey_d, planey_q;
reg signed [31:0] invdet_d, invdet_q;

reg signed [31:0] map_posx_d, map_posx_q;
reg signed [31:0] map_posy_d, map_posy_q;


reg signed [31:0] spritex_d, spritex_q;
reg signed [31:0] spritey_d, spritey_q;
reg signed [31:0] transformx_d, transformx_q;
reg signed [31:0] transformy_d, transformy_q;
reg signed [15:0] sprite_screenx_d, sprite_screenx_q;
reg signed [15:0] sprite_height_d, sprite_height_q;
reg signed [15:0] draw_starty_d, draw_starty_q;
reg signed [15:0] draw_endy_d, draw_endy_q;
reg signed [15:0] draw_startx_d, draw_startx_q;
reg signed [15:0] draw_endx_d, draw_endx_q;

reg [6:0] count_d, count_q;

always @(*) begin
    state_d = state_q;
    next_state_d = next_state_q;
    count_d = count_q;
    smeta_raddr = count_q;
    stmeta_waddr = count_q;
    stmeta_write_data = 120'd0;
    stmeta_write_en = 1'b0;
    idist_addr = 0;
    done = 1'b0;

    mul_res_d = mul_res_q;
    mul_arg1_d = mul_arg1_q;
    mul_arg2_d = mul_arg2_q;
    math_hold_d = math_hold_q;
    next_state_d = next_state_q;

    dirx_d = dirx_q;
    diry_d = diry_q;
    planex_d = planex_q;
    planey_d = planey_q;
    invdet_d = invdet_q;
    map_posx_d = map_posx_q;
    map_posy_d = map_posy_q;

    spritex_d = spritex_q;
    spritey_d = spritey_q;
    transformx_d = transformx_q;
    transformy_d = transformy_q;
    sprite_screenx_d = sprite_screenx_q;
    sprite_height_d = sprite_height_q;
    draw_starty_d = draw_starty_q;
    draw_endy_d = draw_endy_q;
    draw_startx_d = draw_startx_q;
    draw_endx_d = draw_endx_q;

    case (state_q)
        IDLE: begin
            if (start) begin



                dirx_d[31:16] = angle[79:64];
                dirx_d[15:0] = 0;
                diry_d[31:16] = angle[63:48];
                diry_d[15:0] = 0;
                planex_d[31:16] = angle[47:32];
                planex_d[15:0] = 0;
                planey_d[31:16] = angle[31:16];
                planey_d[15:0] = 0;
                invdet_d[31:16] = angle[15:0];
                invdet_d[15:0] = 0;

                map_posx_d = map_pos_x <<< 8;
                map_posy_d = map_pos_y <<< 8;
                count_d = 0;

                state_d = TRANSFORM_START;
            end

            smeta_raddr = 0;
        end
        MULTIPLY: begin
            mul_res_d = trunc_fixed_mul(mul_arg1_q * mul_arg2_q);
            state_d = next_state_q;
        end


        TRANSFORM_START: begin

            dirx_d = dirx_q >>> 8;
            diry_d = diry_q >>> 8;
            planex_d = planex_q >>> 8;
            planey_d = planey_q >>> 8;
            invdet_d = invdet_q >>> 8;


            spritex_d = (smeta_read_data[31:16] << 8) - map_posx_q;
            spritey_d = (smeta_read_data[15:0] << 8) - map_posy_q;




            state_d = TRANSFORMX_1;
        end
        TRANSFORMX_1: begin



            mul_arg1_d = diry_q;
            mul_arg2_d = spritex_q;
            next_state_d = TRANSFORMX_2;
            state_d = MULTIPLY;
        end
        TRANSFORMX_2: begin

            math_hold_d = mul_res_q;
            mul_arg1_d = dirx_q;
            mul_arg2_d = spritey_q;
            next_state_d = TRANSFORMX_3;
            state_d = MULTIPLY;
        end
        TRANSFORMX_3: begin


            mul_arg1_d = invdet_q;
            mul_arg2_d = math_hold_q - mul_res_q;
            next_state_d = TRANSFORMX_4;
            state_d = MULTIPLY;
        end
        TRANSFORMX_4: begin


            transformx_d = mul_res_q;

            mul_arg1_d = ((~planey_q) + 1'b1);
            mul_arg2_d = spritex_q;
            next_state_d = TRANSFORMY_1;
            state_d = MULTIPLY;
        end
        TRANSFORMY_1: begin



            math_hold_d = mul_res_q;
            mul_arg1_d = planex_q;
            mul_arg2_d = spritey_q;
            next_state_d = TRANSFORMY_2;
            state_d = MULTIPLY;
        end
        TRANSFORMY_2: begin




            mul_arg1_d = invdet_q;
            mul_arg2_d = math_hold_q + mul_res_q;
            next_state_d = TRANSFORMY_3;
            state_d = MULTIPLY;
        end
        TRANSFORMY_3: begin



            transformy_d = mul_res_q;



            idist_addr = mul_res_q[21:10];
            state_d = SPRITE_SCREENX;
        end
        SPRITE_SCREENX: begin
            mul_arg1_d = transformx_q;
            mul_arg2_d = idist << 8;
            next_state_d = SPRITE_SCREENX_2;
            state_d = MULTIPLY;
        end
        SPRITE_SCREENX_2: begin
            mul_arg1_d = HALF_WIDTH << 16;

            mul_arg2_d = 31'h0001_0000 + mul_res_q;
            next_state_d = SPRITE_SCREENX_3;
            state_d = MULTIPLY;

            math_hold_d = mul_arg2_q;
        end
        SPRITE_SCREENX_3: begin
            sprite_screenx_d = mul_res_q >>> 16;


            mul_arg1_d = HEIGHT << 16;
            mul_arg2_d = math_hold_q;
            next_state_d = SPRITE_HEIGHT;
            state_d = MULTIPLY;
        end
        SPRITE_HEIGHT: begin


            if (mul_res_q[31]) begin
                sprite_height_d = (~mul_res_q) + 1'b1 >> 16;
            end else begin
                sprite_height_d = mul_res_q >> 16;
            end

            state_d = SPRITE_DRAWBOUNDS;
        end
        SPRITE_DRAWBOUNDS: begin


            draw_starty_d = (~(sprite_height_q >> 1)) + 1'b1 + HALF_HEIGHT;
            draw_endy_d = (sprite_height_q >> 1) + HALF_HEIGHT;
            draw_startx_d = (~(sprite_height_q >> 1)) + 1'b1 + sprite_screenx_q;
            draw_endx_d = (sprite_height_q >> 1) + sprite_screenx_q;
            state_d = SAVE_TRANSFORMS;
        end
        SAVE_TRANSFORMS: begin

            stmeta_write_data = {smeta_read_data[39:32], transformy_q[23:8], sprite_screenx_q, sprite_height_q, draw_starty_q, draw_endy_q, draw_startx_q, draw_endx_q};
            stmeta_write_en = 1'b1;
            count_d = count_q + 1'b1;
            if (count_q == SPRITE_COUNT-1) begin
                done = 1'b1;
                state_d = IDLE;
            end else begin

                state_d = TRANSFORM_START;

            end
        end
    endcase
end

always @(posedge clk) begin
    if (rst) begin
        state_q <= IDLE;
    end else begin
        state_q <= state_d;
    end

    count_q <= count_d;

    mul_res_q <= mul_res_d;
    mul_arg1_q <= mul_arg1_d;
    mul_arg2_q <= mul_arg2_d;
    math_hold_q <= math_hold_d;
    next_state_q <= next_state_d;

    dirx_q <= dirx_d;
    diry_q <= diry_d;
    planex_q <= planex_d;
    planey_q <= planey_d;
    invdet_q <= invdet_d;
    map_posx_q <= map_posx_d;
    map_posy_q <= map_posy_d;

    spritex_q <= spritex_d;
    spritey_q <= spritey_d;
    transformx_q <= transformx_d;
    transformy_q <= transformy_d;
    sprite_screenx_q <= sprite_screenx_d;
    sprite_height_q <= sprite_height_d;
    draw_starty_q <= draw_starty_d;
    draw_endy_q <= draw_endy_d;
    draw_startx_q <= draw_startx_d;
    draw_endx_q <= draw_endx_d;
end



function signed [31:0] trunc_fixed_mul(input signed [63:0] mulres);
  trunc_fixed_mul = mulres[47:16];
endfunction

endmodule
module sprite (
    input clk,
    input rst,

    input start,
    output reg done,

    input [79:0] angle,
    input [15:0] map_pos_x,
    input [15:0] map_pos_y,
    

    input [6:0] smeta_waddr,
    input [39:0] smeta_write_data,
    input smeta_write_en,

    input [6:0] stmeta_raddr,
    output [119:0] stmeta_read_data
);


localparam HEIGHT = 240,
    WIDTH = 320,
    HALF_HEIGHT = 120,
    HALF_WIDTH = 160,
    SPRITE_COUNT = 128;


reg [6:0] smeta_raddr;
wire [39:0] smeta_read_data;


simple_dual_ram #(.SIZE(40), .DEPTH(SPRITE_COUNT)) smeta_ram (
    .wclk(clk),
    .waddr(smeta_waddr),
    .write_data(smeta_write_data),
    .write_en(smeta_write_en),

    .rclk(clk),
    .raddr(smeta_raddr),
    .read_data(smeta_read_data)
);


reg [6:0] stmeta_waddr;
reg [119:0] stmeta_write_data;
reg stmeta_write_en;


simple_dual_ram #(.SIZE(120), .DEPTH(SPRITE_COUNT)) stmeta_ram (
    .wclk(clk),
    .waddr(stmeta_waddr),
    .write_data(stmeta_write_data),
    .write_en(stmeta_write_en),

    .rclk(clk),
    .raddr(stmeta_raddr),
    .read_data(stmeta_read_data)
);


reg [9:0] idist_addr;
wire [15:0] idist;
idist_rom idist_rom (
    .clk(clk),
    .addr(idist_addr),
    .idist(idist)
);

reg [4:0] state_d, state_q;
reg [4:0] next_state_d, next_state_q;
localparam IDLE = 0,
    MULTIPLY = 1,
    TRANSFORM_START = 2,
    TRANSFORMX_1 = 3,
    TRANSFORMX_2 = 4,
    TRANSFORMX_3 = 5,
    TRANSFORMX_4 = 6,
    TRANSFORMY_1 = 7,
    TRANSFORMY_2 = 8,
    TRANSFORMY_3 = 9,
    SPRITE_SCREENX = 10,
    SPRITE_SCREENX_2 = 11,
    SPRITE_SCREENX_3 = 12,
    SPRITE_HEIGHT = 13,
    SPRITE_DRAWBOUNDS = 14,
    SAVE_TRANSFORMS = 15;


reg signed [31:0] mul_res_d, mul_res_q;
reg signed [31:0] mul_arg1_d, mul_arg1_q;
reg signed [31:0] mul_arg2_d, mul_arg2_q;
reg signed [31:0] math_hold_d, math_hold_q;


reg signed [31:0] dirx_d, dirx_q;
reg signed [31:0] diry_d, diry_q;
reg signed [31:0] planex_d, planex_q;
reg signed [31:0] planey_d, planey_q;
reg signed [31:0] invdet_d, invdet_q;

reg signed [31:0] map_posx_d, map_posx_q;
reg signed [31:0] map_posy_d, map_posy_q;


reg signed [31:0] spritex_d, spritex_q;
reg signed [31:0] spritey_d, spritey_q;
reg signed [31:0] transformx_d, transformx_q;
reg signed [31:0] transformy_d, transformy_q;
reg signed [15:0] sprite_screenx_d, sprite_screenx_q;
reg signed [15:0] sprite_height_d, sprite_height_q;
reg signed [15:0] draw_starty_d, draw_starty_q;
reg signed [15:0] draw_endy_d, draw_endy_q;
reg signed [15:0] draw_startx_d, draw_startx_q;
reg signed [15:0] draw_endx_d, draw_endx_q;

reg [6:0] count_d, count_q;

always @(*) begin
    state_d = state_q;
    next_state_d = next_state_q;
    count_d = count_q;
    smeta_raddr = count_q;
    stmeta_waddr = count_q;
    stmeta_write_data = 120'd0;
    stmeta_write_en = 1'b0;
    idist_addr = 0;
    done = 1'b0;

    mul_res_d = mul_res_q;
    mul_arg1_d = mul_arg1_q;
    mul_arg2_d = mul_arg2_q;
    math_hold_d = math_hold_q;
    next_state_d = next_state_q;

    dirx_d = dirx_q;
    diry_d = diry_q;
    planex_d = planex_q;
    planey_d = planey_q;
    invdet_d = invdet_q;
    map_posx_d = map_posx_q;
    map_posy_d = map_posy_q;

    spritex_d = spritex_q;
    spritey_d = spritey_q;
    transformx_d = transformx_q;
    transformy_d = transformy_q;
    sprite_screenx_d = sprite_screenx_q;
    sprite_height_d = sprite_height_q;
    draw_starty_d = draw_starty_q;
    draw_endy_d = draw_endy_q;
    draw_startx_d = draw_startx_q;
    draw_endx_d = draw_endx_q;

    case (state_q)
        IDLE: begin
            if (start) begin



                dirx_d[31:16] = angle[79:64];
                dirx_d[15:0] = 0;
                diry_d[31:16] = angle[63:48];
                diry_d[15:0] = 0;
                planex_d[31:16] = angle[47:32];
                planex_d[15:0] = 0;
                planey_d[31:16] = angle[31:16];
                planey_d[15:0] = 0;
                invdet_d[31:16] = angle[15:0];
                invdet_d[15:0] = 0;

                map_posx_d = map_pos_x <<< 8;
                map_posy_d = map_pos_y <<< 8;
                count_d = 0;

                state_d = TRANSFORM_START;
            end

            smeta_raddr = 0;
        end
        MULTIPLY: begin
            mul_res_d = trunc_fixed_mul(mul_arg1_q * mul_arg2_q);
            state_d = next_state_q;
        end


        TRANSFORM_START: begin

            dirx_d = dirx_q >>> 8;
            diry_d = diry_q >>> 8;
            planex_d = planex_q >>> 8;
            planey_d = planey_q >>> 8;
            invdet_d = invdet_q >>> 8;


            spritex_d = (smeta_read_data[31:16] << 8) - map_posx_q;
            spritey_d = (smeta_read_data[15:0] << 8) - map_posy_q;




            state_d = TRANSFORMX_1;
        end
        TRANSFORMX_1: begin



            mul_arg1_d = diry_q;
            mul_arg2_d = spritex_q;
            next_state_d = TRANSFORMX_2;
            state_d = MULTIPLY;
        end
        TRANSFORMX_2: begin

            math_hold_d = mul_res_q;
            mul_arg1_d = dirx_q;
            mul_arg2_d = spritey_q;
            next_state_d = TRANSFORMX_3;
            state_d = MULTIPLY;
        end
        TRANSFORMX_3: begin


            mul_arg1_d = invdet_q;
            mul_arg2_d = math_hold_q - mul_res_q;
            next_state_d = TRANSFORMX_4;
            state_d = MULTIPLY;
        end
        TRANSFORMX_4: begin


            transformx_d = mul_res_q;

            mul_arg1_d = ((~planey_q) + 1'b1);
            mul_arg2_d = spritex_q;
            next_state_d = TRANSFORMY_1;
            state_d = MULTIPLY;
        end
        TRANSFORMY_1: begin



            math_hold_d = mul_res_q;
            mul_arg1_d = planex_q;
            mul_arg2_d = spritey_q;
            next_state_d = TRANSFORMY_2;
            state_d = MULTIPLY;
        end
        TRANSFORMY_2: begin




            mul_arg1_d = invdet_q;
            mul_arg2_d = math_hold_q + mul_res_q;
            next_state_d = TRANSFORMY_3;
            state_d = MULTIPLY;
        end
        TRANSFORMY_3: begin



            transformy_d = mul_res_q;



            idist_addr = mul_res_q[21:10];
            state_d = SPRITE_SCREENX;
        end
        SPRITE_SCREENX: begin
            mul_arg1_d = transformx_q;
            mul_arg2_d = idist << 8;
            next_state_d = SPRITE_SCREENX_2;
            state_d = MULTIPLY;
        end
        SPRITE_SCREENX_2: begin
            mul_arg1_d = HALF_WIDTH << 16;

            mul_arg2_d = 31'h0001_0000 + mul_res_q;
            next_state_d = SPRITE_SCREENX_3;
            state_d = MULTIPLY;

            math_hold_d = mul_arg2_q;
        end
        SPRITE_SCREENX_3: begin
            sprite_screenx_d = mul_res_q >>> 16;


            mul_arg1_d = HEIGHT << 16;
            mul_arg2_d = math_hold_q;
            next_state_d = SPRITE_HEIGHT;
            state_d = MULTIPLY;
        end
        SPRITE_HEIGHT: begin


            if (mul_res_q[31]) begin
                sprite_height_d = (~mul_res_q) + 1'b1 >> 16;
            end else begin
                sprite_height_d = mul_res_q >> 16;
            end

            state_d = SPRITE_DRAWBOUNDS;
        end
        SPRITE_DRAWBOUNDS: begin


            draw_starty_d = (~(sprite_height_q >> 1)) + 1'b1 + HALF_HEIGHT;
            draw_endy_d = (sprite_height_q >> 1) + HALF_HEIGHT;
            draw_startx_d = (~(sprite_height_q >> 1)) + 1'b1 + sprite_screenx_q;
            draw_endx_d = (sprite_height_q >> 1) + sprite_screenx_q;
            state_d = SAVE_TRANSFORMS;
        end
        SAVE_TRANSFORMS: begin

            stmeta_write_data = {smeta_read_data[39:32], transformy_q[23:8], sprite_screenx_q, sprite_height_q, draw_starty_q, draw_endy_q, draw_startx_q, draw_endx_q};
            stmeta_write_en = 1'b1;
            count_d = count_q + 1'b1;
            if (count_q == SPRITE_COUNT-1) begin
                done = 1'b1;
                state_d = IDLE;
            end else begin

                state_d = TRANSFORM_START;

            end
        end
    endcase
end

always @(posedge clk) begin
    if (rst) begin
        state_q <= IDLE;
    end else begin
        state_q <= state_d;
    end

    count_q <= count_d;

    mul_res_q <= mul_res_d;
    mul_arg1_q <= mul_arg1_d;
    mul_arg2_q <= mul_arg2_d;
    math_hold_q <= math_hold_d;
    next_state_q <= next_state_d;

    dirx_q <= dirx_d;
    diry_q <= diry_d;
    planex_q <= planex_d;
    planey_q <= planey_d;
    invdet_q <= invdet_d;
    map_posx_q <= map_posx_d;
    map_posy_q <= map_posy_d;

    spritex_q <= spritex_d;
    spritey_q <= spritey_d;
    transformx_q <= transformx_d;
    transformy_q <= transformy_d;
    sprite_screenx_q <= sprite_screenx_d;
    sprite_height_q <= sprite_height_d;
    draw_starty_q <= draw_starty_d;
    draw_endy_q <= draw_endy_d;
    draw_startx_q <= draw_startx_d;
    draw_endx_q <= draw_endx_d;
end



function signed [31:0] trunc_fixed_mul(input signed [63:0] mulres);
  trunc_fixed_mul = mulres[47:16];
endfunction

endmodule