summaryrefslogtreecommitdiff
path: root/nandgame/assembler/disas.cpp
blob: 8f6ff3b89e4d98efe302972d568d2058dc0055eb (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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();
}