那么菜 2024-11-09 11:05 采纳率: 0%
浏览 41

systemverilog 编程 randomize 的疑惑



```c++
如下代码: 

`timescale 1ns/1ps 
`include "uvm_macros.svh"
import uvm_pkg::*;

class my_obj1 extends uvm_test;

  `uvm_component_utils(my_obj1)
   
   rand int  src=4;
   
   rand int dst;
   constraint dst_c {
      dst inside {[7:9]};
   }

   rand bit [3:0]  crc = 4'b0111;
   constraint crc_c {
      crc inside {5,6,7,8};
   }

  function new(string name = "my_obj1" , uvm_component parent = null );
    super.new(name, parent);
  endfunction // new

  extern virtual function print ();
  extern virtual function pure_print ();

  virtual task run_phase(uvm_phase phase);
     super.run_phase(phase);
  endtask 

endclass //

function my_obj1::print();
    randomize (src)   with {(src) inside {[5:5]};} ;
    `uvm_info(get_type_name(),$psprintf("%0h,%0h,%0h",src,dst,crc),UVM_LOW)
endfunction

function my_obj1::pure_print();
    `uvm_info(get_type_name(),$psprintf("%0h,%0h,%0h",src,dst,crc),UVM_LOW)
endfunction
//////////////////////////////

module tb();
  
import uvm_pkg::*;
`include "uvm_macros.svh"

  initial begin
    run_test("my_obj1");
  end

endmodule // tb

​​


```c++
=======================================================

Solver failed when solving following set of constraints 


integer dst = 0;

constraint dst_c    // (from this) (constraint_mode = ON) (lab__uvm_base.sv:15)
{
   (dst inside {[7:9]});
}

=======================================================

Info-[CNST-SATE]  A standalone test-case for this failure has automatically been extracted 
  from randomize serial 1 partition 2.
  To reproduce the error using the extracted testcase, please use the 
  following command:
  cd 
  /home/autumn/project/uvm_learning/lab__uvm_class_randomize_01/./simv.cst/testcases;
  vcs -sverilog extracted_r_1_p_2_inconsistent_constraints.sv -R

请问: 代码中,明明是 randomize(src) , 为什么报错 dst 求解失败呢?

  • 写回答

2条回答 默认 最新

  • 无疆76 2024-11-09 11:06
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    这个问题是关于SystemVerilog中的约束随机化。在你的代码中,你尝试对src进行随机化,但是报错是关于dst的约束求解失败。

    出现这个错误的原因可能是在my_obj1类的实例化或随机化过程中,dst的约束dst_cdst的初始值或其他约束产生了冲突。SystemVerilog的约束求解器试图满足所有约束,但当它不能满足所有约束时,就会报错。

    在你的代码中,dst的约束dst_c要求其值在7到9之间,但如果dst的初始值(在你的代码中未显示)不在这个范围内,或者在随机化过程中其他约束改变了dst的值并使其超出了这个范围,那么约束求解器就会失败。

    为了解决这个问题,你可以检查以下几点:

    1. 确保dst的初始值满足所有约束。
    2. 检查在随机化过程中是否有可能改变dst的值的其他约束或代码,并确保它们不会使dst的值超出约束范围。
    3. 如果可能的话,尝试简化或重新表述约束,使其更容易满足。

    另外,你的代码中有一个地方可能需要注意。在print函数中,你对src进行了随机化并立即打印其值。通常,在函数中对变量进行随机化是没有意义的,因为函数执行完毕后,随机化的值就会丢失。如果你想在随机化后保存某个值,你可能需要在类的实例变量中保存这个值,并在需要的时候进行打印。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月9日