在 "loop" 附近:期待 IF; "process" 附近:期待循环

near "loop": expecting IF; near "process": expecting LOOP

这是错误

library IEEE;

use IEEE.std_logic_1164.all;

use IEEE.std_logic_signed.all;

use IEEE.numeric_std.all;

entity MULT is

GENERIC(n:integer:=4);

 port(A,B: in  std_logic_vector(n-1 downto 0);
       P : out std_logic_vector(2*n-1 downto 0)); -- PRODUCT

end entity;


architecture BOOTH of MULT is

begin

BOOTH:process(A,B)

 variable X:std_logic_vector(2*n downto 0); 

 variable Y:std_logic_vector(n-1 downto 0); 

 variable Z:std_logic_vector(n-1 downto 0); 

 variable U:std_logic_vector(n-1 downto 0); 

begin

for J in 0 to n-1 loop

 U(j):='0';

end loop;

 X:=U & A & '0';

 Y:=B;               
 
for I in 0 to n-1 loop

 if(X(1)='1' and X(0)='0') then

  Z:=X(2*n downto n+1);

   X(2*n downto n+1):= Z-Y;

    X(2*n-1 downto 0):=X(2*n downto 1);


 else if(X(1)='0' and X(0)='1') then

  Z:=X(2*n downto n+1);

   X(2*n downto n+1):= Z+Y;

    x(2*n-1 downto 0):=X(2*n downto 1);


 else

  X(2*n-1 downto 0):=X(2*n downto 1);

end if;

end loop;

P(2*n-1 downto 0) <= X(2*n downto 1);


end process;

end architecture BOOTH;

让我们尝试了解编译器在这里告诉您的内容。在 end loop 它期待 end if。这意味着当您尝试关闭 loop 时,您实际上是在另一个 if 中。下一个错误只是第一个错误的结果。

为什么会这样?

让我们检查一下有问题的块的起点可能在哪里。根据编译器的说法,在 end loop 上方的 end if,您显然已成功关闭一个 if,但您仍在另一个中。你认为你关闭了if(X(1)='1' and X(0)='0') then,但显然你没有,否则我们不会有那个错误。

那个end if之间还有另一个if吗? 是的! 是在这一行:else if(X(1)='0' and X(0)='1') then.

你显然认为这算作原始 if 的一部分,但编译器不这么认为,所以这就是你 google else if vhdl 的地方。然后你会发现正确的语法实际上是 elsif,因为否则你会 else 加上一个新的、单独的 ifelse 里面,这就是这里发生的事情。

通过使用与编译器现在看到的内容相匹配的缩进来可视化发生的事情:

for I in 0 to n-1 loop
 if(X(1)='1' and X(0)='0') then
   ...
 else
   if(X(1)='0' and X(0)='1') then
    ...
   else
    ...
   end if;
 ??? (end if missing here)
end loop; <<< error, end if expected, end loop found

使用elsif,它会变成这样:

for I in 0 to n-1 loop
 if(X(1)='1' and X(0)='0') then
   ...
 elsif(X(1)='0' and X(0)='1') then
  ...
 else
  ...
 end if;
end loop;

底线:您需要 elsif 而不是 else if

顺便说一句:你的缩进没有意义。您的后续行总是进一步缩进 space,即使它们位于同一块级别。这只会让你感到困惑。确保正确缩进代码 - 还有 editor plugins 可以帮助你。