2Write a test bench to test the decbouncer 3Simulate to conf
2.Write a test bench to test the decbouncer
3.Simulate to confirm the result.
State 0 Button -1 delays 250 000 State 1 k delay State 2 1 Button -1 State 3 Button delay 250 000 State 4 delay 0 State 5 Button -0 State 6 debounced 1 delay delay 1 NOTE: The debounced output is combinational, and should be \"1\' when state 6, else it should be zero delay delay 1Solution
module debouncing(
 input clock,
 input reset,
 input button,
 output reg out
 );
 
 localparam N = 19; //for a 10ms tick
 
 reg [N-1:0]count;
 wire tick;
 
 
 // the counter that will generate the tick.
 
 always @ (posedge clock or posedge reset)
 begin
 if(reset)
 count <= 0;
 else
 count <= count + 1;
 end
 
 assign tick = &count; //AND every bit of count with itself. Tick will only go high when all 19 bits of count are 1, i.e. after 10ms
 
 // now for the debouncing FSM
 
 localparam[2:0] //defining the various states to be used
 state0 = 3\'b000,
 state1 = 3\'b001,
 state2 = 3\'b010,
 state3 = 3\'b011,
 one = 3\'b100,
 state4 = 3\'b101,
 state5 = 3\'b110,
 state6 = 3\'b111;
 
 reg [2:0]state_reg;
 reg [2:0]state_next;
 
 always @ (posedge clock or posedge reset)
 begin
 if (reset)
 state_reg <= state0;
 else
 state_reg <= state_next;
 end
 
 
 always @ (*)
 begin
 state_next <= state_reg; // to make the current state the default state
 out <= 1\'b0; // default output low
 
 case(state_reg)
 state0:
 if (button) //if button is detected go to next state state1
 state_next <= state1;
 state1:
 if (~button) //while here if button goes back to state0 then input is not yet stable and go back to state state0
 state_next <= state0;
 else if (tick) //but if button remains high even after 10 ms, go to next state state2.
 state_next <= state2;
 state2:
 if (~button) //while here if button goes back to state0 then input is not yet stable and go back to state state0
 state_next <= state0;
 else if (tick) //else if after 20ms (10ms + 10ms) button is still high go to state3
 state_next <= state3;
 state3:
 if (~button) //while here if button goes back to state0 then input is not yet stable and go back to state state0
 state_next <= state0;
 else if (tick) //and finally even after 30 ms input stays high then it is stable enough to be considered a valid input, go to state one
 state_next <= one;
 
 one: //debouncing eliminated make output high, now here I\'ll check for bouncing when button is released
 begin
 out <= 1\'b1;
 if (~button) //if button appears to be released go to next state state4
 state_next <= state4;
 end
 state4:
 if (button) //while here if button goes back to high then input is not yet stable and go back to state one
 state_next <= one;
 else if (tick) //else if after 10ms it is still high go to next state state5
 state_next <= state5;
 state5:
 if (button) //while here if button goes back to high then input is not yet stable and go back to state one
 state_next <= one;
 else if (tick) //else if after 20ms it is still high go to next state state6
 state_next <= state6;
 state6:
 if (button) //while here if button goes back to high then input is not yet stable and go back to state one
 state_next <= one;
 else if (tick) //after 30 ms if button is low it has actually been released and bouncing eliminated, go back to state0 state to wait for next input.
 state_next <= state0;
 default state_next <= state0;
 
 endcase
 end
 
 endmodule


