0% found this document useful (0 votes)
87 views18 pages

Q1. Write A Program To Make ID - CARD Using Struct and Class & List Down Similarities and Difference

The document discusses object oriented programming concepts in SystemVerilog like classes, inheritance, polymorphism etc. It contains questions and answers on how to write programs to demonstrate these OOP concepts by defining classes with properties and methods, extending classes to implement inheritance, using handles and objects etc.

Uploaded by

Darshan Iyer N
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)
87 views18 pages

Q1. Write A Program To Make ID - CARD Using Struct and Class & List Down Similarities and Difference

The document discusses object oriented programming concepts in SystemVerilog like classes, inheritance, polymorphism etc. It contains questions and answers on how to write programs to demonstrate these OOP concepts by defining classes with properties and methods, extending classes to implement inheritance, using handles and objects etc.

Uploaded by

Darshan Iyer N
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/ 18

Q1.

Write a program to Make ID_CARD using struct and class &


list down similarities and difference.
// Define a struct for the ID card
typedef struct {
string name;
string id_number;
string date_of_birth;
string address;
} ID_Card;

// Define a class that uses the ID card struct


class Person;
ID_Card id_card;

// Constructor
function new(string name, string id_number, string date_of_birth,
string address);
id_card.name = name;
id_card.id_number = id_number;
id_card.date_of_birth = date_of_birth;
id_card.address = address;
endfunction

// Method to display the ID card information


function void display_id_card();
$display("Name: %s", id_card.name);
$display("ID Number: %s", id_card.id_number);
$display("Date of Birth: %s", id_card.date_of_birth);
$display("Address: %s", id_card.address);
endfunction
endclass

// Testbench
module test;
initial begin
// Create a new Person object
Person p = new("Darshan", "123456", "30/08/1991", "Bangalore");

// Display the ID card information


p.display_id_card();
end
endmodule

Output
Q2. Write a class packet, declare bit [7:0] a, int b , and display
function to print the values of class properties.
• create a handle of class (use of new constructor), access the
properties and method of class packet.
class packet;
bit [7:0] a;
int b;

// Default constructor
function new();
a = 8'b0;
b = 0;
endfunction

// Display function
function void display();
$display("a = %0d, b = %0d", a, b);
endfunction
endclass

module test;
initial begin
// Create a handle of the class using the default constructor
packet p1 = new();
p1.display(); // Prints "a = 0, b = 0"
end
endmodule
Output

• write customized constructor to initialize the properties of class (use


of this pointer), print the values.
class packet;
bit [7:0] a;
int b;

// Custom constructor
function new(bit [7:0] a_init, int b_init);
this.a = a_init;
this.b = b_init;
endfunction

// Display function
function void display();
$display("a = %0d, b = %0d", a, b);
endfunction
endclass
module test;
initial begin

// Create a handle of the class using the custom constructor


