-----------------------------------------------------------------------------
---                                                        Defintions Package                
---                                                   Random Number Generator
-----------------------------------------------------------------------------
--- File Name:  RNGDefs_pack.vhd
---
--- Description:
--- This block is the definitions package for the Random Number Generator. 
--- 
--- Limitations:
---  None known.
---
--- Errors:
---  None known.
---
--- Dependancies:
---  None.
---
--- Current Target: <unknown>
--- Simulator:      Symphony EDA - Sonata
--- Target Builder: Latice ispExpert
---
--- Author: Peter Antoine       Date: 31 Jan 2004
----------------------------------------------------------------------------
---                                         Copyright (c) 2004 Peter Antoine
----------------------------------------------------------------------------
--- Version   Author Date        Changes
--- -------   ------ ----------  -------------------------------------------
--- 0.1       PA     31.01.2004  Initial revision
----------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

package definitions is
    ----------------------------------------------------
    --- Architecture Definitions
    ----------------------------------------------------
    constant data_width : natural := 32;

    ----------------------------------------------------
    --- Other constants
    ----------------------------------------------------
    constant ZERO_ARRAY : std_logic_vector (data_width-1 downto 0) := (others => '0');
    constant TEST_SEED  : std_logic_vector (data_width-1 downto 0) := "01100101110110100110010110011010";

    constant LFSR2_SEED : std_logic_vector (data_width-1 downto 0) := "01110101101101111010110011010110";
    constant LFSR3_SEED : std_logic_vector (data_width-1 downto 0) := "01001111010101110101011010110110";

    ----------------------------------------------------
    --- LOG 2 function
    ----------------------------------------------------

    function log2 ( a : natural ) return natural;


    ----------------------------------------------------
    --- Decrement
    ----------------------------------------------------

    function Decrement ( din : in std_logic_vector) return std_logic_vector;


    ----------------------------------------------------
    --- LFSR Feedback functions
    ----------------------------------------------------

    function LFSR_1_FeedbackFunction(   lfsr_data : in std_logic_vector(data_width-1 downto 0))
                                        return std_logic;

    function LFSR_2_FeedbackFunction(   lfsr_data : in std_logic_vector(data_width-1 downto 0))
                                        return std_logic;

    function LFSR_3_FeedbackFunction(   lfsr_data : in std_logic_vector(data_width-1 downto 0))
                                        return std_logic;

end package definitions;

package body definitions is

    ----------------------------------------------------
    --- LFSR 1 Feedback function
    --- This uses the following polynomial:
    --- 29,28,27,26,25,24,23,22,21,20,19,18,17,5,4,1
    ----------------------------------------------------
    function LFSR_1_FeedbackFunction(   lfsr_data : in std_logic_vector(data_width-1 downto 0))
                                        return std_logic is
    begin
        return (lfsr_data(28) xor lfsr_data(27) xor lfsr_data(26) xor lfsr_data(25) xor
                lfsr_data(24) xor lfsr_data(23) xor lfsr_data(22) xor lfsr_data(21) xor
                lfsr_data(20) xor lfsr_data(19) xor lfsr_data(18) xor lfsr_data(17) xor
                lfsr_data(16) xor lfsr_data(14) xor lfsr_data(3)  xor lfsr_data(0) xor '1');
    end;

    --------------------------------------------------------
    --- LFSR 2 Feedback function
    --- This uses the following polynomial:
    --- 30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,7,5
    --------------------------------------------------------
    function LFSR_2_FeedbackFunction(   lfsr_data : in std_logic_vector(data_width-1 downto 0))
                                        return std_logic is
    begin
        return (lfsr_data(29) xor lfsr_data(28) xor lfsr_data(27) xor lfsr_data(26) xor
                lfsr_data(25) xor lfsr_data(24) xor lfsr_data(23) xor lfsr_data(22) xor
                lfsr_data(21) xor lfsr_data(20) xor lfsr_data(19) xor lfsr_data(18) xor
                lfsr_data(17) xor lfsr_data(16) xor lfsr_data(15) xor lfsr_data(14) xor
                lfsr_data(15) xor lfsr_data( 7) xor lfsr_data( 4) xor '1');
    end;

    --------------------------------------------------------
    --- LFSR 3 Feedback function
    --- This uses the following polynomial:
    --- 30,29,26,25,22,21,18,17,14,13,12,10,9,8,6,5,4,3
    --------------------------------------------------------
    function LFSR_3_FeedbackFunction(   lfsr_data : in std_logic_vector(data_width-1 downto 0))
                                        return std_logic is
    begin
        return (lfsr_data(29) xor lfsr_data(28) xor lfsr_data(25) xor lfsr_data(24) xor
                lfsr_data(21) xor lfsr_data(20) xor lfsr_data(17) xor lfsr_data(16) xor
                lfsr_data(13) xor lfsr_data(12) xor lfsr_data(11) xor lfsr_data( 9) xor
                lfsr_data( 8) xor lfsr_data( 7) xor lfsr_data( 5) xor lfsr_data( 3) xor
                lfsr_data( 2) xor '1');
    end;


    --------------------------------------------------------
    --- Log2
    --- This function will return the rounded up log2 of the
    --- number passed in. It will return the min number of
    --- bits that a number needs to be realised in a bit
    --- array.
    --------------------------------------------------------

    function Log2 ( a : natural ) return natural is

        variable    num_bits : natural := 1;
        variable    count    : natural := 1;

    begin
        while ( count < a) loop
            count := count * 2;
            num_bits := num_bits + 1;
        end loop;

        return num_bits;
    end;

    --------------------------------------------------------
    --- Decrement
    --- This is a vary simple decrement function that will
    --- keep counting down every time it is called and will
    --- not stop.
    --------------------------------------------------------

    function Decrement ( din : std_logic_vector ) return std_logic_vector is

        variable x      : std_logic := '1';
        variable dout   : std_logic_vector (din'range);

    begin
        x := '0';

        for i in din'reverse_range loop

            dout(i) := din(i) xnor x;

            x := x or din(i);
        end loop;

        return dout;

    end function;


end definitions;

--- $Id: RNGDefs_pack.vhd,v 1.6 2004/08/22 09:41:24 hardware Exp hardware $
--- vi:nocin:sw=4 ts=4