我正在编写一个算法的verilog代码,但是我遇到一个模块的问题,例如:接收来自前一个模块的10个二进制数(每个4位)(每个正边沿clk输入1个)所以有10个时钟周期来获得10个二进制数。
如何计算每个数字重复的次数并保存每个数字的频率,供以后使用verilog hardawre语言的其他模块使用? 例如:最后这个模块找到0000两次,0001一次,....,1111零。在10个时钟周期。
提前致谢...
答案 0 :(得分:0)
假设你有一个时钟和低电平有效复位:
module tracker(
input clk,
input rst_n,
input [3:0] data_rx
);
reg [7:0] count [0:15];
integer i;
always @(posedge clk, negedge rst_n) begin
if (~rst_n) begin
for(i=0; i<16, i=i+1) begin
count[i] <= 8'b0;
end
end
else begin
count[data_rx] <= count[data_rx] + 1;
end
end
endmodule
对于FPGA默认值,可以使用初始块代替看起来像的复位信号:
initial begin
for(i=0; i<16, i=i+1) begin
count[i] <= 8'b0;
end
end
always @(posedge clk) begin
count[data_rx] <= count[data_rx] + 1;
end
请注意,异步复位和初始化中使用了for循环,可以静态展开,没有动态目标,因此可以完全合成。