错题集:HDLBits Exams/m2014 q4k (移位寄存器)

错题笔记:

移位寄存器的多种写法;

 

我的方法:(实例化4个一位寄存器,并连接起来的思想)

module top_module (
    input clk,
    input resetn,   // synchronous reset
    input in,
    output out);
    wire q0,q1,q2;
    
    my_sr inst0(in,clk,resetn,q0);
    my_sr inst1(q0,clk,resetn,q1);
    my_sr inst2(q1,clk,resetn,q2);
    my_sr inst3(q2,clk,resetn,out);

endmodule

module my_sr(
    input D,
    input clk,
    input R,
    output Q
);
    always@(posedge clk)begin
        if(R==1'b0)
            Q<=0;
        else
            Q<=D;
    end
endmodule

官方答案:(直接实现一个4位的寄存器,每一位都可以是单独的寄存器,编写他们的关系)

module top_module (
	input clk,
	input resetn,
	input in,
	output out
);

	reg [3:0] sr;
	
	// Create a shift register named sr. It shifts in "in".
	always @(posedge clk) begin
		if (~resetn)		// Synchronous active-low reset
			sr <= 0;
		else 
			sr <= {sr[2:0], in};
	end
	
	assign out = sr[3];		// Output the final bit (sr[3])

endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值