Class Assignment 2
Class Assignment 2
date : 01-06-2022
QUESTIONS
1. Difference between $display and $strobe and $monitor with code
2. Write a verilog code for generating 5 random between 1.0 and 2.0.
3. write RTL for 3:8 decoder using shift operator
4. Write an RTL for N bit shift register using for loop?
EXAMPLE CODE
// RTL
module test_t(a,b,c,x,y);
input a,b,c;
output reg x,y;
always@(*)
begin
x <= a + b;
y <= a - b;
x <= c;
end
endmodule
// TESTBENCH
module test();
reg a,b,c;
wire x,y;
test_t DUT(a,b,c,x,y);
initial
begin
#10 a = 1'b1;
#10 b = 1'b0;
#10 c = 1'b0;
end
initial
$display("inputs = %b %b %b outputs = %b %b",a,b,c,x,y); // $display is used to display values of
a,b,c,x,y
initial
#50 $finish();
endmodule
$strobe
1. Strobing is done with system task called $strobe.
2. $strobe is much similar to $display except,if multiple statements and $display are executed at same
time unit the execution of $display is non-deterministic,where as $strobe is executed after all other
assignment statements complete the execution.
3. $strobe provides synchronization in simulation environment ,that is, data is displayed only after other
assignment statements which impact the values displayed by $strobe are executed.
EXAMPLE CODE
// RTL
module test_t(clk,b,d,a,c);
input clk,b,d;
output reg a,c;
always@(posedge clk)
begin
a = b;
c = d;
end
endmodule
// TESTBENCH
module test();
reg b,d,clk;
wire a,c;
test_t DUT(clk,b,d,a,c);
initial
begin
repeat(5)
clk = 1'b0;
#5 clk = ~clk;
end
initial
begin
#10 b = 1'b1;
#10 c = 1'b0;
#10 b = 1'b0; c = 1'b1;
end
always@(posedge clk)
$strobe("display a = %b,c = %b",a,c);
endmodule
$monitor
1. Mechanism to monitor a signal when it changes it value is provided by $monitor task.
2. Format used by $monitor is similar to that of $display .
3. It continuously monitors the parameter of signals mentioned and displays all parameters in the list
whenever the value of any one variable changes .
4. Unlike $display , $monitor gets invoked only once.
5. additional tasks called $monitoron and $monitoroff are used to start and terminate $monitor task
mid-way through the simulation.
EXAMPLE CODE
module test();
reg clk,rst;
initial
begin
repeat(5)
#5 clk = 1'b1;
#10 clk = 1'b0;
end
initial
begin
rst = 1'b0;
#20 rst = 1'b1;
#10 rst = 1'b0;
end
initial
$monitor($time,"value of clock signal = %b reset = %b",clk,rst);
endmodule
2) WRITE A VERILOG CODE FOR GENERATING 5 RANDOM BETWEEN 1.0 AND 2.0.
CODE
module test(y);
output reg y;
integer i;
always@(*)
begin
for(i=0;i<6;i=i+1)
y = {$random}%1.0 + 1.0;
end
endmodule
always@(*)
begin
for(i=0;i<10;i=i+1)
y = {d,y[9-1:0]}; // assigning input to LSB everytime new value is entered to d
end
endmodule