$display and $monitor help you see what is happening inside your Verilog code while it runs. They show values of signals so you can understand and check your design.
Using $display and $monitor in Verilog
$display("format string", variables); $monitor("format string", variables);
$display prints once when it is called.
$monitor prints automatically whenever any variable in its list changes.
$display("Time=%0t, a=%b, b=%b", $time, a, b);$monitor("At time %0t: x=%d, y=%d", $time, x, y);This program uses $monitor to print the values of a and b whenever they change. It shows how $monitor automatically updates output on signal changes.
module test;
reg a, b;
initial begin
$monitor("At time %0t: a=%b, b=%b", $time, a, b);
a = 0; b = 0;
#5 a = 1;
#5 b = 1;
#5 a = 0;
#5 $finish;
end
endmodule$display is like taking a snapshot at one moment.
$monitor keeps watching and prints whenever something changes.
You can use format specifiers like %b for binary, %d for decimal, and %0t for time.
$display prints once when called, useful for single messages.
$monitor prints automatically on changes, useful for watching signals.
Both help you understand what your Verilog code is doing during simulation.
Practice
$display and $monitor in Verilog?Solution
Step 1: Understand
$displaybehavior$displayprints a message only once at the time it is called during simulation.Step 2: Understand
$monitorbehavior$monitorautomatically prints a message every time any of the monitored variables change value.Final Answer:
$displayprints once when called;$monitorprints whenever a variable changes. -> Option BQuick Check:
$display= once,$monitor= on change [OK]
- Thinking $display prints continuously
- Confusing $monitor with file output
- Assuming $monitor prints only once
reg [3:0] count; using $display?Solution
Step 1: Identify the data type and format specifier
The registercountis 4-bit binary, so binary format specifier%bis appropriate.Step 2: Match format specifier with
$displaysyntax$display("Count value: %b", count);correctly prints the binary value ofcount.Final Answer:
$display("Count value: %b", count); -> Option DQuick Check:
Binary format = %b [OK]
- Using %s which is for strings, not numbers
- Using %d for binary values
- Forgetting to include variable after format string
reg [1:0] a;
initial begin
a = 2'b00;
$monitor("Time=%0t a=%b", $time, a);
#5 a = 2'b01;
#5 a = 2'b11;
#5 a = 2'b10;
endSolution
Step 1: Understand $monitor initial print
$monitor prints immediately when called (time 0) and then whenever monitored variables change.Step 2: Trace changes to 'a' and timing
'a' changes at 5, 10, and 15 time units, so $monitor prints at these times plus initial time 0.Final Answer:
Prints four lines showing time and value of 'a' at 0, 5, 10, and 15 time units. -> Option AQuick Check:
$monitor prints on call + changes = 4 prints [OK]
- Assuming $monitor prints only on changes, missing initial print
- Thinking $monitor prints only once
- Confusing $display behavior with $monitor
reg clk;
initial begin
$monitor("Clock = %b", clk);
clk = 0;
#10 clk = 1;
#10 clk = 0;
endSolution
Step 1: Check order of $monitor and variable initialization
$monitor is called before clk is assigned a value, so initial print shows 'x' (unknown).Step 2: Understand $monitor initial print behavior
$monitor prints immediately on call, so it prints clk's value before initialization.Final Answer:
$monitor is called before clk is initialized, so initial print shows unknown value. -> Option CQuick Check:
Initialize variables before $monitor call [OK]
- Assuming $monitor waits for variable initialization
- Thinking missing semicolon causes error here
- Confusing reg and wire for $monitor usage
reg [3:0] data; and reg valid; so that the simulation prints their values only when valid changes. Which is the best way to do this using $monitor?Solution
Step 1: Understand $monitor behavior with multiple variables
$monitor prints whenever any monitored variable changes. Monitoring both data and valid prints on either change.Step 2: Restrict printing to valid changes only
To print only when valid changes, monitor only valid. But then data won't print updated values automatically.Step 3: Best practical approach
Monitoring both variables prints all changes, but since data changes only when valid changes, this works well. $monitor("Data=%b Valid=%b", data, valid); is simplest and correct.Final Answer:
$monitor("Data=%b Valid=%b", data, valid); -> Option AQuick Check:
Monitor all needed vars; prints on any change [OK]
- Trying to put conditions inside $monitor (not allowed)
- Monitoring only one variable and missing others' changes
- Using $display inside $monitor incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
