SayoriOS 0.3.3

cmos.c
См. документацию.
1
10 #include "common.h"
11 #include "io/ports.h"
12 #include "drv/cmos.h"
13
14 // FIXME: We need this?
15 #define CURRENT_YEAR 2022 // Change this each year!
16
17 int32_t century_register = 0x00; // Set by ACPI table parsing code if possible
18
19 unsigned char cmos_second;
20 unsigned char cmos_minute;
21 unsigned char cmos_hour;
22 unsigned char cmos_day;
23 unsigned char cmos_month;
24 unsigned short cmos_year;
25 unsigned char cmos_century;
26
27 unsigned char last_second;
28 unsigned char last_minute;
29 unsigned char last_hour;
30 unsigned char last_day;
31 unsigned char last_month;
32 unsigned char last_year;
33 unsigned char last_century;
34 unsigned char registerB;
35
36 enum {
37 cmos_address = 0x70,
38 cmos_data = 0x71
39 };
40
46 int32_t get_update_in_progress_flag() {
47 outb(cmos_address, 0x0A);
48 return (inb(cmos_data) & 0x80);
49 }
50
54 unsigned char get_RTC_register(int32_t reg) {
55 outb(cmos_address, reg);
56 return inb(cmos_data);
57 }
58
62 void read_rtc() {
63 while (get_update_in_progress_flag()); // Make sure an update isn't in progress
64
65 cmos_second = get_RTC_register(0x00);
66 cmos_minute = get_RTC_register(0x02);
67 cmos_hour = get_RTC_register(0x04);
68 cmos_day = get_RTC_register(0x07);
69 cmos_month = get_RTC_register(0x08);
70 cmos_year = get_RTC_register(0x09);
71
72 if(century_register != 0) {
73 cmos_century = get_RTC_register(century_register);
74 }
75
76 do {
77 last_second = cmos_second;
78 last_minute = cmos_minute;
79 last_hour = cmos_hour;
80 last_day = cmos_day;
81 last_month = cmos_month;
82 last_year = cmos_year;
83 last_century = cmos_century;
84
85 while (get_update_in_progress_flag()); // Make sure an update isn't in progress
86
87 cmos_second = get_RTC_register(0x00);
88 cmos_minute = get_RTC_register(0x02);
89 cmos_hour = get_RTC_register(0x04);
90 cmos_day = get_RTC_register(0x07);
91 cmos_month = get_RTC_register(0x08);
92 cmos_year = get_RTC_register(0x09);
93 if(century_register != 0) {
94 cmos_century = get_RTC_register(century_register);
95 }
96 } while( (last_second != cmos_second) || (last_minute != cmos_minute) || (last_hour != cmos_hour) ||
97 (last_day != cmos_day) || (last_month != cmos_month) || (last_year != cmos_year) ||
98 (last_century != cmos_century) );
99
100 registerB = get_RTC_register(0x0B);
101
102 // Convert BCD to binary values if necessary
103
104 if (!(registerB & 0x04)) {
105 cmos_second = (cmos_second & 0x0F) + ((cmos_second / 16) * 10);
106 cmos_minute = (cmos_minute & 0x0F) + ((cmos_minute / 16) * 10);
107 cmos_hour = ( (cmos_hour & 0x0F) + (((cmos_hour & 0x70) / 16) * 10) ) | (cmos_hour & 0x80);
108 cmos_day = (cmos_day & 0x0F) + ((cmos_day / 16) * 10);
109 cmos_month = (cmos_month & 0x0F) + ((cmos_month / 16) * 10);
110 cmos_year = (cmos_year & 0x0F) + ((cmos_year / 16) * 10);
111 if(century_register != 0) {
112 cmos_century = (cmos_century & 0x0F) + ((cmos_century / 16) * 10);
113 }
114 }
115
116 // Convert 12-hour clock to 24-hour clock if necessary
117
118 if (!(registerB & 0x02) && (cmos_hour & 0x80)) {
119 cmos_hour = ((cmos_hour & 0x7F) + 12) % 24;
120 }
121
122 // Calculate the full (4-digit) year
123
124 if(century_register != 0) {
125 cmos_year += cmos_century * 100;
126 } else {
127 cmos_year += (CURRENT_YEAR / 100) * 100;
128 if(cmos_year < CURRENT_YEAR) cmos_year += 100;
129 }
130 }
131
137 int isleap(int year) {
138 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
139 }
140
144 sayori_time_t get_time() {
145 read_rtc();
146 struct sayori_time time = {
147 cmos_second, cmos_minute, cmos_hour, cmos_day, cmos_month, cmos_year, cmos_century
148 };
149 return time;
150 }
isleap
int isleap(int year)
Это високосный год?
Definition: cmos.c:137
read_rtc
void read_rtc()
Считывает время с CMOS.
Definition: cmos.c:62
get_RTC_register
unsigned char get_RTC_register(int32_t reg)
Получает регистр CMOS.
Definition: cmos.c:54
get_time
sayori_time_t get_time()
Считывает время и передает в удобной структуре
Definition: cmos.c:144
get_update_in_progress_flag
int32_t get_update_in_progress_flag()
Проверяет CMOS на обновление
Definition: cmos.c:46
common.h
Основные определения ядра
sayori_time
Definition: cmos.h:5