2012-01-09 21:05:49 +01:00
|
|
|
/*
|
|
|
|
* \brief Generic accessor framework for highly structured memory regions
|
|
|
|
* \author Martin stein
|
|
|
|
* \date 2011-11-10
|
|
|
|
*/
|
|
|
|
|
2012-01-10 12:56:45 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2012 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
2012-01-09 21:05:49 +01:00
|
|
|
#ifndef _BASE__INCLUDE__UTIL__REGISTER_H_
|
|
|
|
#define _BASE__INCLUDE__UTIL__REGISTER_H_
|
|
|
|
|
|
|
|
#include <base/stdint.h>
|
|
|
|
|
|
|
|
namespace Genode
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A POD-like highly structured memory region
|
|
|
|
*/
|
|
|
|
template <typename STORAGE_T>
|
|
|
|
struct Register
|
|
|
|
{
|
|
|
|
typedef STORAGE_T storage_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A bitregion within a register
|
|
|
|
*/
|
|
|
|
template <unsigned long BIT_SHIFT, unsigned long BIT_SIZE>
|
|
|
|
struct Subreg
|
|
|
|
{
|
|
|
|
enum {
|
|
|
|
SHIFT = BIT_SHIFT,
|
|
|
|
WIDTH = BIT_SIZE,
|
|
|
|
MASK = (1 << WIDTH) - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Back reference to containing register
|
|
|
|
*/
|
|
|
|
typedef Register<storage_t> Compound_reg;
|
|
|
|
|
|
|
|
/**
|
2012-01-10 12:56:45 +01:00
|
|
|
* Get a register value with this subreg set to 'value' and the rest left zero
|
|
|
|
*
|
|
|
|
* \detail Useful to combine successive access to multiple subregs
|
|
|
|
* into one operation
|
2012-01-09 21:05:49 +01:00
|
|
|
*/
|
|
|
|
static storage_t bits(storage_t const value) { return (value & MASK) << SHIFT; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of this subreg from 'reg'
|
|
|
|
*/
|
2012-01-10 12:56:45 +01:00
|
|
|
static storage_t get(storage_t const reg) { return (reg >> SHIFT) & MASK; }
|
2012-01-09 21:05:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get registervalue 'reg' with this subreg set to zero
|
|
|
|
*/
|
2012-01-10 12:56:45 +01:00
|
|
|
static void clear(storage_t & reg) { reg &= ~(MASK << SHIFT); }
|
2012-01-09 21:05:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get registervalue 'reg' with this subreg set to 'value'
|
|
|
|
*/
|
2012-01-10 12:56:45 +01:00
|
|
|
static void set(storage_t & reg, storage_t const value = ~0)
|
2012-01-09 21:05:49 +01:00
|
|
|
{
|
2012-01-10 12:56:45 +01:00
|
|
|
clear(reg);
|
2012-01-09 21:05:49 +01:00
|
|
|
reg |= (value & MASK) << SHIFT;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-01-10 12:56:45 +01:00
|
|
|
#endif /* _BASE__INCLUDE__UTIL__REGISTER_H_ */
|
2012-01-09 21:05:49 +01:00
|
|
|
|