-- altera vhdl_input_version vhdl_2008
-- XXX: QSYS Fix (https://www.intel.com/content/www/us/en/support/programmable/articles/000079458.html)

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

use work.rtps_package.all;
use work.user_config.all;
use work.rtps_config_package.all;
use work.Type1_package.all;

entity L2_Testbench_Lib3 is
    port (
        -- SYSTEM
        clk                     : in std_logic;
        reset                   : in std_logic;
        time                    : in TIME_TYPE;
        -- INPUT
        empty                   : in std_logic;
        read                    : out std_logic;
        data_in                 : in std_logic_vector(WORD_WIDTH-1 downto 0);
        -- OUTPUT
        full                    : in std_logic;
        write                   : out std_logic;
        data_out                : out std_logic_vector(WORD_WIDTH-1 downto 0);
        -- TO USER ENTITY
        start_user              : in std_logic;
        ack_user                : out std_logic;
        opcode_user             : in DDS_READER_OPCODE_TYPE;
        instance_state_user     : in std_logic_vector(INSTANCE_STATE_KIND_WIDTH-1 downto 0);
        view_state_user         : in std_logic_vector(VIEW_STATE_KIND_WIDTH-1 downto 0);
        sample_state_user       : in std_logic_vector(SAMPLE_STATE_KIND_WIDTH-1 downto 0);
        instance_handle_user    : in INSTANCE_HANDLE_TYPE;
        max_samples_user        : in std_logic_vector(MAX_SAMPLES_WIDTH-1 downto 0);
        get_data_user           : in std_logic;
        done_user               : out std_logic;
        return_code_user        : out std_logic_vector(RETURN_CODE_WIDTH-1 downto 0);
        sample_info_user        : out SAMPLE_INFO_TYPE;    
        sample_info_valid_user  : out std_logic;
        sample_info_ack_user    : in std_logic;
        eoc_user                : out std_logic;
        -- Communication Status
        status_user             : out std_logic_vector(STATUS_KIND_WIDTH-1 downto 0);
        decode_error            : out std_logic;
        -- ###GENERATED START###
        id                      : out std_logic_vector(CDR_LONG_WIDTH-1 downto 0);
        a                       : out std_logic_vector(CDR_LONG_WIDTH-1 downto 0);
        -- ###GENERATED END###
        valid                   : out std_logic
    );
end entity;

architecture arch of L2_Testbench_Lib3 is

    signal full_fire_rh, write_rh_fire : std_logic_vector(0 to NUM_ENDPOINTS-1);
    signal last_word_rh_firb, last_word_rh_fire : std_logic;
    signal data_rh_fire, data_rh_firb : std_logic_vector(WORD_WIDTH-1 downto 0);
    signal full_firb_rh, write_rh_firb : std_logic;
    signal read_rb_firb, empty_firb_rb, last_word_firb_rb : std_logic;
    signal data_firb_rb : std_logic_vector(WORD_WIDTH-1 downto 0);
    signal read_re_fire, last_word_fire_re : std_logic_vector(0 to 1);
    signal empty_fire_re : std_logic_vector(0 to NUM_ENDPOINTS-1);
    signal data_fire_re : WORD_ARRAY_TYPE(0 to 1);
    signal alive_re_rb, full_frbre_re, write_rb_frbre : std_logic_vector(0 to NUM_ENDPOINTS-1);
    signal last_word_rb_frbre , last_word_rb_firo : std_logic;
    signal data_rb_frbre , data_rb_firo : std_logic_vector(WORD_WIDTH-1 downto 0);
    signal full_firo_rb, write_rb_firo : std_logic;
    signal read_re_frbre, last_word_frbre_re : std_logic_vector(0 to 1);
    signal empty_frbre_re : std_logic_vector(0 to NUM_ENDPOINTS-1);
    signal data_frbre_re : WORD_ARRAY_TYPE(0 to 1);
    signal full_firo_re, write_re_firo, last_word_re_firo : std_logic_vector(0 to 1);
    signal data_re_firo : WORD_ARRAY_TYPE(0 to 1);
    signal start_rr_dr, ack_dr_rr, done_dr_rr, valid_rr_dr, ready_dr_rr, last_word_rr_dr : std_logic_vector(0 to NUM_READERS-1);
    signal opcode_rr_dr : HISTORY_CACHE_OPCODE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal ret_dr_rr : HISTORY_CACHE_RESPONSE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal data_rr_dr : WORD_ARRAY_TYPE(0 to NUM_READERS-1);
    signal liveliness_assertion_dw_rw, data_available_dw_rw, start_rw_dw, ack_dw_rw, done_rw_dw, get_data_rw_dw, valid_dw_rw, ready_rw_dw, last_word_dw_rw : std_logic_vector(0 to NUM_WRITERS-1);
    signal opcode_rw_dw : HISTORY_CACHE_OPCODE_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal seq_nr_rw_dw, cc_seq_nr_dw_rw : SEQUENCENUMBER_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal ret_dw_rw : HISTORY_CACHE_RESPONSE_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal data_dw_rw : WORD_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal cc_instance_handle_dw_rw : INSTANCE_HANDLE_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal cc_kind_dw_rw : CACHE_CHANGE_KIND_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal cc_source_timestamp_dw_rw : TIME_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal sample_info_dr_ri : SAMPLE_INFO_ARRAY_TYPE(0 to NUM_READERS-1);
    signal start_ri_dr, ack_dr_ri, get_data_ri_dr, done_dr_ri, ready_ri_dr, valid_dr_ri, sample_info_valid_dr_ri, sample_info_ack_ri_dr, eoc_dr_ri, last_word_dr_ri : std_logic_vector(0 to NUM_READERS-1);
    signal opcode_ri_dr : DDS_READER_OPCODE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal instance_state_ri_dr : INSTANCE_STATE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal view_state_ri_dr : VIEW_STATE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal sample_state_ri_dr : SAMPLE_STATE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal instance_handle_ri_dr : INSTANCE_HANDLE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal max_samples_ri_dr : MAX_SAMPLES_ARRAY_TYPE(0 to NUM_READERS-1);
    signal return_code_dr_ri : RETURN_CODE_ARRAY_TYPE(0 to NUM_READERS-1);
    signal data_dr_ri : WORD_ARRAY_TYPE(0 to NUM_READERS-1);
    signal status_dr_ri : STATUS_KIND_ARRAY_TYPE(0 to NUM_READERS-1);
    signal start_wi_dw, ack_dw_wi, done_dw_wi, valid_wi_dw, valid_dw_wi, ready_wi_dw, ready_dw_wi, last_word_wi_dw, last_word_dw_wi : std_logic_vector(0 to NUM_WRITERS-1);
    signal opcode_wi_dw : DDS_WRITER_OPCODE_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal instance_handle_wi_dw, instance_handle_dw_wi : INSTANCE_HANDLE_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal source_ts_wi_dw : TIME_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal max_wait_wi_dw : DURATION_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal return_code_dw_wi : RETURN_CODE_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal data_dw_wi, data_wi_dw : WORD_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal status_dw_wi : STATUS_KIND_ARRAY_TYPE(0 to NUM_WRITERS-1);
    signal empty_firo_ro, read_ro_firo, last_word_firo_ro : std_logic_vector(0 to 2);
    signal data_firo_ro : WORD_ARRAY_TYPE(0 to 2);
    
begin
    
    
    rtps_handler_inst : entity work.rtps_handler(arch)
        port map (
            -- SYSTEM
            clk                 => clk,
            reset               => reset,
            -- INPUT
            empty               => empty,
            rd                  => read,
            data_in             => data_in,
            -- TO DISCOVERY MODULE
            full_dm             => full_firb_rh,
            wr_dm               => write_rh_firb,
            data_out_dm         => data_rh_firb,
            last_word_out_dm    => last_word_rh_firb,
            -- TO USER ENDPOINTS
            full_rtps           => full_fire_rh,
            wr_rtps             => write_rh_fire,
            data_out_rtps       => data_rh_fire,
            last_word_out_rtps  => last_word_rh_fire
        );
    
    
    fifo_in_rb_inst : configuration work.FWFT_FIFO_cfg
            generic map (
                FIFO_DEPTH  => 2,
                DATA_WIDTH  => WORD_WIDTH+1
            )
            port map (
                -- SYSTEM
                reset       => reset,
                clk         => clk,
                -- INPUT
                full        => full_firb_rh,
                write       => write_rh_firb,
                data_in(WORD_WIDTH-1 downto 0)  => data_rh_firb,
                data_in(WORD_WIDTH)             => last_word_rh_firb,
                -- OUTPUT
                empty       => empty_firb_rb,
                read        => read_rb_firb,
                data_out(WORD_WIDTH-1 downto 0) => data_firb_rb,
                data_out(WORD_WIDTH)            => last_word_firb_rb,
                -- MISC
                free        => open
            );
    
    fifo_in_re_r_if : if (NUM_READERS > 0) generate
        fifo_in_re_inst : entity work.vector_FIFO
            generic map (
                FIFO_DEPTH  => 2,
                DATA_WIDTH  => WORD_WIDTH+1,
                FIFO_WIDTH  => NUM_READERS
            )
            port map (
                -- SYSTEM
                reset       => reset,
                clk         => clk,
                -- INPUT
                full        => full_fire_rh(0 to NUM_READERS-1),
                write       => write_rh_fire(0 to NUM_READERS-1),
                data_in(WORD_WIDTH-1 downto 0)  => data_rh_fire, -- Multicast
                data_in(WORD_WIDTH)             => last_word_rh_fire, -- Multicast
                -- OUTPUT
                empty       => empty_fire_re(0 to NUM_READERS-1),
                read        => read_re_fire(0),
                data_out(WORD_WIDTH-1 downto 0) => data_fire_re(0),
                data_out(WORD_WIDTH)            => last_word_fire_re(0)
            );
    end generate;
    fifo_in_re_w_if : if (NUM_WRITERS > 0) generate
        fifo_in_re_inst : entity work.vector_FIFO
            generic map (
                FIFO_DEPTH  => 2,
                DATA_WIDTH  => WORD_WIDTH+1,
                FIFO_WIDTH  => NUM_WRITERS
            )
            port map (
                -- SYSTEM
                reset       => reset,
                clk         => clk,
                -- INPUT
                full        => full_fire_rh(NUM_READERS to NUM_ENDPOINTS-1),
                write       => write_rh_fire(NUM_READERS to NUM_ENDPOINTS-1),
                data_in(WORD_WIDTH-1 downto 0)  => data_rh_fire, -- Multicast
                data_in(WORD_WIDTH)             => last_word_rh_fire, -- Multicast
                -- OUTPUT
                empty       => empty_fire_re(NUM_READERS to NUM_ENDPOINTS-1),
                read        => read_re_fire(1),
                data_out(WORD_WIDTH-1 downto 0) => data_fire_re(1),
                data_out(WORD_WIDTH)            => last_word_fire_re(1)
            );
    end generate;
    
    rtps_discovery_module_inst : entity work.rtps_discovery_module(arch)
        generic map (
            MAX_REMOTE_PARTICIPANTS => MAX_REMOTE_PARTICIPANTS,
            PREFER_MULTICAST        => PREFER_MULTICAST_LOCATORS
        )
        port map (
            clk                 => clk,
            reset               => reset,
            time                => time,
            -- FROM RTPS HANDLER
            empty               => empty_firb_rb,
            rd                  => read_rb_firb,
            data_in             => data_firb_rb,
            last_word_in        => last_word_firb_rb,
            -- FROM USER ENDPOINTS
            alive               => alive_re_rb,
            -- TO USER ENDPOINTS
            full_rtps           => full_frbre_re,
            wr_rtps             => write_rb_frbre,
            data_out_rtps       => data_rb_frbre,
            last_word_out_rtps  => last_word_rb_frbre,
            -- TO RTPS OUT
            full_ro             => full_firo_rb,
            wr_ro               => write_rb_firo,
            data_out_ro         => data_rb_firo,
            last_word_out_ro    => last_word_rb_firo
        );
    
    fifo_rb_re_r_if : if (NUM_READERS > 0) generate
        fifo_rb_re_inst : entity work.vector_FIFO
            generic map (
                FIFO_DEPTH  => 2,
                DATA_WIDTH  => WORD_WIDTH+1,
                FIFO_WIDTH  => NUM_READERS
            )
            port map (
                -- SYSTEM
                reset       => reset,
                clk         => clk,
                -- INPUT
                full        => full_frbre_re(0 to NUM_READERS-1),
                write       => write_rb_frbre(0 to NUM_READERS-1),
                data_in(WORD_WIDTH-1 downto 0)  => data_rb_frbre,
                data_in(WORD_WIDTH)             => last_word_rb_frbre,
                -- OUTPUT
                empty       => empty_frbre_re(0 to NUM_READERS-1),
                read        => read_re_frbre(0),
                data_out(WORD_WIDTH-1 downto 0) => data_frbre_re(0),
                data_out(WORD_WIDTH)            => last_word_frbre_re(0)
            );
    end generate;
    fifo_rb_re_w_if : if (NUM_WRITERS > 0) generate
        fifo_rb_re_inst : entity work.vector_FIFO
            generic map (
                FIFO_DEPTH  => 2,
                DATA_WIDTH  => WORD_WIDTH+1,
                FIFO_WIDTH  => NUM_WRITERS
            )
            port map (
                -- SYSTEM
                reset       => reset,
                clk         => clk,
                -- INPUT
                full        => full_frbre_re(NUM_READERS to NUM_ENDPOINTS-1),
                write       => write_rb_frbre(NUM_READERS to NUM_ENDPOINTS-1),
                data_in(WORD_WIDTH-1 downto 0)  => data_rb_frbre,
                data_in(WORD_WIDTH)             => last_word_rb_frbre,
                -- OUTPUT
                empty       => empty_frbre_re(NUM_READERS to NUM_ENDPOINTS-1),
                read        => read_re_frbre(1),
                data_out(WORD_WIDTH-1 downto 0) => data_frbre_re(1),
                data_out(WORD_WIDTH)            => last_word_frbre_re(1)
            );
    end generate;
    
    
    rtps_endpoint_r_if : if (NUM_READERS > 0) generate
        rtps_reader_inst : entity work.rtps_reader(arch)
            generic map (
                NUM_READERS             => NUM_READERS,
                CONFIG_ARRAY            => to_QUARTUS_CONFIG_ARRAY_TYPE(ENDPOINT_CONFIG(0 to NUM_READERS-1)),
                ENTITYID                => ENTITYID(0 to NUM_READERS-1),
                MAX_REMOTE_ENDPOINTS    => MAX_REMOTE_ENDPOINTS
            )
            port map (
                -- SYSTEM
                clk                     => clk,
                reset                   => reset,
                time                    => time,
                -- FROM RTPS_HANDLER (USER TRAFFIC)
                empty_user              => empty_fire_re(0 to NUM_READERS-1),
                rd_user                 => read_re_fire(0),
                data_in_user            => data_fire_re(0),
                last_word_in_user       => last_word_fire_re(0),
                -- FROM DISCOVERY MODULE (META TRAFFIC)
                empty_meta              => empty_frbre_re(0 to NUM_READERS-1),
                rd_meta                 => read_re_frbre(0),
                data_in_meta            => data_frbre_re(0),
                last_word_in_meta       => last_word_frbre_re(0),
                -- RTPS OUTPUT
                full_ro                 => full_firo_re(0),
                wr_ro                   => write_re_firo(0),
                data_out_ro             => data_re_firo(0),
                last_word_out_ro        => last_word_re_firo(0),
                -- TO HISTORY CACHE
                start_hc                => start_rr_dr(0 to NUM_READERS-1),
                opcode_hc               => opcode_rr_dr(0 to NUM_READERS-1),
                ack_hc                  => ack_dr_rr(0 to NUM_READERS-1),
                done_hc                 => done_dr_rr(0 to NUM_READERS-1),
                ret_hc                  => ret_dr_rr(0 to NUM_READERS-1),
                valid_out_hc            => valid_rr_dr(0 to NUM_READERS-1),
                ready_out_hc            => ready_dr_rr(0 to NUM_READERS-1),
                data_out_hc             => data_rr_dr(0 to NUM_READERS-1),
                last_word_out_hc        => last_word_rr_dr(0 to NUM_READERS-1)
            );
        
        -- Set Alive Signals of Readers to Zero
        alive_re_rb(0 to NUM_READERS-1) <= (others => '0');
    end generate;
    
    rtps_endpoint_w_if : if (NUM_WRITERS > 0) generate
        rtps_writer_inst : entity work.rtps_writer(arch)
            generic map (
                NUM_WRITERS                 => NUM_WRITERS,
                CONFIG_ARRAY                => to_QUARTUS_CONFIG_ARRAY_TYPE(ENDPOINT_CONFIG(NUM_READERS to NUM_ENDPOINTS-1)),
                ENTITYID                    => ENTITYID(NUM_READERS to NUM_ENDPOINTS-1),
                INLINE_QOS                  => INLINE_QOS_DATA(0 to NUM_WRITERS-1),
                MAX_REMOTE_ENDPOINTS        => MAX_REMOTE_ENDPOINTS
            )
            port map (
                -- SYSTEM
                clk                     => clk,
                reset                   => reset,
                time                    => time,
                -- FROM RTPS_HANDLER (USER TRAFFIC)
                empty_user              => empty_fire_re(NUM_READERS to NUM_ENDPOINTS-1),
                rd_user                 => read_re_fire(1),
                data_in_user            => data_fire_re(1),
                last_word_in_user       => last_word_fire_re(1),
                -- FROM DISCOVERY MODULE (META TRAFFIC)
                empty_meta              => empty_frbre_re(NUM_READERS to NUM_ENDPOINTS-1),
                rd_meta                 => read_re_frbre(1),
                data_in_meta            => data_frbre_re(1),
                last_word_in_meta       => last_word_frbre_re(1),
                -- TO DISCOVERY MODULE (META TRAFFIC)
                alive_sig               => alive_re_rb(NUM_READERS to NUM_ENDPOINTS-1),
                -- RTPS OUTPUT
                full_ro                 => full_firo_re(1),
                wr_ro                   => write_re_firo(1),
                data_out_ro             => data_re_firo(1),
                last_word_out_ro        => last_word_re_firo(1),
                -- FROM HC
                liveliness_assertion    => liveliness_assertion_dw_rw(0 to NUM_WRITERS-1),
                data_available          => data_available_dw_rw(0 to NUM_WRITERS-1),
                start_hc                => start_rw_dw(0 to NUM_WRITERS-1),
                opcode_hc               => opcode_rw_dw(0 to NUM_WRITERS-1),
                ack_hc                  => ack_dw_rw(0 to NUM_WRITERS-1),
                seq_nr_hc               => seq_nr_rw_dw(0 to NUM_WRITERS-1),
                done_hc                 => done_rw_dw(0 to NUM_WRITERS-1),
                ret_hc                  => ret_dw_rw(0 to NUM_WRITERS-1),
                get_data_hc             => get_data_rw_dw(0 to NUM_WRITERS-1),
                valid_in_hc             => valid_dw_rw(0 to NUM_WRITERS-1),
                ready_in_hc             => ready_rw_dw(0 to NUM_WRITERS-1),
                data_in_hc              => data_dw_rw(0 to NUM_WRITERS-1),
                last_word_in_hc         => last_word_dw_rw(0 to NUM_WRITERS-1),
                cc_instance_handle      => cc_instance_handle_dw_rw(0 to NUM_WRITERS-1),
                cc_kind                 => cc_kind_dw_rw(0 to NUM_WRITERS-1),
                cc_source_timestamp     => cc_source_timestamp_dw_rw(0 to NUM_WRITERS-1),
                cc_seq_nr               => cc_seq_nr_dw_rw(0 to NUM_WRITERS-1)
            );
    end generate;
    
    dds_endpoint_w_if : if (NUM_WRITERS > 0) generate
        dds_writer_inst : entity work.dds_writer(arch)
            generic map (
                NUM_WRITERS     => NUM_WRITERS,
                CONFIG_ARRAY    => to_QUARTUS_CONFIG_ARRAY_TYPE(ENDPOINT_CONFIG(NUM_READERS to NUM_ENDPOINTS-1))
            )
            port map (
                -- SYSTEM
                clk                     => clk,
                reset                   => reset,
                time                    => time,
                -- TO/FROM RTPS ENDPOINT
                start_rtps              => start_rw_dw(0 to NUM_WRITERS-1),
                opcode_rtps             => opcode_rw_dw(0 to NUM_WRITERS-1),
                ack_rtps                => ack_dw_rw(0 to NUM_WRITERS-1),
                done_rtps               => done_rw_dw(0 to NUM_WRITERS-1),
                ret_rtps                => ret_dw_rw(0 to NUM_WRITERS-1),
                seq_nr_rtps             => seq_nr_rw_dw(0 to NUM_WRITERS-1),
                get_data_rtps           => get_data_rw_dw(0 to NUM_WRITERS-1),
                valid_out_rtps          => valid_dw_rw(0 to NUM_WRITERS-1),
                ready_out_rtps          => ready_rw_dw(0 to NUM_WRITERS-1),
                data_out_rtps           => data_dw_rw(0 to NUM_WRITERS-1),
                last_word_out_rtps      => last_word_dw_rw(0 to NUM_WRITERS-1),
                liveliness_assertion    => liveliness_assertion_dw_rw(0 to NUM_WRITERS-1),
                data_available          => data_available_dw_rw(0 to NUM_WRITERS-1),
                -- Cache Change
                cc_instance_handle      => cc_instance_handle_dw_rw(0 to NUM_WRITERS-1),
                cc_kind                 => cc_kind_dw_rw(0 to NUM_WRITERS-1),
                cc_source_timestamp     => cc_source_timestamp_dw_rw(0 to NUM_WRITERS-1),
                cc_seq_nr               => cc_seq_nr_dw_rw(0 to NUM_WRITERS-1),
                -- TO/FROM USER ENTITY
                start_dds               => start_wi_dw(0 to NUM_WRITERS-1),
                ack_dds                 => ack_dw_wi(0 to NUM_WRITERS-1),
                opcode_dds              => opcode_wi_dw(0 to NUM_WRITERS-1),
                instance_handle_in_dds  => instance_handle_wi_dw(0 to NUM_WRITERS-1),
                source_ts_dds           => source_ts_wi_dw(0 to NUM_WRITERS-1),
                max_wait_dds            => max_wait_wi_dw(0 to NUM_WRITERS-1),
                done_dds                => done_dw_wi(0 to NUM_WRITERS-1),
                return_code_dds         => return_code_dw_wi(0 to NUM_WRITERS-1),
                instance_handle_out_dds => instance_handle_dw_wi(0 to NUM_WRITERS-1),
                valid_in_dds            => valid_wi_dw(0 to NUM_WRITERS-1),
                ready_in_dds            => ready_dw_wi(0 to NUM_WRITERS-1),
                data_in_dds             => data_wi_dw(0 to NUM_WRITERS-1),
                last_word_in_dds        => last_word_wi_dw(0 to NUM_WRITERS-1),
                valid_out_dds           => valid_dw_wi(0 to NUM_WRITERS-1),
                ready_out_dds           => ready_wi_dw(0 to NUM_WRITERS-1),
                data_out_dds            => data_dw_wi(0 to NUM_WRITERS-1),
                last_word_out_dds       => last_word_dw_wi(0 to NUM_WRITERS-1),
                -- Communication Status
                status                  => status_dw_wi(0 to NUM_WRITERS-1)
            );
    end generate;
    dds_endpoint_r_if : if (NUM_READERS > 0) generate
        dds_reader_inst : entity work.dds_reader(arch)
            generic map (
                NUM_READERS     => NUM_READERS,
                CONFIG_ARRAY    => to_QUARTUS_CONFIG_ARRAY_TYPE(ENDPOINT_CONFIG(0 to NUM_READERS-1)),
                MAX_REMOTE_ENDPOINTS        => MAX_REMOTE_ENDPOINTS
            )
            port map (
                -- SYSTEM
                clk                             => clk,
                reset                           => reset,
                time                            => time,
                -- FROM RTPS ENDPOINT
                start_rtps                      => start_rr_dr(0 to NUM_READERS-1),
                opcode_rtps                     => opcode_rr_dr(0 to NUM_READERS-1),
                ack_rtps                        => ack_dr_rr(0 to NUM_READERS-1),
                done_rtps                       => done_dr_rr(0 to NUM_READERS-1),
                ret_rtps                        => ret_dr_rr(0 to NUM_READERS-1),
                valid_in_rtps                   => valid_rr_dr(0 to NUM_READERS-1),
                ready_in_rtps                   => ready_dr_rr(0 to NUM_READERS-1),
                data_in_rtps                    => data_rr_dr(0 to NUM_READERS-1),
                last_word_in_rtps               => last_word_rr_dr(0 to NUM_READERS-1),
                -- TO USER ENTITY
                start_dds                       => start_ri_dr(0 to NUM_READERS-1),
                ack_dds                         => ack_dr_ri(0 to NUM_READERS-1),
                opcode_dds                      => opcode_ri_dr(0 to NUM_READERS-1),
                instance_state_dds              => instance_state_ri_dr(0 to NUM_READERS-1),
                view_state_dds                  => view_state_ri_dr(0 to NUM_READERS-1),
                sample_state_dds                => sample_state_ri_dr(0 to NUM_READERS-1),
                instance_handle_dds             => instance_handle_ri_dr(0 to NUM_READERS-1),
                max_samples_dds                 => max_samples_ri_dr(0 to NUM_READERS-1),
                get_data_dds                    => get_data_ri_dr(0 to NUM_READERS-1),
                done_dds                        => done_dr_ri(0 to NUM_READERS-1),
                return_code_dds                 => return_code_dr_ri(0 to NUM_READERS-1),
                valid_out_dds                   => valid_dr_ri(0 to NUM_READERS-1),
                ready_out_dds                   => ready_ri_dr(0 to NUM_READERS-1),
                data_out_dds                    => data_dr_ri(0 to NUM_READERS-1),
                last_word_out_dds               => last_word_dr_ri(0 to NUM_READERS-1),
                sample_info                     => sample_info_dr_ri(0 to NUM_READERS-1),
                sample_info_valid               => sample_info_valid_dr_ri(0 to NUM_READERS-1),
                sample_info_ack                 => sample_info_ack_ri_dr(0 to NUM_READERS-1),
                eoc                             => eoc_dr_ri(0 to NUM_READERS-1),
                -- Communication Status
                status                          => status_dr_ri(0 to NUM_READERS-1)
            );
    end generate;
    
    
    fifo_in_ro_gen : for i in 0 to 2 generate
        fifo_in_ro_if : if (i = 2) generate
            fifo_in_ro_inst : configuration work.FWFT_FIFO_cfg
                generic map (
                    FIFO_DEPTH  => 2,
                    DATA_WIDTH  => WORD_WIDTH+1
                )
                port map (
                    -- SYSTEM
                    reset       => reset,
                    clk         => clk,
                    -- INPUT
                    full        => full_firo_rb,
                    write       => write_rb_firo,
                    data_in(WORD_WIDTH-1 downto 0)  => data_rb_firo,
                    data_in(WORD_WIDTH)             => last_word_rb_firo,
                    -- OUTPUT
                    empty       => empty_firo_ro(i),
                    read        => read_ro_firo(i),
                    data_out(WORD_WIDTH)            => last_word_firo_ro(i),
                    data_out(WORD_WIDTH-1 downto 0) => data_firo_ro(i),
                    -- MISC
                    free        => open
                );
        else generate        
            fifo_in_ro_inst : configuration work.FWFT_FIFO_cfg
                generic map (
                    FIFO_DEPTH  => 2,
                    DATA_WIDTH  => WORD_WIDTH+1
                )
                port map (
                    -- SYSTEM
                    reset       => reset,
                    clk         => clk,
                    -- INPUT
                    full        => full_firo_re(i),
                    write       => write_re_firo(i),
                    data_in(WORD_WIDTH-1 downto 0)  => data_re_firo(i),
                    data_in(WORD_WIDTH)             => last_word_re_firo(i),
                    -- OUTPUT
                    empty       => empty_firo_ro(i),
                    read        => read_ro_firo(i),
                    data_out(WORD_WIDTH)            => last_word_firo_ro(i),
                    data_out(WORD_WIDTH-1 downto 0) => data_firo_ro(i),
                    -- MISC
                    free        => open
                );
        end generate;
    end generate;
    
    
    rtps_out_inst : entity work.rtps_out(arch)
        generic map (
            RTPS_OUT_WIDTH  => 3
        )
        port map (
            -- SYSTEM
            clk             => clk,
            reset           => reset,
            -- INPUT
            empty           => empty_firo_ro,
            rd              => read_ro_firo,
            data_in         => data_firo_ro,
            last_word_in    => last_word_firo_ro,
            -- OUTPUT
            full            => full,
            wr              => write,
            data_out        => data_out
        );
    
    
    --#####################################################################
    
    
    Type1_reader_interface_inst : entity work.Type1_reader_interface(arch)
        port map (
            -- SYSTEM
            clk                                 => clk,
            reset                               => reset,
            -- FROM DDS READER
            start_dds                           => start_ri_dr(0),
            ack_dds                             => ack_dr_ri(0),
            opcode_dds                          => opcode_ri_dr(0),
            instance_state_dds                  => instance_state_ri_dr(0),
            view_state_dds                      => view_state_ri_dr(0),
            sample_state_dds                    => sample_state_ri_dr(0),
            instance_handle_dds                 => instance_handle_ri_dr(0),
            max_samples_dds                     => max_samples_ri_dr(0),
            get_data_dds                        => get_data_ri_dr(0),
            done_dds                            => done_dr_ri(0),
            return_code_dds                     => return_code_dr_ri(0),
            valid_in_dds                        => valid_dr_ri(0),
            ready_in_dds                        => ready_ri_dr(0),
            data_in_dds                         => data_dr_ri(0),
            last_word_in_dds                    => last_word_dr_ri(0),
            sample_info_dds                     => sample_info_dr_ri(0),
            sample_info_valid_dds               => sample_info_valid_dr_ri(0),
            sample_info_ack_dds                 => sample_info_ack_ri_dr(0),
            eoc_dds                             => eoc_dr_ri(0),
            -- Communication Status
            status_dds                          => status_dr_ri(0),
            -- TO USER ENTITY
            start_user                          => start_user,
            ack_user                            => ack_user,
            opcode_user                         => opcode_user,
            instance_state_user                 => instance_state_user,
            view_state_user                     => view_state_user,
            sample_state_user                   => sample_state_user,
            instance_handle_user                => instance_handle_user,
            max_samples_user                    => max_samples_user,
            get_data_user                       => get_data_user,
            done_user                           => done_user,
            return_code_user                    => return_code_user,
            sample_info_user                    => sample_info_user,
            sample_info_valid_user              => sample_info_valid_user,
            sample_info_ack_user                => sample_info_ack_user,
            eoc_user                            => eoc_user,
            -- Communication Status
            status_user                         => status_user,
            decode_error                        => open,
            -- ###GENERATED START###
            id                                  => id,
            a                                   => a,
            -- ###GENERATED END###
            valid                               => valid
        );
    
end architecture;
