Subversion Repositories shark

Rev

Rev 1618 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/* Project:     OSLib
2
 * Description: The OS Construction Kit
3
 * Date:                1.6.2000
4
 * Idea by:             Luca Abeni & Gerardo Lamastra
5
 *
6
 * OSLib is an SO project aimed at developing a common, easy-to-use
7
 * low-level infrastructure for developing OS kernels and Embedded
8
 * Applications; it partially derives from the HARTIK project but it
9
 * currently is independently developed.
10
 *
11
 * OSLib is distributed under GPL License, and some of its code has
12
 * been derived from the Linux kernel source; also some important
13
 * ideas come from studying the DJGPP go32 extender.
14
 *
15
 * We acknowledge the Linux Community, Free Software Foundation,
16
 * D.J. Delorie and all the other developers who believe in the
17
 * freedom of software and ideas.
18
 *
19
 * For legalese, check out the included GPL license.
20
 */
21
 
22
/*      Time management code: ll_getttime()     */
23
 
120 giacomo 24
/* Added Advanced Timer Code
25
 *
26
 * Date:   8.4.2003
27
 * Author: Giacomo Guidi <giacomo@gandalf.sssup.it>
28
 *
29
 */
30
 
1621 fabio 31
#include <arch/i386/stdlib.h>
2 pj 32
#include <ll/i386/pit.h>
33
#include <ll/i386/error.h>
34
#include <ll/i386/mem.h>
131 giacomo 35
#include <ll/i386/advtimer.h>
2 pj 36
#include <ll/sys/ll/ll-data.h>
37
#include <ll/sys/ll/ll-func.h>
38
#include <ll/sys/ll/time.h>
39
 
40
/* These are for the EXECT and TICK modes */
40 pj 41
extern DWORD ticksize;          /* From init.c  */
42
extern struct timespec actTime; /* From event.c */
43
extern WORD  pit_time_const;    /* From init.c  */
44
extern DWORD  timermode;        /* From init.c  */
2 pj 45
 
46
/* These two are for the NEW algorithm */
40 pj 47
extern WORD lastTime;                /* From event.c */
48
extern struct pitspec globalCounter; /* From event.c */
2 pj 49
extern BYTE frc;
50
extern int activeEvent;
650 giacomo 51
extern unsigned char use_tsc;
2 pj 52
 
53
FILE(Time);
54
 
55
TIME ll_gettime(int mode, struct timespec *tsres)
56
{
305 giacomo 57
 
619 mauro 58
        if (!use_tsc) {
305 giacomo 59
 
619 mauro 60
                DWORD res, tc;
61
                BYTE isr;
62
                struct timespec tmp;
2 pj 63
 
40 pj 64
#if 1
619 mauro 65
                if (activeEvent) {
66
                        if (tsres != NULL) {
67
                                PITSPEC2TIMESPEC(&globalCounter, tsres);
68
                        } else {
69
                                struct timespec tmp;
2 pj 70
 
619 mauro 71
                                PITSPEC2TIMESPEC(&globalCounter, &tmp);
72
                                return TIMESPEC2USEC(&tmp);
73
                        }
74
                        return TIMESPEC2USEC(tsres);
75
                }
40 pj 76
#endif
77
 
619 mauro 78
                if (mode == TIME_PTICK) {
79
                        if (timermode != LL_PERIODIC) {
80
                                return 0;
81
                        }
82
                        res = TIMESPEC2USEC(&actTime);
83
                        if (tsres != NULL) {
84
                                memcpy(tsres, &actTime, sizeof(struct timespec));
85
                        }
86
                        return res;
40 pj 87
                }
2 pj 88
 
619 mauro 89
                if (mode == TIME_NEW) {
90
                        WORD tmp;
40 pj 91
 
619 mauro 92
                        tmp = pit_read(frc);
93
                        ADDPITSPEC((WORD)(lastTime - tmp), &globalCounter);
94
                        lastTime = tmp;
95
                        if (tsres != NULL) {
96
                                PITSPEC2TIMESPEC(&globalCounter, tsres);
40 pj 97
                        }
619 mauro 98
                        return (PITSPEC2USEC(&globalCounter));
99
                }
2 pj 100
 
619 mauro 101
                if (mode == TIME_EXACT) {
102
                        if (timermode == LL_PERIODIC) {
103
                                memcpy(&tmp, &actTime, sizeof(struct timespec));
104
                                /* How much time has elapsed
105
                                 * from the last Tick Boundary?
106
                                 */
107
                                tc = pit_read(0);
108
                                if (tc > pit_time_const) {
109
                                        error("LL Time Panic!!!\n");
110
                                        ll_abort(1);
111
                                }
112
                                res = pit_time_const - tc;
113
                                res *= ticksize;
114
                                res /= pit_time_const;
2 pj 115
 
619 mauro 116
                                /* Detect tick boundary and adjust the time... */
117
                                outp(0x20, 0x0A);
118
                                isr = inp(0x20);
119
                                if ((isr & 1) && res < ((8*ticksize)/10)){
120
                                        /*
121
                                        res += ticksize;
122
                                        ADDNANO2TIMESPEC(ticksize * 1000, &tmp);
123
                                        */
124
                                        res = ticksize;
125
                                }
2 pj 126
 
619 mauro 127
                                /* Sum the Tick time... */
128
                                ADDNANO2TIMESPEC(res * 1000, &tmp);
129
                                res += TIMESPEC2USEC(&actTime);
130
 
131
                                if (tsres != NULL) {
132
                                        memcpy(tsres, &tmp, sizeof(struct timespec));
133
                                }
134
                                return res;
135
                        } else {
136
                                return 0;
40 pj 137
                        }
619 mauro 138
                }
139
                return 0;
140
        } else {
141
                if (tsres != NULL) {
142
                        ll_read_timespec(tsres);
143
                        return TIMESPEC2USEC(tsres);
40 pj 144
                } else {
619 mauro 145
                        struct timespec tmp;
146
 
147
                        ll_read_timespec(&tmp);
148
                        return TIMESPEC2USEC(&tmp);
40 pj 149
                }
2 pj 150
        }
151
}