diff --git a/src/Cpu.cpp b/src/Cpu.cpp index 47e1e56..37818a6 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -8,7 +8,8 @@ namespace nes { - Cpu::Cpu(Bus* bus): _ticks{}, _bus{bus}, A{}, X{}, Y{}, PC{}, SP{}, flags{} { + Cpu::Cpu(): _ticks{}, A{}, X{}, Y{}, PC{}, SP{}, flags{} { + _bus = std::make_unique(); _instructions = std::vector(256); _instructions[0x00] = {"BRK", &Cpu::BRK, &Cpu::IMP, 7, false}; _instructions[0x01] = {"ORA", &Cpu::ORA, &Cpu::IZX, 6, false}; @@ -188,9 +189,9 @@ namespace nes { auto args = (this->*instruction.getAddress)(); (this->*instruction.process)(args); - 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; +// 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; @@ -219,6 +220,10 @@ namespace nes { PC = address; } + Bus* Cpu::bus() const { + return _bus.get(); + } + void Cpu::branch(Cpu::InstructionArgs args) { _ticks++; diff --git a/src/Cpu.h b/src/Cpu.h index 6f127d3..2a5ff80 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -40,16 +40,17 @@ namespace nes { static constexpr uint16_t STACK_BASE = 0x0100; public: - explicit Cpu(Bus* bus); + Cpu(); void reset(); void tick(); void setFlag(CpuFlags flag, bool value); bool getFlag(CpuFlags flag) const; void setStartAddress(uint16_t address); + Bus* bus() const; private: size_t _ticks; - Bus* _bus; + std::unique_ptr _bus; std::vector _instructions; private: diff --git a/src/Nes.cpp b/src/Nes.cpp index 282280e..a7bfb0b 100644 --- a/src/Nes.cpp +++ b/src/Nes.cpp @@ -11,24 +11,15 @@ namespace nes { Nes::Nes() { - _bus = std::make_unique(); - _cpu = std::make_unique(_bus.get()); + _cpu = std::make_unique(); } void Nes::runRom(const fs::path &path, std::optional address) { - _cartridge = std::make_unique(path); - _bus->connect(_cartridge.get()); + _cartridge = std::make_shared(path); + _cpu->bus()->connect(_cartridge.get()); reset(address); } - uint8_t Nes::read(uint16_t address) { - return _bus->read(address); - } - - void Nes::write(uint16_t address, uint8_t value) { - _bus->write(address, value); - } - void Nes::reset(std::optional address) { _cpu->reset(); diff --git a/src/Nes.h b/src/Nes.h index b254c8f..8b245b5 100644 --- a/src/Nes.h +++ b/src/Nes.h @@ -20,14 +20,9 @@ namespace nes { void runRom(const fs::path& path, std::optional address = std::nullopt); void reset(std::optional address = std::nullopt); - public: // For debug - uint8_t read(uint16_t address); - void write(uint16_t address, uint8_t value); - private: - std::unique_ptr _bus; std::unique_ptr _cpu; - std::unique_ptr _cartridge; + std::shared_ptr _cartridge; }; }