Subversion Repositories shark

Rev

Rev 120 | 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
 
2 pj 31
#include <ll/i386/pit.h>
32
#include <ll/i386/stdlib.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;
51
 
120 giacomo 52
extern unsigned char use_tsc;
53
 
2 pj 54
FILE(Time);
55
 
56
TIME ll_gettime(int mode, struct timespec *tsres)
57
{
40 pj 58
        DWORD res, tc;
59
        BYTE isr;
60
        struct timespec tmp;
2 pj 61
 
120 giacomo 62
   if (!use_tsc) {
63
 
40 pj 64
#if 1
65
        if (activeEvent) {
66
          if (tsres != NULL) {
67
            PITSPEC2TIMESPEC(&globalCounter, tsres);
68
          } else {
69
            struct timespec tmp;
2 pj 70
 
40 pj 71
            PITSPEC2TIMESPEC(&globalCounter, &tmp);
72
            return TIMESPEC2USEC(&tmp);
73
          }
74
          return TIMESPEC2USEC(tsres);
2 pj 75
        }
40 pj 76
#endif
77
 
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;
2 pj 87
        }
88
 
40 pj 89
        if (mode == TIME_NEW) {
90
          WORD tmp;
91
 
92
          tmp = pit_read(frc);
93
          ADDPITSPEC((WORD)(lastTime - tmp), &globalCounter);
94
          lastTime = tmp;
95
          if (tsres != NULL) {
2 pj 96
            PITSPEC2TIMESPEC(&globalCounter, tsres);
40 pj 97
          }
98
          return (PITSPEC2USEC(&globalCounter));
2 pj 99
        }
100
 
40 pj 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
 
40 pj 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
 
40 pj 127
                        /* Sum the Tick time... */
128
                        ADDNANO2TIMESPEC(res * 1000, &tmp);
129
                        res += TIMESPEC2USEC(&actTime);
2 pj 130
 
40 pj 131
                        if (tsres != NULL) {
132
                                memcpy(tsres, &tmp, sizeof(struct timespec));
133
                        }
134
                        return res;
135
                } else {
136
                        return 0;
137
                }
2 pj 138
        }
40 pj 139
        return 0;
120 giacomo 140
 
141
    } else {
142
 
143
#if 1
144
        if (activeEvent) {
145
          if (tsres != NULL) {
131 giacomo 146
            ll_read_timespec(tsres);
120 giacomo 147
          } else {
148
            struct timespec tmp;
149
 
131 giacomo 150
            ll_read_timespec(&tmp);
120 giacomo 151
            return TIMESPEC2USEC(&tmp);
152
          }
153
          return TIMESPEC2USEC(tsres);
154
        }
155
#endif
156
 
157
        if (mode == TIME_PTICK) {
158
                if (timermode != LL_PERIODIC) {
159
                        return 0;
160
                }
161
 
162
                if(tsres != NULL) {
131 giacomo 163
                        ll_read_timespec(tsres);
120 giacomo 164
                } else {
165
                        struct timespec tmp;
166
 
131 giacomo 167
                        ll_read_timespec(&tmp);
120 giacomo 168
                        return TIMESPEC2USEC(&tmp);
169
                }
170
                return TIMESPEC2USEC(tsres);
171
        }
172
 
173
        if (mode == TIME_NEW) {
174
          if (tsres != NULL) {
131 giacomo 175
            ll_read_timespec(tsres);
120 giacomo 176
            return TIMESPEC2USEC(tsres);
177
          } else {
178
            struct timespec tmp;
179
 
131 giacomo 180
            ll_read_timespec(&tmp);
120 giacomo 181
            return TIMESPEC2USEC(&tmp);
182
          }
183
        }
184
 
185
        if (mode == TIME_EXACT) {
186
                if (timermode == LL_PERIODIC) {
187
                        if (tsres != NULL) {
131 giacomo 188
                                ll_read_timespec(tsres);
120 giacomo 189
                        } else {
190
                                struct timespec tmp;
191
 
131 giacomo 192
                                ll_read_timespec(&tmp);
120 giacomo 193
                                return TIMESPEC2USEC(&tmp);
194
                        }
195
                        return TIMESPEC2USEC(tsres);
196
                } else {
197
                        return 0;
198
                }
199
        }
200
 
201
        return 0;
202
 
203
    }
204
 
2 pj 205
}