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


Tutorials



PHASE 4 PACKET




In this Phase, we will develop Transaction as per the verification plan. We will define required methods and constraints. We will also develop a small logic to test our implementation of this class.



Packet



We will write the packet class in Packet.sv file. Packet class variables and constraints have been derived from stimulus generation plan.

One way to model Packet is by extending ovm_sequence_item. ovm_sequence_item provides basic functionality for sequence items and sequences to operate in a sequence mechanism. Packet class should be able to generate all possible packet types randomly. To define copy, compare, record, print and sprint methods, we will use OVM field macros. For packing and Unpacking, we will define the logic and not use the field macros.


Revisit Stimulus Generation Plan
1) Packet DA: Generate packet DA with the configured address.
2) Payload length: generate payload length ranging from 2 to 255.
3) Generate good and bad FCS.





1) Define enumerated type data for fcs.


typedef enum { GOOD_FCS, BAD_FCS } fcs_kind_t;


2) Define transaction by extending ovm_sequence_item.


class Packet extends ovm_sequence_item;

endclass : Packet


3) Define all the fields as rand variables.


rand fcs_kind_t fcs_kind;

rand bit [7:0] length;
rand bit [7:0] da;
rand bit [7:0] sa;
rand bit [7:0] data[];
rand byte fcs;



4) Define constraints to constraint payload size of data.


constraint payload_size_c { data.size inside { [2 : 255]};}

constraint length_c { length == data.size; }



5) Define the constructor method.


function new(string name = "");
super.new(name);
endfunction : new


6) In post_randomize() , define the fcs value based on fcs_kind.


function void post_randomize();
if(fcs_kind == GOOD_FCS)
fcs = 8'b0;
else
fcs = 8'b1;
fcs = cal_fcs();
endfunction : post_randomize


7) Define cal_fcs() method which computes the fcs value.


virtual function byte cal_fcs;
integer i;
byte result ;
result = 0;
result = result ^ da;
result = result ^ sa;
result = result ^ length;
for (i = 0;i< data.size;i++)
result = result ^ data[i];
result = fcs ^ result;
return result;
endfunction : cal_fcs


8) Using ovm_field_* macros, define transaction required method.
We will define packing and unpacking methods manually, so use OVM_NOPACK for excluding atomic creation of packing and un packing method.


