#ifdef SYSTEMC
/*
 * $Id: Register_Address_Translation_unit_genMealy_retire.cpp 88 2008-12-10 18:31:39Z rosiere $
 *
 * [ Description ]
 * 
 */

#include "Behavioural/Core/Multi_OOO_Engine/OOO_Engine/Rename_unit/Register_translation_unit/Register_Address_Translation_unit/include/Register_Address_Translation_unit.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::genMealy_retire"
  void Register_Address_Translation_unit::genMealy_retire (void)
  {
    log_begin(Register_Address_Translation_unit,FUNCTION);
    log_function(Register_Address_Translation_unit,FUNCTION,_name.c_str());

    // Init internal update table
    for (uint32_t i=0; i<_param->_nb_front_end; i++)
      for (uint32_t j=0; j<_param->_nb_context[i]; j++)
        {
          for (uint32_t k=0; k<_param->_nb_general_register_logic; ++k)
            internal_rat_gpr_update_table [i][j][k] = rat_gpr_update_table [i][j][k];
          for (uint32_t k=0; k<_param->_nb_special_register_logic; ++k)
            internal_rat_spr_update_table [i][j][k] = rat_spr_update_table [i][j][k];
        }


    // RETIRE is in order -> also don't need test if an instruction is valid
    for (uint32_t i=0; i<_param->_nb_inst_retire; i++)
      {
        log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"  * inst_retire [%d]",i);

        // init -> need't restore old value
        Tcontrol_t     retire_restore_rd_phy_old = false;
        Tcontrol_t     retire_restore_re_phy_old = false;
        Tevent_state_t event_state               = PORT_READ(in_RETIRE_EVENT_STATE [i]);

        // Test if event -> need restore ?
        if (event_state != EVENT_STATE_NO_EVENT)
          {
            Tcontext_t front_end_id = (_param->_have_port_front_end_id)?PORT_READ(in_RETIRE_FRONT_END_ID [i]):0;
            Tcontext_t context_id   = (_param->_have_port_context_id  )?PORT_READ(in_RETIRE_CONTEXT_ID   [i]):0;

            log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * Have event");
            log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * front_end_id         : %d",front_end_id);
            log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * context_id           : %d",context_id  );
                        
            // Test if event have just occure -> reset update_table
            if (event_state == EVENT_STATE_EVENT)
              {
                // Reset update_table
                for (uint32_t j=0; j<_param->_nb_general_register_logic; j++)
                  internal_rat_gpr_update_table[front_end_id][context_id][j] = 0;
                for (uint32_t j=0; j<_param->_nb_special_register_logic; j++)
                  internal_rat_spr_update_table[front_end_id][context_id][j] = 0;
              }
            
            // Test and update update table
            if (PORT_READ(in_RETIRE_WRITE_RD [i]))
              {
                Tgeneral_address_t rd_log = PORT_READ(in_RETIRE_NUM_REG_RD_LOG [i]);
                retire_restore_rd_phy_old = (internal_rat_gpr_update_table[front_end_id][context_id][rd_log] == 0);

//                 log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * write_rd");
//                 log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"      * rd_log             : %d",rd_log);
//                 log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"      * rat_gpr_update     : %d",internal_rat_gpr_update_table[front_end_id][context_id][rd_log]);

                internal_rat_gpr_update_table[front_end_id][context_id][rd_log] = 1;
              }
            if (PORT_READ(in_RETIRE_WRITE_RE [i]))
              {
//                 log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * write_re");

                Tgeneral_address_t re_log = PORT_READ(in_RETIRE_NUM_REG_RE_LOG [i]);
                retire_restore_re_phy_old = (internal_rat_spr_update_table[front_end_id][context_id][re_log] == 0);
                internal_rat_spr_update_table[front_end_id][context_id][re_log] = 1;
              }

            log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * restore_rd_phy_old   : %d",retire_restore_rd_phy_old);
            log_printf(TRACE,Register_Address_Translation_unit,FUNCTION,"    * restore_re_phy_old   : %d",retire_restore_re_phy_old);
          }
              
        PORT_WRITE(out_RETIRE_RESTORE_RD_PHY_OLD[i], retire_restore_rd_phy_old);
        PORT_WRITE(out_RETIRE_RESTORE_RE_PHY_OLD[i], retire_restore_re_phy_old);
      }

    log_end(Register_Address_Translation_unit,FUNCTION);
  };

}; // 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              
#endif
