Add a nice debug printf for debug_module_t::store
[riscv-isa-sim.git] / riscv / debug_module.cc
index fafddf14ab3d6fdd9328b6c811eda57feac92589..53df8a47d1a7415f20ac8ad88d11cb74b8b29022 100644 (file)
@@ -8,7 +8,7 @@
 #include "debug_rom/debug_rom.h"
 #include "debug_rom/debug_rom_defines.h"
 
-#if 1
+#if 0
 #  define D(x) x
 #else
 #  define D(x)
@@ -39,7 +39,7 @@ debug_module_t::debug_module_t(sim_t *sim) : sim(sim)
           jal(ZERO, debug_abstract_start - DEBUG_ROM_WHERETO));
 
   memset(debug_abstract, 0, sizeof(debug_abstract));
+
 }
 
 void debug_module_t::reset()
@@ -96,7 +96,7 @@ bool debug_module_t::load(reg_t addr, size_t len, uint8_t* bytes)
     memcpy(bytes, dmdata + addr - debug_data_start, len);
     return true;
   }
-  
+
   if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + sizeof(program_buffer)))) {
     memcpy(bytes, program_buffer + addr - debug_progbuf_start, len);
     return true;
@@ -110,6 +110,19 @@ bool debug_module_t::load(reg_t addr, size_t len, uint8_t* bytes)
 
 bool debug_module_t::store(reg_t addr, size_t len, const uint8_t* bytes)
 {
+  D(
+      switch (len) {
+        case 4:
+          fprintf(stderr, "store(addr=0x%lx, len=%d, bytes=0x%08x); "
+              "hartsel=0x%x\n", addr, (unsigned) len, *(uint32_t *) bytes,
+              dmcontrol.hartsel);
+          break;
+        default:
+          fprintf(stderr, "store(addr=0x%lx, len=%d, bytes=...); "
+              "hartsel=0x%x\n", addr, (unsigned) len, dmcontrol.hartsel);
+          break;
+      }
+   );
 
   uint8_t id_bytes[4];
   uint32_t id = 0;
@@ -119,16 +132,15 @@ bool debug_module_t::store(reg_t addr, size_t len, const uint8_t* bytes)
   }
 
   addr = DEBUG_START + addr;
-  
+
   if (addr >= debug_data_start && (addr + len) <= (debug_data_start + sizeof(dmdata))) {
     memcpy(dmdata + addr - debug_data_start, bytes, len);
     return true;
   }
-  
+
   if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + sizeof(program_buffer)))) {
-    fprintf(stderr, "Successful write to program buffer %d bytes at %x\n", (int) len, (int) addr);
     memcpy(program_buffer + addr - debug_progbuf_start, bytes, len);
-    
+
     return true;
   }
 
@@ -205,7 +217,11 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
   D(fprintf(stderr, "dmi_read(0x%x) -> ", address));
   if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
     unsigned i = address - DMI_DATA0;
-    result = abstractcs.busy ? -1 : read32(dmdata, i);
+    result = read32(dmdata, i);
+    if (abstractcs.busy) {
+      result = -1;
+      fprintf(stderr, "\ndmi_read(0x%02x (data[%d]) -> -1 because abstractcs.busy==true\n", address, i);
+    }
 
     if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
       abstractcs.cmderr = CMDERR_BUSY;
@@ -216,7 +232,11 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
     }
   } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
     unsigned i = address - DMI_PROGBUF0;
-    result = abstractcs.busy ? -1 : read32(program_buffer, i);
+    result = read32(program_buffer, i);
+    if (abstractcs.busy) {
+      result = -1;
+      fprintf(stderr, "\ndmi_read(0x%02x (progbuf[%d]) -> -1 because abstractcs.busy==true\n", address, i);
+    }
     if (!abstractcs.busy && ((abstractauto.autoexecprogbuf >> i) & 1)) {
       perform_abstract_command();
     }
@@ -268,7 +288,7 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
           } else {
             dmstatus.allresumeack = false;
           }
-          
+
          result = set_field(result, DMI_DMSTATUS_ALLNONEXISTENT, dmstatus.allnonexistant);
          result = set_field(result, DMI_DMSTATUS_ALLUNAVAIL, dmstatus.allunavail);
          result = set_field(result, DMI_DMSTATUS_ALLRUNNING, dmstatus.allrunning);
@@ -373,7 +393,7 @@ bool debug_module_t::perform_abstract_command()
       //NOP
       write32(debug_abstract, 0, addi(ZERO, ZERO, 0));
     }
-    
+
     if (get_field(command, AC_ACCESS_REGISTER_POSTEXEC)) {
       // Since the next instruction is what we will use, just use nother NOP
       // to get there.
@@ -383,7 +403,7 @@ bool debug_module_t::perform_abstract_command()
     }
 
     debug_rom_flags[dmcontrol.hartsel] |= 1 << DEBUG_ROM_FLAG_GO;
-    
+
     abstractcs.busy = true;
   } else {
     abstractcs.cmderr = CMDERR_NOTSUP;
@@ -410,7 +430,7 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
 
   } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
     unsigned i = address - DMI_PROGBUF0;
-    
+
     if (!abstractcs.busy)
       write32(program_buffer, i, value);
 
@@ -418,7 +438,7 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
       perform_abstract_command();
     }
     return true;
-    
+
   } else {
     switch (address) {
       case DMI_DMCONTROL:
@@ -455,9 +475,11 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
         return true;
 
       case DMI_ABSTRACTAUTO:
-        abstractauto.autoexecprogbuf = get_field(value, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF);
-        abstractauto.autoexecdata = get_field(value, DMI_ABSTRACTAUTO_AUTOEXECDATA);
-        break;
+        abstractauto.autoexecprogbuf = get_field(value,
+            DMI_ABSTRACTAUTO_AUTOEXECPROGBUF);
+        abstractauto.autoexecdata = get_field(value,
+            DMI_ABSTRACTAUTO_AUTOEXECDATA);
+        return true;
     }
   }
   return false;