`ovm_object_utils_begin(Packet)
`ovm_field_int(da, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_int(sa, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_int(length, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_array_int(data, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_int(fcs, OVM_ALL_ON|OVM_NOPACK)
`ovm_object_utils_end



9) Define do_pack() method which does the packing operation.


function void do_pack(ovm_packer packer);
super.do_pack(packer);
packer.pack_field_int(da,$bits(da));
packer.pack_field_int(sa,$bits(sa));
packer.pack_field_int(length,$bits(length));
foreach(data[i])
packer.pack_field_int(data[i],8);
packer.pack_field_int(fcs,$bits(fcs));
endfunction : do_pack


10) Define do_unpack() method which does the unpacking operation.



function void do_unpack(ovm_packer packer);
int sz;
super.do_pack(packer);

da = packer.unpack_field_int($bits(da));
sa = packer.unpack_field_int($bits(sa));
length = packer.unpack_field_int($bits(length));

data.delete();
data = new[length];
foreach(data[i])
data[i] = packer.unpack_field_int(8);
fcs = packer.unpack_field_int($bits(fcs));
endfunction : do_unpack

(S) Packet class source code

`ifndef GUARD_PACKET
`define GUARD_PACKET

`include "ovm.svh"
import ovm_pkg::*;

//Define the enumerated types for packet types
typedef enum { GOOD_FCS, BAD_FCS } fcs_kind_t;

class Packet extends ovm_sequence_item;

rand fcs_kind_t fcs_kind;

rand bit [7:0] length;
rand bit [7:0] da;
rand bit [7:0] sa;
rand bit [7:0] data[];
rand byte fcs;

constraint payload_size_c { data.size inside { [1 : 6]};}

constraint length_c { length == data.size; }

function new(string name = "");
super.new(name);
endfunction : new

function void post_randomize();
if(fcs_kind == GOOD_FCS)
fcs = 8'b0;
else
fcs = 8'b1;
fcs = cal_fcs();
endfunction : post_randomize

///// method to calculate the fcs /////
virtual function byte cal_fcs;
integer i;
byte result ;
result = 0;
result = result ^ da;
result = result ^ sa;
result = result ^ length;
for (i = 0;i< data.size;i++)
result = result ^ data[i];
result = fcs ^ result;
return result;
endfunction : cal_fcs

`ovm_object_utils_begin(Packet)
`ovm_field_int(da, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_int(sa, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_int(length, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_array_int(data, OVM_ALL_ON|OVM_NOPACK)
`ovm_field_int(fcs, OVM_ALL_ON|OVM_NOPACK)
`ovm_object_utils_end

function void do_pack(ovm_packer packer);
super.do_pack(packer);
packer.pack_field_int(da,$bits(da));
packer.pack_field_int(sa,$bits(sa));
packer.pack_field_int(length,$bits(length));
foreach(data[i])
packer.pack_field_int(data[i],8);
packer.pack_field_int(fcs,$bits(fcs));
endfunction : do_pack

function void do_unpack(ovm_packer packer);
int sz;
super.do_pack(packer);

da = packer.unpack_field_int($bits(da));
sa = packer.unpack_field_int($bits(sa));
length = packer.unpack_field_int($bits(length));

data.delete();
data = new[length];
foreach(data[i])
data[i] = packer.unpack_field_int(8);
fcs = packer.unpack_field_int($bits(fcs));
endfunction : do_unpack

endclass : Packet


Test The Transaction Implementation




Now we will write a small logic to test our packet implantation. This module is not used in normal verification.

Define a module and take the instance of packet class. Randomize the packet and call the print method to analyze the generation. Then pack the packet in to bytes and then unpack bytes and then call compare method to check all the method implementation.





1) Declare Packet objects and dynamic arrays.


Packet pkt1 = new("pkt1");
Packet pkt2 = new("pkt2");
byte unsigned pkdbytes[];


2) In a initial block, randomize the packet, pack the packet in to pkdbytes and then unpack it and compare the packets.


if(pkt1.randomize)
begin
$display(" Randomization Sucessesfull.");
pkt1.print();
ovm_default_packer.use_metadata = 1;
void'(pkt1.pack_bytes(pkdbytes));
$display("Size of pkd bits %d",pkdbytes.size());
pkt2.unpack_bytes(pkdbytes);
pkt2.print();
if(pkt2.compare(pkt1))
$display(" Packing,Unpacking and compare worked");
else
$display(" *** Something went wrong in Packing or Unpacking or compare *** \n \n");

(S)Logic to test the transaction implementation
module test;

Packet pkt1 = new("pkt1");
Packet pkt2 = new("pkt2");
byte unsigned pkdbytes[];

initial
repeat(10)
if(pkt1.randomize)
begin
$display(" Randomization Successesfull.");
pkt1.print();
ovm_default_packer.use_metadata = 1;
void'(pkt1.pack_bytes(pkdbytes));
$display("Size of pkd bits %d",pkdbytes.size());
pkt2.unpack_bytes(pkdbytes);
pkt2.print();
if(pkt2.compare(pkt1))
$display(" Packing,Unpacking and compare worked");
else
$display(" *** Something went wrong in Packing or Unpacking or compare *** \n \n");
end
else
$display(" *** Randomization Failed ***");

endmodule


(S)Download the Source Code


ovm_switch_4.tar
Browse the code in ovm_switch_4.tar


(S)Command to run the simulation


vcs -sverilog +incdir+$OVM_HOME/src $OVM_HOME/src/ovm_pkg.sv -R Packet.sv
qverilog +incdir+$OVM_HOME/src $OVM_HOME/src/ovm_pkg.sv -R Packet.sv


(S)Log report after simulation

Randomization Sucessesfull.
----------------------------------------------------------------------
Name Type Size Value
----------------------------------------------------------------------
pkt1 Packet - pkt1@3
--da integral 8 'ha5
--sa integral 8 'ha1
--length integral 8 'h6
--data da(integral) 6 -
----[0] integral 8 'h58
----[1] integral 8 'h60
----[2] integral 8 'h34
----[3] integral 8 'hdd
----[4] integral 8 'h9
----[5] integral 8 'haf
--fcs integral 8 'h75
----------------------------------------------------------------------
Size of pkd bits 10
----------------------------------------------------------------------
Name Type Size Value
----------------------------------------------------------------------
pkt2 Packet - pkt2@5
--da integral 8 'ha5
--sa integral 8 'ha1
--length integral 8 'h6
--data da(integral) 6 -
----[0] integral 8 'h58
----[1] integral 8 'h60
----[2] integral 8 'h34
----[3] integral 8 'hdd
----[4] integral 8 'h9
----[5] integral 8 'haf
--fcs integral 8 'h75
----------------------------------------------------------------------
Packing,Unpacking and compare worked

....
....
....
....



Index
Introduction
Specification
Verification Plan
Phase 1 Top
Phase 2 Configuration
Phase 3 Environment N Testcase
Phase 4 Packet
Phase 5 Sequencer N Sequence
Phase 6 Driver
Phase 7 Receiver
Phase 8 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