diff options
Diffstat (limited to 'nandgame/cpp/disas.cpp')
| -rw-r--r-- | nandgame/cpp/disas.cpp | 310 |
1 files changed, 164 insertions, 146 deletions
diff --git a/nandgame/cpp/disas.cpp b/nandgame/cpp/disas.cpp index ac7ca0c..f6c3cf7 100644 --- a/nandgame/cpp/disas.cpp +++ b/nandgame/cpp/disas.cpp @@ -2,193 +2,211 @@ #include "disas.h" +#include <iomanip> +#include <sstream> #include <string> #include <vector> -#include <sstream> -#include <iomanip> -static const std::string ZERO = "#0"; +static const std::string ZERO = "#0"; static const std::string DEST_NONE = "_"; static const std::string JUMP_NONE = ""; -static const std::vector<std::string> JUMPS_IF_NZERO = {"jgt", "jlt", "jne", "jmp"}; -static const std::vector<std::string> JUMPS_IF_ZERO = {"jge", "jle", "jeq", "jmp"}; +static const std::vector<std::string> JUMPS_IF_NZERO = {"jgt", "jlt", "jne", + "jmp"}; +static const std::vector<std::string> JUMPS_IF_ZERO = {"jge", "jle", "jeq", + "jmp"}; struct Decoded { - std::string mnemonic; - std::string dest; - std::string op1; - std::string op2; - std::string jumpdest; + std::string mnemonic; + std::string dest; + std::string op1; + std::string op2; + std::string jumpdest; }; static bool in_list(const std::string &s, const std::vector<std::string> &v) { - for (const auto &x : v) { - if (x == s) return true; - } - return false; + for (const auto &x : v) { + if (x == s) + return true; + } + return false; } static std::string decode_jump(uint16_t ins) { - if ((ins & 0x7) == 0) { - return JUMP_NONE; - } - - if ((ins & 0x7) == 0x7) { - return "jmp"; - } - - bool jl = (ins & (1 << 2)) != 0; - bool je = (ins & (1 << 1)) != 0; - bool jg = (ins & (1 << 0)) != 0; - - if (jl && je) { - return "jle"; - } - if (jl && jg) { - return "jne"; - } - if (je && jg) { - return "jge"; - } - - if (jl) return "jlt"; - if (je) return "jeq"; - if (jg) return "jgt"; - - return "<unknown>"; + if ((ins & 0x7) == 0) { + return JUMP_NONE; + } + + if ((ins & 0x7) == 0x7) { + return "jmp"; + } + + bool jl = (ins & (1 << 2)) != 0; + bool je = (ins & (1 << 1)) != 0; + bool jg = (ins & (1 << 0)) != 0; + + if (jl && je) { + return "jle"; + } + if (jl && jg) { + return "jne"; + } + if (je && jg) { + return "jge"; + } + + if (jl) + return "jlt"; + if (je) + return "jeq"; + if (jg) + return "jgt"; + + return "<unknown>"; } static std::pair<std::string, bool> decode_ins(uint16_t ins) { - int opcode = (ins >> 8) & 0x03; - bool ar_n_log = (ins & (1 << 10)) != 0; - opcode |= (ar_n_log ? 1 : 0) << 2; - - switch (opcode) { - case 0b000: return {"and", true}; - case 0b001: return {"or", true}; - case 0b010: return {"xor", true}; - case 0b011: return {"not", false}; - case 0b100: return {"add", true}; - case 0b101: return {"inc", false}; - case 0b110: return {"sub", true}; - case 0b111: return {"dec", false}; - default: return {"<?>", false}; - } + int opcode = (ins >> 8) & 0x03; + bool ar_n_log = (ins & (1 << 10)) != 0; + opcode |= (ar_n_log ? 1 : 0) << 2; + + switch (opcode) { + case 0b000: + return {"and", true}; + case 0b001: + return {"or", true}; + case 0b010: + return {"xor", true}; + case 0b011: + return {"not", false}; + case 0b100: + return {"add", true}; + case 0b101: + return {"inc", false}; + case 0b110: + return {"sub", true}; + case 0b111: + return {"dec", false}; + default: + return {"<?>", false}; + } } static std::string decode_arg1(uint16_t ins) { - bool use_mem = (ins & (1 << 12)) != 0; - bool zx = (ins & (1 << 7)) != 0; - bool sw = (ins & (1 << 6)) != 0; - - if (zx) return ZERO; - if (!sw) return "D"; - return use_mem ? "M" : "A"; + bool use_mem = (ins & (1 << 12)) != 0; + bool zx = (ins & (1 << 7)) != 0; + bool sw = (ins & (1 << 6)) != 0; + + if (zx) + return ZERO; + if (!sw) + return "D"; + return use_mem ? "M" : "A"; } static std::string decode_arg2(uint16_t ins) { - bool use_mem = (ins & (1 << 12)) != 0; - bool sw = (ins & (1 << 6)) != 0; + bool use_mem = (ins & (1 << 12)) != 0; + bool sw = (ins & (1 << 6)) != 0; - if (sw) return "D"; - return use_mem ? "M" : "A"; + if (sw) + return "D"; + return use_mem ? "M" : "A"; } static std::string decode_dest(uint16_t ins) { - bool dA = (ins & (1 << 5)) != 0; - bool dD = (ins & (1 << 4)) != 0; - bool dM = (ins & (1 << 3)) != 0; - - std::string dest; - if (dA) dest += "A"; - if (dD) dest += "D"; - if (dM) dest += "M"; - - return dest.empty() ? DEST_NONE : dest; + bool dA = (ins & (1 << 5)) != 0; + bool dD = (ins & (1 << 4)) != 0; + bool dM = (ins & (1 << 3)) != 0; + + std::string dest; + if (dA) + dest += "A"; + if (dD) + dest += "D"; + if (dM) + dest += "M"; + + return dest.empty() ? DEST_NONE : dest; } static Decoded decode_instruction(uint16_t ins) { - if ((ins & 0x8000) == 0) { - return {"mov", "A", "#" + std::to_string(ins), "", ""}; - } else { - auto [mnemonic, two_op] = decode_ins(ins); - std::string dest = decode_dest(ins); - std::string op1 = decode_arg1(ins); - std::string op2 = two_op ? decode_arg2(ins) : ""; - std::string jumpdst = decode_jump(ins); - return {mnemonic, dest, op1, op2, jumpdst}; - } + if ((ins & 0x8000) == 0) { + return {"mov", "A", "#" + std::to_string(ins), "", ""}; + } else { + auto [mnemonic, two_op] = decode_ins(ins); + std::string dest = decode_dest(ins); + std::string op1 = decode_arg1(ins); + std::string op2 = two_op ? decode_arg2(ins) : ""; + std::string jumpdst = decode_jump(ins); + return {mnemonic, dest, op1, op2, jumpdst}; + } } static Decoded fixup_ins(uint16_t ins) { - Decoded d = decode_instruction(ins); - std::string &mnemonic = d.mnemonic; - std::string &dest = d.dest; - std::string &op1 = d.op1; - std::string &op2 = d.op2; - std::string &jumpdest = d.jumpdest; - - if (op1 == ZERO) { - if (mnemonic == "sub") { - return {"neg", dest, op2, "", jumpdest}; - } + Decoded d = decode_instruction(ins); + std::string &mnemonic = d.mnemonic; + std::string &dest = d.dest; + std::string &op1 = d.op1; + std::string &op2 = d.op2; + std::string &jumpdest = d.jumpdest; + + if (op1 == ZERO) { + if (mnemonic == "sub") { + return {"neg", dest, op2, "", jumpdest}; + } - if (mnemonic == "and") { - if (dest == DEST_NONE) { - if (in_list(jumpdest, JUMPS_IF_ZERO)) { - return {"jmp", "", "", "", ""}; - } else { - return {"nop", "", "", "", ""}; - } - } else { - std::string newjmp = in_list(jumpdest, JUMPS_IF_ZERO) ? "jmp" : ""; - return {"mov", dest, ZERO, "", newjmp}; - } + if (mnemonic == "and") { + if (dest == DEST_NONE) { + if (in_list(jumpdest, JUMPS_IF_ZERO)) { + return {"jmp", "", "", "", ""}; + } else { + return {"nop", "", "", "", ""}; } + } else { + std::string newjmp = in_list(jumpdest, JUMPS_IF_ZERO) ? "jmp" : ""; + return {"mov", dest, ZERO, "", newjmp}; + } + } - if (mnemonic == "add" || mnemonic == "or" || mnemonic == "xor") { - if (dest == DEST_NONE && jumpdest == JUMP_NONE) { - return {"nop", "", "", "", ""}; - } else { - return {"mov", dest, op2, "", jumpdest}; - } - } + if (mnemonic == "add" || mnemonic == "or" || mnemonic == "xor") { + if (dest == DEST_NONE && jumpdest == JUMP_NONE) { + return {"nop", "", "", "", ""}; + } else { + return {"mov", dest, op2, "", jumpdest}; + } + } - if (mnemonic == "not") { - if (dest == DEST_NONE) { - if (jumpdest == "jeq" || jumpdest == "jgt" || - jumpdest == "jge" || jumpdest == JUMP_NONE) { - return {"nop", "", "", "", ""}; - } else { - return {"jmp", "", "", "", ""}; - } - } + if (mnemonic == "not") { + if (dest == DEST_NONE) { + if (jumpdest == "jeq" || jumpdest == "jgt" || jumpdest == "jge" || + jumpdest == JUMP_NONE) { + return {"nop", "", "", "", ""}; + } else { + return {"jmp", "", "", "", ""}; } + } } + } - return d; + return d; } std::string print_decoded(uint16_t ins, bool simplify) { - // illegal instruction - if ((ins & 0xC000) == 0x8000) - return "halt"; - - Decoded d = simplify ? fixup_ins(ins) : decode_instruction(ins); - - std::string jumpdest_str = d.jumpdest.empty() ? "" : "." + d.jumpdest; - std::string opcode_str = d.mnemonic + jumpdest_str; - std::string dest_str = d.dest.empty() ? std::string(7, ' ') - : d.dest + ", "; - std::string op2_str = d.op2.empty() ? "" : ", "; - std::string op1_str = d.op1 + op2_str; - - std::ostringstream oss; - oss << std::left << std::setw(9) << opcode_str - << std::setw(6) << dest_str - << std::setw(4) << op1_str - << d.op2; - - return oss.str(); + // illegal instruction + if ((ins & 0xC000) == 0x8000) + return "halt"; + + Decoded d = simplify ? fixup_ins(ins) : decode_instruction(ins); + + std::string jumpdest_str = d.jumpdest.empty() ? "" : "." + d.jumpdest; + std::string opcode_str = d.mnemonic + jumpdest_str; + std::string dest_str = d.dest.empty() ? std::string(7, ' ') : d.dest + ", "; + std::string op2_str = d.op2.empty() ? "" : ", "; + std::string op1_str = d.op1 + op2_str; + + std::ostringstream oss; + oss << std::left << std::setw(9) << opcode_str << std::setw(6) << dest_str + << std::setw(4) << op1_str << d.op2; + + return oss.str(); } |
