0% found this document useful (0 votes)
11 views3 pages

Inheritance Base Class

The document illustrates Object-Oriented Programming in SystemVerilog through class inheritance, demonstrating an 'add' class for addition and a 'mul' class that extends it for multiplication. It includes code snippets for class definitions, a testbench, and compilation instructions. The expected output confirms the results of both addition and multiplication operations.

Uploaded by

126004033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Inheritance Base Class

The document illustrates Object-Oriented Programming in SystemVerilog through class inheritance, demonstrating an 'add' class for addition and a 'mul' class that extends it for multiplication. It includes code snippets for class definitions, a testbench, and compilation instructions. The expected output confirms the results of both addition and multiplication operations.

Uploaded by

126004033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Object-Oriented Programming in SystemVerilog

Introduction
This example demonstrates Object-Oriented Programming (OOP) in SystemVerilog using class in-
heritance. The ‘add‘ class performs addition, and the ‘mul‘ class extends it to include multiplication.

SystemVerilog Code
Class Definition and Inheritance
class add ;

bit [3:0] a ;
bit [3:0] b ;
bit [4:0] c ;

function new ( bit [3:0] a , bit [3:0] b ) ;


this . a = a ;
this . b = b ;
this . c = this . a + this . b ; // Ensure correct init ializati on
endfunction

endclass

// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /

class mul extends add ;

bit [7:0] r ; // Mul tiplicat ion result

function new ( bit [3:0] a , bit [3:0] b ) ;


super . new (a , b ) ; // Call parent constructor correctly
this . r = a * b ; // Multipl ication using inherited variables
endfunction

endclass

Testbench
module tb ;

mul t ;
bit [4:0] a ; // Matching c ’ s width
bit [7:0] m ; // Matching r ’ s width

initial begin
t = new (4 ’ b0001 , 4 ’ b0010 ) ;
a = t . c ; // Accessing superclass variable
m = t . r ; // Accessing subclass variable
$ display ( " Addition result : %0 d , Mul tiplicat ion result : %0 d " , a , m ) ;
end
endmodule

Expected Output
Addition r e s u l t : 3 , M u l t i p l i c a t i o n r e s u l t : 2

1
Explanation
ˆ The ‘add‘ class contains variables ‘a‘, ‘b‘, and ‘c‘, where ‘c = a + b‘.

ˆ The ‘mul‘ class extends ‘add‘ and introduces ‘r = a * b‘.

ˆ The testbench instantiates an object of ‘mul‘, calls the constructor, and prints the results.

Package File: add pkg.sv


package add_pkg ;

class add ;

bit [3:0] a ;
bit [3:0] b ;
bit [4:0] c ;

function new ( bit [3:0] a , bit [3:0] b ) ;


this . a = a ;
this . b = b ;
this . c = this . a + this . b ; // Ensure correct init ializati on
endfunction

endclass

endpackage

Main File: tb.sv


‘include " add_pkg . sv " // Include the package file
import add_pkg ::*; // Import the package

class mul extends add ;

bit [7:0] r ; // Mul tiplicat ion result

function new ( bit [3:0] a , bit [3:0] b ) ;


super . new (a , b ) ; // Call parent constructor correctly
this . r = a * b ; // Multipl ication using inherited variables
endfunction

endclass

// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /

module tb ;

mul t ;
bit [4:0] a ; // Matching c ’ s width
bit [7:0] m ; // Matching r ’ s width

initial begin
t = new (4 ’ b0001 , 4 ’ b0010 ) ;
a = t . c ; // Accessing superclass variable
m = t . r ; // Accessing subclass variable
$ display ( " Addition result : %0 d , Mul tiplicat ion result : %0 d " , a , m ) ;
end
endmodule

2
Compilation Instructions
To compile and run the testbench, follow this sequence:
v l o g add pkg . sv // Compile package f i r s t
v l o g tb . sv // Compile t h e main f i l e
vsim tb // Run s i m u l a t i o n

Expected Output
Addition r e s u l t : 3 , M u l t i p l i c a t i o n r e s u l t : 2

Explanation
ˆ The ’add’ class is encapsulated in ’add pkg.sv’ for modularity.

ˆ The ‘mul‘ class extends ‘add‘ and introduces ‘r = a * b‘.

ˆ The testbench instantiates ‘mul‘, calls the constructor, and prints the results.

You might also like