edge_detect_mod.v 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // vim: ts=4 sw=4 noexpandtab
  2. /*
  3. * Edge detection
  4. *
  5. * Copyright (c) 2019 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. `ifndef EDGE_DETECT_MOD_V_
  22. `define EDGE_DETECT_MOD_V_
  23. module edge_detect #(
  24. parameter NR_BITS = 1, /* Number of bits */
  25. ) (
  26. input wire clk, /* Clock */
  27. input wire [NR_BITS - 1 : 0] signal, /* Input signal */
  28. output wire [NR_BITS - 1 : 0] rising, /* Rising edge detected on input signal */
  29. output wire [NR_BITS - 1 : 0] falling, /* Falling edge detected on input signal */
  30. );
  31. reg [NR_BITS - 1 : 0] prev_signal;
  32. always @(posedge clk) begin
  33. prev_signal <= signal;
  34. end
  35. assign rising = ~prev_signal & signal;
  36. assign falling = prev_signal & ~signal;
  37. endmodule
  38. `endif /* EDGE_DETECT_MOD_V_ */