欢迎大家加入2022届数字IC交流群,QQ群号 1060380138
可以通过使用相同的约束名称在派生类中重写基类定义的约束。下面是一个示例
class Base;
rand int a ;
rand int b;
constraint c_a_b_const {a < b;}
endclass
class Derived extends Base;
constraint c_a_b_const {a > b;}
endclass
function int count_ones ( ref bit [9:0] vec );
for( count_ones = 0; vec != 0; vec = vec >> 1 )
begin
count_ones += vec & 1'b1;
end
endfunction
constraint C1 { length == count_ones( myvec ) ; }
在约束中,不允许调用方向为ref的函数,除非使用“const ref”,这保证函数在内部不会修改参数。
class Base;
virtual function printA();
endfunction
endclass
1)
class Derived extends Base;
function printA();
//new print implementation
endfunction
endclass
2)
class Derived extends Base;
virtual function printA();
//new print implementation
endfunction
endclass
两者并没有区别,在基类中如果定义了virtual关键字,那么派生类也会继承该属性,无论有没有显式的二次声明。
class Packet;
bit [31:0] addr;
endclass
class ErrPacket extends Packet;
bit err;
endclass
module Test;
initial begin
Packet p;
ErrPacket ep;
ep = new();
p = ep;
$display("packet addr=%h err=%b", p.addr, p.err);
end
endmodule
没有问题,基类句柄可以指向派生类对象,但是反过来是不允许的。
class Packet; //Base Class
rand bit [31:0] src, dst, data[8]; // Variables
bit [31:0] crc;
virtual function void compute_crc;
crc = src ^ dst ^ data.xor;
endfunction
endclass : Packet
class BadPacket extends Packet; //Derived class
rand bit bad_crc;
virtual function void compute_crc; //overriding definition
super.compute_crc(); // Compute good CRC
if (bad_crc) crc = ~crc; // Corrupt the CRC bits
endfunction
endclass : BadPacket
示例
Packet pkt;
BadPacket badPkt;
initial begin
pkt = new;
pkt.compute_crc; // 1) Which of compute_crc() gets called?
badPkt = new;
badPkt.compute_crc; // 2) Which of compute_crc() gets called?
pkt = badPkt; // Base handle points to ext obj
pkt.compute_crc; // 3) Which of compute_crc() gets called ?
end
1)
for (i=0; i < length*count; i++) begin
a[i] = b[i];
end
2)
l_end = length * count;
for (i=0; i < l_end; i++) begin
a[i] = b[i]
end
class ABC;
local int var;
endclass
class DEF extends ABC;
function new();
var = 10;
endfunction
endclass
正确答案将在下一期公布,或者到下面的文章获取答案