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

use work.rtps_package.all;
use work.Type1_package.all;

package user_config is
    
    --*****USER CONFIG*****
    -- NOTE: All strings have to be padded to 256 characters
    
    -- Period of system clock
    constant CLOCK_PERIOD               : time := 20 ns; -- 50 MHz
    -- Maximum number of supported remote Participants (Affects generated Memory Size)
    constant MAX_REMOTE_PARTICIPANTS    : natural := 50;
    -- Maximum number of supported remote Endpoints (Affects generated Memory Size)
    constant MAX_REMOTE_ENDPOINTS       : natural := 50;
    -- If Multicast Locators should be used instead of Unicast Locators
    constant PREFER_MULTICAST_LOCATORS  : boolean := FALSE;
    
    -- Unicast IPv4 Address used by all RTPS Entities [Default 192.168.0.90]
    constant DEFAULT_IPv4_ADDRESS   : std_logic_vector(IPv4_ADDRESS_WIDTH-1 downto 0) := x"C0A8005A";
    -- Number of RTPS Writer Endpoints
    constant NUM_WRITERS            : natural := 1;
    -- Number of RTPS Reader Endpoints
    constant NUM_READERS            : natural := 1; 
    -- Number of RTPS Endpoints (Do not modify)
    constant NUM_ENDPOINTS          : natural := NUM_READERS+NUM_WRITERS;
    -- PB Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)
    constant PORT_CONFIG_PB         : natural := 7400;
    -- DG Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)    
    constant PORT_CONFIG_DG         : natural := 250;
    -- PG Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)
    constant PORT_CONFIG_PG         : natural := 2; 
    -- D0 Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)
    constant PORT_CONFIG_D0         : natural := 0; 
    -- D1 Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)
    constant PORT_CONFIG_D1         : natural := 10;
    -- D2 Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)    
    constant PORT_CONFIG_D2         : natural := 1; 
    -- D3 Value of Default Port Generation (see DDSI-RTPS 2.3 Section 9.6.1)
    constant PORT_CONFIG_D3         : natural := 11;
    -- MAC Address of underlying network stack (Used to generate GUIDs)
    constant MAC_ADDRESS            : std_logic_vector(47 downto 0) := x"924DD9E79DE6";
    -- Domain ID
    constant USER_DOMAIN_ID         : natural := 1;
    -- Domain TAG
    constant USER_DOMAIN_TAG        : string(1 to 256) := gen_user_string("");
    
    -- *TIMING CHARACTERISTICS*
    -- Timing Characteristics for Participant
    constant PARTICIPANT_ANNOUNCEMENT_PERIOD    : DURATION_TYPE := gen_duration(10 sec);
    constant PARTICIPANT_LEASE_DURATION         : DURATION_TYPE := DEFAULT_PARTICIPANT_LEASE_DURATION;
    -- Denotes how much faster then the deadline/period we schedule in order to account for transport delay.
    constant DURATION_DELTA                     : DURATION_TYPE := gen_duration(100 ms);
    -- Timing Characteristics for built-in Endpoints
    constant PARTICIPANT_HEARTBEAT_PERIOD               : DURATION_TYPE := gen_duration(1 sec);
    constant PARTICIPANT_HEARTBEAT_RESPONSE_DELAY       : DURATION_TYPE := gen_duration(500 ms);
    constant PARTICIPANT_HEARTBEAT_SUPPRESSION_DELAY    : DURATION_TYPE := gen_duration(0 ms);
    constant PARTICIPANT_ACKNACK_RESPONSE_DELAY         : DURATION_TYPE := gen_duration(200 ms);
    constant PARTICIPANT_ACKNACK_SUPPRESSION_DELAY      : DURATION_TYPE := gen_duration(0 ms);

    -- *ENDPOINT CONFIG*
    constant ENDPOINT_CONFIG : CONFIG_ARRAY_TYPE(0 to NUM_ENDPOINTS-1); -- Deferred to Package Body
    
    -- Set to TRUE for Simulation Testing (Extra Code generated)
    constant SIMULATION_FLAG : boolean := FALSE;
end package;

package body user_config is

    function gen_config return CONFIG_ARRAY_TYPE is
        variable ret : CONFIG_ARRAY_TYPE(0 to NUM_ENDPOINTS-1);
        variable c : CONFIG_TYPE;
    begin
        
        c   := DEFAULT_WRITER_CONFIG;
        c.WITH_KEY                      := TRUE;
        c.PUSH_MODE                     := TRUE;
        c.TOPICNAME                     := gen_user_string("Topic1");
        c.TYPENAME                      := gen_user_string("Type1");
        c.DURABILITY_QOS                := TRANSIENT_LOCAL_DURABILITY_QOS;
        c.RELIABILITY_QOS               := RELIABLE_RELIABILITY_QOS;
        c.HISTORY_DEPTH                 := std_logic_vector(to_unsigned(5, CDR_LONG_WIDTH));
        c.MAX_SAMPLES                   := std_logic_vector(to_unsigned(20, CDR_LONG_WIDTH));
        c.MAX_INSTANCES                 := std_logic_vector(to_unsigned(5, CDR_LONG_WIDTH));
        c.MAX_PAYLOAD_SIZE              := MAX_TYPE1_SIZE;
        
        ret := (others => c);
        
        return ret;
    end function;
    
    constant ENDPOINT_CONFIG : CONFIG_ARRAY_TYPE(0 to NUM_ENDPOINTS-1) := gen_config;
    
end package body;