--- nono/vm/mpu.cpp 2026/04/29 17:04:45 1.1.1.5 +++ nono/vm/mpu.cpp 2026/04/29 17:05:41 1.1.1.16 @@ -4,25 +4,39 @@ // Licensed under nono-license.txt // -// MPU 共通部 +// +// MPU (共通部分) +// + +// Device +// | +-----------+ +// +--| MPUDevice | (全 MPU の共通部) +// +-----------+ +// | +// | +---------------+ +// +--| MainMPUDevice | (680x0/88xx0 の共通部) +// | +---------------+ +// | | +// | +-- MPU680x0Device +// | +-- MPU88xx0Device +// | +// +-- MPU64180Device #include "mpu.h" #include "config.h" +#include "debugger.h" +#include "mainbus.h" +#include "scheduler.h" +#include "syncer.h" -std::unique_ptr gMPU; +// +// 全 MPU 共通 +// // コンストラクタ -MPUDevice::MPUDevice() +MPUDevice::MPUDevice(uint objid_) + : inherited(objid_) { - logname = "mpu"; - devname = "MPU"; - - // リセット例外を起こすイベント。 - // ハンドラは MPU ごとに異なるので継承クラス側で用意している。 - // (この時点ではまだ Scheduler インスタンスは存在してないが、イベントを - // 実際に Scheduler に登録するのは ResetHard() の時点なので問題ない) - event.dev = this; - event.SetName("MPU"); } // デストラクタ @@ -30,41 +44,114 @@ MPUDevice::~MPUDevice() { } +// 初期化 bool MPUDevice::Init() { - // MPU クロック - const ConfigItem& item = gConfig->Find("mpu-clock"); - const std::string& val = item.AsString(); - if (val.empty()) { - item.Err(); + debugger = GetDebugger(); + + // パラメータは継承側でセットしている。 + scheduler->RegistEvent(exec_event); + + return true; +} + +void +MPUDevice::PowerOff() +{ + scheduler->StopEvent(exec_event); +} + + +// +// メイン MPU (680x0/88xx0) 共通部 +// + +// コンストラクタ +MainMPUDevice::MainMPUDevice() + : inherited(OBJ_MPU) +{ +} + +// デストラクタ +MainMPUDevice::~MainMPUDevice() +{ +} + +// 初期化 +bool +MainMPUDevice::Init() +{ + if (inherited::Init() == false) { return false; } - // 文字列を double に変換 - char *end; - errno = 0; - double f = strtod(val.c_str(), &end); - if (end == val.c_str() || end[0] != '\0' || errno == ERANGE) { + + mainbus = GetMainbusDevice(); + syncer = GetSyncer(); + + // MPU クロック。 + // 設定の "mpu-clock" は MHz 単位だが、変数 clock_khz は kHz 単位。 + int val; + const ConfigItem& item = gConfig->Find("mpu-clock"); + if (item.TryFixedDecimal(&val, 3) == false) { item.Err(); return false; } - // 設定の "mpu-clock" は MHz 単位だが、変数 clock_khz は kHz 単位。 - clock_khz = (int)(f * 1000); - // 1MHz 以下はエラー。実際 10MHz 以下でもいい気がするけど + clock_khz = (uint)val; + // 1MHz 未満はエラー。実際 10MHz 未満でもいい気がするけど if (clock_khz < 1000) { item.Err(); return false; } + clock_nsec = 1000 * 1000 / clock_khz; + if (clock_nsec < 1) { + item.Err("Clock speed too high"); + return false; + } + + scheduler->ConnectMessage(MessageID::MPU_TRACE_MAIN, this, + ToMessageCallback(&MainMPUDevice::TraceMessage)); + + exec_event.SetName("MPU Execute"); return true; } -// リセット +// MPU トレース状態設定要求メッセージ +void +MainMPUDevice::TraceMessage(MessageID msgid, uint32 arg) +{ + // リセット中は SetTrace しない + if (resetting) { + return; + } + + // デバッガから MPU のトレース状態を設定してくれと言われた + SetTrace((bool)arg); +} + void -MPUDevice::ResetHard() +MainMPUDevice::ChangeState(uint32 new_state) { - // 0 サイクル後にリセット例外を起こす - event.time = 0; - event.code = (uint32)-1; - event.Start(); + // new_mode (CPU_STATE_*) と + // RequestCPUMode() の引数 Syncer::SYNCMODE_CPU_* は + // 実は同じ値なのでそのまま渡してよいことにする。 + static_assert(CPU_STATE_NORMAL == Syncer::SYNCMODE_CPU_NORMAL, ""); + static_assert(CPU_STATE_STOP == Syncer::SYNCMODE_CPU_STOP, ""); + static_assert(CPU_STATE_HALT == Syncer::SYNCMODE_CPU_HALT, ""); + + if (cpu_state != new_state) { + cpu_state = new_state; + // スケジューラに通知 + syncer->RequestCPUMode(cpu_state); + } } + +// Round Mode (モニタ表示用) +/*static*/ const char * const +MainMPUDevice::rmstr[4] = { + "Near", + "Zero", + "Minus", + "Plus", +};