plusargs in systemverilog

Master the Power of Plusargs in SystemVerilog: Must-Know Tips

In short: Plusargs are command line switches that a simulator passes into your SystemVerilog code at run time. You read them with two system functions: $test$plusargs checks whether a flag is present, and $value$plusargs reads a value that follows a flag. Because they are read at run time, you can change test name, seed, or debug level without recompiling. Pass them on the run command with a leading plus sign, such as +TEST=axi_test.

A good testbench should not need a rebuild every time you want a different test or a higher debug level. Plusargs give you that control from the command line. The compiled image stays the same, and you steer its behaviour with switches when you run it.

A simple way to picture it

Think of a coffee machine with buttons on the front. The machine is built once, but each morning you press different buttons for a small or large cup, extra shot, or decaf. You do not rebuild the machine to get a different drink; you just press the buttons at the time you use it. Plusargs are those buttons: the compiled testbench is the machine, and the switches you type when you run it are the button presses.

The two system functions

SystemVerilog gives you two functions to read plusargs. Both search the list of run time arguments and return a one bit result you can test in an if.

FunctionReturnsUse it to
$test$plusargs("FLAG")1 if the flag is present, else 0Turn a mode on or off, such as a debug switch
$value$plusargs("NAME=%s", var)1 if matched and var is set, else 0Read a value such as a test name, seed, or count

If $value$plusargs does not find a match, it returns 0 and leaves the variable unchanged, so always give the variable a sensible default first.

Example 1: turn a mode on with $test$plusargs

Use $test$plusargs for on or off switches. Here a debug section runs only when you pass the flag on the command line.

module test_flag;
  initial begin
    if ($test$plusargs("DEBUG"))
      $display("DEBUG mode is ON");
    else
      $display("DEBUG mode is OFF");
  end
endmodule

// Run with:  +DEBUG   -> prints "DEBUG mode is ON"
// Run without it       -> prints "DEBUG mode is OFF"

Example 2: read a value with $value$plusargs

Use $value$plusargs to read a value into a variable. The format specifier says what type to read: %s for a string, %d for a decimal number, and so on. Set a default first so a missing switch is safe.

module read_value;
  initial begin
    string test_name = "smoke_test";   // default
    int    num_txn   = 10;              // default

    void'($value$plusargs("TEST=%s", test_name));
    void'($value$plusargs("NUM=%d", num_txn));

    $display("Running test = %s with %0d transactions", test_name, num_txn);
  end
endmodule

// Run with:  +TEST=axi_test +NUM=50
// prints:    Running test = axi_test with 50 transactions

Example 3: a real seed and config pattern

A common real use is to control the random seed and pick a config from the command line, so one build can run many different runs in regression. Here the code reads a seed and a mode, and falls back to defaults when a switch is missing.

module run_control;
  initial begin
    int    seed = 1;
    string mode = "normal";

    if (!$value$plusargs("SEED=%d", seed))
      $display("No SEED given, using default %0d", seed);
    void'($value$plusargs("MODE=%s", mode));

    $display("seed=%0d mode=%s", seed, mode);
    // use seed to control randomization, mode to pick a config
  end
endmodule

// Run with:  +SEED=42 +MODE=stress
// prints:    seed=42 mode=stress

Expected output, in plain words

In example 1 the DEBUG line prints ON only when you add the switch. In example 2 the run prints the test name and count you passed, or the defaults if you passed nothing. In example 3 a missing SEED prints the fallback note and keeps the default, while a given SEED and MODE are echoed back.

Note: these outputs describe what the code is written to produce, based on a read of the IEEE 1800 LRM. The exact switch syntax can vary slightly by simulator, so check your tool docs and run it yourself to confirm.

When to use plusargs

  • You want to pick a test, seed, or config at run time without recompiling.
  • You want a debug or logging mode you can turn on only when needed.
  • You run many variations in a regression from one compiled image.
  • You want quick manual control while bringing up a new block.

Common mistakes to avoid

  • No default value: if $value$plusargs does not match, your variable keeps its old value, so set a sensible default first.
  • Wrong format specifier: reading a number with %s or a string with %d gives surprising results. Match the specifier to the type.
  • Ignoring the return value: the return flag tells you whether the switch was present. Use it when the difference matters.
  • Expecting compile time effect: plusargs are read at run time, so they cannot add or remove code. For that you need a compile macro.

Plusargs vs compile macros

Plusargs are read while the simulation runs, so the whole design is compiled and you change behaviour at run time. A compile macro such as an ifdef decides what code is even built, so changing it needs a recompile. We compare these two in detail in our guide on ifdef vs plusargs.

For more run time control patterns, see our SystemVerilog tutorials and interview questions.

Frequently asked questions

What are plusargs in SystemVerilog?

Plusargs are command line switches that a simulator passes into your code at run time. You read them with $test$plusargs and $value$plusargs to control test name, seed, debug mode, and more without recompiling.

What is the difference between $test$plusargs and $value$plusargs?

$test$plusargs checks whether a flag is present and returns 1 or 0. $value$plusargs reads a value that follows a flag, such as TEST=axi_test, into a variable and returns whether it matched.

How do I pass a plusarg on the command line?

Add it to the simulator run command with a leading plus sign, for example +DEBUG for a flag or +TEST=axi_test for a value. The exact command differs by simulator.

What happens if a plusarg is not provided?

$test$plusargs returns 0 and $value$plusargs returns 0 and leaves the variable unchanged. Always set a sensible default before reading so a missing switch is safe.

Can plusargs add or remove code?

No. Plusargs are read at run time, so the whole design is already compiled. To include or exclude code at build time you need a compile macro such as ifdef.

What is a good real use of plusargs?

Controlling the random seed, selecting a test, and picking a config from the command line, so one compiled image can run many different regression runs.

Similar Posts