/*
 * $Id$
 *
 * [ Description ]
 * 
 */

#include "Behavioural/Core/Multi_OOO_Engine/OOO_Engine/Rename_unit/Register_translation_unit/Register_Address_Translation_unit/include/Register_Address_Translation_unit.h"
#include "Behavioural/include/Allocation.h"

namespace morpheo                    {
namespace behavioural {
namespace core {
namespace multi_ooo_engine {
namespace ooo_engine {
namespace rename_unit {
namespace register_translation_unit {
namespace register_address_translation_unit {

#undef  FUNCTION
#define FUNCTION "Register_Address_Translation_unit::allocation"
  void Register_Address_Translation_unit::allocation 
  (
#ifdef STATISTICS
   morpheo::behavioural::Parameters_Statistics * param_statistics
#else
   void
#endif
   )
  {
    log_printf(FUNC,Register_Address_Translation_unit,FUNCTION,"Begin");
    
    _component   = new Component (_usage);
    
    Entity * entity = _component->set_entity (_name       
					      ,"Register_Address_Translation_unit"
#ifdef POSITION
					      ,COMBINATORY 
#endif
					      );
    
    _interfaces = entity->set_interfaces();
    
    // ~~~~~[ Interface : "" ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {
      Interface * interface = _interfaces->set_interface(""
#ifdef POSITION
							 ,IN
							 ,SOUTH,
							 "Generalist interface"
#endif
							 );
      
      in_CLOCK        = interface->set_signal_clk              ("clock" ,1, CLOCK_VHDL_YES);
      in_NRESET       = interface->set_signal_in  <Tcontrol_t> ("nreset",1, RESET_VHDL_YES);
    }
    
    // ~~~~~[ Interface "rename" ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {
      ALLOC1_INTERFACE("rename",IN,EAST,"Input to rename source logical register", _param->_nb_inst_insert);

      ALLOC1_VALACK_IN ( in_RENAME_VAL               , VAL);
      ALLOC1_VALACK_OUT(out_RENAME_ACK               , ACK);
      ALLOC1_SIGNAL_IN ( in_RENAME_FRONT_END_ID      ,"front_end_id"      ,Tcontext_t        ,_param->_size_front_end_id);
      ALLOC1_SIGNAL_IN ( in_RENAME_CONTEXT_ID        ,"context_id"        ,Tcontext_t        ,_param->_size_context_id  );
      ALLOC1_SIGNAL_IN ( in_RENAME_NUM_REG_RA_LOG    ,"num_reg_ra_log"    ,Tgeneral_address_t,_param->_size_general_register_logic);
      ALLOC1_SIGNAL_IN ( in_RENAME_NUM_REG_RB_LOG    ,"num_reg_rb_log"    ,Tgeneral_address_t,_param->_size_general_register_logic);
      ALLOC1_SIGNAL_IN ( in_RENAME_NUM_REG_RC_LOG    ,"num_reg_rc_log"    ,Tspecial_address_t,_param->_size_special_register_logic);
      ALLOC1_SIGNAL_IN ( in_RENAME_NUM_REG_RD_LOG    ,"num_reg_rd_log"    ,Tgeneral_address_t,_param->_size_general_register_logic);
      ALLOC1_SIGNAL_IN ( in_RENAME_NUM_REG_RE_LOG    ,"num_reg_re_log"    ,Tspecial_address_t,_param->_size_special_register_logic);
      ALLOC1_SIGNAL_OUT(out_RENAME_NUM_REG_RA_PHY    ,"num_reg_ra_phy"    ,Tgeneral_address_t,_param->_size_general_register);
      ALLOC1_SIGNAL_OUT(out_RENAME_NUM_REG_RB_PHY    ,"num_reg_rb_phy"    ,Tgeneral_address_t,_param->_size_general_register);
      ALLOC1_SIGNAL_OUT(out_RENAME_NUM_REG_RC_PHY    ,"num_reg_rc_phy"    ,Tspecial_address_t,_param->_size_special_register);
      ALLOC1_SIGNAL_OUT(out_RENAME_NUM_REG_RD_PHY_OLD,"num_reg_rd_phy_old",Tgeneral_address_t,_param->_size_general_register);
      ALLOC1_SIGNAL_OUT(out_RENAME_NUM_REG_RE_PHY_OLD,"num_reg_re_phy_old",Tspecial_address_t,_param->_size_special_register);
    }
    
    // ~~~~~[ Interface "insert" ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {
      ALLOC1_INTERFACE("insert",IN,NORTH,"Input to rename destination logical register", _param->_nb_inst_insert);
      
