Can you please highlight Problem with the following code

typedef enum{MEM_WR,MEM_RD,IO_WR,IO_RD,CFG_WR,CFG_RD}packet_type;
class packet;
  rand packet_type pkt_type;
  rand bit [31:0] addr;
  rand longint [9:0] length;
  rand bit [31:0] data[];
  rand bit[7:0] tag[];
  
  constraint word_aligned{
                       addr[2:0] == 3'b0;
                        }
  constraint max_length{
                      data.size() == length;
                      }
  constraint addr_c{
    (pkt_type == MEM_WR || MEM_RD) -> (addr > 'h4FFF);
    (pkt_type == IO_WR || IO_RD) -> (addr inside { [ 'h4000 : 'h4FFF ] });
    (pkt_type == CFG_WR || CFG_RD) -> (addr inside { [ 'h0 : 'h3FFF ] });
  }
  constraint length_c{
    (pkt_type == MEM_WR || MEM_RD) -> (length inside { [1:1023] });
    (pkt_type == CFG_WR || CFG_RD || IO_WR || IO_RD ) -> (length == 1);
  }
  constraint weight_c{
    pkt_type dist { (MEM_WR && MEM_RD):= 50, (CFG_WR && CFG_RD):= 30, 
                    (IO_WR && IO_RD):= 20 };
  }
    constraint tag_val{ foreach(tag[i]) { tag[i] =i; if(tag[i] > 127) -> tag[i] = tag[i-1]-1 ;}
  }   endclass

The above code is with respect to the following problem

Create a packet class with the following,
Address - 32 bit double world aligned(32 bit aligned)
Length - 10 bit , Number of double words(32 bit) in data
Data - A collection of 32 bit data, number of items as specified by length.
type - Mem WR, Mem RD, IO WR, IO RD, CFG WR, CFG RD
Tag - values from 0 to 127

Rules

  1. Address is always 32 bit aligned. Address should be from 0 to 3FFF for CFG types and 4000 to 4FFF for IO types. Above that is always memory types
  2. Length is always 1 for CFG and IO types and can be 1-1023 or Mem types
  3. Weightage of packet type should be as below
    MEM - 50%
    CFG - 30%
    I/O - 20%
  4. Tag should be linearly incrementing for different packets. Ie 0,1,2…127 only after 127 it should go back to 0 and re start from there.
  5. Make sure that there is no repetition in data elements.

In reply to rustyguru:

Please go back and edit your post to use code tags making your code easier to read.

We don’t know what packet_type is.

Pleas explain your problem in terms of what you expect to see happend versus what is actually happening.

In reply to dave_59:

Added tag, apology for not doing it earlier.
My concern about the implementation of tag,as it says tag will be incrementing for each packet from 0 to 127 and then rollover.
However I am not sure how it will be assigned to each packet type which is enum type.
I hope my question makes sense to you.

In reply to dave_59:

I want to create a logic by which each packet should have an associated “tag value”, which can be from 0 to 127.
There can be n numbers of packets.If number of packets are more than 127 then again tag value will be repeat in the same manner like 0,1,2…127.
I tried with array however I understood that by array it can not be done,also i am not able to think of any constraint for this.
Please help me,Thank you.

In reply to rustyguru:

In reply to dave_59:
I want to create a logic by which each packet should have an associated “tag value”, which can be from 0 to 127.
There can be n numbers of packets.If number of packets are more than 127 then again tag value will be repeat in the same manner like 0,1,2…127.
I tried with array however I understood that by array it can not be done,also i am not able to think of any constraint for this.
Please help me,Thank you.

Hi,
Here we need to use post_randomize function, which will be used for storing previous id value and set present value as increment to previous one.
we need to declare static variable which will store the current tag value

static bit [7:0] tag_value;

In the post_rondomization() method, add below code.
tag = tag_value+1;
tag_value = tag;
I think this will meet your requirement.