📄 update.cc
字号:
#include <inttypes.hh>#include "rtc.hh"// Returns 1 if y is a leap year, 0 otherwise. Taken directly from the public// domain code in 4.4BSD-Lite2.inline boolis_leap(int y){ return ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0));}// Update the SRAM registers using the current value of the clock.voidMT48Tx2::update_read(Clock *clock){ UInt8 month_length[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; ClockValue now, seconds, minutes, hour, day; ClockValue weekday, date, month, year; now = clock->nanoseconds(*clock) - initial_time; seconds = initial_seconds + (now / 1000 / 1000 / 1000); minutes = initial_minutes + (seconds / 60); hour = initial_hour + (minutes / 60); weekday = initial_weekday + (hour / 24); seconds %= 60; minutes %= 60; hour %= 24; // The rest is a legendary mess. date = initial_date; month = initial_month; year = initial_year; for (day = hour / 24; day; --day) { if (++date > month_length[is_leap(year)][month]) { date = 1; if (++month > 12) { month = 1; if (++year > 99) { year = 0; } } } } data[SECONDS] = ((seconds / 10) << 4) + seconds % 10; data[MINUTES] = ((minutes / 10) << 4) + minutes % 10; data[HOUR] = ((hour / 10) << 4) + hour % 10; data[WEEKDAY] = (weekday - 1) % 7 + 1; data[DATE] = ((date / 10) << 4) + date % 10; data[MONTH] = ((month / 10) << 4) + month % 10; data[YEAR] = ((year / 10) << 4) + year % 10;}// Update the timer based on the current values of time registers. This is// done by storing the start time and the corresponding wall time in// nanoseconds, from which update_read() can compute the current time on// request.voidMT48Tx2::update_write(Clock *clock){ // Wall time. initial_time = clock->nanoseconds(*clock); // Start time. initial_seconds = bits((UInt8)data[SECONDS], 3, 0) + bits((UInt8)data[SECONDS], 6, 4) * 10; initial_minutes = bits((UInt8)data[MINUTES], 3, 0) + bits((UInt8)data[SECONDS], 6, 4) * 10; initial_hour = bits((UInt8)data[HOUR], 3, 0) + bits((UInt8)data[SECONDS], 5, 4) * 10; initial_weekday = bits((UInt8)data[WEEKDAY], 2, 0); initial_date = bits((UInt8)data[DATE], 3, 0) + bits((UInt8)data[DATE], 5, 4) * 10; initial_month = bits((UInt8)data[MONTH], 3, 0) + bits((UInt8)data[MONTH], 4, 4) * 10; initial_year = bits((UInt8)data[YEAR], 3, 0) + bits((UInt8)data[YEAR], 7, 4) * 10;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -