Move memory fns in Cpu

This commit is contained in:
scawful
2023-10-17 17:06:25 -04:00
parent 656c2fa5d6
commit 013a989928
2 changed files with 97 additions and 79 deletions

View File

@@ -9,66 +9,6 @@ namespace yaze {
namespace app {
namespace emu {
uint8_t CPU::ReadByte(uint16_t address) const {
auto value = memory.ReadByte(address);
return value;
}
uint16_t CPU::ReadWord(uint16_t address) const {
return memory.ReadWord(address);
}
uint32_t CPU::ReadWordLong(uint16_t address) const {
return memory.ReadWordLong(address);
}
void CPU::WriteByte(uint32_t address, uint8_t value) {
memory.WriteByte(address, value);
}
void CPU::WriteWord(uint32_t address, uint16_t value) {
memory.WriteWord(address, value);
}
uint8_t CPU::FetchByte() {
uint8_t byte = memory.ReadByte(PC); // Read a byte from memory at PC
PC++; // Increment the Program Counter
return byte;
}
uint16_t CPU::FetchWord() {
uint16_t value = memory.ReadWord(PC);
PC += 2;
return value;
}
uint32_t CPU::FetchLong() {
uint32_t value = memory.ReadWordLong(PC);
PC += 3;
return value;
}
int8_t CPU::FetchSignedByte() { return static_cast<int8_t>(FetchByte()); }
int16_t CPU::FetchSignedWord() {
auto offset = static_cast<int16_t>(FetchWord());
return offset;
}
uint8_t CPU::FetchByteDirectPage(uint8_t operand) {
uint16_t distance = D * 0x100;
// Calculate the effective address in the Direct Page
uint16_t effectiveAddress = operand + distance;
// Fetch the byte from memory
uint8_t fetchedByte = memory.ReadByte(effectiveAddress);
PC++; // Increment the Program Counter
return fetchedByte;
}
void CPU::Update() {
auto cycles_to_run = clock.GetCycleCount();
@@ -1174,8 +1114,23 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
std::cout << std::endl;
}
// Interrupt Vectors
// Emulation mode, e = 1 Native mode, e = 0
//
// 0xFFFE,FF - IRQ/BRK 0xFFEE,EF - IRQ
// 0xFFFC,FD - RESET
// 0xFFFA,FB - NMI 0xFFEA,EB - NMI
// 0xFFF8,F9 - ABORT 0xFFE8,E9 - ABORT
// 0xFFE6,E7 - BRK
// 0xFFF4,F5 - COP 0xFFE4,E5 - COP
void CPU::HandleInterrupts() {}
/**
* 65816 Instruction Set
*
* TODO: MVN, MVP, STP, WDM
*/
// ADC: Add with carry
void CPU::ADC(uint8_t operand) {
bool C = GetCarryFlag();
@@ -1572,8 +1527,23 @@ void CPU::LSR(uint16_t address) {
SetZeroFlag(value == 0);
}
// MVN: Move negative ```
// MVP: Move positive ```
// MVN: Block Move Next
void CPU::MVN(uint16_t source, uint16_t dest, uint16_t length) {
for (uint16_t i = 0; i < length; i++) {
memory.WriteByte(dest, memory.ReadByte(source));
source++;
dest++;
}
}
// MVP: Block Move Previous
void CPU::MVP(uint16_t source, uint16_t dest, uint16_t length) {
for (uint16_t i = 0; i < length; i++) {
memory.WriteByte(dest, memory.ReadByte(source));
source--;
dest--;
}
}
// NOP: No operation
void CPU::NOP() {

View File

@@ -81,14 +81,6 @@ class CPU : public Memory, public Loggable {
memory.ClearMemory();
}
uint8_t FetchByte();
uint16_t FetchWord();
uint32_t FetchLong();
int8_t FetchSignedByte();
int16_t FetchSignedWord();
uint8_t FetchByteDirectPage(uint8_t operand);
void Update();
void ExecuteInstruction(uint8_t opcode);
void HandleInterrupts();
@@ -640,14 +632,74 @@ class CPU : public Memory, public Loggable {
// XCE: Exchange carry and emulation bits
void XCE();
uint8_t ReadByte(uint16_t address) const override;
uint16_t ReadWord(uint16_t address) const override;
uint32_t ReadWordLong(uint16_t address) const override;
void WriteByte(uint32_t address, uint8_t value) override;
void WriteWord(uint32_t address, uint16_t value) override;
// Memory access routines
uint8_t ReadByte(uint16_t address) const override {
auto value = memory.ReadByte(address);
return value;
}
uint16_t ReadWord(uint16_t address) const override {
return memory.ReadWord(address);
}
uint32_t ReadWordLong(uint16_t address) const override {
return memory.ReadWordLong(address);
}
std::vector<uint8_t> ReadByteVector(uint16_t address,
uint16_t size) const override {
return memory.ReadByteVector(address, size);
}
void WriteByte(uint32_t address, uint8_t value) override {
memory.WriteByte(address, value);
}
void WriteWord(uint32_t address, uint16_t value) override {
memory.WriteWord(address, value);
}
uint8_t FetchByte() {
uint8_t byte = memory.ReadByte(PC); // Read a byte from memory at PC
PC++; // Increment the Program Counter
return byte;
}
uint16_t FetchWord() {
uint16_t value = memory.ReadWord(PC);
PC += 2;
return value;
}
uint32_t FetchLong() {
uint32_t value = memory.ReadWordLong(PC);
PC += 3;
return value;
}
int8_t FetchSignedByte() { return static_cast<int8_t>(FetchByte()); }
int16_t FetchSignedWord() {
auto offset = static_cast<int16_t>(FetchWord());
return offset;
}
uint8_t FetchByteDirectPage(uint8_t operand) {
uint16_t distance = D * 0x100;
// Calculate the effective address in the Direct Page
uint16_t effectiveAddress = operand + distance;
// Fetch the byte from memory
uint8_t fetchedByte = memory.ReadByte(effectiveAddress);
PC++; // Increment the Program Counter
return fetchedByte;
}
void SetMemory(const std::vector<uint8_t>& data) override {
memory.SetMemory(data);
}
int16_t SP() const override { return memory.SP(); }
void SetSP(int16_t value) override { memory.SetSP(value); }
void UpdateClock(int delta_time) { clock.UpdateClock(delta_time); }
@@ -683,10 +735,6 @@ class CPU : public Memory, public Loggable {
bool GetFlag(uint8_t mask) const { return (status & mask) != 0; }
// Appease the C++ Gods...
std::vector<uint8_t> ReadByteVector(uint16_t address,
uint16_t size) const override {
return memory.ReadByteVector(address, size);
}
void PushByte(uint8_t value) override { memory.PushByte(value); }
void PushWord(uint16_t value) override { memory.PushWord(value); }
uint8_t PopByte() override { return memory.PopByte(); }