--- nono/vm/scheduler.cpp 2026/04/29 17:04:29 1.1.1.2 +++ nono/vm/scheduler.cpp 2026/04/29 17:04:39 1.1.1.5 @@ -1,13 +1,17 @@ // // nono -// Copyright (C) 2017 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // +#include "scheduler.h" +#include "config.h" +#include "mainapp.h" #include "mfp.h" -#include "mpu.h" +#include "mpu680x0.h" +#include "mpu88xx0.h" +#include "mythread.h" #include "rtc.h" -#include "scheduler.h" -#include "configfile.h" #include "vm.h" // VM スケジューラは、仮想時間とイベントを管理する。 @@ -19,14 +23,13 @@ static void *scheduler_run(void *arg); -Scheduler *gScheduler; +std::unique_ptr gScheduler; // コンストラクタ Scheduler::Scheduler() { logname = "scheduler"; devname = "Scheduler"; - monitor.Init(68, 30); eventlist.clear(); } @@ -34,35 +37,46 @@ Scheduler::Scheduler() // デストラクタ Scheduler::~Scheduler() { + Terminate(); } // 初期化 // ここで mpu_clock が(非ゼロに)初期化されるとこれ以降 putlog() が使える。 -// そのため、Scheduler デバイスを必ずデバイスリストの先頭に置くことで、 -// これ以外のオブジェクトの Init() は putlog() を使ってよいとできる。 +// そのため、Scheduler デバイスをデバイスリストの先頭のほうに置くことで、 +// これ以降のオブジェクトの Init() は putlog() を使ってよいとできる。 +// BusErrDevice だけは Scheduler より先に作るが BusErrDevice に Init() は +// ないので (= 問題は起きないので)、よいことにする。 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->Find("mpu-clock"); + const std::string& val = item.AsString(); + if (val.empty()) { + item.Err(); + return false; } - new_clock = gConfig->ReadInt("mpu_clock", mpu_clock); - // ReadInt() は変数があって値が解釈できなければ 0 を返す - if (new_clock == 0) { - gConfig->Err("mpu_clock", "invalid number"); + // 文字列を 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; } - mpu_clock = new_clock; + // 設定の "mpu-clock" は MHz 単位だが、変数 mpu_clock は kHz 単位。 + mpu_clock = (int)(f * 1000); + // 1MHz 以下はエラー。実際 10MHz 以下でもいい気がするけど + if (mpu_clock < 1000) { + item.Err(); + return false; + } + + // 起動時引数指定の高速モード + 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; } @@ -80,8 +94,7 @@ Scheduler::Run() void * scheduler_run(void *arg) { - pthread_setname_np("VM"); - pthread_detach(pthread_self()); + PTHREAD_SETNAME("VM"); gScheduler->ThreadRun(); return NULL; @@ -92,12 +105,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 +133,6 @@ Scheduler::ThreadRun() cycle = INT64_MAX; mode = 0; for (;;) { - uint32 req; req = atomic_reqflag.exchange(0); if (req) { if ((req & REQ_SYNC)) { @@ -163,7 +187,7 @@ Scheduler::ThreadRun() } // 電源オン要求は来ないはず - assert(req == 0); + assertmsg(req == 0, "req=$%x", req); } // RTC は常にホスト時刻で動いており @@ -203,7 +227,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); // 同時刻のイベントがあるかも知れないのでなくなるまで調べる @@ -237,9 +261,10 @@ Scheduler::ThreadRun() // CPU の STOP 状態をこっちのモードに反映。 // このために SCHED_STOP と CPU_REQ_STOP は同じビット位置にしてある。 + // CPU の HALT はスケジューラ的には STOP と同じ扱いでいい。 if (((mode ^ outer) & SCHED_STOP)) { // 状態変更要求 - if ((outer & CPU_REQ_STOP)) { + if ((outer & (CPU_REQ_STOP | CPU_REQ_HALT)) != 0) { atomic_reqflag |= REQ_STOP; } else { atomic_reqflag |= REQ_RUN; @@ -248,11 +273,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 +296,12 @@ Scheduler::SetFullSpeed(bool enable) } } -// イベントをスケジューラに登録する。 +// 指定のイベントを開始する。 // すでに同イベントが登録されている場合は古いイベントを削除してから // 新しいイベントを再登録となる。 // イベントはワンショットのみ。 void -Scheduler::AddEvent(Event *newev) +Scheduler::StartEvent(Event *newev) { bool inserted = false; bool updated = false; @@ -311,17 +340,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 +366,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()); } } @@ -390,8 +419,17 @@ TimeToStr(uint64 t) return std::string(buf, p - buf); } -bool -Scheduler::MonitorUpdate() +// モニターサイズ取得 +nnSize +Scheduler::GetMonitorSize() +{ + // 本当は Init() 完了時点でイベント個数は確定しているはずだけど、 + // そのタイミングで一度だけ呼ばれるフックが今の所ない。 + return nnSize(68, 11 + gEvents.size()); +} + +void +Scheduler::MonitorUpdate(TextScreen& monitor) { int x, y; @@ -430,16 +468,16 @@ Scheduler::MonitorUpdate() // 3.123'456'789 3.123'456'789 0123456789012345678901234 $01234567 x = 0; y++; - monitor.Print(x, y, "Set Time"); - monitor.Print(x + 16, y, "Remain Time"); - monitor.Print(x + 32, y, "Description"); - monitor.Print(x + 58, y, "Code"); + monitor.Puts(x, y, "Set Time"); + monitor.Puts(x + 16, y, "Remain Time"); + monitor.Puts(x + 32, y, "Description"); + monitor.Puts(x + 58, y, "Code"); y++; for (const auto& e : gEvents) { uint64 rem; - uint attr; + TA attr; if (e->active) { - attr = 0; + attr = TA::Normal; rem = Cycle2Vtime(e->cycle - cycle); } else { attr = TA::Disable; @@ -458,6 +496,4 @@ Scheduler::MonitorUpdate() e->GetName().c_str(), e->code); } - - return true; }