2
0

synchro.sv 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  13. #(parameter width = 1,
  14. parameter stages = 2,
  15. parameter noioregs = 0)
  16. (
  17. input rst_n,
  18. input clk,
  19. input [width-1:0] d,
  20. output [width-1:0] q
  21. );
  22. // SYNCHRONIZER_IDENTIFICATION FORCED identifies the *beginning* of
  23. // the synchro; it needs to be used with AUTO for the other stages or
  24. // the chains will be broken up for each stage.
  25. //
  26. // Because of different attributes, this is not simply qreg[0].
  27. `ifdef ALTERA_RESERVED_QIS
  28. (*
  29. syn_preserve = 1,
  30. dont_replicate = 1,
  31. useioff = noioregs,
  32. altera_attribute =
  33. {"-name SYNCHRONIZER_IDENTIFICATION FORCED ; ",
  34. "-name SYNCHRONIZATION_REGISTER_CHAIN_LENGTH ", tostr(stages-1)}
  35. *)
  36. `endif
  37. reg [width-1:0] qreg0;
  38. `ifdef ALTERA_RESERVED_QIS
  39. (*
  40. syn_preserve = 1,
  41. dont_replicate = 1,
  42. useioff = 1,
  43. altera_attribute =
  44. {"-name SYNCHRONIZER_IDENTIFICATION AUTO ; ",
  45. "-name SYNCHRONIZATION_REGISTER_CHAIN_LENGTH ", tostr(stages-1)}
  46. *)
  47. `endif
  48. reg [width-1:0] qreg[stages-1:1];
  49. always @(posedge clk or negedge rst_n)
  50. if (~rst_n)
  51. qreg0 <= {width{1'b0}};
  52. else
  53. qreg0 <= d;
  54. always @(posedge clk or negedge rst_n)
  55. if (~rst_n)
  56. qreg[1] <= {width{1'b0}};
  57. else
  58. qreg[1] <= qreg0;
  59. generate
  60. genvar i;
  61. for (i = 2; i < stages; i = i + 1)
  62. begin : stage
  63. always @(posedge clk or negedge rst_n)
  64. if (~rst_n)
  65. qreg[i] <= {width{1'b0}};
  66. else
  67. qreg[i] <= qreg[i-1];
  68. end
  69. endgenerate
  70. assign q = qreg[stages-1];
  71. endmodule // synchronizer