Refactor: ModRM Handler replaced with compile-time lookup table.

Started refactoring to a library-based model to easily integrate
comprehensive unit testing. Added CPU exception handling system for
the GUI. Non-critical exceptions (though critical to execution of
emulated application) will not shutdown the application in the future
releases. Refactored Userspace::Run() logic so that the GUI-controls
reflect emulator behavior.
This commit is contained in:
0x221E
2026-02-10 14:57:55 +01:00
parent 3e1e19023d
commit 9b250a6270
22 changed files with 377 additions and 137 deletions

View File

@@ -0,0 +1,76 @@
#include "InstructionModifierLookup.h"
#include "CPUContext.h"
#include "Instruction.h"
#include "Bus.h"
#include <array>
#include <cstdint>
#include <utility>
#include <cstring>
#include <iostream>
void Mod32_SIB(CPUContext &cc) {
std::cout << "[Mod32_SIB] SIB byte encountered." << std::endl;
}
void Mod32_LR(CPUContext& cc) {
std::cout << "[Mod32_LR] Executed." << std::endl;
if (cc.m_Instruction.m_ModRM.m_SIB) {
uint32_t src = cc.m_Bus->AccessX<uint32_t>(cc.m_InstructionPointer + cc.m_Instruction.m_Length);
std::memcpy(&cc.m_Instruction.m_Displacement, &src, 4);
Mod32_SIB(cc);
}
}
void Mod32_DISP32(CPUContext& cc) {
uint32_t src = cc.m_Bus->AccessX<uint32_t>(cc.m_InstructionPointer + cc.m_Instruction.m_Length);
std::memcpy(&cc.m_Instruction.m_Displacement, &src, 4);
cc.m_Instruction.m_Length += 4;
if (cc.m_Instruction.m_ModRM.m_SIB)
Mod32_SIB(cc);
}
template<uint8_t RM>
void Mod32_LRDISP32(CPUContext& cc) {
uint32_t disp = cc.m_Registers[RM] + cc.m_Bus->AccessX<uint32_t>(cc.m_Instruction.m_Length);
std::memcpy(&cc.m_Instruction.m_Displacement, &disp, 4);
cc.m_Instruction.m_Length += 4;
if (cc.m_Instruction.m_ModRM.m_SIB)
Mod32_SIB(cc);
}
template<uint8_t RM>
void Mod32_LRDISP8(CPUContext& cc) {
uint32_t disp = cc.m_Registers[RM] + cc.m_Bus->AccessX<uint8_t>(cc.m_Instruction.m_Length);
cc.m_Instruction.m_Displacement[0] = disp;
cc.m_Instruction.m_Length += 1;
if (cc.m_Instruction.m_ModRM.m_SIB)
Mod32_SIB(cc);
}
void Mod32_R(CPUContext& cc) {
std::cout << "[Mod32_R] Executed." << std::endl;
}
template<uint8_t...Is>
static constexpr std::array<ModifierEntry, 256> GenerateModRM32(std::integer_sequence<uint8_t, Is...>) {
return std::array<ModifierEntry, 256>{
([]<uint8_t I>() {
constexpr auto modrm = x86::ProcessMODRM(I);
switch (modrm.m_State) {
case x86::ModRMState::LR: return Mod32_LR;
case x86::ModRMState::LR_DISP32: return Mod32_LRDISP32<modrm.m_Rm>;
case x86::ModRMState::LR_DISP8: return Mod32_LRDISP8<modrm.m_Rm>;
case x86::ModRMState::DISP32: return Mod32_DISP32;
case x86::ModRMState::R: return Mod32_R;
}
}.template operator()<Is>())... // provide the lambda with a proper template argument
};
}
static constexpr std::array<ModifierEntry, 256> s_ModRM32 = GenerateModRM32(std::make_integer_sequence<uint8_t, 255>{});
std::array<ModifierEntry, 256> GetMod32Table() { return s_ModRM32; }