---------------------------------------------------------------------
-- Atlys ITSDAQ Specific Declarations
--
---------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

library utils;
use utils.pkg_types.all;

library hsio;
use hsio.pkg_core_globals.all;


package pkg_atlys_itsdaq is


-- type dm_modes is (Driver, MB_H35);




  -- ================================================================================
  -- Build Details
  -- ================================================================================

  constant C_FW_BUILD_NO : integer := 16#a065#;

  constant C_TDC_EN : integer := 0; --not used.  about to be removed

  constant PTYP_NONE : integer := 0;
  constant PTYP_TLU : integer := 1;
  constant PTYP_TTC  : integer := 2;

  constant C_PMOD_TYPE : integer := PTYP_TTC;

  constant C_TLU_EN : integer := C_PMOD_TYPE; -- this isn't ideal


-- HV Strip V1
-- ===========
--constant C_RO_TYPE  : integer := RTYP_HVCSV1;
--constant C_DIO_TYPE : integer := DIO_HVCSV1;
--constant C_MOD_TYPE : slv36 := x"3ffffffff";  -- 0: ABCN, 1: ABC130/CMOS

--constant C_LINKS_EN : slv16   := "1110000000001111";  -- MB H35

-- ABCN
-- ====
--   constant C_RO_TYPE : integer  := RTYP_ABCN;
--   constant C_DIO_TYPE : integer  := DIO_ABCN;
--   constant C_MOD_TYPE : slv36 := x"000000000";  -- 0: ABCN, 1: ABC130/CMOS
--   constant C_LINKS_EN : slv16 := "0000000000000011";


-- IDC ASICS130
-- ============
   constant C_RO_TYPE : integer  := RTYP_130;
   constant C_DIO_TYPE : integer  := DIO_IDC;
   constant C_MOD_TYPE : slv36 := x"3ffffffff";  -- 0: ABCN, 1: ABC130/CMOS
   constant C_LINKS_EN : slv16 := "0000000000000011"; -- IDC16
-- constant C_LINKS_EN : slv16 := "0000000011111111"; -- IDC28!
-- Order:                          fedcba9876543210

-- DRV (ASICS130)
-- ============
--   constant C_RO_TYPE : integer  := RTYP_130;
--   constant C_DIO_TYPE : integer  := DIO_DRV;  
--   constant C_MOD_TYPE : slv36 := x"3ffffffff";  -- 0: ABCN, 1: ABC130/CMOS
--   constant C_LINKS_EN : slv16 := "1000010010010010"; -- Driver
-- Order:                          fedcba9876543210
-- Driver uses links    1  2  7 10 (+ 15 and 20 for TMU)
-- Driver uses streams  2  8 14 20 (+ 30 and 40 for TMU)


--constant C_LINKS_EN : slv16 := "1111111111111111";

-- Concise format, each bit corrosponds to 4 streams (1 module) worth of inputs
  -- Thus bit 0 = streams 0-3, bit 1 = streams 4-7 etc.

  --nib streams               streams per bit
  --=== =======  =========================================
  -- 1   31-16   |  31-28  |  27-24  |  23-20  |  19-16  | 
  -- 0   15-0    |  15-12  |  11-8   |   7-4   |   3-0   | 


  -- HSIO style enables - mostly unused and constant on Atlys

  constant C_MOD_RAW : slv36 := x"0000000" &
                                (C_LINKS_EN(15) or C_LINKS_EN(14)) &
                                (C_LINKS_EN(13) or C_LINKS_EN(12)) &
                                (C_LINKS_EN(11) or C_LINKS_EN(10)) &
                                (C_LINKS_EN(9) or C_LINKS_EN(8)) &
                                (C_LINKS_EN(7) or C_LINKS_EN(6)) &
                                (C_LINKS_EN(5) or C_LINKS_EN(4)) &
                                (C_LINKS_EN(3) or C_LINKS_EN(2)) &
                                (C_LINKS_EN(1) or C_LINKS_EN(0));

  constant C_MOD_HST  : slv36 := x"000000000";  -- max 0x30fff0fff
  constant C_MOD_IDBG : slv36 := x"000000000";  -- DEBUG - skips IDELAY for internal e.g. ABC-TESTING
  constant C_MOD_PRES : slv36 := (C_MOD_RAW or C_MOD_HST);




