Adding mcycle/mcycleh CSRs, and refactoring how CRSs with counters work.

PiperOrigin-RevId: 670687615
Change-Id: I2f7819812bcc5ab1f9ef00df59737d48d69e23c2
diff --git a/riscv/BUILD b/riscv/BUILD
index 18a1d2d..e6e4984 100644
--- a/riscv/BUILD
+++ b/riscv/BUILD
@@ -48,7 +48,6 @@
     name = "riscv_state",
     srcs = [
         "riscv_csr.cc",
-        "riscv_minstret.cc",
         "riscv_misa.cc",
         "riscv_register.cc",
         "riscv_sim_csrs.cc",
@@ -58,8 +57,8 @@
         "riscv_xstatus.cc",
     ],
     hdrs = [
+        "riscv_counter_csr.h",
         "riscv_csr.h",
-        "riscv_minstret.h",
         "riscv_misa.h",
         "riscv_pmp.h",
         "riscv_register.h",
diff --git a/riscv/riscv_counter_csr.h b/riscv/riscv_counter_csr.h
new file mode 100644
index 0000000..734630c
--- /dev/null
+++ b/riscv/riscv_counter_csr.h
@@ -0,0 +1,98 @@
+#ifndef THIRD_PARTY_MPACT_RISCV_RISCV_COUNTER_CSR_H_
+#define THIRD_PARTY_MPACT_RISCV_RISCV_COUNTER_CSR_H_
+
+#include <cstdint>
+#include <limits>
+#include <string>
+
+#include "mpact/sim/generic/counters.h"
+#include "riscv/riscv_csr.h"
+
+// This file defines a base class from which to derive CSR classes for
+// minstret/minstreth and CounterCsr/mycycleh.Once instantiated, the instances
+// of these classes need to be tied to a cycle counter of the top level of the
+// simulator. That binding is done when the simulator is instantiated. Until
+// that is done, the CSRs just work like scratch CSRs.
+
+// Since these CSRs are both readable and writable, but the counter value cannot
+// be changed, every time the registers are written, a relative offset is
+// computed from the counter, so that the values read are relative to the most
+// recent write of the CSR.
+
+namespace mpact::sim::riscv {
+
+using ::mpact::sim::generic::SimpleCounter;
+using ::mpact::sim::riscv::RiscVSimpleCsr;
+
+// 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> {
+ public:
+  const T kMax = std::numeric_limits<T>::max();
+  RiscVCounterCsr(std::string name, RiscVCsrEnum csr_enum, S* state)
+      : RiscVSimpleCsr<T>(name, csr_enum, state) {}
+  RiscVCounterCsr(const RiscVCounterCsr&) = delete;
+  RiscVCounterCsr& operator=(const RiscVCounterCsr&) = delete;
+  ~RiscVCounterCsr() override = default;
+
+  // RiscVSimpleCsr method overrides.
+  uint32_t GetUint32() override { return (GetCounterValue() + offset_) & kMax; }
+  uint64_t GetUint64() override {
+    return (GetCounterValue() + offset_) & kMax;
+  };
+
+  // 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(uint64_t value) override {
+    offset_ = (value & kMax) - GetCounterValue();
+  }
+  // This is called to tie a cycle counter to the CSR.
+  void set_counter(SimpleCounter<uint64_t>* counter) { counter_ = counter; }
+
+ private:
+  inline T GetCounterValue() const {
+    if (counter_ == nullptr) return 0;
+    return static_cast<T>(counter_->GetValue() & kMax);
+  };
+
+  SimpleCounter<uint64_t>* counter_ = nullptr;
+  T offset_ = 0;
+};
+
+// This class implements the "high" version of the CSR on 32-bit RiscV.
+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(const RiscVCounterCsrHigh&) = delete;
+  RiscVCounterCsrHigh& operator=(const RiscVCounterCsrHigh&) = delete;
+  ~RiscVCounterCsrHigh() override = default;
+
+  // RiscVSimpleCsr method overrides.
+  uint32_t GetUint32() override { return GetCounterValue() + offset_; };
+  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(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 {
+    if (counter_ == nullptr) return 0;
+    return static_cast<uint32_t>(counter_->GetValue() >> 32);
+  };
+
+  SimpleCounter<uint64_t>* counter_ = nullptr;
+  uint32_t offset_ = 0;
+};
+
+}  // namespace mpact::sim::riscv
+
+#endif  // THIRD_PARTY_MPACT_RISCV_RISCV_COUNTER_CSR_H_
diff --git a/riscv/riscv_minstret.cc b/riscv/riscv_minstret.cc
deleted file mode 100644
index b70f5be..0000000
--- a/riscv/riscv_minstret.cc
+++ /dev/null
@@ -1,65 +0,0 @@
-// Copyright 2024 Google LLC
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "riscv/riscv_minstret.h"
-
-#include <cstdint>
-#include <string>
-
-#include "riscv/riscv_csr.h"
-#include "riscv/riscv_state.h"
-
-namespace mpact::sim::riscv {
-
-using ::mpact::sim::riscv::RiscVCsrEnum;
-
-RiscVMInstret::RiscVMInstret(std::string name, RiscVState *state)
-    : RiscVSimpleCsr<uint32_t>(name, RiscVCsrEnum::kMInstret, state) {}
-
-// Read the current value of the counter and apply the offset.
-uint32_t RiscVMInstret::GetUint32() {
-  if (counter_ == nullptr) return offset_;
-  uint32_t value = GetCounterValue() + offset_;
-  return value;
-}
-
-uint64_t RiscVMInstret::GetUint64() {
-  return static_cast<uint64_t>(GetUint32());
-}
-
-void RiscVMInstret::Set(uint32_t value) { offset_ = value - GetCounterValue(); }
-
-void RiscVMInstret::Set(uint64_t value) { Set(static_cast<uint32_t>(value)); }
-
-RiscVMInstreth::RiscVMInstreth(std::string name, RiscVState *state)
-    : RiscVSimpleCsr<uint32_t>(name, RiscVCsrEnum::kMInstretH, state) {}
-
-// Read the current value of the counter and apply the offset.
-uint32_t RiscVMInstreth::GetUint32() {
-  if (counter_ == nullptr) return offset_;
-  uint32_t value = GetCounterValue() + offset_;
-  return value;
-}
-
-uint64_t RiscVMInstreth::GetUint64() {
-  return static_cast<uint64_t>(GetUint32());
-}
-
-void RiscVMInstreth::Set(uint32_t value) {
-  offset_ = value - GetCounterValue();
-}
-
-void RiscVMInstreth::Set(uint64_t value) { Set(static_cast<uint32_t>(value)); }
-
-}  // namespace mpact::sim::riscv
diff --git a/riscv/riscv_minstret.h b/riscv/riscv_minstret.h
deleted file mode 100644
index 7fa106b..0000000
--- a/riscv/riscv_minstret.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright 2024 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef THIRD_PARTY_MPACT_RISCV_RISCV_MINSTRET_H_
-#define THIRD_PARTY_MPACT_RISCV_RISCV_MINSTRET_H_
-
-#include <cstdint>
-#include <string>
-
-#include "mpact/sim/generic/counters.h"
-#include "riscv/riscv_csr.h"
-
-// This file provides the declarations for the CherIoT minstret/minstreth
-// CSRs. They are tied to the instruction counter of the top level of the
-// simulator. That binding is done when the simulator is instantiated. Until
-// that is done, the CSR just works like a scratch CSR.
-
-// Since this CSR is both readable and writable, but the counter value cannot
-// be changed, every time the register is written, a relative offset is computed
-// from the counter, so that the values read are relative to the most recent
-// write of the CSR.
-
-namespace mpact::sim::riscv {
-
-using ::mpact::sim::generic::SimpleCounter;
-using ::mpact::sim::riscv::RiscVSimpleCsr;
-
-class RiscVState;
-
-class RiscVMInstret : public RiscVSimpleCsr<uint32_t> {
- public:
-  RiscVMInstret(std::string name, RiscVState* state);
-  RiscVMInstret(const RiscVMInstret&) = delete;
-  RiscVMInstret& operator=(const RiscVMInstret&) = delete;
-  ~RiscVMInstret() override = default;
-
-  // RiscVSimpleCsr method overrides.
-  uint32_t GetUint32() override;
-  uint64_t GetUint64() override;
-
-  void Set(uint32_t) override;
-  void Set(uint64_t) override;
-
-  void set_counter(SimpleCounter<uint64_t>* counter) { counter_ = counter; }
-
- private:
-  inline uint32_t GetCounterValue() const {
-    if (counter_ == nullptr) return 0;
-    return static_cast<uint32_t>(counter_->GetValue() & 0xffff'ffffULL);
-  };
-
-  SimpleCounter<uint64_t>* counter_ = nullptr;
-  uint32_t offset_ = 0;
-};
-
-class RiscVMInstreth : public RiscVSimpleCsr<uint32_t> {
- public:
-  RiscVMInstreth(std::string name, RiscVState* state);
-  RiscVMInstreth(const RiscVMInstret&) = delete;
-  RiscVMInstreth& operator=(const RiscVMInstret&) = delete;
-  ~RiscVMInstreth() override = default;
-
-  // RiscVSimpleCsr method overrides.
-  uint32_t GetUint32() override;
-  uint64_t GetUint64() override;
-
-  void Set(uint32_t) override;
-  void Set(uint64_t) override;
-
-  void set_counter(SimpleCounter<uint64_t>* counter) { counter_ = counter; }
-
- private:
-  inline uint32_t GetCounterValue() const {
-    if (counter_ == nullptr) return 0;
-    return static_cast<uint32_t>(counter_->GetValue() >> 32);
-  };
-
-  SimpleCounter<uint64_t>* counter_ = nullptr;
-  uint32_t offset_ = 0;
-};
-
-}  // namespace mpact::sim::riscv
-
-#endif  // THIRD_PARTY_MPACT_RISCV_RISCV_MINSTRET_H_
diff --git a/riscv/riscv_renode.cc b/riscv/riscv_renode.cc
index cbfd5f0..f7381f2 100644
--- a/riscv/riscv_renode.cc
+++ b/riscv/riscv_renode.cc
@@ -22,6 +22,7 @@
 #include <functional>
 #include <iostream>
 #include <memory>
+#include <new>
 #include <string>
 #include <string_view>
 
@@ -49,7 +50,6 @@
 #include "riscv/riscv_debug_info.h"
 #include "riscv/riscv_debug_interface.h"
 #include "riscv/riscv_instrumentation_control.h"
-#include "riscv/riscv_minstret.h"
 #include "riscv/riscv_register.h"
 #include "riscv/riscv_register_aliases.h"
 #include "riscv/riscv_renode_cli_top.h"
@@ -131,18 +131,6 @@
 
   riscv_top_ = new RiscVTop(name, rv_state_, rv_decoder_);
 
-  // Initialize minstret/minstreth. Bind the instruction counter to those
-  // registers.
-  auto minstret_res = riscv_top_->state()->csr_set()->GetCsr("minstret");
-  auto minstreth_res = riscv_top_->state()->csr_set()->GetCsr("minstreth");
-  if (!minstret_res.ok() || !minstreth_res.ok()) {
-    LOG(ERROR) << name << ": Error while initializing minstret/minstreth\n";
-  }
-  auto *minstret = static_cast<RiscVMInstret *>(minstret_res.value());
-  auto *minstreth = static_cast<RiscVMInstreth *>(minstreth_res.value());
-  minstret->set_counter(riscv_top_->counter_num_instructions());
-  minstreth->set_counter(riscv_top_->counter_num_instructions());
-
   // Set up the memory router with the system bus. Other devices are added once
   // config info has been received. Add a tagged default memory transactor, so
   // that any tagged loads/stores are forward to the sysbus without tags.
diff --git a/riscv/riscv_state.cc b/riscv/riscv_state.cc
index fc3ecd1..74c262c 100644
--- a/riscv/riscv_state.cc
+++ b/riscv/riscv_state.cc
@@ -17,6 +17,7 @@
 #include <algorithm>
 #include <cstdint>
 #include <limits>
+#include <new>
 #include <string>
 #include <vector>
 
@@ -27,8 +28,8 @@
 #include "mpact/sim/generic/arch_state.h"
 #include "mpact/sim/generic/type_helpers.h"
 #include "mpact/sim/util/memory/memory_interface.h"
+#include "riscv/riscv_counter_csr.h"
 #include "riscv/riscv_csr.h"
-#include "riscv/riscv_minstret.h"
 #include "riscv/riscv_misa.h"
 #include "riscv/riscv_pmp.h"
 #include "riscv/riscv_register.h"
@@ -224,10 +225,23 @@
            nullptr);
 
   // minstret/minstreth
