在 VHDL 中实现 JK-Flip Flop 时出现问题

Problem while implementing JK-Flip Flop in VHDL

我正在尝试在 VHDL 中实现 JK 触发器,这是我的代码:

library ieee;
use ieee.std_logic_1164.all;

entity jk_flip_flop is
port(
    J, K     : in std_logic;
    clk      : in std_logic;
    Q, Q_bar : out std_logic
);
end jk_flip_flop;

architecture behavioral of jk_flip_flop is
begin
   process(J, K, clk)
        variable temp: std_logic;
   begin
        if (clk'event and clk='1') then
             temp:='1' when(J='1' AND K='0') else
                    '0' when(J='0' AND K='1') else
                    NOT temp when(J='1' AND K='1') else
                    temp when(J='0' AND K='0');
              Q <= temp;
              Q_bar <= NOT temp;
         end if;
    end process;
end behavioral;

我从中得到的错误是:

Error (10500): VHDL syntax error at jk_flip_flop.vhd(18) near text "when";  expecting ";"
Error (10500): VHDL syntax error at jk_flip_flop.vhd(18) near text "else";  expecting ":=", or "<="
Error (10500): VHDL syntax error at jk_flip_flop.vhd(19) near text "else";  expecting ":=", or "<="
Error (10500): VHDL syntax error at jk_flip_flop.vhd(20) near text "else";  expecting ":=", or "<="
Error (10500): VHDL syntax error at jk_flip_flop.vhd(21) near text ";";  expecting ":=", or "<="

这里到底出了什么问题?我是否不允许在进程中使用 when-else 或此处 when-else 语句的语法错误?

我也很困惑是否需要将 JK 传递到上面的 process 中,或者如果我只传递 clk,因为如果在时钟脉冲的上升沿产生新的输出就足够了。

因此您的 temp := value when ... else ... 语句仅在流程语句之外有效。所以你有三个选择。

选项 1

升级到VHDL 2008,也可以在进程中使用这种语句了。

选项 2

使用全局变量来存储下一个值...类似于:

architecture behavioral of jk_flip_flop is
   signal Q_val: std_logic;
   signal Q_next: std_logic;
begin
   Q_next <= '1' when(J='1' AND K='0') else
             '0' when(J='0' AND K='1') else
             NOT Q_val when(J='1' AND K='1') else
             Q_val;
   Q <= Q_next;
   Q_bar <= not Q_val;

   process(clk)
   begin
        if (clk'event and clk='1') then
             Q_val := Q_next;
         end if;
    end process;
end behavioral;

选项 3

使用 ifs 而不是 when。

architecture behavioral of jk_flip_flop is
begin
   process(J, K, clk)
        variable temp: std_logic;
   begin
        if (clk'event and clk='1') then
             if (J='1' and K='0') then
               temp := '1';
             elsif (J='0' AND K='1') then
               temp := '0';
             elsif (J='1' AND K='1') then
               temp := NOT temp;
             end if;
             Q <= temp;
             Q_bar <= NOT temp;
         end if;
    end process;
end behavioral;

我不确定这是否可行...我认为需要在体系结构级别定义 temp 才能使其具有内存...但我可能错了。

并且在敏感列表上...

Also I'm confused whether I need to pass J and K into the process above, or is it enough if I pass only clk, since it should be enough if the new output gets produced at rising edge of the clock pulse.

只需要传入clk即可