|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 uvm_sequence_item. uvm_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 UVM 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 uvm_sequence_item.

   class Packet extends uvm_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.

   ///// method to calculate the fcs /////
    virtual function byte cal_fcs;
       return da ^ sa ^ length ^ data.xor() ^ fcs;
    endfunction : cal_fcs

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

    `uvm_object_utils_begin(Packet)
       `uvm_field_int(da, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_int(sa, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_int(length, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_array_int(data, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_int(fcs, UVM_ALL_ON|UVM_NOPACK)
    `uvm_object_utils_end


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

    function void do_pack(uvm_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(uvm_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

Packet class source code

`ifndef GUARD_PACKET
`define GUARD_PACKET

 `include "uvm.svh"
 import uvm_pkg::*;

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

class Packet extends uvm_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;
       return da ^ sa ^ length ^ data.xor() ^ fcs;
    endfunction : cal_fcs
    
    `uvm_object_utils_begin(Packet)
       `uvm_field_int(da, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_int(sa, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_int(length, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_array_int(data, UVM_ALL_ON|UVM_NOPACK)
       `uvm_field_int(fcs, UVM_ALL_ON|UVM_NOPACK)
    `uvm_object_utils_end
    
    function void do_pack(uvm_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(uvm_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();
          uvm_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");

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();
          uvm_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


Download the Source Code

uvm_switch_4.tar
Browse the code in uvm_switch_4.tar

Command to run the simulation

VCS Users : make vcs
Questa Users: make questa

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