System Verilog Singleton Example

Purpose of a singleton:
To have a class which has only one instance(object)
All the objects of this class refer to same global object
that is created only once
This design pattern provides a mechanism for providing
namespaces to global variables

Download here. singleton-design-pattern
C++ example for singleton design pattern: click here

System Verilog example for singleton design pattern

// Filename  : singleton.sv
// Author    : A.G.Raja
// License   : GPL
// Website   : https://agraja.wordpress.com

module tb;

class Singleton;
// object that is created only once
extern static Singleton single;
// constructor that is called only once
extern function new;
// method to get object
extern function Singleton get_instance;
// data to check the functionality
int data;
endclass

initial Singleton::single = null;

function Singleton::new;
data = 14;
endfunction

function Singleton Singleton::get_instance;
// call the constructor for the first time
// if the object has been created already
// return reference to the object already created
if(single==null) single = new;
// return the reference to the object
return single;
endfunction

Singleton s, t;

initial begin
// don’t use s= new;
// since it will create new instances
// instead use the get_instance method
s = s.get_instance();
t = t.get_instance();
$display(“initial value of s.data  = “,s.data);
s.data = 25;
$display(“modified value of s.data = “,s.data);
$display(“value of t.data          = “,t.data);
end
endmodule

Result:
initial value of s->data  = 14
modified value of s->data = 25
value of t->data          = 25
Download here. singleton-design-pattern

C++ Singleton Example

Download here.  singleton-design-pattern

Purpose of a singleton:
To have a class which has only one instance(object)
All the objects of this class refer to same global object
that is created only once
This design pattern provides a mechanism for providing
namespaces to global variables

C++ example for singleton design pattern

// Filename  : singleton.cpp
// Author    : A.G.Raja
// License   : GPL
// Website   : https://agraja.wordpress.com

#include <iostream>

using namespace std;

class Singleton {
// constructor that is called only once
Singleton();
// object that is created only once
static Singleton *single;
public:
// method to get object
Singleton* get_instance();
// data to check the functionality
int data;
};

Singleton* Singleton::single = NULL;

Singleton* Singleton::get_instance() {
// call the constructor for the first time
// if the object has been created already
// return reference to the object already created
if(single==NULL) single = new Singleton();
return single;
}

Singleton::Singleton() {
data = 14;
}

int main() {
Singleton *s, *t ;
s = s->get_instance();
cout<<“initial value of s->data  = “<<s->data<<endl;
s->data = 25;
cout<<“modified value of s->data = “<<s->data<<endl;
t = t->get_instance();
cout<<“value of t->data          = “<<t->data<<endl;
}
// end of file singleton.cpp

Result:
initial value of s->data  = 14
modified value of s->data = 25
value of t->data          = 25

Download here.  singleton-design-pattern

System Verilog Virtual Interface

Download here. system_verilog_virtual_interface

// Filename    : virtual_interface.sv
// Author      : A.G.Raja
// License     : GPL
// Website     : https://agraja.wordpress.com

interface bus(input wire clk);
int address;
int data;

modport virtual_interface(input clk);
endinterface

module virtual_interface;
bit clk;
bus BUS(clk);
class monitor;
virtual bus VBUS;
task disp;
$display(VBUS.address);
$display(VBUS.data);
endtask
endclass
monitor m;

initial begin
m = new;
m.VBUS = BUS;
BUS.address = 100;
BUS.data    = 1234;
m.disp;
end
endmodule

// Running simulation in ncverilog
// ncverilog +sv virtual_interface.sv

// Running simulation in modelsim/questasim
// vlib work
// vlog -sv virtual_interface.sv
// vsim -c virtual_interface
// run

Download here. system_verilog_virtual_interface

Transaction Level Modeling in System Verilog

Download here. transactionlevelmodeling_systemverilog

// Filename    : tlm.sv
// Author      : A.G.Raja
// License     : GPL
// Website     : https://agraja.wordpress.com

module producer_consumer;

virtual class put_if;

// Abstract Class whose methods are not yet implemented
virtual task put(int val);
endtask
endclass

class producer;
put_if put_port;
task run;
int i;
for(i=0;i<10;i++) put_port.put(i);
endtask
endclass

class consumer extends put_if;

// methods of put_if class are over-ridden

// and defined here
task put(int val);
$display(“Consumer receiving “,val);
endtask
endclass

producer p;
consumer c;

initial begin
p = new;
c = new;
p.put_port = c;
p.run;
end
endmodule

// Running simulation in ncverilog

ncverilog +sv tlm.sv

// Running simulation in modelsim/questasim

vlib work

vlog -sv tlm.sv

vsim -c producer_consumer

run

Download here. transactionlevelmodeling_systemverilog

C++ String Getline Class

Download here. getline

Features of the below string getline class

1) Can be used like getline function for streams

2) Can have multiple delimiters as opposed to the getline function for streams

