-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
184 lines (156 loc) · 6.04 KB
/
time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stddef.h>
#include <stdint.h>
#include "time.h"
#include "cmos.h"
#include "acpi.h"
#include "heap.h"
#include "shell.h"
#include "clib/stdio.h"
// Funkcje statyczne
static uint8_t convert_bcd_to_bin(uint8_t bcd);
static uint8_t convert_hour_to_24_format(uint8_t hour12, uint8_t pm);
static void convert_time_to_bin(struct time_t *time_bcd);
static uint8_t compare_time(struct time_t *t1, struct time_t *t2);
static inline void __get_time(struct time_t *time);
static uint32_t calc_full_year_v1(uint8_t year);
static uint32_t calc_full_year_v2(uint8_t year);
static void time_command(const char* tokens, uint32_t tokens_count);
// Nazwy miesięcy i dni tygodnia
const char *months_names[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Noveber", "December"};
const char *weekdays_names[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Po inicjalizacji zawiera informacje o obcności rejestru wieku - Century
uint8_t century_register = 0;
// Po inicjalizacji zawiera informacje o formacje czasu
uint8_t binary_mode = 0;
uint8_t hour_format_24 = 0;
// Inicjalizacja
void time_initialize(void)
{
printf("Time initialization\n");
// Sprawdza obecność rejestru wieku
struct FADT *fadt = (struct FADT*)find_table("FACP");
if(fadt == NULL) century_register = 0;
else century_register = fadt->Century;
// Uzupełnia informacje o formacie godziny
uint8_t status = cmos_read_register(CMOS_REGISTER_STATUS_B);
if(status & STATUS_REGISTERB_BINARY_MODE) binary_mode = 1;
if(status & STATUS_REGISTERB_24_HOUR_FORMAT) hour_format_24 = 1;
register_command("time", "Display current time", time_command);
printf("\n______________Current_Time______________\n");
struct time_t *time = NULL;
get_time(time);
time_display(time);
printf("\n\n");
}
// Wypełnia strukturę aktualnym czasem
void get_time(struct time_t *time)
{
struct time_t time1, time2;
// Nie mamy pewności czy nie trwa aktualizacja czasu
// Trzeba się upewnić czy odczytujemy poprawny czas
do
{
__get_time(&time1);
__get_time(&time2);
} while(compare_time(&time1, &time2));
// Zapisanie i usunięcie bitu pory dnia AM/PM
uint8_t pm = time1.hours & HOURS_AM_PM;
time1.hours = time1.hours & HOURS_HOURS;
// Ewentualna konwersja do liczb binarnych
if(!binary_mode) convert_time_to_bin(&time1);
// Obliczenie pełnego roku
if(century_register) time1.year = calc_full_year_v2(time1.year);
else calc_full_year_v1(time1.year);
// Poprawka dla tryby 12 godinnego
if(!hour_format_24) time1.hours = convert_hour_to_24_format(time1.hours, pm);
*time = time1;
}
// Wyświetla podaną godzinę
void time_display(struct time_t *time)
{
printf("%s, %s %u ", weekdays_names[time->weekday-1], months_names[time->month], time->day_of_month);
if(time->hours < 10) printf("0");
printf("%u:", time->hours);
if(time->minutes < 10) printf("0");
printf("%u:", time->minutes);
if(time->seconds < 10) printf("0");
printf("%u ", time->seconds);
printf("%u", time->year);
}
// Funkcja haszująca, przydatna do tworzenia seed
uint32_t time_hash(void)
{
struct time_t time = {0};
get_time(&time);
uint32_t hash = time.seconds * 1103515245 + time.minutes * 1904515775 + time.hours * 903515111 + time.day_of_month * 1144414545 + time.year * 4354555245 + (uint32_t)time.month * 1993599245 + (uint32_t)time.weekday * 110355;
return hash;
}
// Zamienia wartość zapiwaną w BCD na wartość binarną
static uint8_t convert_bcd_to_bin(uint8_t bcd)
{
uint8_t binary = ((bcd & 0xF0)>>1)+((bcd & 0xF0)>>3)+(bcd & 0xf);
return binary;
}
// Zamienia godzine z formatu 12 na 24
static uint8_t convert_hour_to_24_format(uint8_t hour12, uint8_t pm)
{
// Past Morning
if(pm)
{
// Poprawka na północ
if(hour12 == 12) return 0;
return 12 + hour12;
}
// Am Morning
else return hour12;
}
// Zgaduje rok na podstawie ostatnich cyft
static uint32_t calc_full_year_v1(uint8_t year)
{
return 2000 + year;
}
// Oblicza pełny rok na podstawie rejestru wieku
static uint32_t calc_full_year_v2(uint8_t year)
{
uint8_t century = convert_bcd_to_bin(cmos_read_register(century_register));
uint32_t full_year = century * 100 + year;
return full_year;
}
// Porównuje dwie struktury czasu
static uint8_t compare_time(struct time_t *t1, struct time_t *t2)
{
if(t1->seconds != t2->seconds || t1->minutes != t2->minutes || t1->hours != t2->hours ||
t1->weekday != t2->weekday || t1->day_of_month!= t2->day_of_month || t1->month != t2->month || t1->year != t2->year)
return 1;
return 0;
}
// Pobiera z z pamięci CMOS czas w wersji surowej, być może szesnastkowej i bez gwarancji poprawności przez trającą aktualizacje
static inline void __get_time(struct time_t *time)
{
time->seconds = cmos_read_register(CMOS_REGISTER_SECONDS);
time->minutes = cmos_read_register(CMOS_REGISTER_MINUTES);
time->hours = cmos_read_register(CMOS_REGISTER_HOURS);
time->weekday = cmos_read_register(CMOS_REGISTER_WEEKDAY);
time->day_of_month = cmos_read_register(CMOS_REGISTER_DAY_OF_MONTH);
time->month = cmos_read_register(CMOS_REGISTER_MONTH);
time->year = cmos_read_register(CMOS_REGISTER_YEAR);
}
// Dokonuje konwersji wszystkich pól z BCD do wartości binarnych
static void convert_time_to_bin(struct time_t *time_bcd)
{
time_bcd->seconds = convert_bcd_to_bin(time_bcd->seconds);
time_bcd->minutes = convert_bcd_to_bin(time_bcd->minutes);
time_bcd->hours = convert_bcd_to_bin(time_bcd->hours);
time_bcd->weekday = convert_bcd_to_bin(time_bcd->weekday);
time_bcd->day_of_month = convert_bcd_to_bin(time_bcd->day_of_month);
time_bcd->month = convert_bcd_to_bin(time_bcd->month);
time_bcd->year = convert_bcd_to_bin(time_bcd->year);
}
// Komenda wyświetlająca aktualny czas
static void time_command(const char* tokens, uint32_t tokens_count)
{
struct time_t time;
get_time(&time);
time_display(&time);
printf("\n");
}