In Verilog, the timescale
directive is used to define the time units and precision for the simulation. It is essential for determining how time is represented and measured in a Verilog design. The timescale affects how delays (like #
delays) are interpreted in the simulation.
Syntax:
`timescale <time_unit> / <time_precision>
<time_unit>
specifies the base time unit used for the simulation (e.g.,1ns
,1ps
,10ms
, etc.).<time_precision>
specifies the precision for fractional time values (e.g.,1ps
,100fs
, etc.).
Example:
`timescale 1ns / 1ps
This directive indicates that:
- The base time unit for simulation is 1 nanosecond (1ns).
- The precision of time values is 1 picosecond (1ps).
This means delays like #10
would be interpreted as 10ns, and any fractional time values in the simulation would be rounded to the nearest 1ps.
Common Usage:
- Time Unit (
<time_unit>
): The most commonly used time units are:s
(seconds)ms
(milliseconds)us
(microseconds)ns
(nanoseconds)ps
(picoseconds)fs
(femtoseconds)
- Time Precision (
<time_precision>
): The time precision typically is set to a value smaller than or equal to the time unit. Common values are1ns
,1ps
,100fs
, etc.
Example with Time Delays:
`timescale 1ns / 1ps
module delay_example;
initial begin
#5; // Delay of 5ns
#10.5; // Delay of 10.5ns (precision of 1ps)
#15.75; // Delay of 15.75ns
end
endmodule
Important Notes:
- Global Scope: The
timescale
directive is typically placed at the top of your Verilog file and applies to the entire module or file. - Accuracy: The precision allows for accurate representation of time, especially in high-speed designs.
- Legacy and Compatibility: The use of
timescale
in Verilog is necessary for simulation accuracy but does not affect synthesizable hardware or the synthesized RTL code. The synthesis tools will ignore it.
Example of Verilog File with Timescale:
`timescale 1ns / 1ps
module testbench;
reg clk;
reg rst;
initial begin
clk = 0;
rst = 1;
#5 rst = 0;
#10 clk = 1;
#15 clk = 0;
#20 $stop;
end
endmodule
In this example:
- The
timescale
directive tells the simulator that all delays are in nanoseconds, and the precision is set to picoseconds.