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

package typedef_package is
    
    --*****CONSTANT DECLARATIONS*****
    -- FIXED
    constant ADC_DATA_WIDTH : integer := 12;--*
    constant DAC_DATA_WIDTH : integer := 16;--*
    
    --* If these values are changed, the signal mappings in 'xillybus_link' have to be updated.
    
    constant ADC_TRANSFER_CLK_COUNT : integer := 16;
    constant ADC_DELAY_CLK_CNT      : integer := 2;
    constant DAC_TRANSFER_CLK_COUNT : integer := 16;
    
    constant MAX_DELAY      : integer := 200;
    constant DELAY_WIDTH    : integer := 8; --at least log2(MAX_DELAY)
    constant FACTOR_WIDTH   : integer := 5;
    
    constant TIMESTAMP_WIDTH : integer := 32;
    
    --XILLYBUS
    constant DEBUG_FIFO_DATA_WIDTH  : integer := 32;
    constant DEBUG_FIFO_DEPTH       : integer := 16;
    
    constant CONFIG_MEM_DATA_WIDTH  : integer := 32;
    constant CONFIG_DATA_WIDTH      : integer := CONFIG_MEM_DATA_WIDTH*2;
    constant CONFIG_MEM_ADDR_WIDTH  : integer := 15;
    
    
    --***CLOCKGEN SETTINGS***
    --100 MHz IN, 20 MHz OUT
    constant CLKFBOUT_MULT   : integer := 41;
    constant CLKIN1_PERIOD   : real := 10.000000;
    constant CLKOUT0_DIVIDE  : integer := 41;
    constant DIVCLK_DIVIDE   : integer := 5;
    
    constant SYNC_STAGES : integer := 2;
    
    constant DEBUG_SEND_INTERVAL : integer := 20000000;
    
    constant LED_WIDTH : integer := 4;
    
    --TODO: 3-stage sync to delay write-mode 1 clk cycle
    
    --*****FUNCTION DECLARATIONS*****
    function log2c(constant value : in integer) return integer;
    
end package;

package body typedef_package is
    
    
    --*****FUNCTION DEFINITIONS*****
    function log2c(constant value : in integer) return integer is
        variable ret_value : integer;
        variable cur_value : integer;
    begin
        ret_value := 0;
        cur_value := 1;
        while cur_value < value loop
            ret_value := ret_value + 1;
            cur_value := cur_value * 2;
        end loop;
        return ret_value;
    end function log2c;
    
    
end package body;