--==============================================================================
-- VMOD Mapping (differential)
--==============================================================================

  constant vIO1   : integer := 1;
  constant vIO2   : integer := 2;
  constant vIO3   : integer := 3;
  constant vIO4   : integer := 4;
  constant vIO5   : integer := 5;
  constant vIO6   : integer := 6;
  constant vIO7   : integer := 7;
  constant vIO8   : integer := 8;
  constant vIO9   : integer := 9;
  constant vCLK10 : integer := 10;
  constant vCLK11 : integer := 11;
  constant vIO12  : integer := 12;
  constant vIO13  : integer := 13;
  constant vIO14  : integer := 14;
  constant vIO15  : integer := 15;
  constant vIO16  : integer := 16;
  constant vIO17  : integer := 17;
  constant vIO18  : integer := 18;
  constant vIO19  : integer := 19;
  constant vIO20  : integer := 20;

--==============================================================================
-- VMOD Mapping - Single-ended
--==============================================================================
  --                                -- vhdci pin
  constant vIO1p   : integer := 21;     -- 1;  
  constant vIO2p   : integer := 23;     -- 3;  
  constant vIO3p   : integer := 25;     -- 4;  
  constant vIO4p   : integer := 27;     -- 6;  
  constant vIO5p   : integer := 29;     -- 7;  
  constant vIO6p   : integer := 31;     -- 9;  
  constant vIO7p   : integer := 33;     -- 10;  
  constant vIO8p   : integer := 35;     -- 12;  
  constant vIO9p   : integer := 37;     -- 13;  
  constant vCLK10p : integer := 39;     -- 15;  
  constant vCLK11p : integer := 41;     -- 20;  
  constant vIO12p  : integer := 43;     -- 22;  
  constant vIO13p  : integer := 45;     -- 23;  
  constant vIO14p  : integer := 47;     -- 25;  
  constant vIO15p  : integer := 49;     -- 26;  
  constant vIO16p  : integer := 51;     -- 28;  
  constant vIO17p  : integer := 53;     -- 29;  
  constant vIO18p  : integer := 55;     -- 31;  
  constant vIO19p  : integer := 57;     -- 32;  
  constant vIO20p  : integer := 59;     -- 34;  

  constant vIO1n   : integer := 22;     -- 35;  
  constant vIO2n   : integer := 24;     -- 37;  
  constant vIO3n   : integer := 26;     -- 38;  
  constant vIO4n   : integer := 28;     -- 40;  
  constant vIO5n   : integer := 30;     -- 41;  
  constant vIO6n   : integer := 32;     -- 43;  
  constant vIO7n   : integer := 34;     -- 44;  
  constant vIO8n   : integer := 36;     -- 46;  
  constant vIO9n   : integer := 38;     -- 47;  
  constant vCLK10n : integer := 40;     -- 49;  
  constant vCLK11n : integer := 42;     -- 54;  
  constant vIO12n  : integer := 44;     -- 56;  
  constant vIO13n  : integer := 46;     -- 57;  
  constant vIO14n  : integer := 48;     -- 59;  
  constant vIO15n  : integer := 50;     -- 60;  
  constant vIO16n  : integer := 52;     -- 62;  
  constant vIO17n  : integer := 54;     -- 63;  
  constant vIO18n  : integer := 56;     -- 65;  
  constant vIO19n  : integer := 58;     -- 66;  
  constant vIO20n  : integer := 60;     -- 68;  




--==============================================================================
-- VMOD-IB Mapping
--==============================================================================

  constant vibCMD : integer := 1;
  constant vibL1R : integer := 2;
  constant vibD0  : integer := 3;
  constant vibD1  : integer := 4;
  constant vibD2  : integer := 5;
  constant vibD3  : integer := 6;
  constant vibD4  : integer := 7;
  constant vibD5  : integer := 8;
  constant vibD6  : integer := 9;
  constant vibBCO : integer := 10;
  constant vibDRC : integer := 11;
  constant vibD7  : integer := 12;
  constant vibD8  : integer := 13;
  constant vibD9  : integer := 14;
  constant vibD10 : integer := 15;
  constant vibD11 : integer := 16;
  constant vibD12 : integer := 17;
  constant vibD13 : integer := 18;
  constant vibD14 : integer := 19;
  constant vibD15 : integer := 20;

  constant vibRST  : integer := 5; -- Also reset for ABCN

  --==============================================================================
