Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/wonderswan/rtc.cpp
2 views
1
/* Cygne
2
*
3
* Copyright notice for this file:
4
* Copyright (C) 2002 Dox [email protected]
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
*/
20
21
#include "system.h"
22
#include <time.h>
23
24
namespace MDFN_IEN_WSWAN
25
{
26
// TODO: replace all use of libc time functions with internal stuff for movie recording
27
// (when userealtime == true, OK to use libc stuff)
28
29
static void GMTime(uint64 ticks, tm &time)
30
{
31
time_t t = ticks;
32
#if defined _MSC_VER
33
gmtime_s(&time, &t);
34
#elif defined __MINGW32__
35
tm *tmp = gmtime(&t);
36
time = *tmp;
37
#elif defined __GNUC__
38
gmtime_r(&t, &time);
39
#endif
40
}
41
42
void RTC::Write(uint32 A, uint8 V)
43
{
44
switch(A)
45
{
46
case 0xca:
47
if(V==0x15)
48
wsCA15=0;
49
Command = V;
50
break;
51
case 0xcb: Data = V; break;
52
}
53
54
}
55
56
57
uint8 RTC::Read(uint32 A)
58
{
59
switch(A)
60
{
61
case 0xca : return (Command|0x80);
62
case 0xcb :
63
if(Command == 0x15)
64
{
65
tm newtime;
66
uint64 now = userealtime ? time(nullptr) : CurrentTime;
67
GMTime(now, newtime);
68
69
switch(wsCA15)
70
{
71
case 0: wsCA15++;return mBCD(newtime.tm_year-100);
72
case 1: wsCA15++;return mBCD(newtime.tm_mon);
73
case 2: wsCA15++;return mBCD(newtime.tm_mday);
74
case 3: wsCA15++;return mBCD(newtime.tm_wday);
75
case 4: wsCA15++;return mBCD(newtime.tm_hour);
76
case 5: wsCA15++;return mBCD(newtime.tm_min);
77
case 6: wsCA15=0;return mBCD(newtime.tm_sec);
78
}
79
return 0;
80
}
81
else
82
return Data | 0x80;
83
84
}
85
return(0);
86
}
87
88
void RTC::Init(uint64 initialtime, bool realtime)
89
{
90
if (realtime)
91
{
92
userealtime = true;
93
CurrentTime = time(nullptr);
94
}
95
else
96
{
97
userealtime = false;
98
CurrentTime = initialtime;
99
}
100
101
ClockCycleCounter = 0;
102
wsCA15 = 0; // is this also possibly set to 0 on reset?
103
}
104
105
void RTC::Clock(uint32 cycles)
106
{
107
if (!userealtime)
108
{
109
ClockCycleCounter += cycles;
110
while(ClockCycleCounter >= 3072000)
111
{
112
ClockCycleCounter -= 3072000;
113
CurrentTime++;
114
}
115
}
116
}
117
118
SYNCFUNC(RTC)
119
{
120
NSS(CurrentTime);
121
NSS(userealtime);
122
123
NSS(ClockCycleCounter);
124
NSS(wsCA15);
125
NSS(Command);
126
NSS(Data);
127
}
128
}
129
130