I'm learning FPGA development, and this is my first Verilog module - a button bouncer using a state machine. The code works as I expected it to be, but I would like some feedback on the code itself such as: state machine design, naming and code style, and any potential edge case that I did not think of.
Here is my current implementation:
module button_debounce (
// Inputs
input [1:0] pmod,
input clk,
// Outputs
output reg [3:0] led
);
wire rst;
wire go;
// Inverse signal
assign rst = ~pmod[0];
assign go = ~pmod[1];
// States
localparam STATE_IDLE = 2'd0;
localparam STATE_PUSH = 2'd1;
localparam STATE_DONE = 2'd2;
reg [1:0] state;
reg [19:0] clk_count;
// State transition
always @ (posedge clk or posedge rst) begin
if (rst == 1) begin
state <= STATE_IDLE;
led <= 4'd0;
end else begin
case (state)
STATE_IDLE: begin
if (go == 1'b1) begin
state <= STATE_PUSH;
clk_count <= 0;
end
end
STATE_PUSH: begin
if (go == 1'b0) begin
state <= STATE_DONE;
led <= led + 1;
end
end
STATE_DONE: begin
if (clk_count != 20'd600000) begin
clk_count <= clk_count + 1;
end else begin
clk_count <= 20'b0;
state <= STATE_IDLE;
end
end
default: state <= STATE_IDLE;
endcase
end
end
endmodule
Any feedback on improving this code would be greatly appreciated.