data/full_repos/permissive/186443491/controller/rope_ram.v
 line:114: before: "."
1: b'%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:86: Cannot find file containing module: \'Score\'\n    Score my_score(\n    ^~~~~\n        ... Looked in:\n             data/full_repos/permissive/186443491/controller,data/full_repos/permissive/186443491/Score\n             data/full_repos/permissive/186443491/controller,data/full_repos/permissive/186443491/Score.v\n             data/full_repos/permissive/186443491/controller,data/full_repos/permissive/186443491/Score.sv\n             Score\n             Score.v\n             Score.sv\n             obj_dir/Score\n             obj_dir/Score.v\n             obj_dir/Score.sv\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:98: Cannot find file containing module: \'trig\'\n    trig deg_trig(\n    ^~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:108: Cannot find file containing module: \'key_detector\'\n    key_detector go_DET(\n    ^~~~~~~~~~~~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:142: Operator EQ expects 3 bits on the RHS, but RHS\'s CONST \'2\'h3\' generates 2 bits.\n                                                                              : ... In instance Rope\n        else if(level == 2\'d3)begin\n                      ^~\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:136: Operator EQ expects 3 bits on the RHS, but RHS\'s CONST \'2\'h2\' generates 2 bits.\n                                                                              : ... In instance Rope\n        else if(level == 2\'d2)begin\n                      ^~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:150: Cannot find file containing module: \'initialize_1\'\n initialize_1 initial_1(\n ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:158: Cannot find file containing module: \'initialize_2\'\n    initialize_2 initial_2(\n    ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:165: Cannot find file containing module: \'initialize_3\'\n    initialize_3 initial_3(\n    ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:173: Cannot find file containing module: \'initialize_4\'\n    initialize_4 initial_4(\n    ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:181: Cannot find file containing module: \'map1\'\n    map1 map1(\n    ^~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:187: Cannot find file containing module: \'map2\'\n    map2 map2(\n    ^~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:193: Cannot find file containing module: \'map3\'\n    map3 map3(\n    ^~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:198: Cannot find file containing module: \'map4\'\n    map4 map4(\n    ^~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:217: Cannot find file containing module: \'hex_decoder\'\n    hex_decoder H0(\n    ^~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:221: Cannot find file containing module: \'hex_decoder\'\n    hex_decoder H1(\n    ^~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:226: Cannot find file containing module: \'hex_decoder\'\n    hex_decoder H3(\n    ^~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:230: Cannot find file containing module: \'hex_decoder\'\n    hex_decoder H4(\n    ^~~~~~~~~~~\n%Error: data/full_repos/permissive/186443491/controller/rope_ram.v:234: Cannot find file containing module: \'hex_decoder\'\n    hex_decoder H5(\n    ^~~~~~~~~~~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:274: Operator ADD expects 64 bits on the LHS, but LHS\'s VARREF \'originX\' generates 10 bits.\n                                                                              : ... In instance Rope\n        endX = originX + ((rope_len * deg_cos) >> 8) * (deg_signCos ? 64\'d1 : -64\'d1);\n                       ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:274: Operator ASSIGN expects 10 bits on the Assign RHS, but Assign RHS\'s ADD generates 64 bits.\n                                                                              : ... In instance Rope\n        endX = originX + ((rope_len * deg_cos) >> 8) * (deg_signCos ? 64\'d1 : -64\'d1);\n             ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:275: Operator ASSIGN expects 10 bits on the Assign RHS, but Assign RHS\'s ADD generates 64 bits.\n                                                                              : ... In instance Rope\n        endY = originY + ((rope_len * deg_sin) >> 8) * 64\'d1;\n             ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:292: Operator GTE expects 5 bits on the RHS, but RHS\'s VARREF \'quantity\' generates 4 bits.\n                                                                              : ... In instance Rope\n                if(rope_index >= quantity)begin\n                              ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:316: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s VARREF \'ROPE_MIN\' generates 10 bits.\n                                                                              : ... In instance Rope\n                length = ROPE_MIN << 8;\n                                  ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:391: Operator GTE expects 64 bits on the LHS, but LHS\'s VARREF \'frame_counter\' generates 32 bits.\n                                                                              : ... In instance Rope\n                if (frame_counter >= (FRAME_CLOCK * 2 + (64\'b1 * found_stone * UP_DELAY_TIMES * FRAME_CLOCK))) begin\n                                  ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:400: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s VARREF \'DELTA_LEN\' generates 18 bits.\n                                                                              : ... In instance Rope\n                length = length - (DELTA_LEN << 8);\n                                             ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:422: Operator ASSIGN expects 5 bits on the Assign RHS, but Assign RHS\'s VARREF \'move_index\' generates 4 bits.\n                                                                              : ... In instance Rope\n                rope_index = move_index;\n                           ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:437: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s SEL generates 2 bits.\n                                                                              : ... In instance Rope\n                data_write = (tempType[1:0] << 2) + ((endX[8:0] - 4) << 23) + ((endY[7:0] - 4) << 11) + 2\'b11;\n                                            ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:437: Operator SUB expects 32 bits on the LHS, but LHS\'s SEL generates 9 bits.\n                                                                              : ... In instance Rope\n                data_write = (tempType[1:0] << 2) + ((endX[8:0] - 4) << 23) + ((endY[7:0] - 4) << 11) + 2\'b11;\n                                                                ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:437: Operator SUB expects 32 bits on the LHS, but LHS\'s SEL generates 8 bits.\n                                                                              : ... In instance Rope\n                data_write = (tempType[1:0] << 2) + ((endX[8:0] - 4) << 23) + ((endY[7:0] - 4) << 11) + 2\'b11;\n                                                                                          ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:437: Operator ADD expects 32 bits on the RHS, but RHS\'s CONST \'2\'h3\' generates 2 bits.\n                                                                              : ... In instance Rope\n                data_write = (tempType[1:0] << 2) + ((endX[8:0] - 4) << 23) + ((endY[7:0] - 4) << 11) + 2\'b11;\n                                                                                                      ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:507: Operator SHIFTL expects 32 bits on the LHS, but LHS\'s VARREF \'DELTA_LEN\' generates 18 bits.\n                                                                              : ... In instance Rope\n                length = length + (DELTA_LEN << 8);\n                                             ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:519: Operator GTE expects 5 bits on the RHS, but RHS\'s VARREF \'quantity\' generates 4 bits.\n                                                                              : ... In instance Rope\n                if (rope_index >= quantity) begin\n                               ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:549: Operator ASSIGN expects 4 bits on the Assign RHS, but Assign RHS\'s VARREF \'rope_index\' generates 5 bits.\n                                                                              : ... In instance Rope\n                            move_index = rope_index;\n                                       ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:544: Operator LT expects 14 bits on the RHS, but RHS\'s VARREF \'endX\' generates 10 bits.\n                                                                              : ... In instance Rope\n                else if (((tempX) < endX) & \n                                  ^\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:545: Operator LTE expects 32 or 14 bits on the LHS, but LHS\'s VARREF \'endX\' generates 10 bits.\n                                                                              : ... In instance Rope\n                        (endX <= (tempX + 16 < 320 ? tempX + 16: 320)) &\n                              ^~\n%Warning-WIDTH: data/full_repos/permissive/186443491/controller/rope_ram.v:547: Operator LTE expects 32 or 14 bits on the LHS, but LHS\'s VARREF \'endY\' generates 10 bits.\n                                                                              : ... In instance Rope\n                        (endY <= (tempY + 16 < 240 ? tempY + 16 : 240))) begin\n                              ^~\n%Error: Exiting due to 16 error(s), 20 warning(s)\n'
module Rope(
    input clock, resetn, enable,
    input draw_stone_flag,
    input [3:0] draw_index,
    input [3:0] quantity,
    input [2:0]level,

    input go_KEY,



    output reg[9:0] rotation_speed, line_speed, endX, endY, degree,
    output [9:0]rope_len,

    output [31:0] data,
    output [9:0] current_score,
    output reg reset_done,


    output [9:0]LEDR,
    output[6:0] HEX0, HEX1, HEX2, HEX3, HEX4, HEX5


 );
    parameter mode = 1'b0;
    parameter player = 1'b0;
    parameter originX = ((!mode) ? 10'd160 : (player ? 10'd237 : 10'd77));
    parameter originY = 64'd45;


    reg [1:0] state; 
    reg rCW;
    reg [31: 0] length;
    assign rope_len[9:0] = length[17:8];
    
    reg [4:0] current_state, next_state;


    parameter FRAME_CLOCK = 833_334;

    parameter ROPE_MIN = 10'd20;
    parameter UP_DELAY_TIMES = 3;
    parameter DELTA_LEN = 18'd5;
    

    reg [4:0] rope_index;
    reg [31:0] data_write;
    reg writeEn;
    

    reg [31:0]read_data;
    assign data = read_data;
	wire [3:0]read_address;
    assign read_address = draw_stone_flag ? draw_index : rope_index[3:0];


    reg [31:0] frame_counter;
    reg found_stone;
    reg [3:0] move_index;
    reg [31:0] tempData;    
    wire [1:0] tempType;
    assign tempType[1:0] = read_data[3:2];


    wire [13:0] tempX, tempY;
    assign tempX = {5'b0, read_data[31:23]};
    assign tempY = {6'b0, read_data[18:11]};



    parameter SCORE_STONE = 8'd10;
    parameter SCORE_GOLD = 8'd20;
    parameter SCORE_DIAMOND = 8'd50;
    reg scoreEn, scorePlus;
    reg [7:0] score_change;
    Score my_score(
        .clock(clock),
        .resetn(resetn),
        .writeEn(scoreEn),
        .plus(scorePlus),
        .score_change_DATA(score_change),
        .score(current_score)
    );
    

    wire [8:0] deg_sin, deg_cos;
    wire deg_signSin, deg_signCos;
    trig deg_trig(
        .degree(degree),
        .cos(deg_cos),
        .sin(deg_sin),
        .signCos(deg_signCos),
        .signSin(deg_signSin)
    );


    wire go, fire;
    key_detector go_DET(
        .clock(clock), 
        .resetn(resetn),
        .key(go_KEY),
        .pulse(go)
    );
    defparam go_DET.PULSE_LENGTH = FRAME_CLOCK;


    reg [31:0]data_write1, data_write2, data_write3, data_write4;
    reg writeEn1, writeEn2, writeEn3, writeEn4;
    wire [31:0]read_data1, read_data2, read_data3, read_data4;
    wire [31:0]data_mif1, data_mif2, data_mif3, data_mif4;
    reg [31:0]data_mif;

    always@(*)begin
        if(level == 0)begin
            data_write1 = data_write;
            read_data = read_data1;
            writeEn1 = writeEn;
            data_mif = data_mif1;
        end
        else if(level == 1)begin
            data_write2 = data_write;
            read_data = read_data2;
            writeEn2 = writeEn;
            data_mif = data_mif2;
        end
        else if(level == 2'd2)begin
            data_write3 = data_write;
            read_data = read_data3;
            writeEn3 = writeEn;
            data_mif = data_mif3;
        end
        else if(level == 2'd3)begin
            data_write4 = data_write;
            read_data = read_data4;
            writeEn4 = writeEn;
            data_mif =  data_mif4;
        end
    end
    
	initialize_1 initial_1(
        .address(read_address),
        .clock(clock),
        .data(data_write1),
        .wren(writeEn1),
        .q(read_data1)
    );

    initialize_2 initial_2(
        .address(read_address),
        .clock(clock),
        .data(data_write2),
        .wren(writeEn2),
        .q(read_data2)
    );
    initialize_3 initial_3(
        .address(read_address),
        .clock(clock),
        .data(data_write3),
        .wren(writeEn3),
        .q(read_data3)
    );

    initialize_4 initial_4(
        .address(read_address),
        .clock(clock),
        .data(data_write4),
        .wren(writeEn4),
        .q(read_data4)
    );

    map1 map1(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif1)
    );

    map2 map2(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif2)
    );

    map3 map3(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif3)
    );
    map4 map4(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif4)
    );













    assign LEDR[4:0] = current_state[4:0];
    hex_decoder H0(
        .hex_digit(rope_index), 
        .segments(HEX0)
        );
    hex_decoder H1(
        .hex_digit(read_address), 
        .segments(HEX1)
        );

    hex_decoder H3(
        .hex_digit(rope_len[3:0]), 
        .segments(HEX3)
        );
    hex_decoder H4(
        .hex_digit(rope_len[7:4]), 
        .segments(HEX4)
        );
    hex_decoder H5(
        .hex_digit({3'b0,rope_len[8]}), 
        .segments(HEX5)
        );



    localparam  S_START        = 5'd25,
                S_RESET_MEMORY = 5'd24,
                S_RESET_MEMORY_WAIT = 5'd26,
                S_RESET_MEMORY_WRITE = 5'd28,
                S_RESET_MEMORY_AFTER = 5'd27,
                S_STOP      = 5'd0,
                S_PRE_RCCW  = 5'd1,
                S_IN_RCCW   = 5'd2,
                S_PRE_RCW   = 5'd3,
                S_IN_RCW    = 5'd4,
                S_PRE_UP    = 5'd5,
                S_IN_UP     = 5'd6,
                S_PRE_DOWN  = 5'd7,
                S_IN_DOWN   = 5'd8,
                S_MOVE      = 5'd9,
                S_MOVE_READ = 5'd14,
                S_MOVE_READ_WAIT    = 5'd15,
                S_MOVE_NEW_XY       = 5'd18,
                S_MOVE_WRITE= 5'd16,
                S_MOVE_WRITE_WAIT   = 5'd17,
                S_MOVE_DELAY    = 5'd22,
                S_AFTER_MOVE    = 5'd21,
                S_AFTER_MOVE_WAIT   = 5'd23,
                S_UP_DELAY  = 5'd10,
                S_PRE_CHECK = 5'd11,
                S_IN_CHECK  = 5'd20,
                S_IN_CHECK_READ     = 5'd12,
                S_IN_CHECK_CHECK    = 5'd19,
                S_SAVE      = 5'd13,
                S_WAIT_FOR_LIVE = 5'd23;

    always @(posedge clock) begin

        endX = originX + ((rope_len * deg_cos) >> 8) * (deg_signCos ? 64'd1 : -64'd1);
        endY = originY + ((rope_len * deg_sin) >> 8) * 64'd1;

        scoreEn = 0;
        writeEn = 0;
        reset_done = 0;

        case (current_state)
            S_START:begin
                frame_counter = 0;
                found_stone = 0;
                tempData = 0;
                rope_index = 0;
                data_write = 0;
                move_index = 0;
                next_state = (enable)? S_RESET_MEMORY : S_START;
            end
            S_RESET_MEMORY:begin
                if(rope_index >= quantity)begin
                    reset_done = 1'b1;
                    next_state = S_STOP;
                end
                else begin
                    data_write = data_mif;
                    next_state = S_RESET_MEMORY_WAIT;
                    end
            end
            S_RESET_MEMORY_WAIT:begin
                next_state = S_RESET_MEMORY_WRITE;
            end
            S_RESET_MEMORY_WRITE :begin
                writeEn = 1'b1;
                next_state = S_RESET_MEMORY_AFTER;
            end
            S_RESET_MEMORY_AFTER:begin
              rope_index = rope_index  + 1'b1;
              next_state = S_RESET_MEMORY;
            end

            S_STOP: begin
                state = 2'b00;
                rCW = 0;
                length = ROPE_MIN << 8;
                degree = 10'd90;
                next_state = S_PRE_RCCW;

                frame_counter = 0;
                found_stone = 0;
                tempData = 0;
                rope_index = 0;
                data_write = 0;
                move_index = 0;
            end
            S_PRE_RCCW: begin
                if (rope_len < ROPE_MIN + 1) begin

                    if (found_stone)
                        next_state = S_MOVE;
                    else if (rCW)
                        next_state = S_PRE_RCW;
                    else
                        next_state = S_PRE_RCCW;
                end
                found_stone = 0;
                frame_counter = frame_counter + 1;
                if (frame_counter >= FRAME_CLOCK) begin
                    next_state = S_IN_RCCW;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_RCCW;
                end
            end
            S_IN_RCCW: begin
                degree = degree - 1;
                rCW = 0;
                if (degree < 15 + 1)
                    next_state = S_PRE_RCW;
                else if (go)
                    next_state = S_PRE_DOWN;
                else
                    next_state = S_PRE_RCCW;
                frame_counter = 0;
            end
            S_PRE_RCW: begin
                if (rope_len < ROPE_MIN + 1) begin

                    if (found_stone)
                        next_state = S_MOVE;
                    else if (rCW)
                        next_state = S_PRE_RCW;
                    else
                        next_state = S_PRE_RCCW;
                end
                found_stone = 0;
                frame_counter = frame_counter + 1;
                if (frame_counter >= FRAME_CLOCK) begin
                    next_state = S_IN_RCW;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_RCW;
                end
            end
            S_IN_RCW: begin
                degree = degree + 1;
                rCW = 1;
                if (degree >= 165 - 1)
                    next_state = S_PRE_RCCW;
                else if (go)
                    next_state = S_PRE_DOWN;
                else
                    next_state = S_PRE_RCW;
                frame_counter = 0;
            end
            S_PRE_UP: begin
                frame_counter = frame_counter + 1;
                if (frame_counter >= (FRAME_CLOCK * 2 + (64'b1 * found_stone * UP_DELAY_TIMES * FRAME_CLOCK))) begin
                    next_state = S_IN_UP;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_UP;
                end
            end
            S_IN_UP: begin
                length = length - (DELTA_LEN << 8);
                if (rope_len < ROPE_MIN + 1) begin

                    if (found_stone)
                        next_state = S_MOVE;
                    else if (rCW)
                        next_state = S_PRE_RCW;
                    else
                        next_state = S_PRE_RCCW;
                end
                else begin
                    if (found_stone)
                        next_state = S_MOVE;
                    else
                        next_state = S_PRE_UP;
                end
                frame_counter = 0;
            end
            S_MOVE: begin
                next_state = S_MOVE_READ;
            end
            S_MOVE_READ: begin
                rope_index = move_index;
                next_state = S_MOVE_READ_WAIT;
            end
            S_MOVE_READ_WAIT: begin
                if (draw_stone_flag)
                    next_state = S_MOVE_READ_WAIT;
                else begin 
                    tempData = read_data;
                    next_state = S_MOVE_NEW_XY;
                end
            end
            S_MOVE_NEW_XY: begin



                data_write = (tempType[1:0] << 2) + ((endX[8:0] - 4) << 23) + ((endY[7:0] - 4) << 11) + 2'b11;

                next_state = S_MOVE_WRITE;
            end
            S_MOVE_WRITE: begin



                next_state = S_MOVE_WRITE_WAIT;
            end
            S_MOVE_WRITE_WAIT: begin
                if (draw_stone_flag)
                    next_state = S_MOVE_WRITE_WAIT;
                else begin 
                    writeEn = 1;

                    if (rope_len < ROPE_MIN + 10'd10) begin
                        next_state = S_AFTER_MOVE;
                    end
                    else begin

                        next_state = S_PRE_UP;
                    end
                    frame_counter = 0;
                end
            end











            S_AFTER_MOVE: begin

                scorePlus = 1'b1;
                scoreEn = 1'b1;
                score_change = (tempType == 2'b0 ? SCORE_STONE : 
                        (tempType == 2'b1 ? SCORE_GOLD : SCORE_DIAMOND));

                data_write[1:0] = 2'b0;
                next_state = S_AFTER_MOVE_WAIT;
            end
            S_AFTER_MOVE_WAIT: begin
                if (draw_stone_flag)
                    next_state = S_AFTER_MOVE_WAIT;
                else begin 
                    writeEn = 1;
                    found_stone = 0;
                    next_state = S_PRE_UP;
                    frame_counter = 0;
                end
            end
            S_PRE_DOWN: begin
                found_stone = 0;
                frame_counter = frame_counter + 1;
                if (frame_counter >= FRAME_CLOCK) begin
                    next_state = S_IN_DOWN;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_DOWN;
                end
            end
            S_IN_DOWN: begin
                frame_counter = 0;
                length = length + (DELTA_LEN << 8);
                if (endX < 2 | endX >= 318 | endY >= 238 ) begin
                    next_state = S_IN_UP;
                end
                else
                    next_state = S_PRE_CHECK;
            end
            S_PRE_CHECK: begin
                rope_index = 0;
                next_state = S_IN_CHECK;
            end
            S_IN_CHECK: begin
                if (rope_index >= quantity) begin

                    next_state = S_PRE_DOWN;
                    frame_counter = 0;
                end
                else 
                    next_state = S_IN_CHECK_READ;
            end
            S_IN_CHECK_READ: begin
                if (draw_stone_flag)
                    next_state = S_IN_CHECK_READ;
                else begin 
                    tempData <= read_data;
                    next_state = S_IN_CHECK_CHECK;
                end
            end
            S_IN_CHECK_CHECK: begin
                if (tempData[0] | !tempData[1]) begin
                    rope_index = rope_index + 1;
                    next_state = S_IN_CHECK;
                end




                else if (((tempX) < endX) & 
                        (endX <= (tempX + 16 < 320 ? tempX + 16: 320)) &
                        ((tempY - 6 > 0 ? tempY - 6 : 0) < endY) & 
                        (endY <= (tempY + 16 < 240 ? tempY + 16 : 240))) begin
                            found_stone = 1;
                            move_index = rope_index;
                            next_state = S_SAVE;
                            frame_counter = 0;
                end
                else begin
                    next_state = S_IN_CHECK;
                    rope_index = rope_index + 1;
                end
            end
            S_SAVE: begin

                tempData[0] = 1;
                next_state = S_PRE_UP;
                frame_counter = 0;
            end
            S_WAIT_FOR_LIVE: begin

            end
          default: next_state = S_START;
        endcase
    end

    always @(posedge clock) begin
        if (!resetn) begin
            current_state <= S_START;
        end            



        else begin
            current_state <= next_state;
        end
    end

endmodule
module Rope(
    input clock, resetn, enable,
    input draw_stone_flag,
    input [3:0] draw_index,
    input [3:0] quantity,
    input [2:0]level,

    input go_KEY,



    output reg[9:0] rotation_speed, line_speed, endX, endY, degree,
    output [9:0]rope_len,

    output [31:0] data,
    output [9:0] current_score,
    output reg reset_done,


    output [9:0]LEDR,
    output[6:0] HEX0, HEX1, HEX2, HEX3, HEX4, HEX5


 );

    parameter mode = 1'b0;
    parameter player = 1'b0;
    parameter originX = ((!mode) ? 10'd160 : (player ? 10'd237 : 10'd77));
    parameter originY = 64'd45;


    reg [1:0] state; 
    reg rCW;
    reg [31: 0] length;
    assign rope_len[9:0] = length[17:8];
    
    reg [4:0] current_state, next_state;


    parameter FRAME_CLOCK = 833_334;

    parameter ROPE_MIN = 10'd20;
    parameter UP_DELAY_TIMES = 3;
    parameter DELTA_LEN = 18'd5;
    

    reg [4:0] rope_index;
    reg [31:0] data_write;
    reg writeEn;
    

    reg [31:0]read_data;
    assign data = read_data;
	wire [3:0]read_address;
    assign read_address = draw_stone_flag ? draw_index : rope_index[3:0];


    reg [31:0] frame_counter;
    reg found_stone;
    reg [3:0] move_index;
    reg [31:0] tempData;    
    wire [1:0] tempType;
    assign tempType[1:0] = read_data[3:2];


    wire [13:0] tempX, tempY;
    assign tempX = {5'b0, read_data[31:23]};
    assign tempY = {6'b0, read_data[18:11]};



    parameter SCORE_STONE = 8'd10;
    parameter SCORE_GOLD = 8'd20;
    parameter SCORE_DIAMOND = 8'd50;
    reg scoreEn, scorePlus;
    reg [7:0] score_change;
    Score my_score(
        .clock(clock),
        .resetn(resetn),
        .writeEn(scoreEn),
        .plus(scorePlus),
        .score_change_DATA(score_change),
        .score(current_score)
    );
    

    wire [8:0] deg_sin, deg_cos;
    wire deg_signSin, deg_signCos;
    trig deg_trig(
        .degree(degree),
        .cos(deg_cos),
        .sin(deg_sin),
        .signCos(deg_signCos),
        .signSin(deg_signSin)
    );


    wire go, fire;
    key_detector go_DET(
        .clock(clock), 
        .resetn(resetn),
        .key(go_KEY),
        .pulse(go)
    );
    defparam go_DET.PULSE_LENGTH = FRAME_CLOCK;


    reg [31:0]data_write1, data_write2, data_write3, data_write4;
    reg writeEn1, writeEn2, writeEn3, writeEn4;
    wire [31:0]read_data1, read_data2, read_data3, read_data4;
    wire [31:0]data_mif1, data_mif2, data_mif3, data_mif4;
    reg [31:0]data_mif;

    always@(*)begin
        if(level == 0)begin
            data_write1 = data_write;
            read_data = read_data1;
            writeEn1 = writeEn;
            data_mif = data_mif1;
        end
        else if(level == 1)begin
            data_write2 = data_write;
            read_data = read_data2;
            writeEn2 = writeEn;
            data_mif = data_mif2;
        end
        else if(level == 2'd2)begin
            data_write3 = data_write;
            read_data = read_data3;
            writeEn3 = writeEn;
            data_mif = data_mif3;
        end
        else if(level == 2'd3)begin
            data_write4 = data_write;
            read_data = read_data4;
            writeEn4 = writeEn;
            data_mif =  data_mif4;
        end
    end
    
	initialize_1 initial_1(
        .address(read_address),
        .clock(clock),
        .data(data_write1),
        .wren(writeEn1),
        .q(read_data1)
    );

    initialize_2 initial_2(
        .address(read_address),
        .clock(clock),
        .data(data_write2),
        .wren(writeEn2),
        .q(read_data2)
    );
    initialize_3 initial_3(
        .address(read_address),
        .clock(clock),
        .data(data_write3),
        .wren(writeEn3),
        .q(read_data3)
    );

    initialize_4 initial_4(
        .address(read_address),
        .clock(clock),
        .data(data_write4),
        .wren(writeEn4),
        .q(read_data4)
    );

    map1 map1(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif1)
    );

    map2 map2(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif2)
    );

    map3 map3(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif3)
    );
    map4 map4(
        .address(read_address),
	    .clock(clock),
	    .q(data_mif4)
    );













    assign LEDR[4:0] = current_state[4:0];
    hex_decoder H0(
        .hex_digit(rope_index), 
        .segments(HEX0)
        );
    hex_decoder H1(
        .hex_digit(read_address), 
        .segments(HEX1)
        );

    hex_decoder H3(
        .hex_digit(rope_len[3:0]), 
        .segments(HEX3)
        );
    hex_decoder H4(
        .hex_digit(rope_len[7:4]), 
        .segments(HEX4)
        );
    hex_decoder H5(
        .hex_digit({3'b0,rope_len[8]}), 
        .segments(HEX5)
        );



    localparam  S_START        = 5'd25,
                S_RESET_MEMORY = 5'd24,
                S_RESET_MEMORY_WAIT = 5'd26,
                S_RESET_MEMORY_WRITE = 5'd28,
                S_RESET_MEMORY_AFTER = 5'd27,
                S_STOP      = 5'd0,
                S_PRE_RCCW  = 5'd1,
                S_IN_RCCW   = 5'd2,
                S_PRE_RCW   = 5'd3,
                S_IN_RCW    = 5'd4,
                S_PRE_UP    = 5'd5,
                S_IN_UP     = 5'd6,
                S_PRE_DOWN  = 5'd7,
                S_IN_DOWN   = 5'd8,
                S_MOVE      = 5'd9,
                S_MOVE_READ = 5'd14,
                S_MOVE_READ_WAIT    = 5'd15,
                S_MOVE_NEW_XY       = 5'd18,
                S_MOVE_WRITE= 5'd16,
                S_MOVE_WRITE_WAIT   = 5'd17,
                S_MOVE_DELAY    = 5'd22,
                S_AFTER_MOVE    = 5'd21,
                S_AFTER_MOVE_WAIT   = 5'd23,
                S_UP_DELAY  = 5'd10,
                S_PRE_CHECK = 5'd11,
                S_IN_CHECK  = 5'd20,
                S_IN_CHECK_READ     = 5'd12,
                S_IN_CHECK_CHECK    = 5'd19,
                S_SAVE      = 5'd13,
                S_WAIT_FOR_LIVE = 5'd23;

    always @(posedge clock) begin

        endX = originX + ((rope_len * deg_cos) >> 8) * (deg_signCos ? 64'd1 : -64'd1);
        endY = originY + ((rope_len * deg_sin) >> 8) * 64'd1;

        scoreEn = 0;
        writeEn = 0;
        reset_done = 0;

        case (current_state)
            S_START:begin
                frame_counter = 0;
                found_stone = 0;
                tempData = 0;
                rope_index = 0;
                data_write = 0;
                move_index = 0;
                next_state = (enable)? S_RESET_MEMORY : S_START;
            end
            S_RESET_MEMORY:begin
                if(rope_index >= quantity)begin
                    reset_done = 1'b1;
                    next_state = S_STOP;
                end
                else begin
                    data_write = data_mif;
                    next_state = S_RESET_MEMORY_WAIT;
                    end
            end
            S_RESET_MEMORY_WAIT:begin
                next_state = S_RESET_MEMORY_WRITE;
            end
            S_RESET_MEMORY_WRITE :begin
                writeEn = 1'b1;
                next_state = S_RESET_MEMORY_AFTER;
            end
            S_RESET_MEMORY_AFTER:begin
              rope_index = rope_index  + 1'b1;
              next_state = S_RESET_MEMORY;
            end

            S_STOP: begin
                state = 2'b00;
                rCW = 0;
                length = ROPE_MIN << 8;
                degree = 10'd90;
                next_state = S_PRE_RCCW;

                frame_counter = 0;
                found_stone = 0;
                tempData = 0;
                rope_index = 0;
                data_write = 0;
                move_index = 0;
            end
            S_PRE_RCCW: begin
                if (rope_len < ROPE_MIN + 1) begin

                    if (found_stone)
                        next_state = S_MOVE;
                    else if (rCW)
                        next_state = S_PRE_RCW;
                    else
                        next_state = S_PRE_RCCW;
                end
                found_stone = 0;
                frame_counter = frame_counter + 1;
                if (frame_counter >= FRAME_CLOCK) begin
                    next_state = S_IN_RCCW;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_RCCW;
                end
            end
            S_IN_RCCW: begin
                degree = degree - 1;
                rCW = 0;
                if (degree < 15 + 1)
                    next_state = S_PRE_RCW;
                else if (go)
                    next_state = S_PRE_DOWN;
                else
                    next_state = S_PRE_RCCW;
                frame_counter = 0;
            end
            S_PRE_RCW: begin
                if (rope_len < ROPE_MIN + 1) begin

                    if (found_stone)
                        next_state = S_MOVE;
                    else if (rCW)
                        next_state = S_PRE_RCW;
                    else
                        next_state = S_PRE_RCCW;
                end
                found_stone = 0;
                frame_counter = frame_counter + 1;
                if (frame_counter >= FRAME_CLOCK) begin
                    next_state = S_IN_RCW;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_RCW;
                end
            end
            S_IN_RCW: begin
                degree = degree + 1;
                rCW = 1;
                if (degree >= 165 - 1)
                    next_state = S_PRE_RCCW;
                else if (go)
                    next_state = S_PRE_DOWN;
                else
                    next_state = S_PRE_RCW;
                frame_counter = 0;
            end
            S_PRE_UP: begin
                frame_counter = frame_counter + 1;
                if (frame_counter >= (FRAME_CLOCK * 2 + (64'b1 * found_stone * UP_DELAY_TIMES * FRAME_CLOCK))) begin
                    next_state = S_IN_UP;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_UP;
                end
            end
            S_IN_UP: begin
                length = length - (DELTA_LEN << 8);
                if (rope_len < ROPE_MIN + 1) begin

                    if (found_stone)
                        next_state = S_MOVE;
                    else if (rCW)
                        next_state = S_PRE_RCW;
                    else
                        next_state = S_PRE_RCCW;
                end
                else begin
                    if (found_stone)
                        next_state = S_MOVE;
                    else
                        next_state = S_PRE_UP;
                end
                frame_counter = 0;
            end
            S_MOVE: begin
                next_state = S_MOVE_READ;
            end
            S_MOVE_READ: begin
                rope_index = move_index;
                next_state = S_MOVE_READ_WAIT;
            end
            S_MOVE_READ_WAIT: begin
                if (draw_stone_flag)
                    next_state = S_MOVE_READ_WAIT;
                else begin 
                    tempData = read_data;
                    next_state = S_MOVE_NEW_XY;
                end
            end
            S_MOVE_NEW_XY: begin



                data_write = (tempType[1:0] << 2) + ((endX[8:0] - 4) << 23) + ((endY[7:0] - 4) << 11) + 2'b11;

                next_state = S_MOVE_WRITE;
            end
            S_MOVE_WRITE: begin



                next_state = S_MOVE_WRITE_WAIT;
            end
            S_MOVE_WRITE_WAIT: begin
                if (draw_stone_flag)
                    next_state = S_MOVE_WRITE_WAIT;
                else begin 
                    writeEn = 1;

                    if (rope_len < ROPE_MIN + 10'd10) begin
                        next_state = S_AFTER_MOVE;
                    end
                    else begin

                        next_state = S_PRE_UP;
                    end
                    frame_counter = 0;
                end
            end











            S_AFTER_MOVE: begin

                scorePlus = 1'b1;
                scoreEn = 1'b1;
                score_change = (tempType == 2'b0 ? SCORE_STONE : 
                        (tempType == 2'b1 ? SCORE_GOLD : SCORE_DIAMOND));

                data_write[1:0] = 2'b0;
                next_state = S_AFTER_MOVE_WAIT;
            end
            S_AFTER_MOVE_WAIT: begin
                if (draw_stone_flag)
                    next_state = S_AFTER_MOVE_WAIT;
                else begin 
                    writeEn = 1;
                    found_stone = 0;
                    next_state = S_PRE_UP;
                    frame_counter = 0;
                end
            end
            S_PRE_DOWN: begin
                found_stone = 0;
                frame_counter = frame_counter + 1;
                if (frame_counter >= FRAME_CLOCK) begin
                    next_state = S_IN_DOWN;
                    frame_counter = 0;
                end
                else begin
                    next_state = S_PRE_DOWN;
                end
            end
            S_IN_DOWN: begin
                frame_counter = 0;
                length = length + (DELTA_LEN << 8);
                if (endX < 2 | endX >= 318 | endY >= 238 ) begin
                    next_state = S_IN_UP;
                end
                else
                    next_state = S_PRE_CHECK;
            end
            S_PRE_CHECK: begin
                rope_index = 0;
                next_state = S_IN_CHECK;
            end
            S_IN_CHECK: begin
                if (rope_index >= quantity) begin

                    next_state = S_PRE_DOWN;
                    frame_counter = 0;
                end
                else 
                    next_state = S_IN_CHECK_READ;
            end
            S_IN_CHECK_READ: begin
                if (draw_stone_flag)
                    next_state = S_IN_CHECK_READ;
                else begin 
                    tempData <= read_data;
                    next_state = S_IN_CHECK_CHECK;
                end
            end
            S_IN_CHECK_CHECK: begin
                if (tempData[0] | !tempData[1]) begin
                    rope_index = rope_index + 1;
                    next_state = S_IN_CHECK;
                end




                else if (((tempX) < endX) & 
                        (endX <= (tempX + 16 < 320 ? tempX + 16: 320)) &
                        ((tempY - 6 > 0 ? tempY - 6 : 0) < endY) & 
                        (endY <= (tempY + 16 < 240 ? tempY + 16 : 240))) begin
                            found_stone = 1;
                            move_index = rope_index;
                            next_state = S_SAVE;
                            frame_counter = 0;
                end
                else begin
                    next_state = S_IN_CHECK;
                    rope_index = rope_index + 1;
                end
            end
            S_SAVE: begin

                tempData[0] = 1;
                next_state = S_PRE_UP;
                frame_counter = 0;
            end
            S_WAIT_FOR_LIVE: begin

            end
          default: next_state = S_START;
        endcase
    end

    always @(posedge clock) begin
        if (!resetn) begin
            current_state <= S_START;
        end            



        else begin
            current_state <= next_state;
        end
    end

endmodule