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

library osvvm; -- Utility Library
context osvvm.OsvvmContext;

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

-- This testbench tests the User traffic handling of the RTPS Reader.
-- The testbench checks which packets are accepted and propagated as Cache Changes to the History Cache.
-- This testbench covers following:
-- * Traffic from matched/unmatched remote Endpoint
-- * Traffic with/without prior HEARTBEAT
-- * HEARTBEAT Handling
-- * GAP Handling
-- * Inline-QoS Handling
-- * Traffic without Payload (Unregister/Dispose DDS Operations)
-- * Traffic with Payload bigger than memory

entity L0_rtps_reader_test2 is
end entity;

architecture testbench of L0_rtps_reader_test2 is
    
    -- *CONSTANT DECLARATION*
    constant ACCEPT_RES : std_logic_vector(WORD_WIDTH downto 0) := (others => '0');
    constant REJECT_RES : std_logic_vector(WORD_WIDTH downto 0) := (others => '1');
    constant MAX_REMOTE_ENDPOINTS   : natural := 3;
    constant NUM_READERS            : natural := 5;
    
    impure function gen_test_config return CONFIG_ARRAY_TYPE is
        variable ret : CONFIG_ARRAY_TYPE(0 to NUM_READERS-1) := (others => DEFAULT_READER_CONFIG);
    begin
        -- vrk
        ret(0).RELIABILITY_QOS              := RELIABLE_RELIABILITY_QOS;
        ret(0).LIVELINESS_QOS               := AUTOMATIC_LIVELINESS_QOS;
        ret(0).DURABILITY_QOS               := VOLATILE_DURABILITY_QOS;
        ret(0).HEARTBEAT_RESPONSE_DELAY     := DURATION_ZERO;
        ret(0).HEARTBEAT_SUPPRESSION_DELAY  := DURATION_ZERO;
        ret(0).LEASE_DURATION               := DURATION_INFINITE;
        ret(0).WITH_KEY                     := TRUE;
        ret(0).MAX_PAYLOAD_SIZE             := 44;
        -- trk
        ret(1).RELIABILITY_QOS              := RELIABLE_RELIABILITY_QOS;
        ret(1).LIVELINESS_QOS               := AUTOMATIC_LIVELINESS_QOS;
        ret(1).DURABILITY_QOS               := TRANSIENT_LOCAL_DURABILITY_QOS;
        ret(1).HEARTBEAT_RESPONSE_DELAY     := DURATION_ZERO;
        ret(1).HEARTBEAT_SUPPRESSION_DELAY  := DURATION_ZERO;
        ret(1).LEASE_DURATION               := DURATION_INFINITE;
        ret(1).WITH_KEY                     := TRUE;
        ret(1).MAX_PAYLOAD_SIZE             := 44;
        -- vbk
        ret(2).RELIABILITY_QOS              := BEST_EFFORT_RELIABILITY_QOS;
        ret(2).LIVELINESS_QOS               := AUTOMATIC_LIVELINESS_QOS;
        ret(2).DURABILITY_QOS               := VOLATILE_DURABILITY_QOS;
        ret(2).HEARTBEAT_RESPONSE_DELAY     := DURATION_ZERO;
        ret(2).HEARTBEAT_SUPPRESSION_DELAY  := DURATION_ZERO;
        ret(2).LEASE_DURATION               := DURATION_INFINITE;
        ret(2).WITH_KEY                     := TRUE;
        ret(2).MAX_PAYLOAD_SIZE             := 44;
        -- tbk
        ret(3).RELIABILITY_QOS              := BEST_EFFORT_RELIABILITY_QOS;
        ret(3).LIVELINESS_QOS               := AUTOMATIC_LIVELINESS_QOS;
        ret(3).DURABILITY_QOS               := TRANSIENT_LOCAL_DURABILITY_QOS;
        ret(3).HEARTBEAT_RESPONSE_DELAY     := DURATION_ZERO;
        ret(3).HEARTBEAT_SUPPRESSION_DELAY  := DURATION_ZERO;
        ret(3).LEASE_DURATION               := DURATION_INFINITE;
        ret(3).WITH_KEY                     := TRUE;
        ret(3).MAX_PAYLOAD_SIZE             := 44;
        -- vrn
        ret(4).RELIABILITY_QOS              := RELIABLE_RELIABILITY_QOS;
        ret(4).LIVELINESS_QOS               := AUTOMATIC_LIVELINESS_QOS;
        ret(4).DURABILITY_QOS               := VOLATILE_DURABILITY_QOS;
        ret(4).HEARTBEAT_RESPONSE_DELAY     := DURATION_ZERO;
        ret(4).HEARTBEAT_SUPPRESSION_DELAY  := DURATION_ZERO;
        ret(4).LEASE_DURATION               := DURATION_INFINITE;
        ret(4).WITH_KEY                     := FALSE;
        ret(4).MAX_PAYLOAD_SIZE             := 44;
        return ret;
    end function;
    constant TEST_CONFIG : CONFIG_ARRAY_TYPE := gen_test_config;
    
    -- *TYPE DECLARATION*
    type SEND_STAGE_TYPE is (IDLE, BUSY);
    type HC_STAGE_TYPE is (IDLE, CHECK, DONE);
    
    -- *SIGNAL DECLARATION*
    signal clk, rd_user, rd_meta, last_word_in_user, last_word_in_meta : std_logic := '0';
    signal empty_user, empty_meta, w_map : std_logic_vector(0 to NUM_READERS-1) := (others => '0');
    signal reset : std_logic := '1';
    signal data_in_user, data_in_meta : std_logic_vector(WORD_WIDTH-1 downto 0) := (others => '0');
    signal data_out_hc : WORD_ARRAY_TYPE(0 to NUM_READERS-1) := (others => (others => '0'));
    signal start_hc, ack_hc, done_hc, ready_out_hc, valid_out_hc, last_word_out_hc : std_logic_vector(0 to NUM_READERS-1) := (others => '0');
    signal opcode_hc : HISTORY_CACHE_OPCODE_ARRAY_TYPE(0 to NUM_READERS-1) := (others => NOP);
    signal ret_hc : HISTORY_CACHE_RESPONSE_ARRAY_TYPE(0 to NUM_READERS-1) := (others => OK);
    signal stim_stage_user, stim_stage_meta : SEND_STAGE_TYPE := IDLE;
    signal stage_hc : HC_STAGE_TYPE := IDLE;
    shared variable stimulus_user, stimulus_meta, reference : TEST_PACKET_TYPE := EMPTY_TEST_PACKET;
    signal packet_sent_user, packet_sent_meta : std_logic := '0';
    signal cnt_stim_meta, cnt_stim_user : natural := 0;
    signal start_meta, start_user : std_logic := '0';
    shared variable SB_hc : osvvm.ScoreBoardPkg_slv.ScoreBoardPType;
    signal stim_done, mem_check_done, out_check_done, test_done : std_logic := '0';
    
    -- *FUNCTION DECLARATION*
    function gen_sn(input : natural) return SEQUENCENUMBER_TYPE is
        variable ret : SEQUENCENUMBER_TYPE;
    begin
        ret(0)  := (others => '0');
        ret(1)  := unsigned(int(input, WORD_WIDTH));
        return ret;
    end function;
    
    procedure wait_on_sig(signal sig : std_logic) is
    begin
        if (sig /= '1') then
            wait on sig until sig = '1';
        end if;
    end procedure;
    
