1.设置第三方EDA工具
在Tools -> Options中设置ModelSim的安装路径,注意要设置到win32文件夹(64位软件对应的就是win64)。
2.建立工程。
一直点next直到第四页:设置仿真工具为ModelSim。
或者在在Assignments -> Settings中设置仿真工具为ModelSim也可以。
3.编写.v文件
module add_mec(a_in,b_in,c_out,mclk,rst_n);
input mclk,rst_n;
input [7:0]a_in,b_in;
output [8:0]c_out;
reg[8:0]c_out;
always @(posedge mclk or negedge rst_n)
begin
if(!rst_n)
c_out<=1'b0;
else
c_out<=a_in+b_in;
end
endmodule
编译至无错
3.通过Quartus自动生成一个Testbench的模板,选择Processing -> Start -> Start Test Bench Template Writer,等待完成后打开刚才生成的Testbench,默认是保存在simulation\modelsim文件夹下的.vt格式文件。
修改.vt文件为
// Copyright (C) 1991-2013 Altera Corporation
// Your use of Altera 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 Altera Program License
// Subscription Agreement, Altera MegaCore Function License
// Agreement, or other applicable license agreement, including,
// without limitation, that your use is for the sole purpose of
// programming logic devices manufactured by Altera and sold by
// Altera 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 "07/25/2019 08:49:58"
// Verilog Test Bench template for design : add_mec
//
// Simulation tool : ModelSim (Verilog)
//
`timescale 1 ns/ 1 ps
module add_mec_vlg_tst();
// constants
// general purpose registers
reg eachvec;
// test vector input registers
reg [7:0] a_in;
reg [7:0] b_in;
reg mclk;
reg rst_n;
// wires
wire [8:0] c_out;
// assign statements (if any)
add_mec i1 (
// port map - connection between master ports and signals/registers
.a_in(a_in),
.b_in(b_in),
.c_out(c_out),
.mclk(mclk),
.rst_n(rst_n)
);
initial
begin
// code that executes only once
// insert code here --> begin
rst_n=1;
mclk=0;
a_in=1;
b_in=3;
#50 a_in=5;
b_in=10;
// --> 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 mclk=~mclk;
//@eachvec;
// --> end
end
endmodule
一些注意事项:
1)`timescale 1 ns/ 1 ps
仿真单位时间为1ns,精度为1ps.
2)initial中的内容只执行一次。
#20 a_in=1;
b_in=2;
其中在延时20个单位时间后a_in与b_in同时赋值
另外其中的延时是顺序进行的?(猜测)
3)always中的语句会循环执行
4) @eachvec;这句话一定要注释掉
5)Testbench本身可以看做一个模块或者设备(本例中的模块名为add_vlg_tst),和你自己编写的模块进行通信。通过Testbench模块向待测模块输出信号作为激励,同时接收从待测模块输出的信号来查看结果。因此,在待测模块中的reg型信号在Testbench中就变成了wire,待测模块中的wire型信号在Testbench中则对应为reg型。那么inout怎么办呢,inout型信号也要设成wire,同时要用一个reg型信号作为输出寄存器,同时设置一个三态门,由一个使能信号控制,如:assign inout_sig = out_en ? out_reg : 1’bz;
5.
在Simulation选项卡中配置仿真选项,可以配置仿真语言、仿真时间的格式以及输出目录。选中compile test bench,点击Test Benches打开Test Benches对话框。
点击New新建一个Test BenchSetting,填入Testbench模块的名称(这里是add_vlg_tst),酌情设置仿真运行的时间(这里设为800ns,只是进入ModelSim后仿真自动执行的时间,不设或随意设置也行),并将刚才编写的Testbench添加进来。
6.
打开modelsim(!!!!!注意此处与其它教程不同)
删除worlk的lib以及其物理内容
7.重新建立work,新建project
8.点击Add Existing File添加.v文件与.vt文件,并编译至无无错
9.随后在modelsim进行仿真即可,注意时间大于500ns好一些。
观察波形时缩小一下界面,不然看不到开始波形,会让你以为仿真错了。