Friday, March 02, 2012

Tasks & Functions - Examples

Tasks & Functions - Examples

Examples for Function

(a) Ethernet

(i) Example for Function

// CRC checking calculations: Ethernet packet// Example for class and function call

module CRC1;
class crc;
rand bit [15:0]length;
rand bit [47:0]addr_s; // source address
rand bit[47:0]addr_d; // Destination address
rand byte fcs[4:0]; // frame check sequence
rand bit preamble[63:0];
rand byte data[10:0];
function integer fcs_cal(bit a_fcs_cal);
integer k;
a_fcs_cal = a_fcs_cal ^ addr_s;
a_fcs_cal = a_fcs_cal ^ addr_d;
a_fcs_cal = a_fcs_cal ^ length;
$display("The value of length, source address, destination address %h %h %h",length,addr_s,addr_d);
$display("The value of length crc = %h, source address crc = %h,destination address crc = %h",a_fcs_cal,a_fcs_cal,a_fcs_cal);
for (k= 0;k < $size(data); k++)
begin
a_fcs_cal = a_fcs_cal ^ data[k];
$display("The value of data crc %h ",data[k]);
$display("The value of CRC = %h ",a_fcs_cal);
end
return a_fcs_cal;
endfunction
endclass
initial
begin
crc cal;// object of class is defined
cal = new();
cal.randomize();
$display("***********Simulation started**************");
$display("Calculation = %h",cal.fcs_cal(9));
$display("\n***********Simulation ended**************");
end
endmodule

Output in VCS

***********Simulation started**************

The value of length, source address, destination address 1e213 33fd69262e70 b2973d5d03e2
The value of length crc = 0, source address crc = 0,destination address crc = 0
The value of data crc 0c
The value of CRC = 0
The value of data crc 8d
The value of CRC = 1
The value of data crc c8
The value of CRC = 1
The value of data crc 1f
The value of CRC = 0
The value of data crc ed
The value of CRC = 1
The value of data crc 23
The value of CRC = 0
The value of data crc 99
The value of CRC = 1
The value of data crc 42
The value of CRC = 1
The value of data crc 8f
The value of CRC = 0
The value of data crc f6
The value of CRC = 0
The value of data crc 15
The value of CRC = 1
Calculation = 00000001

***********Simulation ended**************




(b) Sonet

(i) Example for Function

//Example of Generating the Parity in Sonet Frames using Function. For even number 1's, parity is 0 else 1

module sonet_Parity();
reg [7:0] data;
reg parity_out;
integer i;
function parity;
input [47:0] data;
integer i;
begin
parity = 0;
for (i = 0; i < 47; i = i + 1)
begin
parity = parity ^ data[i];
end
end
endfunction
initial
begin
parity_out = 0;
data = 0;
for (byte i = 0; i < 8; i = i + 1)
begin
#5 data = i;
parity_out = parity (data);
$display ("Data = %b, Parity = %b", data, parity_out);
end
#10 $finish;
end
endmodule

Output in VCS

Data = 00000000, Parity = 0
Data = 00000001, Parity = 1
Data = 00000010, Parity = 1
Data = 00000011, Parity = 0
Data = 00000100, Parity = 1
Data = 00000101, Parity = 0
Data = 00000110, Parity = 0
Data = 00000111, Parity = 1


No comments:

Post a Comment

Popular Posts