HDLbits: Lfsr5

我的错误写法,半成品,完全错误:

cs 复制代码
module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 5'h1
    output [4:0] q
); 
    
    dff dff_1(clk, 0 ^ q[0],q[4]);
    dff dff_2(clk, q[4] ,q[3]);
    dff dff_3(clk, q[3] ^ q[0] ,q[2]);
    dff dff_4(clk, q[2] ,q[1]);
    dff dff_5(clk, q[1] ,q[0]);
    
    always@(posedge clk)
        if(reset)
            q <= 1;
   		else
            q <= q;
endmodule

module dff(input clk, input d, output Q);
    always@(posedge clk)
        Q <= d;
endmodule

参考网友的写法:

cpp 复制代码
module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 5'h1
    output [4:0] q
); 
    always@(posedge clk)
        if(reset)
            q <= 5'h1;
    	else
            q <= {0 ^ q[0],q[4],q[3]^q[0],q[2],q[1]};   
endmodule

官方的写法:感觉像第一个always是一个组合逻辑块(阻塞赋值,执行有先后顺序),第二个always是时序逻辑块。

其中,q_next[4] = q[0];应该是q_next[4] = q[0] ^ 0; 因为值不变省略了。

另外q_next = q[4:1]; 应该是q_next ={q[0],q[4:1]};

cs 复制代码
module top_module(
	input clk,
	input reset,
	output reg [4:0] q);
	
	reg [4:0] q_next;		// q_next is not a register

	// Convenience: Create a combinational block of logic that computes
	// what the next value should be. For shorter code, I first shift
	// all of the values and then override the two bit positions that have taps.
	// A logic synthesizer creates a circuit that behaves as if the code were
	// executed sequentially, so later assignments override earlier ones.
	// Combinational always block: Use blocking assignments.
	always @(*) begin
		q_next = q[4:1];	// Shift all the bits. This is incorrect for q_next[4] and q_next[2]
		q_next[4] = q[0];	// Give q_next[4] and q_next[2] their correct assignments
		q_next[2] = q[3] ^ q[0];
	end
	
	
	// This is just a set of DFFs. I chose to compute the connections between the
	// DFFs above in its own combinational always block, but you can combine them if you wish.
	// You'll get the same circuit either way.
	// Edge-triggered always block: Use non-blocking assignments.
	always @(posedge clk) begin
		if (reset)
			q <= 5'h1;
		else
			q <= q_next;
	end
	
endmodule
相关推荐
怪小庄吖21 小时前
翻译:How do I reset my FPGA?
经验分享·嵌入式硬件·fpga开发·硬件架构·硬件工程·信息与通信·信号处理
海涛高软2 天前
FPGA同步复位和异步复位
fpga开发
FakeOccupational2 天前
fpga系列 HDL:verilog 常见错误与注意事项 quartus13 bug 初始失效 reg *** = 1;
fpga开发·bug
zxfeng~2 天前
AG32 FPGA 的 Block RAM 资源:M9K 使用
fpga开发·ag32
whik11942 天前
FPGA 开发工作需求明确:关键要点与实践方法
fpga开发
whik11942 天前
FPGA开发中的团队协作:构建高效协同的关键路径
fpga开发
南棱笑笑生2 天前
20250117在Ubuntu20.04.6下使用灵思FPGA的刷机工具efinity刷机
fpga开发
我爱C编程2 天前
基于FPGA的BPSK+costas环实现,包含testbench,分析不同信噪比对costas环性能影响
fpga开发·verilog·锁相环·bpsk·costas环
移知3 天前
备战春招—数字IC、FPGA笔试题(2)
fpga开发·数字ic
楠了个难3 天前
以太网实战AD采集上传上位机——FPGA学习笔记27
笔记·学习·fpga开发