Skip to content

Support for DEC zp, zp+x, abs, abs+x #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ Total number of stack instructions: `4`.
| **Opcode** | **Addressing Mode** | **Opcode (Hex)** | **Bytes** | **Cycles** | **Supported** |
|------------|---------------------|------------------|-----------|------------|--------------------|
| DEC | Zero Page | 0xC6 | 2 | 5 | :white_check_mark: |
| DEC | Zero Page, X | 0xD6 | 2 | 6 | :x: |
| DEC | Absolute | 0xCE | 3 | 6 | :x: |
| DEC | Absolute, X | 0xDE | 3 | 7 | :x: |
| DEC | Zero Page, X | 0xD6 | 2 | 6 | :ok: |
| DEC | Absolute | 0xCE | 3 | 6 | :ok: |
| DEC | Absolute, X | 0xDE | 3 | 7 | :ok: |
| DEX | Implied | 0xCA | 1 | 2 | :white_check_mark: |
| DEY | Implied | 0x88 | 1 | 2 | :white_check_mark: |
| INC | Zero Page | 0xE6 | 2 | 5 | :white_check_mark: |
Expand Down
61 changes: 57 additions & 4 deletions emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,14 @@ std::optional<InstructionConfig> inc_absolute_plus_x(emulator::Cpu& cpu, std::sp
return std::make_optional<InstructionConfig>(3, 7);
}

/* Decrement operations */
void decrement_operation(emulator::Cpu& cpu, std::uint16_t pos)
{
cpu.mem[pos]--;
cpu.flags.z = cpu.mem[pos] == 0;
cpu.flags.n = cpu.mem[pos] & 0b1000'0000;
}

std::optional<InstructionConfig> dec_zeropage(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
{
ENABLE_PROFILER(cpu);
Expand All @@ -872,13 +880,53 @@ std::optional<InstructionConfig> dec_zeropage(emulator::Cpu& cpu, std::span<cons
}

std::uint8_t const pos = program[cpu.reg.pc + 1];
cpu.mem[pos]--;
cpu.flags.z = cpu.mem[pos] == 0;
cpu.flags.n = cpu.mem[pos] & 0b1000'0000;
decrement_operation(cpu, pos);
return std::make_optional<InstructionConfig>(2, 5);
}

return std::make_optional<InstructionConfig>(2);
std::optional<InstructionConfig> dec_zp_indexed(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
{
ENABLE_PROFILER(cpu);
if ((cpu.reg.pc + 1) >= program.size())
{
return std::nullopt;
}

auto const pos = zeropage_indexed(cpu, program[cpu.reg.pc + 1], &emulator::Registers::x);
decrement_operation(cpu, pos);
return std::make_optional<InstructionConfig>(2, 5);
}

std::optional<InstructionConfig> dec_abs(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
{
ENABLE_PROFILER(cpu);
if ((cpu.reg.pc + 2) >= program.size())
{
return std::nullopt;
}

// (hsb << 8) + lsb convert little endian to the address
auto const lsb = program[cpu.reg.pc + 1];
auto const hsb = program[cpu.reg.pc + 2];
decrement_operation(cpu, (hsb << 8) | lsb);
return std::make_optional<InstructionConfig>(3, 6);
}

std::optional<InstructionConfig> dec_abs_indexed(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
{
ENABLE_PROFILER(cpu);
if ((cpu.reg.pc + 2) >= program.size())
{
return std::nullopt;
}

// (hsb << 8) + lsb convert little endian to the address
auto const pos = absolute_indexed(cpu, program[cpu.reg.pc + 1], program[cpu.reg.pc + 2], &emulator::Registers::x);
decrement_operation(cpu, pos);
return std::make_optional<InstructionConfig>(3, 7);
}
/* End Decrement operations */

Instruction inc_reg(std::uint8_t emulator::Registers::*reg)
{
return [=](emulator::Cpu& cpu, std::span<const std::uint8_t> /* program */)
Expand Down Expand Up @@ -1525,7 +1573,12 @@ std::array<Instruction, 256> get_instructions()
supported_instructions[0xc8] = inc_reg(&emulator::Registers::y);
supported_instructions[0xe8] = inc_reg(&emulator::Registers::x);

// DEC opcodes
supported_instructions[0xc6] = dec_zeropage;
supported_instructions[0xd6] = dec_zp_indexed;
supported_instructions[0xce] = dec_abs;
supported_instructions[0xde] = dec_abs_indexed;

supported_instructions[0x88] = dec_reg(&emulator::Registers::y);
supported_instructions[0xca] = dec_reg(&emulator::Registers::x);

Expand Down