Skip to content

Commit

Permalink
Merge pull request #83 from matheusgomes28/add-bit-tests
Browse files Browse the repository at this point in the history
Fix for BIT and unit tests
  • Loading branch information
matheusgomes28 authored Oct 21, 2024
2 parents cb1dbdc + 497386d commit c8ce648
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 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 | :ok: |
| BIT | Absolute | 0x2c | 3 | 4 | :ok: |
| BIT | Zeropage | 0x24 | 2 | 3 | :white_check_mark: |
| BIT | Absolute | 0x2c | 3 | 4 | :white_check_mark: |
| NOP | Implied | 0xEA | 1 | 2 | :white_check_mark: |

total number of random instructions: `2`.
Expand Down
6 changes: 3 additions & 3 deletions emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ std::optional<InstructionConfig> nop(emulator::Cpu& cpu, std::span<const std::ui

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;
cpu.flags.n = static_cast<bool>(value & 0b1000'0000);
cpu.flags.v = static_cast<bool>(value & 0b0100'0000);
cpu.flags.z = !static_cast<bool>(cpu.reg.a & value);
}

std::optional<InstructionConfig> bit_zp(emulator::Cpu& cpu, std::span<const std::uint8_t> program)
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ create_tests(and_indirect_indexed_tests)
create_tests(and_zeropage_indexed_tests)
create_tests(and_zeropage_tests)
create_tests(asl_tests)
create_tests(bit_tests)
create_tests(branch_tests)
create_tests(cmp_tests)
create_tests(cpx_tests)
Expand Down
114 changes: 114 additions & 0 deletions tests/bit_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import emulator;

#include "common.h"

#include <gtest/gtest.h>

#include <array>
#include <cstdint>

// NOLINTNEXTLINE
TEST(BITTests, ZeropageNegativeFlag)
{
constexpr std::array<std::uint8_t, 2> program{0x24, 0xff};

emulator::Cpu cpu;
cpu.reg.a = 0b1111'1111;
cpu.mem[0xff] = 0b1000'0000;

EXPECT_EQ(emulator::execute(cpu, program), 3);
EXPECT_EQ(cpu.reg.a, 0b1111'1111);
EXPECT_EQ(cpu.reg.x, 0x00);
EXPECT_EQ(cpu.reg.y, 0x00);
EXPECT_EQ(cpu.reg.sp, 0xff);
EXPECT_EQ(cpu.reg.pc, 0x02);
EXPECT_EQ(cpu.flags, make_flags(0b1000'0000));
}

// NOLINTNEXTLINE
TEST(BITTests, ZeropageOverflowFlag)
{
constexpr std::array<std::uint8_t, 2> program{0x24, 0xff};

emulator::Cpu cpu;
cpu.reg.a = 0b1111'1111;
cpu.mem[0xff] = 0b0100'0000;

EXPECT_EQ(emulator::execute(cpu, program), 3);
EXPECT_EQ(cpu.reg.a, 0b1111'1111);
EXPECT_EQ(cpu.reg.x, 0x00);
EXPECT_EQ(cpu.reg.y, 0x00);
EXPECT_EQ(cpu.reg.sp, 0xff);
EXPECT_EQ(cpu.reg.pc, 0x02);
EXPECT_EQ(cpu.flags, make_flags(0b0100'0000));
}

// NOLINTNEXTLINE
TEST(BITTests, ZeropageZeroFlag)
{
constexpr std::array<std::uint8_t, 2> program{0x24, 0xff};

emulator::Cpu cpu;
cpu.mem[0xff] = 0b0011'1111;

EXPECT_EQ(emulator::execute(cpu, program), 3);
EXPECT_EQ(cpu.reg.a, 0x00);
EXPECT_EQ(cpu.reg.x, 0x00);
EXPECT_EQ(cpu.reg.y, 0x00);
EXPECT_EQ(cpu.reg.sp, 0xff);
EXPECT_EQ(cpu.reg.pc, 0x02);
EXPECT_EQ(cpu.flags, make_flags(0b0000'0010));
}

// NOLINTNEXTLINE
TEST(BITTests, AbsoluteNegativeFlag)
{
constexpr std::array<std::uint8_t, 3> program{0x2c, 0xfe, 0xff};

emulator::Cpu cpu;
cpu.reg.a = 0b1111'1111;
cpu.mem[0xfffe] = 0b1000'0000;

EXPECT_EQ(emulator::execute(cpu, program), 4);
EXPECT_EQ(cpu.reg.a, 0b1111'1111);
EXPECT_EQ(cpu.reg.x, 0x00);
EXPECT_EQ(cpu.reg.y, 0x00);
EXPECT_EQ(cpu.reg.sp, 0xff);
EXPECT_EQ(cpu.reg.pc, 0x03);
EXPECT_EQ(cpu.flags, make_flags(0b1000'0000));
}

// NOLINTNEXTLINE
TEST(BITTests, AbsoluteOverflowFlag)
{
constexpr std::array<std::uint8_t, 3> program{0x2c, 0xfe, 0xff};

emulator::Cpu cpu;
cpu.reg.a = 0b1111'1111;
cpu.mem[0xfffe] = 0b0100'0000;

EXPECT_EQ(emulator::execute(cpu, program), 4);
EXPECT_EQ(cpu.reg.a, 0b1111'1111);
EXPECT_EQ(cpu.reg.x, 0x00);
EXPECT_EQ(cpu.reg.y, 0x00);
EXPECT_EQ(cpu.reg.sp, 0xff);
EXPECT_EQ(cpu.reg.pc, 0x03);
EXPECT_EQ(cpu.flags, make_flags(0b0100'0000));
}

// NOLINTNEXTLINE
TEST(BITTests, AbsoluteZeroFlag)
{
constexpr std::array<std::uint8_t, 3> program{0x2c, 0xfe, 0xff};

emulator::Cpu cpu;
cpu.mem[0xfffe] = 0b0011'1111;

EXPECT_EQ(emulator::execute(cpu, program), 4);
EXPECT_EQ(cpu.reg.a, 0x00);
EXPECT_EQ(cpu.reg.x, 0x00);
EXPECT_EQ(cpu.reg.y, 0x00);
EXPECT_EQ(cpu.reg.sp, 0xff);
EXPECT_EQ(cpu.reg.pc, 0x03);
EXPECT_EQ(cpu.flags, make_flags(0b0000'0010));
}

0 comments on commit c8ce648

Please sign in to comment.