【发布时间】:2020-01-28 03:51:21
【问题描述】:
我正在研究一个计数器,该计数器计算可变宽度输入比特流中的高位数。代码如下:
module counter(i_clk, i_arst, i_data, o_done, o_cnt);
// ---------------------------------------- SIGNALS ---------------------------------------- //
// Parameters
parameter OUT_WIDTH; // Width of the output in bits
parameter IN_WIDTH; // Number of bits in an input bit stream
// Input
input wire i_clk; // Clock
input wire i_arst; // Active high asynchronous reset
input wire i_data; // Input data
// Outputs
output reg o_done; // Output done bit
output reg[OUT_WIDTH-1:0] o_cnt; // WIDTH-bit output counter
// Internal signals
integer index; // Bit index for the input
reg[OUT_WIDTH-1:0] r_cnt_tmp; // Temporary counter for assignment
// ---------------------------------------- LOGIC ---------------------------------------- //
// Combinational logic
always @(*) begin
o_cnt = r_cnt_tmp;
end
// Sequential logic
always @(posedge i_clk or posedge i_arst) begin
// Reset the counter
if(i_arst) begin
r_cnt_tmp <= {OUT_WIDTH{1'b0}};
o_done <= 1'b0;
index <= 0;
end
else begin
// When a new bit stream arrives
if(index == 0) begin
r_cnt_tmp <= {OUT_WIDTH{1'b0}}; // Reset the output data
o_done <= 1'b0; // Data is now invalid because it is a new bit stream
// It only happens after a reset or a valid data output
end
// If bit is set
if(i_data == 1'b1) begin
r_cnt_tmp <= r_cnt_tmp + 1; // Count up
end
index <= index + 1; // Increment the index
if(index == IN_WIDTH) begin // The input has been completely looped over
o_done <= 1'b1; // Data is now valid for the output
index <= 0; // Reset the index
end
end
end
endmodule
完成当前比特流后,我设置 o_done 信号以通知输出数据有效,并重置变量索引以从新比特流开始。然后在下一个时钟上升沿,我重新设置了 o_done 信号和计数器值并重新开始计数。
我的问题是我的计数器并不总是重置。由于信号只在块的末尾取值,如果比特流的第一个比特为高,那么它不会被重置。
我想在同一个时钟周期内重新设置并重新开始计数,因为我不想要更多的延迟,而且我有连续的比特流到达输入端。
有没有办法避免这个问题?
感谢您的帮助。
【问题讨论】:
-
我很难理解您究竟想要什么。你说的是
both assignments。所有分配都在同一个块中。这个:o_cnt = r_cnt_tmp;是多余的。如果我明白你想要什么 1/ 需要一个额外的位计数器副本来输出。 2 / 您需要考虑到在您想要重新开始计数时可能会到达一些位。这需要一个更复杂的 if 语句来处理比特是否到达的额外条件。