--- nono/vm/scheduler.cpp 2026/04/29 17:04:42 1.1.1.6 +++ nono/vm/scheduler.cpp 2026/04/29 17:04:45 1.1.1.7 @@ -16,10 +16,8 @@ // VM スケジューラは、仮想時間とイベントを管理する。 // -// 仮想時間は MPU の起動時からのクロックサイクルの経過数で表すこととする。 -// このエミュレータはシングルプロセッサシステムのみがターゲットであり、 -// 実行中に動作クロックが変わることはないので、これで問題ない。 -// VM 界からは gMPU->total_cycle() で取得する。 +// 仮想時間は起動時からの nsec で表し、MPU デバイス (実際はその向こうの +// CPU コア) が管理している。VM 界からは gMPU->GetVirtTime() で取得する。 static void *scheduler_run(void *arg); @@ -41,36 +39,9 @@ Scheduler::~Scheduler() } // 初期化 -// ここで mpu_clock が(非ゼロに)初期化されるとこれ以降 putlog() が使える。 -// そのため、Scheduler デバイスをデバイスリストの先頭のほうに置くことで、 -// これ以降のオブジェクトの Init() は putlog() を使ってよいとできる。 -// BusErrDevice だけは Scheduler より先に作るが BusErrDevice に Init() は -// ないので (= 問題は起きないので)、よいことにする。 bool Scheduler::Init() { - const ConfigItem& item = gConfig->Find("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; - } - // 設定の "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); @@ -104,7 +75,6 @@ scheduler_run(void *arg) void Scheduler::ThreadRun() { - uint64 cycle; // 今回実行するサイクル数 uint32 req; // 最初の電源オンを待つ @@ -130,7 +100,6 @@ Scheduler::ThreadRun() rtimestart = GetRealTime(); rtc_last_clock = rtimestart; - cycle = 0; mode = 0; for (;;) { req = atomic_reqflag.exchange(0); @@ -167,7 +136,7 @@ Scheduler::ThreadRun() // 基準時刻をリセット rtimebase = ::GetRealTime(); - vtimebase = GetVirtTime(); + vtimebase = gMPU->GetVirtTime(); // 該当ビットを落としておく req &= ~REQ_MODEMASK; @@ -211,13 +180,22 @@ Scheduler::ThreadRun() } // イベントリストを調べ現時点から最も早く発生するイベントまでの必要 - // サイクル数を求めて、このサイクル数だけ MPU を駆動する。 + // 時間を求めて、この時間分だけ MPU を駆動する。 // MPU 実行中にデバイスアクセスなどによってスケジューラにイベントが // 登録されると、その命令の終了とともに処理を打ち切って戻ってくる。 - // また、電源オン(リセット)時には MPU デバイスが 0 サイクル目で - // MPU リセット例外イベントを起こすようになっている。 + // また、電源オン(リセット)時には MPU デバイスが 0nsec で MPU の + // リセット例外イベントを起こすようになっている。 + + // delta_vtime が今回進める仮想時間。 + // とりあえず初期値を仮置きしとくけど、イベントは通常 20msec 以内には + // 必ず起きるので (例えば LUNA の sysclk は 10 or 16 msec 周期)、 + // この初期値は実質上限値。20msec 程度より大きければいくらでもよくて + // 100msec とかでいいんだけど、二進数で置数しやすいよう + // 0x200'0000 (約33.5msec) にしておく。 + uint64 delta_vtime = 0x200'0000; // イベント + uint64 vtime = gMPU->GetVirtTime(); for (;;) { Event *e; // ロック区間 @@ -225,13 +203,12 @@ Scheduler::ThreadRun() std::lock_guard lock(evcs); if (eventlist.empty()) { - cycle = INT64_MAX; break; } e = eventlist.front(); - if (gMPU->total_cycle() < e->cycle) { + if (e->vtime > vtime) { // まだ時刻に到達していない - cycle = e->cycle - gMPU->total_cycle(); + delta_vtime = e->vtime - vtime; break; } @@ -247,24 +224,27 @@ Scheduler::ThreadRun() // 同時刻のイベントがあるかも知れないのでなくなるまで調べる } - // CPU 駆動 + // CPU を駆動 uint32 outer; - outer = gMPU->Run(cycle); + outer = gMPU->Run(delta_vtime); + + // CPU 駆動によって進んだ仮想時刻を再取得 + vtime = gMPU->GetVirtTime(); // 同期モードなら実時間調整。 // あるいは高速モードであってもストップ状態なら実時間駆動する。 if (mode != 0) { // 通常モード - uint64 vtime; - uint64 rtime; + uint64 vspan; + uint64 rspan; // 基準時からの経過時間 - vtime = GetVirtTime() - vtimebase; - rtime = GetRealTime() - rtimebase; + vspan = vtime - vtimebase; + rspan = GetRealTime() - rtimebase; - if (vtime > rtime) { + if (vspan > rspan) { // 仮想時間のほうが進んでいれば、スリープして待つ - uint64 diff = vtime - rtime; + uint64 diff = vspan - rspan; struct timespec ts; ts.tv_sec = diff / (1000 * 1000 * 1000); ts.tv_nsec = diff % (1000 * 1000 * 1000); @@ -323,8 +303,8 @@ Scheduler::StartEvent(Event *newev) bool inserted = false; bool updated = false; - // 相対 time から絶対 cycle を計算 - newev->cycle = gMPU->total_cycle() + Vtime2Cycle(newev->time); + // time は仮想相対時間 [nsec]、vtime は仮想絶対時刻 [nsec] + newev->vtime = gMPU->GetVirtTime() + newev->time; evcs.lock(); // すでにあれば削除 @@ -341,7 +321,7 @@ Scheduler::StartEvent(Event *newev) // ソートされているところに自身を挿入 for (auto it = eventlist.begin(); it != eventlist.end(); ++it) { Event *e = *it; - if (newev->cycle <= e->cycle) { + if (newev->vtime <= e->vtime) { // このイベントの前に入れる eventlist.insert(it, newev); inserted = true; @@ -454,27 +434,29 @@ Scheduler::MonitorUpdate(TextScreen& mon x = 0; y = 0; + int clock_khz = gMPU->GetClockSpeed(); + monitor.Print(0, y++, "Mode: %s %s", (mode & SCHED_SYNC) ? "Sync" : "Full", (mode & SCHED_STOP) ? "STOP" : "Run "); monitor.Print(0, y++, "Req: %08x", (uint32)atomic_reqflag); monitor.Print(0, y++, "MPU Speed: %d.%03dMHz", - (mpu_clock / 1000), (mpu_clock % 1000)); - uint64 vt = GetVirtTime(); - uint64 rt = GetRealTime(); - uint64 relvt = vt - vtimebase; - uint64 relrt = rt - rtimebase; + (clock_khz / 1000), (clock_khz % 1000)); + uint64 vtime = gMPU->GetVirtTime(); + uint64 rtime = GetRealTime(); + uint64 vspan = vtime - vtimebase; + uint64 rspan = rtime - rtimebase; // RealTime は実時間なのでスケジューラ開始からの時間に変換 - rt -= rtimestart; - monitor.Print(0, y++, "Total Real Time: %18s", TimeToStr(rt).c_str()); - monitor.Print(0, y++, " Virtual Time: %18s", TimeToStr(vt).c_str()); - double ratio = (double)vt / rt * 100; + rtime -= rtimestart; + monitor.Print(0, y++, "Total Real Time: %18s", TimeToStr(rtime).c_str()); + monitor.Print(0, y++, " Virtual Time: %18s", TimeToStr(vtime).c_str()); + double ratio = (double)vtime / rtime * 100; monitor.Print(0, y++, " Ratio : %3d.%01d%%", (int)ratio, ((int)(ratio * 10) % 10)); - monitor.Print(0, y++, "Moment Real Time: %18s", TimeToStr(relrt).c_str()); - monitor.Print(0, y++, " Virtual Time: %18s", TimeToStr(relvt).c_str()); + monitor.Print(0, y++, "Moment Real Time: %18s", TimeToStr(rspan).c_str()); + monitor.Print(0, y++, " Virtual Time: %18s", TimeToStr(vspan).c_str()); // XXX 移動平均をとる - double relratio = (double)relvt / relrt * 100; + double relratio = (double)vspan / rspan * 100; monitor.Print(0, y++, " Ratio : %3d.%01d%%", (int)relratio, ((int)(relratio * 10) % 10)); @@ -490,14 +472,13 @@ Scheduler::MonitorUpdate(TextScreen& mon monitor.Puts(x + 58, y, "Code"); y++; - uint64 cycle = gMPU->total_cycle(); for (const auto& e : gEvents) { uint64 rem; TA attr; if (e->active) { attr = TA::Normal; - if (e->cycle > cycle) { - rem = Cycle2Vtime(e->cycle - cycle); + if (e->vtime > vtime) { + rem = e->vtime - vtime; } else { // XXX 実際には起きないけど、この表示をするにあたって // スケジューラスレッドと一切調停していないので、仮想時刻が