Systemverilog-Module3-first part-Nov 2022
Systemverilog-Module3-first part-Nov 2022
• 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
3
For loop in SV- allows declaration of variable inside ,
multiple variable initialization and multiple modifiers
System Verilog Procedural Statements
• do…while loop
– A while loop in Verilog might not execute at all
• If the test of control values is false the first time the loop is encountered in the
execution flow
5
break and continue statements
System Verilog Procedural Statements
• break and continue:
• The execution of a break statement leads to the
end of the loop.
• break shall be used in all the loop constructs
(while, do-while, foreach, for, repeat and
forever).
• Execution of continue statement leads to skip the
execution of statements followed by continue
and jump to next loop or iteration value
Example for break statement
• module break_in_loop;
•
• initial begin
• $display("-----------------------------------------------------------------");
•
• for(int i=0;i<8;i++) begin
• $display("\tValue of i=%0d",i);
• if(i == 4) begin
• $display("\tCalling break,");
• break;
• end
• end
•
• $display("-----------------------------------------------------------------");
• end
• endmodule
• when the loop value equals 4, the break is called this leads to the end of the loop.
Example for continue statement
• module continue_in_loop;
•
• initial begin
• $display("-----------------------------------------------------------------");
•
• for(int i=0;i<8;i++) begin
•
• if((i > 2) && (i < 7))begin
• $display("\t\tCalling continue,");
• continue;
• end
•
• $display("\t\tAfter Continue\t:: Value of i=%0d",i);
• end
•
• $display("-----------------------------------------------------------------");
•
• end
•
• Endmodule
• 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
Verilog module port conventions are cumbersome Why?
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 10
main bus
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
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
interface arb_if (input bit clk); module arb (abf_if.DUT arbif);
logic [1:0] grant, request; …
logic reset; endmodule
modport TEST(output request,reset, 2. arbiter module with interface using modports
input grant, clk);
modport DUT(input request,reset, clk
output grant); module test (abf_if.TEST arbif);
endinterface …
endmodule
1. Interface Declaration using modports
3. test module with interface using modports
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