-- VMOD-Motherboard Mapping
--==============================================================================

  -- This idea is depricated ;-(


  ---- Differential
  --constant vmbIN1    : integer := vIO1;    -- (out) (HSIO COML0) 
  --constant vmbIN2    : integer := vIO2;    -- (out) (HSIO L1R3)
  --constant vmbOUT1   : integer := vIO3;    -- in 
  --constant vmbOUT2   : integer := vIO4;    -- in
  --constant vmbOUT3   : integer := vIO5;    -- (in)
  --constant vmbOUT4   : integer := vIO6;    -- (in)
  ----
  --constant vmbCKFAST : integer := vCLK10;  -- out
  --constant vmbCK     : integer := vCLK11;  -- out

  ---- Single-ended
  --constant vmbPL332     : integer := vIO7p;   -- (in) 
  --constant vmbPL342     : integer := vIO7n;   -- (in) 
  --constant vmbPL341     : integer := vIO8p;   -- (in) 
  --constant vmbPL331     : integer := vIO8n;   -- (in) 
  --constant vmbPL351     : integer := vIO9p;   -- (in) 
  --constant vmbPL352     : integer := vIO9n;   -- (in)
  ----
  --constant vmbPL371     : integer := vIO12p;  -- (in) 
  --constant vmbPL372     : integer := vIO12n;  -- (in) 
  --constant vmbPL373     : integer := vIO13p;  -- (in) 
  --constant vmbHBNORCMOS : integer := vIO13n;  -- in  
  --constant vmbHBCMOS    : integer := vIO14p;  -- in  
  --constant vmbSEROUT    : integer := vIO14n;  -- in
  --constant vmbPL391     : integer := vIO15p;  -- (out)
  --constant vmbPL392     : integer := vIO15n;  -- (out)
  --constant vmbPL401     : integer := vIO16p;  -- (out)
  --constant vmbPL402     : integer := vIO16n;  -- (out)
  --constant vmbSHIFTENB  : integer := vIO17p;  -- out
  --constant vmbCK1       : integer := vIO17n;  -- out
  --constant vmbCK2       : integer := vIO18p;  -- out
  --constant vmbLOADREG   : integer := vIO18n;  -- out
  --constant vmbSIN       : integer := vIO19p;  -- out
  --constant vmbDIGINJ    : integer := vIO19n;  -- out
  --constant vmbSK7       : integer := vIO20p;  -- (out)
  --constant vmbSK15      : integer := vIO20n;  -- (out)




  --Single ended


  function dummy_for_printing_version_in_xst (
    dumdum : std_logic_vector(15 downto 0)
    )
    return std_logic_vector;

end pkg_atlys_itsdaq;



--=========================================================================================
--
-- B O D Y
--
--=========================================================================================

package body pkg_atlys_itsdaq is

  function dummy_for_printing_version_in_xst (
    dumdum : std_logic_vector(15 downto 0)
    )
    return std_logic_vector is
  begin

    report "Version:" & to_hstring(conv_std_logic_vector(C_FW_BUILD_NO,16));
    return dumdum;
  end function dummy_for_printing_version_in_xst;
  end pkg_atlys_itsdaq;


















-- Template? -- This idea is depricated ;-(

-- Outputs
--v_pio(vIO1) <= vo(vIO1p);
--v_nio(vIO1) <= vo(vIO1n);
--v_pio(vIO2) <= vo(vIO2p);
--v_nio(vIO2) <= vo(vIO2n);
--v_pio(vIO3) <= vo(vIO3p);
--v_nio(vIO3) <= vo(vIO3n);
--v_pio(vIO4) <= vo(vIO4p);
--v_nio(vIO4) <= vo(vIO4n);
--v_pio(vIO5) <= vo(vIO5p);
--v_nio(vIO5) <= vo(vIO5n);
--v_pio(vIO6) <= vo(vIO6p);
--v_nio(vIO6) <= vo(vIO6n);
--v_pio(vIO7) <= vo(vIO7p);
--v_nio(vIO7) <= vo(vIO7n);
--v_pio(vIO8) <= vo(vIO8p);
--v_nio(vIO8) <= vo(vIO8n);
--v_pio(vIO9) <= vo(vIO9p);
--v_nio(vIO9) <= vo(vIO9n);
--v_pio(vCLK10) <= vo(vCLK10p);
--v_nio(vCLK10) <= vo(vCLK10n);
--v_pio(vCLK11) <= vo(vCLK11p);
--v_nio(vCLK11) <= vo(vCLK11n);
--v_pio(vIO12) <= vo(vIO12p);
--v_nio(vIO12) <= vo(vIO12n);
--v_pio(vIO13) <= vo(vIO13p);
--v_nio(vIO13) <= vo(vIO13n);
--v_pio(vIO14) <= vo(vIO14p);
--v_nio(vIO14) <= vo(vIO14n);
--v_pio(vIO15) <= vo(vIO15p);
--v_nio(vIO15) <= vo(vIO15n);
--v_pio(vIO16) <= vo(vIO16p);
--v_nio(vIO16) <= vo(vIO16n);
--v_pio(vIO17) <= vo(vIO17p);
--v_nio(vIO17) <= vo(vIO17n);
--v_pio(vIO18) <= vo(vIO18p);
--v_nio(vIO18) <= vo(vIO18n);
--v_pio(vIO19) <= vo(vIO19p);
--v_nio(vIO19) <= vo(vIO19n);
--v_pio(vIO20) <= vo(vIO20p);
--v_nio(vIO20) <= vo(vIO20n);

-- Inputs
--vi(vIO1p) <= v_pio(vIO1p);
--vi(vIO1n) <= v_nio(vIO1n);
--vi(vIO2p) <= v_pio(vIO2p);
--vi(vIO2n) <= v_nio(vIO2n);
--vi(vIO3p) <= v_pio(vIO3p);
--vi(vIO3n) <= v_nio(vIO3n);
--vi(vIO4p) <= v_pio(vIO4p);
--vi(vIO4n) <= v_nio(vIO4n);
--vi(vIO5p) <= v_pio(vIO5p);
--vi(vIO5n) <= v_nio(vIO5n);
--vi(vIO6p) <= v_pio(vIO6p);
--vi(vIO6n) <= v_nio(vIO6n);
--vi(vIO7p) <= v_pio(vIO7p);
--vi(vIO7n) <= v_nio(vIO7n);
--vi(vIO8p) <= v_pio(vIO8p);
--vi(vIO8n) <= v_nio(vIO8n);
--vi(vIO9p) <= v_pio(vIO9p);
--vi(vIO9n) <= v_nio(vIO9n);
--vi(vCLK10p) <= v_pio(vCLK10p);
--vi(vCLK10n) <= v_nio(vCLK10n);
--vi(vCLK11p) <= v_pio(vCLK11p);
--vi(vCLK11n) <= v_nio(vCLK11n);
--vi(vIO12p) <= v_pio(vIO12p);
--vi(vIO12n) <= v_nio(vIO12n);
--vi(vIO13p) <= v_pio(vIO13p);
--vi(vIO13n) <= v_nio(vIO13n);
--vi(vIO14p) <= v_pio(vIO14p);
--vi(vIO14n) <= v_nio(vIO14n);
--vi(vIO15p) <= v_pio(vIO15p);
--vi(vIO15n) <= v_nio(vIO15n);
--vi(vIO16p) <= v_pio(vIO16p);
--vi(vIO16n) <= v_nio(vIO16n);
--vi(vIO17p) <= v_pio(vIO17p);
--vi(vIO17n) <= v_nio(vIO17n);
--vi(vIO18p) <= v_pio(vIO18p);
--vi(vIO18n) <= v_nio(vIO18n);
--vi(vIO19p) <= v_pio(vIO19p);
--vi(vIO19n) <= v_nio(vIO19n);
--vi(vIO20p) <= v_pio(vIO20p);
--vi(vIO20n) <= v_nio(vIO20n);