SystemVerilog Tutorial for Beginners

System Verilog Tutorial for Beginners

Introduction to SystemVerilog

SystemVerilog is a hardware description and verification language that is widely used in the electronic design automation (EDA) industry. It is a powerful and versatile language that combines the capabilities of hardware description languages (HDLs) such as VHDL and Verilog with the features of programming languages such as C and C++.

SystemVerilog is used to design, verify, and document electronic systems, including digital, analog, and mixed-signal systems. It is particularly useful for modeling complex digital logic and for verifying the correctness of hardware designs through simulation and formal verification. 

Features of SystemVerilog

  1. Build a Resusable testbench with the help of the OOP concept
  2. Supports Functional coverage
  3. The random stimulus can be generated based on constraints
  4. Supports assertion-based verification

Basic Syntax and Structure

SystemVerilog has a syntax that is similar to C and C++, with some additional features for hardware modeling. Here are some basic rules for writing SystemVerilog code:

  • SystemVerilog is case-sensitive, so be careful to use the correct capitalization for keywords and variables.
  • Statements are terminated with a semicolon (;)
  • RTL Code is written inside the module block 
  • Comments can be written with either a double-slash (//) for single-line comments, or a slash and an asterisk (/*) for multi-line comments.

Here is an example of a simple SystemVerilog module:

module adder (input a, b, output sum);

assign sum = a + b;

endmodule

This module has two input ports (a, b) and one output port (sum). The assign statement is used to specify the function of the module, which is to add the inputs and output the result.

Also read: How do I start learning SystemVerilog as a fresher?

Data Types and Variables

SystemVerilog has a variety of data types that can be used to model different kinds of hardware signals. The most basic data types are integers and real numbers, which can be signed or unsigned. For example:

int a; // signed 32-bit integer

int b; // signed 32-bit integer

int c; // signed 32-bit integer

real d; // double-precision floating-point number

real e; // double-precision floating-point number

Other data types include bit vectors, arrays, and enumerated types. Bit vectors are used to represent signals with a specific width, such as a 4-bit bus or a 1-bit flag. Arrays are used to store multiple values of the same type, and enumerated types are used to define a set of named constants.

Here is an example of a bit vector and an array:

bit [7:0] data; // 8-bit vector

bit [31:0] addr; // 32-bit vector

int array1 [7:0] ; // 8-element array of signed integers

int array2 [31:0] ; // 32-element array of signed integers

enum {RED, GREEN, BLUE} color; // enumerated type with three values

Structures and Unions

SystemVerilog also supports structures and unions, which are composite data types that can be used to group multiple variables together. Structures are similar to records in other languages, and allow you to define a data type with multiple fields. Unions are similar to C unions, and allow you to define a data type that can hold multiple variables of different types, with only one variable being active at a time.

Here is an example of a structure and a union:

struct {

int a;

real b;

} s; // define a structure with two fields

union {

int a;

real b;

} u; // define a union with two fields

To access the fields of a structure or union, you can use the dot operator (.) followed by the field name. For example:

s.a = 10; // assign a value to field a of structure s

s.b = 3.14; // assign a value to field b of structure s

u.a = 10; // assign a value to field a of union u

u.b = 3.14; // assign a value to field b of union u

Class

Class datatypes are used to develop the reusable testbench. Class encloses the variables of different data types(properties) and various tasks and functions (methods).

module test;

class transaction;

int addr;

int data;

function void display();

$display(“The value of addr = %d, data =%d”);

endfunction

endclass

transaction t;

initial 

  begin

    t = new();// creates an instance for transaction class

    t.addr = 200;//accessing class property addr

    t.data = 500;//accessing class property dat

    t.display();//calling the class method display

end

endmodule

To access the class properties and methods first an instance of class should be created by calling the class constructor new().

Also read: How do I learn SystemVerilog in a week?

Control Statements and Loops

SystemVerilog supports a variety of control statements for managing the flow of your code. These include if-else statements, case statements, and loops.

Here is an example of an if-else statement:

if (a > b) begin

c = a;

end else begin

c = b;

end

This statement compares the values of variables a and b, and assigns the larger value to c.

Here is an example of a case statement:

case (color)

RED: // do something

GREEN: // do something else

BLUE: // do something else

endcase

This statement checks the value of the enumerated type color, and executes the corresponding block of code.

Here is an example of a loop:

for (int i = 0; i < 10; i = i + 1) begin

// do something

end

This loop iterates 10 times, with the variable i taking on the values 0 through 9.

Modules and Functions

SystemVerilog allows you to define modules and functions as reusable blocks of code. Modules are used to describe the behavior and interconnections of hardware components, and functions are used to define reusable pieces of code that can be called from multiple places in your design.

Here is an example of a module:

module adder (input a, b, output sum);

assign sum = a + b;

endmodule

This module has two input ports (a, b) and one output port (sum). The assign statement is used to specify the function of the module, which is to add the inputs and output the result.

Here is an example of a function:

function int add(int a, int b);

return a + b;

endfunction

This function takes two integer arguments and returns the sum of those arguments.

Simulation and Verification

One of the main benefits of SystemVerilog is its ability to simulate and verify hardware designs. SystemVerilog includes a number of built-in constructs for writing testbenches and checking the correctness of your design.

Here is an example of a simple testbench:

module tb_adder;

// Declare signals

bit [7:0] a;

bit [7:0] b;

bit [7:0] sum;

bit [7:0] expected_sum;

// Instantiate the module under test

adder dut (a, b, sum);

initial begin

// Test 1

a = 8’h01;

b = 8’h02;

expected_sum = 8’h03;

#10; // wait for 10 time units

assert (sum == expected_sum) else $display(“Test 1 failed”);

// Test 2

a = 8’hff;

b = 8’h01;

expected_sum = 8’h00;

#10; // wait for 10 time units

assert (sum == expected_sum) else $display(“Test 2 failed”);

end

endmodule

This testbench instantiates the adder module and tests it with two different input cases. The initial block contains the test cases, and the assert statement checks the output of the module against the expected result. If the assert statement fails, it will print an error message.

Also read: Verilog Interview Questions and Answers

Conclusion

SystemVerilog is a powerful and widely-used language for hardware design and verification. It combines the capabilities of hardware description languages with the features of programming languages, making it a versatile tool for modeling complex digital logic and verifying the correctness of hardware designs. Whether you are new to hardware design or an experienced designer, learning SystemVerilog can help you design and verify high-quality electronic systems.

Want to know more about System Verilog? Explore our SystemVerilog for Verification course designed and delivered by the top industry Verification expert and the best VLSI training institute.