VHDL fsm 错误 - "when" 附近:(vcom-1576) 期待结束

VHDL fsm error - near "when": (vcom-1576) expecting END

我正在尝试使用 modelsim 在 vhdl 中制作一个 fsm,但是当我尝试编译我的代码时,我遇到了这个错误

** Error: C:/Users/manor/Desktop/ldh/mult_fsm.vhd(34): near "when": (vcom-1576) expecting END.

** Error: C:/Users/manor/Desktop/ldh/mult_fsm.vhd(60): near "when": (vcom-1576) expecting END.

** Error: C:/Users/manor/Desktop/ldh/mult_fsm.vhd(72): near "else": (vcom-1576) expecting END.

这是我的代码

library ieee;
use ieee.std_logic_1164.all;

entity mult_fsm is
    port(ck,adx,m: in std_logic;
        adsh,sh,cm,mdone: out std_logic);
end entity mult_fsm;

architecture ideal of mult_fsm is
    type StateType is (S0, S1, S2, S3, S4);
    signal CurrentState, NextState: StateType;
begin
    NS_CS: process( ck)
    begin
    if ck'event and ck='1' then
        case CurrentState is
            when S0=>
            if (adx='0') then
                NextState <= S0;
            else
                NextState <= S1;
            end if;

            when S1=>
                NextState <= S2;

            when S2=>
            if (m='1') then
                NextState<=S3;
            else if (m='0') then
                NextState<=S2;
            end if;
            
            when S3=>
                NextState <= S4;

            when S4=>
                NextState <= S0;

        end case;
    end if;
    end process NS_CS;

    OL: process (CurrentState)
    begin
        case CurrentState is
            when S0=>
            if (adx = '0') then
                adsh<='0';
                sh<='0';
                cm<='0';
                mdone<='0';
            else if (adx = '1') then
                if (m='1') then
                    adsh<='1';
                else if (m='0') then
                    sh<='1';
                end if;
            end if;
            when S1=>
            if (m='1') then
                adsh<='1';
            else if (m='0') then
                sh<='1';
            end if;
            when S2=>
            if (m='0') then
                adsh<='0';
                sh<='0';
                cm<='0';
                mdone<='0';
            else if (m='1') then
                adsh<='1';
            end if;
            when S3=>
            if (m='0') then
                sh='1';
            else if (m='1') then
                cm<='1';
                adsh<='1';
            end if;
            when S4=>
                mdone<='1';
        end case;

    end process OL;

end architecture ideal;

我尝试自己修复代码,但我无法弄清楚它有什么问题。

看下面的代码:

    if (m='1') then
      NextState<=S3;
    else if (m='0') then
      NextState<=S2;
    end if;

我想你的意思是 elsif 而不是 else if。或者,由于 m 是一个 std_logic,您可以将此块简化为:

    if (m='1') then
      NextState<=S3;
    else
      NextState<=S2;
    end if;

将您的 else if 替换为 elsif

在 VHDL 中,每个 if 需要一个 end if。如果你写

if ... then 
  ...
else if ... then

你需要两个 end if - 每个 if:

if ... then
  ...
else IF ... THEN
       ...
     END IF;
end if;

VHDL 有一个 elsif 语句。这不会启动新的 if 语句,而是它后面的 if 语句的一部分。如果把上面例子中的else IF替换掉,只需要一个end if:

if ... then
  ...
elsif ... then
  ...
end if;