Fixes issue in the RiscVCounterCsrHigh<> class. PiperOrigin-RevId: 671861339 Change-Id: I615a54df0eb58dd1cf68942c5fdd0853b20b81e1
diff --git a/riscv/riscv_counter_csr.h b/riscv/riscv_counter_csr.h index 734630c..c20c269 100644 --- a/riscv/riscv_counter_csr.h +++ b/riscv/riscv_counter_csr.h
@@ -24,10 +24,15 @@ using ::mpact::sim::generic::SimpleCounter; using ::mpact::sim::riscv::RiscVSimpleCsr; +template <typename S> +class RiscVCounterCsrHigh; + // This class implements the 64 bit CSR, or the low 32 bit CSR depending on the // RiscV xlen width. template <typename T, typename S> class RiscVCounterCsr : public RiscVSimpleCsr<T> { + friend class RiscVCounterCsrHigh<S>; + public: const T kMax = std::numeric_limits<T>::max(); RiscVCounterCsr(std::string name, RiscVCsrEnum csr_enum, S* state) @@ -37,7 +42,10 @@ ~RiscVCounterCsr() override = default; // RiscVSimpleCsr method overrides. - uint32_t GetUint32() override { return (GetCounterValue() + offset_) & kMax; } + uint32_t GetUint32() override { + auto value = (GetCounterValue() + offset_) & kMax; + return value; + } uint64_t GetUint64() override { return (GetCounterValue() + offset_) & kMax; }; @@ -65,32 +73,41 @@ template <typename S> class RiscVCounterCsrHigh : public RiscVSimpleCsr<uint32_t> { public: - RiscVCounterCsrHigh(std::string name, RiscVCsrEnum csr_enum, S* state) - : RiscVSimpleCsr<uint32_t>(name, csr_enum, state) {} + RiscVCounterCsrHigh(std::string name, RiscVCsrEnum csr_enum, S* state, + RiscVCounterCsr<uint32_t, S>* low_csr) + : RiscVSimpleCsr<uint32_t>(name, csr_enum, state), low_csr_(low_csr) {} RiscVCounterCsrHigh(const RiscVCounterCsrHigh&) = delete; RiscVCounterCsrHigh& operator=(const RiscVCounterCsrHigh&) = delete; ~RiscVCounterCsrHigh() override = default; // RiscVSimpleCsr method overrides. - uint32_t GetUint32() override { return GetCounterValue() + offset_; }; + uint32_t GetUint32() override { + uint64_t offset = (offset_ << 32) | low_csr_->offset_; + uint64_t value64 = GetCounterValue() + offset; + uint32_t value = static_cast<uint32_t>(value64 >> 32); + return value; + }; uint64_t GetUint64() override { return static_cast<uint64_t>(GetUint32()); }; // Any value written to the CSR is used to create an offset from the current // value of the counter. - void Set(uint32_t value) override { offset_ = value - GetCounterValue(); }; + void Set(uint32_t value) override { + offset_ = value - (GetCounterValue() >> 32); + }; void Set(uint64_t value) override { Set(static_cast<uint32_t>(value)); }; // This is called to tie a cycle counter to the CSR. void set_counter(SimpleCounter<uint64_t>* counter) { counter_ = counter; } private: - inline uint32_t GetCounterValue() const { + inline uint64_t GetCounterValue() const { if (counter_ == nullptr) return 0; - return static_cast<uint32_t>(counter_->GetValue() >> 32); + return counter_->GetValue(); }; + RiscVCounterCsr<uint32_t, S>* low_csr_; SimpleCounter<uint64_t>* counter_ = nullptr; - uint32_t offset_ = 0; + uint64_t offset_ = 0; }; } // namespace mpact::sim::riscv
diff --git a/riscv/riscv_state.cc b/riscv/riscv_state.cc index 74c262c..290ce0a 100644 --- a/riscv/riscv_state.cc +++ b/riscv/riscv_state.cc
@@ -230,7 +230,9 @@ CHECK_NE(minstret, nullptr); if (sizeof(T) == sizeof(uint32_t)) { CHECK_NE(CreateCsr<RiscVCounterCsrHigh<RiscVState>>( - state, csr_vec, "minstreth", RiscVCsrEnum::kMInstretH, state), + state, csr_vec, "minstreth", RiscVCsrEnum::kMInstretH, state, + reinterpret_cast<RiscVCounterCsr<uint32_t, RiscVState> *>( + minstret)), nullptr); } // mcycle/mcycleh @@ -238,9 +240,11 @@ state, csr_vec, "mcycle", RiscVCsrEnum::kMCycle, state); CHECK_NE(mcycle, nullptr); if (sizeof(T) == sizeof(uint32_t)) { - CHECK_NE(CreateCsr<RiscVCounterCsrHigh<RiscVState>>( - state, csr_vec, "mcycleh", RiscVCsrEnum::kMCycleH, state), - nullptr); + CHECK_NE( + CreateCsr<RiscVCounterCsrHigh<RiscVState>>( + state, csr_vec, "mcycleh", RiscVCsrEnum::kMCycleH, state, + reinterpret_cast<RiscVCounterCsr<uint32_t, RiscVState> *>(mcycle)), + nullptr); } // Supervisor level CSRs
diff --git a/riscv/test/riscv_counter_csr_test.cc b/riscv/test/riscv_counter_csr_test.cc index 479716c..0dc6dc4 100644 --- a/riscv/test/riscv_counter_csr_test.cc +++ b/riscv/test/riscv_counter_csr_test.cc
@@ -51,7 +51,7 @@ RiscVCounterCsr<uint32_t, RiscVState> mcycle("mcycle", RiscVCsrEnum::kMCycle, nullptr); RiscVCounterCsrHigh<RiscVState> mcycleh("mcycleh", RiscVCsrEnum::kMCycleH, - nullptr); + nullptr, &mcycle); mcycle.set_counter(&counter_); mcycleh.set_counter(&counter_); // Initial value should be zero. @@ -101,7 +101,7 @@ RiscVCounterCsr<uint32_t, RiscVState> mcycle("mcycle", RiscVCsrEnum::kMCycle, nullptr); RiscVCounterCsrHigh<RiscVState> mcycleh("mcycleh", RiscVCsrEnum::kMCycleH, - nullptr); + nullptr, &mcycle); mcycle.set_counter(&counter_); mcycleh.set_counter(&counter_); EXPECT_EQ(mcycle.GetUint32(), 0);