Guest
Hi everybody
I have a simple question about inferred latches in combinational
processes. Are
latches good or bad or what in such cases? As far as I get it, latches
are inferred
whenever a signal is not assigned a default value in the process and
not all possible
paths of execution assign a value to the signal in consideration.
Following
is a simple example:
....
signal a : std_logic_vector(7 downto 0);
signal b : std_logic_vector(7 downto 0);
signal result : std_logic_vector(7 downto 0);
signal op : std_logic;
signal overflow : std_logic;
....
simple_alu: process(a, b, op)
-- variable to hold the sum a + b when determinig overflow
variable tmp : std_logic_vector(8 downto 0);
begin
if op = '1' then
-- compute the sum
--
tmp := ("0" & op_a) + ("0" & op_b);
-- detect overflow
overflow <= a(7) xor b(7) xor tmp(7) xor tmp(8);
else
-- compute the exclusive or of a and b
tmp := ("0" & op_a) xor ("0" & op_b);
end if;
result <= tmp(7 downto 0);
end process simple_alu;
....
In this process the signal 'overflow' is assigned only in
the first case of the 'if' statement. As I get it, a latch
here shall be inferred for this signal, that ouputs the computed
value for 'overflow' whenever signal 'op' equals 1, and will
keep the most recent valid output value when 'op' is different
than 1. Am I correct?
Also, here let us assume that the simple ALU is used by some
external logic that is clever enough to take care of the
'overflow' signal only when performing additions, effectively
discarding its value when performing exclusive-ors. In this case
it really does not matter what the ALU outputs when performing
xors, so it is safe that the latch be removed, right?
In all, I simply do not get the purpose of latches in such cases.
Also, is this not possible - suppose that (as in real life) some
synchronous logic changes the inputs to the ALU but the
inputs do not change simultaneously, some values are propagated
for 'a' and 'b' (perhaps even not all of the values in these
vectors - say a(3) and b(7) are slower, for some reason)
when the operation is 'addidtion'. The ALU computes (literally)
'something', then the type of operation gets changed to 'xor' and
in a short time all signals settle down to their stable states until
changed at the next heartbeat of the synchronous logic controlling
the ALU. If this is possible (I think it is) then the value of
'overflow' is obviously totally wrong and is therefore useless.
Any logic that relies on using the latest assigned value will
therefore behave incorrectly. I suppose this all means
that in real life such latches are a very thin ice to tread onto
and, if the desired behavior is indeed to use the last valid assigned
value to a signal, this must be handled by means of some explicit
'enable' signals and/or synchronous logic (can't think of an example
right now),
instead of relying on the input signals to indirectly drive the
inferred latch enable signals. Is this correct? I will really
appreciate any comments on these topics.
Thanks and best regards,
Stoyan Shopov
I have a simple question about inferred latches in combinational
processes. Are
latches good or bad or what in such cases? As far as I get it, latches
are inferred
whenever a signal is not assigned a default value in the process and
not all possible
paths of execution assign a value to the signal in consideration.
Following
is a simple example:
....
signal a : std_logic_vector(7 downto 0);
signal b : std_logic_vector(7 downto 0);
signal result : std_logic_vector(7 downto 0);
signal op : std_logic;
signal overflow : std_logic;
....
simple_alu: process(a, b, op)
-- variable to hold the sum a + b when determinig overflow
variable tmp : std_logic_vector(8 downto 0);
begin
if op = '1' then
-- compute the sum
--
tmp := ("0" & op_a) + ("0" & op_b);
-- detect overflow
overflow <= a(7) xor b(7) xor tmp(7) xor tmp(8);
else
-- compute the exclusive or of a and b
tmp := ("0" & op_a) xor ("0" & op_b);
end if;
result <= tmp(7 downto 0);
end process simple_alu;
....
In this process the signal 'overflow' is assigned only in
the first case of the 'if' statement. As I get it, a latch
here shall be inferred for this signal, that ouputs the computed
value for 'overflow' whenever signal 'op' equals 1, and will
keep the most recent valid output value when 'op' is different
than 1. Am I correct?
Also, here let us assume that the simple ALU is used by some
external logic that is clever enough to take care of the
'overflow' signal only when performing additions, effectively
discarding its value when performing exclusive-ors. In this case
it really does not matter what the ALU outputs when performing
xors, so it is safe that the latch be removed, right?
In all, I simply do not get the purpose of latches in such cases.
Also, is this not possible - suppose that (as in real life) some
synchronous logic changes the inputs to the ALU but the
inputs do not change simultaneously, some values are propagated
for 'a' and 'b' (perhaps even not all of the values in these
vectors - say a(3) and b(7) are slower, for some reason)
when the operation is 'addidtion'. The ALU computes (literally)
'something', then the type of operation gets changed to 'xor' and
in a short time all signals settle down to their stable states until
changed at the next heartbeat of the synchronous logic controlling
the ALU. If this is possible (I think it is) then the value of
'overflow' is obviously totally wrong and is therefore useless.
Any logic that relies on using the latest assigned value will
therefore behave incorrectly. I suppose this all means
that in real life such latches are a very thin ice to tread onto
and, if the desired behavior is indeed to use the last valid assigned
value to a signal, this must be handled by means of some explicit
'enable' signals and/or synchronous logic (can't think of an example
right now),
instead of relying on the input signals to indirectly drive the
inferred latch enable signals. Is this correct? I will really
appreciate any comments on these topics.
Thanks and best regards,
Stoyan Shopov