Set impebreak.
[riscv-isa-sim.git] / riscv / debug_module.cc
index 5f66ebed0a1843d07ac038c83baf9a5172e37ba7..981e9913467a9e41bbc739ca2b376aae61d4efc8 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)
 
 ///////////////////////// debug_module_t
 
-debug_module_t::debug_module_t(sim_t *sim) : sim(sim)
+debug_module_t::debug_module_t(sim_t *sim, unsigned progbufsize) :
+  progbufsize(progbufsize),
+  program_buffer_bytes(4 + 4*progbufsize),
+  debug_progbuf_start(debug_data_start - program_buffer_bytes),
+  debug_abstract_start(debug_progbuf_start - debug_abstract_size*4),
+  sim(sim)
 {
   dmcontrol = {0};
 
   dmstatus = {0};
+  dmstatus.impebreak = true;
   dmstatus.authenticated = 1;
-  dmstatus.versionlo = 2;
+  dmstatus.version = 2;
 
   abstractcs = {0};
-  abstractcs.progsize = progsize;
+  abstractcs.progbufsize = progbufsize;
 
   abstractauto = {0};
 
+  program_buffer = new uint8_t[program_buffer_bytes];
+
   memset(halted, 0, sizeof(halted));
   memset(debug_rom_flags, 0, sizeof(debug_rom_flags));
   memset(resumeack, 0, sizeof(resumeack));
-  memset(program_buffer, 0, sizeof(program_buffer));
+  memset(program_buffer, 0, program_buffer_bytes);
+  program_buffer[4*progbufsize] = ebreak();
+  program_buffer[4*progbufsize+1] = ebreak() >> 8;
+  program_buffer[4*progbufsize+2] = ebreak() >> 16;
+  program_buffer[4*progbufsize+3] = ebreak() >> 24;
   memset(dmdata, 0, sizeof(dmdata));
 
   write32(debug_rom_whereto, 0,
           jal(ZERO, debug_abstract_start - DEBUG_ROM_WHERETO));
 
   memset(debug_abstract, 0, sizeof(debug_abstract));
+}
+
+debug_module_t::~debug_module_t()
+{
+  delete[] program_buffer;
 }
 
 void debug_module_t::reset()
@@ -53,12 +69,13 @@ void debug_module_t::reset()
   dmcontrol = {0};
 
   dmstatus = {0};
+  dmstatus.impebreak = true;
   dmstatus.authenticated = 1;
-  dmstatus.versionlo = 2;
+  dmstatus.version = 2;
 
   abstractcs = {0};
   abstractcs.datacount = sizeof(dmdata) / 4;
-  abstractcs.progsize = progsize;
+  abstractcs.progbufsize = progbufsize;
 
   abstractauto = {0};
 }
@@ -96,8 +113,8 @@ 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)))) {
+
+  if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + program_buffer_bytes))) {
     memcpy(bytes, program_buffer + addr - debug_progbuf_start, len);
     return true;
   }
@@ -110,6 +127,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 +149,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);
+
+  if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + program_buffer_bytes))) {
     memcpy(program_buffer + addr - debug_progbuf_start, bytes, len);
-    
+
     return true;
   }
 
@@ -206,18 +235,26 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
   if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
     unsigned i = address - DMI_DATA0;
     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;
     }
 
-    if ((abstractauto.autoexecdata >> i) & 1){
+    if (!abstractcs.busy && ((abstractauto.autoexecdata >> i) & 1)) {
       perform_abstract_command();
     }
-  } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
+  } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progbufsize) {
     unsigned i = address - DMI_PROGBUF0;
     result = read32(program_buffer, i);
-    if ((abstractauto.autoexecprogbuf >> i) & 1) {
+    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 +305,9 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
           } else {
             dmstatus.allresumeack = false;
           }
-          
+
+          result = set_field(result, DMI_DMSTATUS_IMPEBREAK,
+              dmstatus.impebreak);
          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);
