-- (c) Copyright 2008 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
--

--
--  RX_AURORA_LANE_SIMPLEX_GTX_4BYTE
--
--
--  Description: the RX_AURORA_LANE_SIMPLEX_4BYTE module provides a simplex
--               4-byte aurora lane connection using a single GTX.  The
--               module handles lane initialization, symbol decoding and
--               error detection.  It also decodes  some of the channel
--               bonding indicator signals needed by the Global logic.
--
--               * Supports Virtex-5
--

library IEEE;
use IEEE.STD_LOGIC_1164.all;
use WORK.AURORA_PKG.all;

entity aurora_1ln_rx_lpm_RX_AURORA_LANE_SIMPLEX_GTX_4BYTE is
    generic (
            EXAMPLE_SIMULATION       : integer :=  0     
         );
    port (

    -- GTX Interface

            RX_DATA             : in  std_logic_vector(31 downto 0);    -- 4-byte data bus from the GTX.
            RX_NOT_IN_TABLE     : in  std_logic_vector(3 downto 0);     -- Invalid 10-bit code was recieved.
            RX_DISP_ERR         : in  std_logic_vector(3 downto 0);     -- Disparity error detected on RX interface.
            RX_CHAR_IS_K        : in  std_logic_vector(3 downto 0);     -- Indicates which bytes of RX_DATA are control.
            RX_CHAR_IS_COMMA    : in  std_logic_vector(3 downto 0);     -- Comma received on given byte.
            RX_STATUS           : in  std_logic_vector(5 downto 0);     -- Part of GT_11 status and error bus
            RX_BUF_ERR          : in  std_logic;                        -- Part of GT_11 status and error bus
            RX_REALIGN          : in  std_logic;                        -- SERDES was realigned because of a new comma.
            RX_POLARITY         : out std_logic;                        -- Controls interpreted polarity of serial data inputs.
            GTX_RX_RESET        : out std_logic;                        -- Reset RX side of GTX logic.
            LINK_RESET_OUT    : out std_logic;                     -- Link reset for hotplug scenerio.
            HPCNT_RESET       : in  std_logic;                     -- Hotplug count reset input.
            INIT_CLK          : in  std_logic;

    -- Comma Detect Phase Align Interface

            ENA_COMMA_ALIGN     : out std_logic;                        -- Request comma alignment.

    -- RX_LL Interface

            RX_PAD              : out std_logic_vector(0 to 1);         -- Indicates lane received PAD.
            RX_PE_DATA          : out std_logic_vector(0 to 31);        -- RX data from lane to RX_LL.
            RX_PE_DATA_V        : out std_logic_vector(0 to 1);         -- RX_PE_DATA is data, not control symbol.
            RX_SCP              : out std_logic_vector(0 to 1);         -- Indicates lane received SCP.
            RX_ECP              : out std_logic_vector(0 to 1);         -- Indicates lane received ECP
            RX_SUF              : out std_logic_vector(0 to 1);         -- Indicates lane received SUF
            RX_FC_NB            : out std_logic_vector(0 to 7);         -- Size code for SNF or SUF

    -- Global Logic Interface

            LANE_UP             : out std_logic;                        -- Lane is ready for bonding and verification.
            CHANNEL_UP          : in std_logic;
            SOFT_ERR          : out std_logic_vector(0 to 1);         -- Soft error detected.
            HARD_ERR          : out std_logic;                        -- Hard error detected.
            CHANNEL_BOND_LOAD   : out std_logic;                        -- Channel Bongding done code recieved.
            GOT_A               : out std_logic_vector(0 to 3);         -- Indicates lane recieved 'A character' bytes.
            GOT_V               : out std_logic;                        -- Verification symbols received.

    -- System Interface

            USER_CLK            : in  std_logic;                        -- System clock for all non-GTX Aurora Logic.
            RESET               : in  std_logic                         -- Reset the lane.
         );

end aurora_1ln_rx_lpm_RX_AURORA_LANE_SIMPLEX_GTX_4BYTE;

