Fixed some errors

This commit is contained in:
Selim Mustafaev 2023-08-20 15:47:38 +03:00
parent 40e2f45177
commit 80fb089e17
4 changed files with 13 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
cmake-build-debug/ cmake-build-debug/
.idea/ .idea/
.vs/

View File

@ -13,7 +13,7 @@ namespace nes {
auto romSize = fs::file_size(path); auto romSize = fs::file_size(path);
_romData = std::make_unique<uint8_t[]>(romSize); _romData = std::make_unique<uint8_t[]>(romSize);
std::ifstream rom(path); std::ifstream rom(path, std::ios::binary);
rom.read(reinterpret_cast<char*>(_romData.get()), static_cast<std::streamsize>(romSize)); rom.read(reinterpret_cast<char*>(_romData.get()), static_cast<std::streamsize>(romSize));
// UB here // UB here

View File

@ -70,7 +70,7 @@ namespace nes {
A = 0; A = 0;
X = 0; X = 0;
Y = 0; Y = 0;
SP = 0xFF; SP = 0xFD;
flags = 0; flags = 0;
uint16_t lo = _bus->read(0xFFFC); uint16_t lo = _bus->read(0xFFFC);
@ -92,7 +92,11 @@ namespace nes {
auto args = (this->*instruction.getAddress)(); auto args = (this->*instruction.getAddress)();
(this->*instruction.process)(args); (this->*instruction.process)(args);
std::cout << instruction.name << std::hex << ", PC: " << (int)PC << ", A: " << (int)A << ", X: " << (int)X << ", Y: " << (int)Y << std::endl; auto str = std::format("{} ({:02X}), PC: {:X}, SP: {:X}, A: {:02X}, X: {:02X}, Y: {:02X}, [N:{}, V:{}, B{}, D{}, I{}, Z:{}, C:{}]", instruction.name, opcode, PC, SP, A, X, Y,
(int)getFlag(Negative), (int)getFlag(Overflow), (int)getFlag(Break), (int)getFlag(DecimalMode), (int)getFlag(InterruptDisable), (int)getFlag(Zero), (int)getFlag(Carry));
std::cout << str << std::endl;
//std::cout << instruction.name << std::hex << ", OpCode: " << (int)opcode << ", PC: " << (int)PC << ", SP: " << (int)SP << ", A: " << (int)A << ", X: " << (int)X << ", Y: " << (int)Y << std::endl;
_ticks = instruction.cycles; _ticks = instruction.cycles;
if(instruction.variableCycles) { if(instruction.variableCycles) {
@ -136,7 +140,7 @@ namespace nes {
// Addressing modes // Addressing modes
Cpu::InstructionArgs Cpu::IMM() { Cpu::InstructionArgs Cpu::IMM() {
return {++PC, 0}; return {PC++, 0};
} }
Cpu::InstructionArgs Cpu::ABS() { Cpu::InstructionArgs Cpu::ABS() {
@ -259,7 +263,6 @@ namespace nes {
} }
void Cpu::JSR(Cpu::InstructionArgs args) { void Cpu::JSR(Cpu::InstructionArgs args) {
std::cout << "+++ >>> Jumping from: " << PC << std::endl;
PC--; PC--;
_bus->write(STACK_BASE + SP--, PC >> 8); _bus->write(STACK_BASE + SP--, PC >> 8);
_bus->write(STACK_BASE + SP--, PC & 0x00FF); _bus->write(STACK_BASE + SP--, PC & 0x00FF);
@ -313,8 +316,6 @@ namespace nes {
PC = (hi << 8) | lo; PC = (hi << 8) | lo;
PC++; PC++;
std::cout << "+++ <<< return to: " << PC << std::endl;
} }
void Cpu::SED(Cpu::InstructionArgs args) { void Cpu::SED(Cpu::InstructionArgs args) {
@ -342,7 +343,7 @@ namespace nes {
void Cpu::CMP(Cpu::InstructionArgs args) { void Cpu::CMP(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _bus->read(args.address);
uint16_t diff = A - value; uint16_t diff = A - value;
setFlag(Carry, A > value); setFlag(Carry, A >= value);
setFlag(Zero, (diff & 0x00FF) == 0); setFlag(Zero, (diff & 0x00FF) == 0);
setFlag(Negative, diff & 0x0080); setFlag(Negative, diff & 0x0080);
} }
@ -381,7 +382,7 @@ namespace nes {
void Cpu::CPY(Cpu::InstructionArgs args) { void Cpu::CPY(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _bus->read(args.address);
uint16_t diff = Y - value; uint16_t diff = Y - value;
setFlag(Carry, Y > value); setFlag(Carry, Y >= value);
setFlag(Zero, (diff & 0x00FF) == 0); setFlag(Zero, (diff & 0x00FF) == 0);
setFlag(Negative, diff & 0x0080); setFlag(Negative, diff & 0x0080);
} }
@ -389,7 +390,7 @@ namespace nes {
void Cpu::CPX(Cpu::InstructionArgs args) { void Cpu::CPX(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _bus->read(args.address);
uint16_t diff = X - value; uint16_t diff = X - value;
setFlag(Carry, X > value); setFlag(Carry, X >= value);
setFlag(Zero, (diff & 0x00FF) == 0); setFlag(Zero, (diff & 0x00FF) == 0);
setFlag(Negative, diff & 0x0080); setFlag(Negative, diff & 0x0080);
} }

View File

@ -37,7 +37,7 @@ namespace nes {
} }
size_t ticks = 0; size_t ticks = 0;
while (ticks <= 2000) { while (ticks <= 5000) {
_cpu->tick(); _cpu->tick();
ticks++; ticks++;
} }