From 2c93d6e2f24e95e60b2757faa0e40bee4a49f44d Mon Sep 17 00:00:00 2001 From: OmniaX-Dev Date: Thu, 21 May 2026 15:16:20 +0200 Subject: [PATCH] Started work on CPU class --- src/hardware/CPU.cpp | 29 ++++++++++++++++++++++++++++- src/hardware/CPU.hpp | 15 +++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/hardware/CPU.cpp b/src/hardware/CPU.cpp index 432d7b0..8162db7 100644 --- a/src/hardware/CPU.cpp +++ b/src/hardware/CPU.cpp @@ -24,8 +24,35 @@ namespace dragon { namespace hw { - CPU::CPU(void) + CPU::CPU(BUS& bus) : m_bus(bus), m_mmu(m_bus, &m_accessMode) { } + + bool CPU::run(void) + { + while (!m_halted) + { + try + { + step(); // fetch + decode + execute one instruction + } + catch (const GuestException& e) + { + take_exception(e.cause, e.fault_addr); + } + } + return true; + } + + void CPU::take_exception(ExceptionCause cause, VirtualAddress fault_addr) + { + // m_epc = m_faulting_pc; // saved before/during the faulting instr + // m_cause = static_cast(cause); + // m_badaddr = fault_addr; + // m_estatus = pack_mode_and_ie(); // save mode/IE + // set_mode_supervisor(); + // set_interrupts_enabled(false); + // m_pc = m_iv; // jump to single entry point + } } } diff --git a/src/hardware/CPU.hpp b/src/hardware/CPU.hpp index 2069960..3163b98 100644 --- a/src/hardware/CPU.hpp +++ b/src/hardware/CPU.hpp @@ -20,18 +20,29 @@ #pragma once -// #include "../common/Data.hpp" +#include "../common/Data.hpp" +#include "MMU.hpp" namespace dragon { namespace hw { + class BUS; class CPU { public: - CPU(void); + CPU(BUS& bus); + bool run(void); private: + void take_exception(ExceptionCause cause, VirtualAddress fault_addr); + void step(void); + + private: + bool m_halted { true }; + MMUAccessMode m_accessMode { MMUAccessMode::Privileged }; + BUS& m_bus; + MMU m_mmu; }; } }