#ifndef morpheo_behavioural_generic_select_pseudo_lru_Pseudo_LRU_h
#define morpheo_behavioural_generic_select_pseudo_lru_Pseudo_LRU_h

/*
 * $Id$
 *
 * [ Description ]
 * 
 */
    // Tree of Pseudo-LRU
    //
    // |               4-5-6-7?              |
    // |          0_______|_______1	     |
    // |          |               |	     |
    // |         2-3?            6-7?        |
    // |      0___|___1       0___|___1      |
    // |      |       |       |       |      |
    // |      1?      3?      5?      7?     |
    // |    0_|_1   0_|_1   0_|_1   0_|_1    |
    // |    |   |   |   |   |   |   |   |    |
    // |   Way Way Way Way Way Way Way Way   |
    // |    0   1   2   3   4   5   6   7    |



#ifdef SYSTEMC
#include "systemc.h"
#endif

#include <iostream>
#include "Include/ToString.h"
#include "Include/Debug.h"

#include "Behavioural/Generic/Select/Pseudo_LRU/include/Parameters.h"
#include "Behavioural/Generic/Select/Pseudo_LRU/include/Types.h"
#ifdef STATISTICS
#include "Behavioural/Generic/Select/Pseudo_LRU/include/Statistics.h"
#endif
#ifdef VHDL
#include "Behavioural/include/Vhdl.h"
#endif
#ifdef VHDL_TESTBENCH
#include "Behavioural/include/Vhdl_Testbench.h"
#endif

using namespace std;

namespace morpheo {
namespace behavioural {
namespace generic {
namespace select {
namespace pseudo_lru {

  class Pseudo_LRU 
#if SYSTEMC
    : public sc_module
#endif
  {
    // -----[ internal class ]--------------------------------------------
  protected : class entry_t
  {
    // Numerotation of pseudo_LRU's tree
    // Tree of Pseudo-LRU
    //
    // | d2              [3]                 |
    // |          0_______|_______1	     |
    // |          |               |	     |
    // | d1      [1]             [5]         |
    // |      0___|___1       0___|___1      |
    // |      |       |       |       |      |
    // | d0  [0]     [2]     [4]     [6]     |
    // |    0_|_1   0_|_1   0_|_1   0_|_1    |
    // |    |   |   |   |   |   |   |   |    |
    // |   Way Way Way Way Way Way Way Way   |
    // |    0   1   2   3   4   5   6   7    |
    //
    // Access :  Way N with N=2*n   -> bit 2*n = 0
    //                 with N=2*n+1 -> bit 2*n = 1
    //
    // if bit = B, depth = D, next_B = B+D if B==1
    //                               = B-D if B==0
    //
    // Update : 

  private : bool    * _entry;
  private : uint32_t  _size;

  public  : entry_t () 
    {
    };
  public  : entry_t (uint32_t size) : _size (size)
    {
      _entry = new bool [size];
      
      // initialisation
      for (uint32_t i=0; i<size; i++)
	_entry [i] = false;
    }
    
  public : ~entry_t ()
    {
      delete _entry;
    }
    
  private : uint32_t one_access (uint32_t index, uint32_t offset)
    {
      bool val = _entry[index];
      
      // Compute next slot
      if (val == true)
	return index + offset;
      else
	return index - offset;
    }

  public : uint32_t access ()
    {
      uint32_t index = (_size>>1)-1; // medium

      for (int32_t i=static_cast<uint32_t>(log2(_size)-1); i>= 1; i--)
	{
	  index = one_access (index,(1<<(i-1)));
	}
      index = one_access (index,0);
      
      // reverse by one_access make always a reverse
      uint32_t offset = (_entry[index]==true)?1:0; 

      return index+offset;
    }

  private : uint32_t one_update (uint32_t index, uint32_t offset, uint32_t value)
    {
      uint32_t mask = (offset==0)?1:(offset<<1);
      bool     val  = ((value & mask) != 0);

      // reverse 
      _entry[index] = not val;

      if (val == true)
      // Compute next slot
	return index + offset;
      else
	return index - offset;
    }
  public : void update (uint32_t value)
    {
      uint32_t index = (_size>>1)-1; // medium

      for (int32_t i=static_cast<uint32_t>(log2(_size)-1); i>=1; i--)
	{
	  index = one_update (index,1<<(i-1),value);
	}
      index = one_update (index,0,value);
    }

  public : string print ()
    {
      string res = "";

      for (int32_t i=static_cast<int32_t>(_size)-1; i>=0; i--)
	res += toString(_entry[i]) + " ";

      return res;
    }

  };
    // -----[ fields ]----------------------------------------------------
    // Parameters
  protected : const string     _name;

  protected : const Parameters _param;
#ifdef STATISTICS
  private   : Statistics                     * _stat;
#endif

#ifdef VHDL_TESTBENCH
  private   : Vhdl_Testbench                 * _vhdl_testbench;
#endif

#ifdef SYSTEMC
    // ~~~~~[ Interface ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Interface
  public    : SC_CLOCK                      *  in_CLOCK         ;

    // Interface access
  public    : SC_IN (Tcontrol_t)           **  in_ACCESS_VAL    ;
  public    : SC_OUT(Tcontrol_t)           ** out_ACCESS_ACK    ;
  public    : SC_IN (Taddress_t)           **  in_ACCESS_ADDRESS;
  public    : SC_OUT(Tentity_t )           ** out_ACCESS_ENTITY ;
    // Interface update
  public    : SC_IN (Tcontrol_t)           **  in_UPDATE_VAL    ;
  public    : SC_OUT(Tcontrol_t)           ** out_UPDATE_ACK    ;
  public    : SC_IN (Taddress_t)           **  in_UPDATE_ADDRESS;
  public    : SC_IN (Tentity_t )           **  in_UPDATE_ENTITY ;

    // Interface update
    // ~~~~~[ Register ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
  private   : entry_t                      ** reg_TABLE;

    // ~~~~~[ Internal ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  private   : Tentity_t                     * internal_ACCESS_ENTITY;
#endif

    // -----[ methods ]---------------------------------------------------

#ifdef SYSTEMC
    SC_HAS_PROCESS (Pseudo_LRU);
#endif

  public  :          Pseudo_LRU              (
#ifdef SYSTEMC
					      sc_module_name                              name,
#else					       
					      string                                      name,
#endif					       
#ifdef STATISTICS
					      morpheo::behavioural::Parameters_Statistics param_statistics,
#endif
					      Parameters                                  param );
					       
  public  :          Pseudo_LRU              (Parameters param );
  public  :          ~Pseudo_LRU             (void);
					       
#ifdef SYSTEMC				       
  private : void     allocation                (void);
  private : void     deallocation              (void);
					       
  public  : void     transition                (void);
  public  : void     genMealy_access           (void);
#endif					       
#ifdef STATISTICS
  public  : string   statistics                (uint32_t depth);
#endif
					       
#if VHDL				       
  public  : void     vhdl                      (void);
  private : void     vhdl_port                 (Vhdl & vhdl);
  private : void     vhdl_declaration          (Vhdl & vhdl);
  private : void     vhdl_body                 (Vhdl & vhdl);
#endif					       
					       
#ifdef VHDL_TESTBENCH			       
  private : void     vhdl_testbench            (Vhdl_Testbench & vhdl_testbench);
  private : void     vhdl_testbench_port       (Vhdl_Testbench & vhdl_testbench);
  private : void     vhdl_testbench_transition (Vhdl_Testbench & vhdl_testbench);
#endif

  };

}; // end namespace pseudo_lru
}; // end namespace select
}; // end namespace generic

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

#endif
