ADI Testbenches coding guidelines
1. Introduction
This document contains coding and documentation guidelines which must be followed by all HDL testbenches.
The coding rules are intended to be applied to testbenches written using System Verilog.
The HDL coding guidelines ADI HDL coding guidelines are also applicable here. In addition, there are a set of rules for System Verilog, listed below.
2. Coding style
A. Naming
A1
Class names should be composed in the following way:
<class_name>_vif - Virtual Interface
<class_name>_env - Environment
<class_name>_wd - Watchdog
<class_name>_api - API class for an IP
<class_name>_agent - Agent class for an VIP
<class_name>_driver - Driver class for an VIP
<class_name>_monitor - Monitor class for an VIP
<class_name>_sequencer - Sequencer class for an VIP
A2
File names should be composed in the following way:
<driver_name>_api - API
Makeinclude_<include_name> - Makefile includes
sp_include_<include_name> - system_project.tcl includes
adi_regmap_<ip_name>_pkg - used in registermap class definitions
<module_name>_pkg - generic file name
adi_<vip_name>_vip.sv - packaged VIP module
adi_<vip_name>_vip_top.v - packaged VIP top module
adi_<vip_name>_vip_pkg - VIP agent, driver, monitor and sequencer containing package
adi_<vip_name>_if - VIP interface
adi_<vip_name>_if_base_pkg - abstract interface class, to be used and implemented in the VIP interface
B. General
B1
Use safe-guarded thread construct when attempting to stop/kill a thread to prevent unknown behavior.
Incorrect:
fork
...
join_any
disable fork;
Correct:
fork begin
fork
...
join_any
disable fork;
end join
B2
Include files should be used in Makefile and system_project.tcl scripts for classes that have multiple dependencies. If a simulation dependency needs multiple other dependencies, then an include file should be created if it doesn’t already exist.
B3
Testbench must finish with all VIPs and threads stopped, testbench done message
must be printed and the finish() system function must be called afterwards.
B4
Forever statement must be used instead of while(1).
Incorrect:
while(1) begin
...
end
Correct:
forever begin
...
end
B5
Repeat statement must be used instead of for loops, where the same operation block is repeated with no changes.
Incorrect:
int i;
for (i=0; i<5; i++) begin
...
end
Correct:
repeat(5) begin
...
end
B6
Watchdogs must be used to ensure that the simulation doesn’t get stuck during a run, and all code must be covered by one or multiple watchdogs.
B7
Test programs must use localparams instead of parameter.
Incorrect:
parameter VAL = 3;
Correct:
localparam VAL = 3;
B8
Use the proper equation type for various comparisons
== for logical equality (1 and 0 comparison only)
=== for case equality (1, 0, x and z comparison)
B9
begin…``end`` blocks must be present for all conditionals and loops, even
if they contain a single statement.
Incorrect:
if (transfer_done)
counter++;
repeat(5)
send_data();
Correct:
if (transfer_done) begin
counter++;
end
repeat(5) begin
send_data();
end
B10
Definitions must be used only when the value is used in multiple files, or when
it is defined in the block design using adi_sim_add_define. In every other
case use localparam in the SystemVerilog file.
Incorrect:
// used only in this test program
`define PATTERN_LENGTH 16
Correct:
// used only in this test program
localparam PATTERN_LENGTH = 16;
// definition defined in the block design, see example in rule C2
offset = `RX_DMA_BA;
B11
Parameters, local parameters and defines must not store values of type
time. Store the numeric value and apply the time scale where it is used.
Incorrect:
localparam time TIMEOUT = 10us;
`define TIMEOUT 10us
Correct:
localparam TIMEOUT_US = 10;
#(TIMEOUT_US * 1us);
B12
Output variables of the test program must be initialized.
Incorrect:
program test_program(
input logic system_clk,
output logic test_done);
Correct:
program test_program(
input logic system_clk,
output logic test_done);
initial begin
test_done = 1'b0;
end
B13
force…``release`` must be used only in very rare occasions, when there is
no other way of controlling a signal.
C. Design Under Test
C1
DUT IPs with a parameterizable registermap must be initialized using the macro that is provided in the SystemVerilog header file next to the registermap.
C2
IP base addresses must be defined when the block design is created.
Case 1: AXI interface not yet connected.
set RX_DMA 0x7C420000
ad_cpu_interconnect $RX_DMA dut_rx_dma
adi_sim_add_define "RX_DMA_BA=[format "%d" ${RX_DMA}]"
Case 2: AXI interface already connected.
set RX_DMA 0x7C420000
set_property offset $RX_DMA [get_bd_addr_segs {mng_axi_vip/Master_AXI/SEG_data_dut_rx_dma}]
adi_sim_add_define "RX_DMA_BA=[format "%d" ${RX_DMA}]"
C3
Project level testbench’s FPGA part must be compatible with one of the designs of the HDL projects.
Set FPGA part number in system_project.tcl:
# VCU118 board example:
adi_sim_project_xilinx $project_name "xcvu9p-flga2104-2L-e"
# Xilinx 7-series FPGA example:
adi_sim_project_xilinx $project_name "xc7z007sclg400-1"
C4
All configuration parameters must be defined in the ad_project_params associative array.
global ad_project_params
set ad_project_params(DATA_WIDTH) 16
set rx_dma_cfg [list \
DMA_TYPE_SRC 1 \
DMA_TYPE_DEST 0 \
ID 0 \
AXI_SLICE_SRC 1 \
AXI_SLICE_DEST 1 \
SYNC_TRANSFER_START 0 \
DMA_LENGTH_WIDTH 24 \
DMA_2D_TRANSFER 0 \
MAX_BYTES_PER_BURST 4096 \
CYCLIC 0 \
DMA_DATA_WIDTH_SRC 32 \
DMA_DATA_WIDTH_DEST 32 \
]
set ad_project_params(rx_dma_cfg) $rx_dma_cfg
C5
In project level testbenches, the DUT block design should come from the HDL repository.
C6
If multiple test programs are created, the test program’s name should hint towards the use-case of that test program.
C7
If multiple configurations are created, the configuration file’s name should hint towards the use-case of that configuration file.
C8
system_tb.sv must not contain any combinatorial logic. Its only purpose is
to connect the test harness with the test program with wires.
Incorrect:
wire sys_rstn;
assign sys_rstn = ~sys_rst;
Correct:
wire sys_clk;
wire sys_rst;
// no assign or always blocks
C9
Waveform configuration files must be updated if the block design or the configuration files are changed.
C10
The interconnect type must match the one recommended by AMD for the board that is specified for the simulation.
C11
Signals that are internal to an IP must not be accessed. The IP must be treated as a black box module.
Incorrect:
// peeking into the IP internals
fifo_level = `TH.dut_rx_dma.inst.i_fifo.level;
Correct:
// using the registermap through the API
status = dma_api.get_status();
D. Methods
D1
Function method definition must be used for operations that have no relation with simulation time.
Incorrect:
task add_function(
input int a,
input int b,
output int c);
c = a + b;
endtask: add_function
Correct:
function int add_function(
input int a,
input int b);
return a + b;
endfunction: add_function
D2
Brackets must be present at method and system function calls and declarations, even if these don’t require any arguments. This is to distinguish them from variables and constants.
Incorrect:
task add_function;
...
endtask: add_function
Correct:
task add_function();
...
endtask: add_function
D3
All method arguments must have their direction specified as input, output, inout or ref.
D4
Every named block must end with their identifier.
Incorrect:
class verifier;
...
endclass
Correct:
class verifier;
...
endclass: verifier
D5
Argument initial values in method definitions must be used only when the vast majority of the calls rely on the same default value, and there is a very low chance that forgetting to modify the argument value in a call causes issues.
Incorrect:
// most of the calls use different lengths, a forgotten argument
// goes unnoticed
task start_transfer(
input int length = 1024,
input bit cyclic = 1'b0);
...
endtask: start_transfer
Correct:
task start_transfer(
input int length,
input bit cyclic);
...
endtask: start_transfer
D6
On method calls, argument names must be specified alongside the argument values, except for logging, macros and system function calls.
Incorrect:
dma_api.setup_transfer(1024, 1'b0);
Correct:
dma_api.setup_transfer(
.length(1024),
.cyclic(1'b0));
D7
Multiple arguments in a method call must be broken into multiple lines with slight indentation. Single argument calls, logging, macros and system function calls may remain in a single line.
Incorrect:
dma_api.setup_transfer(.length(1024), .cyclic(1'b0));
Correct:
dma_api.setup_transfer(
.length(1024),
.cyclic(1'b0));
dma_api.start(.length(1024));
this.info($sformatf("Transfer length: %d", length), ADI_VERBOSITY_MEDIUM);
D8
Return values of methods for 1 bit variables (true/false) can be without bit width specification.
function bit is_busy();
return 1;
endfunction: is_busy
E. Event scheduling
E1
Event synchronization between multiple threads should be avoided, unless the user is very familiar with the simulation scheduler and knows about all of the corner cases that may arise using multiple threads or there is no other way around it.
E2
#0 and #1 delay must be removed and the logic around it changed. Leaving #0 and #1 would mean that synchronization between events is not properly handled and it’s prone to error if not used exactly the way it was intended to be used.
E3
Time value and scale must be specified for delaying statements.
Incorrect:
#5;
Correct:
#5us;
#(5*1us);
E4
Arbitrary delay events must be used only when there is no other option. Wait for a signal change, a register value to match a given value or an event to be triggered instead.
Incorrect:
// hoping that the transfer is done by now
#100us;
Correct:
wait(transfer_done == 1'b1);
// or
@(transfer_done_event);
F. Reporting
F1
Reporting system functions calls from the standard must only be used in VIP files related to the block design IP, including the interface and the base interface class.
$info("Info message example");
F2
Reporting macros must only be used in the test programs.
`INFO(("Info message example"), ADI_VERBOSITY_LOW);
F3
Reporting functions from the base reporting class must be used in classes that inherit these functions.
this.info($sformatf("Data received: %d", data), ADI_VERBOSITY_MEDIUM);
F4
Error messages should be used where the simulation may continue if something is not working as expected. In every other instance, where the simulation must halt immediately, use fatal messages.
this.error($sformatf("Error, but can continue"));
this.fatal($sformatf("Error and cannot continue"));
F5
Verbosity settings must be set as follows:
ADI_VERBOSITY_NONE: Only for simulation randomization state and simulation done messages
ADI_VERBOSITY_LOW: All info messages inside the test program, with the exceptions of ADI_VERBOSITY_NONE
ADI_VERBOSITY_MEDIUM: Inside drivers
ADI_VERBOSITY_HIGH: Inside VIP modules, regmaps, utilities
G. Classes
G1
Checker and scoreboard modules must be written to be parameterizable, which means that they should be able to work with any data type.
G2
Data and methods inside classes should be protected from outside access using protected and local keywords where it makes sense.
Incorrect:
int class_id;
int device_address;
task access_device();
...
endtask: access_device
Correct:
// always assigned by the parent, invisible to child classes
local int class_id;
// both parent and child might need access to it, but prevent the outside
// from accessing it
protected int device_address;
task access_device();
...
endtask: access_device
G3
adi_fifo or adi_lifo must be used for queues to avoid push-pull/front-back style differences and other issues.
adi_fifo #(data_length) fifo_buffer;
fifo_buffer = new();
G4
Checker or scoreboard class should be used whenever comparing data streams.
G5
Checker and scoreboard classes must use a subscriber class to get data.
H. VIP
H1
VIP agents must contain a driver, monitor and sequencer modules.
class vip_driver;
...
endclass: vip_driver
class vip_monitor;
...
endclass: vip_monitor
class vip_sequencer;
...
endclass: vip_sequencer
class vip_agent;
vip_driver vip_drv;
vip_monitor vip_mon;
vip_sequencer vip_seq;
...
endclass: vip_agent
H2
VIP monitors must contain a publisher module.
class vip_monitor;
adi_publisher #(<data_type>) publisher;
...
endclass: vip_monitor
H3
VIP drivers, monitors and sequencers must have their agent parents set.
Incorrect:
class vip_agent;
vip_driver vip_drv;
vip_monitor vip_mon;
vip_sequencer vip_seq;
function new();
vip_drv = new(.name("Driver"));
vip_mon = new(.name("Monitor"));
vip_seq = new(.name("Sequencer"));
endfunction: new
endclass: vip_agent
Correct:
class vip_agent;
vip_driver vip_drv;
vip_monitor vip_mon;
vip_sequencer vip_seq;
function new();
vip_drv = new(
.name("Driver"),
.parent(this));
vip_mon = new(
.name("Monitor"),
.parent(this));
vip_seq = new(
.name("Sequencer"),
.parent(this));
endfunction: new
endclass: vip_agent
H4
VIP drivers, monitors and sequencers must not be instantiated outside of the agent.
H5
VIP agents, drivers, monitors and sequencers should not be parameterizable. These classes should read parameter values from the interface class, which has direct access to the interface parameters if needed.
H6
VIP agents should have an environment as a parent.
Incorrect:
class environment;
vip_agent vip_agnt;
function new();
vip_agnt = new(.name("Agent"));
endfunction: new
endclass: environment
Correct:
class environment;
vip_agent vip_agnt;
function new();
vip_agnt = new(
.name("Agent"),
.parent(this));
endfunction: new
endclass: environment
H7
VIP classes must be instantiated with an interface class and not with a virtual interface. AMD VIPs are an exception from this rule.
Incorrect:
class vip_agent;
protected vif proxy_vif;
function new(virtual interface proxy_if_type proxy_vif);
this.proxy_vif = proxy_vif;
endfunction: new
endclass: vip_agent
Correct:
class vip_agent;
protected vif_class proxy_vif;
function new(vif_class proxy_vif);
this.proxy_vif = proxy_vif;
endfunction: new
endclass: vip_agent
H8
AMD AXI and AXI Streaming VIPs must be linked to ADI base class VIPs located inside the environment.
test_harness_env base_env;
base_env = new(...);
mng = new(
.name(""),
.axi_vip_if(`TH.`MNG_AXI.inst.IF));
ddr = new(
.name(""),
.axi_vip_if(`TH.`DDR_AXI.inst.IF));
`LINK(mng, base_env, mng)
`LINK(ddr, base_env, ddr)
H9
Use the clocking VIP when generating clocks. The exception is when the desired clock cannot be generated using the clocking VIP.
Incorrect:
always #5ns clk = ~clk;
Correct:
clk_vip.set_clk_frq(.user_frequency(100000000));
clk_vip.start_clock();
...
clk_vip.stop_clock();
H10
Use the IO VIP when manipulating signals that are not part of an interface, such as AXI or AXI Stream.
H11
An IP or VIP definition in the test harness should be touched only once in the test program, when assigning it to a class.
Incorrect:
`TH.`IO_VIP.inst.inst.IF.set_io(.o(1'b1));
`TH.`IO_VIP.inst.inst.IF.set_io(.o(1'b0));
Correct:
io_vip_if_base io_vip;
io_vip = `TH.`IO_VIP.inst.inst.IF;
io_vip.set_io(.o(1'b1));
io_vip.set_io(.o(1'b0));
I. API
I1
APIs that can control an IP that has a parameterizable registermap must also be parameterized with the same parameters using a macro.
I2
APIs with registermaps must have a sanity test implemented. Exceptions are those that don’t have Version, Magic and Scratch registers.
task sanity_test();
// check version compatibility
...
// check magic number
...
// check scratch register
...
endtask: sanity_test
I3
IP register access calls must only be written inside an API.
I4
Every ADI IP that has a registermap must have its own API driver class.
I5
IRQ handler class must be used when dealing with interrupt requests.
I6
Setting register values must have bit width specifications, in the form of
<width>'<identifier><value>.
Incorrect:
dma_api.write_register(
.address(DMA_CONTROL),
.data(1));
Correct:
dma_api.write_register(
.address(DMA_CONTROL),
.data(32'h0000_0001));
J. Environment
J1
An environment must only contain VIP agents, APIs and/or checkers.
J2
Test_harness_env should not be inherited by any environment.
Incorrect:
class test_harness_env extends adi_environment;
...
endclass: test_harness_env
class new_env extends test_harness_env;
...
endclass: new_env
Correct:
class test_harness_env extends adi_environment;
...
endclass: test_harness_env
class new_env extends adi_environment;
...
endclass: new_env
J3
New environments should be created with the intent to be reused in other testbenches.
K. Randomization
K1
Constrained randomized values should be used for randomized testing.
K2
Variable randomization states must always be checked.
class randomizer_class extends adi_component;
rand bit switch;
function void randomize_switch();
if (!this.randomize()) begin
this.fatal($sformatf("Randomization failed!"));
end
endfunction: randomize_switch
endclass: randomizer_class
K3
All random variables must be randomized when the class creation occurs.
class randomizer_class extends adi_component;
rand bit switch;
function new();
this.randomize_init();
endfunction: new
function void randomize_init();
if (!this.randomize()) begin
this.fatal($sformatf("Randomization failed!"));
end
endfunction: randomize_init
endclass: randomizer_class
K4
Test programs must output the simulation randomization state at the very beginning of the simulation.
process current_process;
string current_process_random_state;
current_process = process::self();
current_process_random_state = current_process.get_randstate();
`INFO(("Randomization state: %s", current_process_random_state), ADI_VERBOSITY_NONE);
K5
The testbench should have a randomized configuration file paired with a randomized testbench.