--- nono/vm/rtc.cpp 2026/04/29 17:05:11 1.1.1.8 +++ nono/vm/rtc.cpp 2026/04/29 17:05:43 1.1.1.12 @@ -11,29 +11,25 @@ #include "rtc.h" #include "config.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 +39,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 +79,11 @@ RTCDevice::Init() rtc_tm.tm_sec = ss; } + event.func = ToEventCallback(&RTCDevice::Callback); + event.time = period; + event.SetName("RTC ClockIn"); + scheduler->RegistEvent(event); + return true; } @@ -104,7 +95,6 @@ RTCDevice::StartTime() struct tm tm; if (force_fixed) { - putmsgn("force_fixed"); // 固定の時刻を設定 struct tm fixed { .tm_sec = 16, @@ -155,9 +145,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); } // デバッグオプションの日付文字列を解析する。 @@ -244,9 +234,9 @@ 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 +248,7 @@ RTCDevice::ClockIn_PowerOff() ClockIn(); // XXX RealTime ではなく HostTime? - uint64 rt = gSync->GetRealTime(); + uint64 rt = syncer->GetRealTime(); if (rt > stime) { // RTC が現実より遅れているので早回し return 0; @@ -375,11 +365,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 +378,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++; }