-  CHECK_NE(CreateCsr<RiscVMInstret>(state, csr_vec, "minstret", state),
-           nullptr);
-  CHECK_NE(CreateCsr<RiscVMInstreth>(state, csr_vec, "minstreth", state),
-           nullptr);
+  auto *minstret = CreateCsr<RiscVCounterCsr<T, RiscVState>>(
+      state, csr_vec, "minstret", RiscVCsrEnum ::kMInstret, state);
+  CHECK_NE(minstret, nullptr);
+  if (sizeof(T) == sizeof(uint32_t)) {
+    CHECK_NE(CreateCsr<RiscVCounterCsrHigh<RiscVState>>(
+                 state, csr_vec, "minstreth", RiscVCsrEnum::kMInstretH, state),
+             nullptr);
+  }
+  // mcycle/mcycleh
+  auto *mcycle = CreateCsr<RiscVCounterCsr<T, RiscVState>>(
+      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);
+  }
 
   // Supervisor level CSRs
 
diff --git a/riscv/riscv_top.cc b/riscv/riscv_top.cc
index 229132a..fe4c61b 100644
--- a/riscv/riscv_top.cc
+++ b/riscv/riscv_top.cc
@@ -17,8 +17,9 @@
 #include <algorithm>
 #include <cstdint>
 #include <cstring>
