Should fix mpact-cheriot git issue #2.

PiperOrigin-RevId: 913881224
Change-Id: Iea13334d3f34f2ea276b0b4a422f35b2e1a477db
diff --git a/cheriot/cheriot_register.cc b/cheriot/cheriot_register.cc
index 7926a57..075588d 100644
--- a/cheriot/cheriot_register.cc
+++ b/cheriot/cheriot_register.cc
@@ -204,7 +204,7 @@
   // Compute the exponent that will be used. Note, the largest exponent is 14.
   uint64_t ext_base = req_base;
   uint32_t trunc_length = static_cast<uint32_t>(req_length);
-  uint32_t exp_l = 31 - absl::countl_zero(trunc_length);
+  uint32_t exp_l = 23 - absl::countl_zero(trunc_length | 0x1ff);
   uint32_t exp_b = absl::countr_zero(ext_base);
   uint32_t exp = std::min(14u, std::min(exp_l, exp_b));
   // Reduce the requested length to the nearest representable length.
@@ -232,6 +232,8 @@
   }
   uint64_t base_bits = raw_ & 0x1ff;
   uint64_t top_bits = (raw_ >> 9) & 0x1ff;
+  uint64_t exp = (raw_ >> 18) & 0xf;
+  if (exp == 15) exp = 24;
   uint64_t a_mid = (address() >> exponent_) & 0x1ff;
   uint64_t a_hi = (a_mid < base_bits ? 1 : 0);
   uint64_t t_hi = top_bits < base_bits ? 1 : 0;
diff --git a/cheriot/cheriot_top.cc b/cheriot/cheriot_top.cc
index 36eab83..007c9de 100644
--- a/cheriot/cheriot_top.cc
+++ b/cheriot/cheriot_top.cc
@@ -17,10 +17,12 @@
 #include <algorithm>
 #include <cstdint>
 #include <cstring>
+#include <iostream>
 #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"
@@ -54,6 +56,8 @@
 #include "riscv//riscv_csr.h"
 #include "riscv//riscv_register.h"
 
+ABSL_FLAG(bool, trace, false, "Enables instruction tracing.");
+
 namespace mpact {
 namespace sim {
 namespace cheriot {
@@ -229,9 +233,9 @@
   // Execute the instruction.
   inst->Execute(nullptr);
   counter_pc_.SetValue(inst->address());
-  // Comment out instruction logging during execution.
-  // LOG(INFO) << "[" << std::hex << inst->address() << "] " <<
-  // inst->AsString();
+  if (absl::GetFlag(FLAGS_trace)) {
+    LOG(INFO) << "[" << std::hex << inst->address() << "] " << inst->AsString();
+  }
   return true;
 }
 
diff --git a/cheriot/riscv_cheriot_instructions.cc b/cheriot/riscv_cheriot_instructions.cc
index ca2d896..56fc375 100644
--- a/cheriot/riscv_cheriot_instructions.cc
+++ b/cheriot/riscv_cheriot_instructions.cc
@@ -16,6 +16,7 @@
 
 #include <any>
 #include <cstdint>
+#include <iostream>
 #include <type_traits>
 
 #include "absl/log/log.h"
@@ -147,7 +148,6 @@
 void CheriotCGetTop(const Instruction* instruction) {
   auto* cs1 = GetCapSource(instruction, 0);
   auto [unused, top] = cs1->ComputeBounds();
-  // auto top = cs1->top();
   if (top == 0x1'0000'0000ULL) {
     top = 0xffff'ffff;
   }