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

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

use work.rtps_package.all;
use work.rtps_config_package.all;

entity TYPENAME_writer_interface is
    generic (
        LITTLE_ENDIAN   : std_logic := '0'
    );
    port (
        -- SYSTEM
        clk                         : in std_logic;
        reset                       : in std_logic;
        -- FROM DDS WRITER
        start_dds                   : out std_logic;
        ack_dds                     : in std_logic;
        opcode_dds                  : out DDS_WRITER_OPCODE_TYPE;
        instance_handle_in_dds      : out INSTANCE_HANDLE_TYPE;
        source_ts_dds               : out TIME_TYPE;
        max_wait_dds                : out DURATION_TYPE;
        done_dds                    : in std_logic;
        return_code_dds             : in std_logic_vector(RETURN_CODE_WIDTH-1 downto 0);
        instance_handle_out_dds     : in INSTANCE_HANDLE_TYPE;
        valid_out_dds               : out std_logic;
        ready_out_dds               : in std_logic;
        data_out_dds                : out std_logic_vector(WORD_WIDTH-1 downto 0);
        last_word_out_dds           : out std_logic;
        valid_in_dds                : in std_logic;
        ready_in_dds                : out std_logic;
        data_in_dds                 : in std_logic_vector(WORD_WIDTH-1 downto 0);
        last_word_in_dds            : in std_logic;
        -- Communication Status
        status_dds                  : in std_logic_vector(STATUS_KIND_WIDTH-1 downto 0);
        
        -- TO USER ENTITY
        start_user                  : in std_logic;
        ack_user                    : out std_logic;
        opcode_user                 : in DDS_WRITER_OPCODE_TYPE;
        instance_handle_in_user     : in INSTANCE_HANDLE_TYPE;
        source_ts_user              : in TIME_TYPE;
        max_wait_user               : in DURATION_TYPE;
        done_user                   : out std_logic;
        return_code_user            : out std_logic_vector(RETURN_CODE_WIDTH-1 downto 0);
        instance_handle_out_user    : out INSTANCE_HANDLE_TYPE;
        -- Communication Status
        status_user                 : out std_logic_vector(STATUS_KIND_WIDTH-1 downto 0);
        
        -- ###GENERATED START###
            -- TYPE SPECIFIC PORTS
        -- ###GENERATED END###
        
        encode_done                 : out std_logic
    );
end entity;

architecture arch of TYPENAME_writer_interface is
    
    --*****TYPE DECLARATION*****
    -- FSM states. Explained below in detail
    type STAGE_TYPE is (IDLE,WRITE_PAYLOAD_HEADER,PUSH,ALIGN_STREAM,ENCODE_PAYLOAD);
    -- ###GENERATED START###
    type ENCODE_STAGE_TYPE is (TODO);
        -- TYPES DECLARATIONS
    -- ###GENERATED END###
    
    -- *MAIN PROCESS*
    signal stage, stage_next : STAGE_TYPE;
    signal cnt, cnt_next : natural range 0 to 5;
    signal align_offset, align_offset_next  : unsigned(MAX_ALIGN_OFFSET_WIDTH-1 downto 0);
    signal align_op, align_op_next : std_logic;
    signal target_align, target_align_next : ALIGN_TYPE;
    signal data_out_latch, data_out_latch_next : std_logic_vector(WORD_WIDTH-1 downto 0);
    signal abort_mem : std_logic;
    signal finalize_payload, finalize_payload_next : std_logic;
    signal encode_stage, encode_stage_next : ENCODE_STAGE_TYPE;
    -- ###GENERATED START###
        -- SIGNAL DECLARATION
    -- ###GENERATED END###
    
    
