|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



PACKET




Packet is inherited from avm_tranction. 3 methods copy,comp and convert2string definition. convert2string method returns a string which describes the transaction. comp method is for determining equality of two objects. clone method returns a handle to a newly allocated copy of this(object).


CODE:packet.sv


`ifndef PKT_CLASS
`define PKT_CLASS



//Define the enumerated types for packet payload size type
typedef enum { SMALL_P, MEDIUM_P, LARGE_P } payload_size_t ;
typedef enum { GOOD_P, BAD_P } packet_kind_t;


class packet extends avm_transaction ;



string msg;

rand payload_size_t payload_size;//Control field for the payload size
byte uid; // Unique id field to identify the packet
rand bit [7:0] len;
rand bit [7:0] da;
rand bit [7:0] sa;

rand byte data[];//Payload using Dynamic array,size is generated on the fly
rand byte parity;
static bit [7:0] mem [3:0];

constraint addr_8bit {(da == mem[3])||(da == mem[0])||(da == mem[1])||(da == mem[2]);}

// Constrain the len according the payload_size control field
constraint len_size {
(payload_size == SMALL_P ) -> len inside { [5 : 6]};
(payload_size == MEDIUM_P ) -> len inside { [7 : 8]};
(payload_size == LARGE_P ) -> len inside {[9 : 10]}; }



// Control field for GOOD/BAD
rand packet_kind_t packet_kind;

// May be assigned either a good or bad value,parity will be calculated in portrandomize
constraint parity_type {
(packet_kind == GOOD_P ) -> parity == 0;
(packet_kind == BAD_P ) -> parity != 0;}

// define clone as per avm
function packet clone();
packet t = new();
t.copy( this );
return t;
endfunction

function void do_cfg(Configuration cfg);
this.mem[0]= cfg.da_port[0];
this.mem[1]= cfg.da_port[1];
this.mem[2]= cfg.da_port[2];
this.mem[3]= cfg.da_port[3];
$swrite(msg," packet new ::%x %x %x %x",mem[0],mem[1],mem[2],mem[3]);
avm_report_message("packet",msg);
endfunction

// as per avm, define convert2string
function string convert2string;
string psdisplay;
int i;

$write(psdisplay, "da:0x%h sa:0x%h len:0x%h \n",this.da,this.sa,this.len,);

for (i = 0; i < this.len; i++) $write(psdisplay, "%s data[%0d] 0x%h \n", psdisplay,i, data[i]);
$write(psdisplay,"%s parity :0x%h \n",psdisplay,this.parity);
convert2string = psdisplay;
endfunction





function void copy(input packet to = null);


// Copying to an existing instance. Correct type?
if (!$cast(this, to))
begin
avm_report_error("packet", "Attempting to copy to a non packet instance");
return;
end



this.da = to.da;
this.sa = to.sa;
this.len = to.len;
this.data = new[to.len];
foreach(data[i])
begin
this.data[i] = to.data[i];
end

this.parity = to.parity;
return;

endfunction



//unpacking function for converting recived data to class properties
function void unpack(byte bytes[$]);
$swrite(msg," bytes size %d",bytes.size());
void'(bytes.pop_front());
da = bytes.pop_front();
sa = bytes.pop_front();
len = bytes.pop_front();


$swrite(msg,"recieved packet::da:0x%h sa:0x%h len:0x%h \n", this.da, this.sa, this.len);
avm_report_message("packet",msg);
data = new [len - 4];
parity = bytes.pop_back();
foreach (data[i]) data[i] = bytes.pop_front();
endfunction

function byte parity_cal();
integer i;
byte result ;
result = result ^ this.da;
result = result ^ this.sa;
result = result ^ this.len;
for (i = 0;i<this.len;i++)
begin
result = result ^ this.data[i];
end
return result;
endfunction

//post randomize fun to cal parity
function void post_randomize();
data = new[len];
foreach(data[i])
data[i] = $random();
parity = parity ^ parity_cal();
endfunction

function void pre_randomize();
data.delete();
endfunction

// define comp a per avm
function bit comp(input packet cmp,input packet to);
string diff;
bit compare;
compare = 1; // Assume success by default.
diff = "No differences found";

if (!$cast(cmp, to))
begin
avm_report_error("packet", "Attempting to compare to a non packet instance");
compare = 0;
diff = "Cannot compare non packets";

return compare;
end

// data types are the same, do comparison:
if (to.da != cmp.da)
begin
$swrite(diff,"Different DA values: %b != %b", to.da, cmp.da);
compare = 0;
avm_report_error("packet",diff);
return compare;
end

if (to.sa != cmp.sa)
begin
$swrite(diff,"Different SA values: %b != %b", to.sa, cmp.sa);
compare = 0;
avm_report_error("packet",diff);
return compare;
end
if (to.len != cmp.len)
begin
$swrite(diff,"Different LEN values: %b != %b", to.len, cmp.len);
compare = 0;
avm_report_error("packet",diff);
return compare;
end

foreach(data[i])
if (to.data[i] != cmp.data[i])
begin
$swrite(diff,"Different data[%0d] values: 0x%h != 0x%h",i, to.data[i], cmp.data[i]);
compare = 0;
avm_report_error("packet",diff);
return compare;
end
if (to.parity != cmp.parity)
begin
$swrite(diff,"Different PARITY values: %b != %b", to.parity, cmp.parity);
compare = 0;
avm_report_error("packet",diff);


return compare;
end
return 1;
endfunction

function int unsigned byte_pack(ref logic [7:0] bytes[],
input int unsigned offset ,
input int kind);
byte_pack = 0;
bytes = new[this.len + 4];
bytes[0] = this.da;
bytes[1] = this.sa;
bytes[2] = this.len;
foreach(data[i])
bytes[3+i] = data[i];
bytes[this.len + 3 ] = parity;
byte_pack = this.len + 4;
endfunction

endclass
`endif
Index
Avm Introduction
Dut Specification
Rtl
Top
Interface
Environment
Packet
Packet Generator
Configuration
Driver
Reciever
Scoreboard

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer