--------------------------------------------------------------------------------
-- Title   : Top - LINAC multipulse function
-- Project : TimEX3
--------------------------------------------------------------------------------
-- Description : Top sheet of the LINAC multipluse block
--    inTTL(3 donwto 0) are anded to produce a multipulse on outTTL(4)
--    outTTL(3 donwto 0) are not used as the board IO connectors are hard
--    configured as inputs
--------------------------------------------------------------------------------
-- File     : linacMP_top.vhdl
-- Revision : x.x.x
-- Created  : March 14, 2013
-- Updated  : July 08, 2013
--------------------------------------------------------------------------------
-- Author       : Jean-Paul Ricaud
-- Organization : Synchrotron Soleil
-- Web          : http://www.synchrotron-soleil.fr
-- Email        : jean-paul.ricaud@synchrotron-soleil.fr
--------------------------------------------------------------------------------
-- Copyright (C) 2012 - 2013 Synchrotron Soleil
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
--
-- You should have received a copy of the Lesser GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>
--------------------------------------------------------------------------------
--
--------------------------------------------------------------------------------
-- Modifications :
--
--------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------
--------------------------------- ENTITY ---------------------------------------
--------------------------------------------------------------------------------
entity linacMP_top is
  port (
    p_linacMP_inTTL             : in  std_logic_vector (3 downto 0); -- TTL inputs
    p_linacMP_outTTL            : out std_logic_vector (4 downto 0); -- TTL outputs
    p_linacMP_outPECL           : out std_logic_vector (4 downto 0); -- LVPECL outputs
    p_linacMP_clk1kHz           : in  std_logic;
    p_linacMP_clk500mHz         : in  std_logic;
    p_linacMP_clk60MHz          : in  std_logic;
    p_linacMP_missingPulseDelay : in  std_logic_vector (31 downto 0);
    p_linacMP_csw               : in  std_logic;
    p_linacMP_reset             : in  std_logic;
    p_linacMP_pulseMon          : out std_logic_vector (31 downto 0); -- pulses monitor
    p_linacMP_led               : out std_logic_vector (1 downto 0)
  );
end entity linacMP_top;

--------------------------------------------------------------------------------
------------------------------- ARCHITECTURE -----------------------------------
--------------------------------------------------------------------------------
architecture rtl_linacMP_top of linacMP_top is

  ------------------------------------------------------------------------------
  -- constant
  ------------------------------------------------------------------------------

  ------------------------------------------------------------------------------
  -- signal
  ------------------------------------------------------------------------------
  signal s_impulse : std_logic; -- detection of impulse

  ------------------------------------------------------------------------------
  --------------------------------- Main ---------------------------------------
  ------------------------------------------------------------------------------
  begin

    -- And between the four inputs
    orPulses : entity work.linacMP_orPulses (rtl_linacMP_orPulses)
      port map (
        p_inTTL   => p_linacMP_inTTL,    -- TTL in
        p_outTTL  => p_linacMP_outTTL(4) -- TTL out
      );

    -- Signal monitoring block
    -- The time betwwen 2 input(0) pulses in stored in MONITOR register (in ms)
    pulseMonitor : entity work.linacMP_monitoring (rtl_linacMP_monitoring)
      port map (
        p_inTTL   => p_linacMP_inTTL(0), -- TTL input
        p_clk1kHz => p_linacMP_clk1kHz,
        p_reset   => p_linacMP_reset,
        p_monitor => p_linacMP_pulseMon  -- register with the time value between two pulses
      );

    -- LEDs management for the duplication block
    -- The green LED is blinked for 5s after a reset or after a input pulse is
    -- détected. It remains ON in other cases.
    -- The red LED is off after a reset. It gos ON if no input signal is detected
    -- after delay defined in the missingPulseDelay register.
    leds : entity work.linacMP_leds (rtl_linacMP_leds)
      port map (
        p_inTTL             => p_linacMP_inTTL(0),  -- TTL input
        p_clk1kHz           => p_linacMP_clk1kHz,
        p_clk500mHz         => p_linacMP_clk500mHz,
        p_clk60MHz          => p_linacMP_clk60MHz,
        p_missingPulseDelay => p_linacMP_missingPulseDelay,
        p_csw               => p_linacMP_csw,
        p_reset             => p_linacMP_reset,
        p_led               => p_linacMP_led
      );

  ------------------------------------------------------------------------------
  ------------------------------------------------------------------------------
  p_linacMP_outPECL (4 downto 0) <= "00000";
  p_linacMP_outTTL  (3 downto 0) <= "0000";

end architecture rtl_linacMP_top;
