Add JMP Absolute, JMP Indirect, JSR Absolute, JSL_AbsoluteLong

This commit is contained in:
scawful
2023-08-19 17:40:36 -04:00
parent d83d341b1e
commit f5c5c34b47
4 changed files with 119 additions and 6 deletions

View File

@@ -448,13 +448,13 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0x4C: // JMP Absolute
// JMP();
JMP(Absolute());
break;
case 0x5C: // JMP Absolute Long
// JMP();
break;
case 0x6C: // JMP Absolute Indirect
// JMP();
JMP(AbsoluteIndirect());
break;
case 0x7C: // JMP Absolute Indexed Indirect, X
// JMP();
@@ -464,11 +464,11 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0x20: // JSR Absolute
// JSR();
JSR(Absolute());
break;
case 0x22: // JSL Absolute Long
// JSL();
JSL(AbsoluteLong());
break;
case 0xFC: // JSR Absolute Indexed Indirect, X

View File

@@ -344,7 +344,6 @@ class CPU : public Memory {
// TSB: Test and set bits
// WAI: Wait for interrupt
// XBA: Exchange B and A accumulator
// XCE: Exchange carry and emulation
void ADC(uint8_t operand);
void AND(uint16_t address);
@@ -452,6 +451,26 @@ class CPU : public Memory {
}
}
// JMP: Jump to new address
void JMP(uint16_t address) {
PC = address; // Set program counter to the new address
}
// JSR: Jump to subroutine
void JSR(uint16_t address) {
PC -= 1; // Subtract 1 from program counter
memory.PushWord(PC); // Push the program counter onto the stack
PC = address; // Set program counter to the new address
}
// JSL: Jump to subroutine long
void JSL(uint32_t address) {
PC -= 1; // Subtract 1 from program counter
memory.PushLong(PC); // Push the program counter onto the stack as a long
// value (24 bits)
PC = address; // Set program counter to the new address
}
// Push Accumulator on Stack
void PHA() { memory.PushByte(A); }
@@ -620,6 +639,8 @@ class CPU : public Memory {
void PushWord(uint16_t value) override { memory.PushWord(value); }
uint8_t PopByte() override { return memory.PopByte(); }
uint16_t PopWord() override { return memory.PopWord(); }
void PushLong(uint32_t value) override { memory.PushLong(value); }
uint32_t PopLong() override { return memory.PopLong(); }
void ClearMemory() override { memory.ClearMemory(); }
void LoadData(const std::vector<uint8_t>& data) override {
memory.LoadData(data);

View File

@@ -38,6 +38,9 @@ class Memory {
virtual uint8_t PopByte() = 0;
virtual void PushWord(uint16_t value) = 0;
virtual uint16_t PopWord() = 0;
virtual void PushLong(uint32_t value) = 0;
virtual uint32_t PopLong() = 0;
virtual int16_t SP() const = 0;
virtual void SetSP(int16_t value) = 0;
@@ -74,7 +77,7 @@ class MemoryImpl : public Memory {
}
void WriteByte(uint32_t address, uint8_t value) override {
// uint32_t mapped_address = GetMappedAddress(address);
// uint32_t mapped_address = GetMappedAddress(address);
memory_[address] = value;
}
void WriteWord(uint32_t address, uint16_t value) override {
@@ -114,6 +117,20 @@ class MemoryImpl : public Memory {
return (static_cast<uint16_t>(high) << 8) | low;
}
void PushLong(uint32_t value) override {
PushByte(value >> 16);
PushByte(value >> 8);
PushByte(value & 0xFF);
}
uint32_t PopLong() override {
uint8_t low = PopByte();
uint8_t mid = PopByte();
uint8_t high = PopByte();
return (static_cast<uint32_t>(high) << 16) |
(static_cast<uint32_t>(mid) << 8) | low;
}
int16_t SP() const override { return SP_; }
void SetSP(int16_t value) override { SP_ = value; }