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

library ieee_proposed;
use ieee_proposed.fixed_pkg.all;
use ieee_proposed.float_pkg.all;

use work.tools.all;


entity exp_carrier_generator is
    generic (
        g_number_of_prn_elements : integer;
        g_number_of_nc_sums : integer;
        g_sampling_frequency : integer;
        g_number_of_doppler_shifts : integer;
        g_doppler_bandwidth : integer
    );
    port (
        -- Common control signals.
        aclk : in std_logic;
        aclken : in std_logic;
        aresetn : in std_logic;
        -- Data signals.
        result_ready : in std_logic;
        result_valid : out std_logic;
        result_last : out std_logic := '0';
        result_data : out std_logic_vector(31 downto 0) := (others => '0')
    );
end exp_carrier_generator;


architecture rtl of exp_carrier_generator is
    -- Types.
    type shift_array_t is array (0 to g_number_of_doppler_shifts - 1) of float32;
    
    -- Constants.
    constant c_doppler_bandwidth : float32 := to_float(to_unsigned(g_doppler_bandwidth, 23), 8, 23);
    
    
    -- Functions.
    function initialize_shifts return shift_array_t is
        variable v_result : shift_array_t;
        variable v_doppler_step : float32 := c_doppler_bandwidth / to_float(to_unsigned(g_number_of_doppler_shifts, 23), 8, 23);
    begin
        for i in 0 to g_number_of_doppler_shifts - 1 loop
            v_result(i) := to_float(0.5, 8, 23) * (v_doppler_step - c_doppler_bandwidth) + to_float(to_unsigned(i, 23), 8, 23) * v_doppler_step;
        end loop;
        return v_result;
    end function initialize_shifts;
    
    -- Constants.
    constant c_doppler_frequency_multiplier : float32 := to_float(-2.0 / real(g_sampling_frequency), 8, 23);
    constant c_doppler_shifts : shift_array_t := initialize_shifts;
    
    -- Signals.
    signal result_valid_io : std_logic := '0';
begin
    generate_data : process(aclk, aclken, aresetn)
        -- Constants.
        constant c_positive_one : sfixed(2 downto -29) := to_sfixed(1.0, 2, -29);
        constant c_negative_one : sfixed(2 downto -29) := to_sfixed(-1.0, 2, -29);
        constant c_positive_two : sfixed(2 downto -29) := to_sfixed(2.0, 2, -29);
        -- Variables.
        variable v_index : integer := 0;
        variable v_nc_sum_index : integer := 0;
        variable v_ds_index : integer := 0;
        variable v_value : sfixed(2 downto -29) := to_sfixed(0.0, 2, -29);
        variable v_theta : sfixed(2 downto -29) := to_sfixed(c_doppler_frequency_multiplier * c_doppler_shifts(0), 2, -29);
    begin       
        if (aresetn = '0') then
            v_index := 0;
            v_nc_sum_index := 0;
            v_ds_index := 0;
            v_value := to_sfixed(0.0, 2, -29);
            v_theta := to_sfixed(c_doppler_frequency_multiplier * c_doppler_shifts(0), 2, -29);
            result_valid_io <= '0';
            result_last <= '0';
            result_data <= (others => '0');
        elsif (aclken = '1' and rising_edge(aclk)) then
            if (result_ready = '1' and result_valid_io = '1') then
                v_value := resize(arg => v_value + v_theta, size_res => v_value);
                if (v_value > c_positive_one) then
                    v_value := resize(arg => v_value - c_positive_two, size_res => v_value);
                elsif (v_value < c_negative_one) then
                    v_value := resize(arg => v_value + c_positive_two, size_res => v_value);
                end if;
                
                v_index := (v_index + 1) rem g_number_of_prn_elements;
                
                if (v_index = g_number_of_prn_elements - 1) then
                    result_last <= '1';
                else
                    result_last <= '0';
                end if;
                
                if (v_index = 0) then
                    v_value := to_sfixed(0.0, 2, -29);
                    v_nc_sum_index := (v_nc_sum_index + 1) rem g_number_of_nc_sums;
                    if (v_nc_sum_index = 0) then
                        v_ds_index := (v_ds_index + 1) rem g_number_of_doppler_shifts;
                        v_theta := to_sfixed(c_doppler_frequency_multiplier * c_doppler_shifts(v_ds_index), 2, -29);
                    end if;
                end if;
            end if;

            result_valid_io <= '1';
            result_data <= std_logic_vector(v_value);
        end if;
    end process generate_data;
    
    result_valid <= result_valid_io;
end rtl;
