Electronic – VHDL assignment and condition at the same clock edge on parallel processes

clockfpgavhdl

Suppose that I have two processes in VHDL: One process is triggered on the rising clock edge and it is a state machine that sets a flag in one of its states. The second process is also triggered on the rising clock edge and it has a condition statement that reads this flag to see if it is '1'. When will the condition in the second process become 'true'? On the same clock that the flag is set, or at the next clock?

Here is an example:

-- state machine 
process (clk)
begin
    if rising_edge (clk) then       
        case StateMachine is
            when '0' =>
                flagA <= '0';
                StateMachine <= '1';
            when '1' =>
                flagA <= '1';
                StateMachine <= '1'; -- loop forever
        end case;
    end if;
end process;

-- condition
process (clk)
begin
    if rising_edge (clk) then       
        if (flagA = '1') then
            flagB <= '1';
        else
            flagB <= '0';
        end if;
    end if;
end process;

In this example, will flagB become '1' at the same clock that flagA became '1'? Or will flagB become '1' at the next clock?

Best Answer

Flag state will be sampled on the next clock edge. In the synchronous design, all the conditions are (or must be) "prepared" before clock edge occurs, and when it occurs, circuit transitions to new state within very short timeframe. Thus in situations like yours circuit will see previously "prepared" condition, thus previous state of the flag. On the next clock edge new flag value will be "prepared", and it will be sampled.

You spend whole cycle to get the new propagated flag value; you may consider to trigger one process on rising edge, and another process on the falling edge, this way you will spend only half of the cycle.

Update (as per comments below): when designing circuits driven by both edges of the clock, you must pay close attention to the duty cycle of the clock, not just its period.