Systemverilog-Module3-Dec2021
Systemverilog-Module3-Dec2021
• May cause a race condition since ++ and -- operators behave as blocking assignments
2
System Verilog Procedural Statements
for loop variables are declared outside the loop in Verilog
• Concurrent loops interfere with each other
• when ever the loop value is with in 3 to 6, continue statement will be executed, this leads to skip the execution of display
statement after the continue.
Task and Function in SV
• A function is meant to do some processing on
the input and return a single value, whereas a
task is more general and can calculate multiple
result values and return them using output
and inout type arguments.
Task and Function in SV
• module sv_task;
• int x;
•
• //task to add two integer numbers.
• task sum(input int a,b,output int c);
• c = a+b;
• endtask
•
• initial begin
• sum(10,5,x);
• $display("\tValue of x = %0d",x);
• end
• endmodule
task arguments in declarations and mentioning directions
• module sv_task;
• int x;
•
• //task to add two integer numbers.
• task sum;
• input int a,b;
• output int c;
• c = a+b;
• endtask
•
• initial begin
• sum(10,5,x);
• $display("\tValue of x = %0d",x);
• end
• endmodule
Functions
• A Function can contain declarations of range,
returned type, parameters, input arguments,
registers, and events.
• A function without a range or return type
declaration returns a one-bit value
• Any expression can be used as a function call
argument
• Functions cannot contain any time-controlled
statements, and they cannot enable tasks
• Functions can return only one value
• Function declration can be as in verilog 1995/2001 or
can be declared as in C or C++. In SystemVerilog
following rules hold good for any Function declaration
• Default Port Direction : Any port is seen as input, unless
declared as other types. Following are port types
• input : copy value in at beginning
• output : copy value out at end
• inout : copy in at beginning and out at end
• ref : pass reference
• Default Data TYpe : Unless declared, data types of ports are
of logic type.
• begin..end : There is no need to have begin, end, when more
then one statement is used.
• return : A function can be terminated before endfunction, by
usage of return statement.
• Variables : Systemverilog allows to have local static, or local
dynamic variables.
• life time : SystemVerilog allows a function to be static or
automatic.
• Wire : Wire data type can not be used in port list;
• void : SystemVerilog allows functions to be declared as type void
function arguments in parentheses
• module sv_function;
• int x;
• //function to add two integer numbers.
• function int sum(input int a,b);
• sum = a+b;
• endfunction
•
• initial begin
• x=sum(10,5);
• $display("\tValue of x = %0d",x);
• end
• endmodule
function arguments in declarations and mentioning directions
• module sv_function;
• int x;
•
• //function to add two integer numbers.
• function int sum;
• input int a,b;
• sum = a+b;
• endfunction
• initial begin
• x=sum(10,5);
• $display("\tValue of x = %0d",x);
• end
• endmodule
function with return value with the return keyword
• module sv_function;
• int x;
•
• //function to add two integer numbers.
• function int sum;
• input int a,b;
• return a+b;
• endfunction
•
• initial begin
• x=sum(10,5);
• $display("\tValue of x = %0d",x);
• end
• endmodule
Void function
• module sv_function;
• int x;
• //void function to display current simulation time
• function void current_time;
• $display("\tCurrent simulation time is %0d",$time);
• endfunction
•
• initial begin
• #10;
• current_time();
• #20;
• current_time();
• end
discarding function return value
• module sv_function;
• int x;
• //function to add two integer numbers.
• function int sum;
• input int a,b;
• return a+b;
• endfunction
• initial begin
• x = 10 + sum(10,5);
• $display("\tValue of x = %0d",x);
• end
• endmodule
SystemVerilog Tasks and Functions
• Tasks and Functions
– SystemVerilog functions and tasks do not require begin and end
statements
– SystemVerilog adds a return statement
– Void functions do not return a value
– Functions can have output and inout as formal arguments
task reset();
reset_l = 1’b0;
function int add2(int n);
#100
return n + 2;
reset_l = 1’b1;
endfunction
endtask
27
SystemVerilog Tasks and Functions
• Return Statement
– SystemVerilog adds a return statement
• If a return statement is executed that value is returned
else the last value assigned to the function name is the
return value
• A return statement can be used to exit a task or a
function
function int add_and_inc (input [31:0] a,b);
add_and_inc=a+b+1; Return has priority over
endfunction returning the value in the
function int add_and_inc (input [31:0] a,b); name of the function
add_and_inc=a+b;
return ++add_and_inc;
endfunction
28
SystemVerilog Tasks and Functions
• Void functions
– Void functions do not return a value
– Output and inout formal arguments allow a void
function to propagate changes to the scope
– A void function can be called like a task but must
adhere to the restriction of function contents
29
• typedef struct{
• logic valid;
• logic [7:0] check;
• logic [63:0] data;
• } packet_t;
31
• function int divide (input int numerator, denominator);
• if(denominator==0)
• begin
• return 0; always @(posedge clock)
• end result<=divide(b,a)
• else
• return numerator/denominator;
• endfunction
Testbench
Design Under
Driver
Test
.*Port, .Name,
?
Interfaces
33
Connecting the Testbench and the Design
• One way to connect the testbench and the design is to use the
conventional verilog module ports convention
A_out_signal1 B_in_signal1
A_out_signal2 B_in_signal2
Module A Module B
A_in_signal1 B_out_signal1
A_in_signal2 B_out_signal2
module B (
module A (
input B_in_signal1,
input A_in_signal1,
input B_in_signal2
input A_in_signal2
output B_out_signal1,
output A_out_signal1,
output B_out_signal2,
output A_out_signal2,
);
);
…
…
endmodule
endmodule
34
Connecting the Testbench and the Design
• Verilog connection review
– Verilog language connects modules together through module ports
A_out_signal1 conn1 B_in_signal1
module top (
wire conn1,
wire conn2
wire conn3,
wire conn4,
);
A A_instance1(.A_out_signal1(conn1),
.A_out_signal2(conn2),
.A_in_signal1(conn3),
.A_in_signal2(conn4));
B B_instance1(.B_in_signal1(conn1),
.B_in_signal2(conn2),
.B_out_signal1(conn3),
.B_out_signal2(conn4));
endmodule
35
Connecting the Testbench and the Design
Lets do another example:
Testbench
36
System Verilog: .*Port Connections
• Implicit .* port connections
.* infers connections of all nets and ports of the same name
For a connection to be inferred the name and the vector sizes
should be the same
Types connected together should be compatible
module top
logic [1:0] grant,request;
logic clk, reset;
arb_port a1(.grant(grant), .request(request), .reset(reset), .clk(clk));
test t1(.grant(grant), .request(request), .reset(reset), .clk(clk));
…
endmodule
module top
logic [1:0]
grant,request;
logic clk, reset;
arb_port a1(.*);
test t1(.*);
…
endmodule .*Port Connections
37
System Verilog: .Name Connections
• Implicit .name connections
.name is an abbreviation of named port connections
.name infers a connection of a net and port of the same
name and same vector sizes
.name simplifies connections to module instances
.name can be combined with named port connections
module top
logic [1:0] grant,request;
logic clk, reset;
arb_port a1(.grant, .request, .reset, .clk);
test t1(.grant, .request, .reset, .clk);
…
endmodule
38
Downside of Verilog Connection Conventions
Why?
Verilog module port conventions are cumbersome
Lets change the name of a port request to request 1
module arb_port ( module test (
output logic [1:0] grant, input logic [1:0] grant,
input logic [1:0] request1, output logic [1:0] request1,
input logic reset, output logic reset,
input logic clk input logic clk
); );
… …
endmodule endmodule
39
Downside of Verilog Connection Conventions
Verilog connections become especially tedious
and cumbersome for large designs
10 main bus
10
10 10
Test Generator Slave Processor
Number of ports
40
Disadvantages of Verilog Connection Conventions
• Disadvantage of Verilog Module Connections
Declarations must be duplicated in multiple
modules
Communication protocols must be duplicated in
several modules
Risk of mismatched declarations
A change in design specifications can require
modifications in multiple modules
Solution!!!!
41
SystemVerilog Interfaces
• SystemVerilog Interfaces
SystemVerilog adds a powerful new port type to Verilog, called an
interface.
An interface allows a number of signals to be grouped together
and represented as a single port
The declarations of the signals that make up the interface are
contained in a single location.
Each module that uses these signals then has a single port of the
interface type, instead of many ports with the discrete signals.
All the signals that are common between the major blocks
of the design are encapsulated in one location- the
interface declaration
42
SystemVerilog Interfaces
Using an interface to simplify connections
request [1:0]
interface arb_if (input bit clk);
logic [1:0] grant, request;
grant [1:0]
reset logic reset;
endinterface
Interface Declaration
clk
module top;
Interface bit clk;
always #5 clk=~clk;
arb_if arbif(clk);
arb a1(arbif);
test t1(arbif);
endmodule: top
Top module using a simple arbiter interface
43
SystemVerilog Interfaces
• Using an interface to simplify connections
interface arb_if (input bit clk);
logic [1:0] grant, request;
logic reset;
endinterface
Interface Declaration
44
SystemVerilog Interfaces
• Connecting interfaces and ports
Signals in an interface are referenced using the port name
<port_name>.<internal_interface_signal_name>
module top;
bit clk;
always #5 clk=~clk;
arb_if arbif(clk);
arb a1(.grant(arbif.grant),
.request(arbif.request),
.reset(arbif.reset),
.clk(arbif.clk));
test t1(arbif);
endmodule: top
Connecting the arbiter module using ports to the test module using an interface
45
SystemVerilog Interfaces: modports
• Interface modports
SystemVerilog interfaces provide a means to define different views of the
interface signal
modport is an abbreviation for module port
An interface can have any number of modport definitions
The modport declaration only defines whether the connecting module
sees a signal as an input, output or bidirectional
By specifying the port directions, modport provides access restrictions
46
SystemVerilog Interfaces
• SystemVerilog interfaces overview
SystemVerilog interfaces can contain functionality
► They are not just bundle of wires
► Discrete signal and ports for communications can be defined
in one location
► Communication protocols can be defined in the interface
► Protocol checking and other verification routines can be built
into the interface
SystemVerilog also allows multiple views of the interface to be
defined
► For instance the data_bus signal can be defined to be an
input, output or bidirectional port
SystemVerilog interfaces cannot contain design hierarchy
► cannot contain instances of modules
SystemVerilog interfaces can be used as a module port
SystemVerilog interfaces can contain modports
► modports allow modules to see interface differently
47
System verilog modports-example code
• //Defining modport in interface
• interface intf();
•
• //declaring the signals
• logic [3:0] a;
• logic [3:0] b;
• logic [6:0] c;
•
• modport driver (output a, b, input c);
•
• endinterface
•
• //driving using modort
• //run task
• task run;
• vif.driver.a = 6;
• vif.driver.b = 4;
•
• $display("Value of a = %0d, b = %0d",vif.driver.a,vif.driver.b);
• #5;
• $display("Sum of a and b, c = %0d",vif.driver.c);
• $finish;
• Endtask
• Simulator output:
• Value of a = 6, b = 4 Sum of a and b, c = 10
Example2
50
When test bench drives start signal first and then other signals, race occurs.
Stimulus Timing
The timing between the testbench and the design
should be maintained to avoid race conditions
Tsetup
start start
write write
addr 42 addr 42
5a data 5a
data
10 10
52
Stimulus Timing: Clocking Blocks
• Controlling timing of synchronous signals by using Clocking
Blocks
An interface may contain a clocking block to specify the timing of
synchronous signals relative to the clock
► Any signal in the clocking block is driven or sampled
synchronously, ensuring that the testbench interacts with the
signals at the right time
► Clocking block ensures that the testbench interacts with the signals
at the right time
► Clocking blocks are mainly used by testbenches
► An interface can contain multiple clocking blocks.
► Synthesis tools do not support clocking blocks.
53
System Verilog Testbench in Simulation
– When you are using interfaces with a clocking block:
• There is a 1-cycle delay from DUT output to testbench input
– “Virtual synchronizer” added to TB input
• No delay from testbench output to DUT input
default input #1step output #0;
clock
Design
Testbench
54
Stimulus Timing: Clocking Blocks
• Clocking Block reset
Can be declared as @(posedge clk) request[1:0]
An interface can use a clocking block to control timing grant[1:0]
► Directions are relative to program block clock
arb.sv
interface arb_if (input bit clk);
logic [1:0] grant, request;
logic reset; Declare the clocking block and
clocking cb @(posedge clk); indicate that the signals will be
output request active on positive edge of the
input grant clock
endclocking
modport TEST(clocking cb,output reset); Using the clocking block,
modport DUT(input request, reset, output grant); (request is output and grant is
input)
endinterface
Example of clocking block
arb_if arbif;
• Referencing signals in the Clocking Block arbif.cb.request<=2’b01;
if(arbif.cb.grant!=2’b01)
@arbif.cb
<my_interface.cb.signal_name>
Example of refrencing signals in the clocking blo
55
Clocking Block: Signal Synchronization
• Synchronize to active clock edge specified in clocking block
@arbif.cb; // continue on clock edge from arb_if
repeat (3) @arbif.cb; // Wait for 3 posedges
56
Clocking Block: Synchronous Signal Access
• Clocking Block signals are referenced by pre-pending the clocking
block name to the signal:
All drives must use non-blocking assignment
58
Stimulus Timing: Timing Regions
• Timing Regions
Race conditions are caused by mixing design and testbench events
during the same time slot
SystemVerilog introduces division of time slots
► Active Region: Simulation of design code in modules
► Observed Region: Assertions evaluated after design executes
► Reactive Region: Execution of testbench
► Postpone Region: Sampling signals after all design activity
clock
data
DUT arb.grant 1 2 2 3
TEST arbif.cb.grant 1 2 2 3
10 20 30 40
60
Timing Regions
• Interface Drive(no delay in driving)
TEST arbif.cb.request 3 2 2 1
DUT arbif.request
3 2 1
10 20 30 40
61
Program Block
• Program Block
• In order to avoid mixing of design and test bench events
during the same time, program block is used in SV.
In Systemverilog the test bench code is in a program block
► Program block is similar to a module and can contain code and
variables and be instantiated in other modules
► A program cannot have hierarchy such as instances of modules or
interfaces
program test (arb_if.TEST arbif);
initial begin
@arbif.cb; Continue on active clock edge
repeat (3) arbif.cb; Wait for 3 active edges
@arbif.cb.grant; Continue on any edge
@(posedge arbif.cb.grant); Continue on positive edge
@(negedge arbif.cb.grant); Continue on negative edge
wait (arbif.cb.grant==1); Wait for expression to be true
@(posedge arbif.cb.grant or negedge arbif.reset); Wait for several signals
end
endprogram
62
Program Block
• Create testbench program: test.sv
clk
reset
program test(arb_if.TB arbif); request
initial begin
// Asynch drive reset
arbif.reset <= 0; interface arb_if (input bit clk);
#15ns arbif.reset <= 1; logic grant, request, reset;
#35ns arbif.reset <= 0; clocking cb @(posedge clk);
ns! input grant;
// Synch drive request output request;
##1 arbif.cb.request <= 1; endclocking
##1 arbif.cb.request <= 0; modport TB (clocking cb,
wait (arbif.cb.grant == 1); output reset);
end endinterface: arb_if
endprogram
• Functionality:
Can be instantiated in any hierarchical location
► Typically at the top level
Interfaces and ports can be connected in the same manner as
any other module
Code goes in initial blocks & routines, no always blocks
Executes in the Reactive region
Implicit $finish when all initial blocks end in program
64
Testbench Environment – Top Block
• Create top module Implicit port connections:
The syntax .* connect ports
and signals with same
names