From 925c4969499138f569bb0fd1b1daae9fbda0fda7 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 1 Mar 2019 17:41:41 +0300 Subject: [PATCH] Read composite CSRs as one 64-bit value --- src/register/macros.rs | 22 ++++++++++++++++++++++ src/register/mcycle.rs | 1 + src/register/minstret.rs | 1 + src/register/time.rs | 1 + 4 files changed, 25 insertions(+) diff --git a/src/register/macros.rs b/src/register/macros.rs index d46d3a4..02fc424 100644 --- a/src/register/macros.rs +++ b/src/register/macros.rs @@ -208,3 +208,25 @@ macro_rules! set_clear_csr { clear_csr!($(#[$attr])*, $clear_field, $e); } } + +macro_rules! read_composite_csr { + ($hi:expr, $lo:expr) => { + /// Reads the CSR as a 64-bit value + #[inline] + pub fn read64() -> u64 { + match () { + #[cfg(riscv32)] + () => loop { + let hi = $hi; + let lo = $lo; + if hi == $hi { + return ((hi as u64) << 32) | lo as u64; + } + }, + + #[cfg(not(riscv32))] + () => $lo as u64, + } + } + } +} diff --git a/src/register/mcycle.rs b/src/register/mcycle.rs index 41e8115..95d172b 100644 --- a/src/register/mcycle.rs +++ b/src/register/mcycle.rs @@ -1,3 +1,4 @@ //! mcycle register read_csr_as_usize!(0xB00, __read_mcycle); +read_composite_csr!(super::mcycleh::read(), read()); diff --git a/src/register/minstret.rs b/src/register/minstret.rs index 6aba6b3..d553dd8 100644 --- a/src/register/minstret.rs +++ b/src/register/minstret.rs @@ -1,3 +1,4 @@ //! minstret register read_csr_as_usize!(0xB02, __read_minstret); +read_composite_csr!(super::minstreth::read(), read()); diff --git a/src/register/time.rs b/src/register/time.rs index 8793475..665b507 100644 --- a/src/register/time.rs +++ b/src/register/time.rs @@ -1,3 +1,4 @@ //! time register read_csr_as_usize!(0xC01, __read_time); +read_composite_csr!(super::timeh::read(), read());