我想计算信号的高值和低值之间的时钟周期数。我有一个信号SIG,它在模拟过程中多次高低。我想计算高/低时的平均周期数。
有人可以指导我如何实现它。让我知道你是否需要更多的澄清。
你可以有一个计数器用于时钟周期,当信号SIG
为高时开始计数,并在SIG
变低时停止计数,以便在SIG
=“1”时测量时钟周期数:
int counter = 0; // counter initialization
@(posedge SIG); // waits for SIG to go high
while(SIG == 1) begin // while SIG = "1"
@(posedge CLK); // when clock signal gets high
counter++; // increase counter by 1
end
你可以这样做,而SIG
=“0”,与@(negedge SIG);
。
你可以做的是计算每个值高/低的周期数并将它们放入队列。然后在结束时将它们平均化。
module top;
bit sig, clk;
int hiCount[$]={0}, loCount[$]={0};
always #5 clk++;
always @(negedge clk) // 5:1 chance of keeping the same value
randomize(sig) with {sig == const'(sig) dist { 1 := 5, 0 := 1 }; };
always @(posedge clk) begin
case(sig)
1: if ($stable(sig))
hiCount[$]++;
else
hiCount.push_back(1); // first cycle high
0: if ($stable(sig))
loCount[$]++;
else
loCount.push_back(1); // first cycle low
endcase // case (sig)
$display("sig: %0d\nhi:%p\nlo: %p",sig,hiCount,loCount);
end
initial begin
repeat(100) @(posedge clk);
if (loCount[0] == 0) void'(loCount.pop_front());
if (hiCount[0] == 0) void'(hiCount.pop_front());
$display("sig was high %0d times with average cycle count of %0d",
hiCount.size(), hiCount.sum()/hiCount.size);
$display("sig was low %0d times with average cycle count of %0d",
loCount.size(), loCount.sum()/loCount.size);
$finish;
end
endmodule // top