Many times a user would like to be able to modify the behavior of a program based on arguments on the command line. Let’s take as an example a user who would like to pass the name of a file which includes input parameters to the e program.
In the C language this can be done using argv, which is an array of strings where each array element represents a command line argument. However, since Specman and the simulator are programs themselves, the command line arguments are passed to those programs and the underlying e code does not have access to them. One possible solution is to use define macros. The code would look something like this:
var f: file = files.open(IN_FILE, "r", “”);
Then the user could define the macro on the command line, like this:
specman -c 'define IN_FILE "in.txt"; load in.e; test‘
There are several drawbacks to this solution. The first is that this will not work for compiled e code, since the macro must be defined at compile time and cannot be changed afterwards. Another drawback is that the user must define the macro, even if the code is never used. Otherwise the code will fail to load since IN_FILE is not defined.
So what can be done? Specman has a more robust solution based on plusargs. A plusarg is an argument on the command line that is preceded by a plus sign (‘+’). It has one of the following formats:
- +arg
- +arg=value
Specman has 2 routines to support plusargs:
- sn_plusarg_exists(arg: string): bool
- sn_plusarg_value(arg: string): string
The first routine returns TRUE if the arg exists on the command line as a plusarg. The second routine returns the value of a particular plusarg argument if it exists on the command line.
These routines are similar, but not identical to the Verilog system functions $test$plusargs() and $value$plusargs(). Note the following differences between the Specman routines sn_plusarg_exists() and sn_plusarg_value() and the Verilog system functions $test$plusargs() and $value$plusargs():
- $test$plusargs() returns TRUE even if its argument is only the prefix of the actual plusarg, whereas sn_plusarg_exists() returns TRUE only if its argument is equal to the actual argument. So if the plusarg is ‘+hello’, then $test$plusargs(“he”) will return TRUE, but sn_plusarg_exists(“he”) will return FALSE.
- $value$plusargs() receives as its first argument a format string similar to $fscanf() so that values other than strings can be read directly, whereas sn_plusarg_value() always returns a string value that can be converted to another type using as_a(). So if we had a plusarg ‘+count=100’, then
- in Verilog we would specify:
$value$plusargs("count=%d", counter);
- and in Specman we would specify:
counter = sn_plusarg_value(“count”).as_a(int);
Using these Specman routines, our code to open a file passed to the e program from command line, and allowing for the possible use of a default file name, would look something like this:
var infile: file;
var fname: string = sn_plusarg_value("in_file");
if (fname == "") {
if sn_plusarg_exists(“use_default”) {
fname = “in_file.txt”;
}
else {
error("No in file was given");
};
}
else {
infile = files.open(fname, "r", NULL);
};
The command line would then be one of the following:
- To use the default file name:
specman +use_default -c "load in1; test“
- To explicitly specify the input file name.
specman +in_file=in.txt -c "load in1; test“
If the e code was compiled, the command line would be:
in1 +in_file=in.txt -c "test“
One could also run this code as part of a full simulation environment, in which case the command line would be:
irun +in_file=in.txt in1.e xor.v
You could even use the same plusarg for both your Verilog code and your e code.
For more details on this feature, search for sn_plusarg in the Specman e Language Reference manual.
Avraham Bloch