3) Delimiter need not be single char. It can be a string

4) Pointer to current position in the string is automatically incremented

similar to that in stringstream

5) It is possible to rewind the pointer position to zero using the rewind() method.

6) It is also possible to get single char at the current position of pointer from the string

7) It is possible to get single char at current pointer position if it is one of the

characters of the list.  This is an optional argument to the get method

8) stream getline feature can be realized by calling the getline method of this

class without passing any arguments. No arguments to getline method defaults to

newline char as delimiter

// File : Getline.hh
// Author : A.G.Raja
// License : GPL
// Website : https://agraja.wordpress.com

#include <iostream>

using namespace std;

class Getline {
string text, buffer;
int pos,p;
public:
int str(string txt) {
pos = 0;
this->text = txt;
}
string getline(string delim = “\n”, int delim_type = 1);
char get(string delim = “”);
int unget(int decr = 1) {
pos -= decr;
}
int end() {
if(pos>=text.size()) return 1;
else return 0;
}
int rewind() {
pos = 0;
}
};

string Getline::getline(string delim, int delim_type) {
p = delim_type ? text.find(delim,pos)
: text.find_first_of(delim,pos);
if(p!=-1) {
buffer = text.substr(pos,p-pos);
pos = delim_type ? p+delim.length() : p +1;
} else {
buffer = text.substr(pos);
pos += buffer.length();
} return buffer;
}

char Getline::get(string delim) {
if(delim.empty() and pos<text.size()) return text[pos++];
if(delim.find(text[pos])!=-1) {
pos++;
return text[pos-1];
}
return 0; // must
}

// End of File Getline.hh

// File : test_Getline.cpp
// Author : A.G.Raja
// License : GPL
// Website : https://agraja.wordpress.com

#include <iostream>
#include “Getline.hh”

using namespace std;

int main() {
Getline g;
g.str(“Hi, my name is Raja\nI’m an Engineer”);
// Get the substring terminated by “y na”
cout<<g.getline(“y na”)<<endl;
// Result: “Hi, m”

// Get substring terminated by first of “sji”
cout<<g.getline(“sji”,0)<<endl;

// Get line terminated by ‘\n’
cout<<g.getline()<<endl;
// Result: “s Raja”

// Get single char at current position of pointer
cout<<g.get()<<endl;
// Result: “I”

// Get a char at current position of pointer
// if it is in the list “_$%’#”
cout<<g.get(“_$%’#”)<<endl;
// Result: “‘”

// Unget single char, unget 4 chars
g.unget(); g.unget(4);
}

Download here. getline

C++ Constructor Destructor

// constructor_destructor.cpp
// Author: A.G.Raja
// Website: agraja.wordpress.com
// Licence: GPL

#include <iostream>
using namespace std;
class Employee{
char *name;
int age;
double salary;
public: Employee(); //Constructor
void print();
void setval(char *name, int age, double salary);
char* getname() { return name; }
int getage() { return age; }
double getsalary() { return salary; }
~Employee(); //Destructor
};

Employee::Employee()
{
cout<<“Object Created”<<endl;
name=”fresher”;
age=23;
salary=17500;
}
void Employee::print()
{
cout<< name<<“\t”<<age<<“\t”<<salary <<endl;
}
void Employee::setval(char *n, int a, double s)
{
name=n;
age=a;
salary=s;
}

Employee::~Employee()
{
cout<<“Object Destroyed”<<endl;
}

int main()
{
char *NAME;
int AGE;
double SALARY;
//Employee E257 = {“raja”,25,23519.78};
Employee E257;
cout<<“Print Default Values”<<endl;
E257.print(); // Default Values
E257.setval(“raja”,25,23768);
cout<<“Print New Values”<<endl;
E257.print(); // New values
NAME=E257.getname();
AGE=E257.getage();
SALARY=E257.getsalary();
cout<<NAME<<“\t”<<AGE<<“\t”<<SALARY<<endl;
}
// g++ constructor_destructor.cpp

Download here.

constructor.doc

C++ struct vs class

This is an example to show the similarity between a struct and a class

  #include <iostream>using namespace std;

int main()

{

struct employee

{char *name;

int age;

double salary;

};

employee e257 = {”raja“,25,23519.78};

cout<< e257.name<<”\t”<<e257.age

<<”\t”<<e257.salary <<endl;

class Employee { public:      char *name;
     int age;double salary; };

Employee E257 = {”raja”,25,23519.78};

cout<< E257.name<<”\t”<<E257.age

<<”\t”<<E257.salary <<endl;

} 
Note:
1) All members of a struct are public by default 
2) All members of a class are private by default 
3) For a class member to be public, it has be 
declared public with the keyword "public".  
Advantage of using classes 
1) Member variables are private and protected. 
2) Constructors functions(declared public) can be 
used  to initialize and modify values of that private variables. 
See this page for Constructors:  Constructors & Destructors </pre
 Download here.struct.doc