This adds a cache class that can be used as part of the memory hierarchy.

As an example, an instruction cache is added (with an optional flag) to
mpact_riscv and mpact_cheriot.

PiperOrigin-RevId: 678760853
Change-Id: I63ad1e775f6559b0301c0680128a4fdf51d9d62b
diff --git a/riscv/BUILD b/riscv/BUILD
index d6b2d18..9568f49 100644
--- a/riscv/BUILD
+++ b/riscv/BUILD
@@ -708,6 +708,7 @@
         ":riscv_fp_state",
         ":riscv_state",
         "@com_google_absl//absl/container:flat_hash_map",
+        "@com_google_absl//absl/flags:flag",
         "@com_google_absl//absl/functional:any_invocable",
         "@com_google_absl//absl/functional:bind_front",
         "@com_google_absl//absl/log",
@@ -726,6 +727,7 @@
         "@com_google_mpact-sim//mpact/sim/generic:decode_cache",
         "@com_google_mpact-sim//mpact/sim/generic:type_helpers",
         "@com_google_mpact-sim//mpact/sim/util/memory",
+        "@com_google_mpact-sim//mpact/sim/util/memory:cache",
     ],
 )
 
diff --git a/riscv/riscv32g.isa b/riscv/riscv32g.isa
index 5b987c6..4415c40 100644
--- a/riscv/riscv32g.isa
+++ b/riscv/riscv32g.isa
@@ -130,7 +130,7 @@
       semfunc: "&RiscVINop";
     jal{: J_imm20 : next_pc, rd},
       resources: { next_pc : next_pc[0..], rd[0..]},
-      disasm: "jal", "%rd, %(@+J_imm20:08x)",
+      disasm: "jal", "%rd, 0x%(@+J_imm20:08x)",
       semfunc: "&RV32::RiscVIJal";
     jalr{: rs1, J_imm12 : next_pc, rd},
       resources: { next_pc, rs1 : next_pc[0..], rd[0..]},
@@ -138,7 +138,7 @@
       semfunc: "&RV32::RiscVIJalr";
     j{: J_imm20 : next_pc, rd},
       resources: { next_pc : next_pc[0..], rd[0..]},
-      disasm: "j", "%(@+J_imm20:08x)",
+      disasm: "j", "0x%(@+J_imm20:08x)",
       semfunc: "&RV32::RiscVIJal";
     jr{: rs1, J_imm12 : next_pc, rd},
       resources: { next_pc, rs1 : next_pc[0..], rd[0..]},
@@ -146,27 +146,27 @@
       semfunc: "&RV32::RiscVIJalr";
     beq{: rs1, rs2, B_imm12 : next_pc},
       resources: { next_pc, rs1, rs2 : next_pc[0..]},
-      disasm: "beq", "%rs1, %rs2, %(@+B_imm12:08x)",
+      disasm: "beq", "%rs1, %rs2, 0x%(@+B_imm12:08x)",
       semfunc: "&RV32::RiscVIBeq";
     bne{: rs1, rs2, B_imm12 : next_pc},
       resources: { next_pc, rs1, rs2 : next_pc[0..]},
-      disasm: "bne", "%rs1, %rs2, %(@+B_imm12:08x)",
+      disasm: "bne", "%rs1, %rs2, 0x%(@+B_imm12:08x)",
       semfunc: "&RV32::RiscVIBne";
     blt{: rs1, rs2, B_imm12 : next_pc},
       resources: { next_pc, rs1, rs2 : next_pc[0..]},
-      disasm: "blt", "%rs1, %rs2, %(@+B_imm12:08x)",
+      disasm: "blt", "%rs1, %rs2, 0x%(@+B_imm12:08x)",
       semfunc: "&RV32::RiscVIBlt";
     bltu{: rs1, rs2, B_imm12 : next_pc},
       resources: { next_pc, rs1, rs2 : next_pc[0..]},
-      disasm: "bltu", "%rs1, %rs2, %(@+B_imm12:08x)",
+      disasm: "bltu", "%rs1, %rs2, 0x%(@+B_imm12:08x)",
       semfunc: "&RV32::RiscVIBltu";
     bge{: rs1, rs2, B_imm12 : next_pc},
       resources: { next_pc, rs1, rs2 : next_pc[0..]},
