VHDL syntaxe error near if

2019-03-03 16:16发布

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;

标签: vhdl
2条回答
看我几分像从前
2楼-- · 2019-03-03 17:00

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:

 Cout <= A(3) and B(3);

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:

Cout <= '1'  when A(3) = '1' and B(3) = '1' else '0';

(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 BOOLEAN and which is a short-circuit operator taking the place of Ada's if expression and then .... Without one or more intervening statements following an if statement (including it's end if) there is no reason to nest if 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

if A(3) = '1' and B(3) = '1' then
    Cout <= '1';
end if;

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.

查看更多
戒情不戒烟
3楼-- · 2019-03-03 17:06

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'.

查看更多
登录 后发表回答