Moving bus into the CPU
This commit is contained in:
parent
0b7bba0730
commit
b17d32de15
13
src/Cpu.cpp
13
src/Cpu.cpp
@ -8,7 +8,8 @@
|
|||||||
|
|
||||||
namespace nes {
|
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 = 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};
|
||||||
@ -188,9 +189,9 @@ namespace nes {
|
|||||||
auto args = (this->*instruction.getAddress)();
|
auto args = (this->*instruction.getAddress)();
|
||||||
(this->*instruction.process)(args);
|
(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,
|
// 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));
|
// (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 << 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;
|
//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;
|
PC = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bus* Cpu::bus() const {
|
||||||
|
return _bus.get();
|
||||||
|
}
|
||||||
|
|
||||||
void Cpu::branch(Cpu::InstructionArgs args) {
|
void Cpu::branch(Cpu::InstructionArgs args) {
|
||||||
_ticks++;
|
_ticks++;
|
||||||
|
|
||||||
|
|||||||
@ -40,16 +40,17 @@ namespace nes {
|
|||||||
static constexpr uint16_t STACK_BASE = 0x0100;
|
static constexpr uint16_t STACK_BASE = 0x0100;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Cpu(Bus* bus);
|
Cpu();
|
||||||
void reset();
|
void reset();
|
||||||
void tick();
|
void 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;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t _ticks;
|
size_t _ticks;
|
||||||
Bus* _bus;
|
std::unique_ptr<Bus> _bus;
|
||||||
std::vector<Instruction> _instructions;
|
std::vector<Instruction> _instructions;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
15
src/Nes.cpp
15
src/Nes.cpp
@ -11,24 +11,15 @@
|
|||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
Nes::Nes() {
|
Nes::Nes() {
|
||||||
_bus = std::make_unique<Bus>();
|
_cpu = std::make_unique<Cpu>();
|
||||||
_cpu = std::make_unique<Cpu>(_bus.get());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nes::runRom(const fs::path &path, std::optional<uint16_t> address) {
|
void Nes::runRom(const fs::path &path, std::optional<uint16_t> address) {
|
||||||
_cartridge = std::make_unique<Cartridge>(path);
|
_cartridge = std::make_shared<Cartridge>(path);
|
||||||
_bus->connect(_cartridge.get());
|
_cpu->bus()->connect(_cartridge.get());
|
||||||
reset(address);
|
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) {
|
void Nes::reset(std::optional<uint16_t> address) {
|
||||||
_cpu->reset();
|
_cpu->reset();
|
||||||
|
|
||||||
|
|||||||
@ -20,14 +20,9 @@ namespace nes {
|
|||||||
void runRom(const fs::path& path, std::optional<uint16_t> address = std::nullopt);
|
void runRom(const fs::path& path, std::optional<uint16_t> address = std::nullopt);
|
||||||
void reset(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:
|
private:
|
||||||
std::unique_ptr<Bus> _bus;
|
|
||||||
std::unique_ptr<Cpu> _cpu;
|
std::unique_ptr<Cpu> _cpu;
|
||||||
std::unique_ptr<Cartridge> _cartridge;
|
std::shared_ptr<Cartridge> _cartridge;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user