mirror of
https://github.com/zoriya/ComSquare.git
synced 2025-12-20 14:15:11 +00:00
Implementing a SBC without decimal mode
This commit is contained in:
@@ -77,7 +77,7 @@ add_executable(unit_tests
|
||||
sources/APU/Instructions/Stack.cpp
|
||||
sources/APU/Instructions/Subroutine.cpp
|
||||
sources/APU/Instructions/ProgramFlow.cpp
|
||||
)
|
||||
tests/CPU/Math/testSBC.cpp)
|
||||
|
||||
# include criterion & coverage
|
||||
target_link_libraries(unit_tests criterion -lgcov)
|
||||
|
||||
@@ -328,6 +328,22 @@ namespace ComSquare::CPU
|
||||
|
||||
case Instructions::XCE: this->XCE(); return 2;
|
||||
|
||||
case Instructions::SBC_IM: this->SBC(this->_getImmediateAddr()); return 2 + !this->_registers.p.m;
|
||||
case Instructions::SBC_ABS: this->SBC(this->_getAbsoluteAddr()); return 4 + !this->_registers.p.m;
|
||||
case Instructions::SBC_ABSl: this->SBC(this->_getAbsoluteLongAddr()); return 5 + !this->_registers.p.m;
|
||||
case Instructions::SBC_DP: this->SBC(this->_getDirectAddr()); return 3 + !this->_registers.p.m + this->_registers.dl != 0;
|
||||
case Instructions::SBC_DPi: this->SBC(this->_getDirectIndirectAddr()); return 5 + !this->_registers.p.m + this->_registers.dl != 0;
|
||||
case Instructions::SBC_DPil: this->SBC(this->_getDirectIndirectLongAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
|
||||
case Instructions::SBC_ABSX: this->SBC(this->_getAbsoluteIndexedByXAddr()); return 4 + !this->_registers.p.m + this->_hasIndexCrossedPageBoundary;
|
||||
case Instructions::SBC_ABSXl:this->SBC(this->_getAbsoluteIndexedByXLongAddr()); return 5 + !this->_registers.p.m;
|
||||
case Instructions::SBC_ABSY: this->SBC(this->_getAbsoluteIndexedByYAddr()); return 4 + !this->_registers.p.m + this->_hasIndexCrossedPageBoundary;
|
||||
case Instructions::SBC_DPX: this->SBC(this->_getDirectIndexedByXAddr()); return 4 + !this->_registers.p.m + this->_registers.dl != 0;
|
||||
case Instructions::SBC_DPXi: this->SBC(this->_getDirectIndirectIndexedXAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
|
||||
case Instructions::SBC_DPYi: this->SBC(this->_getDirectIndirectIndexedYAddr()); return 5 + !this->_registers.p.m + this->_registers.dl != 0 + this->_hasIndexCrossedPageBoundary;
|
||||
case Instructions::SBC_DPYil:this->SBC(this->_getDirectIndirectIndexedYLongAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
|
||||
case Instructions::SBC_SR: this->SBC(this->_getStackRelativeAddr()); return 4 + !this->_registers.p.m;
|
||||
case Instructions::SBC_SRYi: this->SBC(this->_getStackRelativeIndirectIndexedYAddr()); return 7 + !this->_registers.p.m;
|
||||
|
||||
default:
|
||||
throw InvalidOpcode("CPU", opcode);
|
||||
}
|
||||
|
||||
@@ -310,7 +310,23 @@ namespace ComSquare::CPU
|
||||
AND_SR = 0x23,
|
||||
AND_SRYi = 0x33,
|
||||
|
||||
XCE = 0xFB
|
||||
XCE = 0xFB,
|
||||
|
||||
SBC_IM = 0xE9,
|
||||
SBC_ABS = 0xED,
|
||||
SBC_ABSl = 0xEF,
|
||||
SBC_DP = 0xE5,
|
||||
SBC_DPi = 0xF2,
|
||||
SBC_DPil = 0xE7,
|
||||
SBC_ABSX = 0xFD,
|
||||
SBC_ABSXl = 0xFF,
|
||||
SBC_ABSY = 0xF9,
|
||||
SBC_DPX = 0xF5,
|
||||
SBC_DPXi = 0xE1,
|
||||
SBC_DPYi = 0xF1,
|
||||
SBC_DPYil = 0xF7,
|
||||
SBC_SR = 0xE3,
|
||||
SBC_SRYi = 0xF3,
|
||||
};
|
||||
|
||||
//! @brief The main CPU
|
||||
|
||||
@@ -29,6 +29,21 @@ namespace ComSquare::CPU
|
||||
|
||||
void CPU::SBC(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;
|
||||
bool oldCarry = this->_registers.p.c;
|
||||
|
||||
this->_registers.p.c = this->_registers.a >= value;
|
||||
if ((this->_registers.a & negativeMask) == (value & negativeMask))
|
||||
this->_registers.p.v = (this->_registers.a & negativeMask) != ((this->_registers.a + value) & negativeMask);
|
||||
else
|
||||
this->_registers.p.v = false;
|
||||
this->_registers.a += ~value + oldCarry;
|
||||
if (this->_isEmulationMode)
|
||||
this->_registers.a %= 0x100;
|
||||
this->_registers.p.z = this->_registers.a == 0;
|
||||
this->_registers.p.n = this->_registers.a & negativeMask;
|
||||
}
|
||||
}
|
||||
@@ -242,6 +242,22 @@ namespace ComSquare::Debugger
|
||||
|
||||
case Instructions::XCE: return "XCE";
|
||||
|
||||
case Instructions::SBC_IM: return "SBC";
|
||||
case Instructions::SBC_ABS: return "SBC";
|
||||
case Instructions::SBC_ABSl: return "SBC";
|
||||
case Instructions::SBC_DP: return "SBC";
|
||||
case Instructions::SBC_DPi: return "SBC";
|
||||
case Instructions::SBC_DPil: return "SBC";
|
||||
case Instructions::SBC_ABSX: return "SBC";
|
||||
case Instructions::SBC_ABSXl:return "SBC";
|
||||
case Instructions::SBC_ABSY: return "SBC";
|
||||
case Instructions::SBC_DPX: return "SBC";
|
||||
case Instructions::SBC_DPXi: return "SBC";
|
||||
case Instructions::SBC_DPYi: return "SBC";
|
||||
case Instructions::SBC_DPYil:return "SBC";
|
||||
case Instructions::SBC_SR: return "SBC";
|
||||
case Instructions::SBC_SRYi: return "SBC";
|
||||
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,9 @@
|
||||
//
|
||||
|
||||
#include <criterion/criterion.h>
|
||||
#include <iostream>
|
||||
#include <bitset>
|
||||
#include "../../tests.hpp"
|
||||
#include "../../../sources/SNES.hpp"
|
||||
#include "../../../sources/Memory/MemoryBus.hpp"
|
||||
using namespace ComSquare;
|
||||
|
||||
Test(ADC, addingOne)
|
||||
|
||||
92
tests/CPU/Math/testSBC.cpp
Normal file
92
tests/CPU/Math/testSBC.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by anonymus-raccoon on 27/02/20.
|
||||
//
|
||||
|
||||
#include <criterion/criterion.h>
|
||||
#include <bitset>
|
||||
#include "../../tests.hpp"
|
||||
#include "../../../sources/SNES.hpp"
|
||||
using namespace ComSquare;
|
||||
|
||||
Test(SBC, removingOne)
|
||||
{
|
||||
auto pair = Init();
|
||||
pair.second.cpu->_isEmulationMode = false;
|
||||
pair.second.cpu->_registers.p.c = true;
|
||||
pair.second.cpu->_registers.a = 0x1;
|
||||
pair.second.wram->_data[0] = 0x1;
|
||||
pair.second.cpu->SBC(0x0);
|
||||
cr_assert_eq(pair.second.cpu->_registers.a, 0, "The accumulator's value should be 0x0 but it was 0x%x.", pair.second.cpu->_registers.a);
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.c, true, "The carry flags should be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.n, false, "The negative flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.z, true, "The zero flags should be set.");
|
||||
}
|
||||
|
||||
Test(SBC, legitOverflowWithCarry)
|
||||
{
|
||||
auto pair = Init();
|
||||
pair.second.cpu->_isEmulationMode = false;
|
||||
pair.second.cpu->_registers.a = 0x1;
|
||||
pair.second.cpu->_registers.p.m = true;
|
||||
pair.second.cpu->_registers.p.c = true;
|
||||
pair.second.wram->_data[0] = 0x03;
|
||||
pair.second.wram->_data[1] = 0x20;
|
||||
pair.second.cpu->SBC(0x0);
|
||||
cr_assert_eq(pair.second.cpu->_registers.a, 0xDFFE, "The accumulator's value should be 0xDFFE but it was 0x%x.", pair.second.cpu->_registers.a);
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flags should be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should not be set.");
|
||||
}
|
||||
|
||||
Test(SBC, overflowWithCarry)
|
||||
{
|
||||
auto pair = Init();
|
||||
pair.second.cpu->_isEmulationMode = false;
|
||||
pair.second.cpu->_registers.a = 0x1;
|
||||
pair.second.cpu->_registers.p.m = true;
|
||||
pair.second.cpu->_registers.p.c = true;
|
||||
pair.second.wram->_data[0] = 0x03;
|
||||
pair.second.wram->_data[1] = 0x20;
|
||||
pair.second.cpu->SBC(0x0);
|
||||
cr_assert_eq(pair.second.cpu->_registers.a, 0xDFFE, "The accumulator's value should be 0xDFFE but it was 0x%x.", pair.second.cpu->_registers.a);
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should be not set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flags should be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should not be set.");
|
||||
}
|
||||
|
||||
Test(SBC, overflowEmulation)
|
||||
{
|
||||
auto pair = Init();
|
||||
pair.second.cpu->_isEmulationMode = true;
|
||||
pair.second.cpu->_registers.a = 0x1;
|
||||
pair.second.cpu->_registers.p.m = false;
|
||||
pair.second.cpu->_registers.p.c = false;
|
||||
pair.second.wram->_data[0] = 0x02;
|
||||
pair.second.cpu->SBC(0x0);
|
||||
cr_assert_eq(pair.second.cpu->_registers.a, 0xFE, "The accumulator's value should be 0xFE but it was 0x%x.", pair.second.cpu->_registers.a);
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flags should be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should be not set.");
|
||||
}
|
||||
|
||||
|
||||
Test(SBC, decimal)
|
||||
{
|
||||
auto pair = Init();
|
||||
pair.second.cpu->_isEmulationMode = true;
|
||||
pair.second.cpu->_registers.a = 0x1;
|
||||
pair.second.cpu->_registers.p.d = true;
|
||||
pair.second.cpu->_registers.p.m = true;
|
||||
pair.second.wram->_data[0] = 0x03;
|
||||
pair.second.wram->_data[1] = 0x20;
|
||||
pair.second.cpu->SBC(0x0);
|
||||
cr_assert_eq(pair.second.cpu->_registers.a, 0x7998, "The accumulator's value should be 0x7998 but it was 0x%x.", pair.second.cpu->_registers.a);
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.n, false, "The negative flags should not be set.");
|
||||
cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should be not set.");
|
||||
}
|
||||
Reference in New Issue
Block a user