You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用SystemVerilog约束实现16通道随机选不同数量发送数据?

使用SystemVerilog约束实现通道随机选择逻辑

针对你提出的16个通道(0-15)随机选择需求,每次迭代选中的通道数量唯一、通道可重复选择的场景,可以通过以下方式实现:

核心实现思路

  1. 记录已使用过的通道数量,确保每次迭代选中的数量不重复;
  2. 随机生成本次迭代的通道数量,范围为1~16(覆盖所有可能的非空选择);
  3. 约束选中的通道列表长度等于本次生成的数量,且每个通道编号在0~15范围内;
  4. 每次随机化完成后,将本次使用的数量加入已使用集合,避免后续重复。

完整代码实现

class channel_selector;
  // 存储已使用过的通道数量,保证每次迭代数量唯一
  int used_sizes[$];
  // 本次选中的通道列表
  rand int unsigned selected_channels[];
  // 本次要选中的通道数量
  rand int unsigned current_size;

  // 约束:通道数量范围1~16,且未被使用过
  constraint size_constraint {
    current_size inside {[1:16]};
    !(current_size inside used_sizes);
  }

  // 约束:通道列表长度匹配当前数量,每个通道编号合法
  constraint channels_constraint {
    selected_channels.size() == current_size;
    foreach (selected_channels[i]) {
      selected_channels[i] inside {[0:15]};
    }
  }

  // 随机化后记录本次使用的数量
  function void post_randomize();
    used_sizes.push_back(current_size);
  endfunction

  // 重置已使用数量集合,重新开始迭代
  function void reset();
    used_sizes.delete();
  endfunction
endclass

使用示例

module testbench;
  channel_selector sel;

  initial begin
    sel = new();
    // 执行16次迭代,覆盖所有1~16的数量
    repeat(16) begin
      if (!sel.randomize()) begin
        $error("Randomization failed - all sizes have been used");
        $finish;
      end
      $display("Iteration %0d: Selected %0d channels: %p", 
               sel.used_sizes.size(), sel.current_size, sel.selected_channels);
    end

    // 重置后可再次进行迭代
    sel.reset();
    $display("\nAfter reset, starting new iterations:");
    repeat(3) begin
      if (!sel.randomize()) begin
        $error("Randomization failed");
        $finish;
      end
      $display("Iteration %0d: Selected %0d channels: %p", 
               sel.used_sizes.size(), sel.current_size, sel.selected_channels);
    end
  end
endmodule

关键注意事项

  • 由于只有16种不同的非空通道数量(1~16),当迭代次数超过16次时,随机化会失败。如果需要持续迭代,可以在post_randomize中判断used_sizes是否已满,自动调用reset清空集合;
  • 上述代码允许同一迭代内重复选择同一个通道(符合需求描述)。如果需要同一迭代内通道不重复,可在channels_constraint中添加unique {selected_channels}约束;
  • reset方法用于清空已使用的数量记录,方便重新开始一轮全新的迭代。

内容的提问来源于stack exchange,提问作者atniner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 23:15:27