Skip to content

Commit

Permalink
Merge pull request #81 from matheusgomes28/support-bit-opcode
Browse files Browse the repository at this point in the history
Adding bit support with NO TESTS
  • Loading branch information
matheusgomes28 authored Oct 21, 2024
2 parents 8bf0318 + 51c2efd commit cb1dbdc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ total number of interrupt instructions: `2`.

| **Opcode** | **Addressing Mode** | **Opcode (Hex)** | **Bytes** | **Cycles** | **Supported** |
|------------|---------------------|------------------|-----------|------------|--------------------|
| BIT | Zeropage | 0x24 | 2 | 3 | :x: |
| BIT | Absolute | 0x2c | 3 | 4 | :x: |
| BIT | Zeropage | 0x24 | 2 | 3 | :ok: |
| BIT | Absolute | 0x2c | 3 | 4 | :ok: |
| NOP | Implied | 0xEA | 1 | 2 | :white_check_mark: |

total number of random instructions: `2`.
Expand Down
38 changes: 38 additions & 0 deletions emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,42 @@ std::optional<InstructionConfig> nop(emulator::Cpu& cpu, std::span<const std::ui
{
return std::make_optional<InstructionConfig>(1, 2);
}

void bit_operation(emulator::Cpu& cpu, std::uint8_t value)
{
cpu.flags.n = static_cast<bool>(value | 0b1000'0000);
cpu.flags.z = static_cast<bool>(value | 0b0100'0000);
cpu.flags.c = cpu.reg.a & value;
}

std::optional<InstructionConfig> bit_zp(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 zp = program[cpu.reg.pc + 1];
auto const value = cpu.mem[zp];
bit_operation(cpu, value);
return std::make_optional<InstructionConfig>(2, 3);
}

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

auto const lsb = program[cpu.reg.pc + 1];
auto const hsb = program[cpu.reg.pc + 2];
auto const pos = (hsb << 8) | lsb;
auto const value = cpu.mem[pos];
bit_operation(cpu, value);
return std::make_optional<InstructionConfig>(3, 4);
}
/* End functions with no context */

/* Stack Related Functions */
Expand Down Expand Up @@ -1749,6 +1785,8 @@ std::array<Instruction, 256> get_instructions()

// Opcodes with no context
supported_instructions[0xea] = nop;
supported_instructions[0x24] = bit_zp;
supported_instructions[0x2c] = bit_abs;

return supported_instructions;
}
Expand Down

0 comments on commit cb1dbdc

Please sign in to comment.