I'm a trying to find out the problem with this simple VHDL code. I'll be grateful if someone could help me. PS: I tried the code without the conditional block and it works :S !
*The message error is : Error (10500): VHDL syntax error at Four_Bits_Adder.vhd(18) near text "if"; expecting ";", or an identifier ("if" is a reserved keyword), or "architecture"*
The 4 bits adder code is :
Library ieee;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Use ieee.std_logic_unsigned.all;
entity Four_Bits_Adder is
port(A,B:in std_logic_vector(3 downto 0);
S:out std_logic_vector(3 downto 0);
Cout:out std_logic);
end Four_Bits_Adder;
architecture description of Four_Bits_Adder is
begin
S<= A+B;
if A(3)=1 then
if B(3)=1 then
Cout<=1;
end if;
end if;
end description;
An
if
statement is a sequential statement, and from your usage should either be in a process statement or you should instead use a concurrent signal assignment statement. It hardly seems complex enough for a process statement.Concurrent assignment statement:
Note that concurrent assignment statement using an expression as the value in it's signal assignment can pass values other than '0' or '1'.
A different style of concurrent signal assignment statement could pass only binary representing enumeration values:
(Your
if
statement also appears to infer a latch and could be optimized as a constant '1').Note also that your original
if
nested statements could be expressed with a BOOLEANand
which is a short-circuit operator taking the place of Ada'sif expression and then ...
. Without one or more intervening statements following anif
statement (including it'send if
) there is no reason to nestif
statements with different expressions. The short-circuit operator would only evaluate the subsequent expression if the first expression evaluated true.The form would be along the lines of
And could still only be used where a sequential statement is appropriate.
Note that std_logic requires enumeration values ('U', 'X', '0', '1',...) while
1
is a numeric value and would result in errors.Besides David's (correct) answer, note that if you are trying to make a 4-bit adder with Cout as the carry output, that process will not give the correct value. Consider A = "1111" and B = "0001". A(3) is '1' but B(3) is not, and the result is S = "0000" and Cout should be '1' but is '0'.