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;

entity L0_dds_writer_test1_afk is
end entity;

-- This testbench tests the General Operation of the DDS Writer. It tests the correctness of the RTPS 
-- GET_MIN_SN, GET_MAX_SN, GET_CACHE_CHANGE, REMOVE_CACHE_CHANGE, ACK_CACHE_CHANGE, and NACK_CACHE_CHANGE Operations and the
-- DDS REGISTER_INSTANCE, UNREGISTER_INSTANCE, WRITE, DISPOSE, and LOOKUP_INSTANCE Operations.
-- More specifically the testbench covers following tests:
-- TEST: GET_MIN_SN/GET_MAX_SN ON EMPTY
-- TEST: GET_MIN_SN/GET_MAX_SN ON 1 SAMPLE
-- TEST: GET_MIN_SN/GET_MAX_SN ON >1 SAMPLE
-- TEST: ADD SAMPLE WITH KEY_HASH [UNKNOWN INSTANCE]
-- TEST: ADD SAMPLE WITH KEY_HASH [KNOWN INSTANCE]
-- TEST: ADD SAMPLE WITH HANDLE_NIL [UNKNOWN INSTANCE]
-- TEST: ADD SAMPLE WITH HANDLE_NIL [KNOWN INSTANCE]
-- TEST: NORMAL WRITE
-- TEST: WRITE ON PAYLOAD MEMORY FULL [WITHOUT ACKed SAMPLES]
-- TEST: WRITE ON PAYLOAD MEMORY FULL [WITH ACKed SAMPLES]
-- TEST: WRITE ON DISPOSED INSTANCE
-- TEST: WRITE ON UNREGISTERED INSTANCE
-- TEST: WRITE ALIGNED PAYLOAD
-- TEST: WRITE UNALIGNED PAYLOAD [>1 SLOT]
-- TEST: WRITE UNALIGNED PAYLOAD [<1 SLOT]
-- TEST: NORMAL REGISTER
-- TEST: REGISTER INSTANCE [KNOWN INSTANCE]
-- TEST: REGISTER INSTANCE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITHOUT STALE INSTANCE]
-- TEST: REGISTER INSTANCE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITH STALE INSTANCE]
-- TEST: REGISTER ON UNREGISTERED INSTANCE
-- TEST: NORMAL DISPOSE
-- TEST: DISPOSE ON PAYLOAD MEMORY FULL [WITHOUT ACKed SAMPLES]
-- TEST: DISPOSE ON PAYLOAD MEMORY FULL [WITH ACKed SAMPLES]
-- TEST: DISPOSE ON UNREGISTERED INSTANCE
-- TEST: GET_CACHE_CHANGE [UNKNOWN SN]
-- TEST: GET_CACHE_CHANGE [KNOWN SN, ALIGNED PAYLOAD]
-- TEST: GET_CACHE_CHANGE [KNOWN SN, UNALIGNED PAYLOAD, >1 SLOT]
-- TEST: GET_CACHE_CHANGE [KNOWN SN, UNALIGNED PAYLOAD, <1 SLOT]
-- TEST: NORMAL ACK_CACHE_CHANGE
-- TEST: ACK_CACHE_CHANGE [ALREADY ACKed SN]
-- TEST: NORMAL NACK_CACHE_CHANGE
-- TEST: REMOVE_CACHE_CHANGE [UNKNOWN SN]
-- TEST: REMOVE_CACHE_CHANGE [KNOWN SN]
-- TEST: NORMAL UNREGISTER
-- TEST: UNREGISTER INSTANCE ON PAYLOAD MEMORY FULL [WITHOUT ACKed SAMPLES]
-- TEST: UNREGISTER INSTANCE ON PAYLOAD MEMORY FULL [WITH ACKed SAMPLES]
-- TEST: UNREGISTER ON DISPOSED INSTANCE
-- TEST: UNREGISTER UNKNOWN INSTANCE
-- TEST: REMOVE STALE INSTANCE WITH 0 SAMPLES
-- TEST: REMOVE STALE INSTANCE WITH 1 SAMPLES
-- TEST: REMOVE STALE INSTANCE WITH >1 SAMPLES
-- TEST: ADD SAMPLE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITH STALE INSTANCE]
-- TEST: ADD SAMPLE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITHOUT STALE INSTANCES]
-- TEST: ADD SAMPLE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITH FULLY ACKed INSTANCE, WITHOUT STALE INSTANCE]
-- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITHOUT ACKed SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITH ACKed SAMPLES, WITHOUT ACKed INSTANCE SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITH ACKed INSTANCE SAMPLE]
-- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITH ACKed INSTANCE SAMPLES(>1)]
-- TEST: ADD SAMPLE ON MAX_SAMPLES [KNOWN INSTANCE,WITHOUT ACKed SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES [KNOWN INSTANCE,WITH ACKed SAMPLE]
-- TEST: ADD SAMPLE ON MAX_SAMPLES [KNOWN INSTANCE,WITH ACKed SAMPLES (>1)]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE,WITHOUT ACKed SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE,WITH ACKed SAMPLES, WITHOUT ACKed INSTANCE SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE,WITH ACKed INSTANCE SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITH STALE INSTANCE, WITHOUT ACKed SAMPLE]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITHOUT STALE INSTANCES,WITHOUT ACKed SAMPLES]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITHOUT STALE INSTANCE, WITH ACKed SAMPLE]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITH STALE INSTANCE (>0 SAMPLES)]
-- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITH STALE INSTANCE, WITH ACKed SAMPLE]
-- TEST: ADD SAMPLE ON PAYLOAD FULL & MAX_INSTANCES [UNKNOWN INSTANCE,WITH ACKed SAMPLES,WITH STALE INSTANCE (>= 1 SAMPLE)] (Induce Double Remove)
-- TEST: ADD SAMPLE BIGGER THAN AVAILABLE MEMORY SPACE [WITH ACKed SAMPLES]
-- TEST: INSTANCE LOOKUP [KNOWN INSTANCE]
-- TEST: INSTANCE LOOKUP [UNKNOWN INSTANCE]