+#include <new>
 #include <string>
-#include <thread>
+#include <thread>  // NOLINT: third_party.
 #include <utility>
 
 #include "absl/functional/any_invocable.h"
@@ -37,6 +38,7 @@
 #include "mpact/sim/generic/decode_cache.h"
 #include "mpact/sim/generic/decoder_interface.h"
 #include "riscv/riscv_action_point_memory_interface.h"
+#include "riscv/riscv_counter_csr.h"
 #include "riscv/riscv_csr.h"
 #include "riscv/riscv_debug_interface.h"
 #include "riscv/riscv_fp_state.h"
@@ -127,6 +129,41 @@
         << "Failed to register opcode counter";
   }
 
+  // Connect counters to instret(h) and mcycle(h) CSRs.
+  auto csr_res = state_->csr_set()->GetCsr("minstret");
+  CHECK_OK(csr_res.status()) << "Failed to get minstret CSR";
+  if (state_->xlen() == RiscVXlen::RV32) {
+    // Minstret/minstreth.
+    auto *minstret = reinterpret_cast<RiscVCounterCsr<uint32_t, RiscVState> *>(
+        csr_res.value());
+    minstret->set_counter(&counter_num_instructions_);
+    csr_res = state_->csr_set()->GetCsr("minstreth");
+    CHECK_OK(csr_res.status()) << "Failed to get minstret CSR";
+    auto *minstreth =
+        reinterpret_cast<RiscVCounterCsrHigh<RiscVState> *>(csr_res.value());
+    minstreth->set_counter(&counter_num_instructions_);
+    // Mcycle/mcycleh.
+    csr_res = state_->csr_set()->GetCsr("mcycle");
+    CHECK_OK(csr_res.status()) << "Failed to get mcycle CSR";
+    auto *mcycle = reinterpret_cast<RiscVCounterCsr<uint32_t, RiscVState> *>(
+        csr_res.value());
+    mcycle->set_counter(&counter_num_cycles_);
+    csr_res = state_->csr_set()->GetCsr("mcycleh");
+    CHECK_OK(csr_res.status()) << "Failed to get mcycleh CSR";
+    auto *mcycleh =
+        reinterpret_cast<RiscVCounterCsrHigh<RiscVState> *>(csr_res.value());
+    mcycleh->set_counter(&counter_num_cycles_);
+  } else {
+    // Minstret/minstreth.
+    auto *minstret = reinterpret_cast<RiscVCounterCsr<uint64_t, RiscVState> *>(
+        csr_res.value());
+    minstret->set_counter(&counter_num_instructions_);
+    // Mcycle/mcycleh.
+    auto *mcycle = reinterpret_cast<RiscVCounterCsr<uint64_t, RiscVState> *>(
+        csr_res.value());
+    mcycle->set_counter(&counter_num_cycles_);
+  }
+
   // Set up break and action points.
   rv_action_point_memory_interface_ = new RiscVActionPointMemoryInterface(
       state_->memory(),
diff --git a/riscv/test/BUILD b/riscv/test/BUILD
index fda50c4..f021df3 100644
--- a/riscv/test/BUILD
+++ b/riscv/test/BUILD
@@ -728,3 +728,14 @@
         "@com_google_mpact-sim//mpact/sim/generic:type_helpers",
     ],
 )
