--- nono/vm/mpu.h 2026/04/29 17:04:42 1.1.1.4 +++ nono/vm/mpu.h 2026/04/29 17:05:59 1.1.1.18 @@ -4,51 +4,118 @@ // Licensed under nono-license.txt // +// // MPU 共通部 +// #pragma once #include "device.h" +#include "message.h" #include "event.h" +#include +class Debugger; +class MainbusDevice; +class Syncer; + +// 全 MPU の共通部 class MPUDevice : public Device { using inherited = Device; public: - MPUDevice(); + explicit MPUDevice(uint objid_); ~MPUDevice() override; - virtual void ResetHard() override; + bool Init() override; + void ResetHard(bool poweron) override; + void PowerOff() override; - // MPU を request サイクル分実行する - virtual uint32 Run(uint64 request) = 0; + // 現在実行中の命令の PC を取得 + virtual uint32 GetPPC() const = 0; - // 基準サイクル数を取得 - virtual uint64 total_cycle() const = 0; + // 最後に発生した例外ベクタを取得し、同時にクリアする + int32 GetAndResetLastVector(); - // MPU の処理をこの命令で中断させる。 - // スケジューラのイベントリストに追加が発生した時に VM 側から - // MPU へ処理の中断を指示する。 - virtual void Release() = 0; + // exec イベントの回数 + uint64 GetExecCount() const { return exec_event->count; } - // 現在実行中の命令の PC を取得 - virtual uint32 GetPPC() const = 0; + protected: + // 1命令実行イベント + Event *exec_event {}; - // 現在アクセス中の論理アドレス、物理アドレスを取得。 - // Laddr == Paddr が MMU オフなのか PA=VA で運用中なのかの区別は - // ここでは付かない。 - virtual uint32 GetLaddr() const = 0; - virtual uint32 GetPaddr() const = 0; + Debugger *debugger {}; + + // 最後に発生した例外ベクタ。起きてなければ -1。 + int32 last_vector {}; +}; + +// メイン MPU (680x0/88xxx0) 共通部 +class MainMPUDevice : public MPUDevice +{ + using inherited = MPUDevice; + protected: + // CPU の実行状態 + static const uint32 CPU_STATE_NORMAL = 0; + static const uint32 CPU_STATE_STOP = 1; + static const uint32 CPU_STATE_HALT = 2; + + public: + MainMPUDevice(); + ~MainMPUDevice() override; - // アクセスウェイトを加算 - virtual void AddCycle(uint64 cycle) = 0; + bool Init() override; - // 割り込み発生を MPU に通知 (オートベクタ) - virtual void Interrupt(Object *src, int level) = 0; + // 現在の VBR を取得 + virtual uint32 GetVBR() const = 0; + + // CPU の実行状態 (uint32) を返す。 + uint32 GetState() const noexcept { return cpu_state; } + + // MPU 名を取得する。("MC68030" みたいなやつ、ログ名とは別) + virtual const char *GetMPUName() const = 0; + + // MPU クロック [kHz] を取得する。 + uint GetClockSpeed() const { return clock_khz; } + + // MPU クロック [tsec] を取得する。 + uint64 GetClock_tsec() const { return clock_tsec; } + + // 割り込み発生を MPU に通知 + virtual void Interrupt(int level) = 0; + + // デバイスが CI (キャッシュ禁止) をセットする。 + virtual void SetCI() = 0; + + // 例外カウンタ + std::vector excep_counter {}; protected: - // リセット例外を起こすイベント - Event reset_event {}; + void TraceMessage(MessageID, uint32); + virtual void SetTrace(bool enable) = 0; + + // CPU の状態 + void ChangeState(uint32 new_state); + + // CPU の実行状態 + uint32 cpu_state {}; + + uint clock_khz {}; // MPU クロック [kHz] + uint64 clock_tsec {}; // MPU クロック [tsec] + + bool resetting {}; // リセット処理中フラグ + + // キャッシュを強制的に無効にする。テスト用。 + bool force_disable_dcache {}; + bool force_disable_icache {}; + + MainbusDevice *mainbus {}; + Syncer *syncer {}; + + // Round Mode (モニタ表示用) + static const char * const rmstr[4]; }; -extern std::unique_ptr gMPU; +inline MainMPUDevice *GetMPUDevice() { + return Object::GetObject(OBJ_MPU); +}