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 matching of remote participants. Due to the fact, that participant matching does not have an immediate effect visible to the outside,
-- this testbench is using external names to access the memory of the rtps_builtin_endpoint directly and check the contents at the supposed participant locations.
-- This testbench covers following:
--  * Mathing compatible Participants
--  * Ignoring incompatible Participants
--  * Unmatching previously matched Participants (Due to incompatibility)
--  * Update data of previously matched Participant
--  * Big/Little Endian Participant Data


entity L0_rtps_builtin_endpoint_test1 is
end entity;

architecture testbench of L0_rtps_builtin_endpoint_test1 is
    
    -- *CONSTANT DECLARATION*
    constant MAX_REMOTE_PARTICIPANTS    : natural := 3;
    
    -- *TYPE DECLARATION*
    type TEST_STAGE_TYPE is (IDLE, BUSY);
    type TEST_RAM_TYPE is array (0 to (MAX_REMOTE_PARTICIPANTS*PARTICIPANT_FRAME_SIZE)-1) of std_logic_vector(WORD_WIDTH-1 downto 0);
    
    -- *SIGNAL DECLARATION*
    signal clk, in_empty, rd_sig, last_word_in, last_word_out: std_logic := '0';
    signal reset : std_logic := '1'; --TODO: Do that in all testbenches
    signal endpoint_wr, endpoint_full   : std_logic_vector(0 to NUM_ENDPOINTS-1) := (others => '0');
    signal data_in, data_out : std_logic_vector(WORD_WIDTH-1 downto 0) := (others => '0');
    signal stim_stage : TEST_STAGE_TYPE := IDLE;
    shared variable stimulus, reference : TEST_PACKET_TYPE := EMPTY_TEST_PACKET;
    signal packet_sent : std_logic := '0';
    signal cnt_stim : natural := 0;
    signal start : std_logic := '0';
    shared variable SB_out : work.ScoreBoardPkg_builtin_endpoint.ScoreBoardPType;
    shared variable SB_mem : work.ScoreBoardPkg_MemoryTest.ScoreBoardPType;
    signal stim_done, check_done : std_logic := '0';
    -- *FUNCTION DECLARATION*
    procedure wait_on_complete is
    begin
        wait until rising_edge(packet_sent);
    end procedure;
    
