FPGA 学习小组
直播中

alexdos

6年用户 804经验值
擅长:可编程逻辑 电源/新能源 嵌入式技术 模拟技术
私信 关注

以太网控制器程序的仿真与测试顶层程序代码示范

顶层程序负责连接仿真程序的各个部分:模拟 PHY 程序、模拟主机程序和以太网控制程序。同时顶层程序需要控制仿真的进行,主要代码如下:

  1. `include "eth_phy_defines.v"
  2. `include "wb_model_defines.v"
  3. `include "tb_eth_defines.v"
  4. `include "eth_defines.v"
  5. `include "timescale.v"
  6. module tb_ethernet();
  7. //寄存器与连线
  8. reg wb_clk;
  9. ……
  10. //连接以太网控制器
  11. eth_top eth_top
  12. (
  13. .wb_clk_i(wb_clk), .wb_rst_i(wb_rst),
  14. .wb_adr_i(eth_sl_wb_adr_i[11:2]), .wb_sel_i(eth_sl_wb_sel_i), .wb_we_i(eth_sl_wb_we_i
  15. ),
  16. .wb_cyc_i(eth_sl_wb_cyc_i), .wb_stb_i(eth_sl_wb_stb_i), .wb_ack_o(eth_sl_wb_ack
  17. _o),
  18. .wb_err_o(eth_sl_wb_err_o), .wb_dat_i(eth_sl_wb_dat_i), .wb_dat_o(eth_sl_wb_dat
  19. _o),
  20. .m_wb_adr_o(eth_ma_wb_adr_o), .m_wb_sel_o(eth_ma_wb_sel_o), .m_wb_we_o(eth_ma_wb_we
  21. _o), .m_wb_dat_i(eth_ma_wb_dat_i), .m_wb_dat_o(eth_ma_wb_dat_o), .m_wb_cyc_o(eth_ma_wb_cyc
  22. _o),
  23. .m_wb_stb_o(eth_ma_wb_stb_o), .m_wb_ack_i(eth_ma_wb_ack_i), .m_wb_err_i(eth_ma_wb_e
  24. rr_i),
  25. //发送数据
  26. .mtx_clk_pad_i(mtx_clk), .mtxd_pad_o(MTxD), .mtxen_pad_o(MTxEn), .mtxerr_pad_o(MTxErr),
  27. //接收数据部分
  28. .mrx_clk_pad_i(mrx_clk), .mrxd_pad_i(MRxD), .mrxdv_pad_i(MRxDV), .mrxerr_pad_i(MRxErr),
  29. .mcoll_pad_i(MColl), .mcrs_pad_i(MCrs),
  30. //媒体无关接口模块
  31. .mdc_pad_o(Mdc_O), .md_pad_i(Mdi_I), .md_pad_o(Mdo_O), .md_padoe_o(Mdo_OE),
  32. .int_o(wb_int)
  33. );
  34. //连接模拟 PHY 部分
  35. assign Mdio_IO = Mdo_OE ? Mdo_O : 1'bz ;
  36. assign Mdi_I = Mdio_IO;
  37. integer phy_log_file_desc;
  38. eth_phy eth_phy
  39. (
  40. .m_rst_n_i(!wb_rst),
  41. // MAC 发送数据
  42. .mtx_clk_o(mtx_clk), .mtxd_i(MTxD), .mtxen_i(MTxEn), .mtxerr_i(MTxErr),
  43. // MAC 接收数据
  44. .mrx_clk_o(mrx_clk), .mrxd_o(MRxD), .mrxdv_o(MRxDV), .mrxerr_o(MRxErr),
  45. .mcoll_o(MColl), .mcrs_o(MCrs),
  46. //媒体无关接口模块
  47. .mdc_i(Mdc_O), .md_io(Mdio_IO),
  48. .phy_log(phy_log_file_desc)
  49. );
  50. // 连接主机模块
  51. integer host_log_file_desc;
  52. WB_MASTER_BEHAVIORAL wb_master
  53. (
  54. .CLK_I(wb_clk),
  55. .RST_I(wb_rst),
  56. .TAG_I({`WB_TAG_WIDTH{1'b0}}),
  57. .TAG_O(),
  58. .ACK_I(eth_sl_wb_ack_o),
  59. .ADR_O(eth_sl_wb_adr), // only eth_sl_wb_adr_i[11:2] used
  60. .CYC_O(eth_sl_wb_cyc_i),
  61. .DAT_I(eth_sl_wb_dat_o),
  62. .DAT_O(eth_sl_wb_dat_i),
  63. .ERR_I(eth_sl_wb_err_o),
  64. .RTY_I(1'b0), // inactive (1'b0)
  65. .SEL_O(eth_sl_wb_sel_i),
  66. .STB_O(eth_sl_wb_stb_i),
  67. .WE_O (eth_sl_wb_we_i),
  68. .CAB_O() // NOT USED for now!
  69. );
  70. assign eth_sl_wb_adr_i = {20'h0, eth_sl_wb_adr[11:2], 2'h0};
  71. ……
  72. //初始化
  73. initial
  74. begin
  75. //复位信号
  76. wb_rst = 1'b1;
  77. #423 wb_rst = 1'b0;
  78. //清除存储器内容
  79. clear_memories;
  80. clear_buffer_descriptors;
  81. #423 StartTB = 1'b1;
  82. end
  83. //产生时钟信号
  84. initial
  85. begin
  86. wb_clk=0;
  87. forever #15 wb_clk = ~wb_clk; // 2*10 ns -> 33.3 MHz
  88. end
  89. integer tests_successfull;
  90. integer tests_failed;
  91. reg [799:0] test_name; // used for tb_log_file
  92. reg [3:0] wbm_init_waits; // initial wait cycles between CYC_O and STB_O of WB Master
  93. reg [3:0] wbm_subseq_waits; // subsequent wait cycles between STB_Os of WB Master
  94. reg [2:0] wbs_waits; // wait cycles befor WB Slave responds
  95. reg [7:0] wbs_retries; // if RTY response, then this is the number of retries before ACK
  96. reg wbm_working; // tasks wbm_write and wbm_read set signal when working and reset
  97. it when stop working
  98. //开始测试内容
  99. initial
  100. begin
  101. wait(StartTB); // 开始测试
  102. //初始化全局变量
  103. tests_successfull = 0;
  104. tests_failed = 0;
  105. wbm_working = 0;
  106. wbm_init_waits = 4'h1;
  107. wbm_subseq_waits = 4'h3;
  108. wbs_waits = 4'h1;
  109. wbs_retries = 8'h2;
  110. wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
  111. //测试的各个任务
  112. test_note("PHY generates ideal Carrier sense and Collision signals for following tests");
  113. eth_phy.carrier_sense_real_delay(0);
  114. test_mac_full_duplex_transmit(0, 21); //测试全双工方式下传输数据
  115. test_mac_full_duplex_receive(0, 13); //测试全双工方式下接收数据
  116. test_mac_full_duplex_flow_control(0, 4); // 测试整个数据流程
  117. test_note("PHY generates 'real delayed' Carrier sense and Collision signals for following
  118. tests");
  119. eth_phy.carrier_sense_real_delay(1);
  120. // 结束测试
  121. test_summary;
  122. $stop;
  123. end


测试内容通过多个测试任务来执行。

更多回帖

发帖
×
20
完善资料,
赚取积分