--- nono/vm/rtc.cpp 2026/04/29 17:05:11 1.1.1.8 +++ nono/vm/rtc.cpp 2026/04/29 17:05:51 1.1.1.13 @@ -10,30 +10,27 @@ #include "rtc.h" #include "config.h" +#include "event.h" #include "scheduler.h" -#include "sync.h" - -// グローバル参照用 -RTCDevice *gRTC; +#include "syncer.h" // コンストラクタ -RTCDevice::RTCDevice(const std::string& objname_) - : inherited(objname_) +RTCDevice::RTCDevice() + : inherited(OBJ_RTC) { - event.Regist("RTC ClockIn"); - event.time = period; - event.func = ToEventCallback(&RTCDevice::Callback); } // デストラクタ RTCDevice::~RTCDevice() { - gRTC = NULL; } +// 初期化 bool RTCDevice::Init() { + syncer = GetSyncer(); + // TODO: use_localtime を設定可能にするべきかどうか // 時刻固定オプション (パフォーマンス測定用)。 @@ -43,17 +40,7 @@ RTCDevice::Init() // VM を作り、パフォーマンス測定を安定させる。 force_fixed = gConfig->Find(".rtc-force-fixed").AsInt(); - // クロックの同期方法 - const ConfigItem& item_sync = gConfig->Find("clock-sync"); - auto clock_sync = item_sync.AsString(); - if (clock_sync == "real") { - sync_rt = true; - } else if (clock_sync == "virtual") { - sync_rt = false; - } else { - item_sync.Err(); - return false; - } + sync_rt = syncer->GetSyncRealtime(); rtc_tm.tm_year = -1; rtc_tm.tm_mon = -1; @@ -93,6 +80,13 @@ RTCDevice::Init() rtc_tm.tm_sec = ss; } + auto evman = GetEventManager(); + event = evman->Regist(this, + ToEventCallback(&RTCDevice::Callback), + "RTC ClockIn"); + event->time = period; + + return true; } @@ -104,7 +98,6 @@ RTCDevice::StartTime() struct tm tm; if (force_fixed) { - putmsgn("force_fixed"); // 固定の時刻を設定 struct tm fixed { .tm_sec = 16, @@ -155,9 +148,9 @@ RTCDevice::StartTime() SetYear(tm.tm_year + 1900); SetLeap(tm.tm_year & 3); - stime = gSync->GetRealTime(); + stime = syncer->GetRealTime(); - gScheduler->StartEvent(event); + scheduler->StartEvent(event); } // デバッグオプションの日付文字列を解析する。 @@ -239,14 +232,14 @@ RTCDevice::ParseTime(const std::string& // イベントハンドラ void -RTCDevice::Callback(Event& ev) +RTCDevice::Callback(Event *ev) { stime += period; ClockIn(); if (sync_rt) { - gScheduler->StartRealtimeEvent(ev, stime, period); + scheduler->StartRealtimeEvent(ev, stime, period); } else { - gScheduler->StartEvent(ev); + scheduler->StartEvent(ev); } } @@ -258,7 +251,7 @@ RTCDevice::ClockIn_PowerOff() ClockIn(); // XXX RealTime ではなく HostTime? - uint64 rt = gSync->GetRealTime(); + uint64 rt = syncer->GetRealTime(); if (rt > stime) { // RTC が現実より遅れているので早回し return 0; @@ -375,11 +368,11 @@ RTCDevice::CountUpMin() } // 現在の月の最終日を返す (月の繰り上がり処理用) -int +uint RTCDevice::GetLastMday() const { // 下記の理由により 0 月が必要。値は適当。 - static const int mdays[] = { + static const uint mdays[] = { 30, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; @@ -388,8 +381,8 @@ RTCDevice::GetLastMday() const // 範囲外の値に対するロジックは違うだろうが規定されていないので // こっち都合で実装しておく。 // m は 0..12 - int m = GetMon() % 13; - int lastday = mdays[m]; + uint m = GetMon() % 13; + uint lastday = mdays[m]; if (m == 2 && GetLeap() == 0) { lastday++; }