OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [SprCache.cpp] - Diff between revs 63 and 462

Only display areas with differences | Details | Blame | View Log

Rev 63 Rev 462
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
 
 
// Debug Unit SPR cache: implementation
// Debug Unit SPR cache: implementation
 
 
// Copyright (C) 2008  Embecosm Limited <info@embecosm.com>
// Copyright (C) 2008  Embecosm Limited <info@embecosm.com>
 
 
// Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
// Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
 
 
// This file is part of the GDB interface to the cycle accurate model of the
// This file is part of the GDB interface to the cycle accurate model of the
// OpenRISC 1000 based system-on-chip, ORPSoC, built using Verilator.
// OpenRISC 1000 based system-on-chip, ORPSoC, built using Verilator.
 
 
// This program is free software: you can redistribute it and/or modify it
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
// option) any later version.
 
 
// This program is distributed in the hope that it will be useful, but WITHOUT
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
// License for more details.
// License for more details.
 
 
// You should have received a copy of the GNU Lesser General Public License
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
 
 
// $Id: SprCache.cpp 331 2009-03-12 17:01:48Z jeremy $
// $Id: SprCache.cpp 331 2009-03-12 17:01:48Z jeremy $
 
 
#include <iostream>
#include <iostream>
#include <cstring>
#include <cstring>
 
 
#include "SprCache.h"
#include "SprCache.h"
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Constructor
//! Constructor
 
 
//! Allocate tables and clear the cache
//! Allocate tables and clear the cache
 
 
//! @param[in] _tableSize  The desire hash table size. A prime number is
//! @param[in] _tableSize  The desire hash table size. A prime number is
//!                         recommended.
//!                         recommended.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
SprCache::SprCache (int _tableSize) :
SprCache::SprCache(int _tableSize):
  tableSize (_tableSize)
tableSize(_tableSize)
{
{
  sprIsValid = new bool [tableSize];
        sprIsValid = new bool[tableSize];
  sprKeyNum  = new uint16_t [tableSize];
        sprKeyNum = new uint16_t[tableSize];
  sprValue   = new uint32_t [tableSize];
        sprValue = new uint32_t[tableSize];
 
 
  clear ();
        clear();
 
 
}       // SprCache ()
}                               // SprCache ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Destructor
//! Destructor
 
 
//! Free up the tables
//! Free up the tables
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
SprCache::~SprCache ()
SprCache::~SprCache()
{
{
  delete [] sprValue;
        delete[]sprValue;
  delete [] sprKeyNum;
        delete[]sprKeyNum;
  delete [] sprIsValid;
        delete[]sprIsValid;
 
 
}       // ~SprCache ()
}                               // ~SprCache ()
 
 
 
 
//! Empty the hash table
//! Empty the hash table
 
 
//! Only need to worry about the validity field
//! Only need to worry about the validity field
void
void
SprCache::clear ()
 SprCache::clear()
{
{
  memset (sprIsValid, false, tableSize);
        memset(sprIsValid, false, tableSize);
 
 
  // No more than 70% full
        // No more than 70% full
  maxToUse = tableSize * 7 / 10;
        maxToUse = tableSize * 7 / 10;
 
 
}       // clear ()
}                               // clear ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Write a new value into the cache
//! Write a new value into the cache
 
 
//! If the hash table is full silently does nothing, unless the force
//! If the hash table is full silently does nothing, unless the force
//! parameter is set to TRUE. Under this circumstance the value WILL be
//! parameter is set to TRUE. Under this circumstance the value WILL be
//! written into the hash table. This is safe, because the table is never more
//! written into the hash table. This is safe, because the table is never more
//! than 70% full, and force is used only for NPC.
//! than 70% full, and force is used only for NPC.
 
 
//! @param[in] spr    The SPR being written to
//! @param[in] spr    The SPR being written to
//! @param[in] value  The value to write
//! @param[in] value  The value to write
//! @param[in] force  If TRUE the value will be written to the hash table,
//! @param[in] force  If TRUE the value will be written to the hash table,
//!                   even if it is too full.
//!                   even if it is too full.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void
void SprCache::write(uint16_t sprNum, uint32_t value, bool force)
SprCache::write (uint16_t  sprNum,
 
                 uint32_t  value,
 
                 bool      force)
 
{
 
  if (maxToUse <= 0)
 
    {
    {
 
        if (maxToUse <= 0) {
      return;                           // Table is full
      return;                           // Table is full
    }
        }
 
 
  int  hv = sprNum % tableSize;
        int hv = sprNum % tableSize;
 
 
  // We can use the slot if either it is empty, or it is full and the key
        // We can use the slot if either it is empty, or it is full and the key
  // number matches.
  // number matches.
  while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum))
        while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum)) {
    {
 
      hv = (hv + 1) % tableSize;
      hv = (hv + 1) % tableSize;
    }
        }
 
 
  sprIsValid[hv] = true;
        sprIsValid[hv] = true;
  sprKeyNum[hv]  = sprNum;
        sprKeyNum[hv] = sprNum;
  sprValue[hv]   = value;
        sprValue[hv] = value;
  maxToUse--;
        maxToUse--;
 
 
}       // write ()
}                               // write ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Try to read a value from the cache
//! Try to read a value from the cache
 
 
//! The entry must be valid.
//! The entry must be valid.
 
 
//! @param[in]  sprNum  The SPR being read from
//! @param[in]  sprNum  The SPR being read from
//! @param[out] value   The value read. Will be written, even if the value is
//! @param[out] value   The value read. Will be written, even if the value is
//!                     not valid.
//!                     not valid.
 
 
//! @return  True if the value was found in the hash table
//! @return  True if the value was found in the hash table
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool
bool SprCache::read(uint16_t sprNum, uint32_t & value)
SprCache::read (uint16_t  sprNum,
 
                uint32_t &value)
 
{
{
  int  hv = sprNum % tableSize;
        int hv = sprNum % tableSize;
 
 
  // Look for either an empty slot (we are not there) or a matching key (we
        // Look for either an empty slot (we are not there) or a matching key (we
  // are there)
  // are there)
  while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum))
        while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum)) {
    {
 
      hv = (hv + 1) % tableSize;
      hv = (hv + 1) % tableSize;
    }
        }
 
 
  value = sprValue[hv];
        value = sprValue[hv];
  return  sprIsValid[hv];
        return sprIsValid[hv];
 
 
}       // read ()
}                               // read ()
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.