---------------------------------------------------------------------------
-- File           : ea_pattern_mon.vhd
-- Author(s)      : Nikola Avramovic
-- Created        : March 11th, 2019
---------------------------------------------------------------------------
-- Description    : Monitor pattern top module
---------------------------------------------------------------------------

---------------------------------------------------------------------------
library ieee;
   use ieee.std_logic_1164.all;
   use ieee.numeric_std.all;
   use work.p_interfaces.all;
   --use work.p_addresses.all;
   use work.p_monitor_interface.all;
   use work.p_patt_mon_interface.all;

entity ea_pattern_mon is
   generic(
      G_PATTERN_MON_NO : natural 
   );
   port  (
      clk              : in  std_logic;
      reset            : in  std_logic;
      -- to/from input command module
      cmd_decoderi     : in  TYPE_CMD_DECODER_TO_PATTERN_MON;
      cmd_decodero     : out TYPE_PATTERN_MON_TO_CMD_DECODER;
      -- to/from port monitor
      port_moni        : in  TYPE_AVALON_ST_SOURCE_TO_SINK;
      port_mono        : out TYPE_AVALON_ST_SINK_TO_SOURCE;
      -- status interface
      statuso          : out  TYPE_PATTERN_MON_TO_STATUS
   );
end entity ea_pattern_mon;

architecture rtl of ea_pattern_mon is

   -- intercontect signals
   signal lfsr_to_control_patt          : TYPE_LFSR_TO_CTRL_PATTERN;
   signal control_patt_to_lfsr          : TYPE_CTRL_PATTERN_TO_LFSR;
   signal linear_to_control_patt        : TYPE_LINEAR_TO_CTRL_PATTERN;
   signal control_patt_to_linear        : TYPE_CTRL_PATTERN_TO_LINEAR;
   signal patt_mux_to_control_patt      : TYPE_PATTERN_MUX_TO_CTRL_PATTERN;
   signal control_patt_to_patt_mux      : TYPE_CTRL_PATTERN_TO_PATTERN_MUX;
   signal exp_fifo_avalon_to_lfsr       : TYPE_EXP_FIFO_AVALON_TO_LFSR;
   signal lfsr_to_patt_mux              : TYPE_LFSR_MON_TO_PATTERN_MUX;
   signal exp_fifo_avalon_to_linear     : TYPE_EXP_FIFO_AVALON_TO_LINEAR;
   signal linear_to_patt_mux            : TYPE_LINEAR_MON_TO_PATTERN_MUX;
   signal patt_mux_to_exp_fifo_avalon   : TYPE_PATT_MUX_TO_EXP_FIFO_AVALON;
   signal scoreboard_to_exp_fifo_avalon : TYPE_AVALON_ST_SINK_TO_SOURCE;  -- TYPE_SCOREBOARD_TO_EXP_FIFO_AVALON;
   signal exp_fifo_avalon_to_scoreboard : TYPE_AVALON_ST_SOURCE_TO_SINK;  -- TYPE_EXP_FIFO_AVALON_TO_SCOREBOARD;
   signal scoreboard_to_obs_fifo_avalon : TYPE_AVALON_ST_SINK_TO_SOURCE;  -- TYPE_SCOREBOARD_TO_OBS_FIFO_AVALON;
   signal obs_fifo_avalon_to_scoreboard : TYPE_AVALON_ST_SOURCE_TO_SINK;  -- TYPE_OBS_FIFO_AVALON_TO_SCOREBOARD;
   -- status signals
   signal scoreboard_to_control_patt    : TYPE_SCOREBOARD_TO_CTRL_PATTERN;
   signal control_patt_to_scoreboard    : TYPE_CTRL_PATTERN_TO_SCOREBOARD;
begin

   -- random generator
   lfsr_mon_i : entity work.ea_lfsr_mon
   port map (
      clk              => clk,
      reset            => reset,
      control_patti    => control_patt_to_lfsr,
      control_patto    => lfsr_to_control_patt,
      exp_fifo_avaloni => exp_fifo_avalon_to_lfsr,
      patt_muxo        => lfsr_to_patt_mux
   );

   -- non random geenrator
   linear_mon_i: entity work.ea_linear_mon 
   port map ( 
      clk              => clk,
      reset            => reset,
      control_patti    => control_patt_to_linear,
      control_patto    => linear_to_control_patt,
      exp_fifo_avaloni => exp_fifo_avalon_to_linear,
      patt_muxo        => linear_to_patt_mux
   );

   -- control pattern
   ctrl_patt_mon_i: entity work.ea_control_patt_mon
   generic map (
      G_PATTERN_MON_NO => G_PATTERN_MON_NO
   ) port map (
      clk              => clk,
      reset            => reset,
      cmd_decoderi     => cmd_decoderi,
      cmd_decodero     => cmd_decodero,
      lfsri            => lfsr_to_control_patt,
      lfsro            => control_patt_to_lfsr,
      lineari          => linear_to_control_patt,
      linearo          => control_patt_to_linear,
      scoreboardi      => scoreboard_to_control_patt,
      scoreboardo      => control_patt_to_scoreboard,
      patt_muxi        => patt_mux_to_control_patt,
      patt_muxo        => control_patt_to_patt_mux,
      statuso          => statuso
   );

   pattern_mon_mux_i: entity work.ea_patt_mon_mux
   port map ( 
      clk              => clk,
      reset            => reset,
      control_patti    => control_patt_to_patt_mux,
      control_patto    => patt_mux_to_control_patt,
      lfsri            => lfsr_to_patt_mux,
      lineari          => linear_to_patt_mux,
      exp_fifo_avalono => patt_mux_to_exp_fifo_avalon
   );
   
   avalon_exp_fifo_i: entity work.ea_exp_fifo_avalon_st 
   port map (
      clk              => clk,
      reset            => reset,
      patt_muxi        => patt_mux_to_exp_fifo_avalon,
      lfsro            => exp_fifo_avalon_to_lfsr,
      linearo          => exp_fifo_avalon_to_linear,
      scoreboardi      => scoreboard_to_exp_fifo_avalon,
      scoreboardo      => exp_fifo_avalon_to_scoreboard 
   );

   avalon_obs_fifo_i: entity work.ea_obs_fifo_avalon_st 
   port map (
      clk              => clk,
      reset            => reset,
      port_moni        => port_moni,
      port_mono        => port_mono,
      scoreboardi      => scoreboard_to_obs_fifo_avalon,
      scoreboardo      => obs_fifo_avalon_to_scoreboard
   );

   scoreboard_i: entity work.ea_scoreboard
   port map (
      clk              => clk,
      reset            => reset,
      control_patti    => control_patt_to_scoreboard,
      control_patto    => scoreboard_to_control_patt,
      exp_fifo_avaloni => exp_fifo_avalon_to_scoreboard,
      exp_fifo_avalono => scoreboard_to_exp_fifo_avalon,
      obs_fifo_avaloni => obs_fifo_avalon_to_scoreboard,
      obs_fifo_avalono => scoreboard_to_obs_fifo_avalon
   );

end architecture rtl;