Subversion Repositories shark

Rev

Rev 2 | Go to most recent revision | 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
 
24
#include <ll/i386/pit.h>
25
#include <ll/i386/stdlib.h>
26
#include <ll/i386/error.h>
27
#include <ll/i386/mem.h>
28
#include <ll/sys/ll/ll-data.h>
29
#include <ll/sys/ll/ll-func.h>
30
#include <ll/sys/ll/time.h>
31
 
32
/* These are for the EXECT and TICK modes */
33
extern DWORD ticksize;          /* From init.c  */
34
extern struct timespec actTime; /* From event.c */
35
extern WORD pit_time_const;     /* From init.c  */
36
extern DWORD timermode;         /* From init.c  */
37
 
38
/* These two are for the NEW algorithm */
39
extern WORD lastTime;           /* From event.c */
40
extern struct pitspec globalCounter;    /* From event.c */
41
extern BYTE frc;
42
 
43
 
44
extern int activeEvent;
45
 
46
FILE(Time);
47
 
48
TIME ll_gettime(int mode, struct timespec *tsres)
49
{
50
    DWORD res, tc;
51
    BYTE isr;
52
    struct timespec tmp;
53
 
54
    if (activeEvent) {
55
      if (tsres != NULL) {
56
        PITSPEC2TIMESPEC(&globalCounter, tsres);
57
        return TIMESPEC2USEC(tsres);
58
      } else {
59
        PITSPEC2TIMESPEC(&globalCounter, &tmp);
60
        return TIMESPEC2USEC(&tmp);
61
      }
62
    }
63
 
64
    if (mode == TIME_PTICK) {
65
        if (timermode != LL_PERIODIC) {
66
            return 0;
67
        }
68
        res = TIMESPEC2USEC(&actTime);
69
        if (tsres != NULL) {
70
            memcpy(tsres, &actTime, sizeof(struct timespec));
71
        }
72
        return res;
73
    }
74
 
75
    if (mode == TIME_EXACT) {
76
        WORD tmp;
77
 
78
        tmp = pit_read(frc);
79
        ADDPITSPEC((WORD) (lastTime - tmp), &globalCounter);
80
        lastTime = tmp;
81
        if (tsres != NULL) {
82
            PITSPEC2TIMESPEC(&globalCounter, tsres);
83
        }
84
        return (PITSPEC2USEC(&globalCounter));
85
    }
86
 
87
    if (mode == TIME_NEW) {
88
        if (timermode == LL_PERIODIC) {
89
            memcpy(&tmp, &actTime, sizeof(struct timespec));
90
            /* How much time has elapsed
91
             * from the last Tick Boundary?
92
             */
93
            tc = pit_read(0);
94
            if (tc > pit_time_const) {
95
                error("LL Time Panic!!!\n");
96
                ll_abort(1);
97
            }
98
            res = pit_time_const - tc;
99
            res *= ticksize;
100
            res /= pit_time_const;
101
 
102
            /* Detect tick boundary and adjust the time... */
103
            outp(0x20, 0x0A);
104
            isr = inp(0x20);
105
            if ((isr & 1) && res < ((8 * ticksize) / 10)) {
106
                /*
107
                   res += ticksize;
108
                   ADDNANO2TIMESPEC(ticksize * 1000, &tmp);
109
                 */
110
                res = ticksize;
111
            }
112
 
113
            /* Sum the Tick time... */
114
            ADDNANO2TIMESPEC(res * 1000, &tmp);
115
            res += TIMESPEC2USEC(&actTime);
116
 
117
            if (tsres != NULL) {
118
                memcpy(tsres, &tmp, sizeof(struct timespec));
119
            }
120
            return res;
121
        } else {
122
            return 0;
123
        }
124
    }
125
    return 0;
126
}