2013-07-03 23:50:30 -04:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
2013-07-04 00:29:09 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2013-07-03 23:50:30 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-07-04 00:29:09 -04:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <bitset>
|
2013-07-03 23:50:30 -04:00
|
|
|
|
|
|
|
#include <readline/readline.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "loader.h"
|
|
|
|
|
|
|
|
#include <debugger/commands.h>
|
|
|
|
|
2013-07-04 00:29:09 -04:00
|
|
|
#include <cpu/defs.h>
|
|
|
|
#include <cpu/CpuModule.h>
|
2013-07-03 23:50:30 -04:00
|
|
|
|
2013-07-04 00:29:09 -04:00
|
|
|
#include <macos/traps.h>
|
|
|
|
#include <macos/sysequ.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void hexdump(const uint8_t *data, ssize_t size, uint32_t address = 0)
|
|
|
|
{
|
|
|
|
const char *HexMap = "0123456789abcdef";
|
|
|
|
|
|
|
|
char buffer1[16 * 3 + 1 + 1];
|
|
|
|
char buffer2[16 + 1];
|
|
|
|
ssize_t offset = 0;
|
|
|
|
unsigned i, j;
|
|
|
|
|
|
|
|
|
|
|
|
while(size > 0)
|
|
|
|
{
|
|
|
|
std::memset(buffer1, ' ', sizeof(buffer1));
|
|
|
|
std::memset(buffer2, ' ', sizeof(buffer2));
|
|
|
|
|
|
|
|
unsigned linelen = (unsigned)std::min(size, (ssize_t)16);
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < linelen; i++)
|
|
|
|
{
|
|
|
|
unsigned x = data[i];
|
|
|
|
buffer1[j++] = HexMap[x >> 4];
|
|
|
|
buffer1[j++] = HexMap[x & 0x0f];
|
|
|
|
j++;
|
|
|
|
if (i == 7) j++;
|
|
|
|
|
|
|
|
// isascii not part of std:: and may be a macro.
|
|
|
|
buffer2[i] = isascii(x) && std::isprint(x) ? x : '.';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer1[sizeof(buffer1)-1] = 0;
|
|
|
|
buffer2[sizeof(buffer2)-1] = 0;
|
|
|
|
|
|
|
|
|
|
|
|
std::printf("%08x:\t%s\t%s\n", address + (unsigned)offset, buffer1, buffer2);
|
|
|
|
offset += 16;
|
|
|
|
data += 16;
|
|
|
|
size -= 16;
|
|
|
|
}
|
|
|
|
std::printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-07-03 23:50:30 -04:00
|
|
|
|
|
|
|
bool ParseLine(const char *iter, Command *command);
|
2013-07-04 00:29:09 -04:00
|
|
|
|
2013-07-03 23:50:30 -04:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
uint32_t debuggerReadLong(uint32_t address)
|
|
|
|
{
|
|
|
|
uint32_t tmp = 0;
|
|
|
|
for (unsigned i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (address < Flags.memorySize)
|
|
|
|
tmp = (tmp << 8) + Flags.memory[address++];
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2013-07-04 00:29:09 -04:00
|
|
|
uint16_t debuggerReadWord(uint32_t address)
|
|
|
|
{
|
|
|
|
uint16_t tmp = 0;
|
|
|
|
for (unsigned i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
if (address < Flags.memorySize)
|
|
|
|
tmp = (tmp << 8) + Flags.memory[address++];
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t debuggerReadByte(uint32_t address)
|
|
|
|
{
|
|
|
|
if (address < Flags.memorySize)
|
|
|
|
return Flags.memory[address];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-03 23:50:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DebugHelp(const Command &cmd)
|
|
|
|
{
|
|
|
|
printf("help\n");
|
|
|
|
printf("break expression\n");
|
2013-07-04 00:29:09 -04:00
|
|
|
printf("step\n");
|
|
|
|
printf("continue\n");
|
2013-07-03 23:50:30 -04:00
|
|
|
printf("\n");
|
|
|
|
printf("print expression\n");
|
|
|
|
printf("list expression\n");
|
|
|
|
printf("dump expression\n");
|
|
|
|
printf("register=expression\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("registers: a0-7, d0-7, pc, sp, fp, csr\n");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DebugPrint(const Command &cmd)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < cmd.argc; ++i)
|
|
|
|
{
|
|
|
|
uint32_t data = cmd.argv[i];
|
|
|
|
printf("$%08x %12u", data, data);
|
|
|
|
if (data & 0x80000000)
|
|
|
|
printf(" %12d", (int32_t)data);
|
|
|
|
if ((data & 0xffff8000) == 0x8000)
|
|
|
|
printf(" %6d", (int16_t)data);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DebugDump(const Command &cmd)
|
|
|
|
{
|
|
|
|
// TODO -- if no address, use previous address.
|
2013-07-04 00:29:09 -04:00
|
|
|
// TODO -- support range?
|
2013-07-03 23:50:30 -04:00
|
|
|
if (cmd.argc == 1)
|
|
|
|
{
|
|
|
|
uint32_t start = cmd.argv[0];
|
2013-07-04 00:29:09 -04:00
|
|
|
if (start >= Flags.memorySize) return;
|
2013-07-03 23:50:30 -04:00
|
|
|
|
|
|
|
uint32_t end = std::min(start + 512, Flags.memorySize);
|
|
|
|
ssize_t size = end - start;
|
|
|
|
|
|
|
|
hexdump(Flags.memory + start, size, start);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 00:29:09 -04:00
|
|
|
void DebugList(const Command &cmd)
|
|
|
|
{
|
|
|
|
// TODO -- if no address, use previous address.
|
|
|
|
// TODO -- support range?
|
|
|
|
if (cmd.argc == 1)
|
|
|
|
{
|
|
|
|
uint32_t pc = cmd.argv[0];
|
|
|
|
if (pc & 0x01)
|
|
|
|
{
|
|
|
|
printf("address is not aligned: $%08x\n", pc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char strings[4][256];
|
|
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 32; ++i)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (pc >= Flags.memorySize) break;
|
|
|
|
|
|
|
|
uint16_t opcode = debuggerReadWord(pc);
|
|
|
|
|
|
|
|
if ((opcode & 0xf000) == 0xa000)
|
|
|
|
{
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
name = TrapName(opcode);
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
printf("$%08X %-51s ; %04X\n", pc, name, opcode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("$%08X Tool #$%04X ; %04X\n",
|
|
|
|
pc, opcode, opcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
pc += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < 4; ++j) strings[j][0] = 0;
|
|
|
|
|
|
|
|
pc = cpuDisOpcode(pc, strings[0], strings[1], strings[2], strings[3]);
|
|
|
|
printf("%s %-10s %-40s ; %s\n", strings[0], strings[2], strings[3], strings[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DebugPrintRegisters(const Command &cmd)
|
|
|
|
{
|
|
|
|
printf("PC: %08X CSR: %04x\n", cpuGetPC(), cpuGetSR());
|
|
|
|
|
|
|
|
printf("D: %08x %08x %08x %08x %08x %08x %08x %08x\n",
|
|
|
|
cpuGetDReg(0), cpuGetDReg(1), cpuGetDReg(2), cpuGetDReg(3),
|
|
|
|
cpuGetDReg(4), cpuGetDReg(5), cpuGetDReg(6), cpuGetDReg(7)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
printf("A: %08x %08x %08x %08x %08x %08x %08x %08x\n",
|
|
|
|
cpuGetAReg(0), cpuGetAReg(1), cpuGetAReg(2), cpuGetAReg(3),
|
|
|
|
cpuGetAReg(4), cpuGetAReg(5), cpuGetAReg(6), cpuGetAReg(7)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo -- add sigint trap. it shall set a flag to break.
|
|
|
|
|
2013-07-03 23:50:30 -04:00
|
|
|
void DebugShell()
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
add_history("!Andy, it still has history!");
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
cp = readline("] ");
|
|
|
|
if (!cp)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
break; // prompt for exit?
|
|
|
|
}
|
|
|
|
// parse the command...
|
|
|
|
const char *iter = cp;
|
|
|
|
while (*iter && isspace(*iter)) ++iter;
|
|
|
|
|
|
|
|
if (*iter)
|
|
|
|
{
|
|
|
|
Command cmd;
|
|
|
|
std::memset(&cmd, 0, sizeof(cmd));
|
|
|
|
if (ParseLine(iter, &cmd))
|
|
|
|
{
|
|
|
|
switch(cmd.action)
|
|
|
|
{
|
|
|
|
case Print:
|
|
|
|
DebugPrint(cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Dump:
|
|
|
|
DebugDump(cmd);
|
|
|
|
break;
|
|
|
|
|
2013-07-04 00:29:09 -04:00
|
|
|
case List:
|
|
|
|
DebugList(cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrintRegisters:
|
|
|
|
DebugPrintRegisters(cmd);
|
|
|
|
break;
|
|
|
|
|
2013-07-03 23:50:30 -04:00
|
|
|
default:
|
|
|
|
DebugHelp(cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo -- don't add if same as previous command.
|
|
|
|
add_history(cp);
|
|
|
|
}
|
|
|
|
free(cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|