Your code has a few problems, and they all have the same root cause.
You used the Verilog reg
type doto declare some signals, which is the proper thing to do. However, for simulation purposes, a reg
has a default value of x
(unknown) at time 0, and it retains the x
value until you assign the reg
with a known value (0 or 1).
I see you are dumping VCD waveforms. You should use a waveform viewer to look at the internal signals of your design. If you were to look at the inputs of the DflipFlop_0
instance, you would see the the clk
signal is always x
. This signal is driven directly from the testbench. However, you did not initialize the signal in the testbench. One way to do so is in an initial
block. For example, add this line to your testbench:
initial clk = 0;
That will allow the clk
signal to toggle between 0 and 1 as desired.
Another problem is with the DflipFlop_0
instance q
output signal. You will also see that it is always x
, which is the default value for reg
types as already mentioned. This is complicated by the fact that the d
input is also x
, and the d
input is a combination of the outputs of the 2 flip-flops. Since the inputs and outputs of the flops are always unknown, the other signals which depend on them are unknown.
You need a way to initialize the flip-flop outputs to known values. This is commonly done using a reset signal. This reset, like the clock, would be a common signal which is an input to the top-level design module and would be driven by the testbench.
Here is an example of a flip-flop with an asynchronous reset:
module DflipFlop0 (q, rst, clk, d);
parameter WIDTH = 1;
output reg [WIDTH-1:0] q;
input rst;
input clk;
input [WIDTH-1:0] d;
always @ (posedge clk or posedge rst) begin
if (rst) begin
q <= 0;
end else begin
q <= d;
end
end
endmodule
I removed the q_inv
output since you weren't using it.