2

你能举一个大而复杂的 SystemVerilog 约束的例子吗?越大越好,最好是现实的。也许一些地址计算也取决于其他一些变量。

我正在评估将我们的 IP 切换为使用 SystemVerilog 约束,我的管理层希望了解创建/理解 SystemVerilog 约束的难易程度。

4

2 回答 2

9

由于我无法对上一篇文章发表评论,并且我建议的编辑被拒绝,我只能添加看起来像新答案但不是的内容。叹!

这是 Subbdue 的示例,经过编辑以修复以下语法错误:

  1. SVtypedef enum {...} <name>;语法与 OpenVeraenum <name> {...};语法相对
  2. 结束枚举声明;
  3. 声明变量channelNumber
  4. 声明ipVersionrand
  5. SV<var> inside { [<min>:<max>] }语法与 OpenVera<var> in { min:max }语法相对
  6. constraint用s结束表达式;
  7. SV->语法与 OpenVera=>语法相对
  8. 修正错字solver->solve
  9. 修正错字IPVx->IPVX

固定示例:

class RandomConstraints;
    typedef enum {IPV4=2, IPV6, IPVX} IpVersionType;

    //Randomly iterate over values without repetition
    randc bit [7:0] cyclicCounter;
    //Regular random variables
    rand bit [15:0] destAddress;
    rand bit [15:0] sourceAddress;
    rand bit [15:0] numberOfPackets;
    rand bit [15:0] packetLength;
    rand bit [3:0] channelNumber;
    rand bit [7:0]  var1;
    rand bit [7:0]  var2;
    rand IpVersionType ipVersion;

    //Non-random variables that can be used to control constraints
    bit [15:0] minNumberOfPackets, maxNumberOfPackets;
    bit [15:0] minPacketLength, maxPacketLength;
    integer IPV4Weight, IPV6Weight;

    constraint cPacketLength {
        packetLength inside { [ minPacketLength : maxPacketLength ] };
    }
    constraint cChannelNumber {
        channelNumber inside {[0:1]};
    }
    // Assuming total weight adds up to 100
    constraint cIPVersionType {
        ipVersion dist { IPV4 := IPV4Weight, IPV6 := IPV6Weight,
          IPVX := (100 - IPV4Weight - IPV6Weight) };
    }
    //Probability of var1 being 1,2,3,4,5 in the ratio 1,2,4,4,4
    constraint cDist1 {
        var1 dist { 1 := 1, 2 := 2, [3:4] := 4 };
    }
    //Probability of var2 being 1,2,3,4,5 is in the ratio 1,2,4/3,4/3,4/3
    constraint cDist2 {
        var2 dist { 1 := 1, 2 := 2, [3:5] :/ 4 };
    }
    //Implication constraint - if(channelNum == i) then { ... }
    constraint cImplication {
        (channelNumber == 0) -> {
            destAddress inside {[0:200]};
            sourceAddress inside {[201:400]};
        }
        (channelNumber == 1) -> {
            destAddress inside {[201:400]};
            sourceAddress inside {[0:200]};
        }
    }
    //Controlling order of constraints solved using a constraint solver
    constraint order_solver {
        solve channelNumber before destAddress;
        solve channelNumber before sourceAddress;
    }

    function new();
        //Setting default min/max packets and min/max packet length
        minNumberOfPackets = 100;
        maxNumberOfPackets = 1000;
        minPacketLength = 128;
        maxPacketLength = 256;
        IPV4Weight = 50;
        IPV6Weight = 30;
    endfunction
endclass
于 2013-05-08T14:33:28.013 回答
3

这是一个示例,可能并不大也不复杂,但它应该让您对如何使用约束有一个现实的想法。在这里,您将看到使用的以下概念:

  1. randc 和 rand - 循环和非循环随机变量
  2. 两种类型的分布式约束
  3. 在约束中使用 if 条件,使用隐含运算符...根据另一个变量计算的地址
  4. 约束顺序求解器
  5. 在构造函数中约束随机变量的默认范围
  6. 在约束中使用枚举类型

希望这会有所帮助......并注意在线评论:

class RandomConstraints;
    enum IpVersionType {IPV4=2, IPV6, IPVx}

    //Randomly iterate over values without repetition
    randc bit [7:0] cyclicCounter;
    //Regular random variables
    rand bit [15:0] destAddress;
    rand bit [15:0] sourceAddress;
    rand bit [15:0] numberOfPackets;
    rand bit [15:0] packetLength;
    rand bit [7:0]  var1;
    rand bit [7:0]  var2;
    IpVersionType ipVersion;

    //Non-random variables that can be used to control constraints
    bit [15:0] minNumberOfPackets, maxNumberOfPackets;
    bit [15:0] minPacketLength, maxPacketLength;
    integer IPV4Weight, IPV6Weight;

    constraint cPacketLength {
        packetLength in { minPacketLength : maxPacketLength }
    }
    constraint cChannelNumber {
        channelNumber in {0:1}
    }
    constraint cIPVersionType {
        ipVersion dist { IPV4 := IPV4Weight, IPV6 := IPV6Weight, IPVX := (100 - IPV4Weight - IPV6Weight) }
    }
    //Probability of var1 being 1,2,3,4,5 in the ratio 1,2,4,4,4
    constraint cDist1 {
        var1 dist { 1 := 1, 2 := 2, [3:4] := 4 }
    }
    //Probability of var2 being 1,2,3,4,5 is in the ratio 1,2,4/3,4/3,4/3 
    constraint cDist2 {
        var2 dist { 1 := 1, 2 := 2, [3:5] :/ 4 }
    }       
    //Implication constraint - if(channelNum == i) then { ... }
    constraint cImplication {
        (channelNumber == 0) => {
            destAddress in {0:200};
            sourceAddress in {201:400};
        }
        (channelNumber == 1) => {
            destAddress in {201:400};
            sourceAddress in {0:200};
        }
    }
    //Controlling order of constraints solved using a constraint solver
    constraint order_solver {
        solver channelNumber before destAddress;
        solver channelNumber before sourceAddress;
    }

    function new();
        //Setting default min/max packets and min/max packet length
        minNumberOfPackets = 100;
        maxNumberOfPackets = 1000;
        minPacketLength = 128;
        maxPacketLength = 256;
        IPV4Weight = 50;
        IPV6Weight = 30;
    endfunction
endclass
于 2012-11-02T05:46:16.217 回答