xtxdg 发表于 2011-7-4 21:08:44

FPGA如何产生随机数

最近项目中要用FPGA产生随机数,但不知道如何产生,按照通信原理书上产生的随机数可以吗?

cpfdianzi 发表于 2011-7-4 23:53:40

可以的,弄一个m序列发生器就可以。

shawnelee88 发表于 2011-7-5 08:49:58

lfsr?

ssaweee 发表于 2011-7-5 08:56:40

或者外接一个混沌电路。

40130064 发表于 2011-7-5 09:26:09

Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;



entity divide_by_n is

   generic (data_width    : natural := 8 );

   port (

         data_in: inUNSIGNED(data_width - 1 downto 0) ;

         load   : instd_logic ;

         clk      : instd_logic ;

         reset    : instd_logic ;

         divide   : out std_logic

      );

end divide_by_n ;



architecture rtl of divide_by_n is

signal count_reg : UNSIGNED(data_width - 1 downto 0) ;

constant max_count : UNSIGNED(data_width - 1 downto 0) := (others => '1') ;

begin

cont_it :process(clk,reset)

       begin

          if (reset = '1') then

         count_reg <= (others => '0') ;

          elsif (clk = '1' and clk'event) then

            if (load = '1') then

               count_reg <= data_in ;

            else

                count_reg <=count_reg + "01" ;

            end if ;

          end if;

      end process ;

   divide <= '1' when count_reg = max_count else '0' ;

end RTL ;



Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;



entity dlatrg is

   generic (data_width    : natural := 16 );

   port (

         data_in: inUNSIGNED(data_width - 1 downto 0) ;

         clk      : instd_logic ;

         reset    : instd_logic ;

         data_out : out UNSIGNED(data_width - 1 downto 0)

      );

end dlatrg ;



architecture rtl of dlatrg is

begin

latch_it : process(data_in,clk,reset)

      begin

          if (reset = '1') then

            data_out <= (others => '0') ;

          elsif (clk = '1') then

            data_out <= data_in ;

          end if;

      end process ;

end RTL ;



Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;



entity lfsr is

   generic (data_width    : natural := 8 );

   port (

         clk      : instd_logic ;

         reset    : instd_logic ;

         data_out : out UNSIGNED(data_width - 1 downto 0)

      );

end lfsr ;



architecture rtl of lfsr is

signal feedback : std_logic ;

signal lfsr_reg : UNSIGNED(data_width - 1 downto 0) ;

begin

    feedback <= lfsr_reg(7) xor lfsr_reg(0) ;

latch_it :process(clk,reset)

       begin

          if (reset = '1') then

         lfsr_reg <= (others => '0') ;

          elsif (clk = '1' and clk'event) then

            lfsr_reg <= lfsr_reg(lfsr_reg'high - 1 downto 0) & feedback ;

          end if;

      end process ;

   data_out <= lfsr_reg ;

end RTL ;



Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;



entity priority_encoder is

   generic (data_width    : natural := 25 ;

            address_width : natural := 5 ) ;

   port (

         data    : inUNSIGNED(data_width - 1 downto 0) ;

         address : out UNSIGNED(address_width - 1 downto 0) ;

         none    : out STD_LOGIC

      );

end priority_encoder ;



architecture rtl of priority_encoder is

attribute SYNTHESIS_RETURN : STRING ;



FUNCTION to_stdlogic (arg1:BOOLEAN)RETURN STD_LOGIC IS

      BEGIN

      IF(arg1) THEN

      RETURN('1') ;

      ELSE

      RETURN('0') ;

      END IF ;

END ;



    function to_UNSIGNED(ARG: INTEGER; SIZE: INTEGER) return UNSIGNED is

   variable result: UNSIGNED(SIZE-1 downto 0);

   variable temp: integer;

      attribute SYNTHESIS_RETURN of result:variable is "FEED_THROUGH" ;

    begin

   temp := ARG;

   for i in 0 to SIZE-1 loop

         if (temp mod 2) = 1 then

         result(i) := '1';

         else

         result(i) := '0';

         end if;

         if temp > 0 then

         temp := temp / 2;

         else

         temp := (temp - 1) / 2;

         end if;

   end loop;

   return result;

    end;



constant zero : UNSIGNED(data_width downto 1) := (others => '0') ;

begin

PRIO :process(data)

         variable temp_address : UNSIGNED(address_width - 1 downto 0) ;

         begin

          temp_address := (others => '0') ;

          for i in data_width - 1 downto 0 loop

            if (data(i) = '1') then

            temp_address := to_unsigned(i,address_width) ;

            exit ;

            end if ;

          end loop ;

          address <= temp_address ;

          none <= to_stdlogic(data = zero) ;

      end process ;

end RTL ;



Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;

use IEEE.std_logic_unsigned.all ;



entity ram is

   generic (data_width    : natural := 8 ;

            address_width: natural := 8);

   port (

         data_in: inUNSIGNED(data_width - 1 downto 0) ;

         address: inUNSIGNED(address_width - 1 downto 0) ;

         we      : instd_logic ;

          clk   : in std_logic;

         data_out : out UNSIGNED(data_width - 1 downto 0)

      );

end ram ;



architecture rtl of ram is

type mem_type is array (2**address_width downto 0) of UNSIGNED(data_width - 1 downto 0) ;

signal mem : mem_type ;

signal addr_reg : unsigned (address_width -1 downto 0);



begin

    data_out <= mem(conv_integer(addr_reg)) ;

    I0 : process

      begin

       wait until clk'event and clk = '1';

      if (we = '1') then

          mem(conv_integer(address)) <= data_in ;

      end if ;

         addr_reg <= address;

    end process ;

end RTL ;



Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;



entity tbuf is

   generic (data_width    : natural := 16 );

   port (

         data_in: inUNSIGNED(data_width - 1 downto 0) ;

         en       : instd_logic ;

         data_out : out UNSIGNED(data_width - 1 downto 0)

      );

end tbuf ;



architecture rtl of tbuf is

begin

three_state :process(data_in,en)

      begin

          if (en = '1') then

            data_out <=data_in ;

          else

            data_out <= (others => 'Z') ;

          end if;

      end process ;

end RTL ;



Library IEEE ;

use IEEE.std_logic_1164.all ;

use IEEE.std_logic_arith.all ;



entity pseudorandom is

   generic (data_width    : natural := 8 );

   port (

         seed   : inUNSIGNED (24 downto 0) ;

         init   : inUNSIGNED (4 downto 0) ;

         load   : instd_logic ;

         clk    : instd_logic ;

         reset: instd_logic ;

         read   : instd_logic ;

         write: instd_logic ;

         rand   : out UNSIGNED (7 downto 0) ;

         none   : out std_logic

      );

end pseudorandom ;


architecture rtl of pseudorandom is

signal latch_seed : UNSIGNED(24 downto 0) ;

signal encoder_address : UNSIGNED(4 downto 0) ;

signal random_data : UNSIGNED(7 downto 0) ;

signal write_enable : std_logic ;

signal ram_data : UNSIGNED(7 downto 0) ;

begin

    I0 : entity work.dlatrg(rtl)

          generic map (25)

          port map (seed,read,reset,latch_seed) ;

    I1 : entity work.priority_encoder(rtl)

          generic map (25,5)

          port map (latch_seed,encoder_address,none) ;

    I2 : entity work.ram(rtl)

          generic map (8,5)

          port map (random_data,encoder_address,write_enable,clk,ram_data) ;

    I3 : entity work.tbuf(rtl)

          generic map (8)

          port map (ram_data,write,rand) ;

    I4 : entity work.lfsr(rtl)

          generic map (8)

          port map (clk,reset,random_data) ;

   I5 : entity work.divide_by_n(rtl)

          generic map (5)

          port map (init,load,clk,reset,write_enable) ;

end rtl ;

NJ8888 发表于 2011-7-5 10:07:00

IP核里有个线性反馈移位寄存器可以用来产生伪随机序列

zgq800712 发表于 2011-7-8 23:37:01

伪随机的呢

xtxdg 发表于 2011-10-13 19:16:21

谢谢各位啊 自己编verilog代码解决了啊

linxiaoyu275 发表于 2011-10-14 19:41:57

回复【7楼】xtxdg
-----------------------------------------------------------------------

发来共享下谢谢哈

wy2000 发表于 2011-10-14 21:13:50

看了变成混沌状态了./emotion/em044.gif

dellric 发表于 2011-10-14 23:10:42

楼主可能不是要的伪随机数生成器

xbaby123 发表于 2011-10-18 16:03:53

可以通过多个环形振荡器的输出组合来产生真正的随机数

hellofpga 发表于 2011-10-18 16:30:48

m序列???

whxiaowang 发表于 2017-1-18 00:08:14

xbaby123 发表于 2011-10-18 16:03
可以通过多个环形振荡器的输出组合来产生真正的随机数

有尝试过吗?

湛无双 发表于 2017-1-24 21:31:39

whxiaowang 发表于 2017-1-18 00:08
有尝试过吗?

有,混沌电路。做出来了,有相应的文献可以参考,包括原理。

yf869778412 发表于 2017-2-7 09:43:30

学习了几个新名词{:smile:}
页: [1]
查看完整版本: FPGA如何产生随机数