0

我有这个用于 lifo 内存的代码,但我不明白为什么在第 27 行 (if(last = n-2) then full <= '1'; end if;)最后一个信号不等于 n-1。如果有人可以向我解释,我将不胜感激。

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity lifo is
  generic(n : natural := 4);
  port(Din         : in  std_logic_vector(3 downto 0);
       Dout        : out std_logic_vector(3 downto 0);
       wr          : in  std_logic;
       rd          : in  std_logic;
       empty, full : out std_logic;
       clk         : in  std_logic);
end entity lifo;

architecture arh of lifo is
  type memorie is array(0 to n-1) of std_logic_vector(3 downto 0);
  signal mem  : memorie := (others => (others => '0'));
  signal last : integer range -1 to n-1;
begin
  process(clk)
  begin
    if (rising_edge(clk)) and (wr = '1') then
      if (last = n-1) then null;
      else
        if(last = n-2) then full <= '1'; end if;
        if(last = -1) then empty <= '0'; end if;
        mem(last + 1)            <= Din;
        last                     <= last + 1;
      end if;
    elsif (rising_edge(clk)) and (rd = '1') then
      if(last = -1) then null;
      else
        Dout                     <= mem(last);
        last                     <= last - 1; full <= '0';
        if(last = -1) then empty <= '1'; end if;
      end if;
    end if;
  end process;
end architecture arh;
4

1 回答 1

1

lastrange -1 to n-1,当last为 n-1 时,则表示完全 LIFO,并且full必须为高 ( '1')。

当一个写入被接受时, thenlast增加 1 last <= last + 1。在同一上升沿clk确定是否full应该变高,如果此写入将使 LIFO 满,就是这种情况。写入后,则last值为 last+1(接受写入时的 +1),如果等于 n-1(n-1 表示已满),则 LIFO 已满。所以这次写入后full的条件是last+1=n-1,然后写为last = n-2

此外,如果代码不能立即工作,可以通过多种方式改进代码,例如single rising_edge(clk),添加reset,通过否定条件跳过null语句,在同一循环中添加写入和读取操作的处理,删除死代码(最终if)。

于 2014-05-31T18:12:21.053 回答