debugger commands

This commit is contained in:
folkert van heusden 2022-04-12 10:42:39 +02:00
parent f6c1c1440e
commit 9462e2a72b
4 changed files with 85 additions and 23 deletions

View file

@ -47,9 +47,7 @@ void console_posix::put_char_ll(const char c)
void console_posix::put_string_lf(const std::string & what) void console_posix::put_string_lf(const std::string & what)
{ {
put_string(what); put_string(what + "\r\n");
put_string("\r\n");
} }
void console_posix::resize_terminal() void console_posix::resize_terminal()

24
cpu.cpp
View file

@ -45,6 +45,11 @@ void cpu::remove_breakpoint(const uint16_t addr)
breakpoints.erase(addr); breakpoints.erase(addr);
} }
std::set<uint16_t> cpu::list_breakpoints()
{
return breakpoints;
}
uint64_t cpu::get_instructions_executed_count() uint64_t cpu::get_instructions_executed_count()
{ {
// this may wreck havoc as it is not protected by a mutex // this may wreck havoc as it is not protected by a mutex
@ -1507,8 +1512,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
bool old_trace_output = trace_output; bool old_trace_output = trace_output;
trace_output = false; trace_output = false;
uint16_t pc = getPC(); uint16_t instruction = b->peekWord(addr);
uint16_t instruction = b->peekWord(pc);
bool word_mode = !!(instruction & 0x8000); bool word_mode = !!(instruction & 0x8000);
std::string word_mode_str = word_mode ? "B" : ""; std::string word_mode_str = word_mode ? "B" : "";
@ -1531,7 +1535,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
// TODO: 100000011 // TODO: 100000011
if (do_opcode == 0b000) { if (do_opcode == 0b000) {
auto dst_text { addressing_to_string(dst_register, (pc + 2) & 65535, word_mode) }; auto dst_text { addressing_to_string(dst_register, (addr + 2) & 65535, word_mode) };
auto next_word = dst_text.instruction_part; auto next_word = dst_text.instruction_part;
if (next_word != -1) if (next_word != -1)
@ -1619,7 +1623,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
} }
else if (do_opcode == 0b111) { else if (do_opcode == 0b111) {
std::string src_text = format("R%d", (instruction >> 6) & 7); std::string src_text = format("R%d", (instruction >> 6) & 7);
auto dst_text { addressing_to_string(dst_register, (pc + 2) & 65535, word_mode) }; auto dst_text { addressing_to_string(dst_register, (addr + 2) & 65535, word_mode) };
auto next_word = dst_text.instruction_part; auto next_word = dst_text.instruction_part;
if (next_word != -1) if (next_word != -1)
@ -1687,7 +1691,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
} }
// source // source
auto src_text { addressing_to_string(src_register, (pc + 2) & 65535, word_mode) }; auto src_text { addressing_to_string(src_register, (addr + 2) & 65535, word_mode) };
auto next_word_src = src_text.instruction_part; auto next_word_src = src_text.instruction_part;
if (next_word_src != -1) if (next_word_src != -1)
@ -1696,7 +1700,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
work_values.push_back(src_text.work_value); work_values.push_back(src_text.work_value);
// destination // destination
auto dst_text { addressing_to_string(dst_register, (pc + src_text.length) & 65535, word_mode) }; auto dst_text { addressing_to_string(dst_register, (addr + src_text.length) & 65535, word_mode) };
auto next_word_dst = dst_text.instruction_part; auto next_word_dst = dst_text.instruction_part;
if (next_word_dst != -1) if (next_word_dst != -1)
@ -1710,7 +1714,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
if (text.empty()) { // conditional branch instructions if (text.empty()) { // conditional branch instructions
uint8_t cb_opcode = (instruction >> 8) & 255; uint8_t cb_opcode = (instruction >> 8) & 255;
int8_t offset = instruction & 255; int8_t offset = instruction & 255;
uint16_t new_pc = (pc + 2 + offset * 2) & 65535; uint16_t new_pc = (addr + 2 + offset * 2) & 65535;
switch(cb_opcode) { switch(cb_opcode) {
case 0b00000001: case 0b00000001:
@ -1847,7 +1851,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
text = format("TRAP %o", instruction & 255); text = format("TRAP %o", instruction & 255);
if ((instruction & ~0b111111) == 0b0000000001000000) { if ((instruction & ~0b111111) == 0b0000000001000000) {
auto dst_text { addressing_to_string(dst_register, (pc + 2) & 65535, word_mode) }; auto dst_text { addressing_to_string(dst_register, (addr + 2) & 65535, word_mode) };
auto next_word = dst_text.instruction_part; auto next_word = dst_text.instruction_part;
if (next_word != -1) if (next_word != -1)
@ -1859,7 +1863,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
} }
if ((instruction & 0b1111111000000000) == 0b0000100000000000) { if ((instruction & 0b1111111000000000) == 0b0000100000000000) {
auto dst_text { addressing_to_string(dst_register, (pc + 2) & 65535, word_mode) }; auto dst_text { addressing_to_string(dst_register, (addr + 2) & 65535, word_mode) };
auto next_word = dst_text.instruction_part; auto next_word = dst_text.instruction_part;
if (next_word != -1) if (next_word != -1)
@ -1901,7 +1905,7 @@ std::map<std::string, std::vector<std::string> > cpu::disassemble(const uint16_t
else if (i == 6) else if (i == 6)
registers.push_back(format("%06o", sp[psw >> 14])); registers.push_back(format("%06o", sp[psw >> 14]));
else else
registers.push_back(format("%06o", pc)); registers.push_back(format("%06o", addr));
} }
out.insert({ "registers", registers }); out.insert({ "registers", registers });

1
cpu.h
View file

@ -67,6 +67,7 @@ public:
bool check_breakpoint(); bool check_breakpoint();
void set_breakpoint(const uint16_t addr); void set_breakpoint(const uint16_t addr);
void remove_breakpoint(const uint16_t addr); void remove_breakpoint(const uint16_t addr);
std::set<uint16_t> list_breakpoints();
void disassemble(void) const; void disassemble(void) const;
std::map<std::string, std::vector<std::string> > disassemble(const uint16_t addr) const; std::map<std::string, std::vector<std::string> > disassemble(const uint16_t addr) const;

View file

@ -3,6 +3,7 @@
#include <atomic> #include <atomic>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -127,10 +128,10 @@ uint16_t loadTape(bus *const b, const char *const file)
return start; return start;
} }
void disassemble(cpu *const c, console *const cnsl) // returns size of instruction (in bytes)
int disassemble(cpu *const c, console *const cnsl, const int pc, const bool instruction_only)
{ {
#if !defined(ESP32) auto data = c->disassemble(pc);
auto data = c->disassemble(c->getPC());
auto registers = data["registers"]; auto registers = data["registers"];
auto psw = data["psw"][0]; auto psw = data["psw"][0];
@ -145,9 +146,19 @@ void disassemble(cpu *const c, console *const cnsl)
std::string instruction = data["instruction-text"].at(0); std::string instruction = data["instruction-text"].at(0);
std::string result = format("R0: %s, R1: %s, R2: %s, R3: %s, R4: %s, R5: %s, SP: %s, PC: %s, PSW: %s, instr: %s: %s - %s", std::string result;
if (instruction_only)
result = format("PC: %06o, instr: %s\t%s\t%s",
pc,
instruction_values.c_str(),
instruction.c_str(),
work_values.c_str()
);
else
result = format("R0: %s, R1: %s, R2: %s, R3: %s, R4: %s, R5: %s, SP: %s, PC: %06o, PSW: %s, instr: %s: %s - %s",
registers[0].c_str(), registers[1].c_str(), registers[2].c_str(), registers[3].c_str(), registers[4].c_str(), registers[5].c_str(), registers[0].c_str(), registers[1].c_str(), registers[2].c_str(), registers[3].c_str(), registers[4].c_str(), registers[5].c_str(),
registers[6].c_str(), registers[7].c_str(), registers[6].c_str(), pc,
psw.c_str(), psw.c_str(),
instruction_values.c_str(), instruction_values.c_str(),
instruction.c_str(), instruction.c_str(),
@ -158,7 +169,24 @@ void disassemble(cpu *const c, console *const cnsl)
cnsl->debug(result); cnsl->debug(result);
else else
fprintf(stderr, "%s\n", result.c_str()); fprintf(stderr, "%s\n", result.c_str());
#endif
return data["instruction-values"].size() * 2;
}
std::map<std::string, std::string> split(const std::vector<std::string> & kv_array, const std::string & splitter)
{
std::map<std::string, std::string> out;
for(auto pair : kv_array) {
auto kv = split(pair, splitter);
if (kv.size() == 1)
out.insert({ kv[0], "" });
else if (kv.size() == 2)
out.insert({ kv[0], kv[1] });
}
return out;
} }
volatile bool sw = false; volatile bool sw = false;
@ -310,13 +338,43 @@ int main(int argc, char *argv[])
bool temp = terminate; bool temp = terminate;
std::string cmd = cnsl->read_line(format("%d%d", event, temp)); std::string cmd = cnsl->read_line(format("%d%d", event, temp));
auto parts = split(cmd, " "); auto parts = split(cmd, " ");
auto kv = split(parts, "=");
if (cmd == "go") if (cmd == "go")
single_step = false; single_step = false;
else if (cmd == "single" || cmd == "s") else if (cmd == "single" || cmd == "s")
single_step = true; single_step = true;
else if (cmd == "disassemble" || cmd == "d") { else if ((cmd == "sbp" || cmd == "cbp") && parts.size() == 2){
disassemble(c, single_step ? cnsl : nullptr); uint16_t pc = std::stoi(parts.at(1).c_str());
if (cmd == "sbp") {
c->set_breakpoint(pc);
cnsl->put_string_lf(format("Set breakpoint at %06o", pc));
}
else {
c->remove_breakpoint(pc);
cnsl->put_string_lf(format("Clear breakpoint at %06o", pc));
}
continue;
}
else if (cmd == "lbp") {
auto bps = c->list_breakpoints();
cnsl->put_string_lf("Breakpoints:");
for(auto pc : bps)
cnsl->put_string_lf(format(" %06o", pc));
}
else if (parts[0] == "disassemble" || parts[0] == "d") {
int pc = kv.find("pc") != kv.end() ? std::stoi(kv.find("pc")->second, nullptr, 8) : c->getPC();
int n = kv.find("n") != kv.end() ? std::stoi(kv.find("n") ->second, nullptr, 10) : 1;
for(int i=0; i<n; i++)
pc += disassemble(c, cnsl, pc, true);
continue; continue;
} }
else if (cmd == "reset" || cmd == "r") { else if (cmd == "reset" || cmd == "r") {
@ -331,11 +389,12 @@ int main(int argc, char *argv[])
else if (cmd == "quit" || cmd == "q") else if (cmd == "quit" || cmd == "q")
break; break;
else if (cmd == "help") { else if (cmd == "help") {
cnsl->put_string_lf("disassemble/d - show current instruction"); cnsl->put_string_lf("disassemble/d - show current instruction (pc=/n=)");
cnsl->put_string_lf("go - run until trap or ^e"); cnsl->put_string_lf("go - run until trap or ^e");
cnsl->put_string_lf("quit/q - stop emulator"); cnsl->put_string_lf("quit/q - stop emulator");
cnsl->put_string_lf("reset/r - reset cpu/bus/etc"); cnsl->put_string_lf("reset/r - reset cpu/bus/etc");
cnsl->put_string_lf("single/s - run 1 instruction (implicit 'disassemble' command)"); cnsl->put_string_lf("single/s - run 1 instruction (implicit 'disassemble' command)");
cnsl->put_string_lf("sbp/cbp/lbp - set/clear/list breakpoint(s)");
continue; continue;
} }
@ -346,7 +405,7 @@ int main(int argc, char *argv[])
while(!event && !terminate && !interrupt_emulation) { while(!event && !terminate && !interrupt_emulation) {
if (tracing || single_step) if (tracing || single_step)
disassemble(c, single_step ? cnsl : nullptr); disassemble(c, single_step ? cnsl : nullptr, c->getPC(), false);
if (c->check_breakpoint() && !single_step) if (c->check_breakpoint() && !single_step)
break; break;