]> Joshua Wise's Git repositories - fpgaboy.git/blobdiff - PS2Button.v
IP
[fpgaboy.git] / PS2Button.v
index 169346c73335883fede324d1afb2bd4404833e84..81a0e89b7fa3135ea4fcbc72a39a19e50dccc95e 100644 (file)
@@ -1,4 +1,5 @@
 module PS2Button(
 module PS2Button(
+       input clk,
        input inclk,
        input indata,
        output wire [7:0] buttons
        input inclk,
        input indata,
        output wire [7:0] buttons
@@ -10,9 +11,29 @@ module PS2Button(
        reg key_a = 0,key_b = 0,key_st = 0,key_sel = 0,key_up = 0,key_dn = 0,key_l = 0,key_r = 0;
 
        assign buttons = {key_st,key_sel,key_b,key_a,key_dn,key_up,key_l,key_r};
        reg key_a = 0,key_b = 0,key_st = 0,key_sel = 0,key_up = 0,key_dn = 0,key_l = 0,key_r = 0;
 
        assign buttons = {key_st,key_sel,key_b,key_a,key_dn,key_up,key_l,key_r};
+       
+       /* Clock debouncing */
+       reg lastinclk = 0;
+       reg [5:0] debounce = 0;
+       reg fixedclk = 0;
+       reg [9:0] resetcountdown = 0;
+       
+       always @(posedge clk) begin
+               if (inclk != lastinclk) begin
+                       lastinclk <= inclk;
+                       debounce <= 1;
+                       resetcountdown <= 10'b1111111111;
+               end else if (debounce == 0) begin
+                       fixedclk <= inclk;
+                       resetcountdown <= resetcountdown - 1;
+               end else
+                       debounce <= debounce + 1;
+       end
 
 
-       always @ (negedge inclk) begin
-               if(bitcount == 10) begin
+       always @(negedge fixedclk) begin
+               if (resetcountdown == 0)
+                       bitcount <= 0;
+               else if (bitcount == 10) begin
                        bitcount <= 0;
                        if(parity != (^ key)) begin
                                if(keyarrow) begin
                        bitcount <= 0;
                        if(parity != (^ key)) begin
                                if(keyarrow) begin
This page took 0.026468 seconds and 4 git commands to generate.