architecture RTL of aurora_1ln_rx_lpm_RX_AURORA_LANE_SIMPLEX_GTX_4BYTE is
-- Wire Declarations --

    signal rx_cc_i                  : std_logic;
    signal  ena_comma_align_i     : std_logic;
    signal  rx_sp_i               : std_logic;
    signal  rx_spa_i              : std_logic;
    signal  rx_neg_i              : std_logic;
    signal  enable_err_detect_i : std_logic;
    signal  do_word_align_i       : std_logic;
    signal  hard_err_reset_i    : std_logic;

    signal  rx_data_i             : std_logic_vector(31 downto 0);
    signal  rx_char_is_k_i        : std_logic_vector(3 downto 0);
    signal  rx_char_is_comma_i    : std_logic_vector(3 downto 0);
    signal  rx_disp_err_i         : std_logic_vector(3 downto 0);
    signal  rx_not_in_table_i     : std_logic_vector(3 downto 0);

    signal  LANE_UP_Buffer        : std_logic;


-- Component Declarations --

    component aurora_1ln_rx_lpm_RX_LANE_INIT_SM_SIMPLEX_4BYTE

        port (

        -- GTX Interface

                RX_NOT_IN_TABLE     : in std_logic_vector(3 downto 0); -- GTX received invalid 10b code
                RX_DISP_ERR         : in std_logic_vector(3 downto 0); -- GTX received 10b code w/ wrong disparity
                RX_CHAR_IS_COMMA    : in std_logic_vector(3 downto 0); -- GTX received a Comma
                RX_REALIGN          : in std_logic;                    -- GTX had to change alignment due to new comma
                GTX_RX_RESET        : out std_logic;                   -- Reset the RX side of the GTX
                RX_POLARITY         : out std_logic;                   -- Sets polarity used to interpet rx'ed symbols

        -- Comma Detect Phase Alignment Interface

                ENA_COMMA_ALIGN     : out std_logic;                   -- Turn on SERDES Alignment in GTX

        -- Symbol Decoder Interface

                RX_SP               : in std_logic;                    -- Lane rx'ed SP sequence w/ + or - data
                RX_NEG              : in std_logic;                    -- Lane rx'ed inverted SP or SPA data
                DO_WORD_ALIGN       : out std_logic;                   -- Enable word alignment

        -- Error Detection Logic Interface

                ENABLE_ERR_DETECT : out std_logic;                   -- Turn on Soft Error detection
                HARD_ERR_RESET    : in std_logic;                    -- Reset lane due to hard error

        -- Global Logic Interface

                LANE_UP             : out std_logic;                   -- Lane is initialized
                CHANNEL_UP          : in  std_logic;                    -- Channels are bonded and verified

        -- System Interface

                USER_CLK            : in std_logic;                    -- Clock for all non-GTX Aurora logic
                RESET               : in std_logic                     -- Reset Aurora Lane
             );

    end component;


    component aurora_1ln_rx_lpm_CHBOND_COUNT_DEC_4BYTE

        port (

                RX_STATUS         : in std_logic_vector(5 downto 0);
                CHANNEL_BOND_LOAD : out std_logic;
                USER_CLK          : in std_logic

             );

    end component;


    component aurora_1ln_rx_lpm_SYM_DEC_4BYTE

        port (

        -- RX_LL Interface

                RX_PAD           : out std_logic_vector(0 to 1);     -- LSByte is PAD.
                RX_PE_DATA       : out std_logic_vector(0 to 31);    -- Word aligned data from channel partner.
                RX_PE_DATA_V     : out std_logic_vector(0 to 1);     -- Data is valid data and not a control character.
                RX_SCP           : out std_logic_vector(0 to 1);     -- SCP symbol received.
                RX_ECP           : out std_logic_vector(0 to 1);     -- ECP symbol received.
                RX_SUF           : out std_logic_vector(0 to 1);     -- SUF symbol reveived.
                RX_FC_NB         : out std_logic_vector(0 to 7);     -- Flow Control size code.  Valid with RX_SNF or RX_SUF.

        -- Lane Init SM Interface

                DO_WORD_ALIGN    : in std_logic;                     -- Word alignment is allowed.
                LANE_UP          : in std_logic;
                RX_SP            : out std_logic;                    -- SP sequence received with positive or negative data.
                RX_SPA           : out std_logic;                    -- SPA sequence received.
                RX_NEG           : out std_logic;                    -- Inverted data for SP or SPA received.

        -- Global Logic Interface

                GOT_A            : out std_logic_vector(0 to 3);     -- A character received on indicated byte(s).
                GOT_V            : out std_logic;                    -- V sequence received.

        -- GTX Interface

                RX_DATA          : in std_logic_vector(31 downto 0); -- Raw RX data from GTX.
                RX_CHAR_IS_K     : in std_logic_vector(3 downto 0);  -- Bits indicating which bytes are control characters.
                RX_CHAR_IS_COMMA : in std_logic_vector(3 downto 0);  -- Rx'ed a comma.
                RX_CC            : out std_logic;                    -- CC sequence received.

        -- System Interface

                USER_CLK         : in std_logic;                     -- System clock for all non-GTX Aurora Logic.
                RESET            : in std_logic

             );

    end component;


    component aurora_1ln_rx_lpm_RX_ERR_DETECT_SIMPLEX_GTX_4BYTE is

        port (

        -- Lane Init SM Interface

                ENABLE_ERR_DETECT    : in  std_logic;
                HARD_ERR_RESET       : out std_logic;

        -- Global Logic Interface

                SOFT_ERR             : out std_logic_vector(0 to 1);
                HARD_ERR             : out std_logic;

        -- GTX Interface

                RX_DISP_ERR             : in  std_logic_vector(3 downto 0);
                RX_NOT_IN_TABLE         : in  std_logic_vector(3 downto 0);
                RX_BUF_ERR              : in  std_logic;
                RX_REALIGN              : in  std_logic;

        -- System Interface

                USER_CLK                : in std_logic

             );

    end component;

