data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v
1: b'%Error: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:105: Cannot find file containing module: \'jtframe_linebuf\'\njtframe_linebuf #(.DW(15)) u_obj_line (\n^~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/517390617/modules/raizing_video,data/full_repos/permissive/517390617/jtframe_linebuf\n             data/full_repos/permissive/517390617/modules/raizing_video,data/full_repos/permissive/517390617/jtframe_linebuf.v\n             data/full_repos/permissive/517390617/modules/raizing_video,data/full_repos/permissive/517390617/jtframe_linebuf.sv\n             jtframe_linebuf\n             jtframe_linebuf.v\n             jtframe_linebuf.sv\n             obj_dir/jtframe_linebuf\n             obj_dir/jtframe_linebuf.v\n             obj_dir/jtframe_linebuf.sv\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:117: Operator ASSIGNW expects 4 bits on the Assign RHS, but Assign RHS\'s AND generates 32 bits.\n                                                                                            : ... In instance raizing_obj\nwire [3:0] sprite_code = (((sprite_line >> (tx*4)) & \'h0F));\n                       ^\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:118: Operator SUB expects 32 or 10 bits on the RHS, but RHS\'s VARREF \'tx\' generates 3 bits.\n                                                                                            : ... In instance raizing_obj\nwire [9:0] buf_code = xflip ? (sprite_x_pos + (-8 * spr_x_render) - tx) + 7 :\n                                                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:119: Operator ADD expects 32 or 10 bits on the RHS, but RHS\'s VARREF \'tx\' generates 3 bits.\n                                                                                            : ... In instance raizing_obj\n                                (sprite_x_pos + (8 * spr_x_render) + tx);\n                                                                   ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:121: Operator SUB expects 16 bits on the LHS, but LHS\'s SIGNED generates 9 bits.\n                                                                                            : ... In instance raizing_obj\nwire [15:0] yclip = $signed(VRENDER) - sprite_y_pos;\n                                     ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:121: Operator SUB expects 16 bits on the RHS, but RHS\'s VARREF \'sprite_y_pos\' generates 10 bits.\n                                                                                            : ... In instance raizing_obj\nwire [15:0] yclip = $signed(VRENDER) - sprite_y_pos;\n                                     ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:122: Operator ADD expects 32 or 16 bits on the LHS, but LHS\'s VARREF \'sprite_y_size\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\nwire [15:0] lines_down = yflip ? ((sprite_y_size+1)<<3) - yclip - 1 : yclip;\n                                                ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:123: Operator SHIFTL expects 16 bits on the LHS, but LHS\'s VARREF \'spr_x_render\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\nwire [15:0] tiles_across = spr_x_render<<5;\n                                       ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:124: Operator ADD expects 32 or 16 bits on the LHS, but LHS\'s VARREF \'sprite_x_size\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\nwire [15:0] tiles_down = lines_down[15:3] * ((sprite_x_size + 1) << 5);\n                                                            ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:135: Operator ADD expects 32 or 15 bits on the RHS, but RHS\'s VARREF \'sprite_queue_i\' generates 9 bits.\n                                                                                            : ... In instance raizing_obj\nwire [14:0] rd_spr_q_addr = (priority_i*2048) + sprite_queue_i;\n                                              ^\n%Error: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:140: Cannot find file containing module: \'jtframe_dual_ram\'\njtframe_dual_ram #(.dw(8), .aw(15)) u_sprite_pri_q(\n^~~~~~~~~~~~~~~~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:250: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s AND generates 32 or 13 bits.\n                                                                                            : ... In instance raizing_obj\n            multiconnector_x<=SPRITE_SCROLL_X&\'h1FF;\n                            ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:251: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s AND generates 32 or 13 bits.\n                                                                                            : ... In instance raizing_obj\n            multiconnector_y<=SPRITE_SCROLL_Y&\'h1FF;\n                            ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:290: Bit extraction of var[255:0] requires 8 bit index, not 9 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(!spr_idx_queue_reset && !spr_idx_queue[spr] && FAST_OBJ_QUEUE) begin  \n                                                                 ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:338: Bit extraction of var[255:0] requires 8 bit index, not 9 bits.\n                                                                                            : ... In instance raizing_obj\n                            spr_idx_queue[spr]<=1;\n                                         ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:343: Operator ASSIGN expects 5 bits on the Assign RHS, but Assign RHS\'s SEL generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                        priority_l= GP9001RAM_GCU_DOUT[11:8];  \n                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:345: Operator ASSIGN expects 5 bits on the Assign RHS, but Assign RHS\'s SEL generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_y_size_t = GP9001RAM2_GCU_DOUT[3:0];\n                                        ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:347: Operator ADD expects 32 or 13 bits on the LHS, but LHS\'s SEL generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                                            (GP9001RAM2_GCU_DOUT[15:7] + SPRITE_SCROLL_Y + SPRITE_SCROLL_YOFFS) & \'h1FF :\n                                                                       ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:348: Operator ADD expects 32 or 13 bits on the LHS, but LHS\'s VARREF \'multiconnector_y\' generates 10 bits.\n                                                                                            : ... In instance raizing_obj\n                                            (multiconnector_y + GP9001RAM2_GCU_DOUT[15:7]) & \'h1FF;\n                                                              ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:348: Operator ADD expects 32 or 13 bits on the RHS, but RHS\'s SEL generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                                            (multiconnector_y + GP9001RAM2_GCU_DOUT[15:7]) & \'h1FF;\n                                                              ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:346: Operator ASSIGN expects 9 bits on the Assign RHS, but Assign RHS\'s COND generates 32 or 13 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_y_pos_t = !mc ? \n                                       ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:350: Operator ADD expects 32 or 9 bits on the LHS, but LHS\'s VARREF \'sprite_y_size_t\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(yfl) sprite_y_pos_t=sprite_y_pos_t-((sprite_y_size_t+1) << 3) + 1;\n                                                                               ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:352: Operator ASSIGN expects 9 bits on the Assign RHS, but Assign RHS\'s SUB generates 32 or 10 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(sprite_y_pos_t > 384 || (sprite_y_pos_t > 448 && yfl)) sprite_y_pos_t = sprite_y_pos_t - \'h200;\n                                                                                                 ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:355: Operator ADD expects 32 or 9 bits on the LHS, but LHS\'s VARREF \'sprite_y_size_t\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_y_size_t = (sprite_y_size_t+1) - (-sprite_y_pos_t>>3);\n                                                              ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:355: Operator ASSIGN expects 5 bits on the Assign RHS, but Assign RHS\'s SUB generates 32 or 9 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_y_size_t = (sprite_y_size_t+1) - (-sprite_y_pos_t>>3);\n                                            ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:354: Operator ADD expects 32 or 9 bits on the LHS, but LHS\'s VARREF \'sprite_y_size_t\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(sprite_y_pos_t < 0 && $signed(VRENDER) < $signed(sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin \n                                                                                                              ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:354: Operator LTS expects 32 bits on the LHS, but LHS\'s SIGNED generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(sprite_y_pos_t < 0 && $signed(VRENDER) < $signed(sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin \n                                                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:362: Operator SHIFTL expects 15 bits on the LHS, but LHS\'s VARREF \'priority_l\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                            wr_spr_q_addr<=((priority_l<<11) | sprite_queue_priority_n[((priority_l+1)<<3)-1 -: 8]);\n                                                       ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:362: Operator OR expects 15 bits on the RHS, but RHS\'s SEL generates 8 bits.\n                                                                                            : ... In instance raizing_obj\n                            wr_spr_q_addr<=((priority_l<<11) | sprite_queue_priority_n[((priority_l+1)<<3)-1 -: 8]);\n                                                             ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:363: Bit extraction of var[15:0] requires 4 bit index, not 5 bits.\n                                                                                            : ... In instance raizing_obj\n                            pri_has_sprite[priority_l]<=1\'b1;\n                                          ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:359: Operator ADD expects 32 or 9 bits on the LHS, but LHS\'s VARREF \'sprite_y_size_t\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(VRENDER >= sprite_y_pos_t && VRENDER < (sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin\n                                                                                                     ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:381: Bit extraction of var[15:0] requires 4 bit index, not 5 bits.\n                                                                                            : ... In instance raizing_obj\n                    pri_has_sprite[priority_i]<=0;\n                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:384: Operator ASSIGNDLY expects 9 bits on the Assign RHS, but Assign RHS\'s SEL generates 8 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_queue_priority_n_scan_buf_i <= sprite_queue_priority_n[((priority_i+1)<<3)-1 -: 8];\n                                                           ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:405: Operator ASSIGNDLY expects 9 bits on the Assign RHS, but Assign RHS\'s VARREF \'spr_q_out\' generates 8 bits.\n                                                                                            : ... In instance raizing_obj\n                        spr<=spr_q_out;\n                           ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:460: Operator SHIFTL expects 11 bits on the LHS, but LHS\'s SEL generates 6 bits.\n                                                                                            : ... In instance raizing_obj\n                    palette<=(sprite_attributes[55:50]<<4);\n                                                      ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:461: Operator ASSIGNDLY expects 15 bits on the Assign RHS, but Assign RHS\'s AND generates 32 or 16 bits.\n                                                                                            : ... In instance raizing_obj\n                    sprite_num<=(sprite_attributes[47:32] & \'h7FFF);\n                              ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:463: Operator ASSIGNDLY expects 5 bits on the Assign RHS, but Assign RHS\'s SEL generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                    sprite_x_size<=sprite_attributes[19:16];\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:464: Operator ASSIGNDLY expects 5 bits on the Assign RHS, but Assign RHS\'s SEL generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                    sprite_y_size<=sprite_attributes[3:0];\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:470: Operator ADD expects 32 or 13 bits on the LHS, but LHS\'s SEL generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_x_pos<=(sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & \'h01FF;\n                                                               ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:470: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s AND generates 32 or 13 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_x_pos<=(sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & \'h01FF;\n                                    ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:471: Operator ADD expects 32 or 13 bits on the LHS, but LHS\'s SEL generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_y_pos<=(GP9001RAM2_GCU_DOUT[15:7]+SPRITE_SCROLL_Y+SPRITE_SCROLL_YOFFS) & \'h01FF;\n                                                                ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:471: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s AND generates 32 or 13 bits.\n                                                                                            : ... In instance raizing_obj\n                        sprite_y_pos<=(GP9001RAM2_GCU_DOUT[15:7]+SPRITE_SCROLL_Y+SPRITE_SCROLL_YOFFS) & \'h01FF;\n                                    ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:475: Operator ADD expects 32 or 13 bits on the LHS, but LHS\'s SEL generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                    $display("xpos: %d %d %d %d %d", sprite_x_pos, SPRITE_SCROLL_X, SPRITE_SCROLL_XOFFS, (sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & \'h01FF, sprite_x_size);\n                                                                                                                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:482: Operator ASSIGN expects 9 bits on the Assign RHS, but Assign RHS\'s VARREF \'sprite_y_pos\' generates 10 bits.\n                                                                                            : ... In instance raizing_obj\n                    sprite_y_pos_t=sprite_y_pos;\n                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:487: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s SIGNED generates 32 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_x_pos <= $signed(sprite_x_pos - \'h200 - \'d7);\n                                         ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:490: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s SIGNED generates 32 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_x_pos<=$signed(sprite_x_pos-7);\n                                        ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:495: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s SIGNED generates 32 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_x_pos <= $signed(sprite_x_pos - \'h200);\n                                         ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:501: Operator ASSIGN expects 9 bits on the Assign RHS, but Assign RHS\'s SIGNED generates 32 bits.\n                                                                                            : ... In instance raizing_obj\n                             sprite_y_pos_t = $signed(sprite_y_pos - \'h200);\n                                            ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:504: Operator ADD expects 32 or 10 bits on the LHS, but LHS\'s VARREF \'sprite_y_size\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_y_pos_t=$signed(sprite_y_pos - ((sprite_y_size + 1) << 3));\n                                                                                  ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:504: Operator ASSIGN expects 9 bits on the Assign RHS, but Assign RHS\'s SIGNED generates 32 bits.\n                                                                                            : ... In instance raizing_obj\n                            sprite_y_pos_t=$signed(sprite_y_pos - ((sprite_y_size + 1) << 3));\n                                          ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:500: Operator ADD expects 32 or 10 bits on the LHS, but LHS\'s VARREF \'sprite_y_size\' generates 5 bits.\n                                                                                            : ... In instance raizing_obj\n                        if($signed(sprite_y_pos-((sprite_y_size + 1) << 3)) > 384) begin\n                                                                ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:509: Operator ASSIGNDLY expects 10 bits on the Assign RHS, but Assign RHS\'s VARREF \'sprite_y_pos_t\' generates 9 bits.\n                                                                                            : ... In instance raizing_obj\n                    sprite_y_pos<=sprite_y_pos_t;\n                                ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:526: Operator ASSIGNDLY expects 4 bits on the Assign RHS, but Assign RHS\'s VARREF \'sprite_bank\' generates 3 bits.\n                                                                                            : ... In instance raizing_obj\n                            TILE_BANK<=sprite_bank;\n                                     ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:522: Operator LTE expects 32 or 19 bits on the LHS, but LHS\'s VARREF \'sprite_num\' generates 15 bits.\n                                                                                            : ... In instance raizing_obj\n                        if(sprite_num <= max_sprite_num) begin  \n                                      ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:579: Operator ASSIGNDLY expects 4 bits on the Assign RHS, but Assign RHS\'s VARREF \'sprite_bank\' generates 3 bits.\n                                                                                            : ... In instance raizing_obj\n                        TILE_BANK<=sprite_bank;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:591: Operator ASSIGNDLY expects 9 bits on the Assign RHS, but Assign RHS\'s AND generates 32 or 10 bits.\n                                                                                            : ... In instance raizing_obj\n                        buf_addr<=(FLIPX ? 319-buf_code : buf_code)&\'h1FF;\n                                ^~\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:592: Operator ADD expects 32 or 15 bits on the LHS, but LHS\'s SEL generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                        buf_data<=((SHIFT_SPRITE_PRI ? sprite_attributes[59:56] + 1 : sprite_attributes[59:56]) << 12) + (palette&\'h7F0)+sprite_code;\n                                                                                ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:592: Operator COND expects 32 or 15 bits on the Conditional False, but Conditional False\'s SEL generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                        buf_data<=((SHIFT_SPRITE_PRI ? sprite_attributes[59:56] + 1 : sprite_attributes[59:56]) << 12) + (palette&\'h7F0)+sprite_code;\n                                                     ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:592: Operator AND expects 32 or 15 bits on the LHS, but LHS\'s VARREF \'palette\' generates 11 bits.\n                                                                                            : ... In instance raizing_obj\n                        buf_data<=((SHIFT_SPRITE_PRI ? sprite_attributes[59:56] + 1 : sprite_attributes[59:56]) << 12) + (palette&\'h7F0)+sprite_code;\n                                                                                                                                 ^\n%Warning-WIDTH: data/full_repos/permissive/517390617/modules/raizing_video/raizing_obj.v:592: Operator ADD expects 32 or 15 bits on the RHS, but RHS\'s VARREF \'sprite_code\' generates 4 bits.\n                                                                                            : ... In instance raizing_obj\n                        buf_data<=((SHIFT_SPRITE_PRI ? sprite_attributes[59:56] + 1 : sprite_attributes[59:56]) << 12) + (palette&\'h7F0)+sprite_code;\n                                                                                                                                        ^\n%Error: Exiting due to 2 error(s), 58 warning(s)\n'
module raizing_obj (
    input CLK,
    input CLK96,
    input PIXEL_CEN,
    input RESET,
    input RESET96,
    input [8:0] VRENDER,
    input [8:0] H,
    input ACTIVE,
    input HB,
    input VB,
    input FLIPX,

    input SHIFT_SPRITE_PRI,
    input FAST_OBJ_QUEUE,


    output reg [12:0] GP9001RAM_GCU_ADDR,
    input  [15:0] GP9001RAM_GCU_DOUT,
    output reg [12:0] GP9001RAM2_GCU_ADDR,
    input  [15:0] GP9001RAM2_GCU_DOUT,


    output reg GFX_CS,
    input GFX_OK,
    output reg [14:0] TILE_NUMBER,
    output reg [15:0] TILE_NUMBER_OFFS,
    output reg [3:0] TILE_BANK,
    input  [31:0] GFX_DATA,


    input signed [12:0] SPRITE_SCROLL_X,
    input signed [12:0] SPRITE_SCROLL_Y,
    input signed [12:0] SPRITE_SCROLL_XOFFS,
    input signed [12:0] SPRITE_SCROLL_YOFFS,

    output reg [14:0] OBJ_PIXEL
);

