| 1 | #include "Dasm.h" |
| 2 | #include "DasmTables.h" |
| 3 | #include "SymbolTable.h" |
| 4 | #include <sstream> |
| 5 | #include <iomanip> |
| 6 | |
| 7 | static char sign(unsigned char a) |
| 8 | { |
| 9 | return (a & 128) ? '-' : '+'; |
| 10 | } |
| 11 | |
| 12 | static int abs(unsigned char a) |
| 13 | { |
| 14 | return (a & 128) ? (256 - a) : a; |
| 15 | } |
| 16 | |
| 17 | static std::string toHex(unsigned value, unsigned width) |
| 18 | { |
| 19 | std::ostringstream s; |
| 20 | s << std::hex << std::setw(width) << std::setfill('0') << value; |
| 21 | return s.str(); |
| 22 | } |
| 23 | |
| 24 | static std::string translateAddress( |
| 25 | int address, MemoryLayout* memLayout, SymbolTable* symTable) |
| 26 | { |
| 27 | if (Symbol* label = symTable->getAddressSymbol(address, memLayout)) { |
| 28 | return label->text().toStdString(); |
| 29 | } else { |
| 30 | return '#' + toHex(address, 4); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static int get16(const unsigned char* memBuf, int address) |
| 35 | { |
| 36 | return memBuf[address] + 256 * memBuf[address + 1]; |
| 37 | } |
| 38 | |
| 39 | void dasm(const unsigned char* membuf, uint16_t startAddr, uint16_t endAddr, |
| 40 | DisasmLines& disasm, MemoryLayout* memLayout, SymbolTable* symTable, int currentPC) |
| 41 | { |
| 42 | int pc = startAddr; |
| 43 | int labelCount = 0; |
| 44 | Symbol* symbol = symTable->findFirstAddressSymbol(pc, memLayout); |
| 45 | |
| 46 | disasm.clear(); |
| 47 | while (pc <= int(endAddr)) { |
| 48 | // check for a label |
| 49 | while (symbol && symbol->value() == pc) { |
| 50 | ++labelCount; |
| 51 | DisasmRow destsym; |
| 52 | destsym.rowType = DisasmRow::LABEL; |
| 53 | destsym.numBytes = 0; |
| 54 | destsym.infoLine = labelCount; |
| 55 | destsym.addr = pc; |
| 56 | destsym.instr = symbol->text().toStdString(); |
| 57 | disasm.push_back(destsym); |
| 58 | symbol = symTable->findNextAddressSymbol(memLayout); |
| 59 | } |
| 60 | |
| 61 | labelCount = 0; |
| 62 | DisasmRow dest; |
| 63 | dest.rowType = DisasmRow::INSTRUCTION; |
| 64 | dest.addr = pc; |
| 65 | dest.numBytes = 0; |
| 66 | dest.infoLine = 0; |
| 67 | dest.instr.clear(); |
| 68 | |
| 69 | const char* s; |
| 70 | const char* r = nullptr; |
| 71 | switch (membuf[pc]) { |
| 72 | case 0xCB: |
| 73 | s = mnemonic_cb[membuf[pc + 1]]; |
| 74 | dest.numBytes = 2; |
| 75 | break; |
| 76 | case 0xED: |
| 77 | s = mnemonic_ed[membuf[pc + 1]]; |
| 78 | dest.numBytes = 2; |
| 79 | break; |
| 80 | case 0xDD: |
| 81 | case 0xFD: |
| 82 | r = (membuf[pc] == 0xDD) ? "ix" : "iy"; |
| 83 | if (membuf[pc + 1] != 0xcb) { |
| 84 | s = mnemonic_xx[membuf[pc + 1]]; |
| 85 | dest.numBytes = 2; |
| 86 | } else { |
| 87 | s = mnemonic_xx_cb[membuf[pc + 3]]; |
| 88 | dest.numBytes = 4; |
| 89 | } |
| 90 | break; |
| 91 | default: |
| 92 | s = mnemonic_main[membuf[pc]]; |
| 93 | dest.numBytes = 1; |
| 94 | } |
| 95 | |
| 96 | for (int j = 0; s[j]; ++j) { |
| 97 | switch (s[j]) { |
| 98 | case 'A': { |
| 99 | int address = get16(membuf, pc + dest.numBytes); |
| 100 | dest.instr += translateAddress(address, memLayout, symTable); |
| 101 | dest.numBytes += 2; |
| 102 | break; |
| 103 | } |
| 104 | case 'B': |
| 105 | dest.instr += '#' + toHex(membuf[pc + dest.numBytes], 2); |
| 106 | dest.numBytes += 1; |
| 107 | break; |
| 108 | case 'R': { |
| 109 | int address = (pc + 2 + (signed char)membuf[pc + dest.numBytes]) & 0xFFFF; |
| 110 | dest.instr += translateAddress(address, memLayout, symTable); |
| 111 | dest.numBytes += 1; |
| 112 | break; |
| 113 | } |
| 114 | case 'W': |
| 115 | dest.instr += '#' + toHex(get16(membuf, pc + dest.numBytes), 4); |
| 116 | dest.numBytes += 2; |
| 117 | break; |
| 118 | case 'X': { |
| 119 | unsigned char offset = membuf[pc + dest.numBytes]; |
| 120 | dest.instr += '(' + std::string(r) + sign(offset) |
| 121 | + '#' + toHex(abs(offset), 2) + ')'; |
| 122 | dest.numBytes += 1; |
| 123 | break; |
| 124 | } |
| 125 | case 'Y': { |
| 126 | unsigned char offset = membuf[pc + 2]; |
| 127 | dest.instr += '(' + std::string(r) + sign(offset) |
| 128 | + '#' + toHex(abs(offset), 2) + ')'; |
| 129 | break; |
| 130 | } |
| 131 | case 'I': |
| 132 | dest.instr += r; |
| 133 | break; |
| 134 | case '!': |
| 135 | dest.instr = "db #ED,#" + toHex(membuf[pc + 1], 2); |
| 136 | dest.numBytes = 2; |
| 137 | break; |
| 138 | case '@': |
| 139 | dest.instr = "db #" + toHex(membuf[pc], 2); |
| 140 | dest.numBytes = 1; |
| 141 | break; |
| 142 | case '#': |
| 143 | dest.instr = "db #" + toHex(membuf[pc + 0], 2) + |
| 144 | "#CB,#" + toHex(membuf[pc + 2], 2); |
| 145 | dest.numBytes = 2; |
| 146 | break; |
| 147 | case ' ': { |
| 148 | dest.instr.resize(7, ' '); |
| 149 | break; |
| 150 | } |
| 151 | default: |
| 152 | dest.instr += s[j]; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // handle overflow at end or label |
| 158 | int dataBytes = 0; |
| 159 | if (symbol && pc + dest.numBytes > symbol->value()) { |
| 160 | dataBytes = symbol->value() - pc; |
| 161 | } else if (pc + dest.numBytes > endAddr) { |
| 162 | dataBytes = endAddr - pc; |
| 163 | } else if (pc + dest.numBytes > currentPC) { |
| 164 | dataBytes = currentPC - pc; |
| 165 | } |
| 166 | |
| 167 | switch (dataBytes) { |
| 168 | case 1: |
| 169 | dest.instr = "db #" + toHex(membuf[pc + 0], 2); |
| 170 | dest.numBytes = 1; |
| 171 | break; |
| 172 | case 2: |
| 173 | dest.instr = "db #" + toHex(membuf[pc + 0], 2) + |
| 174 | ",#" + toHex(membuf[pc + 1], 2); |
| 175 | dest.numBytes = 2; |
| 176 | break; |
| 177 | case 3: |
| 178 | dest.instr = "db #" + toHex(membuf[pc + 0], 2) + |
| 179 | ",#" + toHex(membuf[pc + 1], 2) + |
| 180 | ",#" + toHex(membuf[pc + 2], 2); |
| 181 | dest.numBytes = 3; |
| 182 | break; |
| 183 | default: |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | if (dest.instr.size() < 8) dest.instr.resize(8, ' '); |
| 188 | disasm.push_back(dest); |
| 189 | pc += dest.numBytes; |
| 190 | } |
| 191 | } |