deglitch.sv 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // deglitch.v
  3. //
  4. module deglitch_bit (
  5. input rst_n,
  6. input clk,
  7. input d,
  8. output reg q
  9. );
  10. parameter cbits = 2; // need 2^cbits same signals in a row
  11. reg d_reg;
  12. reg [cbits-1:0] ctr;
  13. always @(posedge clk or negedge rst_n)
  14. if (~rst_n)
  15. begin
  16. d_reg <= d;
  17. q <= d_reg;
  18. ctr <= {(cbits){1'b0}};
  19. end
  20. else
  21. begin
  22. d_reg <= d;
  23. if (d_reg ^ q)
  24. begin
  25. if (&ctr)
  26. q <= d_reg;
  27. ctr <= ctr + 1'b1;
  28. end
  29. else
  30. begin
  31. if (|ctr)
  32. ctr <= ctr - 1'b1;
  33. end // else: !if(d_reg ^ q)
  34. end // else: !if(~rst_n)
  35. endmodule // deglitch_one
  36. module deglitch #(parameter width = 1, parameter cbits = 2) (
  37. input rst_n,
  38. input clk,
  39. input [width-1:0] d,
  40. output [width-1:0] q
  41. );
  42. generate
  43. genvar i;
  44. for (i = 0; i < width; i = i+1)
  45. begin : genbit
  46. deglitch_bit #(.cbits(cbits)) dg
  47. (
  48. .rst_n ( rst_n ),
  49. .clk ( clk ),
  50. .d ( d[i] ),
  51. .q ( q[i] )
  52. );
  53. end
  54. endgenerate
  55. endmodule