architecture testbench of L0_dds_writer_test1_afk is
    
    -- *CONSTANT DECLARATION*
    constant MAX_REMOTE_ENDPOINTS    : natural := 3;
    
    -- *TYPE DECLARATION*
    type DDS_STAGE_TYPE is (IDLE, START, PUSH, DONE, CHECK);
    type RTPS_STAGE_TYPE is (IDLE, START, DONE, CHECK);
    type KH_STAGE_TYPE is (IDLE, READ_DATA, PUSH_KEY_HASH);
    type RTPS_WRITER_TEST_TYPE is record
        opcode      : HISTORY_CACHE_OPCODE_TYPE;
        cc          : CACHE_CHANGE_TYPE;
        ret_code    : HISTORY_CACHE_RESPONSE_TYPE;
    end record;
    constant DEFAULT_RTPS_WRITER_TEST : RTPS_WRITER_TEST_TYPE := (
        opcode      => NOP,
        cc          => DEFAULT_CACHE_CHANGE,
        ret_code    => OK
    );
    type DDS_TEST_TYPE is record
        opcode      : DDS_WRITER_OPCODE_TYPE;
        cc          : CACHE_CHANGE_TYPE;
        ret_code    : std_logic_vector(RETURN_CODE_WIDTH-1 downto 0);
    end record;
    constant DEFAULT_DDS_TEST : DDS_TEST_TYPE := (
        opcode      => NOP,
        cc          => DEFAULT_CACHE_CHANGE,
        ret_code    => RETCODE_OK
    );
    
    -- *SIGNAL DECLARATION*
    signal clk : std_logic := '0';
    signal reset : std_logic := '1';
    signal check_time : TIME_TYPE := TIME_ZERO;
    signal start_rtps, start_dds, start_kh, ack_rtps, ack_dds, ack_kh, done_rtps, done_dds : std_logic := '0';
    signal opcode_rtps : HISTORY_CACHE_OPCODE_TYPE := NOP;
    signal opcode_dds : DDS_WRITER_OPCODE_TYPE := NOP;
    signal opcode_kh : KEY_HOLDER_OPCODE_TYPE := NOP;
    signal ret_rtps : HISTORY_CACHE_RESPONSE_TYPE := ERROR;
    signal seq_nr_rtps, cc_seq_nr : SEQUENCENUMBER_TYPE := SEQUENCENUMBER_UNKNOWN;
    signal ready_out_rtps, valid_out_rtps, last_word_out_rtps : std_logic := '0';
    signal ready_in_dds, ready_out_dds, valid_in_dds, valid_out_dds, last_word_in_dds, last_word_out_dds : std_logic := '0';
    signal ready_in_kh, ready_out_kh, valid_in_kh, valid_out_kh, last_word_in_kh, last_word_out_kh : std_logic := '0';
    signal data_out_rtps, data_in_dds, data_out_dds, data_in_kh, data_out_kh : std_logic_vector(WORD_WIDTH-1 downto 0) := (others => '0');
    signal get_data_rtps, liveliness_assertion, data_available, abort_kh : std_logic := '0';
    signal cc_source_timestamp, source_ts_dds : TIME_TYPE := TIME_INVALID;
    signal cc_kind : CACHE_CHANGE_KIND_TYPE := ALIVE;
    signal cc_instance_handle, instance_handle_dds : INSTANCE_HANDLE_TYPE := HANDLE_NIL;
    signal max_wait_dds : DURATION_TYPE := DURATION_INFINITE;
    signal return_code_dds : std_logic_vector(RETURN_CODE_WIDTH-1 downto 0) := (others => '0');
    signal status : std_logic_vector(STATUS_KIND_WIDTH-1 downto 0) := (others => '0');
    
    signal dds_start, dds_done, rtps_start, rtps_done : std_logic := '0';
    signal dds_cnt, rtps_cnt, kh_cnt : natural := 0;
    signal dds_stage : DDS_STAGE_TYPE := IDLE;
    signal rtps_stage : RTPS_STAGE_TYPE := IDLE;
    signal kh_stage : KH_STAGE_TYPE := IDLE;
    signal kh_data : TEST_PACKET_TYPE := EMPTY_TEST_PACKET;
    shared variable dds : DDS_TEST_TYPE := DEFAULT_DDS_TEST;
    shared variable rtps : RTPS_WRITER_TEST_TYPE := DEFAULT_RTPS_WRITER_TEST;
    signal inst_id, kind_id, sn_id, ts_id, data_id, ret_id : AlertLogIDType;
    
    -- *FUNCTION DECLARATION*
    function extract_key_hash (payload : TEST_PACKET_TYPE) return INSTANCE_HANDLE_TYPE is
        variable ret : INSTANCE_HANDLE_TYPE := HANDLE_NIL;
    begin
        for i in 0 to 3 loop
            ret(i) := not payload.data(i);
        end loop;
        
        return ret;
    end function;
    
    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;
    
