1
0
mirror of https://github.com/lefticus/6502-cpp.git synced 2025-01-02 12:30:58 +00:00

Use Y over A when possible

This commit is contained in:
Jason Turner 2016-07-07 11:20:41 -06:00
parent d2e2f311c5
commit 04e0a8dbc9

View File

@ -56,8 +56,10 @@ struct mos6502 : ASMLine
{ {
unknown, unknown,
lda, lda,
ldy,
eor, eor,
sta, sta,
sty,
pha, pha,
pla, pla,
lsr, lsr,
@ -76,8 +78,10 @@ struct mos6502 : ASMLine
case OpCode::bne: case OpCode::bne:
return true; return true;
case OpCode::lda: case OpCode::lda:
case OpCode::ldy:
case OpCode::eor: case OpCode::eor:
case OpCode::sta: case OpCode::sta:
case OpCode::sty:
case OpCode::pha: case OpCode::pha:
case OpCode::pla: case OpCode::pla:
case OpCode::lsr: case OpCode::lsr:
@ -97,8 +101,10 @@ struct mos6502 : ASMLine
case OpCode::cmp: case OpCode::cmp:
return true; return true;
case OpCode::lda: case OpCode::lda:
case OpCode::ldy:
case OpCode::eor: case OpCode::eor:
case OpCode::sta: case OpCode::sta:
case OpCode::sty:
case OpCode::pha: case OpCode::pha:
case OpCode::pla: case OpCode::pla:
case OpCode::lsr: case OpCode::lsr:
@ -135,10 +141,14 @@ struct mos6502 : ASMLine
switch (o) { switch (o) {
case OpCode::lda: case OpCode::lda:
return "lda"; return "lda";
case OpCode::ldy:
return "ldy";
case OpCode::eor: case OpCode::eor:
return "eor"; return "eor";
case OpCode::sta: case OpCode::sta:
return "sta"; return "sta";
case OpCode::sty:
return "sty";
case OpCode::pha: case OpCode::pha:
return "pha"; return "pha";
case OpCode::pla: case OpCode::pla:
@ -283,17 +293,13 @@ void translate_instruction(std::vector<mos6502> &instructions, const i386::OpCod
{ {
case i386::OpCode::movb: case i386::OpCode::movb:
if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) {
instructions.emplace_back(mos6502::OpCode::pha); // transfer memory through A register, pushing and popping around it instructions.emplace_back(mos6502::OpCode::ldy, Operand(o1.type, "#" + o1.value));
instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, "#" + o1.value)); instructions.emplace_back(mos6502::OpCode::sty, o2);
instructions.emplace_back(mos6502::OpCode::sta, o2);
instructions.emplace_back(mos6502::OpCode::pla);
} else if (o1.type == Operand::Type::reg && o1.reg_num == 1 && o2.type == Operand::Type::literal) { } else if (o1.type == Operand::Type::reg && o1.reg_num == 1 && o2.type == Operand::Type::literal) {
instructions.emplace_back(mos6502::OpCode::sta, o2); instructions.emplace_back(mos6502::OpCode::sta, o2);
} else if (o1.type == Operand::Type::reg && o1.reg_num == 4 && o2.type == Operand::Type::literal) { } else if (o1.type == Operand::Type::reg && o1.reg_num == 4 && o2.type == Operand::Type::literal) {
instructions.emplace_back(mos6502::OpCode::pha); // transfer memory through A register, pushing and popping around it instructions.emplace_back(mos6502::OpCode::ldy, Operand(Operand::Type::literal, "$00"));
instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "$00")); instructions.emplace_back(mos6502::OpCode::sty, o2);
instructions.emplace_back(mos6502::OpCode::sta, o2);
instructions.emplace_back(mos6502::OpCode::pla); // transfer memory through A register, pushing and popping around it
} else { } else {
throw std::runtime_error("Cannot translate instruction"); throw std::runtime_error("Cannot translate instruction");
} }
@ -309,10 +315,8 @@ void translate_instruction(std::vector<mos6502> &instructions, const i386::OpCod
if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg && o2.reg_num == 1) { if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg && o2.reg_num == 1) {
instructions.emplace_back(mos6502::OpCode::lda, o1); instructions.emplace_back(mos6502::OpCode::lda, o1);
} else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg && o2.reg_num == 4) { } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg && o2.reg_num == 4) {
instructions.emplace_back(mos6502::OpCode::pha); // transfer memory through A register, pushing and popping around it instructions.emplace_back(mos6502::OpCode::ldy, o1);
instructions.emplace_back(mos6502::OpCode::lda, o1); instructions.emplace_back(mos6502::OpCode::sty, Operand(Operand::Type::literal, "$00"));
instructions.emplace_back(mos6502::OpCode::sta, Operand(Operand::Type::literal, "$00"));
instructions.emplace_back(mos6502::OpCode::pla);
} else { } else {
throw std::runtime_error("Cannot translate instruction"); throw std::runtime_error("Cannot translate instruction");
} }