summaryrefslogtreecommitdiff
path: root/nandgame/assembler/disas.py
blob: 11f7508ec24f30ad9e2fb2929db3fe454fd1ae5e (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
#!/usr/bin/env python3

"""
Disassembler for nandgame.

Using my own flavor of assembly language.
I don't like the "C-style" one nandgame introduces.
"""

import sys


def decode_jump(ins: int) -> str:
    if (ins & 0x7) == 0:
        return ""
    if (ins & 0x7) == 0x7:
        return "j"

    jl = (ins & (1 << 2)) != 0
    je = (ins & (1 << 1)) != 0
    jg = (ins & (1 << 0)) != 0

    # implied: and not jg
    if jl and je:
        return "jle"
    # implied: and not je
    if jl and jg:
        return "jne"
    # implied: and not je
    if je and jg:
        return "jge"

    # implied: only one flag is 1
    if jl:
        return "jl"
    if je:
        return "je"
    if jg:
        return "jg"

    return "<unknown>"


# return op, and whether it's a one-op or two-op
def decode_ins(ins: int) -> (str, bool):
    opcode = (ins >> 8) & 0x03
    ar_n_log = (ins & (1 << 10)) != 0
    opcode |= ar_n_log << 2

    if opcode == 0b000:
        return "and", True
    if opcode == 0b001:
        return "or", True
    if opcode == 0b010:
        return "xor", True
    if opcode == 0b011:
        return "neg", False
    if opcode == 0b100:
        return "add", True
    if opcode == 0b101:
        return "inc", False
    if opcode == 0b110:
        return "sub", True
    if opcode == 0b111:
        return "dec", False

    return "<?>"


# normally, X = arg1 = D
def decode_arg1(ins: int) -> str:
    use_mem = (ins & (1 << 12)) != 0
    zx = (ins & (1 << 7)) != 0
    sw = (ins & (1 << 6)) != 0

    if zx:
        return "0"
    if not sw:
        return "D"
    return "M" if use_mem else "A"


# normally, Y = arg2 = A
def decode_arg2(ins: int) -> str:
    use_mem = (ins & (1 << 12)) != 0

    # don't care, only X is zeroed
    # zx = (ins & (1 << 7)) != 0
    sw = (ins & (1 << 6)) != 0

    if sw:
        return "D"
    return "M" if use_mem else "A"


def decode_dest(ins: int) -> str:
    dA = (ins & (1 << 5)) != 0
    dD = (ins & (1 << 4)) != 0
    dM = (ins & (1 << 3)) != 0
    dest = ""
    if dA:
        dest += "A "
    if dD:
        dest += "D "
    if dM:
        dest += "M"

    return dest


def decode_instruction_complete(ins: int) -> list[str]:
    """
    Will return a 5 element list/tuple/whatever
    mnemonic, destination, X, Y, jumpdest
    """
    if ins & 0x8000 == 0:
        # mov? ldr? ldi? aaaaaaaaaaa....
        return ["mov", "A", f"#{ins}", "", ""]
    else:
        codename, two_op = decode_ins(ins)
        dest = decode_dest(ins)
        op1 = decode_arg1(ins)
        op2 = decode_arg2(ins) if two_op else ""
        jumpdest = decode_jump(ins)
        return [codename, dest, op1, op2, jumpdest]


def print_decoded(ins: int) -> str:
    (codename, dest, op1, op2, jumpdest) = decode_instruction_complete(ins)

    dest_str = f"{dest:<5}, " if dest else 7 * " "
    jumpdest_str = f"; {jumpdest}" if jumpdest else ""
    op2_str = f", {op2}" if op2 else ""
    return f"{codename:<5}{dest_str}{op1:<2}{op2_str:<5}{jumpdest_str}"


def main():
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} [filename]")
        sys.exit(1)

    try:
        filename = sys.argv[1]
        with open(filename, "rb") as f:
            while True:
                insb = f.read(2)
                if not insb:
                    break
                ins = int.from_bytes(insb)
                print(f"\t{insb[0]:02x} {insb[1]:02x}\t{print_decoded(ins)}")

    except FileNotFoundError:
        print(f"File {filename} not found.")
        sys.exit(1)
    # head, tail...
    except BrokenPipeError:
        sys.exit(0)


if __name__ == "__main__":
    main()