forked from gotaproblem/ST_HD_Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemurtc.c
91 lines (73 loc) · 2.03 KB
/
emurtc.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
/*
* ATARI ST HDC Emulator
*
* File: emurtc.c
* Author: Steve Bradford
* Created: September 2022
*
* RTC string builder
*/
#include <string.h>
#include <stdlib.h>
#include <pico/stdlib.h>
#include "pico.h"
#include "hardware/rtc.h"
int dow ( datetime_t *dt )
{
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int d = dt->day;
// correction for leap year
if ( dt->year % 4 == 0 && (dt->year % 100 != 0 || dt->year % 400 == 0))
month[1] = 29;
for (int i = 1900; i < dt->year; i++)
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
d += 366;
else
d += 365;
for (int i = 0; i < dt->month - 1; i++)
d += month[i];
return (d % 7);
}
/*
* populate RTC datetime_t structure with programmes build date and time
*/
void buildDateTime ( void )
{
char dateStr [12];
char timeStr [9];
datetime_t tm;
char year [5];
char month [4];
char date [3];
char months [12][3] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
//char days [7][3] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
char hours [3];
char mins [3];
char secs [3];
int i;
sleep_ms (1);
strncpy ( dateStr, __DATE__, 11 );
strncpy ( timeStr, __TIME__, 9 );
strncpy ( year, &dateStr [7], 4 );
strncpy ( date, &dateStr [4], 2 );
strncpy ( month, &dateStr [0], 3 );
strncpy ( hours, &timeStr [0], 2 );
strncpy ( mins, &timeStr [3], 2 );
strncpy ( secs, &timeStr [6], 2 );
for ( i = 0; i < 12; i++ )
{
if ( strncmp ( month, months [i], 3 ) == 0 )
{
break;
}
}
tm.year = atoi ( year );
tm.month = i + 1;
tm.day = atoi ( date );
tm.dotw = dow ( &tm );
tm.hour = atoi ( hours );
tm.min = atoi ( mins );
tm.sec = atoi ( secs );
rtc_set_datetime (&tm); /* initialise rtc to build date/time */
}