+
+cc_test(
+    name = "riscv_counter_csr_test",
+    size = "small",
+    srcs = ["riscv_counter_csr_test.cc"],
+    deps = [
+        "//riscv:riscv_state",
+        "@com_google_googletest//:gtest_main",
+        "@com_google_mpact-sim//mpact/sim/generic:counters",
+    ],
+)
diff --git a/riscv/test/riscv_counter_csr_test.cc b/riscv/test/riscv_counter_csr_test.cc
new file mode 100644
index 0000000..479716c
--- /dev/null
+++ b/riscv/test/riscv_counter_csr_test.cc
@@ -0,0 +1,129 @@
+#include "riscv/riscv_counter_csr.h"
+
+#include <cstdint>
+#include <limits>
+
+#include "googlemock/include/gmock/gmock.h"
+#include "mpact/sim/generic/counters.h"
+#include "riscv/riscv_csr.h"
+#include "riscv/riscv_state.h"
+
+// This file contains unit tests for counter_csr classes used to implement
+// mcycle/mcycleh and minstret/minstreth.
+
+namespace {
+
+using ::mpact::sim::generic::SimpleCounter;
+using ::mpact::sim::riscv::RiscVCounterCsr;
+using ::mpact::sim::riscv::RiscVCounterCsrHigh;
+using ::mpact::sim::riscv::RiscVCsrEnum;
+using ::mpact::sim::riscv::RiscVState;
+
+class RiscVMCycleTest : public ::testing::Test {
+ protected:
+  RiscVMCycleTest() : counter_("cycles", 0) {};
+  ~RiscVMCycleTest() override = default;
+
+  SimpleCounter<uint64_t> counter_;
+};
+
+// Verify the operation of 64 bit mcycle with counter increments.
+TEST_F(RiscVMCycleTest, GetTest64) {
+  RiscVCounterCsr<uint64_t, RiscVState> mcycle("mcycle", RiscVCsrEnum::kMCycle,
+                                               nullptr);
+  mcycle.set_counter(&counter_);
+  // Initial value should be zero.
+  EXPECT_EQ(mcycle.GetUint32(), 0);
+  EXPECT_EQ(mcycle.GetUint64(), 0);
+  counter_.Increment(1);
+  EXPECT_EQ(mcycle.GetUint32(), 1);
+  EXPECT_EQ(mcycle.GetUint64(), 1);
+  mcycle.Write(100u);
+  EXPECT_EQ(mcycle.GetUint32(), 100);
+  EXPECT_EQ(mcycle.GetUint64(), 100);
+  mcycle.Set(static_cast<uint64_t>(1000));
+  EXPECT_EQ(mcycle.GetUint32(), 1000);
+  EXPECT_EQ(mcycle.GetUint64(), 1000);
+}
+
+// Verify the operation of 32 bit mcycle and mcycleh with counter increments.
+TEST_F(RiscVMCycleTest, GetTest32) {
+  RiscVCounterCsr<uint32_t, RiscVState> mcycle("mcycle", RiscVCsrEnum::kMCycle,
+                                               nullptr);
+  RiscVCounterCsrHigh<RiscVState> mcycleh("mcycleh", RiscVCsrEnum::kMCycleH,
+                                          nullptr);
+  mcycle.set_counter(&counter_);
+  mcycleh.set_counter(&counter_);
+  // Initial value should be zero.
+  EXPECT_EQ(mcycle.GetUint32(), 0);
+  EXPECT_EQ(mcycle.GetUint64(), 0);
+  EXPECT_EQ(mcycleh.GetUint32(), 0);
+  EXPECT_EQ(mcycleh.GetUint64(), 0);
+  counter_.Increment(1);
+  // Only low 32 bits should be set.
+  EXPECT_EQ(mcycle.GetUint32(), 1);
+  EXPECT_EQ(mcycle.GetUint64(), 1);
+  EXPECT_EQ(mcycleh.GetUint32(), 0);
+  EXPECT_EQ(mcycleh.GetUint64(), 0);
+  // Increment counter by uint32_t max.
+  counter_.Increment(std::numeric_limits<uint32_t>::max());
+  // Counters should have wrapped around.
+  EXPECT_EQ(mcycle.GetUint32(), 0);
+  EXPECT_EQ(mcycle.GetUint64(), 0);
+  EXPECT_EQ(mcycleh.GetUint32(), 1);
+  EXPECT_EQ(mcycleh.GetUint64(), 1);
+  // Increment counter by 1 again.
+  counter_.Increment(1);
+  EXPECT_EQ(mcycle.GetUint32(), 1);
+  EXPECT_EQ(mcycle.GetUint64(), 1);
+  EXPECT_EQ(mcycleh.GetUint32(), 1);
+  EXPECT_EQ(mcycleh.GetUint64(), 1);
+}
+
+// Test that write to mcycle is reflected in the value of mcycle.
+TEST_F(RiscVMCycleTest, SetTest64) {
+  RiscVCounterCsr<uint64_t, RiscVState> mcycle("mcycle", RiscVCsrEnum::kMCycle,
+                                               nullptr);
+  mcycle.set_counter(&counter_);
+  EXPECT_EQ(mcycle.GetUint32(), 0);
+  EXPECT_EQ(mcycle.GetUint64(), 0);
+  mcycle.Write(100u);
+  EXPECT_EQ(mcycle.GetUint32(), 100);
+  EXPECT_EQ(mcycle.GetUint64(), 100);
+  counter_.Increment(10);
+  EXPECT_EQ(mcycle.GetUint32(), 110);
+  EXPECT_EQ(mcycle.GetUint64(), 110);
+}
+
+// Test that write to mcycle and mcycleh is reflected in the value of mcycle and
+// mcycleh.
+TEST_F(RiscVMCycleTest, SetTest32) {
+  RiscVCounterCsr<uint32_t, RiscVState> mcycle("mcycle", RiscVCsrEnum::kMCycle,
+                                               nullptr);
+  RiscVCounterCsrHigh<RiscVState> mcycleh("mcycleh", RiscVCsrEnum::kMCycleH,
+                                          nullptr);
+  mcycle.set_counter(&counter_);
+  mcycleh.set_counter(&counter_);
+  EXPECT_EQ(mcycle.GetUint32(), 0);
+  EXPECT_EQ(mcycle.GetUint64(), 0);
+  EXPECT_EQ(mcycleh.GetUint32(), 0);
+  EXPECT_EQ(mcycleh.GetUint64(), 0);
+  mcycle.Write(100u);
+  mcycleh.Write(200u);
+  EXPECT_EQ(mcycle.GetUint32(), 100);
+  EXPECT_EQ(mcycle.GetUint64(), 100);
+  EXPECT_EQ(mcycleh.GetUint32(), 200);
+  EXPECT_EQ(mcycleh.GetUint64(), 200);
+  counter_.Increment(10);
+  EXPECT_EQ(mcycle.GetUint32(), 110);
+  EXPECT_EQ(mcycle.GetUint64(), 110);
+  EXPECT_EQ(mcycleh.GetUint32(), 200);
+  EXPECT_EQ(mcycleh.GetUint64(), 200);
+  counter_.Increment(std::numeric_limits<uint32_t>::max());
+  EXPECT_EQ(mcycle.GetUint32(), 109);
+  EXPECT_EQ(mcycle.GetUint64(), 109);
+  EXPECT_EQ(mcycleh.GetUint32(), 201);
+  EXPECT_EQ(mcycleh.GetUint64(), 201);
+}
+
+}  // namespace