forked from AZO234/NP2kai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.c
executable file
·141 lines (111 loc) · 2.53 KB
/
calendar.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
#include <compiler.h>
#include <common/parts.h>
#include <timemng.h>
#include <pccore.h>
#include <calendar.h>
_CALENDAR cal;
static const UINT8 days[12] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
static void secinc(_SYSTIME *dt) {
UINT month;
UINT16 daylimit;
dt->second++;
if (dt->second < 60) {
goto secinc_exit;
}
dt->second = 0;
dt->minute++;
if (dt->minute < 60) {
goto secinc_exit;
}
dt->minute = 0;
dt->hour++;
if (dt->hour < 24) {
goto secinc_exit;
}
dt->hour = 0;
month = dt->month - 1;
if (month < 12) {
daylimit = days[month];
if ((daylimit == 28) && (!(dt->year & 3))) {
daylimit++; // = 29;
}
}
else {
daylimit = 30;
}
dt->week = (UINT16)((dt->week + 1) % 7);
dt->day++;
if (dt->day <= daylimit) {
goto secinc_exit;
}
dt->day = 1;
dt->month++;
if (dt->month <= 12) {
goto secinc_exit;
}
dt->month = 1;
dt->year++;
secinc_exit:
return;
}
static void date2deg(_SYSTIME *t, const UINT8 *bcd) {
UINT16 year;
year = 1900 + AdjustBeforeDivision(bcd[0]);
if (year < 1980) {
year += 100;
}
t->year = (UINT16)year;
t->week = (UINT16)((bcd[1]) & 0x0f);
t->month = (UINT16)((bcd[1]) >> 4);
t->day = (UINT16)AdjustBeforeDivision(bcd[2]);
t->hour = (UINT16)AdjustBeforeDivision(bcd[3]);
t->minute = (UINT16)AdjustBeforeDivision(bcd[4]);
t->second = (UINT16)AdjustBeforeDivision(bcd[5]);
}
static void date2bcd(UINT8 *bcd, const _SYSTIME *t) {
bcd[0] = AdjustAfterMultiply((UINT8)(t->year % 100));
bcd[1] = (UINT8)((t->month << 4) + t->week);
bcd[2] = AdjustAfterMultiply((UINT8)t->day);
bcd[3] = AdjustAfterMultiply((UINT8)t->hour);
bcd[4] = AdjustAfterMultiply((UINT8)t->minute);
bcd[5] = AdjustAfterMultiply((UINT8)t->second);
}
// -----
void calendar_initialize(void) {
timemng_gettime(&cal.dt);
cal.steps = 0;
cal.realchg = 1;
}
void calendar_inc(void) {
cal.realchg = 1;
// 56.4Hzだから…
cal.steps += 10;
if (cal.steps < 564) {
return;
}
cal.steps -= 564;
secinc(&cal.dt);
}
void calendar_set(const UINT8 *bcd) {
date2deg(&cal.dt, bcd);
}
void calendar_getvir(UINT8 *bcd) {
date2bcd(bcd, &cal.dt);
}
void calendar_getreal(UINT8 *bcd) {
timemng_gettime(&cal.realc);
date2bcd(bcd, &cal.realc);
}
void calendar_get(UINT8 *bcd) {
if (!np2cfg.calendar) {
date2bcd(bcd, &cal.dt);
}
else {
if (cal.realchg) {
cal.realchg = 0;
timemng_gettime(&cal.realc);
}
date2bcd(bcd, &cal.realc);
}
}