[Quartus] QuartusII Modelsim使用教程 转

[复制链接]
 楼主| gaochy1126 发表于 2021-3-23 12:40 | 显示全部楼层 |阅读模式
QuartusII中Modelsim是一个很好的仿真软件,相较于VWF,它的仿真时间更长、显示更具体、界面更友好,另外Modelsim还可以结合TestBench来进行仿真,省去了自己设置信号的过程。本文将从以下几个方面来介绍Modelsim的使用:

1、Modelsim联合TestBench进行仿真;
2、Modelsim仿真的几个小窍门;
3、Modelsim软件的bug
在Verilog代码写好编译好之后,可以通过Processing->Start->Start Test Bench Template Writer来自动生成一个TestBench模板。


  1. // Copyright (C) 2017  Intel Corporation. All rights reserved.
  2. // Your use of Intel Corporation's design tools, logic functions
  3. // and other software and tools, and its AMPP partner logic
  4. // functions, and any output files from any of the foregoing
  5. // (including device programming or simulation files), and any
  6. // associated documentation or information are expressly subject
  7. // to the terms and conditions of the Intel Program License
  8. // Subscription Agreement, the Intel Quartus Prime License Agreement,
  9. // the Intel FPGA IP License Agreement, or other applicable license
  10. // agreement, including, without limitation, that your use is for
  11. // the sole purpose of programming logic devices manufactured by
  12. // Intel and sold by Intel or its authorized distributors.  Please
  13. // refer to the applicable agreement for further details.

  14. // *****************************************************************************
  15. // This file contains a Verilog test bench template that is freely editable to  
  16. // suit user's needs .Comments are provided in each section to help the user   
  17. // fill out necessary details.                                                  
  18. // *****************************************************************************
  19. // Generated on "05/17/2020 21:30:07"
  20.                                                                                 
  21. // Verilog Test Bench template for design : AES_encryp
  22. //
  23. // Simulation tool : ModelSim-Altera (Verilog)
  24. //

  25. `timescale 1 ps/ 1 ps
  26. module AES_encryp_vlg_tst();
  27. // constants                                          
  28. // general purpose registers
  29. reg eachvec;
  30. // test vector input registers
  31. reg clk;
  32. reg [127:0] iKey;
  33. reg [127:0] iPlaintext;
  34. reg rst_n;
  35. // wires                                               
  36. wire [127:0]  oCiphertext;

  37. // assign statements (if any)                          
  38. AES_encryp i1 (
  39. // port map - connection between master ports and signals/registers   
  40.         .clk(clk),
  41.         .iKey(iKey),
  42.         .iPlaintext(iPlaintext),
  43.         .oCiphertext(oCiphertext),
  44.         .rst_n(rst_n)
  45. );
  46. initial                                                
  47. begin                                                  
  48. // code that executes only once                        
  49. // insert code here --> begin                          
  50.                                                       
  51. // --> end                                             
  52. $display("Running testbench");                       
  53. end                                                   
  54. always                                                
  55. // optional sensitivity list                           
  56. // @(event1 or event2 or .... eventn)                  
  57. begin                                                  
  58. // code executes for every event on sensitivity list   
  59. // insert code here --> begin                          
  60.                                                       
  61. @eachvec;                                             
  62. // --> end                                             
  63. end                                                   
  64. endmodule
在这里特别提一下最后一个@eachvec 这一串代码,虽然具体不知道这段代码的作用,但如果加上这段代码,那么在进行Modelsim仿真的时候,仿真时间会特别短,clk也无法振动起来,因此再写需要clk的TestBench时建议将@eachvec这一段去了。

那么接下来编写TestBench就变得十分容易,你可以在initial中加入你要初始化的变量,例如rst_n,初始输入的数据等等,除此之外还可以通过#延时,来增加新的输入。然后在always中进行clk的实现,下面是加入初始化变量和时钟的部分TestBench代码。

  1. initial                                                
  2. begin                                                  
  3. // code that executes only once                        
  4. // insert code here --> begin                          
  5. begin                                                  
  6.         #0         clk = 0;
  7.                 rst_n = 0;
  8.         #5        rst_n = 1;
  9.                 iKey = 128'h31_32_33_34_35_36_37_38_39_30_31_32_33_34_35_36;
  10.                 iPlaintext = 128'h30_39_38_37_36_35_34_33_32_31_36_35_34_33_32_31;
  11.         #1000        rst_n = 0;
  12.         #5      rst_n = 1;
  13.                 iKey = 128'h30_39_38_37_36_35_34_33_32_31_36_35_34_33_32_31;
  14.                 iPlaintext = 128'h31_32_33_34_35_36_37_38_39_30_31_32_33_34_35_36;
  15. // --> end                                             
  16. $display("Running testbench");                       
  17. end                                                   
  18. always                                                
  19. // optional sensitivity list                           
  20. // @(event1 or event2 or .... eventn)                  
  21. begin                                                  
  22. // code executes for every event on sensitivity list   
  23. // insert code here --> begin                          
  24.     #10 clk = ~clk;                                                                                             
  25. // --> end                                             
  26. end                                                   
  27. endmodule

在写了一两个TestBench之后,也可以去了解一下repeat等的函数,来进一步提高编写TestBench的能力。

以下是配置Modelsim启用TestBench。

点击菜单栏的Assignments->setting->EDA Tool Settings->Simulation如下图所示









之后就一直Ok,然后就可以打开Modelsim进行仿真了。

2、Modelsim仿真的几个小窍门
1、在使用Modelsim时,我们常常会发现代码的问题然后去修改,那么修改了代码之后如何重新Modelsim仿真呢



如上图所示,只要在Modelsim中的Library->work->修改代码的文件->Recompile即可重新编译,不需要再每次重启Modelsim。

在之后如何重新运行Modelsim



如图所示,Simulate->Restart 选择OK即可,在之后run即可

值得一提的是,run不仅可以通过Simulate->run 还可以在如下图中直接输入run 100来运行仿真



2、如何在Modelsim中加入别的变量?



如上图所示,我们可以在sim中选择相应的模块,其中的assign是你在头文件中定义的变量。

另外再加入变量后,需要Restart Modelsim仿真,这样才可以看到数据一开始的变化过程,当然如果不进行Restart,也可以看到你加入变量之后的时间段的结果。
————————————————
原文链接:https://blog.csdn.net/qq_41783470/article/details/106630672

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:这个社会混好的两种人:一是有权有势,二是没脸没皮的。

1148

主题

11651

帖子

26

粉丝
快速回复 在线客服 返回列表 返回顶部