Verilog-在单个always块中为reg赋值两次
我正在研究一个计数器,它计算可变宽度输入比特流中的高位数。代码如下:Verilog-在单个always块中为reg赋值两次,verilog,counter,Verilog,Counter,我正在研究一个计数器,它计算可变宽度输入比特流中的高位数。代码如下: module counter(i_clk, i_arst, i_data, o_done, o_cnt); // ---------------------------------------- SIGNALS ---------------------------------------- // // Parameters parameter OUT_WID
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
模块计数器(i_clk、i_arst、i_data、o_done、o_cnt);
//-------------------------------------信号-------------------------------------------//
//参数
参数OUT_WIDTH;//以位为单位的输出宽度
_WIDTH;//中的参数输入比特流中的比特数
//输入
输入线i_clk;//钟
输入线i_arst;//有源高异步复位
输入导线i_数据;//输入数据
//输出
输出注册表o_done;//输出完成位
输出寄存器[OUT_WIDTH-1:0]o_cnt;//宽度位输出计数器
//内部信号
整数索引;//输入的位索引
reg[OUT_WIDTH-1:0]r_cnt_tmp;//临时派工柜台
//-------------------------------------逻辑-------------------------------------------//
//组合逻辑
始终@(*)开始
o_cnt=r_cnt_tmp;
结束
//顺序逻辑
始终@(posedge i_clk或posedge i_arst)开始
//重置计数器
如果(我)开始
我认为你的问题在于这些陈述:
// 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
抱歉,我没有测试它,特别是当i_数据为“x”时
初始条件可能需要不同的要求。所以,你可以用自己的方式来写。我正在努力理解你到底想要什么。你谈论两个作业
。所有作业都在同一块中。这个:o_cnt=r_cnt\u tmp代码>是多余的。如果我知道你想要什么,你需要一个额外的位计数器的副本来输出。2/您需要考虑到在重新开始计数时可能会出现位。这需要一个更复杂的if语句来处理位是否到达的额外条件。谢谢你的回答。我在考虑一个更复杂的结构来做这件事,但我觉得它并不优雅,你的肯定更好。到目前为止,它似乎奏效了。这就是我需要的。
// When a new bit stream arrives
if(index == 0) begin
r_cnt_tmp <= i_data; // Reset the output data to '0' or '1' depending on i_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
else if(i_data == 1'b1) begin // you definitely need this 'else'
r_cnt_tmp <= r_cnt_tmp + 1; // Count up
end