aboutsummaryrefslogtreecommitdiff
path: root/riscv/debug_module.cc
blob: e3eb16b160c8c15f37cbbe5023f16a7868368f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cassert>

#include "debug_module.h"
#include "mmu.h"

#include "debug_rom/debug_rom.h"

bool debug_module_t::load(reg_t addr, size_t len, uint8_t* bytes)
{
  addr = DEBUG_START + addr;

  if (addr >= DEBUG_RAM_START && addr + len <= DEBUG_RAM_END) {
    memcpy(bytes, debug_ram + addr - DEBUG_RAM_START, len);
    return true;
  }

  if (addr >= DEBUG_ROM_START && addr + len <= DEBUG_ROM_END) {
    memcpy(bytes, debug_rom_raw + addr - DEBUG_ROM_START, len);
    return true;
  }

  fprintf(stderr, "ERROR: invalid load from debug module: %ld bytes at 0x%lx\n",
      len, addr);
  return false;
}

bool debug_module_t::store(reg_t addr, size_t len, const uint8_t* bytes)
{
  addr = DEBUG_START + addr;

  if (addr & (len-1)) {
    fprintf(stderr, "ERROR: unaligned store to debug module: %ld bytes at 0x%lx\n",
        len, addr);
    return false;
  }

  if (addr >= DEBUG_RAM_START && addr + len <= DEBUG_RAM_END) {
    memcpy(debug_ram + addr - DEBUG_RAM_START, bytes, len);
    return true;
  } else if (len == 4 && addr == DEBUG_CLEARDEBINT) {
    clear_interrupt(bytes[0] | (bytes[1] << 8) |
        (bytes[2] << 16) | (bytes[3] << 24));
    return true;
  }

  fprintf(stderr, "ERROR: invalid store to debug module: %ld bytes at 0x%lx\n",
      len, addr);
  return false;
}

void debug_module_t::ram_write32(unsigned int index, uint32_t value)
{
  char* base = debug_ram + index * 4;
  base[0] = value & 0xff;
  base[1] = (value >> 8) & 0xff;
  base[2] = (value >> 16) & 0xff;
  base[3] = (value >> 24) & 0xff;
}

uint32_t debug_module_t::ram_read32(unsigned int index)
{
  // It'd be better for raw_page (and all memory) to be unsigned chars, but mem
  // in sim_t is just chars, so I'm following that convention.
  unsigned char* base = (unsigned char*) (debug_ram + index * 4);
  uint32_t value = ((uint32_t) base[0]) |
    (((uint32_t) base[1]) << 8) |
    (((uint32_t) base[2]) << 16) |
    (((uint32_t) base[3]) << 24);
  return value;
}