我试图在另一个必须随机化的类中使用参数化的类。
typedef enum logic [1:0] {
TWO_LEGS,
FOUR_LEGS,
SIX_LEGS,
EIGHT_LEGS
} leg_e;
typedef enum logic [2:0] {
HUMAN,
DOGS,
CAT,
ELEPHANT,
FLY,
COCKROACH,
SPIDER
} animal_e;
class parameterized_class #( type enum_type);
static int initial_allocation;
static enum_type list[$];
rand int unsigned ptr;
constraint ptr_c {
ptr < list.size();
}
function new();
enum_type my_enum;
if(initial_allocation == 0) begin
initial_allocation = 1;
for(int unsigned i = 0; i < my_enum.num(); i++)
repeat($urandom_range(1,10)) list.push_back(my_enum);
end
endfunction
endclass
class random_class;
rand parameterized_class#(leg_e) leg_select;
rand parameterized_class#(animal_e) animal_select;
rand leg_e leg_q[$];
rand animal_e animal_q[$];
constraint leg_animal_c {
leg_select.list[leg_select.ptr] == TWO_LEGS -> animal_select.list[animal_select.ptr] inside {HUMAN};
leg_select.list[leg_select.ptr] == FOUR_LEGS -> animal_select.list[animal_select.ptr] inside {DOGS, CAT, ELEPHANT};
leg_select.list[leg_select.ptr] == SIX_LEGS -> animal_select.list[animal_select.ptr] inside {FLY, COCKROACH};
leg_select.list[leg_select.ptr] == EIGHT_LEGS -> animal_select.list[animal_select.ptr] inside {SPIDER};
}
constraint q_c {
leg_q.size() dist {[0:5] := 50, [6:10] := 50};
animal_q.size() == leg_q.size();
foreach(leg_q[i]) {
leg_q[i] == leg_select.list[leg_select.ptr];
animal_q[i] == animal_select.list[animal_select.ptr];
}
}
function new();
leg_select = new;
animal_select = new;
endfunction
endclass
module tb;
initial begin
random_class rnd = new;
repeat (10) begin
rnd.randomize();
$display("Animals in queue = %p", rnd.animal_q);
$display("Legs in queue = %p", rnd.leg_q);
end
end
endmodule
我遇到错误:
求解器将不会求解随机索引的数组“ animal_select.list”
变量“ animal_select.ptr”。
请尝试对数组使用状态变量,常量或循环变量
索引。
为了得到如下结果,我必须进行哪些修改: {狗,猫,人,大象} {FOUR_LEGS,FOUR_LEGS,TWO_LEGS,FOUR_LEGS,}
答案 0 :(得分:0)
我认为您可能想得太多。我认为这段代码会产生您希望得到的结果:
typedef enum logic [1:0] {
TWO_LEGS,
FOUR_LEGS,
SIX_LEGS,
EIGHT_LEGS
} leg_e;
typedef enum logic [2:0] {
HUMAN,
DOGS,
CAT,
ELEPHANT,
FLY,
COCKROACH,
SPIDER
} animal_e;
class random_class;
rand leg_e leg_q[$];
rand animal_e animal_q[$];
constraint leg_animal_c {
foreach (leg_q[i]) leg_q[i] == TWO_LEGS -> animal_q[i] inside {HUMAN};
foreach (leg_q[i]) leg_q[i] == FOUR_LEGS -> animal_q[i] inside {DOGS, CAT, ELEPHANT};
foreach (leg_q[i]) leg_q[i] == SIX_LEGS -> animal_q[i] inside {FLY, COCKROACH};
foreach (leg_q[i]) leg_q[i] == EIGHT_LEGS -> animal_q[i] inside {SPIDER};
}
constraint q_c {
leg_q.size() dist {[0:5] := 50, [6:10] := 50};
animal_q.size() == leg_q.size();
}
endclass
module tb;
initial begin
random_class rnd = new;
repeat (10) begin
rnd.randomize();
$display("Animals in queue = %p", rnd.animal_q);
$display("Legs in queue = %p", rnd.leg_q);
end
end
endmodule
答案 1 :(得分:0)
我在您的代码中进行了$diplay()
调试
foreach (list[i]) $display(list[i].name());
,然后看到您的初始化错误。您总是将第一个枚举推入列表。 我将您的代码更改为
for(int unsigned i = 0; i < my_enum.num(); i++) begin
/*repeat($urandom_range(1,10))*/ list.push_back(my_enum);
my_enum = my_enum.next();
end
,它确实起作用。 EDA代码here。
为什么所有队列都具有相同的元素,这是由于您的约束
foreach(leg_q[i]) {
leg_q[i] == leg_select.list[leg_select.ptr];
animal_q[i] == animal_select.list[animal_select.ptr];
}