CSEE4823_Lab2_verilog_and_modelsim
CSEE4823_Lab2_verilog_and_modelsim
ModelSim
Structural Verilog post-synthesis
*.nl.v dynamic timing verification
Timing info.
*.sdf Lab#4
PrimeTime
post-synthesis
static timing verification
Encounter post-synthesis
Automatic Placement and power analysis
Routing (APR)
Things to Know in Verilog (Design)
• Necessary:
▪ Abstraction level (behavioral, gate-level)
▪ Module declaration and instantiation
▪ Reg, wire usage
▪ If else, case statement, looping statement
▪ Signed vs unsigned arithmetic, bit and logical operators
▪ Procedural & continuous assignment
▪ RTL style coding (look at the document uploaded)
• Helpful
▪ Generate blocks
▪ Iterating over 2D arrays
▪ Part select
▪ Parameters, localparam
▪ Task 5
Things to Know in Verilog (Testbench)
• Many times the design is correct but the test setup
is wrong
• File I/O for giving inputs and checking the results
• Initial block
• Task (makes the code readable and modular)
• Back annotation (Will see later)
• Dumping vcd for power analysis (Will see later)
• Tracing the hierarchy
▪ For vcd
▪ Changing the waveformat.do
6
Verilog HDL Simulator: QuestaSim
• Verilog simulator: QuestaSim by Mentor-Graphics
• Simulator I/O files testbench.v output:
ModelSim
DUT1. or
results
1. Testbench file (testbench.v) v QuestaSim or
▪ defines input waveform DUT2. waveform
v
▪ defines output detectionion
ntiat
Insta
2. Source files
▪ RTL-level Verilog file (*.v)
▪ gate-level netlist (output of synthesis or APR)
3. How to define timing constraints?
▪ RTL-level: use “#xx” expression (not affect actual circuit)
▪ gate-level: include *.sdf file (output of synthesis or APR)
Testbench
• Instantiate DUT
• Generate clk Input
Stimuli
DUT Checker
Design
correct?
• In the initial block
▪ Initialize all the inputs
▪ Delay-control: Give inputs for testing, use small delays to
reflect real inputs
▪ Event-control: Use @(posedge clk) to time when to give inputs
and check outputs
▪ For loops to run through several clock cycles
▪ Finally say $finish
• The testbench starts processing from the initial block
• Simulation ends at $finish or you can time the
simulation in runsim.do
8
Debug tips
• ‘x’ means undefined
▪ See if you gave reset correctly
▪ Check if inputs have been initialized correctly
• ‘z’ means high impedance
▪ No logic driving the wire
▪ There maybe bit width mismatch in connections
• After functional verification synthesize individual
blocks and verify them (Will see later)
9
To start with…
• Folders and files
▪ Create your local folders
[your_path]/4823/rtl/lfsr1/ for source Verilog
[your_path]/4823/qsim_rtl/lfsr1/ for testbench
Verilog/simulation scripts/results
▪ Copy source codes from
/courses/ee6321/share/4823-Fall2024/rtl/lfsr1/lfsr1.v
/courses/ee6321/share/4823-Fall2024/qsim_rtl/lfsr1/*.*
to your local folder
10
Hardware Description Language (HDL)
• An efficient way to design, verify, and test circuits
▪ Custom design → Fine-grained but with low efficiency
▪ Semi-custom design→ HDL → Higher development efficiency
• Popular HDLs: Verilog/SystemVerilog, VHDL
• If you are not familiar with Verilog HDL, you can use the
following tutorials:
▪ http://www.asic-world.com/verilog/veritut.html
▪ IEEE Verilog Standard
▪ http://www.doulos.com/knowhow/verilog_designers_guide
▪ feel free to find one manual that looks good to you
Project Hierarchy
Project
run.sh README
runsim.do
waveformat.do
lfsr1.v
`timescale 1ns/1ps
module lfsr1 (clk, resetn, seed, lfsr_out);
input clk, resetn;
input [15:0] seed;
output [15:0] lfsr_out;
• `timescale specifies the time unit and precision for the modules
▪ Format: `timescale <time_unit>/<time_precision>
• input/output, wire/reg element declaration
• [Important]: Please check the difference between “reg” and
“wire” elements
13
lfsr1.v
always @(posedge clk) begin
if (~resetn) begin
lfsr_out <= #0.1 seed;
end
else begin
lfsr_out <= #0.1 lfsr_next;
end
end
endmodule
reg[12]
reg[14] reg[13]
lsfr_next[12]
lsfr_next[5]
lsfr_next[15] lsfr_next[14] lsfr_next[13] lsfr_next[0]
Verilog Q D Q D Q D Q D Q D Q D
15
test_lfsr.v
`timescale 1ns/1ps
`define SD #0.010
`define HALF_CLOCK_PERIOD #0.90
`define QSIM_OUT_FN "./qsim.out"
`define MATLAB_OUT_FN "../../matlab/lfsr1/lfsr1.results"
module testbench();
reg clk;
reg resetn;
reg [15:0] seed;
integer lfsr_out_matlab;
integer lfsr_out_qsim;
integer i;
integer ret_write;
integer ret_read;
integer qsim_out_file;
integer matlab_out_file;
integer error_count = 0;
Device-Under-Test (DUT) instantiation
lfsr1 lfsr_0 ( .clk(clk), .resetn(resetn), .seed(seed), .lfsr_out(lfsr_out) );
● File I/O
initial begin
// File IO
qsim_out_file = $fopen(`QSIM_OUT_FN,"w");
if (!qsim_out_file) begin
$display("Couldn't create the output file.");
$finish;
end
matlab_out_file = $fopen(`MATLAB_OUT_FN,"r");
if (!matlab_out_file) begin
$display("Couldn't open the Matlab file.");
$finish;
end
18
test_lfsr.v
for (i=0 ; i<256; i=i+1) begin
// compare w/ the results from Matlab sim
ret_read = $fscanf(matlab_out_file, "%d", lfsr_out_matlab);
lfsr_out_qsim = lfsr_out;
• From the second rising edge of clk, for loop statement starts
– You can read the file content by using $fscanf
– $fwrite writes the data on the file
– At the next clock’s rising edge, it starts new comparison
• In this testbench, 256 comparisons are repeated
19
test_lfsr.v
// Any mismatch b/w rtl and matlab sims?
if (error_count > 0) begin
$display("The results DO NOT match with those from Matlab :( ");
end
else begin
$display("The results DO match with those from Matlab :) ");
end
endmodule // testbench
20
Shell Script Files
• run.sh
vsim -do "runsim.do"
• nuke.sh
\rm -rf work modelsim.ini
\rm -rf *.log *.syn *.rpt *.mr *.nl.v *.sdf *.svf *.vcd transcript *.wlf
21
runsim.do
##################################################
# Modelsim do file to run simuilation
##################################################
vlib work
vmap work work
25