Friday, March 02, 2012

Classes examples

Classes - Examples

Example for Classes.

(a) Ethernet

(i) Example

module Encapsulate;
class Transaction;
logic [5:0] source_address,CRC,data[10];
function new(logic [31:0] a = 8, d = 9);
foreach (data[CRC])
begin
data[CRC] = d;
source_address = a;
$display("Data[CRC] = %h ", data[CRC]);
$display("Source address = %d\n",source_address);
end
endfunction
endclass

initial
begin
Transaction ethernet;
ethernet = new(10); // data uses default of 5
end
endmodule

Output in VCS

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

Data[CRC] = 09
Source address = 10

(ii) Example

program static_example;
class data_packet;
static integer Data = 32'hff;// Static variable declaration
function new();
Data--;
endfunction
endclass
initial
begin
data_packet packet_one = new;
data_packet packet_two = new;
$display("%d--Value of static data packet before decrement", packet_one.Data);
$display("\n%d--Value of static data packet after decrement", packet_two.Data);
end
endprogram

Output in VCS

253--Value of static data packet before decrement
253--Value of static data packet after decrement




(b) Sonet

(i) Example

module static_class;
class Sonet_static;
static bit count = 2'b11;
bit indicator ;
function new();
indicator = count++;
endfunction
endclass
Sonet_static sta1,sta2;//objects of class
initial
begin
sta1 = new();
sta2 = new();
$display("First indicator = %d, count = %d", sta1.indicator, sta1.count);
$display("Second indicator = %d, count = %d", sta2.indicator, sta2.count);
end
endmodule

Output in VCS

First indicator = 1, count = 1
Second indicator = 0, count = 1


No comments:

Post a Comment

Popular Posts