-      disasm: "bge", "%rs1, %rs2, %(@+B_imm12:08x)",
+      disasm: "bge", "%rs1, %rs2, 0x%(@+B_imm12:08x)",
       semfunc: "&RV32::RiscVIBge";
     bgeu{: rs1, rs2, B_imm12 : next_pc},
       resources: { next_pc, rs1, rs2 : next_pc[0..]},
-      disasm: "bgeu", "%rs1, %rs2, %(@+B_imm12:08x)",
+      disasm: "bgeu", "%rs1, %rs2, 0x%(@+B_imm12:08x)",
       semfunc: "&RV32::RiscVIBgeu";
     lw{(: rs1, I_imm12), (: : rd)},
       resources: { next_pc, rs1 : rd[0..]},
@@ -754,11 +754,11 @@
       semfunc: "&RV32::RiscVDSd";
     cj{: I_cj_imm11, x0 : next_pc, x0},
       resources: {next_pc,x0 : next_pc[0..], x0[0..]},
-      disasm: "j", "%(@+I_cj_imm11:08x)",
+      disasm: "j", "0x%(@+I_cj_imm11:08x)",
       semfunc: "&RV32::RiscVIJal";
     cjal{: I_cj_imm11, x0 : next_pc, x1},
       resources: {next_pc,x0 : next_pc[0..], x1[0..]},
-      disasm: "jal", "%(@+I_cj_imm11:08x)",
+      disasm: "jal", "0x%(@+I_cj_imm11:08x)",
       semfunc: "&RV32::RiscVIJal";
     cjr{: crs1, x0 : next_pc, x0},
       resources: {next_pc,crs1, x0 : next_pc[0..], x0[0..]},
@@ -770,11 +770,11 @@
       semfunc: "&RV32::RiscVIJalr";
     cbeqz{: c3rs1, x0, I_cb_imm8 : next_pc},
       resources: {next_pc,c3rs1, x0 : next_pc[0..]},
-      disasm: "beqz", "%c3rs1, %(@+I_cb_imm8:08x)",
+      disasm: "beqz", "%c3rs1, 0x%(@+I_cb_imm8:08x)",
       semfunc: "&RV32::RiscVIBeq";
     cbnez{: c3rs1, x0, I_cb_imm8 : next_pc},
       resources: {next_pc,c3rs1, x0 : next_pc[0..]},
-      disasm: "bnez", "%c3rs1, %(@+I_cb_imm8:08x)",
+      disasm: "bnez", "%c3rs1, 0x%(@+I_cb_imm8:08x)",
       semfunc: "&RV32::RiscVIBne";
     cli{: x0, I_ci_imm6 : rd},
       resources: {next_pc,x0 : rd[0..]},
diff --git a/riscv/riscv_state.cc b/riscv/riscv_state.cc
index 290ce0a..0d489fc 100644
--- a/riscv/riscv_state.cc
+++ b/riscv/riscv_state.cc
@@ -17,7 +17,6 @@
 #include <algorithm>
 #include <cstdint>
 #include <limits>
-#include <new>
 #include <string>
 #include <vector>
 
diff --git a/riscv/riscv_top.cc b/riscv/riscv_top.cc
index aa76106..a2459d8 100644
--- a/riscv/riscv_top.cc
+++ b/riscv/riscv_top.cc
@@ -17,11 +17,11 @@
 #include <algorithm>
 #include <cstdint>
 #include <cstring>
-#include <new>
 #include <string>
 #include <thread>  // NOLINT: third_party code.
 #include <utility>
 
+#include "absl/flags/flag.h"
 #include "absl/functional/any_invocable.h"
 #include "absl/functional/bind_front.h"
 #include "absl/log/check.h"
@@ -48,9 +48,13 @@
 // Uncomment if using resource checks below.
 // #include "mpact/sim/generic/resource_operand_interface.h"
 #include "mpact/sim/generic/type_helpers.h"
