Adding the AND instruction

This commit is contained in:
AnonymusRaccoon
2020-02-20 18:32:21 +01:00
parent 52b5a6ba23
commit 91150dfbc1
5 changed files with 108 additions and 3 deletions
+2 -2
View File
@@ -63,7 +63,7 @@ add_executable(unit_tests
tests/CPU/testInternal.cpp
sources/Ram/ExtendedRam.cpp
sources/Ram/ExtendedRam.hpp
sources/Utility/Utility.hpp sources/Utility/Utility.cpp)
sources/Utility/Utility.hpp sources/Utility/Utility.cpp sources/CPU/Instructions/BitsInstructions.cpp tests/CPU/testBits.cpp)
# include criterion & coverage
target_link_libraries(unit_tests criterion -lgcov)
@@ -136,7 +136,7 @@ add_executable(ComSquare
sources/Utility/Utility.hpp
sources/Debugger/MemoryViewer.cpp
sources/Debugger/MemoryViewer.hpp
sources/Utility/Utility.cpp sources/Debugger/HeaderViewer.cpp sources/Debugger/HeaderViewer.hpp)
sources/Utility/Utility.cpp sources/Debugger/HeaderViewer.cpp sources/Debugger/HeaderViewer.hpp sources/CPU/Instructions/BitsInstructions.cpp)
target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED)
+16
View File
@@ -306,6 +306,22 @@ namespace ComSquare::CPU
case Instructions::CLD: this->CLD(); return 2;
case Instructions::CLV: this->CLV(); return 2;
case Instructions::AND_IM: this->AND(this->_getImmediateAddr()); return 2 + !this->_registers.p.m;
case Instructions::AND_ABS: this->AND(this->_getAbsoluteAddr()); return 4 + !this->_registers.p.m;
case Instructions::AND_ABSl: this->AND(this->_getAbsoluteLongAddr()); return 5 + !this->_registers.p.m;
case Instructions::AND_DP: this->AND(this->_getDirectAddr()); return 3 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::AND_DPi: this->AND(this->_getDirectIndirectAddr()); return 5 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::AND_DPil: this->AND(this->_getDirectIndirectLongAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::AND_ABSX: this->AND(this->_getAbsoluteIndexedByXAddr()); return 4 + !this->_registers.p.m + this->_hasIndexCrossedPageBoundary;
case Instructions::AND_ABSXl:this->AND(this->_getAbsoluteIndexedByXLongAddr()); return 5 + !this->_registers.p.m;
case Instructions::AND_ABSY: this->AND(this->_getAbsoluteIndexedByYAddr()); return 4 + !this->_registers.p.m + this->_hasIndexCrossedPageBoundary;
case Instructions::AND_DPX: this->AND(this->_getDirectIndexedByXAddr()); return 4 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::AND_DPXi: this->AND(this->_getDirectIndirectIndexedXAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::AND_DPYi: this->AND(this->_getDirectIndirectIndexedYAddr()); return 5 + !this->_registers.p.m + this->_registers.dl != 0 + this->_hasIndexCrossedPageBoundary;
case Instructions::AND_DPYil:this->AND(this->_getDirectIndirectIndexedYLongAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::AND_SR: this->AND(this->_getStackRelativeAddr()); return 4 + !this->_registers.p.m;
case Instructions::AND_SRYi: this->AND(this->_getStackRelativeIndirectIndexedYAddr()); return 7 + !this->_registers.p.m;
default:
throw InvalidOpcode("CPU", opcode);
}
+19 -1
View File
@@ -288,7 +288,23 @@ namespace ComSquare::CPU
CLC = 0x18,
CLI = 0x58,
CLD = 0xD8,
CLV = 0xB8
CLV = 0xB8,
AND_IM = 0x29,
AND_ABS = 0x2D,
AND_ABSl = 0x2F,
AND_DP = 0x25,
AND_DPi = 0x32,
AND_DPil = 0x27,
AND_ABSX = 0x3D,
AND_ABSXl = 0x3F,
AND_ABSY = 0x39,
AND_DPX = 0x35,
AND_DPXi = 0x21,
AND_DPYi = 0x31,
AND_DPYil = 0x37,
AND_SR = 0x23,
AND_SRYi = 0x33
};
//! @brief The main CPU
@@ -429,6 +445,8 @@ namespace ComSquare::CPU
void CLD();
//! @brief Clear the overflow flag.
void CLV();
//! @brief And accumulator with memory.
void AND(uint24_t valueAddr);
public:
explicit CPU(std::shared_ptr<Memory::MemoryBus> bus, Cartridge::Header &cartridgeHeader);
CPU(const CPU &) = default;
@@ -0,0 +1,21 @@
//
// Created by anonymus-raccoon on 2/20/20.
//
#include "../../Models/Int24.hpp"
#include "../CPU.hpp"
namespace ComSquare::CPU
{
void CPU::AND(uint24_t valueAddr)
{
unsigned negativeMask = this->_isEmulationMode ? 0x80u : 0x8000u;
unsigned value = this->_bus->read(valueAddr);
if (this->_registers.p.m)
value += this->_bus->read(valueAddr + 1) << 8u;
this->_registers.a &= value;
this->_registers.p.n = this->_registers.a & negativeMask;
this->_registers.p.z = this->_registers.a == 0;
}
}
+50
View File
@@ -0,0 +1,50 @@
//
// Created by anonymus-raccoon on 2/20/20.
//
#include <criterion/criterion.h>
#include <bitset>
#include <iostream>
#include "../tests.hpp"
#include "../../sources/SNES.hpp"
using namespace ComSquare;
Test(AND, emulation)
{
auto pair = Init();
pair.second.wram->_data[0] = 0x00;
pair.second.cpu->_registers.a = 0xFF;
pair.second.cpu->_isEmulationMode = true;
pair.second.cpu->AND(0x0);
cr_assert_eq(pair.second.cpu->_registers.a, 0x00, "The flags should be 0x00 but it was %x", pair.second.cpu->_registers.a);
cr_assert_eq(pair.second.cpu->_registers.p.z, true, "The zero flag should be set.");
cr_assert_eq(pair.second.cpu->_registers.p.n, false, "The negative flag should not be set.");
}
Test(AND, nativeNegative)
{
auto pair = Init();
pair.second.wram->_data[0] = 0xF0;
pair.second.wram->_data[1] = 0xF0;
pair.second.cpu->_registers.p.m = true;
pair.second.cpu->_registers.a = 0xFF00;
pair.second.cpu->_isEmulationMode = false;
pair.second.cpu->AND(0x0);
cr_assert_eq(pair.second.cpu->_registers.a, 0xF000, "The flags should be 0xF000 but it was %x", pair.second.cpu->_registers.a);
cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flag should not be set.");
cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flag should be set.");
}
Test(AND, emulationTest)
{
auto pair = Init();
pair.second.wram->_data[0] = 0b00110011;
pair.second.cpu->_registers.a = 0b00110111;
pair.second.cpu->_isEmulationMode = true;
pair.second.cpu->AND(0x0);
cr_assert_eq(pair.second.cpu->_registers.a, 0b00110011, "The flags should be 0b00110011 but it was %x", pair.second.cpu->_registers.a);
cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flag should not be set.");
cr_assert_eq(pair.second.cpu->_registers.p.n, false, "The negative flag should not be set.");
}