forked from r-type/xmil-libretro
-
Notifications
You must be signed in to change notification settings - Fork 12
/
calendar.c
127 lines (98 loc) · 2.16 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
#include "compiler.h"
#include "parts.h"
#include "timemng.h"
#include "pccore.h"
#include "nevent.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;
}
/* ---- */
void calendar_getdate(UINT8 *bcd) {
bcd[2] = AdjustAfterMultiply((UINT8)(cal.dt.year % 100));
bcd[1] = ((cal.dt.month << 4) + cal.dt.week);
bcd[0] = AdjustAfterMultiply((UINT8)cal.dt.day);
}
void calendar_setdate(const UINT8 *bcd) {
UINT year;
year = AdjustBeforeDivision(bcd[2]);
if (year < 80) {
year += 100;
}
cal.dt.year = (UINT16)(year + 1900);
cal.dt.week = (UINT8)(bcd[1] & 0x0f);
cal.dt.month = (UINT8)(bcd[1] >> 4);
cal.dt.day = AdjustBeforeDivision(bcd[0]);
}
void calendar_gettime(UINT8 *bcd) {
bcd[2] = AdjustAfterMultiply((UINT8)cal.dt.hour);
bcd[1] = AdjustAfterMultiply((UINT8)cal.dt.minute);
bcd[0] = AdjustAfterMultiply((UINT8)cal.dt.second);
}
void calendar_settime(const UINT8 *bcd) {
cal.dt.hour = AdjustBeforeDivision(bcd[2]);
cal.dt.minute = AdjustBeforeDivision(bcd[1]);
cal.dt.second = AdjustBeforeDivision(bcd[0]);
}
/* ---- */
void neitem_rtc(NEVENTID id) {
nevent_repeat(id);
cal.steps++;
if (cal.steps >= 5) {
cal.steps -= 5;
secinc(&cal.dt);
}
}
void calendar_reset(void) {
SINT32 clock;
timemng_gettime(&cal.dt);
#if defined(FIX_Z80A)
clock = 2000000 * 2 / 5;
#else
clock = pccore.realclock / 5;
#endif
nevent_set(NEVENT_RTC, clock, neitem_rtc, NEVENT_RELATIVE);
}