Added option to exit on an 'ecall' instruction

PiperOrigin-RevId: 667589844
Change-Id: I2d54d35438fad9ebc9f04595ae2f16f4e00901d7
diff --git a/riscv/rv32g_sim.cc b/riscv/rv32g_sim.cc
index ab488fc..45d02d1 100644
--- a/riscv/rv32g_sim.cc
+++ b/riscv/rv32g_sim.cc
@@ -133,6 +133,9 @@
 // }
 //
 
+// Exit on execution of ecall instruction, default false.
+ABSL_FLAG(bool, exit_on_ecall, false, "Exit on ecall - false by default");
+
 constexpr char kStackEndSymbolName[] = "__stack_end";
 constexpr char kStackSizeSymbolName[] = "__stack_size";
 
@@ -252,6 +255,13 @@
 
   RiscVTop riscv_top("RiscV32Sim", &rv_state, &rv_decoder);
 
+  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);
+      return true;
+    });
+  }
+
   // Initialize the PC to the entry point.
   uint32_t entry_point = load_result.value();
   auto pc_write = riscv_top.WriteRegister("pc", entry_point);
diff --git a/riscv/rv32gv_sim.cc b/riscv/rv32gv_sim.cc
index 1748013..7fedcc5 100644
--- a/riscv/rv32gv_sim.cc
+++ b/riscv/rv32gv_sim.cc
@@ -133,6 +133,9 @@
 // }
 //
 
+// Exit on execution of ecall instruction, default false.
+ABSL_FLAG(bool, exit_on_ecall, false, "Exit on ecall - false by default");
+
 constexpr char kStackEndSymbolName[] = "__stack_end";
 constexpr char kStackSizeSymbolName[] = "__stack_size";
 
@@ -252,6 +255,13 @@
 
   RiscVTop riscv_top("RiscV32GVSim", &rv_state, &rv_decoder);
 
+  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);
+      return true;
+    });
+  }
+
   // Initialize the PC to the entry point.
   uint32_t entry_point = load_result.value();
   auto pc_write = riscv_top.WriteRegister("pc", entry_point);
diff --git a/riscv/rv64g_sim.cc b/riscv/rv64g_sim.cc
index 1c11f7a..e27ba69 100644
--- a/riscv/rv64g_sim.cc
+++ b/riscv/rv64g_sim.cc
@@ -131,6 +131,9 @@
 // }
 //
 
+// Exit on execution of ecall instruction, default false.
+ABSL_FLAG(bool, exit_on_ecall, false, "Exit on ecall - false by default");
+
 constexpr char kStackEndSymbolName[] = "__stack_end";
 constexpr char kStackSizeSymbolName[] = "__stack_size";
 
@@ -228,6 +231,13 @@
 
   RiscVTop riscv_top("RiscV32Sim", &rv_state, &rv_decoder);
 
+  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);
+      return true;
+    });
+  }
+
   // Initialize the PC to the entry point.
   uint64_t entry_point = load_result.value();
   auto pc_write = riscv_top.WriteRegister("pc", entry_point);
diff --git a/riscv/rv64gv_sim.cc b/riscv/rv64gv_sim.cc
index 4e765b9..70a0316 100644
--- a/riscv/rv64gv_sim.cc
+++ b/riscv/rv64gv_sim.cc
@@ -131,6 +131,9 @@
 // }
 //
 
+// Exit on execution of ecall instruction, default false.
+ABSL_FLAG(bool, exit_on_ecall, false, "Exit on ecall - false by default");
+
 constexpr char kStackEndSymbolName[] = "__stack_end";
 constexpr char kStackSizeSymbolName[] = "__stack_size";
 
@@ -228,6 +231,13 @@
 
   RiscVTop riscv_top("RiscV32Sim", &rv_state, &rv_decoder);
 
+  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);
+      return true;
+    });
+  }
+
   // Initialize the PC to the entry point.
   uint64_t entry_point = load_result.value();
   auto pc_write = riscv_top.WriteRegister("pc", entry_point);