Changed how caches are instantiated and configured.

Since it is preferred to have absl::Flags all in one file (the file with,
main()), how that flag is used to configure caches was changed to use config
objects in the X_top classes.

PiperOrigin-RevId: 679300970
Change-Id: Idba06f13245e8ba089873b9020c62612aa3ac1be
diff --git a/riscv/BUILD b/riscv/BUILD
index 9568f49..b3b03f7 100644
--- a/riscv/BUILD
+++ b/riscv/BUILD
@@ -708,7 +708,6 @@
         ":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",
@@ -721,6 +720,7 @@
         "@com_google_mpact-sim//mpact/sim/generic:action_points",
         "@com_google_mpact-sim//mpact/sim/generic:arch_state",
         "@com_google_mpact-sim//mpact/sim/generic:component",
+        "@com_google_mpact-sim//mpact/sim/generic:config",
         "@com_google_mpact-sim//mpact/sim/generic:core",
         "@com_google_mpact-sim//mpact/sim/generic:core_debug_interface",
         "@com_google_mpact-sim//mpact/sim/generic:counters",
diff --git a/riscv/riscv_renode.cc b/riscv/riscv_renode.cc
index 254282a..92bcf1d 100644
--- a/riscv/riscv_renode.cc
+++ b/riscv/riscv_renode.cc
@@ -34,6 +34,7 @@
 #include "mpact/sim/generic/core_debug_interface.h"
 #include "mpact/sim/generic/type_helpers.h"
 #include "mpact/sim/proto/component_data.pb.h"
+#include "mpact/sim/proto/component_data.proto.h"
 #include "mpact/sim/util/memory/atomic_memory.h"
 #include "mpact/sim/util/memory/flat_demand_memory.h"
 #include "mpact/sim/util/memory/memory_interface.h"
