I am currently working through FPGA4FUN's tutorial on quadrature decoding using an FPGA. I am currently just trying to simulate the decoders behavior by using a testbench that essentially drives the A and B inputs of the encoder high and low (out of phase) jsut like a normal encoder would. I am new to Verilog, and have a question about the following code:

module quad(clk, quadA, quadB, count);
output [7:0] count;
reg [7:0] count = 8'b00000000;

always @(posedge clk) begin
if(count_enable)
begin
if(count_direction) count<=count+1; else count<=count-1;
end
end

endmodule


This was recommended by FPGA4FUN because it is asynchronous, so the encoder doesn't have tot ick in sync with the clock - which is realistic. However, I notice something strange in the code. the reg [2:0] QuadA_delayed is not initialized. input quadA is coming from the encoder itself. We then have this statement:

always @(posedge clk) quadA_delayed <= {quadA_delayed[1:0], quadA};


What is the value of quadA_delayed[1:0]? Since the first bit is not initialized, it is currently just floating, and thus the rest of the variables in the code will not be determinable. This is seen on my waveform, as I get just a bunch of x's. I was wondering if anyone knows how I should initiliaze this, and why I need it. FPGA4FUN offers two examples for quadrature decoding. The first one is synchronous, and I managed to get that working. However, I can't get the asynchronous one working due to this quadA_delayed being 2 bits and having an undetermined bit.

To add extra information, I have attached what the circuit diagram looks like.

Any help would be greatly appreciated.

Thanks

Here is my testbench for reference:

timescale 1 ns/10 ps

module testbench;

wire [7:0] count ;

initial begin
clk = 0;
forever begin
#5
clk = ~clk;
end
end

initial begin

#10;
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
#5
/*
#10
*/
#10
\$finish;
end

endmodule


Edit 2:

I have somehow managed to get the counter incrementing now. I simply increased the timing interval in the testbench to 15 as opposed to 5. Thus quadA and quadB were held high and low for 3 times as long. There is still some weird behaviour, where the counter seems to lose track for 4 or 5 clock cyles, and then gets back on track. I also don't know why holding the signals for longer makes a difference, and whether or not this is a realistic simulation of a real encoder.

As you show in your circuit diagram, the line always @(posedge clk) quadA_delayed <= {quadA_delayed[1:0], quadA};` describes a shift register with 3 bits. As it mentions in the tutorial, this is there to prevent metastability in cases where the external async input violates setup and hold times. Since there is no reset line coming into the module, there is no way for quadA_delayed to have an initialization value, since there is no indication of when the initial value should be loaded into the shift register. So for the first 3 clock pulses, uninitialized values will be present in quadA_delayed. However, after that time has passed your waveform should show valid data.