🧪 Verilog Simulation Basics #
Simulation in Verilog is the process of executing your design over time to observe behavior and verify correctness.
💡 Key Concepts: #
- Simulation time progresses in discrete steps (e.g., #10).
- Testbenches apply inputs and observe outputs.
- No synthesis — purely virtual execution.
🧰 What is a Testbench? #
A testbench is a Verilog module that instantiates the DUT (Design Under Test), generates input stimuli, and optionally monitors outputs.
✅ Example: #
module tb;
reg clk, rst;
wire out;
my_design dut (.clk(clk), .rst(rst), .out(out));
initial begin
rst = 1; #5; rst = 0;
#100 $finish;
end
endmodule
Testbenches are not synthesizable and often use
$display
,$monitor
,$dumpvars
.
⏱️ timescale
Directive
#
The timescale
directive defines simulation time units and precision.
✅ Syntax: #
`timescale 1ns / 1ps
Value | Meaning |
---|---|
1ns |
1 simulation time unit = 1 nanosecond |
/1ps |
Time precision = 1 picosecond |
Always place
timescale
at the top of each .v file, especially testbenches.
🧭 Verilog Simulation Scheduling Regions #
Verilog simulation proceeds in 4-phase cycles every time step:
Region | Purpose | Example |
---|---|---|
Active | Evaluate RTL (always , assign ) |
RTL updates |
Inactive | Delayed triggers (e.g., #0 ) |
#0 a = 1; |
Non-blocking Update | Apply scheduled <= updates |
Registers update |
Monitor | $display , $monitor |
Observing changes |
This order ensures correct execution of
<=
,$display
, and signal transitions.
⏰ Clock Generation in Testbench #
A clock signal can be generated using initial
+ forever
or always
.
✅ Example 1: Using always
#
reg clk = 0;
always #5 clk = ~clk; // 10ns clock period
✅ Example 2: Using initial
#
initial begin
clk = 0;
forever #5 clk = ~clk;
end
Avoid using
forever
inside synthesizable code — this is testbench only.
🔄 Typical Simulation Flow #
initial
blocks start at time 0.assign
/always
logic responds to changes.- Time advances using
#
delays or event triggers. $monitor
,$dumpvars
record output.
🧪 Behavioral vs Gate-Level Simulation in Verilog #
🔹 Behavioral Simulation #
- Operates at the RTL (Register Transfer Level).
- Describes the intended functionality using
always
,assign
,initial
, etc. - Fast and ideal for early functional testing.
- Does not include actual gate-level structure or delays.
🔹 Gate-Level Simulation #
- Simulates the synthesized netlist — logic gates, flip-flops, muxes, etc.
- Reflects real hardware behavior, including timing delays and fan-out effects.
- Slower, but essential for final verification.
- Includes Standard Delay Format (SDF) files in ASIC flows.