@@ -281,15 +320,15 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
           result = set_field(result, DMI_DMSTATUS_ANYRESUMEACK, dmstatus.anyresumeack);
           result = set_field(result, DMI_DMSTATUS_AUTHENTICATED, dmstatus.authenticated);
           result = set_field(result, DMI_DMSTATUS_AUTHBUSY, dmstatus.authbusy);
-          result = set_field(result, DMI_DMSTATUS_VERSIONHI, dmstatus.versionhi);
-          result = set_field(result, DMI_DMSTATUS_VERSIONLO, dmstatus.versionlo);
+          result = set_field(result, DMI_DMSTATUS_VERSION, dmstatus.version);
         }
        break;
       case DMI_ABSTRACTCS:
         result = set_field(result, DMI_ABSTRACTCS_CMDERR, abstractcs.cmderr);
         result = set_field(result, DMI_ABSTRACTCS_BUSY, abstractcs.busy);
         result = set_field(result, DMI_ABSTRACTCS_DATACOUNT, abstractcs.datacount);
-        result = set_field(result, DMI_ABSTRACTCS_PROGSIZE, abstractcs.progsize);
+        result = set_field(result, DMI_ABSTRACTCS_PROGBUFSIZE,
+            abstractcs.progbufsize);
         break;
       case DMI_ABSTRACTAUTO:
         result = set_field(result, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF, abstractauto.autoexecprogbuf);
@@ -373,7 +412,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 +422,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;
@@ -396,27 +435,29 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
   D(fprintf(stderr, "dmi_write(0x%x, 0x%x)\n", address, value));
   if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
     unsigned i = address - DMI_DATA0;
-    write32(dmdata, address - DMI_DATA0, value);
+    if (!abstractcs.busy)
+      write32(dmdata, address - DMI_DATA0, value);
 
     if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
       abstractcs.cmderr = CMDERR_BUSY;
     }
 
-    if ((abstractauto.autoexecdata >> i) & 1) {
+    if (!abstractcs.busy && ((abstractauto.autoexecdata >> i) & 1)) {
       perform_abstract_command();
     }
     return true;
 
-  } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
+  } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progbufsize) {
     unsigned i = address - DMI_PROGBUF0;
-    
-    write32(program_buffer, i, value);
 
-    if ((abstractauto.autoexecprogbuf >> i) & 1) {
+    if (!abstractcs.busy)
+      write32(program_buffer, i, value);
+
+    if (!abstractcs.busy && ((abstractauto.autoexecprogbuf >> i) & 1)) {
       perform_abstract_command();
     }
     return true;
-    
+
   } else {
     switch (address) {
       case DMI_DMCONTROL:
@@ -425,6 +466,7 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
           if (dmcontrol.dmactive) {
             dmcontrol.haltreq = get_field(value, DMI_DMCONTROL_HALTREQ);
             dmcontrol.resumereq = get_field(value, DMI_DMCONTROL_RESUMEREQ);
+            dmcontrol.hartreset = get_field(value, DMI_DMCONTROL_HARTRESET);
             dmcontrol.ndmreset = get_field(value, DMI_DMCONTROL_NDMRESET);
             dmcontrol.hartsel = get_field(value, DMI_DMCONTROL_HARTSEL);
           } else {
@@ -437,10 +479,16 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
               debug_rom_flags[dmcontrol.hartsel] |= (1 << DEBUG_ROM_FLAG_RESUME);
               resumeack[dmcontrol.hartsel] = false;
             }
-           if (dmcontrol.ndmreset) {
+           if (dmcontrol.hartreset) {
              proc->reset();
            }
           }
+          if (dmcontrol.ndmreset) {
+            for (size_t i = 0; i < sim->nprocs(); i++) {
+              proc = sim->get_core(i);
+              proc->reset();
+            }
+          }
         }
         return true;
 
@@ -453,9 +501,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;