+#include "mpact/sim/util/memory/cache.h"
 #include "mpact/sim/util/memory/memory_interface.h"
 #include "mpact/sim/util/memory/memory_watcher.h"
 
+// Flag to enable and configure instruction cache.
+ABSL_FLAG(std::string, icache, "", "Instruction cache configuration");
+
 namespace mpact {
 namespace sim {
 namespace riscv {
@@ -99,6 +103,8 @@
     run_halted_ = nullptr;
   }
 
+  delete icache_;
+  if (inst_db_) inst_db_->DecRef();
   delete rv_breakpoint_manager_;
   delete rv_action_point_manager_;
   delete rv_action_point_memory_interface_;
@@ -187,6 +193,15 @@
     }
     return false;
   });
+  if (!absl::GetFlag(FLAGS_icache).empty()) {
+    icache_ = new util::Cache("icache", this);
+    absl::Status status =
+        icache_->Configure(absl::GetFlag(FLAGS_icache), &counter_num_cycles_);
+    if (!status.ok()) {
+      LOG(ERROR) << "Failed to configure instruction cache: " << status;
+    }
+    inst_db_ = state_->db_factory()->Allocate<uint32_t>(1);
+  }
 }
 
 absl::Status RiscVTop::Halt() {
@@ -225,6 +240,7 @@
   uint64_t next_seq_pc = pc + real_inst->size();
   SetPc(next_seq_pc);
   bool executed = false;
+  if (icache_) ICacheFetch(pc);
   do {
     executed = ExecuteInstruction(real_inst);
     counter_num_cycles_.Increment(1);
@@ -282,6 +298,7 @@
     // that is executed will overwrite this.
     SetPc(pc + inst->size());
     bool executed = false;
+    if (icache_) ICacheFetch(pc);
     do {
       executed = ExecuteInstruction(inst);
       counter_num_cycles_.Increment(1);
@@ -368,6 +385,7 @@
       // executed will overwrite this.
       SetPc(pc + inst->size());
       bool executed = false;
+      if (icache_) ICacheFetch(pc);
       do {
         // Try executing the instruction. If it fails, advance a cycle
         // and try again.
@@ -786,6 +804,10 @@
   }
 }
 
+void RiscVTop::ICacheFetch(uint64_t address) {
+  icache_->Load(address, inst_db_, nullptr, nullptr);
+}
+
 }  // namespace riscv
 }  // namespace sim
 }  // namespace mpact
diff --git a/riscv/riscv_top.h b/riscv/riscv_top.h
index f9b9ff5..93b8ce5 100644
--- a/riscv/riscv_top.h
+++ b/riscv/riscv_top.h
@@ -36,6 +36,7 @@
 #include "mpact/sim/generic/decoder_interface.h"
 #include "mpact/sim/generic/register.h"
 #include "mpact/sim/generic/type_helpers.h"
+#include "mpact/sim/util/memory/cache.h"
 #include "mpact/sim/util/memory/memory_watcher.h"
 #include "riscv/riscv_action_point_memory_interface.h"
 #include "riscv/riscv_debug_interface.h"
@@ -49,6 +50,7 @@
 using ::mpact::sim::generic::operator*;  // NOLINT: is used below (clang error).
 using ::mpact::sim::generic::ActionPointManagerBase;
 using ::mpact::sim::generic::BreakpointManager;
+using ::mpact::sim::util::Cache;
 
 // Top level class for the RiscV32G simulator. This is the main interface for
 // interacting and controlling execution of programs running on the simulator.
@@ -147,6 +149,7 @@
   absl::Status StepPastBreakpoint();
   // Set the pc value.
   void SetPc(uint64_t value);
+  void ICacheFetch(uint64_t address);
 
   // The DB factory is used to manage data buffers for memory read/writes.
   generic::DataBufferFactory db_factory_;
@@ -184,6 +187,9 @@
   // disabled with the other counters.
   generic::SimpleCounter<uint64_t> counter_pc_;
   absl::flat_hash_map<uint32_t, std::string> register_id_map_;
+  // ICache.
+  Cache *icache_ = nullptr;
+  DataBuffer *inst_db_ = nullptr;
 };
 
 }  // namespace riscv