0
0
VerilogHow-ToBeginner · 3 min read

How to Use $monitor in Verilog for Signal Monitoring

In Verilog, $monitor is used to automatically print signal values whenever any of the specified signals change during simulation. You use it by calling $monitor with a format string and the signals you want to watch, typically inside an initial block.
📐

Syntax

The basic syntax of $monitor is:

$monitor("format string", signal1, signal2, ...);

Here:

  • format string: A string with placeholders like %d or %b to format the output.
  • signal1, signal2, ...: The signals or variables whose values you want to display.

$monitor prints the message automatically whenever any of the listed signals change.

verilog
$monitor("At time %0t: a=%b, b=%b, sum=%b", $time, a, b, sum);
💻

Example

This example shows how $monitor tracks changes in signals a, b, and sum during simulation.

verilog
module monitor_example;
  reg a, b;
  wire sum;

  assign sum = a ^ b; // XOR operation

  initial begin
    $monitor("At time %0t: a=%b, b=%b, sum=%b", $time, a, b, sum);
    a = 0; b = 0;
    #5 a = 1;
    #5 b = 1;
    #5 a = 0;
    #5 b = 0;
    #5 $finish;
  end
endmodule
Output
At time 0: a=0, b=0, sum=0 At time 5: a=1, b=0, sum=1 At time 10: a=1, b=1, sum=0 At time 15: a=0, b=1, sum=1 At time 20: a=0, b=0, sum=0
⚠️

Common Pitfalls

  • Multiple $monitor calls: Only the last $monitor call is active; previous ones are overridden.
  • Using $monitor outside initial or always blocks: It should be inside an initial block to start monitoring at simulation start.
  • Forgetting to include $time: Without $time, you won't see when changes happen.
  • Confusing $monitor with $display: $display prints once when called; $monitor prints automatically on signal changes.
verilog
/* Wrong: multiple $monitor calls - only last works */
initial begin
  $monitor("a=%b", a);
  $monitor("b=%b", b); // overrides previous monitor
end

/* Right: single $monitor call monitoring multiple signals */
initial begin
  $monitor("a=%b, b=%b", a, b);
end
📊

Quick Reference

FeatureDescription
$monitorAutomatically prints when any listed signal changes
Format stringUse %b for binary, %d for decimal, %h for hex, %0t for time
PlacementInside an initial block to start monitoring at simulation start
Multiple callsOnly the last $monitor call is active
$timeUse to print current simulation time

Key Takeaways

Use $monitor inside an initial block to watch signals automatically on change.
Only one $monitor call is active at a time; multiple calls override each other.
Include $time in the format string to see when changes occur during simulation.
Use format specifiers like %b, %d, and %h to display signals in different formats.
$monitor differs from $display by printing output automatically on signal changes.