Refactoring
This commit is contained in:
parent
116a195cae
commit
cf0a1a5cfe
@ -13,8 +13,6 @@ add_executable(nes
|
|||||||
src/Nes.h
|
src/Nes.h
|
||||||
src/Cpu.cpp
|
src/Cpu.cpp
|
||||||
src/Cpu.h
|
src/Cpu.h
|
||||||
src/Bus.cpp
|
|
||||||
src/Bus.h
|
|
||||||
src/Mapper/Mapper.cpp
|
src/Mapper/Mapper.cpp
|
||||||
src/Mapper/Mapper.h
|
src/Mapper/Mapper.h
|
||||||
src/Mapper/Mapper0.cpp
|
src/Mapper/Mapper0.cpp
|
||||||
|
|||||||
79
src/Bus.cpp
79
src/Bus.cpp
@ -1,79 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by Selim Mustafaev on 12.08.2023.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "Bus.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
namespace nes {
|
|
||||||
|
|
||||||
Bus::Bus(): _cartridge{nullptr} {
|
|
||||||
_ram = std::make_unique<uint8_t[]>(2*1024);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t Bus::read(uint16_t address) {
|
|
||||||
if(address < 0x2000) {
|
|
||||||
return _ram[address & 0x07FF];
|
|
||||||
}
|
|
||||||
else if(address >= 0x2000 && address < 0x4000) {
|
|
||||||
return _ppu->read(address & 0x0007);
|
|
||||||
}
|
|
||||||
else if(address >= 0x8000) {
|
|
||||||
return _cartridge->readPrg(address);
|
|
||||||
}
|
|
||||||
else if(address == 0x4016) {
|
|
||||||
return _controller1->read();
|
|
||||||
}
|
|
||||||
else if(address == 0x4017) {
|
|
||||||
// Controller 2
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bus::write(uint16_t address, uint8_t value) {
|
|
||||||
if(address < 0x2000) {
|
|
||||||
_ram[address & 0x07FF] = value;
|
|
||||||
}
|
|
||||||
else if(address >= 0x2000 && address < 0x4000) {
|
|
||||||
_ppu->write(address & 0x0007, value);
|
|
||||||
}
|
|
||||||
else if(address >= 0x8000) {
|
|
||||||
std::cout << "Cartridge write at address: " << address << std::endl;
|
|
||||||
}
|
|
||||||
else if(address == 0x4014) {
|
|
||||||
// DMA
|
|
||||||
}
|
|
||||||
else if(address == 0x4016) {
|
|
||||||
_controller1->poll();
|
|
||||||
}
|
|
||||||
else if(address == 0x4017) {
|
|
||||||
// Controller 2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bus::connect(std::shared_ptr<Cartridge> cartridge) {
|
|
||||||
_cartridge = std::move(cartridge);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bus::connect(std::shared_ptr<Ppu> ppu) {
|
|
||||||
_ppu = std::move(ppu);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bus::connect(std::shared_ptr<Controller> controller) {
|
|
||||||
_controller1 = std::move(controller);
|
|
||||||
}
|
|
||||||
|
|
||||||
// For debug
|
|
||||||
// Calc "hash" - just sum of the bytes of first page
|
|
||||||
// Can be useful for detecting change in ram
|
|
||||||
uint32_t Bus::zpHash() const {
|
|
||||||
uint32_t sum = 0;
|
|
||||||
for(size_t i = 0; i < 2048; ++i) {
|
|
||||||
sum += _ram[i];
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
36
src/Bus.h
36
src/Bus.h
@ -1,36 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by Selim Mustafaev on 12.08.2023.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef NES_BUS_H
|
|
||||||
#define NES_BUS_H
|
|
||||||
|
|
||||||
#include "Cartridge.h"
|
|
||||||
#include "Ppu.h"
|
|
||||||
#include "Controller.h"
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace nes {
|
|
||||||
|
|
||||||
class Bus {
|
|
||||||
public:
|
|
||||||
Bus();
|
|
||||||
uint8_t read(uint16_t address);
|
|
||||||
void write(uint16_t address, uint8_t value);
|
|
||||||
void connect(std::shared_ptr<Cartridge> cartridge);
|
|
||||||
void connect(std::shared_ptr<Ppu> ppu);
|
|
||||||
void connect(std::shared_ptr<Controller> controller);
|
|
||||||
[[nodiscard]] uint32_t zpHash() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::unique_ptr<uint8_t[]> _ram;
|
|
||||||
std::shared_ptr<Cartridge> _cartridge;
|
|
||||||
std::shared_ptr<Ppu> _ppu;
|
|
||||||
std::shared_ptr<Controller> _controller1;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif //NES_BUS_H
|
|
||||||
155
src/Cpu.cpp
155
src/Cpu.cpp
@ -3,13 +3,14 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "Cpu.h"
|
#include "Cpu.h"
|
||||||
|
#include "Nes.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
|
||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
Cpu::Cpu(): _ticks{}, A{}, X{}, Y{}, PC{}, SP{}, flags{} {
|
Cpu::Cpu(Nes* system): _ticks{}, A{}, X{}, Y{}, PC{}, SP{}, flags{} {
|
||||||
_bus = std::make_unique<Bus>();
|
_system = system;
|
||||||
_instructions = std::vector<Instruction>(256);
|
_instructions = std::vector<Instruction>(256);
|
||||||
_instructions[0x00] = {"BRK", &Cpu::BRK, &Cpu::IMP, 7, false};
|
_instructions[0x00] = {"BRK", &Cpu::BRK, &Cpu::IMP, 7, false};
|
||||||
_instructions[0x01] = {"ORA", &Cpu::ORA, &Cpu::IZX, 6, false};
|
_instructions[0x01] = {"ORA", &Cpu::ORA, &Cpu::IZX, 6, false};
|
||||||
@ -170,8 +171,8 @@ namespace nes {
|
|||||||
SP = 0xFD;
|
SP = 0xFD;
|
||||||
flags = 0;
|
flags = 0;
|
||||||
|
|
||||||
uint16_t lo = _bus->read(0xFFFC);
|
uint16_t lo = _system->read(0xFFFC);
|
||||||
uint16_t hi = _bus->read(0xFFFD);
|
uint16_t hi = _system->read(0xFFFD);
|
||||||
|
|
||||||
PC = (hi << 8) | lo;
|
PC = (hi << 8) | lo;
|
||||||
|
|
||||||
@ -182,7 +183,7 @@ namespace nes {
|
|||||||
bool executed = false;
|
bool executed = false;
|
||||||
|
|
||||||
if(_ticks == 0) {
|
if(_ticks == 0) {
|
||||||
uint8_t opcode = _bus->read(PC++);
|
uint8_t opcode = _system->read(PC++);
|
||||||
auto instruction = _instructions[opcode];
|
auto instruction = _instructions[opcode];
|
||||||
_currentOpcode = opcode;
|
_currentOpcode = opcode;
|
||||||
|
|
||||||
@ -207,16 +208,16 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::nmi() {
|
void Cpu::nmi() {
|
||||||
_bus->write(STACK_BASE + SP--, PC >> 8);
|
_system->write(STACK_BASE + SP--, PC >> 8);
|
||||||
_bus->write(STACK_BASE + SP--, PC & 0x00FF);
|
_system->write(STACK_BASE + SP--, PC & 0x00FF);
|
||||||
|
|
||||||
setFlag(Break, false);
|
setFlag(Break, false);
|
||||||
setFlag(Unused, true);
|
setFlag(Unused, true);
|
||||||
setFlag(InterruptDisable, true);
|
setFlag(InterruptDisable, true);
|
||||||
_bus->write(STACK_BASE + SP--, flags);
|
_system->write(STACK_BASE + SP--, flags);
|
||||||
|
|
||||||
uint8_t lo = _bus->read(0xFFFA);
|
uint8_t lo = _system->read(0xFFFA);
|
||||||
uint8_t hi = _bus->read(0xFFFB);
|
uint8_t hi = _system->read(0xFFFB);
|
||||||
PC = (hi << 8) | lo;
|
PC = (hi << 8) | lo;
|
||||||
|
|
||||||
_ticks = 8;
|
_ticks = 8;
|
||||||
@ -234,7 +235,7 @@ namespace nes {
|
|||||||
(int)getFlag(InterruptDisable),
|
(int)getFlag(InterruptDisable),
|
||||||
(int)getFlag(Zero),
|
(int)getFlag(Zero),
|
||||||
(int)getFlag(Carry),
|
(int)getFlag(Carry),
|
||||||
_bus->zpHash());
|
_system->zpHash());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::setFlag(CpuFlags flag, bool value) {
|
void Cpu::setFlag(CpuFlags flag, bool value) {
|
||||||
@ -253,10 +254,6 @@ namespace nes {
|
|||||||
PC = address;
|
PC = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bus* Cpu::bus() const {
|
|
||||||
return _bus.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Cpu::branch(Cpu::InstructionArgs args) {
|
void Cpu::branch(Cpu::InstructionArgs args) {
|
||||||
_ticks++;
|
_ticks++;
|
||||||
|
|
||||||
@ -278,8 +275,8 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::ABS() {
|
Cpu::InstructionArgs Cpu::ABS() {
|
||||||
uint8_t lo = _bus->read(PC++);
|
uint8_t lo = _system->read(PC++);
|
||||||
uint8_t hi = _bus->read(PC++);
|
uint8_t hi = _system->read(PC++);
|
||||||
uint16_t address = (hi << 8) | lo;
|
uint16_t address = (hi << 8) | lo;
|
||||||
return {address, 0};
|
return {address, 0};
|
||||||
}
|
}
|
||||||
@ -289,7 +286,7 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::REL() {
|
Cpu::InstructionArgs Cpu::REL() {
|
||||||
uint16_t address = _bus->read(PC++);
|
uint16_t address = _system->read(PC++);
|
||||||
|
|
||||||
if (address & 0x80) {
|
if (address & 0x80) {
|
||||||
address |= 0xFF00;
|
address |= 0xFF00;
|
||||||
@ -299,22 +296,22 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::ZP0() {
|
Cpu::InstructionArgs Cpu::ZP0() {
|
||||||
uint16_t address = _bus->read(PC++);
|
uint16_t address = _system->read(PC++);
|
||||||
return {address, 0};
|
return {address, 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::IZX() {
|
Cpu::InstructionArgs Cpu::IZX() {
|
||||||
uint8_t ptrAddress = _bus->read(PC++) + X;
|
uint8_t ptrAddress = _system->read(PC++) + X;
|
||||||
uint8_t lo = _bus->read(ptrAddress);
|
uint8_t lo = _system->read(ptrAddress);
|
||||||
uint8_t hi = _bus->read(++ptrAddress);
|
uint8_t hi = _system->read(++ptrAddress);
|
||||||
uint16_t address = (hi << 8) | lo;
|
uint16_t address = (hi << 8) | lo;
|
||||||
return {address, 0};
|
return {address, 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::IZY() {
|
Cpu::InstructionArgs Cpu::IZY() {
|
||||||
uint8_t ptrAddress = _bus->read(PC++);
|
uint8_t ptrAddress = _system->read(PC++);
|
||||||
uint8_t lo = _bus->read(ptrAddress);
|
uint8_t lo = _system->read(ptrAddress);
|
||||||
uint8_t hi = _bus->read(++ptrAddress);
|
uint8_t hi = _system->read(++ptrAddress);
|
||||||
uint16_t address = (hi << 8) | lo;
|
uint16_t address = (hi << 8) | lo;
|
||||||
address += Y;
|
address += Y;
|
||||||
|
|
||||||
@ -327,17 +324,17 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::IND() {
|
Cpu::InstructionArgs Cpu::IND() {
|
||||||
uint8_t ptrLo = _bus->read(PC++);
|
uint8_t ptrLo = _system->read(PC++);
|
||||||
uint8_t ptrHi = _bus->read(PC++);
|
uint8_t ptrHi = _system->read(PC++);
|
||||||
uint16_t ptrAddress = (ptrHi << 8) | ptrLo;
|
uint16_t ptrAddress = (ptrHi << 8) | ptrLo;
|
||||||
|
|
||||||
uint8_t lo = _bus->read(ptrAddress);
|
uint8_t lo = _system->read(ptrAddress);
|
||||||
uint8_t hi = 0;
|
uint8_t hi = 0;
|
||||||
|
|
||||||
if(ptrLo == 0xFF) {
|
if(ptrLo == 0xFF) {
|
||||||
hi = _bus->read(ptrAddress & 0xFF00);
|
hi = _system->read(ptrAddress & 0xFF00);
|
||||||
} else {
|
} else {
|
||||||
hi = _bus->read(++ptrAddress);
|
hi = _system->read(++ptrAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t address = (hi << 8) | lo;
|
uint16_t address = (hi << 8) | lo;
|
||||||
@ -345,8 +342,8 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::ABX() {
|
Cpu::InstructionArgs Cpu::ABX() {
|
||||||
uint8_t lo = _bus->read(PC++);
|
uint8_t lo = _system->read(PC++);
|
||||||
uint8_t hi = _bus->read(PC++);
|
uint8_t hi = _system->read(PC++);
|
||||||
uint16_t address = (hi << 8) | lo;
|
uint16_t address = (hi << 8) | lo;
|
||||||
address += X;
|
address += X;
|
||||||
|
|
||||||
@ -359,8 +356,8 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::ABY() {
|
Cpu::InstructionArgs Cpu::ABY() {
|
||||||
uint8_t lo = _bus->read(PC++);
|
uint8_t lo = _system->read(PC++);
|
||||||
uint8_t hi = _bus->read(PC++);
|
uint8_t hi = _system->read(PC++);
|
||||||
uint16_t address = (hi << 8) | lo;
|
uint16_t address = (hi << 8) | lo;
|
||||||
address += Y;
|
address += Y;
|
||||||
|
|
||||||
@ -373,41 +370,41 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::ZPX() {
|
Cpu::InstructionArgs Cpu::ZPX() {
|
||||||
uint8_t address = _bus->read(PC++) + X;
|
uint8_t address = _system->read(PC++) + X;
|
||||||
return {address, 0};
|
return {address, 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
Cpu::InstructionArgs Cpu::ZPY() {
|
Cpu::InstructionArgs Cpu::ZPY() {
|
||||||
uint8_t address = _bus->read(PC++) + Y;
|
uint8_t address = _system->read(PC++) + Y;
|
||||||
return {address, 0};
|
return {address, 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
// CPU instructions
|
// CPU instructions
|
||||||
|
|
||||||
void Cpu::LDA(Cpu::InstructionArgs args) {
|
void Cpu::LDA(Cpu::InstructionArgs args) {
|
||||||
A = _bus->read(args.address);
|
A = _system->read(args.address);
|
||||||
setFlag(Negative, A & 0x80);
|
setFlag(Negative, A & 0x80);
|
||||||
setFlag(Zero, A == 0);
|
setFlag(Zero, A == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::LDX(InstructionArgs args) {
|
void Cpu::LDX(InstructionArgs args) {
|
||||||
X = _bus->read(args.address);
|
X = _system->read(args.address);
|
||||||
setFlag(Negative, X & 0x80);
|
setFlag(Negative, X & 0x80);
|
||||||
setFlag(Zero, X == 0);
|
setFlag(Zero, X == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::LDY(Cpu::InstructionArgs args) {
|
void Cpu::LDY(Cpu::InstructionArgs args) {
|
||||||
Y = _bus->read(args.address);
|
Y = _system->read(args.address);
|
||||||
setFlag(Negative, Y & 0x80);
|
setFlag(Negative, Y & 0x80);
|
||||||
setFlag(Zero, Y == 0);
|
setFlag(Zero, Y == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::STA(Cpu::InstructionArgs args) {
|
void Cpu::STA(Cpu::InstructionArgs args) {
|
||||||
_bus->write(args.address, A);
|
_system->write(args.address, A);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::STX(InstructionArgs args) {
|
void Cpu::STX(InstructionArgs args) {
|
||||||
_bus->write(args.address, X);
|
_system->write(args.address, X);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::CLC(Cpu::InstructionArgs args) {
|
void Cpu::CLC(Cpu::InstructionArgs args) {
|
||||||
@ -415,7 +412,7 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ADC(Cpu::InstructionArgs args) {
|
void Cpu::ADC(Cpu::InstructionArgs args) {
|
||||||
uint16_t value = _bus->read(args.address);
|
uint16_t value = _system->read(args.address);
|
||||||
uint16_t result = A + value + getFlag(Carry);
|
uint16_t result = A + value + getFlag(Carry);
|
||||||
|
|
||||||
setFlag(Carry, result > 255);
|
setFlag(Carry, result > 255);
|
||||||
@ -427,7 +424,7 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::SBC(Cpu::InstructionArgs args) {
|
void Cpu::SBC(Cpu::InstructionArgs args) {
|
||||||
uint16_t value = _bus->read(args.address) ^ 0xFF;
|
uint16_t value = _system->read(args.address) ^ 0xFF;
|
||||||
uint16_t result = A + value + getFlag(Carry);
|
uint16_t result = A + value + getFlag(Carry);
|
||||||
|
|
||||||
setFlag(Carry, result > 255);
|
setFlag(Carry, result > 255);
|
||||||
@ -477,8 +474,8 @@ namespace nes {
|
|||||||
|
|
||||||
void Cpu::JSR(Cpu::InstructionArgs args) {
|
void Cpu::JSR(Cpu::InstructionArgs args) {
|
||||||
PC--;
|
PC--;
|
||||||
_bus->write(STACK_BASE + SP--, PC >> 8);
|
_system->write(STACK_BASE + SP--, PC >> 8);
|
||||||
_bus->write(STACK_BASE + SP--, PC & 0x00FF);
|
_system->write(STACK_BASE + SP--, PC & 0x00FF);
|
||||||
PC = args.address;
|
PC = args.address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -505,7 +502,7 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::BIT(Cpu::InstructionArgs args) {
|
void Cpu::BIT(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
setFlag(Zero, (A & value) == 0);
|
setFlag(Zero, (A & value) == 0);
|
||||||
setFlag(Negative, value & (1 << 7));
|
setFlag(Negative, value & (1 << 7));
|
||||||
setFlag(Overflow, value & (1 << 6));
|
setFlag(Overflow, value & (1 << 6));
|
||||||
@ -524,8 +521,8 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::RTS(Cpu::InstructionArgs args) {
|
void Cpu::RTS(Cpu::InstructionArgs args) {
|
||||||
uint16_t lo = _bus->read(STACK_BASE + ++SP);
|
uint16_t lo = _system->read(STACK_BASE + ++SP);
|
||||||
uint16_t hi = _bus->read(STACK_BASE + ++SP);
|
uint16_t hi = _system->read(STACK_BASE + ++SP);
|
||||||
|
|
||||||
PC = (hi << 8) | lo;
|
PC = (hi << 8) | lo;
|
||||||
PC++;
|
PC++;
|
||||||
@ -536,25 +533,25 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::PHP(Cpu::InstructionArgs args) {
|
void Cpu::PHP(Cpu::InstructionArgs args) {
|
||||||
_bus->write(STACK_BASE + SP--, flags | Break | Unused);
|
_system->write(STACK_BASE + SP--, flags | Break | Unused);
|
||||||
setFlag(Break, false);
|
setFlag(Break, false);
|
||||||
setFlag(Unused, false);
|
setFlag(Unused, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::PLA(Cpu::InstructionArgs args) {
|
void Cpu::PLA(Cpu::InstructionArgs args) {
|
||||||
A = _bus->read(STACK_BASE + ++SP);
|
A = _system->read(STACK_BASE + ++SP);
|
||||||
setFlag(Zero, A == 0);
|
setFlag(Zero, A == 0);
|
||||||
setFlag(Negative, A & 0x80);
|
setFlag(Negative, A & 0x80);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::AND(Cpu::InstructionArgs args) {
|
void Cpu::AND(Cpu::InstructionArgs args) {
|
||||||
A &= _bus->read(args.address);;
|
A &= _system->read(args.address);;
|
||||||
setFlag(Zero, A == 0);
|
setFlag(Zero, A == 0);
|
||||||
setFlag(Negative, A & 0x80);
|
setFlag(Negative, A & 0x80);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::CMP(Cpu::InstructionArgs args) {
|
void Cpu::CMP(Cpu::InstructionArgs args) {
|
||||||
uint16_t value = _bus->read(args.address);
|
uint16_t value = _system->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);
|
||||||
@ -568,16 +565,16 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::PHA(Cpu::InstructionArgs args) {
|
void Cpu::PHA(Cpu::InstructionArgs args) {
|
||||||
_bus->write(STACK_BASE + SP--, A);
|
_system->write(STACK_BASE + SP--, A);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::PLP(Cpu::InstructionArgs args) {
|
void Cpu::PLP(Cpu::InstructionArgs args) {
|
||||||
flags = _bus->read(STACK_BASE + ++SP);
|
flags = _system->read(STACK_BASE + ++SP);
|
||||||
setFlag(Unused, true);
|
setFlag(Unused, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ORA(Cpu::InstructionArgs args) {
|
void Cpu::ORA(Cpu::InstructionArgs args) {
|
||||||
A |= _bus->read(args.address);
|
A |= _system->read(args.address);
|
||||||
setFlag(Zero, A == 0);
|
setFlag(Zero, A == 0);
|
||||||
setFlag(Negative, A & 0x80);
|
setFlag(Negative, A & 0x80);
|
||||||
}
|
}
|
||||||
@ -587,13 +584,13 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::EOR(Cpu::InstructionArgs args) {
|
void Cpu::EOR(Cpu::InstructionArgs args) {
|
||||||
A ^= _bus->read(args.address);
|
A ^= _system->read(args.address);
|
||||||
setFlag(Zero, A == 0);
|
setFlag(Zero, A == 0);
|
||||||
setFlag(Negative, A & 0x80);
|
setFlag(Negative, A & 0x80);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::CPY(Cpu::InstructionArgs args) {
|
void Cpu::CPY(Cpu::InstructionArgs args) {
|
||||||
uint16_t value = _bus->read(args.address);
|
uint16_t value = _system->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);
|
||||||
@ -601,7 +598,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 = _system->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);
|
||||||
@ -609,7 +606,7 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::STY(Cpu::InstructionArgs args) {
|
void Cpu::STY(Cpu::InstructionArgs args) {
|
||||||
_bus->write(args.address, Y);
|
_system->write(args.address, Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::INY(Cpu::InstructionArgs args) {
|
void Cpu::INY(Cpu::InstructionArgs args) {
|
||||||
@ -664,25 +661,25 @@ namespace nes {
|
|||||||
PC++;
|
PC++;
|
||||||
setFlag(InterruptDisable, true);
|
setFlag(InterruptDisable, true);
|
||||||
|
|
||||||
_bus->write(STACK_BASE + SP--, PC >> 8);
|
_system->write(STACK_BASE + SP--, PC >> 8);
|
||||||
_bus->write(STACK_BASE + SP--, PC & 0x00FF);
|
_system->write(STACK_BASE + SP--, PC & 0x00FF);
|
||||||
|
|
||||||
setFlag(Break, true);
|
setFlag(Break, true);
|
||||||
_bus->write(STACK_BASE + SP--, flags);
|
_system->write(STACK_BASE + SP--, flags);
|
||||||
setFlag(Break, false);
|
setFlag(Break, false);
|
||||||
|
|
||||||
uint16_t lo = _bus->read(0xFFFE);
|
uint16_t lo = _system->read(0xFFFE);
|
||||||
uint16_t hi = _bus->read(0xFFFF);
|
uint16_t hi = _system->read(0xFFFF);
|
||||||
PC = (hi << 8) | lo;
|
PC = (hi << 8) | lo;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::RTI(Cpu::InstructionArgs args) {
|
void Cpu::RTI(Cpu::InstructionArgs args) {
|
||||||
flags = _bus->read(STACK_BASE + ++SP);
|
flags = _system->read(STACK_BASE + ++SP);
|
||||||
setFlag(Break, false);
|
setFlag(Break, false);
|
||||||
setFlag(Unused, false);
|
setFlag(Unused, false);
|
||||||
|
|
||||||
uint16_t lo = _bus->read(STACK_BASE + ++SP);
|
uint16_t lo = _system->read(STACK_BASE + ++SP);
|
||||||
uint16_t hi = _bus->read(STACK_BASE + ++SP);
|
uint16_t hi = _system->read(STACK_BASE + ++SP);
|
||||||
PC = (hi << 8) | lo;
|
PC = (hi << 8) | lo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -694,12 +691,12 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::LSR(Cpu::InstructionArgs args) {
|
void Cpu::LSR(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
setFlag(Carry, value & 0x01);
|
setFlag(Carry, value & 0x01);
|
||||||
value = value >> 1;
|
value = value >> 1;
|
||||||
setFlag(Zero, value == 0);
|
setFlag(Zero, value == 0);
|
||||||
setFlag(Negative, value & 0x80);
|
setFlag(Negative, value & 0x80);
|
||||||
_bus->write(args.address, value);
|
_system->write(args.address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ASL_ACC(Cpu::InstructionArgs args) {
|
void Cpu::ASL_ACC(Cpu::InstructionArgs args) {
|
||||||
@ -710,12 +707,12 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ASL(Cpu::InstructionArgs args) {
|
void Cpu::ASL(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
setFlag(Carry, value & 0x80);
|
setFlag(Carry, value & 0x80);
|
||||||
value = value << 1;
|
value = value << 1;
|
||||||
setFlag(Zero, value == 0);
|
setFlag(Zero, value == 0);
|
||||||
setFlag(Negative, value & 0x80);
|
setFlag(Negative, value & 0x80);
|
||||||
_bus->write(args.address, value);
|
_system->write(args.address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ROR_ACC(Cpu::InstructionArgs args) {
|
void Cpu::ROR_ACC(Cpu::InstructionArgs args) {
|
||||||
@ -727,13 +724,13 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ROR(Cpu::InstructionArgs args) {
|
void Cpu::ROR(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
uint8_t carryOriginal = getFlag(Carry);
|
uint8_t carryOriginal = getFlag(Carry);
|
||||||
setFlag(Carry, value & 0x01);
|
setFlag(Carry, value & 0x01);
|
||||||
value = (value >> 1) | (carryOriginal << 7);
|
value = (value >> 1) | (carryOriginal << 7);
|
||||||
setFlag(Zero, value == 0);
|
setFlag(Zero, value == 0);
|
||||||
setFlag(Negative, value & 0x80);
|
setFlag(Negative, value & 0x80);
|
||||||
_bus->write(args.address, value);
|
_system->write(args.address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ROL_ACC(Cpu::InstructionArgs args) {
|
void Cpu::ROL_ACC(Cpu::InstructionArgs args) {
|
||||||
@ -745,25 +742,25 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::ROL(Cpu::InstructionArgs args) {
|
void Cpu::ROL(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
uint8_t carryOriginal = getFlag(Carry);
|
uint8_t carryOriginal = getFlag(Carry);
|
||||||
setFlag(Carry, value & 0x80);
|
setFlag(Carry, value & 0x80);
|
||||||
value = (value << 1) | carryOriginal;
|
value = (value << 1) | carryOriginal;
|
||||||
setFlag(Zero, value == 0);
|
setFlag(Zero, value == 0);
|
||||||
setFlag(Negative, value & 0x80);
|
setFlag(Negative, value & 0x80);
|
||||||
_bus->write(args.address, value);
|
_system->write(args.address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::INC(Cpu::InstructionArgs args) {
|
void Cpu::INC(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
_bus->write(args.address, ++value);
|
_system->write(args.address, ++value);
|
||||||
setFlag(Zero, value == 0);
|
setFlag(Zero, value == 0);
|
||||||
setFlag(Negative, value & 0x80);
|
setFlag(Negative, value & 0x80);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cpu::DEC(Cpu::InstructionArgs args) {
|
void Cpu::DEC(Cpu::InstructionArgs args) {
|
||||||
uint8_t value = _bus->read(args.address);
|
uint8_t value = _system->read(args.address);
|
||||||
_bus->write(args.address, --value);
|
_system->write(args.address, --value);
|
||||||
setFlag(Zero, value == 0);
|
setFlag(Zero, value == 0);
|
||||||
setFlag(Negative, value & 0x80);
|
setFlag(Negative, value & 0x80);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,12 +5,14 @@
|
|||||||
#ifndef NES_CPU_H
|
#ifndef NES_CPU_H
|
||||||
#define NES_CPU_H
|
#define NES_CPU_H
|
||||||
|
|
||||||
#include "Bus.h"
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
|
class Nes;
|
||||||
|
|
||||||
enum CpuFlags: uint8_t {
|
enum CpuFlags: uint8_t {
|
||||||
Carry = (1 << 0),
|
Carry = (1 << 0),
|
||||||
Zero = (1 << 1),
|
Zero = (1 << 1),
|
||||||
@ -40,19 +42,18 @@ namespace nes {
|
|||||||
static constexpr uint16_t STACK_BASE = 0x0100;
|
static constexpr uint16_t STACK_BASE = 0x0100;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Cpu();
|
explicit Cpu(Nes* system);
|
||||||
void reset();
|
void reset();
|
||||||
bool tick();
|
bool tick();
|
||||||
void setFlag(CpuFlags flag, bool value);
|
void setFlag(CpuFlags flag, bool value);
|
||||||
bool getFlag(CpuFlags flag) const;
|
bool getFlag(CpuFlags flag) const;
|
||||||
void setStartAddress(uint16_t address);
|
void setStartAddress(uint16_t address);
|
||||||
Bus* bus() const;
|
|
||||||
void nmi();
|
void nmi();
|
||||||
std::string state() const;
|
std::string state() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t _ticks;
|
size_t _ticks;
|
||||||
std::unique_ptr<Bus> _bus;
|
Nes* _system;
|
||||||
std::vector<Instruction> _instructions;
|
std::vector<Instruction> _instructions;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
68
src/Nes.cpp
68
src/Nes.cpp
@ -5,8 +5,11 @@
|
|||||||
#include "Nes.h"
|
#include "Nes.h"
|
||||||
#include "Cartridge.h"
|
#include "Cartridge.h"
|
||||||
#include "Cpu.h"
|
#include "Cpu.h"
|
||||||
|
#include "Ppu.h"
|
||||||
|
#include "Controller.h"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
@ -16,20 +19,19 @@ namespace nes {
|
|||||||
,_logger(500*1024*1024)
|
,_logger(500*1024*1024)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
_cpu = std::make_unique<Cpu>();
|
_ram = std::make_unique<uint8_t[]>(2*1024);
|
||||||
_ppu = std::make_shared<Ppu>();
|
_cpu = std::make_unique<Cpu>(this);
|
||||||
|
_ppu = std::make_unique<Ppu>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nes::insertCartridge(const fs::path &path, std::optional<uint16_t> address) {
|
void Nes::insertCartridge(const fs::path &path, std::optional<uint16_t> address) {
|
||||||
_cartridge = std::make_shared<Cartridge>(path);
|
_cartridge = std::make_unique<Cartridge>(path);
|
||||||
_cpu->bus()->connect(_cartridge);
|
_ppu->connect(_cartridge.get());
|
||||||
_cpu->bus()->connect(_ppu);
|
|
||||||
_ppu->connect(_cartridge);
|
|
||||||
reset(address);
|
reset(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nes::connect(std::shared_ptr<Controller> controller) {
|
void Nes::connect(std::shared_ptr<Controller> controller) {
|
||||||
_cpu->bus()->connect(std::move(controller));
|
_controller1 = std::move(controller);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nes::reset(std::optional<uint16_t> address) {
|
void Nes::reset(std::optional<uint16_t> address) {
|
||||||
@ -77,4 +79,56 @@ namespace nes {
|
|||||||
_cycles++;
|
_cycles++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t Nes::read(uint16_t address) {
|
||||||
|
if(address < 0x2000) {
|
||||||
|
return _ram[address & 0x07FF];
|
||||||
|
}
|
||||||
|
else if(address >= 0x2000 && address < 0x4000) {
|
||||||
|
return _ppu->read(address & 0x0007);
|
||||||
|
}
|
||||||
|
else if(address >= 0x8000) {
|
||||||
|
return _cartridge->readPrg(address);
|
||||||
|
}
|
||||||
|
else if(address == 0x4016) {
|
||||||
|
return _controller1->read();
|
||||||
|
}
|
||||||
|
else if(address == 0x4017) {
|
||||||
|
// Controller 2
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Nes::write(uint16_t address, uint8_t value) {
|
||||||
|
if(address < 0x2000) {
|
||||||
|
_ram[address & 0x07FF] = value;
|
||||||
|
}
|
||||||
|
else if(address >= 0x2000 && address < 0x4000) {
|
||||||
|
_ppu->write(address & 0x0007, value);
|
||||||
|
}
|
||||||
|
else if(address >= 0x8000) {
|
||||||
|
std::cout << "Cartridge write at address: " << address << std::endl;
|
||||||
|
}
|
||||||
|
else if(address == 0x4014) {
|
||||||
|
// DMA
|
||||||
|
}
|
||||||
|
else if(address == 0x4016) {
|
||||||
|
_controller1->poll();
|
||||||
|
}
|
||||||
|
else if(address == 0x4017) {
|
||||||
|
// Controller 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For debug
|
||||||
|
// Calc "hash" - just sum of the bytes of RAM
|
||||||
|
// Can be useful for detecting changes in memory
|
||||||
|
uint32_t Nes::zpHash() const {
|
||||||
|
uint32_t sum = 0;
|
||||||
|
for(size_t i = 0; i < 2048; ++i) {
|
||||||
|
sum += _ram[i];
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/Nes.h
19
src/Nes.h
@ -5,16 +5,19 @@
|
|||||||
#ifndef NES_NES_H
|
#ifndef NES_NES_H
|
||||||
#define NES_NES_H
|
#define NES_NES_H
|
||||||
|
|
||||||
#include "Cpu.h"
|
|
||||||
#include "Cartridge.h"
|
|
||||||
#include "Ppu.h"
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "Cpu.h"
|
||||||
|
#include "Ppu.h"
|
||||||
|
#include "Cartridge.h"
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
|
class Controller;
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
class Nes {
|
class Nes {
|
||||||
@ -26,11 +29,17 @@ namespace nes {
|
|||||||
void setNewFrameCallback(std::function<void(const Pixel*)> onNewFrame);
|
void setNewFrameCallback(std::function<void(const Pixel*)> onNewFrame);
|
||||||
void tick();
|
void tick();
|
||||||
|
|
||||||
|
uint8_t read(uint16_t address);
|
||||||
|
void write(uint16_t address, uint8_t value);
|
||||||
|
[[nodiscard]] uint32_t zpHash() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t _cycles;
|
uint64_t _cycles;
|
||||||
|
std::unique_ptr<uint8_t[]> _ram;
|
||||||
std::unique_ptr<Cpu> _cpu;
|
std::unique_ptr<Cpu> _cpu;
|
||||||
std::shared_ptr<Ppu> _ppu;
|
std::unique_ptr<Ppu> _ppu;
|
||||||
std::shared_ptr<Cartridge> _cartridge;
|
std::unique_ptr<Cartridge> _cartridge;
|
||||||
|
std::shared_ptr<Controller> _controller1;
|
||||||
|
|
||||||
#ifdef NES_LOGGING
|
#ifdef NES_LOGGING
|
||||||
Logger _logger;
|
Logger _logger;
|
||||||
|
|||||||
@ -234,8 +234,8 @@ namespace nes {
|
|||||||
_frameBuffer[index] = pixel;
|
_frameBuffer[index] = pixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ppu::connect(std::shared_ptr<Cartridge> cartridge) {
|
void Ppu::connect(Cartridge* cartridge) {
|
||||||
_cartridge = std::move(cartridge);
|
_cartridge = cartridge;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ppu::reset() {
|
void Ppu::reset() {
|
||||||
|
|||||||
@ -110,7 +110,7 @@ namespace nes {
|
|||||||
void write(uint16_t address, uint8_t value);
|
void write(uint16_t address, uint8_t value);
|
||||||
uint8_t read(uint16_t address);
|
uint8_t read(uint16_t address);
|
||||||
void setPixel(uint16_t row, uint16_t column, Pixel pixel);
|
void setPixel(uint16_t row, uint16_t column, Pixel pixel);
|
||||||
void connect(std::shared_ptr<Cartridge> cartridge);
|
void connect(Cartridge* cartridge);
|
||||||
void reset();
|
void reset();
|
||||||
std::string state() const;
|
std::string state() const;
|
||||||
|
|
||||||
@ -154,7 +154,7 @@ namespace nes {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Pixel[]> _frameBuffer;
|
std::unique_ptr<Pixel[]> _frameBuffer;
|
||||||
std::shared_ptr<Cartridge> _cartridge;
|
Cartridge* _cartridge;
|
||||||
|
|
||||||
private: // For debug
|
private: // For debug
|
||||||
uint8_t _curPalette;
|
uint8_t _curPalette;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user