Today we continue with the Logic Design series on SystemVerilog in order to talk about Command Line Arguments & Dynamic Casting. These are also the final topics that we had to cover before getting into Examples...
So, without further ado, let's get straight into it!
Command Line Arguments
When anything (which includes even a single variable's value) is tweaked within a testbench, the testbench will need to be recompiled each time. In order to allow small tweaks, without the need of recompilation, SystemVerilog allows information to be passed as arguments from the command line.
Passing Arguments
The command line arguments are optional. They start with the + character, followed by an identifier in caps and an = sign. After the = sign comes the value to be used. If left unspecified the value will be an empty string. But, the = can't be missing, as otherwise the argument will not be recognized. Additionally, there should not be any space between these characters.
For example:
+N=100+STR=test+FLAG=
Note: Strings can be passed with and without double quotes ("").
Using Arguments
From within the SystemVerilog code, these command line arguments are accessible through two $plusargs system functions:
The first one is used when the value of the argument is not required, but only it's "existence" should be checked. This can be easily used as a form of flag for enabling / disabling code. A zero is returned when the characters specified don't match any argument.
For example:
if($test$plusargs("FLAG"))// code
The second function requires some kind of string formatting to be specified. Similar to the $display system task.
For example:
$value$plusargs("N=%d",n)
would retrieve the value 100 specified in the argument N and pass it to the variable n.
Dynamic Casting
Sometimes we may want to assign data types, which normally would be invalid using =. For that purpose SystemVerilog provides the $cast method, which is used for dynamic casting.
It can be used as both a task and a function, which changes it's behavior a bit. The difference is that the function returns a 0 when the cast is illegal (1 when it's legal), whilst the task causes an runtime error.
The syntax is:
$cast(destination,source);
For example, this can be used for assigning an integer value to an enumerated value:
Constraints and Randomization → Testing and Verification, Random Variables (Standard, Random-Cyclic), Randomize Method (Constraint and Random Mode, Pre / Post Randomize)