I would like to know if there is a recursive way to write a shift register having already the module of a Flip-Flop?
module DFF_N(Q,D,CK);
output Q;
input D,CK;
reg Q;
always @(negedge CK) begin
Q<=D;
end
endmodule
I want to use this module to create a shift register like this:
`include "DFF_N.v"
module shift_reg(Q,D,CK);
output [10:1] Q;
input D,CK;
DFF_N FFN_1(Q[1],D,CK);
DFF_N FFN_2(Q[2],Q[1],CK);
DFF_N FFN_3(Q[3],Q[2],CK);
DFF_N FFN_4(Q[4],Q[3],CK);
DFF_N FFN_5(Q[5],Q[4],CK);
DFF_N FFN_6(Q[6],Q[5],CK);
DFF_N FFN_7(Q[7],Q[6],CK);
DFF_N FFN_8(Q[8],Q[7],CK);
DFF_N FFN_9(Q[9],Q[8],CK);
DFF_N FFN_10(Q[10],Q[9],CK);
endmodule
Is there an easier way to write it without having to change manually the name of each FFN_i
?
One way is to use a generate
block with a for
loop:
module shift_reg(Q,D,CK);
output [10:1] Q;
input D,CK;
genvar i;
generate
for (i=1; i<=10; i=i+1) begin : shifter
if (i==1) begin
DFF_N FFN (Q[i], D, CK);
end else begin
DFF_N FFN (Q[i], Q[i-1], CK);
end
end
endgenerate
endmodule
Refer to IEEE Std 1800-2017, section 27.3 Generate construct syntax.
This can be easily scaled to different register widths, especially if you use a parameter
instead of 10
.