/*
 * 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 "crypto_arbiter.h"

CryptoArbiter::CryptoArbiter(sc_module_name name, size_t nrq):
  sc_module(name),
  p_clk("clk"),
  p_resetn("resetn")
{
  m_nrq = nrq;
  sc_rq = new CRYPTO_CMD_IN[m_nrq];

  SC_METHOD(transition);
  dont_initialize();
  sensitive << p_clk.pos();

  SC_METHOD(genMealy_cmdack);
  dont_initialize();
  sensitive << p_clk.neg()
	    << sc_ctrl;

  SC_METHOD(genMealy_cmdval);
  dont_initialize();
  sensitive << p_clk.neg();
  for (size_t i=0;i<m_nrq;i++)
    sensitive << sc_rq[i];

  SC_METHOD(genMealy_rsp);
  dont_initialize();
  sensitive << p_clk.neg()
	    << sc_ctrl;

  DEBUG(NONE, STD, "[SECBUS] Building CryptoArbiter (%s) with %u entries\n", 
	basename(), nrq);
}

CryptoArbiter::~CryptoArbiter()
{
  delete sc_rq;
}

void CryptoArbiter::transition()
{
  if (p_resetn.read() == false)
  {
    selected_src_s = 0;
    fsm_s = FSM_IDLE;

    return;
  }

  DEBUG(TRACE, FSM, "[%s]\t\tFSM\t%u\n", 
	basename(), (uint32_t)fsm_s.read());

  switch(fsm_s.read())
  {
  case FSM_IDLE:
    for (size_t i=0;i<m_nrq;i++)
    {
      if (sc_rq[i].cmdval.read())
      {
	selected_src_s = i;
	fsm_s = FSM_CMD;
	break;
      }
    }
    break;

  case FSM_CMD:
    if (sc_rq[selected_src_s].cmdval.read() &&
	sc_ctrl.cmdack.read())
      fsm_s = FSM_RSP;
    break;

  case FSM_RSP:
    if (sc_ctrl.rspval.read())
    {
      for (size_t i=0;i<m_nrq;i++)
      {
	if (sc_rq[i].cmdval.read())
	{
	  selected_src_s = i;
	  fsm_s = FSM_CMD;
	  break;
	}
	fsm_s = FSM_IDLE;
      }
    }
    break;

  default:
    break;
  }
}

void CryptoArbiter::genMealy_cmdval()
{
  unsigned int rq = selected_src_s.read();
  if (fsm_s.read() == FSM_CMD)
  {
    sc_ctrl.cipher = sc_rq[rq].cipher.read();
    sc_ctrl.enable = sc_rq[rq].enable.read();
    sc_ctrl.mode   = sc_rq[rq].mode.read();
    sc_ctrl.cmdval = sc_rq[rq].cmdval.read();
    sc_ctrl.id_src = sc_rq[rq].id_src.read();
    sc_ctrl.id_dest = sc_rq[rq].id_dest.read();
    sc_ctrl.reg_ready = sc_rq[rq].reg_ready.read();
    sc_ctrl.direct_data = sc_rq[rq].direct_data.read();
    sc_ctrl.no_init = sc_rq[rq].no_init.read();
    for (uint32_t i=0;i<DES_KEY_SIZE_IN_WORDS;i++)
      sc_ctrl.key[i] = sc_rq[rq].key[i].read();
    
    for (uint32_t i=0;i<IV_SIZE_IN_WORDS;i++)
    {
      sc_ctrl.iv[i] = sc_rq[rq].iv[i].read();
    }

    sc_ctrl.address = sc_rq[rq].address.read();
    sc_ctrl.size = sc_rq[rq].size.read();
  }
  else
  {
    sc_ctrl.cmdval = false;
  }
}

void CryptoArbiter::genMealy_cmdack()
{
  if (fsm_s.read() == FSM_CMD)
  {
    for (size_t i=0;i<m_nrq;i++)
    {
      if (i == selected_src_s.read())
      {
	sc_rq[i].cmdack = sc_ctrl.cmdack.read();
      }
      else
      {
	sc_rq[i].cmdack = false;
      }
    }
  }
  else
  {
    for (size_t i=0;i<m_nrq;i++)
    {
      sc_rq[i].cmdack = false;
    }
  }
}

void CryptoArbiter::genMealy_rsp()
{
  if (fsm_s.read() == FSM_RSP)
  {
    for (size_t i=0;i<m_nrq;i++)
    {
      if (i == selected_src_s.read())
      {
	sc_rq[i].rspval = sc_ctrl.rspval.read();
      }
      else
      {
	sc_rq[i].rspval = false;
      }
    }
  }
  else
  {
    for (size_t i=0;i<m_nrq;i++)
    {
      sc_rq[i].rspval = false;
    }
  }
}
