diff options
| -rwxr-xr-x | nandgame/assembler/disas.py | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/nandgame/assembler/disas.py b/nandgame/assembler/disas.py index a68acf4..402df5c 100755 --- a/nandgame/assembler/disas.py +++ b/nandgame/assembler/disas.py @@ -6,12 +6,61 @@ if len(sys.argv) != 2: print(f"Usage: {sys.argv[0]} [filename]") sys.exit(1) +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>" + + +def decode_ins(ins: int) -> str: + opcode = (ins >> 8) & 0x03 + ar_n_log = (ins & (1<<10)) != 0 + opcode |= (ar_n_log << 2) + + if opcode == 0b000: return "AND" + if opcode == 0b001: return "OR" + if opcode == 0b010: return "XOR" + if opcode == 0b011: return "NEG" + if opcode == 0b100: return "ADD" + if opcode == 0b101: return "INC" + if opcode == 0b110: return "SUB" + if opcode == 0b111: return "DEC" + + return "<?>" + def decode(ins: int) -> str: if (ins & 0x8000 == 0): # mov? ldr? ldi? aaaaaaaaaaa.... return f"mov A, #{ins}" else: - return "<unknown>" + use_mem = (ins & (1<<12)) != 0 + zx = (ins & (1<<7)) != 0 + sw = (ins & (1<<6)) != 0 + dA = (ins & (1<<5)) != 0 + dD = (ins & (1<<4)) != 0 + dM = (ins & (1<<3)) != 0 + + codename = decode_ins(ins) + return f"{codename:<5}<unknown>{decode_jump(ins)}" try: with open(sys.argv[1], "rb") as f: |
