diff options
| author | uvok | 2026-01-07 18:40:52 +0100 |
|---|---|---|
| committer | uvok | 2026-01-07 18:41:39 +0100 |
| commit | 7d9daab3cd540991784109fc660b663d476e606f (patch) | |
| tree | 0ab984357970ad48e6584b79c972aea865ff12a6 /nandgame/assembler/disas.cpp | |
| parent | 19e820000483be3bcae4acebbcebe393aeb81003 (diff) | |
add LLM-gened disas
Diffstat (limited to 'nandgame/assembler/disas.cpp')
| -rw-r--r-- | nandgame/assembler/disas.cpp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/nandgame/assembler/disas.cpp b/nandgame/assembler/disas.cpp new file mode 100644 index 0000000..8f6ff3b --- /dev/null +++ b/nandgame/assembler/disas.cpp @@ -0,0 +1,190 @@ +// LLM converted. Here be dragons. + +#include "disas.h" + +#include <string> +#include <vector> +#include <sstream> +#include <iomanip> + +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"}; + +struct Decoded { + 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; +} + +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>"; +} + +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}; + } +} + +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"; +} + +static std::string decode_arg2(uint16_t ins) { + bool use_mem = (ins & (1 << 12)) != 0; + bool sw = (ins & (1 << 6)) != 0; + + 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; +} + +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}; + } +} + +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}; + } + + 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 == "not") { + if (dest == DEST_NONE) { + if (jumpdest == "jeq" || jumpdest == "jgt" || + jumpdest == "jge" || jumpdest == JUMP_NONE) { + return {"nop", "", "", "", ""}; + } else { + return {"jmp", "", "", "", ""}; + } + } + } + } + + return d; +} + +std::string print_decoded(uint16_t ins, bool simplify) { + 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(); +} |
