RTL Studio Open IDE

1.4 · SYSTEMVERILOG CLASS

string and $sformatf

Use string variables and $sformatf for formatted simulation logging in testbenches.

Watch on YouTube

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 string variables in SystemVerilog.
  • Use $sformatf to construct formatted runtime messages.
  • Combine string formatting with $display for readable testbench logs.
  • Understand how $sformatf differs from direct $display formatting.

Theory

SystemVerilog provides a built-in string data type for handling text at runtime.

FeatureDescription
string typeDynamic-length text container; supports concatenation, assignment, and methods like .len().
$sformatfReturns a formatted string (like C's sprintf); used to build messages before printing or storing.
$displayPrints 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.

  1. Open student/tb/tb.sv from the Explore tree.
  2. In the first test case block (after a = 5; b = 7; #10;), replace the incomplete $sformatf call with a full call that formats "a=%d b=%d sum=%d" using the current values of a, b, and sum.
  3. Run Simulation and check the Log panel.
  4. 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

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. 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.