QuartusII Modelsim使用教程

我的博客:QuartusII Modelsim使用教程 | Infinite journey (infinite-zh.com)

QuartusII中Modelsim是一个很好的仿真软件,相较于VWF,它的仿真时间更长、显示更具体、界面更友好,另外Modelsim还可以结合TestBench来进行仿真,省去了自己设置信号的过程。本文将从以下几个方面来介绍Modelsim的使用:

  • 1、Modelsim联合TestBench进行仿真;
  • 2、Modelsim仿真的几个小窍门;
  • 3、Modelsim软件的bug

1、Modelsim联合TestBench进行仿真

我一开始的时候对TestBench很不习惯,因为一搜TestBench就有各样的语法,让人头大,但后来在我写了一两个之后,我就不禁发出感叹“我是**”,TestBench免去了我打开modelsim之后,手动配置输入信号、时钟等,大大提高了我的仿真效率。

在Verilog代码写好编译好之后,可以通过Processing->Start->Start Test Bench Template Writer来自动生成一个TestBench模板。我们可以看到他是长这个样子的

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

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

`timescale 1 ps/ 1 ps
module AES_encryp_vlg_tst();
// constants                                           
// general purpose registers
reg eachvec;
// test vector input registers
reg clk;
reg [127:0] iKey;
reg [127:0] iPlaintext;
reg rst_n;
// wires                                               
wire [127:0]  oCiphertext;

// assign statements (if any)                          
AES_encryp i1 (
// port map - connection between master ports and signals/registers   
	.clk(clk),
	.iKey(iKey),
	.iPlaintext(iPlaintext),
	.oCiphertext(oCiphertext),
	.rst_n(rst_n)
);
initial                                                
begin                                                  
// code that executes only once                        
// insert code here --> begin                          
                                                       
// --> end                                             
$display("Running testbench");                       
end                                                    
always                                                 
// optional sensitivity list                           
// @(event1 or event2 or .... eventn)                  
begin                                                  
// code executes for every event on sensitivity list   
// insert code here --> begin                          
                                                       
@eachvec;                                              
// --> end                                             
end                                                    
endmodule

在这里特别提一下最后一个@eachvec 这一串代码,虽然具体不知道这段代码的作用,但如果加上这段代码,那么在进行Modelsim仿真的时候,仿真时间会特别短,clk也无法振动起来,因此再写需要clk的TestBench时建议将@eachvec这一段去了。

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

initial                                                
begin                                                  
// code that executes only once                        
// insert code here --> begin                          
begin                                                  
	#0 	clk = 0;
		rst_n = 0;
	#5	rst_n = 1;
		iKey = 128'h31_32_33_34_35_36_37_38_39_30_31_32_33_34_35_36;
		iPlaintext = 128'h30_39_38_37_36_35_34_33_32_31_36_35_34_33_32_31;
	#1000	rst_n = 0;
        #5      rst_n = 1;
		iKey = 128'h30_39_38_37_36_35_34_33_32_31_36_35_34_33_32_31;
		iPlaintext = 128'h31_32_33_34_35_36_37_38_39_30_31_32_33_34_35_36;
// --> end                                             
$display("Running testbench");                       
end                                                    
always                                                 
// optional sensitivity list                           
// @(event1 or event2 or .... eventn)                  
begin                                                  
// code executes for every event on sensitivity list   
// insert code here --> begin                          
    #10 clk = ~clk;                                                                                              
// --> end                                             
end                                                    
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,也可以看到你加入变量之后的时间段的结果。

3、Modelsim的软件Bug

Modelsim因为版本问题,可能会有一些的坑,我这里记录一个我最近遇到的坑,之后又新的坑会再继续更新。

1、关于Modelsim无法找到mif文件的问题

在我使用的Quartus17.1的自带的Modelsim中,当我使用ROM IP核的时候,我通过Mif文件写入,但通过modelsim仿真却发现,mif文件并没有能够被modelsim读出来,我也搜索了许多的解决方案,例如,将mif文件放在simulation文件夹下,或者说将mif文件放在工程文件下,但最后都不好使。最后我找到了解决方案:将mif文件的路径改成绝对路径,如下图所示。(PS:这个可能看脸,我同学的同样版本的quartus和modelsim他就可以读出来......)

具体就先更新这么多,日后在实践中又遇到新的问题再回来更新哈。

  • 11
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值