Skip to content

Commit

Permalink
Merge pull request #77 from matheusgomes28/nop-support
Browse files Browse the repository at this point in the history
NOP support and tests
  • Loading branch information
matheusgomes28 authored Oct 17, 2024
2 parents b81ba75 + 00bad58 commit 8bf0318
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,23 @@ Total number of `ROL` instructions: `5`.

Total number of `CMP` instructions: `8`.

### CPX - Compare Memory with Accumulator
### CPX - Compare Memory with X Register

| **Opcode** | **Addressing Mode** | **Opcode (Hex)** | **Bytes** | **Cycles** | **Supported** |
|------------|---------------------|------------------|-----------|------------|--------------------|
| CPX | Immediate | 0xE0 | 2 | 2 | :white_check_mark: |
| CPX | Zero Page | 0xE4 | 2 | 3 | :white_check_mark: |
| CPX | Absolute | 0xEC | 3 | 4 | :ok: |
| CPX | Absolute | 0xEC | 3 | 4 | :white_check_mark: |

Total number of `CPX` instructions: `3`.

### CPY - Compare Memory with Accumulator
### CPY - Compare Memory with Y Register

| **Opcode** | **Addressing Mode** | **Opcode (Hex)** | **Bytes** | **Cycles** | **Supported** |
|------------|---------------------|------------------|-----------|------------|--------------------|
| CPY | Immediate | 0xC0 | 2 | 2 | :white_check_mark: |
| CPY | Zero Page | 0xC4 | 2 | 3 | :white_check_mark: |
| CPY | Absolute | 0xCC | 3 | 4 | :ok: |
| CPY | Absolute | 0xCC | 3 | 4 | :white_check_mark: |

Total number of `CPY` instructions: `3`.

Expand Down Expand Up @@ -386,7 +386,7 @@ total number of interrupt instructions: `2`.
|------------|---------------------|------------------|-----------|------------|--------------------|
| BIT | Zeropage | 0x24 | 2 | 3 | :x: |
| BIT | Absolute | 0x2c | 3 | 4 | :x: |
| NOP | Implied | 0xEA | 1 | 2 | :x: |
| NOP | Implied | 0xEA | 1 | 2 | :white_check_mark: |

total number of random instructions: `2`.

Expand Down
11 changes: 10 additions & 1 deletion emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ inline std::uint16_t indirect_indexed(emulator::Cpu& cpu, std::uint8_t val)
return addr;
}

/* Functions with no context */
std::optional<InstructionConfig> nop(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
{
return std::make_optional<InstructionConfig>(1, 2);
}
/* End functions with no context */

/* Stack Related Functions */
std::optional<InstructionConfig> push_accumulator_to_stack(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
{
Expand Down Expand Up @@ -1097,7 +1104,6 @@ void cmp_operation(emulator::Cpu& cpu, std::uint8_t emulator::Registers::*reg, s
cpu.flags.c = (cpu.reg).*reg >= val;
}


/// Compares whichever register was given to the immediate
/// value in the next address in the program array
Instruction cmp_immediate_reg(std::uint8_t emulator::Registers::*reg)
Expand Down Expand Up @@ -1741,6 +1747,9 @@ std::array<Instruction, 256> get_instructions()
supported_instructions[0xb8] = clear_flag(&emulator::Flags::v);
supported_instructions[0xd8] = clear_flag(&emulator::Flags::d);

// Opcodes with no context
supported_instructions[0xea] = nop;

return supported_instructions;
}

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ create_tests(ld_index_indirect_tests)
create_tests(ld_indirect_indexed_tests)
create_tests(ld_zeropage_tests)
create_tests(lsr_tests)
create_tests(nop_tests)
create_tests(ora_absolute_indexed_tests)
create_tests(ora_absolute_tests)
create_tests(ora_immediate_tests)
Expand Down
42 changes: 42 additions & 0 deletions tests/nop_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import emulator;

#include "common.h"

#include <gtest/gtest.h>

#include <array>
#include <cstdint>

// NOLINTNEXTLINE
TEST(NOPTests, VeryBoringTests)
{
constexpr std::array<std::uint8_t, 1> program{0xea};

emulator::Cpu cpu;
ASSERT_EQ(emulator::execute(cpu, program), 2);

ASSERT_EQ(cpu.reg.a, 0x00);
ASSERT_EQ(cpu.reg.x, 0x00);
ASSERT_EQ(cpu.reg.y, 0x00);
ASSERT_EQ(cpu.reg.sp, 0xff);
ASSERT_EQ(cpu.reg.pc, 0x01);

ASSERT_EQ(cpu.flags, make_flags(0b0000'0000));
}

// NOLINTNEXTLINE
TEST(NOPTests, AnotherVeryBoringTests)
{
constexpr std::array<std::uint8_t, 3> program{0xea, 0xea, 0xea};

emulator::Cpu cpu;
ASSERT_EQ(emulator::execute(cpu, program), 6);

ASSERT_EQ(cpu.reg.a, 0x00);
ASSERT_EQ(cpu.reg.x, 0x00);
ASSERT_EQ(cpu.reg.y, 0x00);
ASSERT_EQ(cpu.reg.sp, 0xff);
ASSERT_EQ(cpu.reg.pc, 0x03);

ASSERT_EQ(cpu.flags, make_flags(0b0000'0000));
}

0 comments on commit 8bf0318

Please sign in to comment.