I'm trying to solve this HDLBits question, and my problem occurs when submitting the following code:
module top_module (
input clk,
input reset,
input [7:0] d,
output [7:0] q
);
genvar i;
generate for(i = 0; i < 8; i = i + 1)
begin: DFF
dFlipFlop instance_i(.clk(clk), .reset(reset), .d(d[i]), .q(q[i]));
end
endgenerate
endmodule
module dFlipFlop(input clk, input reset, input d, output q);
always@(negedge clk) begin
if(reset) q <= 8'h34;
else q <= d;
end
endmodule
Submitting this code gives me the hint "Your reset doesn't seem to be working.", and it is an unsuccessful attempt.
This error is fixed by removing the use of the generate loop and implementing a single, 8-bit flip flop as follows:
module top_module (
input clk,
input reset,
input [7:0] d,
output [7:0] q
);
always@(negedge clk) begin
if(reset) q <= 8'h34;
else q <= d;
end
endmodule
Why does the use of a generate loop cause issues?
The problem is not with the
generateloop.The problem is this line:
When you declare
output q,qis 1-bit wide, but you attempt to assign an 8-bit value to it (8'h34). This is the same as8'b0011_0100.qis always assigned to the LSB of the value, which is 0. The 7 MSBs are ignored. This means that all 8 instances ofdFlipFlopare reset to the same value (0). The code behaves as though you used this line:When you declare
qas 8-bit (output [7:0] q), you successfully reset the 8-bitqto the desired value.When I run your code on the HDLBits website, I see this warning when I click on the "Show Quartus messages..." link: