Finishing the array of instruction, it now works well

This commit is contained in:
Anonymus Raccoon
2020-03-26 22:19:16 +01:00
parent 04f51efddf
commit 80ebfa8064
10 changed files with 101 additions and 42 deletions

View File

@@ -709,6 +709,7 @@ namespace ComSquare::APU
cycles -= this->_paddingCycles; cycles -= this->_paddingCycles;
while (total < cycles && this->_state == Running) while (total < cycles && this->_state == Running)
total += this->_executeInstruction(); total += this->_executeInstruction();
std::cout << "Done." << std::endl;
if (this->_state == Running) if (this->_state == Running)
this->_paddingCycles = total - cycles; this->_paddingCycles = total - cycles;
} }

View File

@@ -303,6 +303,8 @@ namespace ComSquare::CPU
case AbsoluteIndirectIndexedByX: case AbsoluteIndirectIndexedByX:
valueAddr = this->_getAbsoluteIndirectIndexedByXAddr(); valueAddr = this->_getAbsoluteIndirectIndexedByXAddr();
break; break;
default:
break;
} }
return cycles + (this->*instruction.call)(valueAddr); return cycles + (this->*instruction.call)(valueAddr);

View File

@@ -532,7 +532,7 @@ namespace ComSquare::CPU
{&CPU::STA, 6, "sta", AddressingMode::DirectPageIndirectIndexedByYLong, 2}, // 97 {&CPU::STA, 6, "sta", AddressingMode::DirectPageIndirectIndexedByYLong, 2}, // 97
{&CPU::BRK, 7, "tya #-#", AddressingMode::Implied, 2}, // 98 {&CPU::BRK, 7, "tya #-#", AddressingMode::Implied, 2}, // 98
{&CPU::STA, 5, "sta", AddressingMode::AbsoluteIndexedByY, 3}, // 99 {&CPU::STA, 5, "sta", AddressingMode::AbsoluteIndexedByY, 3}, // 99
{&CPU::BRK, 7, "txs #-#", AddressingMode::Implied, 2}, // 9A {&CPU::TXS, 2, "txs", AddressingMode::Implied, 1}, // 9A
{&CPU::BRK, 7, "txy #-#", AddressingMode::Implied, 2}, // 9B {&CPU::BRK, 7, "txy #-#", AddressingMode::Implied, 2}, // 9B
{&CPU::STZ, 4, "stz", AddressingMode::Absolute, 3}, // 9C {&CPU::STZ, 4, "stz", AddressingMode::Absolute, 3}, // 9C
{&CPU::STA, 5, "sta", AddressingMode::AbsoluteIndexedByX, 3}, // 9D {&CPU::STA, 5, "sta", AddressingMode::AbsoluteIndexedByX, 3}, // 9D
@@ -546,9 +546,9 @@ namespace ComSquare::CPU
{&CPU::LDA, 3, "lda", AddressingMode::DirectPage, 2}, // A5 {&CPU::LDA, 3, "lda", AddressingMode::DirectPage, 2}, // A5
{&CPU::LDX, 3, "ldx", AddressingMode::DirectPage, 2}, // A6 {&CPU::LDX, 3, "ldx", AddressingMode::DirectPage, 2}, // A6
{&CPU::LDA, 6, "lda", AddressingMode::DirectPageIndirectLong, 2}, // A7 {&CPU::LDA, 6, "lda", AddressingMode::DirectPageIndirectLong, 2}, // A7
{&CPU::BRK, 7, "tay #-#", AddressingMode::Implied, 2}, // A8 {&CPU::TAY, 2, "tay", AddressingMode::Implied, 1}, // A8
{&CPU::LDA, 2, "lda", AddressingMode::ImmediateForA, 2}, // A9 {&CPU::LDA, 2, "lda", AddressingMode::ImmediateForA, 2}, // A9
{&CPU::BRK, 7, "tax #-#", AddressingMode::Implied, 2}, // AA {&CPU::TAX, 2, "tax", AddressingMode::Implied, 1}, // AA
{&CPU::BRK, 7, "trb #-#", AddressingMode::Implied, 2}, // AB {&CPU::BRK, 7, "trb #-#", AddressingMode::Implied, 2}, // AB
{&CPU::LDY, 4, "ldy", AddressingMode::Absolute, 4}, // AC {&CPU::LDY, 4, "ldy", AddressingMode::Absolute, 4}, // AC
{&CPU::LDA, 4, "lda", AddressingMode::Absolute, 3}, // AD {&CPU::LDA, 4, "lda", AddressingMode::Absolute, 3}, // AD

