#include "6502dis.h" #include "6502m.h" #include "6502_opcodes.h" #include "mem.h" #include #include #include #include namespace m6502dis { char buffer[256]; int opcode_size = 0; const char *base_opcodes[256] = {"BRK ", "ORA ($%02x,X)", "---", "---", "---", "ORA $%02x", "ASL $%02x", "---", "PHP ", "ORA #$%02x", "ASL A", "---", "---", "ORA $%04x", "ASL $%04x", "---", "BPL %hhd", "ORA ($%02x),Y", "---", "---", "---", "ORA $%02x,X", "ASL $%02x,X", "---", "CLC ", "ORA $%04x,Y", "---", "---", "---", "ORA $%04x,X", "ASL $%04x,X", "---", "JSR $%04x", "AND ($%02x,X)", "---", "---", "BIT $%02x", "AND $%02x", "ROL $%02x", "---", "PLP ", "AND #$%02x", "ROL A", "---", "BIT $%04x", "AND $%04x", "ROL $%04x", "---", "BMI %hhd", "AND ($%02x),Y", "---", "---", "---", "AND $%02x,X", "ROL $%02x,X", "---", "SEC ", "AND $%04x,Y", "---", "---", "---", "AND $%04x,X", "ROL $%04x,X", "---", "RTI ", "EOR ($%02x,X)", "---", "---", "---", "EOR $%02x", "LSR $%02x", "---", "PHA ", "EOR #$%02x", "LSR A", "---", "JMP ($%04x)", "EOR $%04x", "LSR $%04x", "---", "BVC %hhd", "EOR ($%02x),Y", "---", "---", "---", "EOR $%02x,X", "LSR $%02x,X", "---", "CLI ", "EOR $%04x,Y", "---", "---", "---", "EOR $%04x,X", "LSR $%04x,X", "---", "RTS ", "ADC ($%02x,X)", "---", "---", "---", "ADC $%02x", "ROR $%02x", "---", "PLA ", "ADC #$%02x", "ROR A", "---", "JMP ($%04x)", "ADC $%04x", "ROR $%04x", "---", "BVS %hhd", "ADC ($%02x),Y", "---", "---", "---", "ADC $%02x,X", "ROR $%02x,X", "---", "SEI ", "ADC $%04x,Y", "---", "---", "---", "ADC $%04x,X", "ROR $%04x,X", "---", "---", "STA ($%02x,X)", "---", "---", "STY $%02x", "STA $%02x", "STX $%02x", "---", "DEY ", "---", "TXA ", "---", "STY $%04x", "STA $%04x", "STX $%04x", "---", "BCC %hhd", "STA ($%02x),Y", "---", "---", "STY $%02x,X", "STA $%02x,X", "STX $%02x,Y", "---", "TYA ", "STA $%04x,Y", "TXS ", "---", "---", "STA $%04x,X", "---", "---", "LDY #$%02x", "LDA ($%02x,X)", "LDX #$%02x", "---", "LDY $%02x", "LDA $%02x", "LDX $%02x", "---", "TAY ", "LDA #$%02x", "TAX ", "---", "LDY $%04x", "LDA $%04x", "LDX $%04x", "---", "BCS %hhd", "LDA ($%02x),Y", "---", "---", "LDY $%02x,X", "LDA $%02x,X", "LDX $%02x,Y", "---", "CLV ", "LDA $%04x,Y", "TSX ", "---", "LDY $%04x,X", "LDA $%04x,X", "LDX $%04x,Y", "---", "CPY #$%02x", "CMP ($%02x,X)", "---", "---", "CPY $%02x", "CMP $%02x", "DEC $%02x", "---", "INY ", "CMP #$%02x", "DEX ", "---", "CPY $%04x", "CMP $%04x", "DEC $%04x", "---", "BNE %hhd", "CMP ($%02x),Y", "---", "---", "---", "CMP $%02x,X", "DEC $%02x,X", "---", "CLD ", "CMP $%04x,Y", "---", "---", "---", "CMP $%04x,X", "DEC $%04x,X", "---", "CPX #$%02x", "SBC ($%02x,X)", "---", "---", "CPX $%02x", "SBC $%02x", "INC $%02x", "---", "INX ", "SBC #$%02x", "NOP ", "---", "CPX $%04x", "SBC $%04x", "INC $%04x", "---", "BEQ %hhd", "SBC ($%02x),Y", "---", "---", "---", "SBC $%02x,X", "INC $%02x,X", "---", "SED ", "SBC $%04x,Y", "---", "---", "---", "SBC $%04x,X", "INC $%04x,X", "---"}; const char *getBase(const uint16_t pos) { opcode_size=1; return base_opcodes[mem::read(pos)]; } const char *getAsm(const uint16_t pos) { opcode_size = 0; char base[256]; strcpy(buffer, getBase(pos)); if (strstr(buffer, "4x")) { opcode_size+=2; //const uint8_t memvalue = mem::read(pos); const uint16_t word = mem::read(pos+1) + (mem::read(pos+2)<<8); if (symbols::get(word)[0]!=0) { char *p = strstr(buffer, "$"); (*p)='%'; p++; (*p)='s'; p++; while (*(p-1) != 0) { *p = *(p+3); p++; } strcpy(base, buffer); sprintf(buffer, base, symbols::get(word)); } else { strcpy(base, buffer); sprintf(buffer, base, word); } } if (strstr(buffer, "2x")) { opcode_size+=1; strcpy(base, buffer); sprintf(buffer, base, mem::read(pos+1)); } if (strstr(buffer, "hhd")) { opcode_size+=1; strcpy(base, buffer); sprintf(buffer, base, (int8_t)mem::read(pos+1)); } return buffer; } const char *getOpcode(const uint16_t pos) { opcode_size = getOpcodeSize(pos); buffer[0]=0; char hex[4]; for (int i=0; i<4;++i) { if (opcode_size>i) sprintf(hex, "%02x ", mem::read(pos+i)); else sprintf(hex, " "); strcat(buffer, hex); } return buffer; } const int getOpcodeSize(const uint16_t pos) { return m6502::opcodes::addressingModes[m6502::opcodes::opcodeDescriptors[mem::read(pos)].addrMode].length; } namespace symbols { char symbols[65536][15]; std::vector used_symbols; void load() { used_symbols.clear(); for (int i=0; i<65536; ++i) symbols[i][0] = 0; FILE *f = fopen("symbols.txt", "r"); if (f==NULL) return; while (true) { uint16_t address; char tmp[15]; const int result = fscanf(f, "%hx %s", &address, tmp); if (result != 2) break; strcpy(symbols[address], tmp); used_symbols.push_back(address); } fclose(f); } void save() { FILE *f = fopen("symbols.txt", "w"); for (auto sym_addr : used_symbols) { fprintf(f, "0x%x %s\n", sym_addr, symbols[sym_addr]); } fclose(f); } const char *get(const uint16_t pos) { return symbols[pos]; } void set(const uint16_t pos, const char *sym) { if (sym) { strcpy(symbols[pos], sym); } else { symbols[pos][0] = 0; } used_symbols.clear(); for (int i=0; i<65536; ++i) if (symbols[i][0]!=0) used_symbols.push_back(i); save(); } const int count() { return used_symbols.size(); } const uint16_t address(const int pos) { return used_symbols[pos]; } } }