begin
    
    -- ###GENERATED START###
        -- MEMORY INSTANTIATIONS
    -- ###GENERATED END###
    
    -- PASSTHROUGH
    start_dds                   <= start_user;
    ack_user                    <= ack_dds;
    opcode_dds                  <= opcode_user;
    instance_handle_in_dds      <= instance_handle_in_user;
    source_ts_dds               <= source_ts_user;
    max_wait_dds                <= max_wait_user;
    done_user                   <= done_dds;
    return_code_user            <= return_code_dds;
    instance_handle_out_user    <= instance_handle_out_dds;
    status_user                 <= status_dds;
    
    -- ###GENERATED START###
        -- PORT SIGNAL CONNECTIONS
    -- ###GENERATED END###
    
    main_prc : process (all)
    begin
        -- DEFAULT
        stage_next                  <= stage;
        encode_stage_next           <= encode_stage;
        cnt_next                    <= cnt;
        align_offset_next           <= align_offset;
        align_op_next               <= align_op;
        target_align_next           <= target_align;
        data_out_latch_next         <= data_out_latch;
        finalize_payload_next       <= finalize_payload;
        abort_mem                   <= '0';
        encode_done                 <= '0';
        ready_in_dds                <= '0';
        valid_out_dds               <= '0';
        last_word_out_dds           <= '0';
        data_out_dds                <= (others => '0');
        -- ###GENERATED START###
            -- DEFAULT SIGNAL ASSIGNMENTS
        -- ###GENERATED END###
        
        case (stage) is
            when IDLE =>
                -- User Requests Payload
                if (start_user = '1' and ack_dds = '1') then
                    case (opcode_user) is
                        when REGISTER_INSTANCE =>
                            stage_next  <= WRITE_PAYLOAD_HEADER;
                            -- RESET
                            abort_mem   <= '1';
                        when UNREGISTER_INSTANCE =>
                            stage_next  <= WRITE_PAYLOAD_HEADER;
                            -- RESET
                            abort_mem   <= '1';
                        when LOOKUP_INSTANCE =>
                            stage_next  <= WRITE_PAYLOAD_HEADER;
                            -- RESET
                            abort_mem   <= '1';
                        when WRITE =>
                            stage_next  <= WRITE_PAYLOAD_HEADER;
                            -- RESET
                            abort_mem   <= '1';
                        when DISPOSE =>
                            stage_next  <= WRITE_PAYLOAD_HEADER;
                            -- RESET
                            abort_mem   <= '1';
                        when others =>
                            null;
                    end case;
                else
                    -- ###GENERATED START###
                        -- MEMORY SIGNAL CONNECTIONS
                    -- ###GENERATED END###
                end if;
            when WRITE_PAYLOAD_HEADER =>
                valid_out_dds   <= '1';
                if (LITTLE_ENDIAN = '0') then
                    data_out_dds    <= CDR_BE & x"0000";
                else
                    data_out_dds    <= CDR_LE & x"0000";
                end if;
                -- Output Guard
                if (ready_out_dds = '1') then
                    stage_next          <= ENCODE_PAYLOAD;
                    -- Reset 
                    align_offset_next   <= (others => '0');
                    data_out_latch_next <= (others => '0');
                    -- ###GENERATED START###
                    encode_stage_next   <= TODO;
                    -- ###GENERATED END###
                end if;
            when PUSH =>
                -- Mark Last Word
                if (finalize_payload = '1') then
                    last_word_out_dds   <= '1';
                end if;
                
                valid_out_dds   <= '1';
                data_out_dds    <= data_out_latch;
                -- Output Guard
                if (ready_out_dds = '1') then
                    -- NOTE: Ensures all padding is zero.
                    data_out_latch_next <= (others => '0');
                    -- Alignment Operation in process
                    if (align_op = '1') then
                        stage_next      <= ALIGN_STREAM;
                        -- Reset
                        align_op_next   <= '0';
                    -- DONE
                    elsif (finalize_payload = '1') then
                        finalize_payload_next   <= '0';
                        stage_next              <= IDLE;
                    else
                        stage_next  <= ENCODE_PAYLOAD;
                    end if;
                end if;
            when ALIGN_STREAM =>
                -- Target Stream Alignment reached
                if (check_align(align_offset, target_align)) then
                    -- DONE
                    stage_next  <= ENCODE_PAYLOAD;
                else
                    align_offset_next   <= align_offset + 1;
                    -- Need to push Word
                    if (align_offset(1 downto 0) = "11") then
                        align_op_next   <= '1';
                        stage_next      <= PUSH;
                    end if;
                end if;
            when ENCODE_PAYLOAD =>
                case (encode_stage) is
                    -- ###GENERATED START###
                    when TODO =>
                    -- ###GENERATED END###
                    when others =>
                        null;
                end case;
            when others =>
                null;
        end case;
    end process;
    
    sync_prc : process(clk)
    begin
        if rising_edge(clk) then
            if (reset = '1') then
                stage               <= IDLE;
                encode_stage        <= TODO;
                target_align        <= ALIGN_1;
                cnt                 <= 0;
                finalize_payload    <= '0';
                align_op            <= '0';
                align_offset        <= (others => '0');
                data_out_latch      <= (others => '0');
                -- ###GENERATED START###
                    -- RESET SYNC SIGNAL VALUE
                -- ###GENERATED END###
            else
                stage               <= stage_next;
                encode_stage        <= encode_stage_next;
                target_align        <= target_align_next;
                cnt                 <= cnt_next;
                finalize_payload    <= finalize_payload_next;
                align_op            <= align_op_next;
                align_offset        <= align_offset_next;
                data_out_latch      <= data_out_latch_next;
                -- ###GENERATED START###
                    -- SYNC SIGNALS                
                -- ###GENERATED END###
            end if;
        end if;
    end process;
    
end architecture;