-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrtc.c
411 lines (357 loc) · 10.3 KB
/
rtc.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Tom Hunter, Paul Koning
**
** Name: rtc.c
**
** Description:
** Perform emulation of CDC 6600 real-time clock.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#if defined(_WIN32)
#include <windows.h>
#elif defined(__GNUC__) || defined(__SunOS)
#include <sys/time.h>
#include <unistd.h>
#endif
#include "const.h"
#include "types.h"
#include "proto.h"
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus rtcFunc(PpWord funcCode);
static void rtcIo(void);
static void rtcActivate(void);
static void rtcDisconnect(void);
static bool rtcInitTick (void);
static u64 rtcGetTick(void);
/*
** ----------------
** Public Variables
** ----------------
*/
u32 rtcClock = 0;
/*
** -----------------
** Private Variables
** -----------------
*/
static u8 rtcIncrement;
static bool rtcFull;
static u64 Hz;
static double MHz;
#if CcCycleTime
static u64 startTime;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise RTC.
**
** Parameters: Name Description.
** increment clock increment per iteration.
** setMHz cycle counter frequency in MHz.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void rtcInit(u8 increment, u32 setMHz)
{
DevSlot *dp;
(void)setMHz;
dp = channelAttach(ChClock, 0, DtRtc);
dp->activate = rtcActivate;
dp->disconnect = rtcDisconnect;
dp->func = rtcFunc;
dp->io = rtcIo;
dp->selectedUnit = 0;
activeChannel->ioDevice = dp;
activeChannel->hardwired = TRUE;
if (increment == 0)
{
if (!rtcInitTick())
{
printf("Invalid clock increment 0, defaulting to 1\n");
increment = 1;
}
}
rtcIncrement = increment;
/*
** RTC channel may be active or inactive and empty or full
** depending on model.
*/
rtcFull = (features & HasFullRTC) != 0;
activeChannel->full = rtcFull;
activeChannel->active = (features & HasFullRTC) != 0;
}
/*--------------------------------------------------------------------------
** Purpose: Do a clock tick
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void rtcTick(void)
{
rtcClock += rtcIncrement;
}
/*--------------------------------------------------------------------------
** Purpose: Start timing measurement.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
#if CcCycleTime
void rtcStartTimer(void)
{
if (rtcIncrement == 0)
{
startTime = rtcGetTick();
}
}
#endif
/*--------------------------------------------------------------------------
** Purpose: Complete timing measurement.
**
** Parameters: Name Description.
**
** Returns: Time in microseconds.
**
**------------------------------------------------------------------------*/
#if CcCycleTime
double rtcStopTimer(void)
{
u64 endTime;
if (rtcIncrement == 0)
{
endTime = rtcGetTick();
return((double)(int)(endTime - startTime) / ((double)(i64)Hz / 1000000.0L));
}
else
{
return(0.0);
}
}
#endif
/*--------------------------------------------------------------------------
** Purpose: Read current 32-bit microsecond counter and store in
** global variable rtcClock.
**
** Parameters: Name Description.
**
** Returns: Nothing
**
**------------------------------------------------------------------------*/
#define MaxMicroseconds 400.0L
void rtcReadUsCounter(void)
{
static bool first = TRUE;
static u64 old = 0;
static double fraction = 0.0L;
static double delayedMicroseconds = 0.0L;
u64 new;
u64 difference;
double microseconds;
double result;
if (rtcIncrement != 0)
{
return;
}
if (first)
{
first = FALSE;
old = rtcGetTick();
}
new = rtcGetTick();
if ((i64)new < (i64)old)
{
/* Ignore ticks if they go backward */
old = new;
return;
}
difference = new - old;
old = new;
microseconds = (double)(i64)difference / MHz;
microseconds += fraction + delayedMicroseconds;
delayedMicroseconds = 0.0;
if (microseconds > MaxMicroseconds)
{
delayedMicroseconds = microseconds - MaxMicroseconds;
microseconds = MaxMicroseconds;
}
result = floor(microseconds);
fraction = microseconds - result;
rtcClock += (u32)result;
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Execute function code on RTC pseudo device.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static FcStatus rtcFunc(PpWord funcCode)
{
(void)funcCode;
return(FcAccepted);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void rtcIo(void)
{
rtcReadUsCounter();
activeChannel->full = rtcFull;
activeChannel->data = (PpWord)rtcClock & Mask12;
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void rtcActivate(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void rtcDisconnect(void)
{
}
#if defined(_WIN32)
/*--------------------------------------------------------------------------
** Purpose: Low-level microsecond tick functions.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static bool rtcInitTick(void)
{
LARGE_INTEGER lhz;
if (!QueryPerformanceFrequency(&lhz))
{
printf("No high resolution hardware clock, using emulation cycle counter\n");
return(FALSE);
}
Hz = lhz.QuadPart;
MHz = (double)(i64)Hz / 1000000.0;
printf("Using QueryPerformanceCounter() clock at %f MHz\n", MHz);
return(TRUE);
}
static u64 rtcGetTick(void)
{
LARGE_INTEGER ctr;
QueryPerformanceCounter(&ctr);
return(ctr.QuadPart);
}
#elif defined(__GNUC__) && (defined(__linux__) || defined(__SunOS) || defined (__FreeBSD__) || defined (__APPLE__))
/*--------------------------------------------------------------------------
** Purpose: Low-level microsecond tick functions.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static bool rtcInitTick(void)
{
Hz = 1000000;
MHz = 1.0;
printf("Using gettimeofday() clock at %f MHz\n", MHz);
return(TRUE);
}
static u64 rtcGetTick(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return((u64)tv.tv_sec * (u64)1000000 + (u64)tv.tv_usec);
}
#else
/*--------------------------------------------------------------------------
** Purpose: Low-level microsecond tick functions.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static bool rtcInitTick(void)
{
printf("No high resolution hardware clock, using emulation cycle counter\n");
return(FALSE);
}
#endif
/*--------------------------- End Of File ------------------------------*/