/*
 * 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
 *
 */

#ifndef _VCI_MEM_CTRL_H
#define _VCI_MEM_CTRL_H

#include <systemc>
#include "vci_target.h"

#include "my_interfaces.h"

template<typename vci_param>
SC_MODULE(VciMemCtrl)
{
protected:
  SC_HAS_PROCESS(VciMemCtrl);

public:
  sc_in<bool> p_clk;
  sc_in<bool> p_resetn;

  soclib::caba::VciInitiator<vci_param> p_vci_initiator;

  MEMORY_CTRL_CMD_IN      mem_rq;
  DATA_IN<2>              wr_data;

  DATA_REGISTER_WRITE_OUT *in;
  DATA_REGISTER_READ_IN   *out;

  VciMemCtrl(sc_module_name name, size_t nreg);
  ~VciMemCtrl();

private:
  size_t m_nreg;
  void transition();
  void genMoore();

  enum fsm_cmd_state_e
  {
    VCI_CMD_IDLE,
    VCI_CMD_READ,
    VCI_CMD_WRITE
  };

  enum fsm_rsp_state_e
  {
    VCI_RSP_IDLE,
    VCI_RSP_READ,
    VCI_RSP_WRITE
  };

  sc_signal<int> fsm_cmd_s;
  sc_signal<int> fsm_rsp_s;

  sc_signal<unsigned int> cmd_cpt_s;
  sc_signal<unsigned int> cells_to_come_s;
  sc_signal<addr_t>       address_s;
  sc_signal<int>    cmd_s;
  sc_signal<unsigned int> id_s;
  sc_signal<unsigned int> plen_s;
  sc_signal<unsigned int> reg_idx_s;
  sc_signal<unsigned int> wr_data_s[2];

  sc_signal<bool>         write_in_reg_s;
  sc_signal<bool>         flush_reg_s;

  sc_signal<unsigned int> data_s;

  size_t m_cycle;
};

#endif
