data/full_repos/permissive/102543783/hdl/snes_gamepad.v
1: b'%Warning-WIDTH: data/full_repos/permissive/102543783/hdl/snes_gamepad.v:119: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'counter_ff\' generates 11 bits.\n                                                                           : ... In instance snes_gamepad\n                        if(counter_ff)\n                        ^~\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/102543783/hdl/snes_gamepad.v:153: Operator ASSIGN expects 4 bits on the Assign RHS, but Assign RHS\'s ADD generates 5 bits.\n                                                                           : ... In instance snes_gamepad\n                                        btn_counter_ns = btn_counter_ff + 5\'d1;\n                                                       ^\n%Warning-WIDTH: data/full_repos/permissive/102543783/hdl/snes_gamepad.v:135: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'counter_ff\' generates 11 bits.\n                                                                           : ... In instance snes_gamepad\n                        if(counter_ff)\n                        ^~\n%Error: Exiting due to 3 warning(s)\n'
module snes_gamepad
    (
    input wire clk,
    input wire rst,
    
    input wire rd,
    output reg busy,
    
    output wire snes_clk,
    output reg snes_latch,
    input wire snes_data,
    
    output wire [15:0] buttons
    );
    
    

    
    localparam S_IDLE  = 2'b00,
               S_LATCH = 2'b01,
               S_CLOCK = 2'b10;
    
    localparam CLOCK_VALUE = 11'd599,
               LATCH_VALUE = 11'd1199;
    
    

    
    reg [1:0] state_ff, state_ns;
    reg [10:0] counter_ff, counter_ns;
    
    reg snes_clk_ff, snes_clk_ns;
    
    reg [3:0] btn_counter_ff, btn_counter_ns;
    reg [15:0] buttons_ff, buttons_ns;
    
    

    
    always @(posedge clk, posedge rst)
        if(rst)
            begin
                state_ff <= S_IDLE;
                counter_ff <= 11'd0;
                
                snes_clk_ff <= 1'b1;
                
                btn_counter_ff <= 4'd0;
                buttons_ff <= {16{1'b1}};
            end
        else
            begin
                state_ff <= state_ns;
                counter_ff <= counter_ns;
                
                snes_clk_ff <= snes_clk_ns;
                
                btn_counter_ff <= btn_counter_ns;
                buttons_ff <= buttons_ns;
            end
    
    

    
    always @*
        begin
            state_ns = state_ff;
            counter_ns = counter_ff;
            
            busy = 1'b0;
            
            snes_latch = 1'b0;
            snes_clk_ns = snes_clk_ff;
            
            btn_counter_ns = btn_counter_ff;
            buttons_ns = buttons_ff;
            
            case(state_ff)
                S_IDLE:
                    begin                    
                        if(rd)
                            begin

                                counter_ns = LATCH_VALUE;
                                btn_counter_ns = 4'd0;
                                buttons_ns = {16{1'b1}};
                                
                                state_ns = S_LATCH;
                            end
                    end
                
                S_LATCH:
                    begin
                        busy = 1'b1;
                        snes_latch = 1'b1;
                    

                        if(counter_ff)
                            begin
                                counter_ns = counter_ff - 11'd1;
                            end
                        else
                            begin
                                counter_ns = CLOCK_VALUE;
                                state_ns = S_CLOCK;
                            end
                    end
                
                S_CLOCK:
                    begin
                        busy = 1'b1;
                        

                        if(counter_ff)
                            begin
                                counter_ns = counter_ff - 11'd1;
                            end
                        else
                            begin

                                counter_ns = CLOCK_VALUE;
                                snes_clk_ns = ~snes_clk_ff;
                                
                                if(snes_clk_ff)

                                    buttons_ns = {snes_data, buttons_ff[15:1]};
                                else

                                    if(btn_counter_ff == 4'd15)
                                        state_ns = S_IDLE;
                                    else
                                        btn_counter_ns = btn_counter_ff + 5'd1;
                            end
                    end
                
                default:
                    begin
                        state_ns = S_IDLE;
                    end
            endcase
        end
    
    

    
    assign snes_clk = snes_clk_ff;
    assign buttons = ~buttons_ff;
    
endmodule
module snes_gamepad
    (
    input wire clk,
    input wire rst,
    
    input wire rd,
    output reg busy,
    
    output wire snes_clk,
    output reg snes_latch,
    input wire snes_data,
    
    output wire [15:0] buttons
    );

    
    

    
    localparam S_IDLE  = 2'b00,
               S_LATCH = 2'b01,
               S_CLOCK = 2'b10;
    
    localparam CLOCK_VALUE = 11'd599,
               LATCH_VALUE = 11'd1199;
    
    

    
    reg [1:0] state_ff, state_ns;
    reg [10:0] counter_ff, counter_ns;
    
    reg snes_clk_ff, snes_clk_ns;
    
    reg [3:0] btn_counter_ff, btn_counter_ns;
    reg [15:0] buttons_ff, buttons_ns;
    
    

    
    always @(posedge clk, posedge rst)
        if(rst)
            begin
                state_ff <= S_IDLE;
                counter_ff <= 11'd0;
                
                snes_clk_ff <= 1'b1;
                
                btn_counter_ff <= 4'd0;
                buttons_ff <= {16{1'b1}};
            end
        else
            begin
                state_ff <= state_ns;
                counter_ff <= counter_ns;
                
                snes_clk_ff <= snes_clk_ns;
                
                btn_counter_ff <= btn_counter_ns;
                buttons_ff <= buttons_ns;
            end
    
    

    
    always @*
        begin
            state_ns = state_ff;
            counter_ns = counter_ff;
            
            busy = 1'b0;
            
            snes_latch = 1'b0;
            snes_clk_ns = snes_clk_ff;
            
            btn_counter_ns = btn_counter_ff;
            buttons_ns = buttons_ff;
            
            case(state_ff)
                S_IDLE:
                    begin                    
                        if(rd)
                            begin

                                counter_ns = LATCH_VALUE;
                                btn_counter_ns = 4'd0;
                                buttons_ns = {16{1'b1}};
                                
                                state_ns = S_LATCH;
                            end
                    end
                
                S_LATCH:
                    begin
                        busy = 1'b1;
                        snes_latch = 1'b1;
                    

                        if(counter_ff)
                            begin
                                counter_ns = counter_ff - 11'd1;
                            end
                        else
                            begin
                                counter_ns = CLOCK_VALUE;
                                state_ns = S_CLOCK;
                            end
                    end
                
                S_CLOCK:
                    begin
                        busy = 1'b1;
                        

                        if(counter_ff)
                            begin
                                counter_ns = counter_ff - 11'd1;
                            end
                        else
                            begin

                                counter_ns = CLOCK_VALUE;
                                snes_clk_ns = ~snes_clk_ff;
                                
                                if(snes_clk_ff)

                                    buttons_ns = {snes_data, buttons_ff[15:1]};
                                else

                                    if(btn_counter_ff == 4'd15)
                                        state_ns = S_IDLE;
                                    else
                                        btn_counter_ns = btn_counter_ff + 5'd1;
                            end
                    end
                
                default:
                    begin
                        state_ns = S_IDLE;
                    end
            endcase
        end
    
    

    
    assign snes_clk = snes_clk_ff;
    assign buttons = ~buttons_ff;
    
endmodule