Long MaX
Long MaX

Reputation: 3

Write followed by Read in VHDL process

The following code is for a very simple program in VHDL.

entity ent is
    port(
        clk: in std_logic;
        out_value: out std_logic;
    );
end entity ent;

architecture ent_arch of ent is
    signal counter: std_logic_vector(3 downto 0);
begin
    process(clk)
    begin
        if rising_edge(clk) then
            counter <= counter + 1;
            if counter = 10 then
                counter <= (others => '0') ;
            end if;
        end if;
    end process;
end ent_arch;

Imagine counter = 9 and we enter in the if statement (if rising_edge(clk)). The first statement counter <= counter + 1 will assign 10 to counter. My question is "Is the if statetement (if counter = 10) evaluted as true in this entrance or in the next entrance of the process?". In other words "For this comparison in this entrance of the process, is counter = 10 due to the previous statement?"

Thanks a lot for any answer!

Upvotes: 0

Views: 1113

Answers (1)

Martin Zabel
Martin Zabel

Reputation: 3659

Signal assignments are always delayed. You didn't specified a delay explicitly using the after keyword, thus the assignment is delayed for one delta cycle (same as after 0 fs). That means, that the value of the expression on the right side of this statement:

counter <= counter + 1;

will be assigned to counter at the beginning of the next simulation cycle.

But, all remaining statements in your process, are executed in the current simulation cycle. Thus, this read of the counter value,

if counter = 10 then

will still use the old value, so that, the counter will be reseted when it is already 10 at the rising clock edge. The counter counts from 0 to 10 inclusive.

The delay of at least one delta cycle, easily allows to swap the content of registers:

-- within declarative part of architecture
signal reg_a, reg_b : std_logic;

-- within architecture body
process(clock)
begin
  if rising_edge(clock) then
    reg_a <= reg_b;
    reg_b <= reg_a;  -- assign old value of reg_a ! 
  end if;
end process;

Upvotes: 0

Related Questions