begin

    -- Unit Under Test
    uut : entity work.rtps_reader(arch)
        generic map (
            NUM_READERS             => NUM_READERS,
            CONFIG_ARRAY            => to_QUARTUS_CONFIG_ARRAY_TYPE(TEST_CONFIG),
            ENTITYID                => (0 => x"2aa0b907",1 => x"a60db607",2 => x"d1e94607",3 => x"5b554807",4 => x"10c46b04"),
            MAX_REMOTE_ENDPOINTS    => MAX_REMOTE_ENDPOINTS
        )
        port map (
            -- SYSTEM
            clk                 => clk,
            reset               => reset,
            time                => TIME_ZERO,
            empty_user          => empty_user or (empty_user'range => packet_sent_user),
            rd_user             => rd_user,
            data_in_user        => data_in_user,
            last_word_in_user   => last_word_in_user,
            empty_meta          => empty_meta or (empty_meta'range => packet_sent_meta),
            rd_meta             => rd_meta,
            data_in_meta        => data_in_meta,
            last_word_in_meta   => last_word_in_meta,
            wr_ro               => open,
            full_ro             => '0',
            last_word_out_ro    => open,
            data_out_ro         => open,
            start_hc            => start_hc,
            opcode_hc           => opcode_hc,
            ack_hc              => ack_hc,
            done_hc             => done_hc,
            ret_hc              => ret_hc,
            data_out_hc         => data_out_hc,
            valid_out_hc        => valid_out_hc,
            ready_out_hc        => ready_out_hc,
            last_word_out_hc    => last_word_out_hc
        );
    
    stimulus_prc : process
        variable RV                     : RandomPType;
        variable e0, e1, e2, endpoint   : ENDPOINT_DATA_TYPE := DEFAULT_ENDPOINT_DATA;
        variable payload                : TEST_PACKET_TYPE := EMPTY_TEST_PACKET;
        variable sub                    : RTPS_SUBMESSAGE_TYPE := DEFAULT_RTPS_SUBMESSAGE;
        variable cc                     : CACHE_CHANGE_TYPE := DEFAULT_CACHE_CHANGE;
        variable src_ts                 : TIME_TYPE := TIME_INVALID;
        
        alias idle_sig is <<signal uut.idle_sig : std_logic>>;
        
        -- Wrapper to use procedure as function
        impure function gen_rand_loc_2 return LOCATOR_TYPE is
            variable ret : LOCATOR_TYPE := EMPTY_LOCATOR;
        begin
            gen_rand_loc(RV, ret);
            return ret;
        end function;
        
        impure function gen_rand_guid_prefix return GUIDPREFIX_TYPE is
            variable ret : GUIDPREFIX_TYPE;
        begin
            ret := (0 => RV.RandSlv(WORD_WIDTH), 1 => RV.RandSlv(WORD_WIDTH), 2 => RV.RandSlv(WORD_WIDTH));
            return ret;
        end function;
        
        procedure start_meta_test is
        begin
            start_meta  <= '1';
            wait until rising_edge(clk);
            start_meta  <= '0';
            wait until rising_edge(clk);
        end procedure;
        
        procedure start_user_test is
        begin
            start_user  <= '1';
            wait until rising_edge(clk);
            start_user  <= '0';
            wait until rising_edge(clk);
        end procedure;
        
        procedure push_reference(accept : in boolean; i : in natural) is
            variable t : std_logic_vector(0 to NUM_READERS-1);
        begin
            t := (i => '1', others => '0');
            -- NOTE: First Word represents the Response that we will return.
            if (accept) then
                SB_hc.Push(ACCEPT_RES);
            else
                SB_hc.Push(REJECT_RES);
            end if;
            SB_hc.push(t);
            for i in 0 to reference.length-1 loop
                SB_hc.Push(reference.last(i) & reference.data(i));
            end loop;
        end procedure;
        
        impure function gen_payload return TEST_PACKET_TYPE is
            variable ret : TEST_PACKET_TYPE := EMPTY_TEST_PACKET;
        begin
            for i in 0 to RV.RandInt(1,10) loop
                ret.data(ret.length) := RV.RandSlv(WORD_WIDTH);
                ret.length  := ret.length + 1;
            end loop;
            ret.last(ret.length-1) := '1';
            return ret;
        end function;
        
        impure function gen_key_hash return KEY_HASH_TYPE is
            variable ret : KEY_HASH_TYPE := KEY_HASH_NIL;
        begin
            for i in 0 to KEY_HASH_TYPE'length-1 loop
                ret(i) := RV.RandSlv(WORD_WIDTH);
            end loop;
            return ret;
        end function;
        
        -- NOTE: This procedure waits until the idle_sig is high for at least
        --       two consecutive clock cycles.
        procedure wait_on_idle is
            variable first : boolean := TRUE;
        begin
            loop
                if (idle_sig /= '1') then
                    wait until idle_sig = '1';
                elsif (not first) then
                    exit;
                end if;
                wait until rising_edge(clk);
                wait until rising_edge(clk);
                first := FALSE;
            end loop;
        end procedure;
    begin
        
        SetAlertLogName("L0_rtps_reader_test2 - User Traffic Handling");
        SetAlertEnable(FAILURE, TRUE);
        SetAlertEnable(ERROR, TRUE);
        SetAlertEnable(WARNING, TRUE);
        SetLogEnable(DEBUG, FALSE);
        SetLogEnable(PASSED, FALSE);
        SetLogEnable(INFO, TRUE);
        RV.InitSeed(RV'instance_name);
        SB_hc.SetAlertLogID(Name => "HC", ParentID => OSVVM_ALERTLOG_ID);
        
        -- Endpoint 0
        e0                          := DEFAULT_ENDPOINT_DATA;
        e0.reader                   := FALSE;
        e0.nr                       := 0;
        e0.match                    := MATCH;
        e0.entityid                 := RV.RandSlv(ENTITYID_WIDTH);
        e0.participant.guidPrefix   := gen_rand_guid_prefix;
        e0.unicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        e0.lifespan                 := gen_duration(2 sec);
        -- Endpoint 1
        e1                          := DEFAULT_ENDPOINT_DATA;
        e1.reader                   := FALSE;
        e1.nr                       := 1;
        e1.match                    := MATCH;
        e1.entityid                 := RV.RandSlv(ENTITYID_WIDTH);
        e1.participant.guidPrefix   := gen_rand_guid_prefix;
        e1.unicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        -- Endpoint 2
        e2                          := DEFAULT_ENDPOINT_DATA;
        e2.reader                   := FALSE;
        e2.nr                       := 2;
        e2.match                    := MATCH;
        e2.entityid                 := RV.RandSlv(ENTITYID_WIDTH);
        e2.participant.guidPrefix   := gen_rand_guid_prefix;
        e2.unicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        
        Log("Initiating Test", INFO);
        stim_done   <= '0';
        start_meta  <= '0';
        start_user  <= '0';
        reset       <= '1';
        w_map       <= (others => '0');
        wait until rising_edge(clk);
        wait until rising_edge(clk);
        reset   <= '0';
        
        src_ts              := TIME_INVALID;
        
        -- *GENERAL SEQUENCE NUMBER HANDLING*
        Log("*Test General SequenceNumber Handling*", INFO);
        
        Log("Endpoint 0 sent DATA [SN 1] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(1);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        -- NOTE: We do not wait here for the sent, to allow meta traffic being available during user traffic handling.
        
        Log("Insert Endpoint 0,1,2 {R0,R1,R2,R3,R4}", INFO);
        gen_endpoint_match_frame(e0, stimulus_meta);
        gen_endpoint_match_frame(e1, stimulus_meta);
        gen_endpoint_match_frame(e2, stimulus_meta);
        w_map   <= "11111";
        start_meta_test;
        wait_on_sig(packet_sent_meta);
        wait_on_idle;
        stimulus_meta   := EMPTY_TEST_PACKET;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 0 sent DATA [SN 1] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(1);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, endpoint.lifespan, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 0 sent DATA [SN 1] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(1);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 0 sent DATA [SN 3] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R4: Ignore", DEBUG);
        Log("R2,R3: Accept", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(3);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, endpoint.lifespan, endpoint.nr, reference);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 0 sent DATA [SN 2] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R4: Accept", DEBUG);
        Log("R2,R3: Ignore", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(2);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, endpoint.lifespan, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        -- *HEARTBEAT HANDLING*
        Log("*Test HEARTBEAT Handling*", INFO);
        
        Log("Endpoint 2 Heartbeat [First 2, Last 11] {R0,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(2);
        sub.lastSN          := gen_sn(11);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "10111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 Heartbeat [First 11, Last 18] {R1}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(11);
        sub.lastSN          := gen_sn(18);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "01000";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 1] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(1);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 2] {R0,R2,R3,R4}", INFO);
        Log("R0,R2,R4: Ignore", DEBUG);
        Log("R3: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(2);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,3);
        w_map   <= "10111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 18] {R1}", INFO);
        Log("R1: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(18);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "01000";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 11] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(11);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 Heartbeat [First 5, Last 15] {R0,R1,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(5);
        sub.lastSN          := gen_sn(15);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11001";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 Heartbeat [First 5, Last 13] {R2,R3}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(5);
        sub.lastSN          := gen_sn(13);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "00110";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 5] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(5);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 13] {R2,R3}", INFO);
        Log("R2,R3: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(13);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        w_map   <= "00110";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 15] {R0,R1,R4}", INFO);
        Log("R0,R1,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(15);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11001";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 12] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R4: Accept", DEBUG);
        Log("R2,R3: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(12);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 Heartbeat [First 15, Last 18] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(15);
        sub.lastSN          := gen_sn(18);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 16] {R0,R1,R4}", INFO);
        Log("R0,R1,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(16);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11001";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 18] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R4: Ignore", DEBUG);
        Log("R2,R3: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(18);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 15] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R4: Accept", DEBUG);
        Log("R2,R3: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(15);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 Heartbeat [First 19, Last 18] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(19);
        sub.lastSN          := gen_sn(18);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 19] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(19);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        -- *GAP HANDLING*
        Log("*Test GAP Handling*", INFO);
        
        Log("Endpoint 2 GAP [GAP 1-10] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(1);
        sub.gapList         := (base => gen_sn(11), numBits => int(0, CDR_LONG_WIDTH), bitmap => (others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 11] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(11);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 GAP [GAP 2-10,12] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(2);
        sub.gapList         := (base => gen_sn(10), numBits => int(3, CDR_LONG_WIDTH), bitmap => (0 => '1', 2 => '1', others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 13] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(13);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 GAP [GAP 21-22] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(21);
        sub.gapList         := (base => gen_sn(23), numBits => int(0, CDR_LONG_WIDTH), bitmap => (others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 20] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(20);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 GAP [GAP 21-22, 24] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(21);
        sub.gapList         := (base => gen_sn(22), numBits => int(3, CDR_LONG_WIDTH), bitmap => (0 => '1', 2 => '1', others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 21] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(21);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 GAP [GAP 21-22, 24] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(21);
        sub.gapList         := (base => gen_sn(22), numBits => int(3, CDR_LONG_WIDTH), bitmap => (0 => '1', 2 => '1', others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 23] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(23);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 GAP [GAP 21-22, 24] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(21);
        sub.gapList         := (base => gen_sn(22), numBits => int(3, CDR_LONG_WIDTH), bitmap => (0 => '1', 2 => '1', others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 24] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(24);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 25] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(25);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 GAP [GAP 20-30] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_GAP;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.gapStart        := gen_sn(20);
        sub.gapList         := (base => gen_sn(31), numBits => int(1, CDR_LONG_WIDTH), bitmap => (others => '0'));
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 26] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3: Ignore", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(26);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 2 sent DATA [SN 31] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e2;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(31);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        -- *DURABILITY AND INLINE-QOS HANDLING*
        Log("*Test Durability and Inline-QoS Handling*", INFO);
        
        Log("Endpoint 1 sent DATA [SN 10] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R2,R3,R4: Accept", DEBUG);
        Log("R1: Ignore", DEBUG);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(10);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 Heartbeat [First 10, Last 16] {R1}", INFO);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(10);
        sub.lastSN          := gen_sn(16);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "01000";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 10] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R2,R3,R4: Ignore", DEBUG);
        Log("R1: Accept", DEBUG);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(10);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,1);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 11, Oversized Payload] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(11);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        sub.data.length := 12;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 11, Inline-QoS (Status Info, Key Hash)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        src_ts              := gen_duration(1 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(11);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind          := ALIVE_FILTERED;
        cc.src_timestamp := src_ts;
        cc.instance      := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 12, Inline-QoS (Invalid PID_STATUS_INFO)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        src_ts              := gen_duration(1.3 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(12);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind          := ALIVE_FILTERED;
        cc.src_timestamp := src_ts;
        cc.instance      := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos, PID_STATUS_INFO, -1);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 12, Inline-QoS (Extra Bytes in PID_STATUS_INFO)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        src_ts              := gen_duration(1.4 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(12);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind          := ALIVE_FILTERED;
        cc.src_timestamp := src_ts;
        cc.instance      := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos, PID_STATUS_INFO, +1);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 13, Inline-QoS (Invalid PID_KEY_HASH)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        src_ts              := gen_duration(1.5 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(13);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind          := ALIVE_FILTERED;
        cc.src_timestamp := src_ts;
        cc.instance      := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos, PID_KEY_HASH, -1);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 13, Inline-QoS (Extra Bytes in PID_KEY_HASH)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        src_ts              := gen_duration(1.6 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(13);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind          := ALIVE_FILTERED;
        cc.src_timestamp := src_ts;
        cc.instance      := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos, PID_KEY_HASH, +1);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 14, Inline-QoS (Status Info, Key Hash), Little Endian] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        src_ts              := gen_duration(1.7 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(14);
        sub.flags(SUBMESSAGE_ENDIAN_FLAG_POS)       := '1';
        sub.flags(SUBMESSAGE_DATA_FLAG_POS)         := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS)   := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind             := ALIVE_FILTERED;
        cc.src_timestamp    := src_ts;
        cc.instance         := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '1', sub.inlineQos);
        gen_sentinel('1', sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        -- *NON_PAYLOAD TRAFFIC HANDLING*
        Log("*Test Non-Payload Traffic Handling*", INFO);
        
        Log("Endpoint 1 sent DATA [SN 15, No Payload, Inline-QoS (Status Info, Key Hash)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        src_ts              := gen_duration(2 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(15);
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        cc := gen_cache_change(sub);
        cc.kind          := NOT_ALIVE_DISPOSED;
        cc.src_timestamp := src_ts;
        cc.instance      := gen_key_hash;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 16, Serialized Key, Inline-QoS (Status Info)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        src_ts              := gen_duration(2.5 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(16);
        sub.flags(SUBMESSAGE_KEY_FLAG_POS) := '1';
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        sub.data := gen_payload;
        cc := gen_cache_change(sub);
        cc.kind          := NOT_ALIVE_UNREGISTERED;
        cc.src_timestamp := src_ts;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 1 sent DATA [SN 17, No Payload, Inline-QoS (Status Info)] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        src_ts              := gen_duration(2.6 sec);
        endpoint            := e1;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(17);
        sub.flags(SUBMESSAGE_INLINE_QOS_FLAG_POS) := '1';
        cc := gen_cache_change(sub);
        cc.kind          := NOT_ALIVE_DISPOSED;
        cc.src_timestamp := src_ts;
        gen_inline_qos(cc, endpoint, TRUE, '0', sub.inlineQos);
        gen_sentinel(sub.inlineQos);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        gen_add_cache_change_dds(cc, TIME_INVALID, endpoint.nr, reference);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        src_ts  := TIME_INVALID;
        
        -- *HISTORY CACHE RESPONSE HANDLING*
        Log("*Test History Cache Response Handling*", INFO);
        
        Log("Endpoint 0 Heartbeat [First 4, Last 3] {R0,R1,R2,R3,R4}", INFO);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_HEARTBEAT;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.firstSN         := gen_sn(4);
        sub.lastSN          := gen_sn(3);
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 0 sent DATA [SN 4] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept,HC Reject", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(4);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, endpoint.lifespan, endpoint.nr, reference);
        push_reference(FALSE,0);
        push_reference(FALSE,1);
        push_reference(FALSE,2);
        push_reference(FALSE,3);
        push_reference(FALSE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        Log("Endpoint 0 sent DATA [SN 4] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Accept", DEBUG);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(4);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        cc := gen_cache_change(sub);
        gen_add_cache_change_dds(cc, endpoint.lifespan, endpoint.nr, reference);
        push_reference(TRUE,0);
        push_reference(TRUE,1);
        push_reference(TRUE,2);
        push_reference(TRUE,3);
        push_reference(TRUE,4);
        w_map   <= "11111";
        start_user_test;
        wait_on_sig(packet_sent_user);
        wait_on_sig(out_check_done);
        wait_on_idle;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        -- *CONCURRENT TRAFFIC HANDLING*
        Log("*Test Concurrent User and Metatraffic Handling*", INFO);
        
        Log("Unmatch Endpoint 0 {R0,R1,R2,R3,R4}", INFO);
        Log("Endpoint 0 sent DATA [SN 5] {R0,R1,R2,R3,R4}", INFO);
        Log("R0,R1,R2,R3,R4: Ignore", DEBUG);
        e0.match    := UNMATCH;
        gen_endpoint_match_frame(e0, stimulus_meta);
        endpoint            := e0;
        sub                 := DEFAULT_RTPS_SUBMESSAGE;
        sub.submessageID    := SID_DATA;
        sub.writerId        := endpoint.entityid;
        sub.readerId        := DEFAULT_READER_ENTITYID;
        sub.writerSN        := gen_sn(5);
        sub.flags(SUBMESSAGE_DATA_FLAG_POS) := '1';
        sub.data := gen_payload;
        gen_rtps_handler_out(sub, get_loc(endpoint), FALSE, src_ts, endpoint.participant.guidPrefix, stimulus_user);
        w_map   <= "11111";
        start_user  <= '1';
        start_meta  <= '1';
        wait until rising_edge(clk);
        start_user  <= '0';
        start_meta  <= '0';
        stimulus_meta   := EMPTY_TEST_PACKET;
        stimulus_user   := EMPTY_TEST_PACKET;
        reference       := EMPTY_TEST_PACKET;
        
        stim_done   <= '1';
        wait_on_sig(test_done);
        TranscriptOpen(RESULTS_FILE, APPEND_MODE);
        SetTranscriptMirror;
        ReportAlerts;
        TranscriptClose;
        std.env.stop;
        wait;
    end process;
    
    clock_prc : process
    begin
        clk <= '0';
        wait for 25 ns;
        clk <= '1';
        wait for 25 ns;
    end process;
    
    empty_meta_prc : process
    begin
        empty_meta  <= not w_map;
        wait until rising_edge(clk);
        empty_meta  <= (others => '1');
        wait until rising_edge(clk);
    end process;
    
    empty_user_prc : process
    begin
        empty_user  <= not w_map;
        wait until rising_edge(clk);
        empty_user  <= (others => '1');
        wait until rising_edge(clk);
    end process;
    
    alert_prc : process(all)
    begin
        if rising_edge(clk) then
            alertif(empty_meta = (empty_meta'range => '1') and rd_meta = '1', "Input FIFO read signal high while empty signal high (meta)", FAILURE);
            alertif(empty_user = (empty_user'range => '1') and rd_user = '1', "Input FIFO read signal high while empty signal high (user)", FAILURE);
        end if;
    end process;
    
    input_meta_prc : process(all)
    begin
        data_in_meta        <= stimulus_meta.data(cnt_stim_meta);
        last_word_in_meta   <= stimulus_meta.last(cnt_stim_meta);
        
        if rising_edge(clk) then
            if (reset = '1') then
                cnt_stim_meta       <= 0;
                stim_stage_meta     <= IDLE;
                packet_sent_meta    <= '1';
            else
                case (stim_stage_meta) is
                    when IDLE =>
                        if (start_meta = '1' and stimulus_meta.length /= 0) then
                            stim_stage_meta     <= BUSY;
                            packet_sent_meta    <= '0';
                        end if;
                    when BUSY =>
                        if (rd_meta = '1') then
                            if (cnt_stim_meta = stimulus_meta.length-1) then
                                stim_stage_meta     <= IDLE;
                                packet_sent_meta    <= '1';
                                cnt_stim_meta       <= 0;
                            else
                                cnt_stim_meta   <= cnt_stim_meta + 1;
                            end if;
                        end if;
                end case;
            end if;
        end if;
    end process;
    
    input_user_prc : process(all)
    begin
        data_in_user        <= stimulus_user.data(cnt_stim_user);
        last_word_in_user   <= stimulus_user.last(cnt_stim_user);
        
        if rising_edge(clk) then
            if (reset = '1') then
                cnt_stim_user       <= 0;
                stim_stage_user     <= IDLE;
                packet_sent_user    <= '1';
            else
                case (stim_stage_user) is
                    when IDLE =>
                        if (start_user = '1' and stimulus_user.length /= 0) then
                            stim_stage_user     <= BUSY;
                            packet_sent_user    <= '0';
                        end if;
                    when BUSY =>
                        if (rd_user = '1') then
                            if (cnt_stim_user = stimulus_user.length-1) then
                                stim_stage_user     <= IDLE;
                                packet_sent_user    <= '1';
                                cnt_stim_user       <= 0;
                            else
                                cnt_stim_user   <= cnt_stim_user + 1;
                            end if;
                        end if;
                end case;
            end if;
        end if;
    end process;
    
    done_proc : process(clk)
    begin
        if rising_edge(clk) then
            if (stim_done = '1' and SB_hc.empty) then
                test_done   <= '1';
            else
                test_done   <= '0';
            end if;
        end if;
    end process;
    
    hc_prc : process (all)
        variable ind    : natural := 0;
        variable accept : std_logic_vector(WORD_WIDTH downto 0);
        
        function one_hot_converter(input : std_logic_vector) return natural is
        begin
            for i in 0 to input'length-1 loop
                if (input(i) = '1') then
                    return i;
                end if;
            end loop;
            return 0; -- DEFAULT
        end function;
    begin
        if (SB_hc.empty) then
            out_check_done  <= '1';
        else
            out_check_done  <= '0';
        end if;
        if rising_edge(clk) then
            case (stage_hc) is
                when IDLE =>
                    if (start_hc /= (start_hc'range => '0')) then
                        alertif((signed(start_hc) and (- signed(start_hc))) = (start_hc'range => '0'), "start_hc not in one-hot encoding", FAILURE);
                        ind := one_hot_converter(start_hc);
                        case (opcode_hc(ind)) is
                            when ADD_CACHE_CHANGE =>
                                -- Pop Response Value
                                SB_hc.pop(accept);
                                SB_hc.Check(start_hc);
                                stage_hc    <= CHECK;
                            when others =>
                                Alert("Unexpected HC Opcode", FAILURE);
                                stage_hc    <= DONE;
                        end case;
                    end if;
                when CHECK =>
                    if (valid_out_hc(ind) = '1') then
                        SB_hc.Check(last_word_out_hc(ind) & data_out_hc(ind));
                        
                        if (last_word_out_hc(ind) = '1') then
                            stage_hc    <= DONE;
                        end if;
                    end if;
                when DONE =>
                    stage_hc    <= IDLE;
                when others =>
                    null;
            end case;
        end if;
        
        -- DEFAULT
        ack_hc          <= (others => '0');
        done_hc         <= (others => '0');
        ret_hc          <= (others => ERROR);
        ready_out_hc    <= (others => '0');
        
        case (stage_hc) is
            when IDLE =>
                if (start_hc /= (start_hc'range => '0')) then
                    -- HACK: We trigger all acks because we cannot use 'ind' variable in the same clock cycle
                    --       Since the uut only interfaces with 1 HC at a time (one-hot encoded start_hc) this
                    --       is still valid.
                    ack_hc  <= (others => '1');
                end if;
            when CHECK =>
                ready_out_hc(ind)   <= '1';
            when DONE =>
                done_hc(ind)    <= '1';
                if (accept = ACCEPT_RES) then
                    ret_hc(ind) <= OK;
                else
                    ret_hc(ind) <= REJECTED;
                end if;
            when others =>
                null;
        end case;
    end process;
    
    watchdog : process
    begin
        wait for 1 ms;
        Alert("Test timeout", FAILURE);
        std.env.stop;
    end process;
    
end architecture;