synchro.sv 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // synchro.v
  3. //
  4. // Asynchronous input synchronizer
  5. //
  6. //
  7. // These attributes tell the compiler and fitter respectively
  8. // to treat these registers as low-level constructs and turn
  9. // them into a synchronizer chain. No inferring RAMs or anything like that,
  10. // and pack them close together.
  11. //
  12. module synchronizer #(parameter width = 1, parameter stages = 2)
  13. (
  14. input rst_n,
  15. input clk,
  16. input [width-1:0] d,
  17. output [width-1:0] q
  18. );
  19. // SYNCHRONIZER_IDENTIFICATION FORCED identifies the *beginning* of
  20. // the synchro; it needs to be used with AUTO for the other stages or
  21. // the chains will be broken up for each stage.
  22. //
  23. // Because of different attributes, this is not simply qreg[0].
  24. `ifdef ALTERA_RESERVED_QIS
  25. (*
  26. syn_preserve = 1,
  27. altera_attribute =
  28. {"-name SYNCHRONIZER_IDENTIFICATION FORCED ; ",
  29. "-name SYNCHRONIZATION_REGISTER_CHAIN_LENGTH ", tostr(stages-1)}
  30. *)
  31. `endif
  32. reg [width-1:0] qreg0;
  33. `ifdef ALTERA_RESERVED_QIS
  34. (*
  35. syn_preserve = 1,
  36. altera_attribute =
  37. {"-name SYNCHRONIZER_IDENTIFICATION AUTO ; ",
  38. "-name SYNCHRONIZATION_REGISTER_CHAIN_LENGTH ", tostr(stages-1)}
  39. *)
  40. `endif
  41. reg [width-1:0] qreg[stages-1:1];
  42. always @(posedge clk or negedge rst_n)
  43. if (~rst_n)
  44. qreg0 <= {width{1'b0}};
  45. else
  46. qreg0 <= d;
  47. always @(posedge clk or negedge rst_n)
  48. if (~rst_n)
  49. qreg[1] <= {width{1'b0}};
  50. else
  51. qreg[1] <= qreg0;
  52. generate
  53. genvar i;
  54. for (i = 2; i < stages; i = i + 1)
  55. begin : stage
  56. always @(posedge clk or negedge rst_n)
  57. if (~rst_n)
  58. qreg[i] <= {width{1'b0}};
  59. else
  60. qreg[i] <= qreg[i-1];
  61. end
  62. endgenerate
  63. assign q = qreg[stages-1];
  64. endmodule // synchronizer