Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / cpus / vonk / ss / lib / csr / src / SS_CsrAccess.h
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: SS_CsrAccess.h
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
#ifndef SS_CSR_ACCESS_H
#define SS_CSR_ACCESS_H
/************************************************************************
**
** Copyright (C) 2006, Sun Microsystems, Inc.
**
** Sun considers its source code as an unpublished, proprietary
** trade secret and it is available only under strict license provisions.
** This copyright notice is placed here only to protect Sun in the event
** the source is deemed a published work. Disassembly, decompilation,
** or other means of reducing the object code to human readable form
** is prohibited by the license agreement under which this code is
** provided to the user or company in possession of this copy.
**
*************************************************************************/
#include "SS_Csr.h"
#include "SS_Strand.h"
#include "MemoryTransaction.h"
template <typename T>
class SS_CsrAccess
{
public:
SS_CsrAccess(SS_Csr& csr) :
csr_(csr) {}
virtual ~SS_CsrAccess() {}
const SS_CsrAccess<T> & operator=( const SS_CsrAccess<T> &rhs )
{
fprintf(stderr, "ERROR: SS_CsrAccess<T> & operator=(): Unimplemented function\n");
return *this;
}
/* Access a CSR at index 0 */
bool access(T &csr, bool isRead) {
return access(0, csr, isRead);
}
/* Access a CSR at ndx for read or write.
*
* The CSR is indexed relative to its base address, using its
* memory stride. This information is set by the CSR's class and
* defined by the PRM.
*/
bool access(uint64_t ndx, T &csr, bool isRead) {
SS_Strand* strand = NULL; //TODO need a valid strand-id
uint64_t data = 0;
uint64_t pa = T::BaseAddress + ndx * T::Stride;
if (ndx >= T::Count) {
fprintf(stderr, "ERROR: SS_CsrAccess::access(): Out of bounds index %d\n", ndx);
return true;
}
int access = MemoryTransaction::INTERNAL;
if (isRead) {
int state = csr_.read64(pa, &data, (access|MemoryTransaction::READ), 0/*(strand->strand_id()*/);
// if the Csr hasn't been set, use the reset value
if (state != SS_Io::OK){
T tmp;
csr = tmp;
} else {
csr.set_data(data);
}
}
else {
data = csr.getNative();
csr_.write64(pa, data, (access|MemoryTransaction::WRITE), 0/*strand->strand_id()*/);
}
return true;
}
/* Return the CSR's raw 64-bit at index, ndx. */
uint64_t getNative(uint64_t ndx) {
T csr;
access(ndx, csr, true);
return csr.getNative();
}
/* Ptr-to-member-function types used to access CSR's bitfields. */
typedef uint64_t (T::*GetFn)(void) const;
typedef void (T::*SetFn)(uint64_t);
/* Return the CSR's bitfield associated with getFn() at index 0. */
uint64_t get(GetFn getFn) {
return get(0, getFn);
}
/* Return the CSR's bitfield associated with getFn() at 'ndx'. */
uint64_t get(uint64_t ndx, GetFn getFn) {
T csr;
access(ndx, csr, true);
return ((csr).*(getFn))();
}
/* Set the CSR at index 0 to 'value'. Return the CSR's new result. */
T setNative(uint64_t ndx, uint64_t value) {
T csr;
csr.setNative(value);
access(ndx, csr, false);
return csr;
}
/* Set the CSR's bitfield associated with setFn at index 0 to ones.
* Return the CSR's new result.
*/
T set(SetFn setFn) {
return set(0, setFn, ~0ULL);
}
/* Set the CSR's bitfield associated with setFn at index 0 to 'value'.
* Return the CSR's new result.
*/
T set(SetFn setFn, uint64_t value) {
return set(0, setFn, value);
}
/* Set the CSR's bitfield associated with setFn at 'ndx' to ones.
* Return the CSR's new result.
*/
T set(uint64_t ndx, SetFn setFn) {
T csr;
access(ndx, csr, true);
((csr).*(setFn))(~0ULL);
access(ndx, csr, false);
return csr;
}
/* Set the CSR's bitfield associated with setFn at 'ndx' to 'value'.
* Return the CSR's new result.
*/
T set(uint64_t ndx, SetFn setFn, uint64_t value) {
T csr;
access(ndx, csr, true);
((csr).*(setFn))(value);
access(ndx, csr, false);
return csr;
}
private:
SS_CsrAccess();
SS_Csr& csr_;
};
#endif /* SS_CSR_ACCESS_H */