@@ -63,6 +64,7 @@
 namespace riscv {
 
 using ::mpact::sim::proto::ComponentData;
+using ::mpact::sim::proto::ComponentValueEntry;
 using ::mpact::sim::riscv::RiscVClint;
 using ::mpact::sim::util::AtomicMemoryOpInterface;
 using ::mpact::sim::util::MemoryWatcher;
@@ -82,6 +84,8 @@
 constexpr std::string_view kMemProfile = "memProfile";
 constexpr std::string_view kStackEnd = "stackEnd";
 constexpr std::string_view kStackSize = "stackSize";
+constexpr std::string_view kICache = "iCache";
+constexpr std::string_view kDCache = "dCache";
 
 constexpr char kStackEndSymbolName[] = "__stack_end";
 constexpr char kStackSizeSymbolName[] = "__stack_size";
@@ -227,8 +231,8 @@
   delete riscv_cli_forwarder_;
   delete rv_decoder_;
   delete rv_fp_state_;
-  delete rv_state_;
   delete riscv_top_;
+  delete rv_state_;
   delete semihost_;
   delete router_;
   delete atomic_memory_;
@@ -401,6 +405,8 @@
 
 absl::Status RiscVRenode::SetConfig(const char *config_names[],
                                     const char *config_values[], int size) {
+  std::string icache_cfg;
+  std::string dcache_cfg;
   uint64_t memory_base = 0;
   uint64_t memory_size = 0;
   uint64_t clint_mmr_base = 0;
@@ -413,33 +419,40 @@
   int wait_for_cli = 0;
   for (int i = 0; i < size; ++i) {
     std::string name(config_names[i]);
-    auto res = ParseNumber(config_values[i]);
-    if (!res.ok()) {
-      return res.status();
-    }
-    auto value = res.value();
-    if (name == kMemoryBase) {
-      memory_base = value;
-    } else if (name == kMemorySize) {
-      memory_size = value;
-    } else if (name == kClintMMRBase) {
-      clint_mmr_base = value;
-    } else if (name == kCLIPort) {
-      cli_port = value;
-    } else if (name == kWaitForCLI) {
-      wait_for_cli = value;
-    } else if (name == kInstProfile) {
-      do_inst_profile = value != 0;
-    } else if (name == kMemProfile) {
-      mem_profiler_->set_is_enabled(value != 0);
-    } else if (name == kStackEnd) {
-      stack_end_value = value;
-      stack_end_set = true;
-    } else if (name == kStackSize) {
-      stack_size_value = value;
-      stack_size_set = true;
+    if (name == kICache) {
+      icache_cfg = config_values[i];
+    } else if (name == kDCache) {
+      dcache_cfg = config_values[i];
     } else {
-      LOG(ERROR) << "Unknown config name: " << name << " " << config_values[i];
+      auto res = ParseNumber(config_values[i]);
+      if (!res.ok()) {
+        return res.status();
+      }
+      auto value = res.value();
+      if (name == kMemoryBase) {
+        memory_base = value;
+      } else if (name == kMemorySize) {
+        memory_size = value;
+      } else if (name == kClintMMRBase) {
+        clint_mmr_base = value;
+      } else if (name == kCLIPort) {
+        cli_port = value;
+      } else if (name == kWaitForCLI) {
+        wait_for_cli = value;
+      } else if (name == kInstProfile) {
+        do_inst_profile = value != 0;
+      } else if (name == kMemProfile) {
+        mem_profiler_->set_is_enabled(value != 0);
+      } else if (name == kStackEnd) {
+        stack_end_value = value;
+        stack_end_set = true;
+      } else if (name == kStackSize) {
+        stack_size_value = value;
+        stack_size_set = true;
+      } else {
+        LOG(ERROR) << "Unknown config name: " << name << " "
+                   << config_values[i];
+      }
     }
   }
   if (memory_size == 0) {
@@ -536,6 +549,22 @@
     auto sp_write = riscv_top_->WriteRegister("sp", stack_end + stack_size);
     if (!sp_write.ok()) return sp_write;
   }
+  if (!icache_cfg.empty()) {
+    ComponentValueEntry icache_value;
+    icache_value.set_name("icache");
+    icache_value.set_string_value(icache_cfg);
+    auto *cfg = riscv_top_->GetConfig("icache");
+    auto status = cfg->Import(&icache_value);
+    if (!status.ok()) return status;
+  }
+  if (!dcache_cfg.empty()) {
+    ComponentValueEntry dcache_value;
+    dcache_value.set_name("dcache");
+    dcache_value.set_string_value(dcache_cfg);
+    auto *cfg = riscv_top_->GetConfig("dcache");
+    auto status = cfg->Import(&dcache_value);
+    if (!status.ok()) return status;
+  }
   return absl::OkStatus();
 }
 
diff --git a/riscv/riscv_top.cc b/riscv/riscv_top.cc
index a2459d8..797921b 100644
--- a/riscv/riscv_top.cc
+++ b/riscv/riscv_top.cc
@@ -21,7 +21,6 @@
 #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"
@@ -52,9 +51,6 @@
 #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 {
@@ -90,7 +86,15 @@
       state_(state),
       rv_decoder_(decoder),
       counter_num_instructions_("num_instructions", 0),
-      counter_num_cycles_("num_cycles", 0) {
+      counter_num_cycles_("num_cycles", 0),
+      icache_config_("icache", ""),
+      dcache_config_("dcache", "") {
+  CHECK_OK(AddConfig(&icache_config_));
+  icache_config_.AddValueWrittenCallback(
+      [this]() { ConfigureCache(icache_, icache_config_); });
+  CHECK_OK(AddConfig(&dcache_config_));
+  dcache_config_.AddValueWrittenCallback(
+      [this]() { ConfigureCache(dcache_, dcache_config_); });
   Initialize();
 }
 
@@ -193,14 +197,22 @@
     }
     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);
+  inst_db_ = db_factory_.Allocate<uint32_t>(1);
+}
+
+void RiscVTop::ConfigureCache(Cache *&cache, Config<std::string> &config) {
+  if (cache != nullptr) {
+    LOG(WARNING) << "Cache already configured - ignored";
+    return;
+  }
+  auto cfg_str = config.GetValue();
+  if (cfg_str.empty()) {
+    LOG(WARNING) << "Cache configuration is empty - ignored";
+  }
+  cache = new util::Cache(config.name(), this);
+  absl::Status status = cache->Configure(cfg_str, &counter_num_cycles_);
+  if (!status.ok()) {
+    LOG(ERROR) << "Failed to configure instruction cache: " << status.message();
   }
 }
 
