System Verilog: Program Block & Interface
System Verilog: Program Block & Interface
ROHIT KHANNA
Program Block
Verilog module works well for design but when used for Test
benches may lead to race-around condition between design and
Test bench.
Preponed Active
Re-Active
Program Block
To Next Time Slot
Re-Inactive Runs Here
Postponed Re-NBA
module tb;
reg clk=0, t=1;
module tff (q, clk, t);
wire q=0;
input clk, t;
output reg q=0; always #5 clk=~clk;
endmodule
Result:
5 q= 0
15 q= 1
25 q= 0
35 q= 1
45 q= 0
55 q= 1
endprogram
module top;
reg clk=0, t;
wire q;
always #5 clk=~clk;
endmodule
Result:
5 q= 1
15 q= 0
25 q= 1
35 q= 0
45 q= 1
55 q= 0
program tb;
int a;
initial $monitor(“result is %d”, a);
Result : result is 2
initial begin a=5
#3 a= a + 2;
#4 a= a + 3; $monitor does not execute for
end a=5 because of implicit $exit
endprogram
program tb;
int a;
initial $monitor(“result is %d”, a);
initial begin
#3 a= a + 2; Result :
#4 a= a + 3; result is 2
#1 ; result is 5
end
endprogram
endmodule
initial
begin
$monitor (“a=%0d b=%0d sum=%0d”, a, b, sum);
forever begin a=$random; b=$random; #10; end
end
endprogram
always #5 clk=~clk;
logic [3:0] a, b;
logic [4:0] sum;
endinterface : inf
endmodule : adder
initial begin
$monitor (“a=%0d b=%0d sum=%0d”, inf.a, inf.b, inf.sum);
forever begin
inf.a=$random;
inf.b=$random;
#10; end
end
endprogram : tb
always #5 clk=~clk;
endmodule
logic [3:0] a, b;
logic [4:0] sum;
//incase of inout port use wire
modport DUT (input clk, a, b, output sum);
modport TB (input clk, sum, output a, b);
endinterface : intf
System Verilog Program Block and Interface
Design
endmodule : adder
initial begin
$monitor (“a=%0d b=%0d sum=%0d”, inf.a, inf.b, inf.sum);
forever begin
inf.a=$random;
inf.b=$random;
#10; end
end
endprogram : tb
always #5 clk=~clk;
intf i0 (.*);
adder a0 (.*);
tb t0 (.*);
endmodule
Clk
en
Output of Test Bench
en
Clk
count 1 2 3 4 5 6 7
0 1 2 3 4 5 6
count
Input to Test Bench
………………………
clocking cb @ (posedge clk); //specifying active clock edge
default input #3ns output #2ns;
//Specifying user default for sampling and driving
………………………
clocking cb @ (posedge clk); //specifying active clock edge
default input #3ns output #2ns;
//Specifying user default for sampling and driving
endmodule : counter
initial begin
@inf.cb; //continue on active edge in cb
#3 inf.cb.en<=1; // use NBA for signals in cb
##8 inf.cb.en<=0; //wait for 8 active edges in cb
repeat(2) @inf.cb; //wait for 2 active edges in cb
inf.cb.en<=1;
wait (inf.cb.count==3) inf.cb.en<=0; //wait for count to become 3
end
endprogram : tb
System Verilog Program Block and Interface
Parameterized Interface
endinterface