begin

    -- Unit Under Test
    uut : entity work.dds_writer(arch)
        generic map(
            HISTORY_QOS                 => KEEP_ALL_HISTORY_QOS,
            DEADLINE_QOS                => DURATION_INFINITE,
            LIFESPAN_QOS                => gen_duration(1,0),
            LEASE_DURATION              => DURATION_INFINITE,
            WITH_KEY                    => TRUE,
            MAX_SAMPLES                 => std_logic_vector(to_unsigned(4,CDR_LONG_WIDTH)),
            MAX_INSTANCES               => std_logic_vector(to_unsigned(3,CDR_LONG_WIDTH)),
            MAX_SAMPLES_PER_INSTANCE    => std_logic_vector(to_unsigned(2,CDR_LONG_WIDTH)),
            PAYLOAD_FRAME_SIZE          => 11
        )
        port map (
            clk                     => clk,
            reset                   => reset,
            time                    => check_time,
            start_rtps              => start_rtps,
            opcode_rtps             => opcode_rtps,
            ack_rtps                => ack_rtps,
            done_rtps               => done_rtps,
            ret_rtps                => ret_rtps,
            seq_nr_rtps             => seq_nr_rtps,
            get_data_rtps           => get_data_rtps,
            data_out_rtps           => data_out_rtps,
            valid_out_rtps          => valid_out_rtps,
            ready_out_rtps          => ready_out_rtps,
            last_word_out_rtps      => last_word_out_rtps,
            liveliness_assertion    => liveliness_assertion,
            data_available          => data_available,
            cc_instance_handle      => cc_instance_handle,
            cc_kind                 => cc_kind,
            cc_source_timestamp     => cc_source_timestamp,
            cc_seq_nr               => cc_seq_nr,
            start_kh                => start_kh,
            opcode_kh               => opcode_kh,
            ack_kh                  => ack_kh,
            data_in_kh              => data_in_kh,
            valid_in_kh             => valid_in_kh,
            ready_in_kh             => ready_in_kh,
            last_word_in_kh         => last_word_in_kh,
            data_out_kh             => data_out_kh,
            valid_out_kh            => valid_out_kh,
            ready_out_kh            => ready_out_kh,
            last_word_out_kh        => last_word_out_kh,
            abort_kh                => abort_kh,
            start_dds               => start_dds,
            ack_dds                 => ack_dds,
            opcode_dds              => opcode_dds,
            instance_handle_dds     => instance_handle_dds,
            source_ts_dds           => source_ts_dds,
            max_wait_dds            => max_wait_dds,
            done_dds                => done_dds,
            return_code_dds         => return_code_dds,
            ready_in_dds            => ready_in_dds,
            valid_in_dds            => valid_in_dds,
            data_in_dds             => data_in_dds,
            last_word_in_dds        => last_word_in_dds,
            ready_out_dds           => ready_out_dds,
            valid_out_dds           => valid_out_dds,
            data_out_dds            => data_out_dds,
            last_word_out_dds       => last_word_out_dds,
            status                  => status
        );
    
    stimulus_prc : process
        variable RV                 : RandomPType;
        variable kh1, kh2, kh3, kh4 : INSTANCE_HANDLE_TYPE := HANDLE_NIL;
        variable cc1, cc2, cc3, cc4, cc : CACHE_CHANGE_TYPE := DEFAULT_CACHE_CHANGE;
        
        impure function gen_payload(key_hash : INSTANCE_HANDLE_TYPE; len : natural) return TEST_PACKET_TYPE is
            variable ret : TEST_PACKET_TYPE := EMPTY_TEST_PACKET;
        begin
            assert (len >= 4) report "Payload length has to be at least 16 Bytes long" severity FAILURE;
            
            for i in 0 to len-1 loop
                if (i < 4) then
                    -- NOTE: Beginning of payload is negated key to allow deterministic Key Hash generation from the kh_prc
                    ret.data(ret.length) := not key_hash(i);
                else
                    ret.data(ret.length) := RV.RandSlv(WORD_WIDTH);
                end if;
                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 := (others => (others => '0'));
        begin
            for i in 0 to KEY_HASH_TYPE'length-1 loop
                ret(i) := RV.RandSlv(WORD_WIDTH);
            end loop;
            return ret;
        end function;
        
        procedure start_dds is
        begin
            dds_start  <= '1';
            wait until rising_edge(clk);
            dds_start  <= '0';
            wait until rising_edge(clk);
        end procedure;
        
        procedure start_rtps is
        begin
            rtps_start   <= '1';
            wait until rising_edge(clk);
            rtps_start   <= '0';
            wait until rising_edge(clk);
        end procedure;
        
        procedure wait_on_dds is
        begin
            if (dds_done /= '1') then
                wait until dds_done = '1';
            end if;
        end procedure;
        
        procedure wait_on_rtps is
        begin
            if (rtps_done /= '1') then
                wait until rtps_done = '1';
            end if;
        end procedure;
        
        procedure wait_on_completion is
        begin
            if (rtps_done /= '1' or dds_done /= '1') then
                wait until rtps_done = '1' and dds_done = '1';
            end if;
        end procedure;
        
    begin
        
        SetAlertLogName("dds_writer - (KEEP ALL, Finite Lifespan, Keyed) - Level 0 - General");
        SetAlertEnable(FAILURE, TRUE);
        SetAlertEnable(ERROR, TRUE);
        SetAlertEnable(WARNING, TRUE);
        SetLogEnable(DEBUG, FALSE);
        SetLogEnable(PASSED, FALSE);
        SetLogEnable(INFO, TRUE);
        RV.InitSeed(RV'instance_name);
        inst_id <= GetAlertLogID("Instance", ALERTLOG_BASE_ID);
        kind_id <= GetAlertLogID("Cache Change Kind", ALERTLOG_BASE_ID);
        sn_id   <= GetAlertLogID("SequenceNumber", ALERTLOG_BASE_ID);
        ts_id   <= GetAlertLogID("TimeStamp", ALERTLOG_BASE_ID);
        data_id <= GetAlertLogID("Data Out", ALERTLOG_BASE_ID);
        ret_id  <= GetAlertLogID("Return Code", ALERTLOG_BASE_ID);
        
        -- Key Hashes
        kh1 := gen_key_hash;
        kh2 := gen_key_hash;
        kh3 := gen_key_hash;
        kh4 := gen_key_hash;
        
        
        
        Log("Initiating Test", INFO);
        reset   <= '1';
        wait until rising_edge(clk);
        wait until rising_edge(clk);
        reset   <= '0';
        -- Stored CC: 0, 0, 0, 0
        
        -- TEST: GET_MIN_SN/GET_MAX_SN ON EMPTY
        
        Log("RTPS Operation GET_MIN_SN (Expected SEQUENCENUMBER_UNKNOWN)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := SEQUENCENUMBER_UNKNOWN;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SEQUENCENUMBER_UNKNOWN)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := SEQUENCENUMBER_UNKNOWN;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: WRITE ALIGNED PAYLOAD
        -- TEST: NORMAL WRITE
        -- TEST: ADD SAMPLE WITH KEY_HASH [UNKNOWN INSTANCE]
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(1);
        cc.src_timestamp    := gen_duration(1,0);
        
        Log("DDS Operation WRITE [TS 1s, Instance 1] (REJECTED: Instance not Registered)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_BAD_PARAMETER;
        start_dds;
        wait_on_dds;
        
        -- TEST: ADD SAMPLE WITH HANDLE_NIL [UNKNOWN INSTANCE]
        
        Log("DDS Operation WRITE [TS 1s, Instance 1, HANDLE_NIL] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I1S1, 0, 0, 0
        
        -- TEST: GET_MIN_SN/GET_MAX_SN ON 1 SAMPLE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 1)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(1);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 1)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(1);
        start_rtps;
        wait_on_rtps;
        
        -- TEST: WRITE UNALIGNED PAYLOAD [>1 SLOT]
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,12);
        cc.seq_nr           := gen_sn(2);
        cc.src_timestamp    := gen_duration(2,0);
        
        Log("DDS Operation WRITE [TS 2s, Instance 2, Unaligned Payload (2 Slots)] (REJECTED: Instance not Registered)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_BAD_PARAMETER;
        start_dds;
        wait_on_dds;
        
        -- TEST: NORMAL REGISTER
        
        Log("DDS Operation REGISTER_INSTANCE 2 (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        
        -- TEST: ADD SAMPLE WITH KEY_HASH [KNOWN INSTANCE]
        
        Log("DDS Operation WRITE [TS 2s, Instance 2, Unaligned Payload (2 Slots)] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I1S1, I2S2, 0, 0
        
        -- TEST: ADD SAMPLE WITH HANDLE_NIL [KNOWN INSTANCE]
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,15);
        cc.seq_nr           := gen_sn(3);
        cc.src_timestamp    := gen_duration(3,0);
        
        Log("DDS Operation WRITE [TS 3s, Instance 1, Unaligned Payload (2 Slots)] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I1S1, I2S2, I1S3, 0
        
        -- TEST: GET_CACHE_CHANGE [UNKNOWN SN]
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 4 (Invalid)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc.seq_nr := gen_sn(4);
        rtps.ret_code  := INVALID;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: GET_CACHE_CHANGE [KNOWN SN, ALIGNED PAYLOAD]
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 1", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: GET_CACHE_CHANGE [KNOWN SN, UNALIGNED PAYLOAD, >1 SLOT]
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 2", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 3", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: WRITE UNALIGNED PAYLOAD [<1 SLOT]
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,8);
        cc.seq_nr           := gen_sn(4);
        cc.src_timestamp    := gen_duration(4,0);
        
        -- TEST: WRITE ON PAYLOAD MEMORY FULL [WITHOUT ACKed SAMPLES]
        
        Log("DDS Operation WRITE [TS 4s, Instance 3, HANDLE_NIL, Unaligned Payload (1 Slot)] (REJECTED: Payload Memory Full)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        -- Stored CC: I1S1, I2S2, I1S3, 0
        
        -- TEST: REMOVE_CACHE_CHANGE [UNKNOWN SN]
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 5 (Invalid)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc.seq_nr := gen_sn(5);
        rtps.ret_code  := INVALID;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: REMOVE_CACHE_CHANGE [KNOWN SN]
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 2", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        -- Stored CC: I1S1, 0, I1S3, 0
        
        Log("DDS Operation WRITE [TS 4s, Instance 3, HANDLE_NIL, Unaligned Payload (1 Slot)] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I1S1, I3S4, I1S3, 0
        
        -- TEST: GET_CACHE_CHANGE [KNOWN SN, UNALIGNED PAYLOAD, <1 SLOT]
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 4", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: REGISTER INSTANCE [KNOWN INSTANCE]
        
        Log("DDS Operation REGISTER_INSTANCE 3 (No Change)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc2;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        
        -- TEST: NORMAL DISPOSE
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := TRUE;
        cc.kind             := NOT_ALIVE_DISPOSED;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,5);
        cc.seq_nr           := gen_sn(5);
        cc.src_timestamp    := gen_duration(5,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITHOUT ACKed SAMPLES]
        
        Log("DDS Operation DISPOSE [TS 5s, Instance 1] (REJECTED: MAX_SAMPLES_PER_INSTANCE Exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := DISPOSE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        -- TEST: NORMAL ACK_CACHE_CHANGE
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 4", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITH ACKed SAMPLES, WITHOUT ACKed INSTANCE SAMPLES]
        
        Log("DDS Operation DISPOSE [TS 5s, Instance 1] (REJECTED: MAX_SAMPLES_PER_INSTANCE Exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := DISPOSE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 1", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITH ACKed INSTANCE SAMPLE]
        
        Log("DDS Operation DISPOSE [TS 5s, Instance 1] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := DISPOSE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I1S5, I3S4, I1S3, 0
        
        -- VALIDATE STATE
        -- TEST: GET_MIN_SN/GET_MAX_SN ON >1 SAMPLE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 3)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(3);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 5)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(5);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 5", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(6);
        cc.src_timestamp    := gen_duration(6,0);
        
        -- TEST: ADD SAMPLE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITHOUT STALE INSTANCES]
        
        Log("DDS Operation WRITE [TS 6s, Instance 4, HANDLE_NIL, Aligned Payload] (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 3", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ACK_CACHE_CHANGE [ALREADY ACKed SN]
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 4", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 5", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITH FULLY ACKed INSTANCE, WITHOUT STALE INSTANCE]
        
        Log("DDS Operation WRITE [TS 6s, Instance 4, HANDLE_NIL, Aligned Payload] (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        -- TEST: NORMAL UNREGISTER
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := TRUE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,5);
        cc.seq_nr           := gen_sn(6);
        cc.src_timestamp    := gen_duration(6,0);
        
        -- TEST: UNREGISTER ON DISPOSED INSTANCE
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 6s, Instance 1] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I1S5, I3S4, I1S6, 0
        
        -- VALIDATE STATE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 4)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(4);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 6)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(6);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 6", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(7);
        cc.src_timestamp    := gen_duration(7,0);
        
        Log("DDS Operation WRITE [TS 7s, Instance 4, HANDLE_NIL, Aligned Payload] (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 6", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITH STALE INSTANCE]
        -- TEST: REMOVE STALE INSTANCE WITH >1 SAMPLES
        
        Log("DDS Operation WRITE [TS 7s, Instance 4, HANDLE_NIL, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I4S7, I3S4, 0, 0
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 5 (Invalid)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc.seq_nr := gen_sn(5);
        rtps.ret_code  := INVALID;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 6 (Invalid)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc.seq_nr := gen_sn(6);
        rtps.ret_code  := INVALID;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 7", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,15);
        cc.seq_nr           := gen_sn(8);
        cc.src_timestamp    := gen_duration(8,0);
        
        Log("DDS Operation WRITE [TS 8s, Instance 2, Unaligned Payload (2 Slot)] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I4S7, I3S4, I2S8, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(9);
        cc.src_timestamp    := gen_duration(9,0);
        
        Log("DDS Operation WRITE [TS 9s, Instance 1, Aligned Payload] (REJECTED: Instance not Registered)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_BAD_PARAMETER;
        start_dds;
        wait_on_dds;
        
        -- TEST: REGISTER INSTANCE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITHOUT STALE INSTANCE]
        
        Log("DDS Operation REGISTER_INSTANCE 1 (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        start_dds;
        wait_on_dds;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,5);
        cc.seq_nr           := gen_sn(9);
        cc.src_timestamp    := gen_duration(9,0);
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 9s, Instance 3] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I4S7, I3S4, I2S8, I3S9
        
        -- VALIDATE STATE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 4)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(4);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 9)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(9);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 8", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 9", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 9", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(10);
        cc.src_timestamp    := gen_duration(10,0);
        
        -- TEST: REGISTER INSTANCE ON MAX_INSTANCES [UNKNOWN INSTANCE, WITH STALE INSTANCE]
        
        Log("DDS Operation REGISTER_INSTANCE 1 (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        
        Log("DDS Operation WRITE [TS 10s, Instance 1, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I4S7, I1S10, I2S8, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(11);
        cc.src_timestamp    := gen_duration(11,0);
        
        Log("DDS Operation WRITE [TS 11s, Instance 1, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I4S7, I1S10, I2S8, I4S11
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,10);
        cc.seq_nr           := gen_sn(12);
        cc.src_timestamp    := gen_duration(12,0);
        
        Log("DDS Operation WRITE [TS 12s, Instance 2, Aligned Payload] (REJECTED: Payload Memory Full)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 8", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: WRITE ON PAYLOAD MEMORY FULL [WITH ACKed SAMPLES]
        
        Log("DDS Operation WRITE [TS 12s, Instance 2, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I4S7, I1S10, I2S12, I4S11
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(13);
        cc.src_timestamp    := gen_duration(13,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES [KNOWN INSTANCE,WITHOUT ACKed SAMPLES]
        
        Log("DDS Operation WRITE [TS 13s, Instance 4, Aligned Payload] (REJECTED: MAX_SAMPLES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 7", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES [KNOWN INSTANCE,WITH ACKed SAMPLES]
        
        Log("DDS Operation WRITE [TS 13s, Instance 4, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I4S13, I1S10, I2S12, I4S11
        
        -- VALIDATE STATE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 4)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(10);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 9)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(13);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 10", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 11", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 12", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 13", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 12", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,5);
        cc.seq_nr           := gen_sn(14);
        cc.src_timestamp    := gen_duration(14,0);
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 14s, Instance 2] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I4S13, I1S10, I2S14, I4S11
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 11", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,20);
        cc.seq_nr           := gen_sn(15);
        cc.src_timestamp    := gen_duration(15,0);
        
        -- TEST: ADD SAMPLE BIGGER THAN AVAILABLE MEMORY SPACE [WITH ACKed SAMPLES]
        
        Log("DDS Operation WRITE [TS 15s, Instance 4, Aligned Payload (2 Slots)] (REJECTED: Payload Memory Full)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 11", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        -- Stored CC: I4S13, I1S10, I2S14, 0
        
        Log("DDS Operation WRITE [TS 15s, Instance 4, Aligned Payload (2 Slots)] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I4S13, I1S10, I2S14, I4S15
        
        -- TEST: UNREGISTER INSTANCE ON PAYLOAD MEMORY FULL [WITHOUT ACKed SAMPLES]
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,5);
        cc.seq_nr           := gen_sn(16);
        cc.src_timestamp    := gen_duration(16,0);
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 16s, Instance 1] (REJECTED: Payload memory Full, MAX_SAMPLES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 10", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: UNREGISTER INSTANCE ON PAYLOAD MEMORY FULL [WITH ACKed SAMPLES]
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 16s, Instance 1] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I4S13, I1S16, I2S14, I4S15
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_DISPOSED;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,5);
        cc.seq_nr           := gen_sn(17);
        cc.src_timestamp    := gen_duration(17,0);
        
        -- TEST: DISPOSE ON PAYLOAD MEMORY FULL [WITHOUT ACKed SAMPLES]
        
        Log("DDS Operation DISPOSE [TS 17s, Instance 3] (REJECTED: Payload memory Full, MAX_SAMPLES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := DISPOSE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 13", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 14", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: DISPOSE ON PAYLOAD MEMORY FULL [WITH ACKed SAMPLES]
        -- TEST: ADD SAMPLE ON PAYLOAD FULL & MAX_INSTANCES [UNKNOWN INSTANCE,WITH ACKed SAMPLES,WITH STALE INSTANCE (>= 1 SAMPLE)] (Induce Double Remove)
        -- TEST: REMOVE STALE INSTANCE WITH 1 SAMPLES
        
        Log("DDS Operation DISPOSE [TS 17s, Instance 3] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := DISPOSE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I3S17, I1S16, 0, I4S15
        
        -- VALIDATE STATE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 15)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(15);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 17)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(17);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 15", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 16", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 17", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 15", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        -- Stored CC: I3S17, I1S16, 0, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,10);
        cc.seq_nr           := gen_sn(18);
        cc.src_timestamp    := gen_duration(18,0);
        
        -- TEST: WRITE ON DISPOSED INSTANCE
        
        Log("DDS Operation WRITE [TS 18s, Instance 3, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I3S17, I1S16, I3S18, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(19);
        cc.src_timestamp    := gen_duration(19,0);
        
        Log("DDS Operation WRITE [TS 19s, Instance 4, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I3S17, I1S16, I3S18, I4S19
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,10);
        cc.seq_nr           := gen_sn(20);
        cc.src_timestamp    := gen_duration(20,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITHOUT STALE INSTANCES,WITHOUT ACKed SAMPLES]
        
        Log("DDS Operation WRITE [TS 20s, Instance 2, Aligned Payload] (REJECTED: MAX_SAMPLES exceeded, MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 16", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITH STALE INSTANCE (>0 SAMPLES)]
        
        Log("DDS Operation WRITE [TS 20s, Instance 2, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I3S17, I2S20, I3S18, I4S19
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 17", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,5);
        cc.seq_nr           := gen_sn(21);
        cc.src_timestamp    := gen_duration(21,0);
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 21s, Instance 4] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I4S21, I2S20, I3S18, I4S19
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 19", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(22);
        cc.src_timestamp    := gen_duration(22,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITHOUT STALE INSTANCE, WITH ACKed SAMPLE]
        
        Log("DDS Operation WRITE [TS 22s, Instance 1, Aligned Payload] (REJECTED: MAX_SAMPLES exceeded, MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 19", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        -- Stored CC: I4S21, I2S20, I3S18, 0
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 21", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        -- Stored CC: 0, I2S20, I3S18, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,5);
        cc.seq_nr           := gen_sn(22);
        cc.src_timestamp    := gen_duration(22,0);
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 22s, Instance 2] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I2S22, I2S20, I3S18, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,5);
        cc.seq_nr           := gen_sn(23);
        cc.src_timestamp    := gen_duration(23,0);
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 23s, Instance 3] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I2S22, I2S20, I3S18, I3S23
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITH STALE INSTANCE, WITHOUT ACKed SAMPLE]
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(24);
        cc.src_timestamp    := gen_duration(24,0);
        
        Log("DDS Operation WRITE [TS 24s, Instance 1, Aligned Payload] (REJECTED: MAX_SAMPLES exceeded, MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 20", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: REMOVE STALE INSTANCE WITH 0 SAMPLES
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_INSTANCES [UNKNOWN INSTANCE,WITH STALE INSTANCE, WITH ACKed SAMPLE]
        
        Log("DDS Operation WRITE [TS 24s, Instance 1, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I2S22, I1S24, I3S18, I3S23
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: NORMAL NACK_CACHE_CHANGE
        
        Log("RTPS Operation NACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := NACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(25);
        cc.src_timestamp    := gen_duration(25,0);
        
        Log("DDS Operation REGISTER_INSTANCE 4 (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: REGISTER ON UNREGISTERED INSTANCE
        
        Log("DDS Operation REGISTER_INSTANCE 2 (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc1;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        
        Log("DDS Operation REGISTER_INSTANCE 4 (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 18", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 23", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,10);
        cc.seq_nr           := gen_sn(25);
        cc.src_timestamp    := gen_duration(25,0);
        
        -- TEST: WRITE ON UNREGISTERED INSTANCE
        
        Log("DDS Operation WRITE [TS 25s, Instance 3, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I2S22, I1S24, I3S25, I3S23
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(26);
        cc.src_timestamp    := gen_duration(26,0);
        
        Log("DDS Operation REGISTER_INSTANCE 4 (REJECTED: MAX_INSTANCES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        start_dds;
        wait_on_dds;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,5);
        cc.seq_nr           := gen_sn(26);
        cc.src_timestamp    := gen_duration(26,0);
        
        -- TEST: UNREGISTER UNKNOWN INSTANCE
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 26s, HANDLE_NIL, Instance 4] (IGNORED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation NACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := NACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation NACK_CACHE_CHANGE SN 23", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := NACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := TRUE;
        cc.kind             := NOT_ALIVE_UNREGISTERED;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,5);
        cc.seq_nr           := gen_sn(26);
        cc.src_timestamp    := gen_duration(26,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE,WITHOUT ACKed SAMPLES]
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 26s, Instance 3] (REJECTED: MAX_SAMPLES_PER_INSTANCE exceeded, MAX_SAMPLES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE,WITH ACKed SAMPLES, WITHOUT ACKed INSTANCE SAMPLES]
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 26s, Instance 3] (REJECTED: MAX_SAMPLES_PER_INSTANCE exceeded, MAX_SAMPLES exceeded)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OUT_OF_RESOURCES;
        start_dds;
        wait_on_dds;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 23", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES & MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE,WITH ACKed INSTANCE SAMPLES]
        
        Log("DDS Operation UNREGISTER_INSTANCE [TS 26s, Instance 3] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := UNREGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I2S22, I1S24, I3S25, I3S26
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 25", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := TRUE;
        cc.kind             := NOT_ALIVE_DISPOSED;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,5);
        cc.seq_nr           := gen_sn(27);
        cc.src_timestamp    := gen_duration(27,0);
        
        -- TEST: DISPOSE ON UNREGISTERED INSTANCE
        
        Log("DDS Operation DISPOSE [TS 27s, Instance 3] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := DISPOSE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I2S22, I1S24, I3S27, I3S26
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 26", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 27", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh4;
        cc.payload          := gen_payload(kh4,10);
        cc.seq_nr           := gen_sn(28);
        cc.src_timestamp    := gen_duration(28,0);
        
        Log("DDS Operation REGISTER_INSTANCE 4 (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := REGISTER_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        -- Stored CC: I2S22, I1S24, 0, 0
        
        -- VALIDATE STATE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 22)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(22);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 24)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(24);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 24", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation NACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := NACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation NACK_CACHE_CHANGE SN 24", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := NACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("DDS Operation WRITE [TS 28s, Instance 4, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc3 := cc;
        -- Stored CC: I2S22, I1S24, I4S28, 0
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,10);
        cc.seq_nr           := gen_sn(29);
        cc.src_timestamp    := gen_duration(29,0);
        
        Log("DDS Operation WRITE [TS 29s, Instance 2, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc4 := cc;
        -- Stored CC: I2S22, I1S24, I4S28, I2S29
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 24", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 28", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(30);
        cc.src_timestamp    := gen_duration(30,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES [KNOWN INSTANCE,WITH ACKed SAMPLES (>1)]
        
        Log("DDS Operation WRITE [TS 30s, Instance 1, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc2 := cc;
        -- Stored CC: I2S22, I1S30, I4S28, I2S29
        
        Log("RTPS Operation REMOVE_CACHE_CHANGE SN 28", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := REMOVE_CACHE_CHANGE;
        rtps.cc        := cc3;
        start_rtps;
        wait_on_rtps;
        -- Stored CC: I2S22, I1S30, 0, I2S29
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 22", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation ACK_CACHE_CHANGE SN 29", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := ACK_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh2;
        cc.payload          := gen_payload(kh2,10);
        cc.seq_nr           := gen_sn(31);
        cc.src_timestamp    := gen_duration(31,0);
        
        -- TEST: ADD SAMPLE ON MAX_SAMPLES_PER_INSTANCE [KNOWN INSTANCE, WITH ACKed INSTANCE SAMPLES(>1)]
        
        Log("DDS Operation WRITE [TS 31s, Instance 2, Aligned Payload] (ACCEPTED)", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := WRITE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        cc1 := cc;
        -- Stored CC: I2S31, I1S30, 0, I2S29
        
        -- VALIDATE STATE
        
        Log("RTPS Operation GET_MIN_SN (Expected SN 29)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MIN_SN;
        rtps.cc.seq_nr := gen_sn(29);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_MAX_SN (Expected SN 31)", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_MAX_SN;
        rtps.cc.seq_nr := gen_sn(31);
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 29", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc4;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 30", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc2;
        start_rtps;
        wait_on_rtps;
        
        Log("RTPS Operation GET_CACHE_CHANGE SN 31", INFO);
        rtps           := DEFAULT_RTPS_WRITER_TEST;
        rtps.opcode    := GET_CACHE_CHANGE;
        rtps.cc        := cc1;
        start_rtps;
        wait_on_rtps;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh1;
        cc.payload          := gen_payload(kh1,10);
        cc.seq_nr           := gen_sn(1);
        cc.src_timestamp    := gen_duration(1,0);
        
        -- TEST: INSTANCE LOOKUP [KNOWN INSTANCE]
        
        Log("DDS Operation LOOKUP_INSTANCE [Instance 1]", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := LOOKUP_INSTANCE;
        dds.cc         := cc;
        dds.ret_code   := RETCODE_OK;
        start_dds;
        wait_on_dds;
        
        cc                  := DEFAULT_CACHE_CHANGE;
        cc.serialized_key   := FALSE;
        cc.kind             := ALIVE;
        cc.instance         := kh3;
        cc.payload          := gen_payload(kh3,10);
        cc.seq_nr           := gen_sn(1);
        cc.src_timestamp    := gen_duration(1,0);
        
        -- TEST: INSTANCE LOOKUP [UNKNOWN INSTANCE]
        
        Log("DDS Operation LOOKUP_INSTANCE [Unknown Instance]", INFO);
        dds            := DEFAULT_DDS_TEST;
        dds.opcode     := LOOKUP_INSTANCE;
        dds.cc         := cc;
        dds.cc.instance:= HANDLE_NIL;
        start_dds;
        wait_on_dds;
        
        wait_on_completion;
        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;
    
    alert_prc : process(all)
    begin
        if rising_edge(clk) then
            -- TODO
        end if;
    end process;
    
    dds_prc : process(all)
    begin
        if rising_edge(clk) then
            dds_done   <= '0';
            case (dds_stage) is
                when IDLE =>
                    if (dds_start = '1') then
                        dds_stage  <= START;
                    else
                        dds_done   <= '1';
                    end if;
                when START =>
                    if (ack_dds = '1') then
                        dds_stage  <= PUSH;
                        dds_cnt    <= 0;
                    end if;
                when PUSH =>
                    if (ready_in_dds = '1') then
                        dds_cnt    <= dds_cnt + 1;
                        if (dds_cnt = dds.cc.payload.length-1) then
                            -- DEFAULT
                            dds_stage  <= DONE;
                            
                            case (dds.opcode) is
                                when REGISTER_INSTANCE =>
                                    dds_stage  <= CHECK;
                                    dds_cnt    <= 0;
                                when LOOKUP_INSTANCE =>
                                    dds_stage  <= CHECK;
                                    dds_cnt    <= 0;
                                when others =>
                                    null;
                            end case;
                        end if;
                    end if;
                when DONE =>
                    if (done_dds = '1') then
                        AffirmIfEqual(ret_id, return_code_dds, dds.ret_code);
                        dds_stage  <= IDLE;
                    end if;
                when CHECK =>
                    if (valid_out_dds = '1') then
                        AffirmIfEqual(data_id, data_out_dds, dds.cc.instance(dds_cnt));
                        dds_cnt <= dds_cnt + 1;
                        if (dds_cnt = 3) then
                            AlertIf(data_id, last_word_out_dds /= '1', "Last Word Signal not pulled High", ERROR);
                            dds_stage  <= IDLE;
                        end if;
                    end if;
            end case;
        end if;
        
        -- DEFAULT
        start_dds           <= '0';
        opcode_dds          <= NOP;
        valid_in_dds        <= '0';
        last_word_in_dds    <= '0';
        data_in_dds         <= (others => '0');
        instance_handle_dds <= HANDLE_NIL;
        source_ts_dds       <= TIME_INVALID;
        ready_out_dds       <= '0';
        
        case (dds_stage) is
            when START =>
                start_dds           <= '1';
                opcode_dds          <= dds.opcode;
                instance_handle_dds <= dds.cc.instance;
                source_ts_dds       <= dds.cc.src_timestamp;
            when PUSH =>
                valid_in_dds        <= '1';
                data_in_dds         <= dds.cc.payload.data(dds_cnt);
                last_word_in_dds    <= dds.cc.payload.last(dds_cnt);
            when CHECK =>
                ready_out_dds       <= '1';
            when others =>
                null;
        end case;
    end process;
    
    rtps_prc : process(all)
    begin
        if rising_edge(clk) then
            rtps_done    <= '0';
            case (rtps_stage) is
                when IDLE =>
                    if (rtps_start = '1') then
                        rtps_stage   <= START;
                    else
                        rtps_done    <= '1';
                    end if;
                when START =>
                    if (ack_rtps = '1') then
                        rtps_stage   <= DONE;
                    end if;
                when DONE =>
                    if (done_rtps = '1') then
                        -- DEFAULT
                        rtps_stage   <= IDLE;
                        
                        AffirmIfEqual(ret_id, HISTORY_CACHE_RESPONSE_TYPE'pos(ret_rtps), HISTORY_CACHE_RESPONSE_TYPE'pos(rtps.ret_code));
                        case (rtps.opcode) is
                            when GET_CACHE_CHANGE =>
                                if (rtps.ret_code = OK) then
                                    AffirmIfEqual(inst_id, to_unsigned(cc_instance_handle), to_unsigned(rtps.cc.instance));
                                    AffirmIfEqual(kind_id, CACHE_CHANGE_KIND_TYPE'pos(cc_kind), CACHE_CHANGE_KIND_TYPE'pos(rtps.cc.kind));
                                    AffirmIfEqual(sn_id, convert_from_double_word(cc_seq_nr), convert_from_double_word(rtps.cc.seq_nr));
                                    AffirmIfEqual(ts_id, convert_from_double_word(cc_source_timestamp), convert_from_double_word(rtps.cc.src_timestamp));
                                    rtps_stage   <= CHECK;
                                    rtps_cnt     <= 0;
                                end if;
                            when GET_MIN_SN =>
                                AffirmIfEqual(sn_id, convert_from_double_word(cc_seq_nr), convert_from_double_word(rtps.cc.seq_nr));
                            when GET_MAX_SN =>
                                AffirmIfEqual(sn_id, convert_from_double_word(cc_seq_nr), convert_from_double_word(rtps.cc.seq_nr));
                            when others =>
                                null;
                        end case;
                    end if;
                when CHECK =>
                    if (valid_out_rtps = '1') then
                        AffirmIfEqual(data_id, last_word_out_rtps & data_out_rtps, rtps.cc.payload.last(rtps_cnt) & rtps.cc.payload.data(rtps_cnt));
                        rtps_cnt <= rtps_cnt + 1;
                        if (rtps_cnt = rtps.cc.payload.length-1) then
                            rtps_stage   <= IDLE;
                        end if;
                    end if;
            end case;
        end if;
        
        -- DEFAULT
        start_rtps      <= '0';
        opcode_rtps     <= NOP;
        seq_nr_rtps     <= SEQUENCENUMBER_UNKNOWN;
        get_data_rtps   <= '0';
        ready_out_rtps  <= '0';
        
        case (rtps_stage) is
            when START =>
                start_rtps  <= '1';
                opcode_rtps <= rtps.opcode;
                seq_nr_rtps <= rtps.cc.seq_nr;
            when DONE =>
                if (done_rtps = '1') then
                    case (rtps.opcode) is
                        when GET_CACHE_CHANGE =>
                            get_data_rtps   <= '1';
                        when others =>
                            null;
                    end case;
                end if;
            when CHECK =>
                ready_out_rtps  <= '1';
            when others =>
                null;
        end case;
    end process;
    
    kh_prc : process (all)
        variable tmp_key_hash : INSTANCE_HANDLE_TYPE := HANDLE_NIL;
    begin
        if rising_edge(clk) then
            case (kh_stage) is
                when IDLE =>
                    if (start_kh = '1') then
                        case (opcode_kh) is
                            when PUSH_DATA =>
                                kh_stage    <= READ_DATA;
                                kh_cnt      <= 0;
                                kh_data     <= EMPTY_TEST_PACKET;
                            when PUSH_SERIALIZED_KEY =>
                                kh_stage    <= READ_DATA;
                                kh_cnt      <= 0;
                                kh_data     <= EMPTY_TEST_PACKET;
                            when READ_KEY_HASH =>
                                kh_stage    <= PUSH_KEY_HASH;
                                kh_cnt      <= 0;
                            when others =>
                                Alert("Unexpected Key Holder Operation", FAILURE);
                        end case;
                    end if;
                when READ_DATA =>
                    if (valid_out_kh = '1') then
                        kh_data.data(kh_cnt)    <= data_out_kh;
                        kh_data.last(kh_cnt)    <= last_word_out_kh;
                        kh_data.length          <= kh_data.length + 1;
                        
                        kh_cnt  <= kh_cnt + 1;
                        if (last_word_out_kh = '1') then
                            kh_stage    <= IDLE;
                        end if;
                    end if;
                when PUSH_KEY_HASH =>
                    if (ready_in_kh = '1') then
                        kh_cnt  <= kh_cnt + 1;
                        if (kh_cnt = INSTANCE_HANDLE_TYPE'length-1) then
                            kh_stage    <= IDLE;
                        end if;
                    end if;
            end case;
        end if;
        
        -- DEFAULT
        ack_kh              <= '0';
        ready_out_kh        <= '0';
        valid_in_kh         <= '0';
        data_in_kh          <= (others => '0');
        last_word_in_kh     <= '0';
        
        case (kh_stage) is
            when IDLE =>
                if (start_kh = '1') then
                    ack_kh  <= '1';
                end if;
            when READ_DATA =>
                ready_out_kh    <= '1';
            when PUSH_KEY_HASH =>
                valid_in_kh     <= '1';
                tmp_key_hash    := extract_key_hash(kh_data);
                data_in_kh      <= tmp_key_hash(kh_cnt);
                if (kh_cnt = INSTANCE_HANDLE_TYPE'length-1) then
                    last_word_in_kh <= '1';
                end if;
        end case;
    end process;
    
    watchdog : process
    begin
        wait for 1 ms;
        Alert("Test timeout", FAILURE);
        std.env.stop;
    end process;
    
end architecture;