1
0
mirror of https://github.com/lefticus/6502-cpp.git synced 2024-06-09 20:29:28 +00:00

Fix handling of sbc/sub and enable parsing of -fverbose-asm

This commit is contained in:
Jason Turner 2021-05-09 17:31:44 -06:00
parent 9fe26641b7
commit eedec41b1e
2 changed files with 14 additions and 11 deletions

View File

@ -6,6 +6,8 @@
bool optimize(std::vector<mos6502> &instructions) bool optimize(std::vector<mos6502> &instructions)
{ {
// return false;
if (instructions.size() < 2) { if (instructions.size() < 2) {
return false; return false;
} }

View File

@ -385,11 +385,9 @@ void translate_instruction(const Personality &personality, std::vector<mos6502>
throw std::runtime_error("Unhandled 'std'"); throw std::runtime_error("Unhandled 'std'");
} }
case AVR::OpCode::sub: { case AVR::OpCode::sub: {
// we want to utilize the carry flag, however it was set previously instructions.emplace_back(mos6502::OpCode::sec);
// (it's really a borrow flag on the 6502)
instructions.emplace_back(mos6502::OpCode::clc);
instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num));
instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o2_reg_num));
instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num));
fixup_16_bit_N_Z_flags(instructions); fixup_16_bit_N_Z_flags(instructions);
return; return;
@ -398,7 +396,7 @@ void translate_instruction(const Personality &personality, std::vector<mos6502>
// we want to utilize the carry flag, however it was set previously // we want to utilize the carry flag, however it was set previously
// (it's really a borrow flag on the 6502) // (it's really a borrow flag on the 6502)
instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num));
instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o2_reg_num));
instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num));
fixup_16_bit_N_Z_flags(instructions); fixup_16_bit_N_Z_flags(instructions);
return; return;
@ -716,7 +714,9 @@ void to_mos6502(const Personality &personality, const AVR &from_instruction, std
if (!zeros.empty()) { if (!zeros.empty()) {
instructions.emplace_back(ASMLine::Type::Directive, zeros); instructions.emplace_back(ASMLine::Type::Directive, zeros);
} }
} else if (from_instruction.text[0] == ';') {
// it's a comment
instructions.emplace_back(ASMLine::Type::Directive, from_instruction.text);
} else { } else {
instructions.emplace_back(ASMLine::Type::Directive, "; Unknown directive: " + from_instruction.text); instructions.emplace_back(ASMLine::Type::Directive, "; Unknown directive: " + from_instruction.text);
} }
@ -826,12 +826,12 @@ bool fix_overwritten_flags(std::vector<mos6502> &instructions)
void run(const Personality &personality, std::istream &input) void run(const Personality &personality, std::istream &input)
{ {
std::regex Comment(R"(\s*\#.*)"); std::regex Comment(R"(\s*(\#|;)(.*))");
std::regex Label(R"(^\s*(\S+):.*)"); std::regex Label(R"(^\s*(\S+):.*)");
std::regex Directive(R"(^\s*(\..+))"); std::regex Directive(R"(^\s*(\..+))");
std::regex UnaryInstruction(R"(^\s+(\S+)\s+(\S+))"); std::regex UnaryInstruction(R"(^\s+(\S+)\s+(\S+).*)");
std::regex BinaryInstruction(R"(^\s+(\S+)\s+(\S+),\s*(\S+))"); std::regex BinaryInstruction(R"(^\s+(\S+)\s+(\S+),\s*(\S+).*)");
std::regex Instruction(R"(^\s+(\S+))"); std::regex Instruction(R"(^\s+(\S+).*)");
std::size_t lineno = 0; std::size_t lineno = 0;
@ -846,7 +846,8 @@ void run(const Personality &personality, std::istream &input)
if (std::regex_match(line, match, Label)) { if (std::regex_match(line, match, Label)) {
instructions.emplace_back(lineno, line, ASMLine::Type::Label, match[1].str()); instructions.emplace_back(lineno, line, ASMLine::Type::Label, match[1].str());
} else if (std::regex_match(line, match, Comment)) { } else if (std::regex_match(line, match, Comment)) {
// don't care about comments // save comments!
instructions.emplace_back(lineno, line, ASMLine::Type::Directive, "; " + match[2].str());
} else if (std::regex_match(line, match, Directive)) { } else if (std::regex_match(line, match, Directive)) {
instructions.emplace_back(lineno, line, ASMLine::Type::Directive, match[1].str()); instructions.emplace_back(lineno, line, ASMLine::Type::Directive, match[1].str());
} else if (std::regex_match(line, match, BinaryInstruction)) { } else if (std::regex_match(line, match, BinaryInstruction)) {