      ALLOC1_VALACK_IN ( in_INSERT_VAL           ,VAL);
      ALLOC1_VALACK_OUT(out_INSERT_ACK           ,ACK);
//    ALLOC1_SIGNAL_IN ( in_INSERT_FRONT_END_ID  ,"front_end_id"  ,Tcontext_t        ,_param->_size_front_end_id);
//    ALLOC1_SIGNAL_IN ( in_INSERT_CONTEXT_ID    ,"context_id"    ,Tcontext_t        ,_param->_size_context_id  );
      ALLOC1_SIGNAL_IN ( in_INSERT_WRITE_RD      ,"write_rd"      ,Tcontrol_t        ,1);
      ALLOC1_SIGNAL_IN ( in_INSERT_WRITE_RE      ,"write_re"      ,Tcontrol_t        ,1);
      ALLOC1_SIGNAL_IN ( in_INSERT_NUM_REG_RD_LOG,"num_reg_rd_log",Tgeneral_address_t,_param->_size_general_register_logic);
      ALLOC1_SIGNAL_IN ( in_INSERT_NUM_REG_RE_LOG,"num_reg_re_log",Tspecial_address_t,_param->_size_special_register_logic);
      ALLOC1_SIGNAL_IN ( in_INSERT_NUM_REG_RD_PHY,"num_reg_rd_phy",Tgeneral_address_t,_param->_size_general_register);
      ALLOC1_SIGNAL_IN ( in_INSERT_NUM_REG_RE_PHY,"num_reg_re_phy",Tspecial_address_t,_param->_size_special_register);
    }
    
    // ~~~~~[ Interface "retire" ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {
      ALLOC1_INTERFACE("retire",IN,NORTH,"Input to update on event", _param->_nb_inst_retire);

      ALLOC1_VALACK_IN ( in_RETIRE_VAL               ,VAL);
      ALLOC1_VALACK_OUT(out_RETIRE_ACK               ,ACK);
      ALLOC1_SIGNAL_IN ( in_RETIRE_FRONT_END_ID      ,"front_end_id"      ,Tcontext_t        ,_param->_size_front_end_id);
      ALLOC1_SIGNAL_IN ( in_RETIRE_CONTEXT_ID        ,"context_id"        ,Tcontext_t        ,_param->_size_context_id  );
      ALLOC1_SIGNAL_IN ( in_RETIRE_EVENT_STATE       ,"event_state"       ,Tevent_state_t    ,_param->_size_event_state );
      ALLOC1_SIGNAL_IN ( in_RETIRE_WRITE_RD          ,"write_rd"          ,Tcontrol_t        ,1);
      ALLOC1_SIGNAL_IN ( in_RETIRE_WRITE_RE          ,"write_re"          ,Tcontrol_t        ,1);
      ALLOC1_SIGNAL_IN ( in_RETIRE_NUM_REG_RD_LOG    ,"num_reg_rd_log"    ,Tgeneral_address_t,_param->_size_general_register_logic);
      ALLOC1_SIGNAL_IN ( in_RETIRE_NUM_REG_RE_LOG    ,"num_reg_re_log"    ,Tspecial_address_t,_param->_size_special_register_logic);
      ALLOC1_SIGNAL_IN ( in_RETIRE_NUM_REG_RD_PHY_OLD,"num_reg_rd_phy_old",Tgeneral_address_t,_param->_size_general_register);
      ALLOC1_SIGNAL_IN ( in_RETIRE_NUM_REG_RE_PHY_OLD,"num_reg_re_phy_old",Tspecial_address_t,_param->_size_special_register);
    }

    // ~~~~~[ Component ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
    internal_RENAME_ACK = new Tcontrol_t [_param->_nb_inst_insert];
    internal_INSERT_ACK = new Tcontrol_t [_param->_nb_inst_insert];
    internal_RETIRE_ACK = new Tcontrol_t [_param->_nb_inst_retire];

    rat_gpr              = new Tgeneral_address_t ** [_param->_nb_front_end];
    rat_gpr_update_table = new bool               ** [_param->_nb_front_end];
    rat_spr              = new Tspecial_address_t ** [_param->_nb_front_end];
    rat_spr_update_table = new bool               ** [_param->_nb_front_end];

    for (uint32_t i=0; i<_param->_nb_front_end; i++)
      {
	rat_gpr              [i] = new Tgeneral_address_t * [_param->_nb_context[i]];
	rat_gpr_update_table [i] = new bool               * [_param->_nb_context[i]];
	rat_spr              [i] = new Tspecial_address_t * [_param->_nb_context[i]];
	rat_spr_update_table [i] = new bool               * [_param->_nb_context[i]];

	for (uint32_t j=0; j<_param->_nb_context[i]; j++)
	  {
	    rat_gpr              [i][j] = new Tgeneral_address_t [_param->_nb_general_register_logic];
	    rat_gpr_update_table [i][j] = new bool               [_param->_nb_general_register_logic];
	    rat_spr              [i][j] = new Tspecial_address_t [_param->_nb_special_register_logic];
	    rat_spr_update_table [i][j] = new bool               [_param->_nb_special_register_logic];
	  }
      }

#ifdef POSITION
    _component->generate_file();
#endif

    log_printf(FUNC,Register_Address_Translation_unit,FUNCTION,"End");
  };

}; // end namespace register_address_translation_unit
}; // end namespace register_translation_unit
}; // end namespace rename_unit
}; // end namespace ooo_engine
}; // end namespace multi_ooo_engine
}; // end namespace core

}; // end namespace behavioural
}; // end namespace morpheo              