begin

    -- Unit Under Test
    uut : entity work.rtps_builtin_endpoint(arch)
        generic map (
            MAX_REMOTE_PARTICIPANTS => MAX_REMOTE_PARTICIPANTS
        )
        port map (
            clk             => clk,
            reset           => reset,
            empty           => in_empty or packet_sent,
            rd              => rd_sig,
            data_in         => data_in,
            data_out        => data_out,
            last_word_in    => last_word_in,
            time            => TIME_ZERO,
            endpoint_full   => endpoint_full,
            endpoint_wr     => endpoint_wr,
            rtps_wr         => open,
            rtps_full       => '0',
            last_word_out   => last_word_out,
            alive           => (others => '0')
        );
    
    stimulus_prc : process
        variable rtps_sub       : RTPS_SUBMESSAGE_TYPE := DEFAULT_RTPS_SUBMESSAGE;
        variable RV             : RandomPType;
        variable participant    : PARTICIPANT_DATA_TYPE := DEFAULT_PARTICIPANT_DATA;
        variable p0, p1, p2     : GUIDPREFIX_TYPE;
        
        -- 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;
        
        procedure push_reference is
            variable wr_sig : std_logic_vector(NUM_ENDPOINTS-1 downto 0) := (others => '1');
        begin
            gen_participant_match_frame(participant, reference);
            for i in 0 to reference.length-1 loop
                SB_out.Push(wr_sig & reference.last(i) & reference.data(i));
            end loop;
            reference   := EMPTY_TEST_PACKET;
        end procedure;
        
        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_test is
        begin
            start <= '1';
            wait until rising_edge(clk);
            start <= '0';
            wait until rising_edge(clk);
        end procedure;
    begin
        
        SetAlertLogName("rtps_builtin_endpoint - Level 0 - Participant Matching");
        SetAlertEnable(FAILURE, TRUE);
        SetAlertEnable(ERROR, TRUE);
        SetAlertEnable(WARNING, TRUE);
        SetLogEnable(DEBUG, FALSE);
        SetLogEnable(PASSED, FALSE);
        SetLogEnable(INFO, TRUE);
        RV.InitSeed(RV'instance_name);
        
        -- NOTE: A single incremented Sequence Number is used for all the Participants. This should not be a problem for
        --       the best effort participant data, as long as it is monotonically increasing.
        rtps_sub                                    := DEFAULT_RTPS_SUBMESSAGE;
        rtps_sub.submessageID                       := SID_DATA;
        rtps_sub.writerId                           := ENTITYID_SPDP_BUILTIN_PARTICIPANT_ANNOUNCER;
        rtps_sub.readerId                           := ENTITYID_SPDP_BUILTIN_PARTICIPANT_DETECTOR;
        rtps_sub.flags(SUBMESSAGE_DATA_FLAG_POS)    := '1';
        
        p0  := gen_rand_guid_prefix;
        p1  := gen_rand_guid_prefix;
        p2  := gen_rand_guid_prefix;
        
        
        Log("Initiating Test", INFO);
        start       <= '0';
        reset       <= '1';
        wait until rising_edge(clk);
        wait until rising_edge(clk);
        reset   <= '0';
        
        Log("Match Participant 0 [Compatible]", INFO);
        participant                             := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                  := p0;
        participant.nr                          := 0;
        participant.match                       := MATCH;
        participant.defaultUnicastLocatorList   := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,0,0]
        
        Log("Match Participant 1 [Compatible, Little Endian]", INFO);
        participant                             := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                  := p1;
        participant.nr                          := 1;
        participant.littleEndian                := '1';
        participant.match                       := MATCH;
        participant.defaultUnicastLocatorList   := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,0]
        
        Log("Ignore Participant 2 [Incompatible Domain ID]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p2;
        participant.nr                              := 2;
        participant.domainId                        := int(2, DOMAIN_ID_WIDTH);
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,0]
        
        Log("Unmatch Participant 0 [Incompatible Domain ID]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p0;
        participant.nr                              := 0;
        participant.domainId                        := int(2, DOMAIN_ID_WIDTH);
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        push_reference;
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [0,p1,0]
        
        Log("Ignore Participant 2 [Incompatible Domain TAG]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p2;
        participant.nr                              := 2;
        participant.domainTag                       := DEFAULT_DOMAIN_TAG;
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [0,p1,0]
        
        Log("Unmatch Participant 1 [Incompatible Domain TAG]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p1;
        participant.nr                              := 1;
        participant.domainTag                       := DEFAULT_DOMAIN_TAG;
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        push_reference;
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [0,0,0]
        
        Log("Match Participant 0 [+Unicast Metatraffic Locator]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p0;
        participant.nr                              := 0;
        participant.match                           := MATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.metatrafficUnicastLocatorList   := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,0,0]
        
        Log("Match Participant 1 [ALL Values Set non default]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p1;
        participant.nr                              := 1;
        participant.vendorId                        := RV.RandSlv(VENDORID_WIDTH);
        participant.expectsInlineQoS(0)             := '1';
        participant.leaseDuration                   := gen_duration(5,500);
        participant.manualLivelinessCount           := int(6, CDR_LONG_WIDTH);
        participant.builtinEndpointQoS(0)           := '1';
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.defaultMulticastLocatorList     := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.metatrafficUnicastLocatorList   := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.metatrafficMulticastLocatorList := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.match                           := MATCH;
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,0]
        
        Log("Ignore Participant 2 [Incompatible Protocol Version]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p2;
        participant.nr                              := 2;
        participant.protocolVersion                 := PROTOCOLVERSION_1_0;
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,0]
        
        Log("Unmatch Participant 0 [Incompatible Protocol Version]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p0;
        participant.nr                              := 0;
        participant.protocolVersion                 := PROTOCOLVERSION_1_0;
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        push_reference;
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [0,p1,0]
        
        Log("Match Participant 0 [Compatible, Only Subscribers]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p0;
        participant.nr                              := 0;
        participant.match                           := MATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER):= '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,0]
        
        Log("Match Participant 2 [Compatible, Only Publishers]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p2;
        participant.nr                              := 2;
        participant.match                           := MATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,p2]
        
        Log("Update Participant 1 [Valid/Invalid Default/Metatraffic Locators]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p1;
        participant.nr                              := 1;
        participant.match                           := MATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(4,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, 1 => gen_rand_loc_2, 2 => gen_rand_loc_2, 3 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.defaultUnicastLocatorList.locator(1).kind           := LOCATOR_KIND_UDPv6;
        participant.defaultUnicastLocatorList.locator(2).addr           := (others => '0');
        participant.defaultUnicastLocatorList.locator(3).portn          := (others => '0');
        participant.defaultMulticastLocatorList     := (numLocators => int(4,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, 1 => gen_rand_loc_2, 2 => gen_rand_loc_2, 3 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.defaultMulticastLocatorList.locator(1).kind         := LOCATOR_KIND_UDPv6;
        participant.defaultMulticastLocatorList.locator(2).portn        := (others => '0');
        participant.defaultMulticastLocatorList.locator(3).addr         := (others => '0');
        participant.metatrafficUnicastLocatorList   := (numLocators => int(4,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, 1 => gen_rand_loc_2, 2 => gen_rand_loc_2, 3 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.metatrafficUnicastLocatorList.locator(1).kind       := LOCATOR_KIND_UDPv6;
        participant.metatrafficUnicastLocatorList.locator(2).addr       := (others => '0');
        participant.metatrafficUnicastLocatorList.locator(3).portn      := (others => '0');
        participant.metatrafficMulticastLocatorList := (numLocators => int(4,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, 1 => gen_rand_loc_2, 2 => gen_rand_loc_2, 3 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.metatrafficMulticastLocatorList.locator(1).kind     := LOCATOR_KIND_UDPv6;
        participant.metatrafficMulticastLocatorList.locator(2).portn    := (others => '0');
        participant.metatrafficMulticastLocatorList.locator(3).addr     := (others => '0');
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,p2]
        
        Log("Unmatch Participant 2 [No Endpoints]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p2;
        participant.nr                              := 2;
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        push_reference;
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [p0,p1,0]
        
        Log("Unmatch Participant 0 [Incompatible Built-in Endpoints]", INFO);
        participant                                 := DEFAULT_PARTICIPANT_DATA;
        participant.guidPrefix                      := p0;
        participant.nr                              := 0;
        participant.match                           := UNMATCH;
        participant.defaultUnicastLocatorList       := (numLocators => int(1,CDR_LONG_WIDTH), locator => (0 => gen_rand_loc_2, others => EMPTY_LOCATOR));
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR) := '1';
        participant.availableBuiltinEndpoints(DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR)  := '1';
        gen_participant_data(participant, rtps_sub.data);
        gen_sentinel(rtps_sub.data);
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        SB_mem.Push(gen_participant_mem_frame(participant));
        push_reference;
        start_test;
        wait_on_complete;
        stimulus            := EMPTY_TEST_PACKET;
        rtps_sub.data       := EMPTY_TEST_PACKET;
        rtps_sub.writerSN   := rtps_sub.writerSN + 1;
        -- MEMORY STATE [0,p1,0]
        
        -- DUMMY PACKET (Trigger last mem check)
        -- Mark Packet as non-standard Payload, in order to trigger the packet to be skipped (as early as possible)
        rtps_sub.data   := EMPTY_TEST_PACKET;
        rtps_sub.flags(SUBMESSAGE_NON_STANDARD_PAYLOAD_FLAG_POS):= '1';
        gen_rtps_handler_out(rtps_sub, participant, stimulus);
        start_test;
        wait_on_complete;
        
        stim_done   <= '1';
        wait until check_done = '1';
        TranscriptOpen(RESULTS_FILE, APPEND_MODE);
        SetTranscriptMirror;
        AlertIf((not SB_out.empty) or (not SB_mem.empty), "Incomplete test run");
        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;
    
    in_empty_prc : process
    begin
        in_empty        <= '0';
        wait until rd_sig = '1';
        wait until rising_edge(clk);
        in_empty        <= '1';
        wait until rising_edge(clk);
    end process;
    
    endpoint_full_prc : process
    begin
        endpoint_full   <= (others => '0');
        wait until (or endpoint_wr) = '1';
        wait until rising_edge(clk);
        endpoint_full   <= (others => '1');
        wait until rising_edge(clk);
    end process;
    
    alert_prc : process(all)
    begin
        if rising_edge(clk) then
            alertif(in_empty = '1' and rd_sig = '1', "Input FIFO read signal high while empty signal high", ERROR);
            alertif(endpoint_full /= (0 to NUM_ENDPOINTS-1 => '0') and (endpoint_wr /= (0 to NUM_ENDPOINTS-1 => '0')), "Endpoint FIFO write signal high while full signal high", ERROR);
        end if;
    end process;
    
    input_prc : process(all)
    begin
        data_in         <= stimulus.data(cnt_stim);
        last_word_in    <= stimulus.last(cnt_stim);
        
        if rising_edge(clk) then
            if (reset = '1') then
                cnt_stim    <= 0;
                stim_stage  <= IDLE;
                packet_sent <= '1';
            else
                case (stim_stage) is
                    when IDLE =>
                        if (start = '1' and stimulus.length /= 0) then
                            stim_stage  <= BUSY;
                            packet_sent <= '0';
                        end if;
                    when BUSY =>
                        if (rd_sig = '1') then
                            if (cnt_stim = stimulus.length-1) then
                                stim_stage  <= IDLE;
                                packet_sent <= '1';
                                cnt_stim    <= 0;
                            else
                                cnt_stim    <= cnt_stim + 1;
                            end if;
                        end if;
                end case;
            end if;
        end if;
    end process;
    
    output_check_prc : process(all)
    begin
        check_done  <= '0';
        if rising_edge(clk) then
            if (endpoint_wr /= (0 to NUM_ENDPOINTS-1 => '0')) then
                SB_out.Check(endpoint_wr & last_word_out & data_out);
            end if;
            if (stim_done = '1' and SB_out.empty) then
                check_done  <= '1';
            end if;
        end if;
    end process;
    
    mem_check_prc : process
        alias mem is <<signal uut.ram_inst.mem : TEST_RAM_TYPE>>;
        alias mem_op_done is <<signal uut.mem_op_done : std_logic>>;
        variable reference : TEST_PARTICIPANT_MEMORY_FRAME_TYPE;
    begin
        -- SAFEGUARD: (Prevent Fall-through Behavior)
        if (reset /= '0') then
            wait until reset = '0';
        end if;
        -- NOTE: The first read after the packet is sent signifies that the State Machine has begun processing the next packet.
        --       The memory operation that could still be in progress is the last one concerning the last sent packet.
        wait until packet_sent = '1';
        wait until rising_edge(rd_sig);
        if (mem_op_done /= '1') then
            wait until mem_op_done = '1';
        end if;
        if (not SB_mem.empty) then
            SB_mem.Pop(reference);
            for i in 0 to reference'length-1 loop
                AffirmIf(?? (mem(reference(i).addr) ?= reference(i).data), "Address: " & integer'image(reference(i).addr) & " Received: " & to_hstring(mem(reference(i).addr)) & " Expected: " & to_hstring(reference(i).data));
            end loop;
        end if;
    end process;
    
    watchdog : process
    begin
        wait for 1 ms;
        Alert("Test timeout", FAILURE);
        std.env.stop;
    end process;
    
end architecture;