1
0
mirror of https://github.com/sendyne/cppreg.git synced 2025-05-09 23:24:05 +00:00

CPPREG MEMORY ACCESS REVISION

All access to memory are now done through reference (const reference for
read-only access). This is done to facilitate the implementation of register
packs.
This commit is contained in:
Nicolas Clauvelin 2018-03-12 16:35:26 -04:00
parent f1d98d6bd5
commit e4ae5641ec
4 changed files with 42 additions and 42 deletions

View File

@ -59,10 +59,10 @@ namespace cppreg {
*/ */
template <typename U = void> template <typename U = void>
inline static T read( inline static T read(
const MMIO_t* const mmio_device, const MMIO_t& mmio_device,
typename std::enable_if<!is_trivial, U*>::type = nullptr typename std::enable_if<!is_trivial, U*>::type = nullptr
) noexcept { ) noexcept {
return static_cast<T>((*mmio_device & mask) >> offset); return static_cast<T>((mmio_device & mask) >> offset);
}; };
//! Trivial read implementation. //! Trivial read implementation.
@ -72,10 +72,10 @@ namespace cppreg {
*/ */
template <typename U = void> template <typename U = void>
inline static T read( inline static T read(
const MMIO_t* const mmio_device, const MMIO_t& mmio_device,
typename std::enable_if<is_trivial, U*>::type = nullptr typename std::enable_if<is_trivial, U*>::type = nullptr
) noexcept { ) noexcept {
return static_cast<T>(*mmio_device); return static_cast<T>(mmio_device);
}; };
}; };
@ -112,12 +112,12 @@ namespace cppreg {
*/ */
template <typename U = void> template <typename U = void>
inline static void write( inline static void write(
MMIO_t* const mmio_device, MMIO_t& mmio_device,
T value, T value,
typename std::enable_if<!is_trivial, U*>::type = nullptr typename std::enable_if<!is_trivial, U*>::type = nullptr
) noexcept { ) noexcept {
*mmio_device = static_cast<T>( mmio_device = static_cast<T>(
(*mmio_device & ~mask) | ((value << offset) & mask) (mmio_device & ~mask) | ((value << offset) & mask)
); );
}; };
@ -128,11 +128,11 @@ namespace cppreg {
*/ */
template <typename U = void> template <typename U = void>
inline static void write( inline static void write(
MMIO_t* const mmio_device, MMIO_t& mmio_device,
T value, T value,
typename std::enable_if<is_trivial, U*>::type = nullptr typename std::enable_if<is_trivial, U*>::type = nullptr
) noexcept { ) noexcept {
*mmio_device = value; mmio_device = value;
}; };
}; };
@ -169,11 +169,11 @@ namespace cppreg {
*/ */
template <typename U = void> template <typename U = void>
inline static void write( inline static void write(
MMIO_t* const mmio_device, MMIO_t& mmio_device,
typename std::enable_if<!is_trivial, U*>::type = nullptr typename std::enable_if<!is_trivial, U*>::type = nullptr
) noexcept { ) noexcept {
*mmio_device = static_cast<T>( mmio_device = static_cast<T>(
(*mmio_device & ~mask) | ((value << offset) & mask) (mmio_device & ~mask) | ((value << offset) & mask)
); );
}; };
@ -183,10 +183,10 @@ namespace cppreg {
*/ */
template <typename U = void> template <typename U = void>
inline static void write( inline static void write(
MMIO_t* const mmio_device, MMIO_t& mmio_device,
typename std::enable_if<is_trivial, U*>::type = nullptr typename std::enable_if<is_trivial, U*>::type = nullptr
) noexcept { ) noexcept {
*mmio_device = value; mmio_device = value;
}; };
}; };
@ -205,7 +205,7 @@ namespace cppreg {
* @return The value at the field location. * @return The value at the field location.
*/ */
template <typename MMIO_t, typename T, T mask, Offset_t offset> template <typename MMIO_t, typename T, T mask, Offset_t offset>
inline static T read(const MMIO_t* const mmio_device) noexcept { inline static T read(const MMIO_t& mmio_device) noexcept {
return RegisterRead<MMIO_t, T, mask, offset>::read(mmio_device); return RegisterRead<MMIO_t, T, mask, offset>::read(mmio_device);
}; };
@ -225,7 +225,7 @@ namespace cppreg {
* @param value Value to be written at the field location. * @param value Value to be written at the field location.
*/ */
template <typename MMIO_t, typename T, T mask, Offset_t offset> template <typename MMIO_t, typename T, T mask, Offset_t offset>
inline static void write(MMIO_t* const mmio_device, inline static void write(MMIO_t& mmio_device,
const T value) noexcept { const T value) noexcept {
RegisterWrite<MMIO_t, T, mask, offset>::write(mmio_device, value); RegisterWrite<MMIO_t, T, mask, offset>::write(mmio_device, value);
}; };
@ -240,7 +240,7 @@ namespace cppreg {
* @param mmio_device Pointer to register mapped memory. * @param mmio_device Pointer to register mapped memory.
*/ */
template <typename MMIO_t, typename T, T mask, Offset_t offset, T value> template <typename MMIO_t, typename T, T mask, Offset_t offset, T value>
inline static void write(MMIO_t* const mmio_device) noexcept { inline static void write(MMIO_t& mmio_device) noexcept {
RegisterWriteConstant<MMIO_t, T, mask, offset, value> RegisterWriteConstant<MMIO_t, T, mask, offset, value>
::write(mmio_device); ::write(mmio_device);
}; };
@ -252,7 +252,7 @@ namespace cppreg {
* @param mmio_device Pointer to register mapped memory. * @param mmio_device Pointer to register mapped memory.
*/ */
template <typename MMIO_t, typename T, T mask> template <typename MMIO_t, typename T, T mask>
inline static void set(MMIO_t* const mmio_device) inline static void set(MMIO_t& mmio_device)
noexcept { noexcept {
RegisterWriteConstant<MMIO_t, T, mask, 0u, mask> RegisterWriteConstant<MMIO_t, T, mask, 0u, mask>
::write(mmio_device); ::write(mmio_device);
@ -266,7 +266,7 @@ namespace cppreg {
* @param mmio_device Pointer to register mapped memory. * @param mmio_device Pointer to register mapped memory.
*/ */
template <typename MMIO_t, typename T, T mask> template <typename MMIO_t, typename T, T mask>
inline static void clear(MMIO_t* const mmio_device) inline static void clear(MMIO_t& mmio_device)
noexcept { noexcept {
RegisterWriteConstant<MMIO_t, T, mask, 0u, ~mask> RegisterWriteConstant<MMIO_t, T, mask, 0u, ~mask>
::write(mmio_device); ::write(mmio_device);
@ -280,9 +280,9 @@ namespace cppreg {
* @param mmio_device Pointer to register mapped memory. * @param mmio_device Pointer to register mapped memory.
*/ */
template <typename MMIO_t, typename T, T mask> template <typename MMIO_t, typename T, T mask>
inline static void toggle(MMIO_t* const mmio_device) inline static void toggle(MMIO_t& mmio_device)
noexcept { noexcept {
*mmio_device = static_cast<T>((*mmio_device) ^ mask); mmio_device = static_cast<T>((mmio_device) ^ mask);
}; };
}; };
@ -301,7 +301,7 @@ namespace cppreg {
* @param value Value to be written at the field location. * @param value Value to be written at the field location.
*/ */
template <typename MMIO_t, typename T, T mask, Offset_t offset> template <typename MMIO_t, typename T, T mask, Offset_t offset>
inline static void write(MMIO_t* const mmio_device, inline static void write(MMIO_t& mmio_device,
const T value) noexcept { const T value) noexcept {
// For write-only fields we can only write to the whole register. // For write-only fields we can only write to the whole register.
@ -320,7 +320,7 @@ namespace cppreg {
* @param mmio_device Pointer to register mapped memory. * @param mmio_device Pointer to register mapped memory.
*/ */
template <typename MMIO_t, typename T, T mask, Offset_t offset, T value> template <typename MMIO_t, typename T, T mask, Offset_t offset, T value>
inline static void write(MMIO_t* const mmio_device) noexcept { inline static void write(MMIO_t& mmio_device) noexcept {
// For write-only fields we can only write to the whole register. // For write-only fields we can only write to the whole register.
RegisterWriteConstant< RegisterWriteConstant<

View File

@ -94,7 +94,7 @@ namespace cppreg {
*/ */
inline static type read() noexcept { inline static type read() noexcept {
return policy::template read<MMIO_t, type, mask, offset>( return policy::template read<MMIO_t, type, mask, offset>(
parent_register::ro_mem_pointer() parent_register::ro_mem_device()
); );
}; };
@ -107,7 +107,7 @@ namespace cppreg {
write(const typename std::enable_if<!has_shadow, T>::type value) write(const typename std::enable_if<!has_shadow, T>::type value)
noexcept { noexcept {
policy::template write<MMIO_t, type, mask, offset>( policy::template write<MMIO_t, type, mask, offset>(
parent_register::rw_mem_pointer(), parent_register::rw_mem_device(),
value value
); );
}; };
@ -124,12 +124,12 @@ namespace cppreg {
// Update shadow value. // Update shadow value.
// This assumes that reading a write-only fields return some value. // This assumes that reading a write-only fields return some value.
RegisterWrite<type, type, mask, offset> RegisterWrite<type, type, mask, offset>
::write(&parent_register::shadow::shadow_value, value); ::write(parent_register::shadow::shadow_value, value);
// Write as a block to the register, that is, we do not use the // Write as a block to the register, that is, we do not use the
// mask and offset. // mask and offset.
policy::template write<MMIO_t, type, type_mask<type>::value, 0u>( policy::template write<MMIO_t, type, type_mask<type>::value, 0u>(
parent_register::rw_mem_pointer(), parent_register::rw_mem_device(),
parent_register::shadow::shadow_value parent_register::shadow::shadow_value
); );
@ -152,7 +152,7 @@ namespace cppreg {
>::type >::type
write() noexcept { write() noexcept {
policy::template write<MMIO_t, type, mask, offset, value>( policy::template write<MMIO_t, type, mask, offset, value>(
parent_register::rw_mem_pointer() parent_register::rw_mem_device()
); );
}; };
@ -185,7 +185,7 @@ namespace cppreg {
*/ */
inline static void set() noexcept { inline static void set() noexcept {
policy::template policy::template
set<MMIO_t, type, mask>(parent_register::rw_mem_pointer()); set<MMIO_t, type, mask>(parent_register::rw_mem_device());
}; };
//! Field clear method. //! Field clear method.
@ -194,7 +194,7 @@ namespace cppreg {
*/ */
inline static void clear() noexcept { inline static void clear() noexcept {
policy::template policy::template
clear<MMIO_t, type, mask>(parent_register::rw_mem_pointer()); clear<MMIO_t, type, mask>(parent_register::rw_mem_device());
}; };
//! Field toggle method. //! Field toggle method.
@ -203,7 +203,7 @@ namespace cppreg {
*/ */
inline static void toggle() noexcept { inline static void toggle() noexcept {
policy::template policy::template
toggle<MMIO_t, type, mask>(parent_register::rw_mem_pointer()); toggle<MMIO_t, type, mask>(parent_register::rw_mem_device());
}; };
//! Is field set bool method. //! Is field set bool method.

View File

@ -94,8 +94,8 @@ namespace cppreg {
inline void done() const && noexcept { inline void done() const && noexcept {
// Get memory pointer. // Get memory pointer.
typename Register::MMIO_t* const mmio_device = typename Register::MMIO_t& mmio_device =
Register::rw_mem_pointer(); Register::rw_mem_device();
// Write to the whole register using the current accumulated value // Write to the whole register using the current accumulated value
// and combined mask. // and combined mask.
@ -193,8 +193,8 @@ namespace cppreg {
inline void done() const && noexcept { inline void done() const && noexcept {
// Get memory pointer. // Get memory pointer.
typename Register::MMIO_t* const mmio_device = typename Register::MMIO_t& mmio_device =
Register::rw_mem_pointer(); Register::rw_mem_device();
// Write to the whole register using the current accumulated value // Write to the whole register using the current accumulated value
// and combined mask. // and combined mask.
@ -231,7 +231,7 @@ namespace cppreg {
base_type, base_type,
F::mask, F::mask,
F::offset F::offset
>(&_accumulated_value, value); >(_accumulated_value, value);
return return
std::move( std::move(

View File

@ -62,18 +62,18 @@ namespace cppreg {
//! Memory modifier. //! Memory modifier.
/** /**
* @return A pointer to the writable register memory. * @return A reference to the writable register memory.
*/ */
static MMIO_t* rw_mem_pointer() { static MMIO_t& rw_mem_device() {
return reinterpret_cast<MMIO_t* const>(base_address); return *(reinterpret_cast<MMIO_t* const>(base_address));
}; };
//! Memory accessor. //! Memory accessor.
/** /**
* @return A pointer to the read-only register memory. * @return A reference to the read-only register memory.
*/ */
static const MMIO_t* ro_mem_pointer() { static const MMIO_t& ro_mem_device() {
return reinterpret_cast<const MMIO_t* const>(base_address); return *(reinterpret_cast<const MMIO_t* const>(base_address));
}; };
//! Merge write start function. //! Merge write start function.