当前位置:   article > 正文

「Verilog学习笔记」使用握手信号实现跨时钟域数据传输

「Verilog学习笔记」使用握手信号实现跨时钟域数据传输
专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

  1. `timescale 1ns/1ns
  2. module data_driver(
  3. input clk_a,
  4. input rst_n,
  5. input data_ack,
  6. output reg [3:0]data,
  7. output reg data_req
  8. );
  9. reg data_ack_reg_1, data_ack_reg_2 ;
  10. reg [9:0] cnt ;
  11. always @ (posedge clk_a or negedge rst_n) begin
  12. if (~rst_n) begin
  13. data_ack_reg_1 <= 0 ;
  14. data_ack_reg_2 <= 0 ;
  15. end
  16. else begin
  17. data_ack_reg_1 <= data_ack ;
  18. data_ack_reg_2 <= data_ack_reg_1 ;
  19. end
  20. end
  21. always @ (posedge clk_a or negedge rst_n) begin
  22. if (~rst_n) data <= 0 ;
  23. else if (data_ack_reg_1 && !data_ack_reg_2) data <= data + 1 ;
  24. else data <= data ;
  25. end
  26. always @ (posedge clk_a or negedge rst_n) begin
  27. if (~rst_n) cnt <= 0 ;
  28. else if (data_ack_reg_1 && !data_ack_reg_2) cnt <= 0 ;
  29. else if (data_req) cnt <= cnt ;
  30. else cnt <= cnt + 1 ;
  31. end
  32. always @ (posedge clk_a or negedge rst_n) begin
  33. if (~rst_n) data_req <= 0 ;
  34. else if (cnt == 4) data_req <= 1 ;
  35. else if (data_ack_reg_1 && !data_ack_reg_2) data_req <= 0 ;
  36. else data_req <= data_req ;
  37. end
  38. endmodule
  39. module data_receiver (
  40. input clk_b,
  41. input rst_n,
  42. input data_req,
  43. input [3:0] data,
  44. output reg data_ack
  45. );
  46. reg [3:0] data_in_reg ;
  47. reg data_req_reg_1, data_req_reg_2 ;
  48. always @ (posedge clk_b or negedge rst_n) begin
  49. if (~rst_n) begin
  50. data_req_reg_1 <= 0 ;
  51. data_req_reg_2 <= 0 ;
  52. end
  53. else begin
  54. data_req_reg_1 <= data_req ;
  55. data_req_reg_2 <= data_req_reg_1 ;
  56. end
  57. end
  58. always @ (posedge clk_b or negedge rst_n) begin
  59. if (~rst_n) data_ack <= 0 ;
  60. else if (data_req_reg_1) data_ack <= 1 ;
  61. else data_ack <= 0 ;
  62. end
  63. always @ (posedge clk_b or negedge rst_n) begin
  64. if (~rst_n) data_in_reg <= 0 ;
  65. else if (data_req_reg_1 && !data_req_reg_2) data_in_reg <= data ;
  66. else data_in_reg <= data_in_reg ;
  67. end
  68. endmodule
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/180893
推荐阅读
相关标签
  

闽ICP备14008679号