diff --git a/riscv/riscv_top.h b/riscv/riscv_top.h
index 93b8ce5..3d97f7b 100644
--- a/riscv/riscv_top.h
+++ b/riscv/riscv_top.h
@@ -29,6 +29,7 @@
 #include "mpact/sim/generic/action_point_manager_base.h"
 #include "mpact/sim/generic/breakpoint_manager.h"
 #include "mpact/sim/generic/component.h"
+#include "mpact/sim/generic/config.h"
 #include "mpact/sim/generic/core_debug_interface.h"
 #include "mpact/sim/generic/counters.h"
 #include "mpact/sim/generic/data_buffer.h"
@@ -50,6 +51,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::generic::Config;
 using ::mpact::sim::util::Cache;
 
 // Top level class for the RiscV32G simulator. This is the main interface for
@@ -142,9 +144,14 @@
   const std::string &halt_string() const { return halt_string_; }
   void set_halt_string(std::string halt_string) { halt_string_ = halt_string; }
 
+  Cache *icache() const { return icache_; }
+  Cache *dcache() const { return dcache_; }
+
  private:
   // Initialize the top.
   void Initialize();
+  // Configure cache helper method.
+  void ConfigureCache(Cache *&cache, Config<std::string> &config);
   // Helper method to step past a breakpoint.
   absl::Status StepPastBreakpoint();
   // Set the pc value.
@@ -187,7 +194,11 @@
   // disabled with the other counters.
   generic::SimpleCounter<uint64_t> counter_pc_;
   absl::flat_hash_map<uint32_t, std::string> register_id_map_;
-  // ICache.
+  // Configuration items.
+  Config<std::string> icache_config_;
+  Config<std::string> dcache_config_;
+  // ICache & DCache.
+  Cache *dcache_ = nullptr;
   Cache *icache_ = nullptr;
   DataBuffer *inst_db_ = nullptr;
 };
diff --git a/riscv/rv32g_sim.cc b/riscv/rv32g_sim.cc
index c5c300e..d3d2149 100644
--- a/riscv/rv32g_sim.cc
+++ b/riscv/rv32g_sim.cc
@@ -21,7 +21,6 @@
 #include <ios>
 #include <iostream>
 #include <memory>
-#include <new>
 #include <optional>
 #include <ostream>
 #include <string>
@@ -60,6 +59,7 @@
 
 using ::mpact::sim::generic::Instruction;
 using ::mpact::sim::proto::ComponentData;
+using ::mpact::sim::proto::ComponentValueEntry;
 using ::mpact::sim::riscv::RiscV32Decoder;
 using ::mpact::sim::riscv::RiscV32GBitmanipDecoder;
 using ::mpact::sim::riscv::RiscV32HtifSemiHost;
@@ -143,6 +143,9 @@
 // Enable bit manipulation instructions.
 ABSL_FLAG(bool, bitmanip, false, "Enable bit manipulation instructions");
 
+// Flag to enable and configure the instruction cache.
+ABSL_FLAG(std::string, icache, "", "Instruction cache configuration");
+
 constexpr char kStackEndSymbolName[] = "__stack_end";
 constexpr char kStackSizeSymbolName[] = "__stack_size";
 
@@ -267,6 +270,17 @@
 
   RiscVTop riscv_top("RiscV32Sim", &rv_state, rv_decoder);
 
+  if (!absl::GetFlag(FLAGS_icache).empty()) {
+    ComponentValueEntry icache_value;
+    icache_value.set_name("icache");
+    icache_value.set_string_value(absl::GetFlag(FLAGS_icache));
+    auto *cfg = riscv_top.GetConfig("icache");
+    auto status = cfg->Import(&icache_value);
+    if (!status.ok()) return -1;
+  }
+
+  // TODO: enable dcache.
+
   if (absl::GetFlag(FLAGS_exit_on_ecall)) {
     rv_state.set_on_ecall([&riscv_top](const Instruction *inst) -> bool {
       riscv_top.RequestHalt(RiscVTop::HaltReason::kProgramDone, inst);
diff --git a/riscv/test/riscv_top_test.cc b/riscv/test/riscv_top_test.cc
index dcd0c78..fc23d65 100644
--- a/riscv/test/riscv_top_test.cc
+++ b/riscv/test/riscv_top_test.cc
@@ -193,8 +193,8 @@
     delete loader_;
     delete decoder_;
     delete fp_state_;
-    delete state_;
     delete riscv_top_;
+    delete state_;
     delete memory_;
   }