--------------------------------------------------------------------------------
--
-- Copyright (C) 2014
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
-- JIVE (Joint Institute for VLBI in Europe) <http://www.jive.nl/>
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
--------------------------------------------------------------------------------

-- Purpose: Technology independent component for DDR memory access.
-- Description:
--   The component also supports different types of DDR, so DDR3 and DDR4.
-- Remark:
--   In simulation use g_use_ddr_memory_model=TRUE to also include the DDR
--   memory model that is internally available within tech_ddr. This avoids
--   having to connect a DDR memory model at top level in the test bench.
--   Default g_use_ddr_memory_model must be FALSE to avoid further analysis
--   of the DDR memory model component by synthesis.

LIBRARY IEEE, common_lib, technology_lib;
USE IEEE.STD_LOGIC_1164.ALL;
USE common_lib.common_mem_pkg.ALL;
USE technology_lib.technology_pkg.ALL;
USE technology_lib.technology_select_pkg.ALL;
USE work.tech_ddr_pkg.ALL;

ENTITY tech_ddr IS
  GENERIC (
    g_technology           : NATURAL := c_tech_select_default;
    g_tech_ddr             : t_c_tech_ddr;
    g_use_ddr_memory_model : BOOLEAN := FALSE
  );
  PORT (
    -- PLL reference clock
    ctlr_ref_clk      : IN    STD_LOGIC;
    ctlr_ref_rst      : IN    STD_LOGIC;

    -- Controller user interface
    ctlr_gen_clk      : OUT   STD_LOGIC;
    ctlr_gen_rst      : OUT   STD_LOGIC;
    ctlr_gen_clk_2x   : OUT   STD_LOGIC;
    ctlr_gen_rst_2x   : OUT   STD_LOGIC;

    ctlr_mosi         : IN    t_mem_ctlr_mosi;
    ctlr_miso         : OUT   t_mem_ctlr_miso;

    term_ctrl_out     : OUT   t_tech_ddr_phy_terminationcontrol;
    term_ctrl_in      : IN    t_tech_ddr_phy_terminationcontrol := c_tech_ddr_phy_terminationcontrol_rst;

    -- PHY interface
    phy_in            : IN    t_tech_ddr_phy_in;
    phy_io            : INOUT t_tech_ddr_phy_io;
    phy_ou            : OUT   t_tech_ddr_phy_ou
  );
END tech_ddr;


ARCHITECTURE str OF tech_ddr IS

  SIGNAL i_phy_ou     : t_tech_ddr_phy_ou;
  
BEGIN
 
  gen_ip_stratixiv : IF g_technology=c_tech_stratixiv GENERATE
    u0 : ENTITY work.tech_ddr_stratixiv
    GENERIC MAP (g_tech_ddr)
    PORT MAP (ctlr_ref_clk, ctlr_ref_rst,
              ctlr_gen_clk, ctlr_gen_rst, ctlr_gen_clk_2x, ctlr_gen_rst_2x,
              ctlr_mosi, ctlr_miso, term_ctrl_out, term_ctrl_in,
              phy_in, phy_io, i_phy_ou);
  END GENERATE;
  
  gen_ip_arria10 : IF g_technology=c_tech_arria10 GENERATE
    u0 : ENTITY work.tech_ddr_arria10
    GENERIC MAP (g_tech_ddr)
    PORT MAP (ctlr_ref_clk, ctlr_ref_rst,
              ctlr_gen_clk, ctlr_gen_rst,
              ctlr_mosi, ctlr_miso,
              phy_in, phy_io, i_phy_ou);
  END GENERATE;
  
  -- Include DDR memory model only for simulation
  no_ddr_memory_model : IF g_use_ddr_memory_model=FALSE GENERATE
    phy_ou <= i_phy_ou;
  END GENERATE;
  
  gen_ddr_memory_model: IF g_use_ddr_memory_model=TRUE GENERATE
    u_tech_ddr_memory_model : ENTITY work.tech_ddr_memory_model
    GENERIC MAP (
      g_sim      => g_use_ddr_memory_model,
      g_tech_ddr => g_tech_ddr
    )
    PORT MAP (
      mem_in => i_phy_ou,
      mem_io => phy_io
    );
  END GENERATE;
  
END str;