What you practice
string and $sformatf (1.4). Use string variables and $sformatf for formatted simulation logging in testbenches. It is in Aggregate Data Types — Part 1 · Design in SV of the SystemVerilog Masterclass. Synthesizable SystemVerilog that removes Verilog ambiguity and cuts boilerplate.
The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.
What you will learn
Today you will learn how to use SystemVerilog's native string type and the $sformatf function to build dynamic log messages in testbenches, making simulation output clearer and more informative.
Learning objectives
- Declare and assign
stringvariables in SystemVerilog. - Use
$sformatfto construct formatted runtime messages. - Combine string formatting with
$displayfor readable testbench logs. - Understand how
$sformatfdiffers from direct$displayformatting.
Theory
SystemVerilog provides a built-in string data type for handling text at runtime.
| Feature | Description |
|---|---|
string type | Dynamic-length text container; supports concatenation, assignment, and methods like .len(). |
$sformatf | Returns a formatted string (like C's sprintf); used to build messages before printing or storing. |
$display | Prints directly; can also use format specifiers (%d, %h, %b, %s) but does not return a string. |
sformatf is especially useful when you want to store a formatted message in a variable, reuse it, or build complex log entries conditionally.
Practice
Open this chapter in RtlStudio. Files appear in Explore — no setup needed.
- Open
student/tb/tb.svfrom the Explore tree. - In the first test case block (after
a = 5; b = 7; #10;), replace the incomplete$sformatfcall with a full call that formats"a=%d b=%d sum=%d"using the current values ofa,b, andsum. - Run Simulation and check the Log panel.
- Confirm the log shows formatted messages for all three test cases and ends with
INFO: PASS.
Summary
You practiced using SystemVerilog's native string type to store and manipulate text in testbenches. You learned to construct dynamic log messages with $sformatf, enabling clean and readable runtime output. The key takeaway is that strings and $sformatf together provide a powerful, flexible way to generate informative simulation logs.
Run it in the Verilog IDE
- Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
- Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
- Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
- Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.
The lesson is done when the simulation matches string and $sformatf. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.