sync_signal_mod.v 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // vim: ts=4 sw=4 noexpandtab
  2. /*
  3. * Synchronize a signal to a clock
  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 SYNC_SIGNAL_MOD_V_
  22. `define SYNC_SIGNAL_MOD_V_
  23. module sync_signal(
  24. input clk, /* clock */
  25. input in, /* input signal */
  26. output out, /* synchronized output signal */
  27. output falling, /* synchronized falling edge output */
  28. output rising, /* synchronized rising edge output */
  29. );
  30. reg [2:0] shiftreg;
  31. initial begin
  32. shiftreg <= 0;
  33. end
  34. always @(posedge clk) begin
  35. shiftreg[2:1] <= shiftreg[1:0];
  36. shiftreg[0] <= in;
  37. end
  38. assign out = shiftreg[1];
  39. assign falling = shiftreg[2] & ~shiftreg[1];
  40. assign rising = ~shiftreg[2] & shiftreg[1];
  41. endmodule
  42. `endif /* SYNC_SIGNAL_MOD_V_ */