component aurora_1ln_rx_lpm_HOTPLUG
generic
(
  ENABLE_HOTPLUG          : integer :=   1;
  EXAMPLE_SIMULATION       : integer :=  0     
);
port
(

  ---------------------- Sym Dec Interface -------------------------------
  RX_CC               : in    std_logic;
  RX_SP               : in    std_logic;
  RX_SPA              : in    std_logic;

  ----------------------  GT Wrapper Interface ----------------------------
  LINK_RESET_OUT      : out   std_logic;
  HPCNT_RESET         : in    std_logic;

  ----------------------  System Interface ----------------------------
  INIT_CLK            : in    std_logic;
  USER_CLK            : in    std_logic;
  RESET               : in    std_logic

);
end component;


begin

-- Main Body of Code --


    -- Buffers for twisting data from GTX --
    -- Pro Aurora logic, we twist the data to make it compatible.

    rx_data_i           <=  RX_DATA(7 downto 0) & RX_DATA(15 downto 8) & RX_DATA(23 downto 16) & RX_DATA(31 downto 24);
    rx_char_is_k_i      <=  RX_CHAR_IS_K(0) & RX_CHAR_IS_K(1) & RX_CHAR_IS_K(2) & RX_CHAR_IS_K(3);
    rx_char_is_comma_i  <=  RX_CHAR_IS_COMMA(0) & RX_CHAR_IS_COMMA(1) & RX_CHAR_IS_COMMA(2) & RX_CHAR_IS_COMMA(3);
    rx_disp_err_i       <=  RX_DISP_ERR(0) & RX_DISP_ERR(1) & RX_DISP_ERR(2) & RX_DISP_ERR(3);
    rx_not_in_table_i   <=  RX_NOT_IN_TABLE(0) & RX_NOT_IN_TABLE(1) & RX_NOT_IN_TABLE(2) & RX_NOT_IN_TABLE(3);

    LANE_UP             <= LANE_UP_Buffer;


    -- Lane Initialization state machine

    aurora_1ln_rx_lpm_rx_lane_init_sm_simplex_4byte_i : aurora_1ln_rx_lpm_RX_LANE_INIT_SM_SIMPLEX_4BYTE

        port map (

        -- GTX Interface

                    RX_NOT_IN_TABLE     =>  RX_NOT_IN_TABLE,
                    RX_DISP_ERR         =>  RX_DISP_ERR,
                    RX_CHAR_IS_COMMA    =>  RX_CHAR_IS_COMMA,
                    RX_REALIGN          =>  RX_REALIGN,
                    GTX_RX_RESET        =>  GTX_RX_RESET,
                    RX_POLARITY         =>  RX_POLARITY,

        -- Comma Detect Phase Alignment Interface

                    ENA_COMMA_ALIGN     =>  ENA_COMMA_ALIGN,

        -- Symbol Decoder Interface

                    RX_SP               =>  rx_sp_i,
                    RX_NEG              =>  rx_neg_i,
                    DO_WORD_ALIGN       =>  do_word_align_i,

        -- Error Detection Logic Interface

                    HARD_ERR_RESET    =>  hard_err_reset_i,
                    ENABLE_ERR_DETECT =>  enable_err_detect_i,

        -- Global Logic Interface

                    LANE_UP             =>  LANE_UP_Buffer,
                    CHANNEL_UP          =>  CHANNEL_UP,

        -- System Interface

                    USER_CLK            =>  USER_CLK,
                    RESET               =>  RESET
                 );


    -- Channel Bonding Count Decode module

    aurora_1ln_rx_lpm_chbond_count_dec_4byte_i : aurora_1ln_rx_lpm_CHBOND_COUNT_DEC_4BYTE

        port map (

                    RX_STATUS           =>  RX_STATUS,
                    CHANNEL_BOND_LOAD   =>  CHANNEL_BOND_LOAD,
                    USER_CLK            =>  USER_CLK

                 );




    -- Symbol Decode module

    aurora_1ln_rx_lpm_sym_dec_4byte_i : aurora_1ln_rx_lpm_SYM_DEC_4BYTE

        port map (

        -- RX_LL Interface

                    RX_PAD           => RX_PAD,
                    RX_PE_DATA       => RX_PE_DATA,
                    RX_PE_DATA_V     => RX_PE_DATA_V,
                    RX_SCP           => RX_SCP,
                    RX_ECP           => RX_ECP,
                    RX_SUF           => RX_SUF,
                    RX_FC_NB         => RX_FC_NB,

        -- Lane Init SM Interface

                    DO_WORD_ALIGN    => do_word_align_i,
                    LANE_UP          => LANE_UP_Buffer,
                    RX_SP            => rx_sp_i,
                    RX_SPA           => rx_spa_i,
                    RX_NEG           => rx_neg_i,

        -- Global Logic Interface

                    GOT_A            => GOT_A,
                    GOT_V            => GOT_V,

        -- GTX Interface

                    RX_DATA          => rx_data_i,
                    RX_CHAR_IS_K     => rx_char_is_k_i,
                    RX_CHAR_IS_COMMA => rx_char_is_comma_i,
                    RX_CC            => rx_cc_i,

        -- System Interface

                    USER_CLK            =>  USER_CLK,
                    RESET               =>  RESET

    );


    -- Error Detection module

    aurora_1ln_rx_lpm_rx_err_detect_simplex_gtx_4byte_i : aurora_1ln_rx_lpm_RX_ERR_DETECT_SIMPLEX_GTX_4BYTE

        port map (

        -- Lane Init SM Interface

                    ENABLE_ERR_DETECT =>  enable_err_detect_i,
                    HARD_ERR_RESET    =>  hard_err_reset_i,

        -- Global Logic Interface

                    SOFT_ERR          =>  SOFT_ERR,
                    HARD_ERR          =>  HARD_ERR,

        -- GTX Interface

                    RX_DISP_ERR         =>  rx_disp_err_i,
                    RX_NOT_IN_TABLE     =>  rx_not_in_table_i,
                    RX_BUF_ERR          =>  RX_BUF_ERR,
                    RX_REALIGN          =>  RX_REALIGN,

        -- System Interface

                    USER_CLK            =>  USER_CLK

                 );

    -- Hot Plug module
    aurora_1ln_rx_lpm_hotplug_i : aurora_1ln_rx_lpm_HOTPLUG
    generic map
    (
       EXAMPLE_SIMULATION   => EXAMPLE_SIMULATION
    )
    port map
    (
        -- Sym Dec Interface
        RX_CC               => rx_cc_i,
        RX_SP               => rx_sp_i,
        RX_SPA              => rx_spa_i,

        -- GT Wrapper Interface
        LINK_RESET_OUT      => LINK_RESET_OUT,
        HPCNT_RESET         => HPCNT_RESET,

        -- System Interface
	     INIT_CLK            => INIT_CLK,
        USER_CLK            => USER_CLK,
        RESET               => RESET
    );


end RTL;