reg [7:0] st = 0;

localparam max_sprite = 256, max_priority = 16, max_sprite_num = 'h7FFFF; 
localparam spriteram_offs = 'h0;

reg [8:0] spr = 0;
wire [12:0] sprite_addr_base = spriteram_offs+(spr[7:0]*4);
reg last_HB = 0, start = 0;
wire pedg_HB = !HB && last_HB;
reg last_VB = 0;
reg busy = 1'b0;
wire [7:0] nb_pixels = {|GFX_DATA[31:28], |GFX_DATA[27:24], |GFX_DATA[23:20], |GFX_DATA[19:16], 
                        |GFX_DATA[15:12], |GFX_DATA[11:8], |GFX_DATA[7:4], |GFX_DATA[3:0]};
reg [7:0] drawn_pixels = 8'h0;

reg [4:0] priority_l;
reg signed [4:0] sprite_y_size_t;
reg signed [8:0] sprite_y_pos_t;
reg [8:0] sprite_queue_n = 0;
reg [8:0] sprite_queue_i = 0;
reg [(8*max_priority)-1:0] sprite_queue_priority_n = {max_priority{8'h00}};
reg [8:0] sprite_queue_priority_n_scan_buf_i = 0;
reg [4:0] priority_i = 0;
reg [8:0] spr_scan_i = 0;

reg [4:0] spr_x_render = 0;


reg [63:0] sprite_attributes = 64'h0;
reg [10:0] palette = 0;
reg [14:0] sprite_num = 0;
reg [2:0] sprite_bank = 0;
reg [4:0] sprite_x_size = 0, sprite_y_size = 0;
reg signed [9:0] sprite_x_pos = 0, sprite_y_pos = 0, multiconnector_x = 0, multiconnector_y = 0;
reg xflip = 0, yflip = 0;
reg [31:0] sprite_line = 0;
reg [2:0] tx = 0;


reg [14:0] buf_data = 0;
reg [8:0] buf_addr = 0;
reg buf_we = 1'b0;
wire [14:0] preobj_pxl;
wire [8:0] buf_addr_out = H;
jtframe_linebuf #(.DW(15)) u_obj_line (
    .clk(CLK96),
    .LHBL(~HB),
    .wr_addr(buf_addr),
    .pxl_cen(PIXEL_CEN),
    .wr_data(buf_data),
    .we(buf_we),
    .rd_addr(buf_addr_out),
    .rd_data(),
    .rd_gated(preobj_pxl)
);

wire [3:0] sprite_code = (((sprite_line >> (tx*4)) & 'h0F));
wire [9:0] buf_code = xflip ? (sprite_x_pos + (-8 * spr_x_render) - tx) + 7 :
                                (sprite_x_pos + (8 * spr_x_render) + tx);

wire [15:0] yclip = $signed(VRENDER) - sprite_y_pos;
wire [15:0] lines_down = yflip ? ((sprite_y_size+1)<<3) - yclip - 1 : yclip;
wire [15:0] tiles_across = spr_x_render<<5;
wire [15:0] tiles_down = lines_down[15:3] * ((sprite_x_size + 1) << 5);

wire [15:0] cur_row_lines_down = (lines_down & 7) << 2;
wire [15:0] tile_offs = tiles_down+cur_row_lines_down+tiles_across;

reg mc=0, yfl=0, xfl=0;
reg clr=0;
reg [8:0] clr_addr = 0;

reg [7:0] wr_spr_q;
reg [14:0] wr_spr_q_addr;
wire [14:0] rd_spr_q_addr = (priority_i*2048) + sprite_queue_i;
reg spr_q_we;
wire [7:0] spr_q_out;


jtframe_dual_ram #(.dw(8), .aw(15)) u_sprite_pri_q(
    .clk0(CLK96),
    .clk1(CLK96),
    .data0  (wr_spr_q),
    .addr0  (wr_spr_q_addr),
    .we0    (spr_q_we),
    .q0     (),

    .data1  (~8'h0),
    .addr1  (rd_spr_q_addr),
    .we1    (1'b0),
    .q1     (spr_q_out)
);

reg [15:0] pri_has_sprite = 16'd0;

integer i, c = 0;

always @(posedge CLK96, posedge RESET96) begin
    if(RESET96) begin
        OBJ_PIXEL<=0;
    end else begin
        if(PIXEL_CEN && ACTIVE) begin
            OBJ_PIXEL<=preobj_pxl;
        end
    end
    
end
reg [255:0] spr_idx_queue = 256'd0;
reg spr_idx_queue_reset = 1;

always @(posedge CLK96, posedge RESET96) begin
    if(RESET96) begin
        st<=0;
        start<=1'b0;
        busy<=1'b0;
        priority_l=0;
        sprite_y_size_t<=0;
        sprite_y_pos_t<=0;
        spr<=0;
        sprite_queue_n<=0;
        sprite_queue_i<=0;
        sprite_queue_priority_n<={max_priority{8'h00}};
        priority_i<=0;
        spr_scan_i<=0;
        sprite_queue_priority_n_scan_buf_i<=0;
        mc=0; 
        yfl=0;
        xfl=0;
        clr<=1'b1;
        clr_addr<=0;
        spr_x_render <= 0;
        sprite_attributes <= 64'h0;
        palette <= 0;
        sprite_num <= 0;
        sprite_bank <= 0;
        sprite_x_size <= 0; 
        sprite_y_size <= 0;
        sprite_x_pos <= 0; 
        sprite_y_pos <= 0; 
        multiconnector_x <= 0; 
        multiconnector_y <= 0;
        xflip <= 0;
        yflip <= 0;
        sprite_line <= 0;
        tx <= 0;
        buf_data <= 0;
        buf_addr <= 0;
        buf_we <= 1'b0;
        start<=0; 
        busy<=1'b0;
        TILE_NUMBER<=0;
        TILE_NUMBER_OFFS<=0;
        TILE_BANK<=0;
        GFX_CS<=0;
        GP9001RAM_GCU_ADDR<=0;
        GP9001RAM2_GCU_ADDR<=0;
        pri_has_sprite <= 16'd0;
        spr_q_we<=1'b0;
        spr_idx_queue <= 256'd0;
        spr_idx_queue_reset <= 1'b1;
    end else begin

        last_HB    <= HB;
        last_VB    <= VB;
        c<=c+1;

        if( (pedg_HB && !VB)  || (((!FLIPX && VRENDER == 0) || (FLIPX && VRENDER == 239)) && pedg_HB)) begin
            start <= 1'b1;
        end

        if(VB && !last_VB) begin
            spr_idx_queue<=256'd0;
            spr_idx_queue_reset<=1'b1;
        end

        if(start && !busy) begin

            start<=1'b0;
            busy<=1'b1;
            spr<=0;
            st<=0;
            priority_l=0;
            sprite_y_size_t<=0;
            sprite_y_pos_t<=0;
            sprite_queue_n<=0;
            sprite_queue_i<=0;
            sprite_queue_priority_n<={max_priority{8'h00}};
            priority_i<=0;
            spr_scan_i<=0;
            multiconnector_x<=SPRITE_SCROLL_X&'h1FF;
            multiconnector_y<=SPRITE_SCROLL_Y&'h1FF;
            sprite_queue_priority_n_scan_buf_i<=0;
            clr<=1'b1;
            clr_addr<=0;
            mc=0; 
            yfl=0;
            xfl=0;
            spr_x_render <= 0;
            sprite_attributes <= 64'h0;
            palette <= 0;
            sprite_num <= 0;
            sprite_bank <= 0;
            sprite_x_size <= 0; 
            sprite_y_size <= 0;
            sprite_x_pos <= 0; 
            sprite_y_pos <= 0; 
            xflip <= 0;
            yflip <= 0;
            sprite_line <= 0;
            tx <= 0;
            buf_data <= 0;
            buf_addr <= 0;            
            buf_we <= 1'b0;
            TILE_NUMBER<=0;
            TILE_NUMBER_OFFS<=0;
            TILE_BANK<=0;
            GFX_CS<=0;
            GP9001RAM_GCU_ADDR<=0;
            GP9001RAM2_GCU_ADDR<=0;
            pri_has_sprite <= 16'd0;
            spr_q_we<=1'b0;
        end else if(busy) begin
            st<=st+1;
            clr<=1'b1;


            case(st)
                0: begin
                    if(spr<max_sprite) begin
                        if(!spr_idx_queue_reset && !spr_idx_queue[spr] && FAST_OBJ_QUEUE) begin
                            spr<=spr+1;
                            st<=st;
                        end else begin
                            GP9001RAM_GCU_ADDR<= sprite_addr_base;
                            GP9001RAM2_GCU_ADDR<= sprite_addr_base+3;
                        end
                    end else begin
                        if(spr_idx_queue_reset) spr_idx_queue_reset<=0;

                        if(sprite_queue_n == 0) begin
                            busy<=0;
                            start<=1'b0;
                            spr_q_we<=1'b0;

                        end else begin
                            st<=4;

                            spr_q_we<=1'b0;
                            sprite_queue_priority_n_scan_buf_i<=0;
                            spr_scan_i<=0;
                            
                            priority_i<= pri_has_sprite[0] ? 0 :
                                         pri_has_sprite[1] ? 1 :
                                         pri_has_sprite[2] ? 2 :
                                         pri_has_sprite[3] ? 3 :
                                         pri_has_sprite[4] ? 4 :
                                         pri_has_sprite[5] ? 5 :
                                         pri_has_sprite[6] ? 6 :
                                         pri_has_sprite[7] ? 7 :
                                         pri_has_sprite[8] ? 8 :
                                         pri_has_sprite[9] ? 9 :
                                         pri_has_sprite[10] ? 10 :
                                         pri_has_sprite[11] ? 11 :
                                         pri_has_sprite[12] ? 12 :
                                         pri_has_sprite[13] ? 13 :
                                         pri_has_sprite[14] ? 14 :
                                         pri_has_sprite[15] ? 15 :
                                         16;
                        end
                    end
                end
                1: begin
                    spr_q_we<=1'b1;
                end
                2: begin
                    if(GP9001RAM_GCU_DOUT[15]) begin
                        if(spr_idx_queue_reset) begin
                            spr_idx_queue[spr]<=1;
                        end
                        mc=GP9001RAM_GCU_DOUT[14];
                        yfl= GP9001RAM_GCU_DOUT[13];
                        xfl= GP9001RAM_GCU_DOUT[12];
                        priority_l= GP9001RAM_GCU_DOUT[11:8];

                        sprite_y_size_t = GP9001RAM2_GCU_DOUT[3:0];
                        sprite_y_pos_t = !mc ? 
                                            (GP9001RAM2_GCU_DOUT[15:7] + SPRITE_SCROLL_Y + SPRITE_SCROLL_YOFFS) & 'h1FF :
                                            (multiconnector_y + GP9001RAM2_GCU_DOUT[15:7]) & 'h1FF;
                        
                        if(yfl) sprite_y_pos_t=sprite_y_pos_t-((sprite_y_size_t+1) << 3) + 1;
                        
                        if(sprite_y_pos_t > 384 || (sprite_y_pos_t > 448 && yfl)) sprite_y_pos_t = sprite_y_pos_t - 'h200;
                        
                        if(sprite_y_pos_t < 0 && $signed(VRENDER) < $signed(sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin 
                            sprite_y_size_t = (sprite_y_size_t+1) - (-sprite_y_pos_t>>3);
                            sprite_y_pos_t = 0;
                        end

                        if(VRENDER >= sprite_y_pos_t && VRENDER < (sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin
                            $display("queue: %d, %d, %d, %h, %h, %h", VRENDER, sprite_y_size_t, sprite_y_pos_t, sprite_queue_priority_n[((priority_l+1)<<3)-1 -:8]+1, priority_l, spr[7:0]);
                            wr_spr_q <= spr[7:0];
                            wr_spr_q_addr<=((priority_l<<11) | sprite_queue_priority_n[((priority_l+1)<<3)-1 -: 8]);
                            pri_has_sprite[priority_l]<=1'b1;

                            sprite_queue_priority_n[((priority_l+1)<<3)-1 -:8] <= sprite_queue_priority_n[((priority_l+1)<<3)-1 -: 8]+1;
                            sprite_queue_n<=sprite_queue_n+1;                            
                        end

                        st<=0;
                        spr<=spr+1;
                    end else begin
                        st<=0;
                        spr<=spr+1;
                    end
                end
            endcase


            case(st)
                4: begin
                    pri_has_sprite[priority_i]<=0;
                    if(sprite_queue_priority_n[((priority_i+1)<<3)-1 -: 8] > 0 && priority_i<max_priority) begin

                        sprite_queue_priority_n_scan_buf_i <= sprite_queue_priority_n[((priority_i+1)<<3)-1 -: 8];
                        st<=5;

                        sprite_queue_i<=0;
                        tx<=0;
                        spr_x_render<=0;
                    end else begin

                        busy<=0;
                        start<=1'b0;
                        st<=0;

                    end
                end
            endcase


            case(st)
                6: begin
                    if(sprite_queue_i < sprite_queue_priority_n_scan_buf_i) begin

                        spr<=spr_q_out;
                        spr_x_render<=0;

                    end else begin
                        if(pri_has_sprite> 0) begin
                            st<=4;

                            sprite_queue_priority_n_scan_buf_i<=0;
                            spr_scan_i<=0;

                            priority_i<= pri_has_sprite[0] ? 0 :
                                         pri_has_sprite[1] ? 1 :
                                         pri_has_sprite[2] ? 2 :
                                         pri_has_sprite[3] ? 3 :
                                         pri_has_sprite[4] ? 4 :
                                         pri_has_sprite[5] ? 5 :
                                         pri_has_sprite[6] ? 6 :
                                         pri_has_sprite[7] ? 7 :
                                         pri_has_sprite[8] ? 8 :
                                         pri_has_sprite[9] ? 9 :
                                         pri_has_sprite[10] ? 10 :
                                         pri_has_sprite[11] ? 11 :
                                         pri_has_sprite[12] ? 12 :
                                         pri_has_sprite[13] ? 13 :
                                         pri_has_sprite[14] ? 14 :
                                         pri_has_sprite[15] ? 15 :
                                         16;
                        end else begin
                            busy<=0;
                            start<=1'b0;
                            st<=0;

                        end
                    end
                end
                7: begin
                    GP9001RAM_GCU_ADDR<=sprite_addr_base;
                    GP9001RAM2_GCU_ADDR <= sprite_addr_base + 1;
                end
                8:  begin
                    GP9001RAM_GCU_ADDR<=sprite_addr_base + 2;
                    GP9001RAM2_GCU_ADDR <= sprite_addr_base + 3;
                end
                9: begin
                    sprite_attributes[63:48] <= GP9001RAM_GCU_DOUT; 
                    sprite_attributes[47:32] <= GP9001RAM2_GCU_DOUT;
                end
                10: begin 
                    sprite_attributes[31:16] <= GP9001RAM_GCU_DOUT;
                    sprite_attributes[15:0] <= GP9001RAM2_GCU_DOUT;
                end
                11: begin

                    xflip<=sprite_attributes[60];
                    yflip<=sprite_attributes[61];
                    palette<=(sprite_attributes[55:50]<<4);
                    sprite_num<=(sprite_attributes[47:32] & 'h7FFF);
                    sprite_bank<=sprite_attributes[49:47];
                    sprite_x_size<=sprite_attributes[19:16];
                    sprite_y_size<=sprite_attributes[3:0];
                    
                    if(sprite_attributes[62]) begin
                        sprite_x_pos <= (multiconnector_x + sprite_attributes[31:23]) & 'h1ff;
                        sprite_y_pos <= (multiconnector_y + GP9001RAM2_GCU_DOUT[15:7]) & 'h1ff;
                    end else begin
                        sprite_x_pos<=(sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & 'h01FF;
                        sprite_y_pos<=(GP9001RAM2_GCU_DOUT[15:7]+SPRITE_SCROLL_Y+SPRITE_SCROLL_YOFFS) & 'h01FF;
                    end   
                end
                12: begin
                    $display("xpos: %d %d %d %d %d", sprite_x_pos, SPRITE_SCROLL_X, SPRITE_SCROLL_XOFFS, (sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & 'h01FF, sprite_x_size);

                    

                    multiconnector_x<=sprite_x_pos;
                    multiconnector_y<=sprite_y_pos;

                    sprite_y_pos_t=sprite_y_pos;
                    sprite_y_size_t=sprite_y_size;

                    if(xflip) begin
                        if($signed(sprite_x_pos-7) > 448) begin
                            sprite_x_pos <= $signed(sprite_x_pos - 'h200 - 'd7);
                        end
                        else begin
                            sprite_x_pos<=$signed(sprite_x_pos-7);
                        end
                    end else begin

                        if($signed(sprite_x_pos) > 384) begin
                            sprite_x_pos <= $signed(sprite_x_pos - 'h200);
                        end
                    end

                    if(yflip) begin
                        if($signed(sprite_y_pos-((sprite_y_size + 1) << 3)) > 384) begin
                             sprite_y_pos_t = $signed(sprite_y_pos - 'h200);
                        end
                        else begin
                            sprite_y_pos_t=$signed(sprite_y_pos - ((sprite_y_size + 1) << 3));
                        end
                        sprite_y_pos_t = sprite_y_pos_t+1;
                    end

                    sprite_y_pos<=sprite_y_pos_t;
                    sprite_y_size<=sprite_y_size_t;


                    spr_x_render<=0;
                    if(xflip) tx<=7;
                    else tx<=0;

                    st<=15;
                end

                15: begin
                    if(spr_x_render < (sprite_x_size + 1)) begin
                        if(sprite_num <= max_sprite_num) begin
                            GFX_CS<=1'b1;
                            TILE_NUMBER<=sprite_num;
                            TILE_NUMBER_OFFS<=tile_offs;
                            TILE_BANK<=sprite_bank;
                        end else begin

                            st<=5;
                            tx<=0;
                            GFX_CS<=1'b0;
                            spr_x_render<=0;
                            buf_we<=1'b0;
                            sprite_queue_i<=sprite_queue_i+1;
                        end
                    end else begin

                        st<=5;
                        tx<=0;
                        GFX_CS<=1'b0;
                        spr_x_render<=0;
                        buf_we<=1'b0;
                        sprite_queue_i<=sprite_queue_i+1;
                    end
                end
                16: st<=17;
                17: begin
                    if(spr_x_render == (sprite_x_size + 1)) begin
                        st<=5;
                        tx<=0;
                        GFX_CS<=1'b0;
                        spr_x_render<=0;
                        buf_we<=1'b0;
                        sprite_queue_i<=sprite_queue_i+1;
                    end else begin
                        if(GFX_OK) begin
                            GFX_CS<=1'b0;
                            TILE_NUMBER<=0;
                            TILE_NUMBER_OFFS<=0;
                            TILE_BANK<=0;
                            sprite_line<=GFX_DATA;
                            $display("%d, %d, %d, %h, %h %h", VRENDER, sprite_x_pos, sprite_x_size, TILE_NUMBER, TILE_NUMBER_OFFS, GFX_DATA);

                            st<=18;
                            buf_we<=1'b1;
                            if(xflip) tx<=7;
                            else tx<= 0;
                            drawn_pixels = 8'h0;
                        end else begin
                            st<=st;
                        end
                    end
                end
                18: begin
                    if(spr_x_render + 1 < (sprite_x_size + 1)) begin
                        GFX_CS<=1'b1;
                        TILE_NUMBER<=sprite_num;
                        TILE_NUMBER_OFFS<=tile_offs+32;
                        TILE_BANK<=sprite_bank;
                    end
                    st<=22;
                end
                22: begin
                    if(xflip) tx<=tx-1;
                    else tx<= tx+1;


                    
                    if( sprite_code > 0 && buf_code>=0 && buf_code<320) begin
                        drawn_pixels[tx] = 1'b1;
                        buf_addr<=(FLIPX ? 319-buf_code : buf_code)&'h1FF;
                        buf_data<=((SHIFT_SPRITE_PRI ? sprite_attributes[59:56] + 1 : sprite_attributes[59:56]) << 12) + (palette&'h7F0)+sprite_code;
                    end else begin

                    end
                    
                    if(xflip ? tx == 0 : tx == 7) st<=30;
                    else st<=st;
                end
                30: begin
                    drawn_pixels = 8'h0;
                    buf_we<=1'b0;
                    if(xflip) tx<=7;
                    else tx<=0;
                    spr_x_render<=spr_x_render+1;
                    st<=17;
                end
            endcase
        end else begin

        end
    end
end

endmodule
module raizing_obj (
    input CLK,
    input CLK96,
    input PIXEL_CEN,
    input RESET,
    input RESET96,
    input [8:0] VRENDER,
    input [8:0] H,
    input ACTIVE,
    input HB,
    input VB,
    input FLIPX,

    input SHIFT_SPRITE_PRI,
    input FAST_OBJ_QUEUE,


    output reg [12:0] GP9001RAM_GCU_ADDR,
    input  [15:0] GP9001RAM_GCU_DOUT,
    output reg [12:0] GP9001RAM2_GCU_ADDR,
    input  [15:0] GP9001RAM2_GCU_DOUT,


    output reg GFX_CS,
    input GFX_OK,
    output reg [14:0] TILE_NUMBER,
    output reg [15:0] TILE_NUMBER_OFFS,
    output reg [3:0] TILE_BANK,
    input  [31:0] GFX_DATA,


    input signed [12:0] SPRITE_SCROLL_X,
    input signed [12:0] SPRITE_SCROLL_Y,
    input signed [12:0] SPRITE_SCROLL_XOFFS,
    input signed [12:0] SPRITE_SCROLL_YOFFS,

    output reg [14:0] OBJ_PIXEL
);


reg [7:0] st = 0;

localparam max_sprite = 256, max_priority = 16, max_sprite_num = 'h7FFFF; 
localparam spriteram_offs = 'h0;

reg [8:0] spr = 0;
wire [12:0] sprite_addr_base = spriteram_offs+(spr[7:0]*4);
reg last_HB = 0, start = 0;
wire pedg_HB = !HB && last_HB;
reg last_VB = 0;
reg busy = 1'b0;
wire [7:0] nb_pixels = {|GFX_DATA[31:28], |GFX_DATA[27:24], |GFX_DATA[23:20], |GFX_DATA[19:16], 
                        |GFX_DATA[15:12], |GFX_DATA[11:8], |GFX_DATA[7:4], |GFX_DATA[3:0]};
reg [7:0] drawn_pixels = 8'h0;

reg [4:0] priority_l;
reg signed [4:0] sprite_y_size_t;
reg signed [8:0] sprite_y_pos_t;
reg [8:0] sprite_queue_n = 0;
reg [8:0] sprite_queue_i = 0;
reg [(8*max_priority)-1:0] sprite_queue_priority_n = {max_priority{8'h00}};
reg [8:0] sprite_queue_priority_n_scan_buf_i = 0;
reg [4:0] priority_i = 0;
reg [8:0] spr_scan_i = 0;

reg [4:0] spr_x_render = 0;


reg [63:0] sprite_attributes = 64'h0;
reg [10:0] palette = 0;
reg [14:0] sprite_num = 0;
reg [2:0] sprite_bank = 0;
reg [4:0] sprite_x_size = 0, sprite_y_size = 0;
reg signed [9:0] sprite_x_pos = 0, sprite_y_pos = 0, multiconnector_x = 0, multiconnector_y = 0;
reg xflip = 0, yflip = 0;
reg [31:0] sprite_line = 0;
reg [2:0] tx = 0;


reg [14:0] buf_data = 0;
reg [8:0] buf_addr = 0;
reg buf_we = 1'b0;
wire [14:0] preobj_pxl;
wire [8:0] buf_addr_out = H;
jtframe_linebuf #(.DW(15)) u_obj_line (
    .clk(CLK96),
    .LHBL(~HB),
    .wr_addr(buf_addr),
    .pxl_cen(PIXEL_CEN),
    .wr_data(buf_data),
    .we(buf_we),
    .rd_addr(buf_addr_out),
    .rd_data(),
    .rd_gated(preobj_pxl)
);

wire [3:0] sprite_code = (((sprite_line >> (tx*4)) & 'h0F));
wire [9:0] buf_code = xflip ? (sprite_x_pos + (-8 * spr_x_render) - tx) + 7 :
                                (sprite_x_pos + (8 * spr_x_render) + tx);

wire [15:0] yclip = $signed(VRENDER) - sprite_y_pos;
wire [15:0] lines_down = yflip ? ((sprite_y_size+1)<<3) - yclip - 1 : yclip;
wire [15:0] tiles_across = spr_x_render<<5;
wire [15:0] tiles_down = lines_down[15:3] * ((sprite_x_size + 1) << 5);

wire [15:0] cur_row_lines_down = (lines_down & 7) << 2;
wire [15:0] tile_offs = tiles_down+cur_row_lines_down+tiles_across;

reg mc=0, yfl=0, xfl=0;
reg clr=0;
reg [8:0] clr_addr = 0;

reg [7:0] wr_spr_q;
reg [14:0] wr_spr_q_addr;
wire [14:0] rd_spr_q_addr = (priority_i*2048) + sprite_queue_i;
reg spr_q_we;
wire [7:0] spr_q_out;


jtframe_dual_ram #(.dw(8), .aw(15)) u_sprite_pri_q(
    .clk0(CLK96),
    .clk1(CLK96),
    .data0  (wr_spr_q),
    .addr0  (wr_spr_q_addr),
    .we0    (spr_q_we),
    .q0     (),

    .data1  (~8'h0),
    .addr1  (rd_spr_q_addr),
    .we1    (1'b0),
    .q1     (spr_q_out)
);

reg [15:0] pri_has_sprite = 16'd0;

integer i, c = 0;

always @(posedge CLK96, posedge RESET96) begin
    if(RESET96) begin
        OBJ_PIXEL<=0;
    end else begin
        if(PIXEL_CEN && ACTIVE) begin
            OBJ_PIXEL<=preobj_pxl;
        end
    end
    
end
reg [255:0] spr_idx_queue = 256'd0;
reg spr_idx_queue_reset = 1;

always @(posedge CLK96, posedge RESET96) begin
    if(RESET96) begin
        st<=0;
        start<=1'b0;
        busy<=1'b0;
        priority_l=0;
        sprite_y_size_t<=0;
        sprite_y_pos_t<=0;
        spr<=0;
        sprite_queue_n<=0;
        sprite_queue_i<=0;
        sprite_queue_priority_n<={max_priority{8'h00}};
        priority_i<=0;
        spr_scan_i<=0;
        sprite_queue_priority_n_scan_buf_i<=0;
        mc=0; 
        yfl=0;
        xfl=0;
        clr<=1'b1;
        clr_addr<=0;
        spr_x_render <= 0;
        sprite_attributes <= 64'h0;
        palette <= 0;
        sprite_num <= 0;
        sprite_bank <= 0;
        sprite_x_size <= 0; 
        sprite_y_size <= 0;
        sprite_x_pos <= 0; 
        sprite_y_pos <= 0; 
        multiconnector_x <= 0; 
        multiconnector_y <= 0;
        xflip <= 0;
        yflip <= 0;
        sprite_line <= 0;
        tx <= 0;
        buf_data <= 0;
        buf_addr <= 0;
        buf_we <= 1'b0;
        start<=0; 
        busy<=1'b0;
        TILE_NUMBER<=0;
        TILE_NUMBER_OFFS<=0;
        TILE_BANK<=0;
        GFX_CS<=0;
        GP9001RAM_GCU_ADDR<=0;
        GP9001RAM2_GCU_ADDR<=0;
        pri_has_sprite <= 16'd0;
        spr_q_we<=1'b0;
        spr_idx_queue <= 256'd0;
        spr_idx_queue_reset <= 1'b1;
    end else begin

        last_HB    <= HB;
        last_VB    <= VB;
        c<=c+1;

        if( (pedg_HB && !VB)  || (((!FLIPX && VRENDER == 0) || (FLIPX && VRENDER == 239)) && pedg_HB)) begin
            start <= 1'b1;
        end

        if(VB && !last_VB) begin
            spr_idx_queue<=256'd0;
            spr_idx_queue_reset<=1'b1;
        end

        if(start && !busy) begin

            start<=1'b0;
            busy<=1'b1;
            spr<=0;
            st<=0;
            priority_l=0;
            sprite_y_size_t<=0;
            sprite_y_pos_t<=0;
            sprite_queue_n<=0;
            sprite_queue_i<=0;
            sprite_queue_priority_n<={max_priority{8'h00}};
            priority_i<=0;
            spr_scan_i<=0;
            multiconnector_x<=SPRITE_SCROLL_X&'h1FF;
            multiconnector_y<=SPRITE_SCROLL_Y&'h1FF;
            sprite_queue_priority_n_scan_buf_i<=0;
            clr<=1'b1;
            clr_addr<=0;
            mc=0; 
            yfl=0;
            xfl=0;
            spr_x_render <= 0;
            sprite_attributes <= 64'h0;
            palette <= 0;
            sprite_num <= 0;
            sprite_bank <= 0;
            sprite_x_size <= 0; 
            sprite_y_size <= 0;
            sprite_x_pos <= 0; 
            sprite_y_pos <= 0; 
            xflip <= 0;
            yflip <= 0;
            sprite_line <= 0;
            tx <= 0;
            buf_data <= 0;
            buf_addr <= 0;            
            buf_we <= 1'b0;
            TILE_NUMBER<=0;
            TILE_NUMBER_OFFS<=0;
            TILE_BANK<=0;
            GFX_CS<=0;
            GP9001RAM_GCU_ADDR<=0;
            GP9001RAM2_GCU_ADDR<=0;
            pri_has_sprite <= 16'd0;
            spr_q_we<=1'b0;
        end else if(busy) begin
            st<=st+1;
            clr<=1'b1;


            case(st)
                0: begin
                    if(spr<max_sprite) begin
                        if(!spr_idx_queue_reset && !spr_idx_queue[spr] && FAST_OBJ_QUEUE) begin
                            spr<=spr+1;
                            st<=st;
                        end else begin
                            GP9001RAM_GCU_ADDR<= sprite_addr_base;
                            GP9001RAM2_GCU_ADDR<= sprite_addr_base+3;
                        end
                    end else begin
                        if(spr_idx_queue_reset) spr_idx_queue_reset<=0;

                        if(sprite_queue_n == 0) begin
                            busy<=0;
                            start<=1'b0;
                            spr_q_we<=1'b0;

                        end else begin
                            st<=4;

                            spr_q_we<=1'b0;
                            sprite_queue_priority_n_scan_buf_i<=0;
                            spr_scan_i<=0;
                            
                            priority_i<= pri_has_sprite[0] ? 0 :
                                         pri_has_sprite[1] ? 1 :
                                         pri_has_sprite[2] ? 2 :
                                         pri_has_sprite[3] ? 3 :
                                         pri_has_sprite[4] ? 4 :
                                         pri_has_sprite[5] ? 5 :
                                         pri_has_sprite[6] ? 6 :
                                         pri_has_sprite[7] ? 7 :
                                         pri_has_sprite[8] ? 8 :
                                         pri_has_sprite[9] ? 9 :
                                         pri_has_sprite[10] ? 10 :
                                         pri_has_sprite[11] ? 11 :
                                         pri_has_sprite[12] ? 12 :
                                         pri_has_sprite[13] ? 13 :
                                         pri_has_sprite[14] ? 14 :
                                         pri_has_sprite[15] ? 15 :
                                         16;
                        end
                    end
                end
                1: begin
                    spr_q_we<=1'b1;
                end
                2: begin
                    if(GP9001RAM_GCU_DOUT[15]) begin
                        if(spr_idx_queue_reset) begin
                            spr_idx_queue[spr]<=1;
                        end
                        mc=GP9001RAM_GCU_DOUT[14];
                        yfl= GP9001RAM_GCU_DOUT[13];
                        xfl= GP9001RAM_GCU_DOUT[12];
                        priority_l= GP9001RAM_GCU_DOUT[11:8];

                        sprite_y_size_t = GP9001RAM2_GCU_DOUT[3:0];
                        sprite_y_pos_t = !mc ? 
                                            (GP9001RAM2_GCU_DOUT[15:7] + SPRITE_SCROLL_Y + SPRITE_SCROLL_YOFFS) & 'h1FF :
                                            (multiconnector_y + GP9001RAM2_GCU_DOUT[15:7]) & 'h1FF;
                        
                        if(yfl) sprite_y_pos_t=sprite_y_pos_t-((sprite_y_size_t+1) << 3) + 1;
                        
                        if(sprite_y_pos_t > 384 || (sprite_y_pos_t > 448 && yfl)) sprite_y_pos_t = sprite_y_pos_t - 'h200;
                        
                        if(sprite_y_pos_t < 0 && $signed(VRENDER) < $signed(sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin 
                            sprite_y_size_t = (sprite_y_size_t+1) - (-sprite_y_pos_t>>3);
                            sprite_y_pos_t = 0;
                        end

                        if(VRENDER >= sprite_y_pos_t && VRENDER < (sprite_y_pos_t + ((sprite_y_size_t+1) << 3))) begin
                            $display("queue: %d, %d, %d, %h, %h, %h", VRENDER, sprite_y_size_t, sprite_y_pos_t, sprite_queue_priority_n[((priority_l+1)<<3)-1 -:8]+1, priority_l, spr[7:0]);
                            wr_spr_q <= spr[7:0];
                            wr_spr_q_addr<=((priority_l<<11) | sprite_queue_priority_n[((priority_l+1)<<3)-1 -: 8]);
                            pri_has_sprite[priority_l]<=1'b1;

                            sprite_queue_priority_n[((priority_l+1)<<3)-1 -:8] <= sprite_queue_priority_n[((priority_l+1)<<3)-1 -: 8]+1;
                            sprite_queue_n<=sprite_queue_n+1;                            
                        end

                        st<=0;
                        spr<=spr+1;
                    end else begin
                        st<=0;
                        spr<=spr+1;
                    end
                end
            endcase


            case(st)
                4: begin
                    pri_has_sprite[priority_i]<=0;
                    if(sprite_queue_priority_n[((priority_i+1)<<3)-1 -: 8] > 0 && priority_i<max_priority) begin

                        sprite_queue_priority_n_scan_buf_i <= sprite_queue_priority_n[((priority_i+1)<<3)-1 -: 8];
                        st<=5;

                        sprite_queue_i<=0;
                        tx<=0;
                        spr_x_render<=0;
                    end else begin

                        busy<=0;
                        start<=1'b0;
                        st<=0;

                    end
                end
            endcase


            case(st)
                6: begin
                    if(sprite_queue_i < sprite_queue_priority_n_scan_buf_i) begin

                        spr<=spr_q_out;
                        spr_x_render<=0;

                    end else begin
                        if(pri_has_sprite> 0) begin
                            st<=4;

                            sprite_queue_priority_n_scan_buf_i<=0;
                            spr_scan_i<=0;

                            priority_i<= pri_has_sprite[0] ? 0 :
                                         pri_has_sprite[1] ? 1 :
                                         pri_has_sprite[2] ? 2 :
                                         pri_has_sprite[3] ? 3 :
                                         pri_has_sprite[4] ? 4 :
                                         pri_has_sprite[5] ? 5 :
                                         pri_has_sprite[6] ? 6 :
                                         pri_has_sprite[7] ? 7 :
                                         pri_has_sprite[8] ? 8 :
                                         pri_has_sprite[9] ? 9 :
                                         pri_has_sprite[10] ? 10 :
                                         pri_has_sprite[11] ? 11 :
                                         pri_has_sprite[12] ? 12 :
                                         pri_has_sprite[13] ? 13 :
                                         pri_has_sprite[14] ? 14 :
                                         pri_has_sprite[15] ? 15 :
                                         16;
                        end else begin
                            busy<=0;
                            start<=1'b0;
                            st<=0;

                        end
                    end
                end
                7: begin
                    GP9001RAM_GCU_ADDR<=sprite_addr_base;
                    GP9001RAM2_GCU_ADDR <= sprite_addr_base + 1;
                end
                8:  begin
                    GP9001RAM_GCU_ADDR<=sprite_addr_base + 2;
                    GP9001RAM2_GCU_ADDR <= sprite_addr_base + 3;
                end
                9: begin
                    sprite_attributes[63:48] <= GP9001RAM_GCU_DOUT; 
                    sprite_attributes[47:32] <= GP9001RAM2_GCU_DOUT;
                end
                10: begin 
                    sprite_attributes[31:16] <= GP9001RAM_GCU_DOUT;
                    sprite_attributes[15:0] <= GP9001RAM2_GCU_DOUT;
                end
                11: begin

                    xflip<=sprite_attributes[60];
                    yflip<=sprite_attributes[61];
                    palette<=(sprite_attributes[55:50]<<4);
                    sprite_num<=(sprite_attributes[47:32] & 'h7FFF);
                    sprite_bank<=sprite_attributes[49:47];
                    sprite_x_size<=sprite_attributes[19:16];
                    sprite_y_size<=sprite_attributes[3:0];
                    
                    if(sprite_attributes[62]) begin
                        sprite_x_pos <= (multiconnector_x + sprite_attributes[31:23]) & 'h1ff;
                        sprite_y_pos <= (multiconnector_y + GP9001RAM2_GCU_DOUT[15:7]) & 'h1ff;
                    end else begin
                        sprite_x_pos<=(sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & 'h01FF;
                        sprite_y_pos<=(GP9001RAM2_GCU_DOUT[15:7]+SPRITE_SCROLL_Y+SPRITE_SCROLL_YOFFS) & 'h01FF;
                    end   
                end
                12: begin
                    $display("xpos: %d %d %d %d %d", sprite_x_pos, SPRITE_SCROLL_X, SPRITE_SCROLL_XOFFS, (sprite_attributes[31:23]+SPRITE_SCROLL_X+SPRITE_SCROLL_XOFFS) & 'h01FF, sprite_x_size);

                    

                    multiconnector_x<=sprite_x_pos;
                    multiconnector_y<=sprite_y_pos;

                    sprite_y_pos_t=sprite_y_pos;
                    sprite_y_size_t=sprite_y_size;

                    if(xflip) begin
                        if($signed(sprite_x_pos-7) > 448) begin
                            sprite_x_pos <= $signed(sprite_x_pos - 'h200 - 'd7);
                        end
                        else begin
                            sprite_x_pos<=$signed(sprite_x_pos-7);
                        end
                    end else begin

                        if($signed(sprite_x_pos) > 384) begin
                            sprite_x_pos <= $signed(sprite_x_pos - 'h200);
                        end
                    end

                    if(yflip) begin
                        if($signed(sprite_y_pos-((sprite_y_size + 1) << 3)) > 384) begin
                             sprite_y_pos_t = $signed(sprite_y_pos - 'h200);
                        end
                        else begin
                            sprite_y_pos_t=$signed(sprite_y_pos - ((sprite_y_size + 1) << 3));
                        end
                        sprite_y_pos_t = sprite_y_pos_t+1;
                    end

                    sprite_y_pos<=sprite_y_pos_t;
                    sprite_y_size<=sprite_y_size_t;


                    spr_x_render<=0;
                    if(xflip) tx<=7;
                    else tx<=0;

                    st<=15;
                end

                15: begin
                    if(spr_x_render < (sprite_x_size + 1)) begin
                        if(sprite_num <= max_sprite_num) begin
                            GFX_CS<=1'b1;
                            TILE_NUMBER<=sprite_num;
                            TILE_NUMBER_OFFS<=tile_offs;
                            TILE_BANK<=sprite_bank;
                        end else begin

                            st<=5;
                            tx<=0;
                            GFX_CS<=1'b0;
                            spr_x_render<=0;
                            buf_we<=1'b0;
                            sprite_queue_i<=sprite_queue_i+1;
                        end
                    end else begin

                        st<=5;
                        tx<=0;
                        GFX_CS<=1'b0;
                        spr_x_render<=0;
                        buf_we<=1'b0;
                        sprite_queue_i<=sprite_queue_i+1;
                    end
                end
                16: st<=17;
                17: begin
                    if(spr_x_render == (sprite_x_size + 1)) begin
                        st<=5;
                        tx<=0;
                        GFX_CS<=1'b0;
                        spr_x_render<=0;
                        buf_we<=1'b0;
                        sprite_queue_i<=sprite_queue_i+1;
                    end else begin
                        if(GFX_OK) begin
                            GFX_CS<=1'b0;
                            TILE_NUMBER<=0;
                            TILE_NUMBER_OFFS<=0;
                            TILE_BANK<=0;
                            sprite_line<=GFX_DATA;
                            $display("%d, %d, %d, %h, %h %h", VRENDER, sprite_x_pos, sprite_x_size, TILE_NUMBER, TILE_NUMBER_OFFS, GFX_DATA);

                            st<=18;
                            buf_we<=1'b1;
                            if(xflip) tx<=7;
                            else tx<= 0;
                            drawn_pixels = 8'h0;
                        end else begin
                            st<=st;
                        end
                    end
                end
                18: begin
                    if(spr_x_render + 1 < (sprite_x_size + 1)) begin
                        GFX_CS<=1'b1;
                        TILE_NUMBER<=sprite_num;
                        TILE_NUMBER_OFFS<=tile_offs+32;
                        TILE_BANK<=sprite_bank;
                    end
                    st<=22;
                end
                22: begin
                    if(xflip) tx<=tx-1;
                    else tx<= tx+1;


                    
                    if( sprite_code > 0 && buf_code>=0 && buf_code<320) begin
                        drawn_pixels[tx] = 1'b1;
                        buf_addr<=(FLIPX ? 319-buf_code : buf_code)&'h1FF;
                        buf_data<=((SHIFT_SPRITE_PRI ? sprite_attributes[59:56] + 1 : sprite_attributes[59:56]) << 12) + (palette&'h7F0)+sprite_code;
                    end else begin

                    end
                    
                    if(xflip ? tx == 0 : tx == 7) st<=30;
                    else st<=st;
                end
                30: begin
                    drawn_pixels = 8'h0;
                    buf_we<=1'b0;
                    if(xflip) tx<=7;
                    else tx<=0;
                    spr_x_render<=spr_x_render+1;
                    st<=17;
                end
            endcase
        end else begin

        end
    end
end

endmodule