1

I am trying to write a FSM. A very simple one, which updates its value (0 to 4 and then 0 again) on the posedge of the input SCL.

This is my design source code:

`timescale 1ns / 1ps

module fsm(
    input wire SCL,
    output wire [3:0] state
    );
    reg [3:0] state_drv = 0;
    
    assign state = state_drv;
    
    always@(posedge SCL)begin
        if(state < 4)begin
            state_drv <= state_drv + 1;
        end
        else begin
            state_drv <= 0;
        end        
    end
endmodule

and here is my testbench code

`timescale 1ns / 1ps

module testbench();
    
    reg clk = 0;
    wire [3:0] state;
    wire scl;
    
    always #5 clk = ~clk;
    
    assign scl = clk;
    
    fsm is1(
        .SCL(clk),
        .state(state)
    );
    
    initial begin 
        # 10000 
        $finish;
    end
endmodule

In the post-implementation timing simulation (I use Vivado and its default simulator), I found that the waveform is a little bit different from what I have in mind.

Waveform:

waveform

I want the state to change from 01 (1) → 10 (2), but the state transition is 011110. Looking at the waveform of each element in the array. I think it's because not all the bits change at the same moment.

Is this a common issue or there are problems with my Verilog code?

1
  • if you need to circumvent the whole thing, it's a good ideal to check en.wikipedia.org/wiki/Gray_code (minimum difference code) Commented Jun 30, 2023 at 2:04

1 Answer 1

1

Since you are doing a post-implementation timing simulation, the different elements of the array do in fact not change at the same time.

They are implemented as individual flip-flops in the hardware where individual signal propagation delays are taken into account by the simulator. The synthesis just has to make sure that all state changes occur within the allowed setup and hold time constraints for the given clock signal.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.