你知道我如何让这段代码只生成 1 到 6 之间的数字吗,它生成 1 到 7 之间的数字

Do you have any idea how I can make this code generate numbers only between 1 and 6, it generates between 1 and 7

entity LFSR is
  Port (   clk : in STD_LOGIC; 
           en: in STD_LOGIC; 
           reset: in STD_LOGIC;  
           output_lfsr: out STD_LOGIC_VECTOR(2 downto 0) 
           ); 
end LFSR;

architecture Behavioral of LFSR is
    signal lfsr : std_logic_vector(2 downto 0); 
    constant poly : std_logic_vector(2 downto 0) := "011"; 
    begin 
        process(clk, reset, lfsr)  
        variable ext_inbit: std_logic_vector(2 downto 0);
        variable inbit: std_logic;  
    begin 
        inbit:=lfsr(2);   -- preia valoarea iesirii Q2 
            for i in 0 to 2 loop    
        ext_inbit(i):=inbit; 
            end loop; 
        if (reset='1') then   
          lfsr<="111";
        elsif (clk'event and clk='1') then 
            if(en='1') then   
                   lfsr<=(lfsr(1 downto 0) & '0') xor (ext_inbit and poly);  
                                 
            end if; 
        end if; 
    end process; 
    output_lfsr <= lfsr;   
end Behavioral;

这是一个快速而肮脏的解决方案。有多种方法可以对其进行清理和流水线处理。 如果值为 7,请再次执行操作。

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity lfsr is
  port (clk         : in  std_logic;
        en          : in  std_logic;
        reset       : in  std_logic;
        output_lfsr : out std_logic_vector(2 downto 0)
        );
end lfsr;

architecture behavioral of lfsr is

  signal s_lfsr     : std_logic_vector(2 downto 0);
  constant poly     : std_logic_vector(2 downto 0) := "011";

begin
  process(clk, reset)
    variable v_lfsr_int : std_logic_vector(2 downto 0);
    variable ext_inbit : std_logic_vector(2 downto 0);
    variable inbit     : std_logic;
  begin

    if (reset = '1') then
      s_lfsr <= "011";
    elsif (rising_edge(clk)) then
    
      inbit := s_lfsr(2);               -- preia valoarea iesirii Q2
      for i in 0 to 2 loop
        ext_inbit(i) := inbit;
      end loop;
      
      if(en = '1') then
        v_lfsr_int := (s_lfsr(1 downto 0) & '0') xor (ext_inbit and poly);
        if(v_lfsr_int = "111") then
          s_lfsr <= (v_lfsr_int(1 downto 0) & '0') xor (ext_inbit and poly);
        else
          s_lfsr <= v_lfsr_int;
        end if;
      end if;
    end if;

  end process;

  output_lfsr <= s_lfsr;

end behavioral;

如您所见,我也更改了一些内容:

  • 添加了 ieee 库
  • 为异步重置更新了进程敏感度列表
  • 重新排列了 ext_inbit 以避免工具大喊敏感度列表不完整。考虑到细化后的值与lfsr(2)相同,你甚至可以把它放在进程之外。
  • 信号名称和实体名称相同。重命名以增加可读性。重新检查标准,看是否允许。