首页 > 解决方案 > 你知道如何让这段代码只生成 1 到 6 之间的数字,它生成 1 到 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;

标签: vhdl

解决方案


这是一个快速而肮脏的解决方案。有一些方法可以清理它并通过管道传输它。如果值为 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) 相同,您甚至可以将其放在进程之外。
  • 信号名称和实体名称相同。重命名以增加可读性。重新检查标准以查看是否允许。

推荐阅读