View File

@@ -57,6 +57,11 @@ namespace ComSquare::CPU
std::string name; std::string name;
AddressingMode addressingMode; AddressingMode addressingMode;
int size; int size;
Instruction() = default;
Instruction(const Instruction &) = default;
Instruction &operator=(const Instruction &) = default;
~Instruction() = default;
}; };
} }
#endif //COMSQUARE_INSTRUCTION_HPP #endif //COMSQUARE_INSTRUCTION_HPP

View File

@@ -48,15 +48,15 @@ namespace ComSquare::CPU
return 0; return 0;
} }
int CPU::SEP(uint24_t value) int CPU::SEP(uint24_t valueAddr)
{ {
this->_registers.p.flags |= value; this->_registers.p.flags |= this->_bus->read(valueAddr);
return 0; return 0;
} }
int CPU::REP(uint24_t value) int CPU::REP(uint24_t valueAddr)
{ {
this->_registers.p.flags &= ~value; this->_registers.p.flags &= ~this->_bus->read(valueAddr);
if (this->_isEmulationMode) { if (this->_isEmulationMode) {
this->_registers.p.x_b = true; this->_registers.p.x_b = true;
this->_registers.p.m = true; this->_registers.p.m = true;
@@ -64,19 +64,19 @@ namespace ComSquare::CPU
return 0; return 0;
} }
int CPU::JSR(uint24_t value) int CPU::JSR(uint24_t valueAddr)
{ {
this->_push(--this->_registers.pc); this->_push(--this->_registers.pc);
this->_registers.pc = value; this->_registers.pc = valueAddr;
return 0; return 0;
} }
int CPU::JSL(uint24_t value) int CPU::JSL(uint24_t valueAddr)
{ {
this->_registers.pac--; this->_registers.pac--;
this->_push(this->_registers.pbr); this->_push(this->_registers.pbr);
this->_push(this->_registers.pc); this->_push(this->_registers.pc);
this->_registers.pac = value; this->_registers.pac = valueAddr;
return 0; return 0;
} }

View File

@@ -7,6 +7,7 @@
#include "../Exceptions/InvalidOpcode.hpp" #include "../Exceptions/InvalidOpcode.hpp"
#include <QtEvents> #include <QtEvents>
#include <iostream> #include <iostream>
#include <utility>
using namespace ComSquare::CPU; using namespace ComSquare::CPU;
@@ -24,12 +25,16 @@ namespace ComSquare::Debugger
this->_window->setAttribute(Qt::WA_DeleteOnClose); this->_window->setAttribute(Qt::WA_DeleteOnClose);
this->_ui.setupUi(this->_window); this->_ui.setupUi(this->_window);
//
// this->_ui.disasembly->setModel(&this->_model); this->_ui.disasembly->setModel(&this->_model);
// this->_ui.disasembly->setShowGrid(false); this->_ui.disasembly->setShowGrid(false);
// this->_ui.disasembly->verticalHeader()->hide(); this->_ui.disasembly->verticalHeader()->hide();
// this->_ui.disasembly->horizontalHeader()->hide(); this->_ui.disasembly->horizontalHeader()->hide();
// this->_ui.disasembly->horizontalHeader()->setStretchLastSection(true); this->_ui.disasembly->horizontalHeader()->setStretchLastSection(true);
// uint24_t pc = 0x80800; // The first byte of the ROM //TODO make this work for other rom mapping.
// while (pc < 0x80800 + this->_cartridgeHeader.romSize)
// this->_disassembledInstructions.insert(pc, this->_parseInstruction(pc));
QMainWindow::connect(this->_ui.actionPause, &QAction::triggered, this, &CPUDebug::pause); QMainWindow::connect(this->_ui.actionPause, &QAction::triggered, this, &CPUDebug::pause);
QMainWindow::connect(this->_ui.actionStep, &QAction::triggered, this, &CPUDebug::step); QMainWindow::connect(this->_ui.actionStep, &QAction::triggered, this, &CPUDebug::step);
@@ -72,7 +77,7 @@ namespace ComSquare::Debugger
this->_isStepping = false; this->_isStepping = false;
this->_isPaused = true; this->_isPaused = true;
} }
this->_ui.logger->append((CPUDebug::_getInstructionString(this->_registers.pac - 1) + " - " + Utility::to_hex(opcode)).c_str()); this->_ui.logger->append((this->_parseInstruction(this->_registers.pac - 1).toString() + " - " + Utility::to_hex(opcode)).c_str());
unsigned ret = CPU::_executeInstruction(opcode); unsigned ret = CPU::_executeInstruction(opcode);
this->_updateRegistersPanel(); this->_updateRegistersPanel();
return ret; return ret;
@@ -162,7 +167,7 @@ namespace ComSquare::Debugger
std::string CPUDebug::_getImmediateValue8Bits(uint24_t pc) std::string CPUDebug::_getImmediateValue8Bits(uint24_t pc)
{ {
std::stringstream ss; std::stringstream ss;
ss << "#$" << std::hex << static_cast<int>(this->_bus->read(pc), true); ss << "#$" << std::hex << static_cast<unsigned>(this->_bus->read(pc, true));
return ss.str(); return ss.str();
} }
@@ -179,7 +184,7 @@ namespace ComSquare::Debugger
std::string CPUDebug::_getDirectValue(uint24_t pc) std::string CPUDebug::_getDirectValue(uint24_t pc)
{ {
std::stringstream ss; std::stringstream ss;
ss << "$" << std::hex << static_cast<int>(this->_bus->read(pc), true); ss << "$" << std::hex << static_cast<int>(this->_bus->read(pc, true));
return ss.str(); return ss.str();
} }
@@ -210,20 +215,36 @@ namespace ComSquare::Debugger
return ss.str(); return ss.str();
} }
std::string CPUDebug::_getInstructionString(uint24_t pc) DisassembledInstruction CPUDebug::_parseInstruction(uint24_t pc)
{ {
uint8_t opcode = this->_bus->read(pc++, true); uint24_t opcode = this->_bus->read(pc++, true);
Instruction instruction = this->_instructions[opcode];
return this->_instructions[opcode].name + this->_getInstructionParameter(pc); std::string argument = this->_getInstructionParameter(instruction, pc);
return DisassembledInstruction(instruction, argument, opcode);
} }
std::string CPUDebug::_getInstructionParameter(uint24_t pc) std::string CPUDebug::_getInstructionParameter(Instruction &instruction, uint24_t pc)
{ {
Instruction instruction = this->_instructions[opcode];
switch (instruction.addressingMode) { switch (instruction.addressingMode) {
case Implied:
return "";
case ImmediateForA: case ImmediateForA:
return this->_getImmediateAddrForA(pc); return this->_getImmediateValueForA(pc);
case ImmediateForX:
return this->_getImmediateValueForX(pc);
case Immediate8bits:
return this->_getImmediateValue8Bits(pc);
case Absolute:
return this->_getAbsoluteValue(pc);
case AbsoluteLong:
return this->_getAbsoluteLongValue(pc);
case DirectPage:
return this->_getDirectValue(pc);
case DirectPageIndexedByX:
return this->_getDirectIndexedByXValue(pc);
default:
return "???";
} }
} }
@@ -238,6 +259,14 @@ namespace ComSquare::Debugger
{ {
this->_window->activateWindow(); this->_window->activateWindow();
} }
DisassembledInstruction::DisassembledInstruction(const CPU::Instruction &instruction, std::string arg, uint8_t op)
: CPU::Instruction(instruction), argument(std::move(arg)), opcode(op) {}
std::string DisassembledInstruction::toString()
{
return this->name + " " + this->argument;
}
} }
DisassemblyModel::DisassemblyModel(ComSquare::Debugger::CPUDebug &cpu) : QAbstractTableModel(), _cpu(cpu){ } DisassemblyModel::DisassemblyModel(ComSquare::Debugger::CPUDebug &cpu) : QAbstractTableModel(), _cpu(cpu){ }
@@ -249,10 +278,10 @@ int DisassemblyModel::columnCount(const QModelIndex &) const
int DisassemblyModel::rowCount(const QModelIndex &) const int DisassemblyModel::rowCount(const QModelIndex &) const
{ {
return 0xFFFFFF; return 5;
} }
QVariant DisassemblyModel::data(const QModelIndex &index, int role) const QVariant DisassemblyModel::data(const QModelIndex &, int role) const
{ {
if (role != Qt::DisplayRole) if (role != Qt::DisplayRole)
return QVariant(); return QVariant();

View File

@@ -38,6 +38,18 @@ public:
namespace ComSquare::Debugger namespace ComSquare::Debugger
{ {
struct DisassembledInstruction : public CPU::Instruction {
std::string argument;
uint8_t opcode;
DisassembledInstruction(const CPU::Instruction &instruction, std::string argument, uint8_t opcode);
DisassembledInstruction(const DisassembledInstruction &) = default;
DisassembledInstruction &operator=(const DisassembledInstruction &) = default;
~DisassembledInstruction() = default;
std::string toString();
};
//! @brief A custom CPU with a window that show it's registers and the disassembly. //! @brief A custom CPU with a window that show it's registers and the disassembly.
class CPUDebug : public CPU::CPU, public QObject { class CPUDebug : public CPU::CPU, public QObject {
private: private:
@@ -47,6 +59,8 @@ namespace ComSquare::Debugger
Ui::CPUView _ui; Ui::CPUView _ui;
//! @brief The disassembly viewer's model. //! @brief The disassembly viewer's model.
DisassemblyModel _model; DisassemblyModel _model;
//! @brief The list of disassembled instructions to show on the debugger.
std::map<uint24_t, DisassembledInstruction> _disassembledInstructions;
//! @brief If this is set to true, the execution of the CPU will be paused. //! @brief If this is set to true, the execution of the CPU will be paused.
bool _isPaused = true; bool _isPaused = true;
//! @brief If this is set to true, the CPU will execute one instruction and pause itself. //! @brief If this is set to true, the CPU will execute one instruction and pause itself.
@@ -55,10 +69,10 @@ namespace ComSquare::Debugger
SNES &_snes; SNES &_snes;
//! @brief Reimplement the basic instruction execution method to log instructions inside the logger view. //! @brief Reimplement the basic instruction execution method to log instructions inside the logger view.
unsigned _executeInstruction(uint8_t opcode) override; unsigned _executeInstruction(uint8_t opcode) override;
//! @brief Get a printable string representing an instruction at the program counter given as parameter. //! @brief Parse the instruction at the program counter given to have human readable information.
std::string _getInstructionString(uint24_t pc); DisassembledInstruction _parseInstruction(uint24_t pc);
//! @brief Get the parameter of the instruction as an hexadecimal string. //! @brief Get the parameter of the instruction as an hexadecimal string.
std::string _getInstructionParameter(uint24_t pc); std::string _getInstructionParameter(ComSquare::CPU::Instruction &instruction, uint24_t pc);
//! @brief Get a printable string representing the flags. //! @brief Get a printable string representing the flags.
std::string _getFlagsString(); std::string _getFlagsString();
//! @brief Update the register's panel (accumulator, stack pointer...) //! @brief Update the register's panel (accumulator, stack pointer...)

View File

@@ -31,7 +31,7 @@ namespace ComSquare
{ {
unsigned cycleCount = this->cpu->update(); unsigned cycleCount = this->cpu->update();
this->ppu->update(cycleCount); this->ppu->update(cycleCount);
this->apu->update(cycleCount); // this->apu->update(cycleCount);
} }
void SNES::enableCPUDebugging(bool pause) void SNES::enableCPUDebugging(bool pause)
@@ -40,11 +40,13 @@ namespace ComSquare
if (this->cpu->isDebugger()) { if (this->cpu->isDebugger()) {
auto cpuDebug = std::static_pointer_cast<Debugger::CPUDebug>(this->cpu); auto cpuDebug = std::static_pointer_cast<Debugger::CPUDebug>(this->cpu);
cpuDebug->focus(); cpuDebug->focus();
if (pause)
cpuDebug->pause(); cpuDebug->pause();
} else } else
this->cpu = std::make_shared<Debugger::CPUDebug>(*this->cpu, *this); this->cpu = std::make_shared<Debugger::CPUDebug>(*this->cpu, *this);
#else #else
std::cerr << "Debugging features are not enabled. You can't enable the debugger." << std::endl; std::cerr << "Debugging features are not enabled. You can't enable the debugger." << std::endl;
(void)pause;
#endif #endif
} }

View File

@@ -15,7 +15,8 @@ using namespace ComSquare;
Test(SEP, setall) Test(SEP, setall)
{ {
Init() Init()
snes.cpu->SEP(0xFF); snes.wram->_data[0] = 0xFF;
snes.cpu->SEP(0x00);
auto data = snes.cpu->_registers.p.flags; auto data = snes.cpu->_registers.p.flags;
cr_assert_eq(data, 0xFF, "The flag should be 0xFF but it was %x", data); cr_assert_eq(data, 0xFF, "The flag should be 0xFF but it was %x", data);
} }
@@ -24,7 +25,8 @@ Test(SEP, setsome)
{ {
Init() Init()
snes.cpu->_registers.p.flags = 0b01000000; snes.cpu->_registers.p.flags = 0b01000000;
snes.cpu->SEP(0b10110101); snes.wram->_data[0] = 0b10110101;
snes.cpu->SEP(0x0);
auto data = snes.cpu->_registers.p.flags; auto data = snes.cpu->_registers.p.flags;
cr_assert_eq(data, 0b11110101, "The flag should be 245 but it was %i", data); cr_assert_eq(data, 0b11110101, "The flag should be 245 but it was %i", data);
} }
@@ -33,7 +35,8 @@ Test(REP, resetall)
{ {
Init() Init()
snes.cpu->_isEmulationMode = false; snes.cpu->_isEmulationMode = false;
snes.cpu->REP(0xFF); snes.wram->_data[0] = 0xFF;
snes.cpu->REP(0x00);
auto data = snes.cpu->_registers.p.flags; auto data = snes.cpu->_registers.p.flags;
cr_assert_eq(data, 0x00, "The flag should be 0x00 but it was %x", data); cr_assert_eq(data, 0x00, "The flag should be 0x00 but it was %x", data);
} }
@@ -43,7 +46,8 @@ Test(REP, resetsome)
Init() Init()
snes.cpu->_isEmulationMode = false; snes.cpu->_isEmulationMode = false;
snes.cpu->_registers.p.flags = 0b01000000; snes.cpu->_registers.p.flags = 0b01000000;
snes.cpu->REP(0b01000000); snes.wram->_data[0] = 0b01000000;
snes.cpu->REP(0x0);
auto data = snes.cpu->_registers.p.flags; auto data = snes.cpu->_registers.p.flags;
cr_assert_eq(data, 0x0, "The flag should be 0 but it was %x", data); cr_assert_eq(data, 0x0, "The flag should be 0 but it was %x", data);
} }
@@ -52,7 +56,8 @@ Test(REP, resetallEmulation)
{ {
Init() Init()
snes.cpu->_isEmulationMode = true; snes.cpu->_isEmulationMode = true;
snes.cpu->REP(0xFF); snes.wram->_data[0] = 0xFF;
snes.cpu->REP(0x00);
auto data = snes.cpu->_registers.p.flags; auto data = snes.cpu->_registers.p.flags;
cr_assert_eq(data, 0b00110000, "The flag should be 0b00110000 but it was %x", data); cr_assert_eq(data, 0b00110000, "The flag should be 0b00110000 but it was %x", data);
} }
@@ -62,7 +67,8 @@ Test(REP, resetsomeEmulation)
Init() Init()
snes.cpu->_isEmulationMode = true; snes.cpu->_isEmulationMode = true;
snes.cpu->_registers.p.flags = 0b01000101; snes.cpu->_registers.p.flags = 0b01000101;
snes.cpu->REP(0b01000001); snes.wram->_data[0] = 0b01000001;
snes.cpu->REP(0x0);
auto data = snes.cpu->_registers.p.flags; auto data = snes.cpu->_registers.p.flags;
cr_assert_eq(data, 0b00110100, "The flag should be 0b00110100 but it was %x", data); cr_assert_eq(data, 0b00110100, "The flag should be 0b00110100 but it was %x", data);
} }

View File

@@ -141,7 +141,7 @@
<enum>Qt::RightToLeft</enum> <enum>Qt::RightToLeft</enum>
</property> </property>
<property name="checkable"> <property name="checkable">
<bool>false</bool> <bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>