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 typedefenum { SMALL_P, MEDIUM_P, LARGE_P } payload_size_t ; typedefenum { 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 randbit [7:0] len; randbit [7:0] da; randbit [7:0] sa;
randbyte data[];//Payload using Dynamic array,size is generated on the fly randbyte parity; staticbit [7:0] mem [3:0];
// 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
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
functionvoid 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 functionvoid 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();
functionbyte 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 functionvoid post_randomize();
data = new[len]; foreach(data[i])
data[i] = $random();
parity = parity ^ parity_cal(); endfunction
// define comp a per avm functionbit 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);