--- nono/vm/scheduler.cpp 2026/04/29 17:04:29 1.1.1.2 +++ nono/vm/scheduler.cpp 2026/04/29 17:04:32 1.1.1.3 @@ -3,11 +3,12 @@ // Copyright (C) 2017 isaki@NetBSD.org // +#include "mainapp.h" #include "mfp.h" #include "mpu.h" #include "rtc.h" #include "scheduler.h" -#include "configfile.h" +#include "config.h" #include "vm.h" // VM スケジューラは、仮想時間とイベントを管理する。 @@ -19,7 +20,7 @@ static void *scheduler_run(void *arg); -Scheduler *gScheduler; +std::unique_ptr gScheduler; // コンストラクタ Scheduler::Scheduler() @@ -29,11 +30,27 @@ Scheduler::Scheduler() monitor.Init(68, 30); eventlist.clear(); + + // 機種ごとに MPU クロックの初期値は異なる。単位は MHz。 + int defval; + switch (gMainApp.GetVMType()) { + case VMTYPE_X68030: + case VMTYPE_LUNA88K: + defval = 25; + break; + case VMTYPE_LUNA: + defval = 20; + break; + default: + PANIC("unsupported vmtype"); + } + gConfig->Get("mpu-clock").SetDefault(defval); } // デストラクタ Scheduler::~Scheduler() { + Terminate(); } // 初期化 @@ -43,26 +60,34 @@ Scheduler::~Scheduler() bool Scheduler::Init() { - int new_clock; - - // 先に初期値を設定しておく - // MPU クロック数 [kHz] (25MHz = 25000) - if (gVM->GetType() == VM::TYPE_LUNA) { - mpu_clock = 20000; - } else { - mpu_clock = 25000; + const ConfigItem& item = gConfig->Get("mpu-clock"); + const std::string& val = item.AsString(); + if (val.empty()) { + item.Err(); + return false; + } + // 文字列を double に変換 + char *end; + errno = 0; + double f = strtod(val.c_str(), &end); + if (end == val.c_str() || end[0] != '\0' || errno == ERANGE) { + item.Err(); + return false; } - new_clock = gConfig->ReadInt("mpu_clock", mpu_clock); - // ReadInt() は変数があって値が解釈できなければ 0 を返す - if (new_clock == 0) { - gConfig->Err("mpu_clock", "invalid number"); + // 設定の "mpu-clock" は MHz 単位だが、変数 mpu_clock は kHz 単位。 + mpu_clock = (int)(f * 1000); + // 1MHz 以下はエラー。実際 10MHz 以下でもいい気がするけど + if (mpu_clock < 1000) { + item.Err(); return false; } - mpu_clock = new_clock; + + // 起動時引数指定の高速モード + SetFullSpeed(gMainApp.fast_mode); // スケジューラ(VM)スレッド起動 - pthread_t th; - pthread_create(&th, NULL, scheduler_run, NULL); + pthread_create(&thread, NULL, scheduler_run, NULL); + thread_created = true; return true; } @@ -81,7 +106,6 @@ void * scheduler_run(void *arg) { pthread_setname_np("VM"); - pthread_detach(pthread_self()); gScheduler->ThreadRun(); return NULL; @@ -92,12 +116,24 @@ void Scheduler::ThreadRun() { uint64 cycle; // 今回実行するサイクル数 + uint32 req; // 最初の電源オンを待つ - { + for (;;) { std::unique_lock lock(cvmtx); - cv.wait(lock, [&] { return (atomic_reqflag & REQ_POWER_ON); }); - atomic_reqflag &= ~REQ_POWER_ON; + cv.wait(lock, [&] { return (atomic_reqflag != 0); }); + req = atomic_reqflag.load(); + // 終了 + if ((req & REQ_EXIT)) { + return; + } + // REQ_POWER_ON なら他のフラグを書き戻して抜ける。 + // REQ_{SYNC|FAST} は以降のメインループで処理するため。 + if ((req & REQ_POWER_ON)) { + req &= ~REQ_POWER_ON; + atomic_reqflag.store(req); + break; + } } // ここからは電源オン @@ -108,7 +144,6 @@ Scheduler::ThreadRun() cycle = INT64_MAX; mode = 0; for (;;) { - uint32 req; req = atomic_reqflag.exchange(0); if (req) { if ((req & REQ_SYNC)) { @@ -163,7 +198,7 @@ Scheduler::ThreadRun() } // 電源オン要求は来ないはず - assert(req == 0); + assertmsg(req == 0, "req=$%x", req); } // RTC は常にホスト時刻で動いており @@ -203,7 +238,7 @@ Scheduler::ThreadRun() evcs.unlock(); // コールバック - putlog(2, "イベント '%s' 時刻到達", e->GetName().c_str()); + e->dev->putlog(3, "イベント '%s' 時刻到達", e->GetName().c_str()); ((e->dev)->*(e->func))(e->code); // 同時刻のイベントがあるかも知れないのでなくなるまで調べる @@ -248,11 +283,15 @@ Scheduler::ThreadRun() } } -// スレッド終了指示 +// 必要ならスレッドの終了を指示して完了まで待つ。 void Scheduler::Terminate() { - atomic_reqflag |= REQ_EXIT; + if (thread_created) { + atomic_reqflag |= REQ_EXIT; + pthread_join(thread, NULL); + thread_created = false; + } } // 動作モードを設定する。 @@ -267,12 +306,12 @@ Scheduler::SetFullSpeed(bool enable) } } -// イベントをスケジューラに登録する。 +// 指定のイベントを開始する。 // すでに同イベントが登録されている場合は古いイベントを削除してから // 新しいイベントを再登録となる。 // イベントはワンショットのみ。 void -Scheduler::AddEvent(Event *newev) +Scheduler::StartEvent(Event *newev) { bool inserted = false; bool updated = false; @@ -311,17 +350,17 @@ Scheduler::AddEvent(Event *newev) gMPU->Release(); - putlog(2, "イベント '%s' %s %d.%03d usec 後", + newev->dev->putlog(3, "イベント '%s' %s %d.%03d usec 後", newev->GetName().c_str(), - updated ? "更新" : "追加", + updated ? "更新" : "開始", (int)(newev->time / 1000), (int)(newev->time % 1000)); } -// 指定のイベントを削除する。 +// 指定のイベントを停止する。 // 指定されたイベントが登録されていなければ何もしない。 void -Scheduler::DelEvent(Event *event) +Scheduler::StopEvent(Event *event) { bool found = false; @@ -337,13 +376,13 @@ Scheduler::DelEvent(Event *event) } evcs.unlock(); - // イベントを削除した場合は MPU の実行中断はしなくてよい。 + // イベントを停止した場合は MPU の実行中断はしなくてよい。 // 1msec 後にイベントを追加した後、やっぱりそのイベントを取り消した // 場合 (LUNA の電源オフとか) はここで MPU 処理を中断するよりも // そのまま 1msec 走って問題ない。 if (found) { - putlog(2, "イベント '%s' 削除", event->GetName().c_str()); + event->dev->putlog(3, "イベント '%s' 停止", event->GetName().c_str()); } }