From e35de357fad2eee85bd99d17ea18ce73b97b5866 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 8 May 2022 17:17:46 -0400 Subject: [PATCH] Route reads and writes through a common path. --- InstructionSets/M68k/Executor.hpp | 12 +++ .../Implementation/ExecutorImplementation.hpp | 93 ++++++++++--------- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/InstructionSets/M68k/Executor.hpp b/InstructionSets/M68k/Executor.hpp index 769a1951a..72e334961 100644 --- a/InstructionSets/M68k/Executor.hpp +++ b/InstructionSets/M68k/Executor.hpp @@ -18,6 +18,14 @@ namespace InstructionSet { namespace M68k { +enum class FunctionCode { + UserData = 0b001, + UserProgram = 0b010, + SupervisorData = 0b101, + SupervisorProgram = 0b110, + InterruptAcknowledge = 0b111, +}; + struct BusHandler { template void write(uint32_t address, IntT value); template IntT read(uint32_t address); @@ -84,7 +92,11 @@ template class Executor { void read(DataSize size, uint32_t address, CPU::SlicedInt32 &value); void write(DataSize size, uint32_t address, CPU::SlicedInt32 value); + template IntT read(uint32_t address); + template void write(uint32_t address, IntT value); + template IntT read_pc(); + uint32_t index_8bitdisplacement(); // Processor state. diff --git a/InstructionSets/M68k/Implementation/ExecutorImplementation.hpp b/InstructionSets/M68k/Implementation/ExecutorImplementation.hpp index 671583539..dfb4c5ef2 100644 --- a/InstructionSets/M68k/Implementation/ExecutorImplementation.hpp +++ b/InstructionSets/M68k/Implementation/ExecutorImplementation.hpp @@ -31,43 +31,44 @@ void Executor::reset() { did_update_status(); // Seed stack pointer and program counter. - sp.l = bus_handler_.template read(0); - program_counter_.l = bus_handler_.template read(4); + sp.l = read(0); + program_counter_.l = read(4); +} + +template +template +IntT Executor::read(uint32_t address) { + // TODO: check for an alignment exception, both here and in write. + return bus_handler_.template read(address); +} + +template +template +void Executor::write(uint32_t address, IntT value) { + bus_handler_.template write(address, value); } template void Executor::read(DataSize size, uint32_t address, CPU::SlicedInt32 &value) { switch(size) { - case DataSize::Byte: - value.b = bus_handler_.template read(address); - break; - case DataSize::Word: - value.w = bus_handler_.template read(address); - break; - case DataSize::LongWord: - value.l = bus_handler_.template read(address); - break; + case DataSize::Byte: value.b = read(address); break; + case DataSize::Word: value.w = read(address); break; + case DataSize::LongWord: value.l = read(address); break; } } template void Executor::write(DataSize size, uint32_t address, CPU::SlicedInt32 value) { switch(size) { - case DataSize::Byte: - bus_handler_.template write(address, value.b); - break; - case DataSize::Word: - bus_handler_.template write(address, value.w); - break; - case DataSize::LongWord: - bus_handler_.template write(address, value.l); - break; + case DataSize::Byte: write(address, value.b); break; + case DataSize::Word: write(address, value.w); break; + case DataSize::LongWord: write(address, value.l); break; } } template template IntT Executor::read_pc() { - const IntT result = bus_handler_.template read(program_counter_.l); + const IntT result = read(program_counter_.l); if constexpr (sizeof(IntT) == 4) { program_counter_.l += 4; @@ -339,12 +340,12 @@ void Executor::raise_exception(int index, bool use_current_in did_update_status(); // Push status and the program counter at instruction start. - bus_handler_.template write(sp.l - 4, use_current_instruction_pc ? instruction_address_ : program_counter_.l); - bus_handler_.template write(sp.l - 6, status); + write(sp.l - 4, use_current_instruction_pc ? instruction_address_ : program_counter_.l); + write(sp.l - 6, status); sp.l -= 6; // Fetch the new program counter. - program_counter_.l = bus_handler_.template read(address); + program_counter_.l = read(address); } template @@ -371,14 +372,14 @@ void Executor::add_pc(uint32_t offset) { template void Executor::bsr(uint32_t offset) { sp.l -= 4; - bus_handler_.template write(sp.l, program_counter_.l); + write(sp.l, program_counter_.l); program_counter_.l = instruction_address_ + offset; } template void Executor::jsr(uint32_t address) { sp.l -= 4; - bus_handler_.template write(sp.l, program_counter_.l); + write(sp.l, program_counter_.l); program_counter_.l = address; } @@ -387,7 +388,7 @@ void Executor::link(Preinstruction instruction, uint32_t offs const auto reg = 8 + instruction.reg<0>(); sp.l -= 4; - bus_handler_.template write(sp.l, Dn(reg).l); + write(sp.l, Dn(reg).l); Dn(reg) = sp; sp.l += offset; } @@ -395,33 +396,33 @@ void Executor::link(Preinstruction instruction, uint32_t offs template void Executor::unlink(uint32_t &address) { sp.l = address; - address = bus_handler_.template read(sp.l); + address = read(sp.l); sp.l += 4; } template void Executor::pea(uint32_t address) { sp.l -= 4; - bus_handler_.template write(sp.l, address); + write(sp.l, address); } template void Executor::rtr() { - status_.set_ccr(bus_handler_.template read(sp.l)); + status_.set_ccr(read(sp.l)); sp.l += 2; rts(); } template void Executor::rte() { - status_.set_status(bus_handler_.template read(sp.l)); + status_.set_status(read(sp.l)); sp.l += 2; rts(); } template void Executor::rts() { - program_counter_.l = bus_handler_.template read(sp.l); + program_counter_.l = read(sp.l); sp.l += 4; } @@ -429,8 +430,8 @@ template void Executor::tas(Preinstruction instruction, uint32_t address) { uint8_t value; if(instruction.mode<0>() != AddressingMode::DataRegisterDirect) { - value = bus_handler_.template read(address); - bus_handler_.template write(address, value | 0x80); + value = read(address); + write(address, value | 0x80); } else { value = uint8_t(address); Dn(instruction.reg<0>()).b = uint8_t(address | 0x80); @@ -450,36 +451,36 @@ void Executor::movep(Preinstruction instruction, uint32_t sou uint32_t address = dest; if constexpr (sizeof(IntT) == 4) { - bus_handler_.template write(address, uint8_t(reg >> 24)); + write(address, uint8_t(reg >> 24)); address += 2; - bus_handler_.template write(address, uint8_t(reg >> 16)); + write(address, uint8_t(reg >> 16)); address += 2; } - bus_handler_.template write(address, uint8_t(reg >> 8)); + write(address, uint8_t(reg >> 8)); address += 2; - bus_handler_.template write(address, uint8_t(reg)); + write(address, uint8_t(reg)); } else { // Move memory to register. uint32_t ® = Dn(instruction.reg<1>()).l; uint32_t address = source; if constexpr (sizeof(IntT) == 4) { - reg = bus_handler_.template read(address) << 24; + reg = read(address) << 24; address += 2; - reg |= bus_handler_.template read(address) << 16; + reg |= read(address) << 16; address += 2; } else { reg &= 0xffff0000; } - reg |= bus_handler_.template read(address) << 8; + reg |= read(address) << 8; address += 2; - reg |= bus_handler_.template read(address); + reg |= read(address); } } @@ -509,7 +510,7 @@ void Executor::movem_toM(Preinstruction instruction, uint32_t while(source) { if(source & 1) { address -= sizeof(IntT); - bus_handler_.template write(address, IntT(registers_[index].l)); + write(address, IntT(registers_[index].l)); } --index; source >>= 1; @@ -522,7 +523,7 @@ void Executor::movem_toM(Preinstruction instruction, uint32_t int index = 0; while(source) { if(source & 1) { - bus_handler_.template write(dest, IntT(registers_[index].l)); + write(dest, IntT(registers_[index].l)); dest += sizeof(IntT); } ++index; @@ -544,9 +545,9 @@ void Executor::movem_toR(Preinstruction instruction, uint32_t while(source) { if(source & 1) { if constexpr (sizeof(IntT) == 2) { - registers_[index].l = int16_t(bus_handler_.template read(dest)); + registers_[index].l = int16_t(read(dest)); } else { - registers_[index].l = bus_handler_.template read(dest); + registers_[index].l = read(dest); } dest += sizeof(IntT); }