我有一个队列,其中一些元素表示要生成特定输入的次数。所以我们假设我们有以下队列元素:
my_q = {100,50,50,0};
现在,此队列代表以下内容:
> Input at position-0 should come 2 times
> Input at position-1 should come 1 times
> Input at position-2 should come 1 times
> Input at position-3 should come 0 times
因此,输入顺序应为:
0(输入-0) - 1(输入-1) - 0(输入-0) - 2(输入-2)
所以,我制作了以下代码,用于获取数字的HCF并将它们存储在队列中:
initial begin
int rate[$], mod_rate[$], final_port_seq[$];
int hcf;
int num_slot;
rate = {100,50,50,0};
hcf = find_hcf(rate); // Finds HCF from elements of rate
foreach(rate[i]) begin
num_slot += rate[i]/hcf; // Total number of slots
mod_rate[i] = rate[i]/hcf; // Get number of occurrences of particular element
end
$display("HCF = %0d Number of slots = %0d",hcf, num_slot);
// Need to change the following logic:
for (int i =0; i< num_slot;) begin
foreach (mod_rate[j]) begin
if (mod_rate[j] != 0 ) begin // This keeps the sequence 0-1-2-0
mod_rate[j]--;
final_port_seq[i] = j;
i++;
end
end
end
$display("Final sequence : ");
foreach (final_port_seq[i]) begin
$write(" %0d", final_port_seq[i]);
end
end
end // initial
function int find_hcf(int q[$]);
int min[$];
int max[$];
int q_refined[$];
int flag;
q_refined = q.find(item) with (item !=0); // 0 is port OFF
min = q_refined.min();
max = q_refined.max();
$display("min = %0d max = %0d",min[0],max[0]);
for(int i =min[0];i>0;i--) begin
flag = 0;
foreach(q[j]) begin
if(q[j]%i == 0) flag++;
end
if(flag == q.size()) begin find_hcf = i; break; end
end
endfunction
output:
min = 50 max = 100
HCF = 50 Number of slots = 4
Final sequence :
0 1 2 0
但我想交错队列元素并获得如下输出:
Final sequence :
0 1 0 2
有谁能告诉我需要实现什么样的逻辑才能拥有交错的队列元素?输入速率元素也可以是任何其他序列。
修改: DUT有四个输入。
队列包含输入的输入速率/频率。因此,在一堆4个时隙(4个时钟周期)中,如果my_q为{100,50,50,0}
,那么输入-0应该是input-1和input-3的两倍。并且输入-4不应该来。此外,输入-0应在其他两个输入之间交错。
这里我使用输入的HCF来确定特定输入必须占用多少个插槽,然后以循环方式排列它们。但我希望它们以交错的方式排列。