|
| 1 | +// Independent from the project. |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <fstream> |
| 5 | +#include <string> |
| 6 | +#include <deque> |
| 7 | +#include <cstring> |
| 8 | + |
| 9 | +#define ip (totallen - length - buf.size()) |
| 10 | + |
| 11 | +std::string tohex(int n, int len) { |
| 12 | + std::string retval = ""; |
| 13 | + for (int x = 0; x < len; x++) { |
| 14 | + retval = "0123456789ABCDEF"[n & 0xF] + retval; |
| 15 | + n >>= 4; |
| 16 | + } |
| 17 | + return retval; |
| 18 | +} |
| 19 | +// Those are important to the disassembler. |
| 20 | + |
| 21 | +// ------------------------------------------------------------------ 1 |
| 22 | +// ------------------------------------------------------------------ 2 |
| 23 | + |
| 24 | +int main(int argc, char** argv) { |
| 25 | + char* st = new char[0x400]; |
| 26 | + if (argc != 5) { // argv[0] = executable file name |
| 27 | + std::ifstream fi {"help.txt"}; |
| 28 | + do { |
| 29 | + fi.getline(st, 0x400); |
| 30 | + } while (std::strcmp(st, "*") != 0); |
| 31 | + while (true) { |
| 32 | + fi.getline(st, 0x400); |
| 33 | + if (fi.fail()) { |
| 34 | + fi.close(); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + std::cout << st << "\n"; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + std::ifstream in {argv[1], std::ios_base::binary}; |
| 42 | + in.seekg(std::stoi(argv[2], nullptr, 0)); |
| 43 | + int length = std::stoi(argv[3], nullptr, 0), l1, i, totallen = length; |
| 44 | + std::ofstream out {argv[4]}; |
| 45 | + std::deque<std::uint8_t> buf {}; |
| 46 | + char* readbuf = new char[0x10000]; |
| 47 | + |
| 48 | + l1 = std::min(length, 0x10000); |
| 49 | + in.read(readbuf, l1); |
| 50 | + length -= l1; |
| 51 | + for (i = 0; i < l1; i++) { |
| 52 | + buf.push_back(readbuf[i]); |
| 53 | + } |
| 54 | + |
| 55 | + while (buf.size() > 0) { // assume the block is valid. |
| 56 | + // That part read number in dequeue, pop_front it for some bytes and write to (ofstream) out. |
| 57 | + |
| 58 | +// ------------------------------------------------------------------ 3 |
| 59 | + |
| 60 | +// #1 |
| 61 | + |
| 62 | +// aaabbb00 f1 #{a}, #{b} |
| 63 | +if ((buf[0] & 0b00000011) == 0b00000000) { |
| 64 | + int a = buf[0] >> 5 & 0b111, b = buf[0] >> 2 & 0b111; // >> has higher precedence than & |
| 65 | + out << tohex(ip, 6) << " " << tohex(buf[0], 2) << ' ' << " " // IP and opcode |
| 66 | + << "f1 #" << a << ", #" << b // command |
| 67 | + << "\n"; |
| 68 | + buf.pop_front(); |
| 69 | + goto done; |
| 70 | +} |
| 71 | + |
| 72 | +// aaabbb01 f2 #{a}, #{b} |
| 73 | +if ((buf[0] & 0b00000011) == 0b00000001) { |
| 74 | + int a = buf[0] >> 5 & 0b111, b = buf[0] >> 2 & 0b111; |
| 75 | + out << tohex(ip, 6) << " " << tohex(buf[0], 2) << ' ' << " " // def convenience: exceed 7 spaces, plus one |
| 76 | + << "f2 #" << a << ", #" << b // command |
| 77 | + << "\n"; |
| 78 | + buf.pop_front(); |
| 79 | + goto done; |
| 80 | +} |
| 81 | + |
| 82 | +// #2 |
| 83 | + |
| 84 | +// aaaaaa11 bbbbbbbb f3 #{a}, #{b} |
| 85 | +if ((buf[0] & 0b00000011) == 0b00000011 && (buf[1] & 0b00000000) == 0b00000000) { |
| 86 | + int a = buf[0] >> 2 & 0b111111, b = buf[1] >> 0 & 0b11111111; |
| 87 | + out << tohex(ip, 6) << " " << tohex(buf[0], 2) << ' ' << tohex(buf[1], 2) << ' ' << " " |
| 88 | + << "f3 #" << a << ", #" << b // command |
| 89 | + << "\n"; |
| 90 | + buf.pop_front(); buf.pop_front(); |
| 91 | + goto done; |
| 92 | +} |
| 93 | + |
| 94 | +// * |
| 95 | + |
| 96 | + out << tohex(ip, 6) << " " << tohex(buf[0], 2) << ' ' << " " |
| 97 | + << "Unrecognized command" |
| 98 | + << "\n"; |
| 99 | + buf.pop_front(); |
| 100 | + |
| 101 | +// ------------------------------------------------------------------ 4 |
| 102 | + |
| 103 | +done: |
| 104 | + if (buf.size() < 0x20 && length != 0) { // assume all opcode is shorter than 0x20 bytes |
| 105 | + l1 = std::min(length, 0x10000); |
| 106 | + in.read(readbuf, l1); |
| 107 | + length -= l1; |
| 108 | + for (i = 0; i < l1; i++) { |
| 109 | + buf.push_back(readbuf[i]); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + in.close(); |
| 115 | + out.close(); |
| 116 | + |
| 117 | + return 0; |
| 118 | +} |
0 commit comments