packet p2 = new(8'b10101010, 42);
p2.display(); // Prints "a = 170, b = 42"
end
endmodule
Output

Q3. Write a program to do object/handle assignment and see its


behavior by printing values and handle.
class MyClass;
rand bit [7:0] a;
rand int b;

// Constructor
function new(bit [7:0] a_init, int b_init);
this.a = a_init;
this.b = b_init;
endfunction
// Display function
function void display();
$display("a = %0d, b = %0d, handle = %0d", a, b, this);
endfunction
endclass

module test;
MyClass p1,p2;
initial begin
// Create a handle of the class using the constructor
p1 = new(8'b10101010, 42);
p1.display(); // Prints "a = 170, b = 42, handle = <handle of p1>"

// Assign p1 to another handle


p2 = p1;
p2.display(); // Prints "a = 170, b = 42, handle = <handle of p2>"

// Modify p2 and display both p1 and p2


p2.a = 8'b11110000;
p2.b = 84;
p1.display(); // Prints "a = 240, b = 84, handle = <handle of p1>"
p2.display(); // Prints "a = 240, b = 84, handle = <handle of p2>"
end
endmodule
Output

Q4. Write a program to do shallow copy and see its behaviour by


printing values and handle.

class A;
int a;
endclass

class B;
int b;
A a1; // class A with handle a1
function new(); // creation of object using new function
a1= new();
endfunction
endclass

module tb;

B b1,b2;// class c with handles c1 and c2


initial begin

b1= new(); // c1 is constructed

b1.b= 10;
b1.a1.a= 30;

b2= new b1; // Shallow Copy

b1.b= 0;
b1.a1.a= 0;

$display("a of b1= %0d, b of b1=%0d", b1.a1.a, b1.b);


$display("a of b2= %0d, b of b2=%0d", b2.a1.a, b2.b);
end
endmodule
Output
Q5. Write a program to do deep copy and see its behaviour by
printing values and handle.
class A;
int a;
function A copy();
copy= new();
copy.a= this.a;
return copy;
endfunction
endclass

class B;
int b;
A a1;
function new();
a1= new();
endfunction

function B copy();
copy= new();
copy.b= this.b;
copy.a1= a1.copy;
return copy;
endfunction
endclass
class C;
int c;
B b1;
function new();
b1= new();
endfunction
function C copy();
copy= new();
copy.c= this.c;
copy.b1= b1.copy;
return copy;
endfunction
endclass

module tb;
C c1,c2;
initial begin
c1= new();

c1.c= 10;
c1.b1.b= 20;
c1.b1.a1.a= 30;

c2= c1.copy; // Shallow Copy


c1.c= 0;
c1.b1.b= 0;
c1.b1.a1.a= 0;

$display("a of c1= %0d, b of c1=%0d, c of c1=%0d", c1.b1.a1.a,


c1.b1.b, c1.c);
$display("a of c2= %0d, b of c2=%0d, c of c2=%0d", c2.b1.a1.a,
c2.b1.b, c2.c);
end
endmodule

Output

Q6. Write a program with parametrized class.


// Parameterized class
class MyClass #(parameter WIDTH = 8);
rand bit [WIDTH-1:0] data;

// Constructor
function new();
data = '0;
endfunction

// Display function
function void display();
$display("data = %0d", data);
endfunction
endclass

// Testbench
module test;
initial begin
// Create a handle of the class with a parameter
MyClass #(16) p1 = new();
p1.data = 16'b1010101010101010;
p1.display(); // Prints "data = 43690"
end
endmodule

Output

Q7. Write a program for polymorphism

class parent_class;
int p_value; // base class property

function display();//class method


$display("parent = %0d",p_value);
endfunction
endclass

class child_class extends parent_class;


int c_value;// child class property

function display();// child class method


super.display();// use "super" to access the method of parent class
$display("child = %0d",c_value);
endfunction
endclass

module polymorphism;
initial begin
parent_class p=new();// parent class with handle p is contructed
child_class c=new();// child class with handle c is contructed
c.c_value = 10;
c.p_value = 20;

p = c;//assigning child class handle to parent class handle


c.display();

end
endmodule
Output

Q8. Write a program which counts and display the total number
of objects created for the class.

class myClass;
static int count; // class properties
function new(); // class method
count++; // holds the count value of objects
endfunction
endclass

module tb;
myClass obj_array[10];// obj_array is the handle to class myClass
initial begin
foreach (obj_array[i])
obj_array[i] = new();// objects are constructed
$display("Total number of objects created: %0d", myClass::count);
end
endmodule

Output
Q9. Inheritance
a. Declare eth_pkt.sv class
i. Fields: sof(8bits), len(16 bits), count(int)
ii. Print method to print above fields
b. Declare eth_good_pkt
i. Extend it from eth_pkt
ii. Declare a new field count_good
iii. Define function print to display all class fields
1. Call super.printto print the fields of base class(eth_pkt)
2. You only need to print count_goodin addition.
3. We are learning the concept of reusability, user don’t need
to implement display of eth_pktfields again, user just needs to call
super.print
c. Define print method to print all properties without using
super.print
i. We can use this.field_name, super.field_nameto refer to
properties in parent class(eth_pkt) & child class(eth_good_pkt)

class eth_pkt;
bit [7:0] sof;
bit [15:0] len;
int count;

// Constructor
function new(bit [7:0] sof_init = 0, bit [15:0] len_init = 0, int
count_init = 0);
this.sof = sof_init;
this.len = len_init;
this.count = count_init;
endfunction

// Print method to print fields


function void print();
$display("sof = %0d, len = %0d, count = %0d", sof, len, count);
endfunction
endclass

// Declare eth_good_pkt class that extends eth_pkt


class eth_good_pkt extends eth_pkt;
int count_good;

// Constructor
function new(bit [7:0] sof_init = 0, bit [15:0] len_init = 0, int
count_init = 0, int count_good_init = 0);
super.new(sof_init, len_init, count_init);
this.count_good = count_good_init;
endfunction

// Print method to print all class fields


function void print();
super.print();
$display("count_good = %0d", count_good);
endfunction
endclass

// Testbench
module test;
initial begin
// Create a handle of the eth_good_pkt class
eth_good_pkt p = new(8'b10101010, 16'b1010101010101010, 42,
84);
p.print(); // Prints "sof = 170, len = 43690, count = 42, count_good
= 84"
end
endmodule

Output

Q10. Scope resolution operator (::)


1. :: used to access data types/methods declared inside class
2. Declare m_pktclass inside eth_pktclass
1. Declare a static field count inside m_pktclass
2. Try to access count using eth_pkt::m_pkt::count in module top
The scope resolution operator (::) in SystemVerilog is used to access
data types, methods, or static fields declared inside a class or package.
However, it’s important to note that :: cannot be used to access class
members through an instance of the class or another class.
In the above question we are trying to access count using
eth_pkt::m_pkt::count, which is not valid in SystemVerilog. We can
only use :: to access static members of a class directly, not through
another class or an instance of a class.
Here is an example on how to access count using scope resolution
operator(::)

class m_pktclass;
static int count; // Declare a static field 'count'
endclass

class eth_pktclass;
m_pktclass m_pkt; // Declare 'm_pktclass' inside 'eth_pktclass'
endclass

module top;
initial begin
m_pktclass::count = 5; // Access 'count' using 'm_pktclass::count'
$display("Count: %0d", m_pktclass::count);
end
endmodule

Output

You might also like