-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc.cpp
More file actions
278 lines (242 loc) · 6.13 KB
/
Copy pathrtc.cpp
File metadata and controls
278 lines (242 loc) · 6.13 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <sys/time.h>
#include <extern_api.h>
#define CMOS_FREQUENCY 32768
#define ALARM_SEC 1
#define ALARM_MIN 3
#define ALARM_HOUR 5
#define PERIODIC 0x40
#define ALARM 0x20
#define UPDATE 0x10
using namespace std;
uint8_t cmos_ram[128];
uint8_t cmos_addr, cmos_nmi;
time_t cmos_now;
int cmos_periodic_ticks,
cmos_periodic_ticks_max,
cmos_last_raise;
uint32_t cmos_period;
uint64_t cmos_last_called,
cmos_uip_period,
cmos_last_second_update,
cmos_start_time;
V86_API void cmos_set(uint8_t where, uint8_t data) {
cmos_ram[where] = data;
}
V86_API uint8_t cmos_get(uint8_t where) {
return cmos_ram[where];
}
V86_API int cmos_get_raise() {
return cmos_last_raise;
}
V86_API bool cmos_should_lower() {
return cmos_addr == 0x0C;
}
#define cmos_is24hour() (cmos_ram[0x0B] & 2)
V86_API uint64_t cmos_get_now() {
struct timeval tv;
gettimeofday(&tv, NULL);
uint64_t hi = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec;
if (!cmos_start_time)
cmos_start_time = hi;
return hi - cmos_start_time;
}
static uint8_t cmos_bcd_read(uint8_t val)
{
if (cmos_ram[0x0B] & 4)
return val;
else
return ((val / 10) << 4) | (val % 10);
}
V86_API uint8_t cmos_ram_read(uint8_t addr)
{
struct tm* now;
uint64_t now_ticks, next_second;
switch (addr) {
case 0:
now = localtime(&cmos_now);
return cmos_bcd_read(now->tm_sec);
case 2:
now = localtime(&cmos_now);
return cmos_bcd_read(now->tm_min);
case 4:
now = localtime(&cmos_now);
if (cmos_is24hour())
return cmos_bcd_read(now->tm_hour);
else
return cmos_bcd_read(now->tm_hour % 12) | (now->tm_hour > 12) << 7;
case 6:
now = localtime(&cmos_now);
return cmos_bcd_read(now->tm_wday + 1);
case 7:
now = localtime(&cmos_now);
return cmos_bcd_read(now->tm_mday);
case 8:
now = localtime(&cmos_now);
return cmos_bcd_read(now->tm_mon + 1);
case 9:
now = localtime(&cmos_now);
return cmos_bcd_read(now->tm_year % 100);
case 1:
case 3:
case 5:
return cmos_ram[cmos_addr];
case 0x0A:
now_ticks = cmos_get_now();
next_second = cmos_last_second_update + 1000000;
if(now_ticks >= (next_second-cmos_uip_period) && now_ticks < next_second){
return cmos_ram[0x0A] | 0x80;
}
case 0x0B:
return cmos_ram[cmos_addr];
case 0x0C: {
int res = cmos_ram[0x0C];
cmos_ram[0x0C] = 0;
return res;
}
case 0x0D:
return 0x80;
default:
return 0x00;
}
}
V86_API uint32_t cmos_readb_70() {
return 0xFF;
}
V86_API uint32_t cmos_readb_71() {
if (cmos_addr <= 0x0D)
return cmos_ram_read(cmos_addr);
else
return cmos_ram[cmos_addr];
}
V86_API void cmos_update_timer()
{
int period = cmos_ram[0x0A] & 0x0F;
if (!period)
return;
if (period < 3)
period += 7;
int freq = CMOS_FREQUENCY >> (period - 1);
if (cmos_ram[0x0B] & 0x40) {
cmos_period = 1000000 / freq;
cmos_periodic_ticks = 0;
cmos_periodic_ticks_max = freq;
} else {
cmos_period = 1000000;
}
cmos_last_called = cmos_get_now();
}
static inline int bcd(int data)
{
if (cmos_ram[0x0B] & 4)
return data;
return ((data & 0xf0) >> 1) + ((data & 0xf0) >> 3) + (data & 0x0f);
}
V86_API void cmos_ram_write(uint8_t data)
{
struct tm* now = localtime(&cmos_now);
switch (cmos_addr) {
case 1:
case 3:
case 5:
cmos_set(cmos_addr, data);
break;
case 0:
now->tm_sec = bcd(data);
break;
case 2:
now->tm_min = bcd(data);
break;
case 4:
now->tm_hour = bcd(data & 0x7F);
if (!cmos_is24hour())
if (data & 0x80)
now->tm_hour += 12;
break;
case 6:
now->tm_wday = bcd(data);
break;
case 7:
now->tm_mday = bcd(data);
break;
case 8:
now->tm_mon = bcd(data);
break;
case 9:
now->tm_year = bcd(data) + (bcd(cmos_ram[0x32]) - 19) * 100;
if(now->tm_year < 70) now->tm_year = 70;
break;
case 0x0A:
cmos_ram[0x0A] = (data & 0x7F) | (cmos_ram[0x0A] & 0x80);
cmos_update_timer();
break;
case 0x0B:
cmos_ram[0x0B] = data;
cmos_update_timer();
break;
case 0x0C ... 0x0D:
break;
}
cmos_now = mktime(now);
}
V86_API void cmos_writeb_70(uint32_t data) {
cmos_nmi = data >> 7;
cmos_addr = data & 0x7F;
}
V86_API void cmos_writeb_71(uint32_t data) {
if (cmos_addr <= 0x0D)
cmos_ram_write(data);
else
cmos_ram[cmos_addr] = data;
}
int cmos_clock(uint64_t now)
{
uint64_t next = cmos_last_called + cmos_period;
if (now >= next) {
cmos_last_raise = 0;
if (cmos_ram[0x0B] & 0x40) {
cmos_last_raise |= PERIODIC;
cmos_periodic_ticks++;
if (cmos_periodic_ticks != cmos_periodic_ticks_max)
goto done;
cmos_periodic_ticks = 0;
}
cmos_now++;
if (cmos_ram[0x0B] & 0x20) {
int ok = 1;
ok &= cmos_ram_read(ALARM_SEC) == cmos_ram_read(0);
ok &= cmos_ram_read(ALARM_MIN) == cmos_ram_read(2);
ok &= cmos_ram_read(ALARM_HOUR) == cmos_ram_read(4);
if (ok)
cmos_last_raise |= ALARM;
}
if (cmos_ram[0x0B] & 0x10) {
cmos_last_raise |= UPDATE;
}
cmos_last_second_update = now;
done:
cmos_last_called = cmos_get_now();
if (cmos_last_raise) {
cmos_ram[0x0C] = 0x80 | cmos_last_raise;
return 1;
}
}
return 0;
}
V86_API int cmos_next(uint64_t now)
{
cmos_clock(now);
return cmos_last_called + cmos_period - now;
}
V86_API void cmos_init(uint64_t now)
{
if(now == 0) now = time(NULL);
cmos_now = now;
cmos_last_second_update = cmos_get_now();
cmos_uip_period = 244;
cmos_last_called = cmos_get_now();
cmos_period = 1000000;
}