TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

Specman e Interview Question: Constrained Random Packet Generation

mg
by
0
Share Facebook X LinkedIn WhatsApp Email

Packets dispatched can be of three network types: ATM, IEEE or Ethernet. The packets have a Boolean flag field which indicates whether the packet is "good" or "bad". The header of each packet is 32 bits long. When the packet is ATM, the entire header is randomly generated, but when it is IEEE or Ethernet the lowest 4 bits are always zero. 20% of the time ATM packets are generated, while the remaining time IEEE or Ethernet packets are generated uniformly. Model this scenario using the e language.

What the question is testing

This is a classic Specman interview question. It looks like a small modeling exercise, but it checks three things a verification engineer uses every day:

  • Declaring a data item: an enumerated type for the network kind, a Boolean flag and a 32-bit field.
  • Weighted random generation: making ATM appear 20% of the time and splitting the remaining 80% evenly between IEEE and Ethernet, which means 40% each.
  • Conditional constraints: forcing the lowest four header bits to zero only for IEEE and Ethernet packets, while ATM headers stay fully random.

The e language is the verification language run by the Specman tool. It is standardized as IEEE 1647, the Standard for the Functional Verification Language e, whose current revision was published in 2019.

The idea in plain words

Think of the packet as a form with three boxes: which network it belongs to, whether it is good or bad, and a 32-bit header. The generator fills in the boxes at random, but it must follow two rules. First, roll a weighted die for the network type: one in five rolls is ATM, the rest are split evenly. Second, once the type is known, if it is not ATM, the last hex digit of the header must be zero. Everything else is free.

In e, rules like these are written as keep constraints inside the struct. The tool's constraint solver then produces as many legal packets as you ask for, each one different.

A model answer

<'
type net_kind_t : [ATM, IEEE, ETHERNET];

struct packet_s {
    kind   : net_kind_t;
    good   : bool;
    header : uint (bits: 32);

    // 20% ATM, the remaining 80% split evenly: 40% IEEE, 40% Ethernet
    keep soft kind == select {
        20 : ATM;
        40 : IEEE;
        40 : ETHERNET;
    };

    // IEEE and Ethernet headers always have the lowest 4 bits at zero
    keep kind != ATM => header[3:0] == 0;
};

extend sys {
    packets : list of packet_s;
    keep packets.size() == 1000;
};
'>

Line by line

  • type net_kind_t : [ATM, IEEE, ETHERNET]; declares an enumerated type, so the kind can only take one of three values.
  • good : bool; is the good or bad flag. The question gives no distribution for it, so it is left unconstrained and the generator picks TRUE or FALSE.
  • header : uint (bits: 32); is an unsigned 32-bit field.
  • keep soft kind == select { ... }; is a weighted soft constraint. The weights are relative, so 20, 40 and 40 give 20%, 40% and 40%.
  • keep kind != ATM => header[3:0] == 0; is an implication. When the left side is true, the right side must hold; when the packet is ATM, it places no restriction, so the whole header stays random.
  • The extend sys block creates a list of 1,000 packets so that the distribution can be seen in practice.

Deep dive: the numbers and details

This part holds the statistics, the coverage code, a SystemVerilog comparison and the follow-up questions interviewers ask. Skip it if the model answer is all you need.

Why the distribution is a soft constraint

In e, weighted select is used with keep soft. A soft constraint expresses a default that a test writer can override. That is exactly what you want here: the 20/40/40 mix is the normal traffic profile, but a directed test that needs only ATM packets can add keep kind == ATM; in an extension and the soft weighting gives way without a contradiction. If the distribution were a hard constraint, that test would fail to generate.

The header rule, on the other hand, is a property of the protocol, so it is a hard constraint. No test should ever produce an IEEE packet with a non-zero low nibble unless it is deliberately injecting an error, and that should be done through a separate, explicit error field.

What to expect from 1,000 packets

The number of ATM packets in a run of n packets follows a binomial distribution with p = 0.2. Its mean is n × p and its standard deviation is √(n × p × (1 − p)).

KindProbabilityExpected count (n = 1,000)Standard deviationTypical range (±3 sigma)
ATM0.2200√160, about 12.6about 162 to 238
IEEE0.4400√240, about 15.5about 354 to 446
Ethernet0.4400√240, about 15.5about 354 to 446

One subtle point: an ATM header is fully random, so its low nibble will also be zero by chance in about 1 of 16 ATM packets (about 12 or 13 of the expected 200). A checker that infers the packet kind from the header alone would therefore be wrong. The kind field, not the header bits, is the ground truth.

Adding functional coverage

An interviewer will often ask how you would prove the generator does what you intended. Coverage answers that:

<'
extend packet_s {
    event generated;
    post_generate() is also {
        emit generated;
    };
    cover generated is {
        item kind;
        item good;
        item low_nibble : uint (bits: 4) = header[3:0];
        cross kind, good;
    };
};
'>

After a run, the kind item should show roughly 20/40/40, the cross of kind and good should have all six bins hit, and the low_nibble item should show all 16 values for ATM but only zero for IEEE and Ethernet.

The same model in SystemVerilog

Many teams now use SystemVerilog and UVM instead of e, so it helps to show the equivalent:

typedef enum {ATM, IEEE, ETHERNET} net_kind_e;

class packet;
  rand net_kind_e  kind;
  rand bit         good;
  rand bit [31:0]  header;

  constraint c_kind  { kind dist { ATM := 20, IEEE := 40, ETHERNET := 40 }; }
  constraint c_hdr   { kind != ATM -> header[3:0] == 4'h0; }
  constraint c_order { solve kind before header; }
endclass

The dist operator plays the role of e's weighted select, and -> is the implication. The solve kind before header line makes the intent explicit: pick the kind first, then the header that fits it.

Follow-up questions to expect

  • How would you inject bad packets? Use the good flag to drive an error in the payload or checksum, and make the scoreboard expect the device to drop or flag those packets.
  • How would you make a test with only Ethernet packets? Extend the struct in the test file with keep kind == ETHERNET;, which overrides the soft weighting.
  • What if the weights had to change at run time? Make the weights fields of a configuration struct and refer to them in the select.
  • What is the difference between hard and soft constraints? Hard constraints must always hold and a contradiction is an error; soft constraints are dropped when they conflict with a hard one.

Common mistakes

  • Writing the weights as 20, 80 and splitting the 80 in procedural code, which hides the distribution from the solver and from coverage.
  • Constraining header[3:0] == 0 for all packets, which wrongly restricts ATM headers.
  • Forgetting that the select weights are relative, not percentages that must add to 100.
  • Judging the distribution from a run of 20 packets; with small samples the counts vary widely.

Further reading

Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!