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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
|
/* $NetBSD: clock.c,v 1.10 1995/02/20 00:53:42 chopps Exp $ */
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1982, 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Utah $Hdr: clock.c 1.18 91/01/21$
*
* @(#)clock.c 7.6 (Berkeley) 5/7/91
*/
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <machine/psl.h>
#include <machine/cpu.h>
#include <amiga/amiga/device.h>
#include <amiga/amiga/custom.h>
#include <amiga/amiga/cia.h>
#include <amiga/amiga/isr.h>
#include <amiga/dev/rtc.h>
#include <amiga/dev/zbusvar.h>
#if defined(PROF) && defined(PROFTIMER)
#include <sys/PROF.h>
#endif
extern void hardclock();
/* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
We're using a 100 Hz clock. */
#define CLK_INTERVAL amiga_clk_interval
int amiga_clk_interval;
int eclockfreq;
#if defined(IPL_REMAP_1) || defined(IPL_REMAP_2)
/*
* The INT6 handler copies the clockframe from the stack in here as hardclock
* may be delayed by the IPL-remapping code. At that time the original stack
* location will no longer be valid.
*/
struct clockframe hardclock_frame;
#endif
/*
* Machine-dependent clock routines.
*
* Startrtclock restarts the real-time clock, which provides
* hardclock interrupts to kern_clock.c.
*
* Inittodr initializes the time of day hardware which provides
* date functions.
*
* Resettodr restores the time of day hardware after a time change.
*
* A note on the real-time clock:
* We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
* This is because the counter decrements to zero after N+1 enabled clock
* periods where N is the value loaded into the counter.
*/
int clockmatch __P((struct device *, struct cfdata *, void *));
void clockattach __P((struct device *, struct device *, void *));
struct cfdriver clockcd = {
NULL, "clock", (cfmatch_t)clockmatch, clockattach,
DV_DULL, sizeof(struct device), NULL, 0 };
int
clockmatch(pdp, cfp, auxp)
struct device *pdp;
struct cfdata *cfp;
void *auxp;
{
if (matchname("clock", auxp))
return(1);
return(0);
}
/*
* Start the real-time clock.
*/
void
clockattach(pdp, dp, auxp)
struct device *pdp, *dp;
void *auxp;
{
unsigned short interval;
if (eclockfreq == 0)
eclockfreq = 715909; /* guess NTSC */
CLK_INTERVAL = (eclockfreq / 100);
printf(": system hz %d hardware hz %d\n", hz, eclockfreq);
/*
* stop timer A
*/
ciab.cra = ciab.cra & 0xc0;
ciab.icr = 1 << 0; /* disable timer A interrupt */
interval = ciab.icr; /* and make sure it's clear */
/*
* load interval into registers.
* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
* supprort for PAL WHEN?!?! XXX
*/
interval = CLK_INTERVAL - 1;
/*
* order of setting is important !
*/
ciab.talo = interval & 0xff;
ciab.tahi = interval >> 8;
}
#if defined(IPL_REMAP_1) || defined(IPL_REMAP_2)
int
clockintr ()
{
/* Is it a timer A interrupt? */
if (ciab.icr & 1) {
hardclock(&hardclock_frame);
return 1;
}
return 0;
}
#endif
void
cpu_initclocks()
{
#if defined(IPL_REMAP_1) || defined(IPL_REMAP_2)
static struct isr isr;
#endif
/*
* enable interrupts for timer A
*/
ciab.icr = (1<<7) | (1<<0);
/*
* start timer A in continuous shot mode
*/
ciab.cra = (ciab.cra & 0xc0) | 1;
#if defined(IPL_REMAP_1) || defined(IPL_REMAP_2)
isr.isr_intr = clockintr;
isr.isr_ipl = 6;
isr.isr_mapped_ipl = IPL_CLOCK;
add_isr(&isr);
#else
/*
* and globally enable interrupts for ciab
*/
custom.intena = INTF_SETCLR | INTF_EXTER;
#endif
}
setstatclockrate(hz)
int hz;
{
}
/*
* Returns number of usec since last recorded clock "tick"
* (i.e. clock interrupt).
*/
clkread()
{
u_char hi, hi2, lo;
u_int interval;
hi = ciab.tahi;
lo = ciab.talo;
hi2 = ciab.tahi;
if (hi != hi2) {
lo = ciab.talo;
hi = hi2;
}
interval = (CLK_INTERVAL - 1) - ((hi<<8) | lo);
/*
* should read ICR and if there's an int pending, adjust interval.
* However, * since reading ICR clears the interrupt, we'd lose a
* hardclock int, and * this is not tolerable.
*/
return((interval * tick) / CLK_INTERVAL);
}
u_int micspertick;
/*
* we set up as much of the CIAa as possible
* as all access to chip memory are very slow.
*/
void
setmicspertick()
{
micspertick = (1000000ULL << 20) / 715909;
/*
* disable interrupts (just in case.)
*/
ciaa.icr = 0x3;
/*
* stop both timers if not already
*/
ciaa.cra &= ~1;
ciaa.crb &= ~1;
/*
* set timer B in "count timer A underflows" mode
* set timer A in one-shot mode
*/
ciaa.crb = (ciaa.crb & 0x80) | 0x48;
ciaa.cra = (ciaa.cra & 0xc0) | 0x08;
}
/*
* this function assumes that on any entry beyond the first
* the following condintions exist:
* Interrupts for Timers A and B are disabled.
* Timers A and B are stoped.
* Timers A and B are in one-shot mode with B counting timer A underflows
*
*/
void
delay(mic)
u_int mic;
{
u_int temp;
int s;
if (micspertick == 0)
setmicspertick();
if (mic <= 1)
return;
/*
* basically this is going to do an integer
* usec / (1000000 / 715909) with no loss of
* precision
*/
temp = mic >> 12;
asm("divul %3,%1:%0" : "=d" (temp) : "d" (mic >> 12), "0" (mic << 20),
"d" (micspertick));
if ((temp & 0xffff0000) > 0x10000) {
mic = (temp >> 16) - 1;
temp &= 0xffff;
/*
* set timer A in continous mode
*/
ciaa.cra = (ciaa.cra & 0xc0) | 0x00;
/*
* latch/load/start "counts of timer A underflows" in B
*/
ciaa.tblo = mic & 0xff;
ciaa.tbhi = mic >> 8;
/*
* timer A latches 0xffff
* and start it.
*/
ciaa.talo = 0xff;
ciaa.tahi = 0xff;
ciaa.cra |= 1;
while (ciaa.crb & 1)
;
/*
* stop timer A
*/
ciaa.cra &= ~1;
/*
* set timer A in one shot mode
*/
ciaa.cra = (ciaa.cra & 0xc0) | 0x08;
} else if ((temp & 0xffff0000) == 0x10000) {
temp &= 0xffff;
/*
* timer A is in one shot latch/load/start 1 full turn
*/
ciaa.talo = 0xff;
ciaa.tahi = 0xff;
while (ciaa.cra & 1)
;
}
if (temp < 1)
return;
/*
* temp is now residual ammount, latch/load/start it.
*/
ciaa.talo = temp & 0xff;
ciaa.tahi = temp >> 8;
while (ciaa.cra & 1)
;
}
/*
* Needs to be calibrated for use, its way off most of the time
*/
void
DELAY(mic)
u_int mic;
{
u_long n;
short hpos;
/*
* this function uses HSync pulses as base units. The custom chips
* display only deals with 31.6kHz/2 refresh, this gives us a
* resolution of 1/15800 s, which is ~63us (add some fuzz so we really
* wait awhile, even if using small timeouts)
*/
n = mic/63 + 2;
do {
hpos = custom.vhposr & 0xff00;
while (hpos == (custom.vhposr & 0xff00))
;
} while (n--);
}
#if notyet
/* implement this later. I'd suggest using both timers in CIA-A, they're
not yet used. */
#include "clock.h"
#if NCLOCK > 0
/*
* /dev/clock: mappable high resolution timer.
*
* This code implements a 32-bit recycling counter (with a 4 usec period)
* using timers 2 & 3 on the 6840 clock chip. The counter can be mapped
* RO into a user's address space to achieve low overhead (no system calls),
* high-precision timing.
*
* Note that timer 3 is also used for the high precision profiling timer
* (PROFTIMER code above). Care should be taken when both uses are
* configured as only a token effort is made to avoid conflicting use.
*/
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <vm/vm.h>
#include <amiga/amiga/clockioctl.h>
#include <sys/specdev.h>
#include <sys/vnode.h>
#include <sys/mman.h>
int clockon = 0; /* non-zero if high-res timer enabled */
#ifdef PROFTIMER
int profprocs = 0; /* # of procs using profiling timer */
#endif
#ifdef DEBUG
int clockdebug = 0;
#endif
/*ARGSUSED*/
clockopen(dev, flags)
dev_t dev;
{
#ifdef PROFTIMER
#ifdef PROF
/*
* Kernel profiling enabled, give up.
*/
if (profiling)
return(EBUSY);
#endif
/*
* If any user processes are profiling, give up.
*/
if (profprocs)
return(EBUSY);
#endif
if (!clockon) {
startclock();
clockon++;
}
return(0);
}
/*ARGSUSED*/
clockclose(dev, flags)
dev_t dev;
{
(void) clockunmmap(dev, (caddr_t)0, curproc); /* XXX */
stopclock();
clockon = 0;
return(0);
}
/*ARGSUSED*/
clockioctl(dev, cmd, data, flag, p)
dev_t dev;
u_long cmd;
caddr_t data;
struct proc *p;
{
int error = 0;
switch (cmd) {
case CLOCKMAP:
error = clockmmap(dev, (caddr_t *)data, p);
break;
case CLOCKUNMAP:
error = clockunmmap(dev, *(caddr_t *)data, p);
break;
case CLOCKGETRES:
*(int *)data = CLK_RESOLUTION;
break;
default:
error = EINVAL;
break;
}
return(error);
}
/*ARGSUSED*/
clockmap(dev, off, prot)
dev_t dev;
{
return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
}
clockmmap(dev, addrp, p)
dev_t dev;
caddr_t *addrp;
struct proc *p;
{
int error;
struct vnode vn;
struct specinfo si;
int flags;
flags = MAP_FILE|MAP_SHARED;
if (*addrp)
flags |= MAP_FIXED;
else
*addrp = (caddr_t)0x1000000; /* XXX */
vn.v_type = VCHR; /* XXX */
vn.v_specinfo = &si; /* XXX */
vn.v_rdev = dev; /* XXX */
error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
PAGE_SIZE, VM_PROT_ALL, flags, (caddr_t)&vn, 0);
return(error);
}
clockunmmap(dev, addr, p)
dev_t dev;
caddr_t addr;
struct proc *p;
{
int rv;
if (addr == 0)
return(EINVAL); /* XXX: how do we deal with this? */
rv = vm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
return(rv == KERN_SUCCESS ? 0 : EINVAL);
}
startclock()
{
register struct clkreg *clk = (struct clkreg *)clkstd[0];
clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
clk->clk_cr2 = CLK_CR3;
clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
clk->clk_cr2 = CLK_CR1;
clk->clk_cr1 = CLK_IENAB;
}
stopclock()
{
register struct clkreg *clk = (struct clkreg *)clkstd[0];
clk->clk_cr2 = CLK_CR3;
clk->clk_cr3 = 0;
clk->clk_cr2 = CLK_CR1;
clk->clk_cr1 = CLK_IENAB;
}
#endif
#endif
#ifdef PROFTIMER
/*
* This code allows the amiga kernel to use one of the extra timers on
* the clock chip for profiling, instead of the regular system timer.
* The advantage of this is that the profiling timer can be turned up to
* a higher interrupt rate, giving finer resolution timing. The profclock
* routine is called from the lev6intr in locore, and is a specialized
* routine that calls addupc. The overhead then is far less than if
* hardclock/softclock was called. Further, the context switch code in
* locore has been changed to turn the profile clock on/off when switching
* into/out of a process that is profiling (startprofclock/stopprofclock).
* This reduces the impact of the profiling clock on other users, and might
* possibly increase the accuracy of the profiling.
*/
int profint = PRF_INTERVAL; /* Clock ticks between interrupts */
int profscale = 0; /* Scale factor from sys clock to prof clock */
char profon = 0; /* Is profiling clock on? */
/* profon values - do not change, locore.s assumes these values */
#define PRF_NONE 0x00
#define PRF_USER 0x01
#define PRF_KERNEL 0x80
initprofclock()
{
#if NCLOCK > 0
struct proc *p = curproc; /* XXX */
/*
* If the high-res timer is running, force profiling off.
* Unfortunately, this gets reflected back to the user not as
* an error but as a lack of results.
*/
if (clockon) {
p->p_stats->p_prof.pr_scale = 0;
return;
}
/*
* Keep track of the number of user processes that are profiling
* by checking the scale value.
*
* XXX: this all assumes that the profiling code is well behaved;
* i.e. profil() is called once per process with pcscale non-zero
* to turn it on, and once with pcscale zero to turn it off.
* Also assumes you don't do any forks or execs. Oh well, there
* is always adb...
*/
if (p->p_stats->p_prof.pr_scale)
profprocs++;
else
profprocs--;
#endif
/*
* The profile interrupt interval must be an even divisor
* of the CLK_INTERVAL so that scaling from a system clock
* tick to a profile clock tick is possible using integer math.
*/
if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
profint = CLK_INTERVAL;
profscale = CLK_INTERVAL / profint;
}
startprofclock()
{
unsigned short interval;
/* stop timer B */
ciab.crb = ciab.crb & 0xc0;
/* load interval into registers.
the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
interval = profint - 1;
/* order of setting is important ! */
ciab.tblo = interval & 0xff;
ciab.tbhi = interval >> 8;
/* enable interrupts for timer B */
ciab.icr = (1<<7) | (1<<1);
/* start timer B in continuous shot mode */
ciab.crb = (ciab.crb & 0xc0) | 1;
}
stopprofclock()
{
/* stop timer B */
ciab.crb = ciab.crb & 0xc0;
}
#ifdef PROF
/*
* profclock() is expanded in line in lev6intr() unless profiling kernel.
* Assumes it is called with clock interrupts blocked.
*/
profclock(pc, ps)
caddr_t pc;
int ps;
{
/*
* Came from user mode.
* If this process is being profiled record the tick.
*/
if (USERMODE(ps)) {
if (p->p_stats.p_prof.pr_scale)
addupc(pc, &curproc->p_stats.p_prof, 1);
}
/*
* Came from kernel (supervisor) mode.
* If we are profiling the kernel, record the tick.
*/
else if (profiling < 2) {
register int s = pc - s_lowpc;
if (s < s_textsize)
kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
}
/*
* Kernel profiling was on but has been disabled.
* Mark as no longer profiling kernel and if all profiling done,
* disable the clock.
*/
if (profiling && (profon & PRF_KERNEL)) {
profon &= ~PRF_KERNEL;
if (profon == PRF_NONE)
stopprofclock();
}
}
#endif
#endif
/* this is a hook set by a clock driver for the configured realtime clock,
returning plain current unix-time */
long (*gettod) __P((void));
int (*settod) __P((long));
void *clockaddr;
long a3gettod __P((void));
long a2gettod __P((void));
int a3settod __P((long));
int a2settod __P((long));
int rtcinit __P((void));
/*
* Initialize the time of day register, based on the time base which is, e.g.
* from a filesystem.
*/
inittodr(base)
time_t base;
{
u_long timbuf = base; /* assume no battery clock exists */
if (gettod == NULL && rtcinit() == 0)
printf("WARNING: no battery clock\n");
else
timbuf = gettod();
if (timbuf < base) {
printf("WARNING: bad date in battery clock\n");
timbuf = base;
}
/* Battery clock does not store usec's, so forget about it. */
time.tv_sec = timbuf;
}
resettodr()
{
if (settod && settod(time.tv_sec) == 1)
return;
printf("Cannot set battery backed clock\n");
}
int
rtcinit()
{
clockaddr = (void *)ztwomap(0xdc0000);
if (is_a3000() || is_a4000()) {
if (a3gettod() == 0)
return(0);
gettod = a3gettod;
settod = a3settod;
} else {
if (a2gettod() == 0)
return(0);
gettod = a2gettod;
settod = a2settod;
}
return(1);
}
static int month_days[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
long
a3gettod()
{
struct rtclock3000 *rt;
int i, year, month, day, wday, hour, min, sec;
u_long tmp;
rt = clockaddr;
/* hold clock */
rt->control1 = A3CONTROL1_HOLD_CLOCK;
/* read it */
sec = rt->second1 * 10 + rt->second2;
min = rt->minute1 * 10 + rt->minute2;
hour = rt->hour1 * 10 + rt->hour2;
wday = rt->weekday;
day = rt->day1 * 10 + rt->day2;
month = rt->month1 * 10 + rt->month2;
year = rt->year1 * 10 + rt->year2 + 1900;
/* let it run again.. */
rt->control1 = A3CONTROL1_FREE_CLOCK;
if (range_test(hour, 0, 23))
return(0);
if (range_test(wday, 0, 6))
return(0);
if (range_test(day, 1, 31))
return(0);
if (range_test(month, 1, 12))
return(0);
if (range_test(year, STARTOFTIME, 2000))
return(0);
tmp = 0;
for (i = STARTOFTIME; i < year; i++)
tmp += days_in_year(i);
if (leapyear(year) && month > FEBRUARY)
tmp++;
for (i = 1; i < month; i++)
tmp += days_in_month(i);
tmp += (day - 1);
tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
return(tmp);
}
int
a3settod(tim)
long tim;
{
register int i;
register long hms, day;
u_char sec1, sec2;
u_char min1, min2;
u_char hour1, hour2;
/* u_char wday; */
u_char day1, day2;
u_char mon1, mon2;
u_char year1, year2;
struct rtclock3000 *rt;
rt = clockaddr;
/*
* there seem to be problems with the bitfield addressing
* currently used..
*/
if (! rt)
return 0;
/* prepare values to be written to clock */
day = tim / SECDAY;
hms = tim % SECDAY;
hour2 = hms / 3600;
hour1 = hour2 / 10;
hour2 %= 10;
min2 = (hms % 3600) / 60;
min1 = min2 / 10;
min2 %= 10;
sec2 = (hms % 3600) % 60;
sec1 = sec2 / 10;
sec2 %= 10;
/* Number of years in days */
for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
day -= days_in_year(i);
year1 = i / 10;
year2 = i % 10;
/* Number of months in days left */
if (leapyear(i))
days_in_month(FEBRUARY) = 29;
for (i = 1; day >= days_in_month(i); i++)
day -= days_in_month(i);
days_in_month(FEBRUARY) = 28;
mon1 = i / 10;
mon2 = i % 10;
/* Days are what is left over (+1) from all that. */
day ++;
day1 = day / 10;
day2 = day % 10;
rt->control1 = A3CONTROL1_HOLD_CLOCK;
rt->second1 = sec1;
rt->second2 = sec2;
rt->minute1 = min1;
rt->minute2 = min2;
rt->hour1 = hour1;
rt->hour2 = hour2;
/* rt->weekday = wday; */
rt->day1 = day1;
rt->day2 = day2;
rt->month1 = mon1;
rt->month2 = mon2;
rt->year1 = year1;
rt->year2 = year2;
rt->control1 = A3CONTROL1_FREE_CLOCK;
return 1;
}
long
a2gettod()
{
struct rtclock2000 *rt;
int i, year, month, day, hour, min, sec;
u_long tmp;
rt = clockaddr;
/*
* hold clock
*/
rt->control1 |= A2CONTROL1_HOLD;
i = 0x1000;
while (rt->control1 & A2CONTROL1_BUSY && i--)
;
if (rt->control1 & A2CONTROL1_BUSY)
return (0); /* Give up and say it's not there */
/*
* read it
*/
sec = rt->second1 * 10 + rt->second2;
min = rt->minute1 * 10 + rt->minute2;
hour = (rt->hour1 & 3) * 10 + rt->hour2;
day = rt->day1 * 10 + rt->day2;
month = rt->month1 * 10 + rt->month2;
year = rt->year1 * 10 + rt->year2 + 1900;
if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
if ((rt->hour1 & A2HOUR1_PM) == 0 && hour == 12)
hour = 0;
else if ((rt->hour1 & A2HOUR1_PM) && hour != 12)
hour += 12;
}
/*
* release the clock
*/
rt->control1 &= ~A2CONTROL1_HOLD;
if (range_test(hour, 0, 23))
return(0);
if (range_test(day, 1, 31))
return(0);
if (range_test(month, 1, 12))
return(0);
if (range_test(year, STARTOFTIME, 2000))
return(0);
tmp = 0;
for (i = STARTOFTIME; i < year; i++)
tmp += days_in_year(i);
if (leapyear(year) && month > FEBRUARY)
tmp++;
for (i = 1; i < month; i++)
tmp += days_in_month(i);
tmp += (day - 1);
tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
return(tmp);
}
/*
* there is some question as to whether this works
* I guess
*/
int
a2settod(tim)
long tim;
{
int i;
long hms, day;
u_char sec1, sec2;
u_char min1, min2;
u_char hour1, hour2;
u_char day1, day2;
u_char mon1, mon2;
u_char year1, year2;
struct rtclock2000 *rt;
rt = clockaddr;
/*
* there seem to be problems with the bitfield addressing
* currently used..
*
* XXX Check out the above where we (hour1 & 3)
*/
if (! rt)
return 0;
/* prepare values to be written to clock */
day = tim / SECDAY;
hms = tim % SECDAY;
hour2 = hms / 3600;
hour1 = hour2 / 10;
hour2 %= 10;
min2 = (hms % 3600) / 60;
min1 = min2 / 10;
min2 %= 10;
sec2 = (hms % 3600) % 60;
sec1 = sec2 / 10;
sec2 %= 10;
/* Number of years in days */
for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
day -= days_in_year(i);
year1 = i / 10;
year2 = i % 10;
/* Number of months in days left */
if (leapyear(i))
days_in_month(FEBRUARY) = 29;
for (i = 1; day >= days_in_month(i); i++)
day -= days_in_month(i);
days_in_month(FEBRUARY) = 28;
mon1 = i / 10;
mon2 = i % 10;
/* Days are what is left over (+1) from all that. */
day ++;
day1 = day / 10;
day2 = day % 10;
/*
* XXXX spin wait as with reading???
*/
rt->control1 |= A2CONTROL1_HOLD;
rt->second1 = sec1;
rt->second2 = sec2;
rt->minute1 = min1;
rt->minute2 = min2;
rt->hour1 = hour1;
rt->hour2 = hour2;
rt->day1 = day1;
rt->day2 = day2;
rt->month1 = mon1;
rt->month2 = mon2;
rt->year1 = year1;
rt->year2 = year2;
rt->control2 &= ~A2CONTROL1_HOLD;
return 1;
}
|