Q1. Write A Program To Make ID - CARD Using Struct and Class & List Down Similarities and Difference
Q1. Write A Program To Make ID - CARD Using Struct and Class & List Down Similarities and Difference
// 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
// Testbench
module test;
initial begin
// Create a new Person object
Person p = new("Darshan", "123456", "30/08/1991", "Bangalore");
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
// 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
// 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>"
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;
b1.b= 10;
b1.a1.a= 30;
b1.b= 0;
b1.a1.a= 0;
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;
Output
// 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
class parent_class;
int p_value; // base class property
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;
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
// 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
// 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
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