Moving bus into the CPU

This commit is contained in:
Selim Mustafaev 2023-08-21 18:10:01 +03:00
parent 0b7bba0730
commit b17d32de15
4 changed files with 16 additions and 24 deletions

View File

@ -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<Bus>();
_instructions = std::vector<Instruction>(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++;

View File

@ -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> _bus;
std::vector<Instruction> _instructions;
private:

View File

@ -11,24 +11,15 @@
namespace nes {
Nes::Nes() {
_bus = std::make_unique<Bus>();
_cpu = std::make_unique<Cpu>(_bus.get());
_cpu = std::make_unique<Cpu>();
}
void Nes::runRom(const fs::path &path, std::optional<uint16_t> address) {
_cartridge = std::make_unique<Cartridge>(path);
_bus->connect(_cartridge.get());
_cartridge = std::make_shared<Cartridge>(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<uint16_t> address) {
_cpu->reset();

View File

@ -20,14 +20,9 @@ namespace nes {
void runRom(const fs::path& path, std::optional<uint16_t> address = std::nullopt);
void reset(std::optional<uint16_t> 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> _bus;
std::unique_ptr<Cpu> _cpu;
std::unique_ptr<Cartridge> _cartridge;
std::shared_ptr<Cartridge> _cartridge;
};
}