0

In the following code, A is attached to I/O while B is an internal signal that is not assigned an initial value.

architecture Behavioral of adder is
  signal B : STD_LOGIC_VECTOR(7 DOWNTO 0);
begin
  process(clock)
    variable result : STD_LOGIC_VECTOR(7 DOWNTO 0);
  begin
    if rising_edge(clock) then
      result := STD_LOGIC_VECTOR(unsigned(A) + unsigned(B));
      Y <= result;
      B <= A;
    end if;
  end process;
end Behavioral;

If B is assigned after result is assigned, and B is not assigned until after the first pass, how will result be calculated?

In simulation, it shows 'X' for the output which is as expected, but how is this implemented in hardware?

cepuz
  • 9
  • 1
  • It depends on the underlying implementation. For an ASIC, flip-flops are likely to start with a somewhat random value - but for cryptology, may or may not be random enough. For an FPGA, it will depend on what the device does at power-up and my be deterministic and may not - depending on the technology. If you care, then use a reset signal. You might note that you have the same problem with the output Y before the first clock edge happens. – Jim Lewis May 25 '23 at 00:25
  • _how is this implemented in hardware_: It is not. This is one of the many cases where the model (VHDL) and the real thing differ. On the model side `X` means _unknown_ and `U` means _uninitialized_. On the hardware side there is no such things as _unknown_ or _uninitialized_ (what would it be?), there are only voltages. Depending on your target hardware (ASIC, FPGA...) the registers that you left uninitialized in the VHDL model will take different values at power-up. It can be always 0, or 1, or it can change from one power-up to the next in a more or less random way. – Renaud Pacalet May 25 '23 at 09:45

1 Answers1

0

I assume you are aware that VHDL is a hardware description language, not a programming language.

As a mental model, think about the values of all unused registers in your actual hardware. They are digital*, so they will be either 1 or 0. What values do they hold after power-on?

So depending on your actual hardware, it can be any value, until the first assignment is "executed," in your case after the first clock edge. This is the meaning of X in the simulation, simply said.

Please be aware that although these values are undefined, they do not have to be random.

If you want defined values, use an asynchronous reset.


*) We are talking about real hardware, and as such there is more to it. However, this simplified view works for nearly all practical cases.

the busybee
  • 10,755
  • 3
  • 13
  • 30