前言
前文就是这个:
【验证小bai】randomize中的this.指的是哪个this呢
然后发现还是说的不怎么明白,因此在这篇里把情况掰开揉碎的说明一下,标题就叫:关于多个声明域变量名重名时如何取用的问题;
具体的场景就是下图,在class A的function B中随机C,此时randomize中需要使用一个变量mid,恰好A(下用class表示)/B(下用function表示)/C(下用transaction表示)内都有一个变量名字叫做mid;
最完备的场景罗列
在上次的博客博客中,我们已经确定了randomize中this的执行性,那么问题来了,如果我就是需要使用A、B或C中的mid,那么怎么办呢,看下面的示例代码:
transaction的声明:
class tv0; rand bit[7:0] id; bit[7:0] mid = 8'hF; endclass
最外层的class的声明:
class sanity_case extends base_test; bit[7:0] mid = 8'hA; extern function new(string name = "base_test", uvm_component parent=null); extern virtual function void build_phase(uvm_phase phase); `uvm_component_utils(sanity_case) endclass: sanity_case
function内的声明与取用验证:
function sanity_case::new(string name = "base_test", uvm_component parent=null); bit[7:0] mid = 8'h5; tv0 t0 = new(); tv0 t1 = new(); tv0 t2 = new(); tv0 t3 = new(); super.new(name, parent); t0.randomize with {id == this.mid;}; t1.randomize with {id == sanity_case::mid;}; t2.randomize with {id == local::mid;}; t3.randomize with {id == mid;}; $display("t0.id = 'h%0h", t0.id); $display("t1.id = 'h%0h", t1.id); $display("t2.id = 'h%0h", t2.id); $display("t3.id = 'h%0h", t3.id); $finish; endfunction: new
目前mid的值,transaction内为'hF,class中为'hA,function中为'h5;在randomize中取用方式分别使用了this./sanity_case::/local::/不带前缀四种方式,得到的结果如下:
t0.id = 'hf t1.id = 'ha t2.id = 'h5 t3.id = 'hf $finish called from file "../tc/sanity_case.sv", line 56. $finish at simulation time 0
因此可以得出结论,当class/transaction/function三个维度均存在该同名变量时:
- this.mid选择的是transaction内的变量;
- class_name::mid选择的是class内的变量;
- local::mid选择的是function内的变量;
- mid选择的是transaction内的变量;
当transaction内没有mid时
调整代码如下:
class tv0; rand bit[7:0] id; //bit[7:0] mid = 8'hF; endclass
打印结果:
t0.id = 'ha t1.id = 'ha t2.id = 'h5 t3.id = 'h5 $finish called from file "../tc/sanity_case.sv", line 56. $finish at simulation time 0
结论:
- this.mid选择的是class内的变量;
- class_name::mid选择的是class内的变量;
- local::mid选择的是function内的变量;
- mid选择的是function内的变量;