Mid 2
Mid 2
Humility | Entrepreneurship | Teamwork & Respect for Individual | Deliver the Promise | Learning & Inner Excellence | Social Responsibility | Financial Prudence - Frugality
Unit-III
CIT 743
⚫ Nomenclature varies between the two paradigms but have the same
semantics
PP OOP
variable attribute
function method
argument message
module object
CIT 743
Advantages of OOP
⚫ OOP simulates real world objects th4us providing easy understanding
and visualization of problems as well as the designs of the solutions.
Complexity of problems is reduced, program structures become clear.
⚫ Easy modification (maintenance) of the system. This is because objects
are interacting through public interfaces allowing modifications in their
implementations without affecting the overall performances of the
system provided that modifications do not affect their functionalities.
⚫ Modularity allows scalability of the system. More functionalities of the
system can be easily added at any time if modeled in form of objects.
⚫ Reusability of code is practiced at highest level with OOP. Objects can be
reused as many times as needed and can also be used to solve similar
problems.
⚫ Security of the program is much enhanced due to limitations of object
data from being accessed by other objects.
CIT 743
⚫ Using OOP ;
o The overall program is made up of lots of different self-contained
components (objects),
o each object has a specific role in the program
CIT 743
⚫ Objects have a standard structure; must have attributes and methods
(functions).
⚫ Attributes/variables/data define s7pecifications of objects while
methods define the functionalities offered by objects. Methods are
said to explain behaviors of objects.
⚫ Methods make use of attributes of the same object to define different
behaviors of the object.
⚫ For example
Dog
⯍ ???
CIT 743
⚫ Class is an abstract of objects or can be also defined as a collection of
objects possessing similar general properties.
⚫ Classes have similar structure as t8hat of objects, the difference
between them being the degree of specification.
⚫ Attributes of objects carry specific values while those of classes are
assigned to either general or default ones.
⚫ Several objects can belong to the same class.
CIT 743
Classes are like “templates” for a particular set of objects
9
CIT 743
⚫ A class is a generic template for a set of objects with similar features.
⚫ Instance of a class = object
⚫ If class is the general (generic) re1p0resentation of an object, an
instance is its concrete representation.
⚫ Another way of distinguishing classes from objects is:
A class exists at design time, i.e. when we are writing OO code to
solve problems.
An object exists at runtime when the program that we have just
coded is running.
CIT 743
⚫ Example – address book. For each person we want to store:
Name
Age
Address
Phone number
⚫ We can write a class called Person which will represent the abstract
concept of a Person.
CIT 743
SystemVerilog is a object oriented programming and to understand the
functionality of OOP in SystemVerilog, we first need to understand several
fundamentals related to objects. These include class, method, inheritance,
encapsulation, abstraction, polymorphism etc.
Class:
It is the central point of OOP and that contains data and codes with
behavior. In SystemVerilog OOPS , everything happens within class and it
describes a set of objects with common behavior. The class definition
describes all the properties, behavior, and identity of objects present
within that class.
Object:
Objects are the basic unit of object orientation with behavior, identity. As we 12
mentioned above, these are part of a class but are not the same. An object is
expressed by the variable and methods within the objects. Again these
variables and methods are distinguished from each other as instant variables,
instant methods and class variable and class methods.
Methods:
We know that a class can define both attributes and behaviors. Again attributes
are defined by variables and behaviors are represented by methods. In other
words, methods define the abilities of an object.
Inheritance:
This is the mechanism of organizing and structuring program. Though objects are
distinguished from each other by some additional features but there are objects
that share certain things common. In object oriented programming classes can
inherit some common behavior and state from others. Inheritance in OOP allows
to define a general class and later to organize some other classes simply adding
some details with the old class definition. This saves work as the special class
13
inherits all the properties of the old general class and as a programmer you only
require the new features. This helps in a better data analysis, accurate coding and
reduces development time.
Abstraction:
The process of abstraction in SystemVerilog is used to hide certain details and
only show the essential features of the object. In other words, it deals with the
outside view of an object.
Encapsulation:
This is an important programming concept that assists in separating an object's
state from its behavior. This helps in hiding an object's data describing its state
from any further modification by external component. In SystemVerilog there are
three different terms used for hiding data constructs and these are public, private
and protected . As we know an object can associated with data with predefined
classes and in any application an object can know about the data it needs to know 14
about. So any unnecessary data are not required by an object can be hidden by
this process. It can also be termed as information hiding that prohibits outsiders in
seeing the inside of an object in which abstraction is implemented.
Polymorphism:
It describes the ability of the object in belonging to different types with
specific behavior of each type. So by using this, one object can be treated
like another and in this way it can create and define multiple level of
interface. Here the programmers need not have to know the exact type of
object in advance and this is being implemented at runtime.
15
Class
A class is the implementation of an abstract data type . It defines attributes and
methods which implement the data structure and operations of the abstract data
type, respectively. Instances of classes are called objects. Consequently, classes
define properties and behavior of sets of objects.
class simple ;
int i, j; obj_1 = new();
task printf(); obj_2 = new();
$display( i , j ); obj_1.i = 2;
// Simulation results:
endtask obj_1.j = 5;
2 5
endclass obj_2.i = 4;
4 0
obj_1.printf();
program main; obj_2.printf();
initial begin end
simple obj_1; endprogram 117
simple obj_2;
Declaration of objects:
//Example of declaration objects:
Packet pkt;
Driver driv;
If you declare pkt like this, its value will be undetermined until an object is
actually created and assigned to it using the new method. Simply declaring a
reference variable does not create an object. For that, you need to use the new
operator. You must assign an object to pkt before you use it in your code.
Otherwise, you will get a compiler error.
Creating objects:
18
//Example of creating objects:
Packet pkt = new();
Driver driv = new();
class packet ; class packet ;
int length = 0; int length = 0;
function new (int l); function new (int l);
length = l; length = l;
endfunction endfunction
endclass endclass
12
0
class packet;
byte a
Compiler gets confused on which is the class
byte b;
variable
function void data(byte a, b);
a = a;
a = b; Using this will solve the issue
endfunction this.a = a;
this.b = b;
endclass
121
class packet;
bit [31:0] addr;
bit [31:0] data;
bit write;
string pkt_type;
// simulation result
addr = 0
data = 0
write = 0
pkt_type =
12
3
class packet; // simulation result
bit [31:0] addr; addr = 10
bit [31:0] data; data = ff
bit write; write = 1
string pkt_type; pkt_type = GOOD_PKT
CIT 743
Creating class
•Virtual methods allow you to write flexible and reusable code by enabling derived classes to
customize the behavior of methods inherited from a base class.
Using One-Class Inside Another
Key Concepts:
Dynamic Allocation: Objects are created using the new() method.
Memory Management: SystemVerilog does not have automatic garbage collection, so you
must explicitly manage memory (e.g., avoid memory leaks).
In the above example, the first line declares a clocking block called cb that is to
be clocked on the positive edge of the signal clk. The second line specifies that
by default all signals in the clocking block shall use a 10ns input skew and a 2ns
output skew by default. The next line adds three output signals to the clocking
block: read, enable and addr. The fourth line adds the signal data to the clocking
block as input. Fourth line also contains negedge which overrides the skew ,so
that data is sampled on the negedge of the clk.
10
6
Skew
If an input skew is specified then the signal is sampled at skew time units
before the clock event. If output skew is specified, then output (or inout)
signals are driven skew time units after the corresponding clock event. A
skew must be a constant expression, and can be specified as a parameter.
Cycle delay:
The ## operator can be used to delay execution by a specified number of
clocking events, or clock cycles. What constitutes a cycle is determined by the
default clocking in effect of module, interface, or program.
10
9
Clocking block