错题笔记:
移位寄存器的多种写法;
我的方法:(实例化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