/*
 * SecBus - This file is part of SecBus
 * Copyright (C) 2012 - Telecom ParisTech
 * 
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at    
 * http://www.cecill.info/licences/Licence_CeCILL_V1.1-US.txt
 *
 */

#include "mux.h"
#include "debug.h"

#define mux(x) template<typename in, typename out> x Mux<in, out>

mux(/**/)::Mux(sc_module_name name, size_t ninput):
	  sc_module(name)
{
  m_ninput = ninput;

  input = new in[ninput];
  
  SC_METHOD(genMoore);
  dont_initialize();
  sensitive << output;
  for (size_t i=0;i<m_ninput;i++)
    sensitive << input[i]; 

  DEBUG(NONE, STD, "[SECBUS] Building Mux (%s) with %u inputs\n",
	basename(), ninput);
}

mux(/**/)::~Mux()
{
  delete input;
}

mux(void)::genMoore()
{
  for (size_t i=0;i<m_ninput;i++)
  {
    input[i](output);

    if (input[i].enable.read())
    {
      output(input[i]);
      return;
    }
  }
  output.enable = false;
}
