Subversion Repositories shark

Rev

Rev 120 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/* Project:     OSLib
 * Description: The OS Construction Kit
 * Date:                1.6.2000
 * Idea by:             Luca Abeni & Gerardo Lamastra
 *
 * OSLib is an SO project aimed at developing a common, easy-to-use
 * low-level infrastructure for developing OS kernels and Embedded
 * Applications; it partially derives from the HARTIK project but it
 * currently is independently developed.
 *
 * OSLib is distributed under GPL License, and some of its code has
 * been derived from the Linux kernel source; also some important
 * ideas come from studying the DJGPP go32 extender.
 *
 * We acknowledge the Linux Community, Free Software Foundation,
 * D.J. Delorie and all the other developers who believe in the
 * freedom of software and ideas.
 *
 * For legalese, check out the included GPL license.
 */


/* Added Advanced Timer Code
 *
 * Date:   8.4.2003
 * Author: Giacomo Guidi <giacomo@gandalf.sssup.it>
 *
 */


/*      Inline functions for managing timespec structures.
        All timespec values are pointers!!!
        This file defines these functions:
                TIMESPEC2NANOSEC(t)
                converts a timespec value to a nanosec value, and return
                        it, no checks
                TIMESPEC2USEC(t)
                converts a timespec value to a nanosec value, and return
                it, no checks
                NULL_TIMESPEC(t)
                the timespec value is set to the Epoch (=0)
                ADDNANO2TIMESPEC(n, t)
                t = t + n
                ADDUSEC2TIMESPEC(m, t)
                t = t + m
                SUBTIMESPEC(s1, s2, d)
                d = s1 - s2
                ADDTIMESPEC(s1, s2, d)
                d = s1 + s2
                TIMESPEC_A_LT_B(a,b)
                a < b
                TIMESPEC_A_GT_B(a,b)
                a > b
                TIMESPEC_A_EQ_B(a,b)
                a == b
                TIMESPEC_A_NEQ_B(a,b)
                a != b
                TIMESPEC_ASSIGN(t1,t2)
                t1 = t2 */


/* Advanced Timer Support
 * Giacomo Guidi <giacomo@gandalf.sssup.it>
 */


#ifndef __LL_SYS_LL_TIME_H__
#define __LL_SYS_LL_TIME_H__

#include <ll/i386/defs.h>
BEGIN_DEF

extern signed long long clk_per_msec;
extern signed long long init_tsc;

extern unsigned char use_tsc;

struct timespec {
        long            tv_sec;         /* Seconds */
        long            tv_nsec;        /* Nanoseconds */
        long long       tsc;            /* 64 bit TSC */
};

extern __inline__ void UNSIGNED_TSC2NSEC(unsigned long long tsc, unsigned long long *n)
{

        unsigned long nl,nh;

        nl = *n & 0xFFFFFFFF;
        nh = *n >> 32;
       
        __asm__("mull %%ecx\n\t"
                "movl %%eax,%%esi\n\t"
                "movl %%edx,%%edi\n\t"
                "xorl %%edx,%%edx\n\t"
                "movl %6,%%eax\n\t"
                "mull %%ecx\n\t"
                "addl %%edi,%%eax\n\t"
                "adcl $0,%%edx\n\t"
                "movl %5,%%ecx\n\t"
                "divl %%ecx\n\t"
                "xchgl %%eax,%%esi\n\t"
                "divl %%ecx\n\t"
                : "=a" (nl), "=S" (nh)
                : "c" (1000000), "a" ((unsigned long)(tsc & 0xFFFFFFFF)), "d" (0),
                  "m" ((unsigned long)(clk_per_msec)), "m" ((unsigned long)(tsc >> 32)),
                  "S" (0), "D" (0));

        *n = nh;
        *n <<= 32;
        *n |= nl;

}

extern __inline__ void ADJUST_TIMESPEC(struct timespec *t)
{
       
        signed long long dt,dn;
       
        dt = t->tsc - init_tsc;
       
        if (dt >= 0) UNSIGNED_TSC2NSEC((unsigned long long)(dt),(unsigned long long *)&dn);
                else {
                  UNSIGNED_TSC2NSEC((unsigned long long)(-dt),(unsigned long long *)&dn);
                  dn -= dn;
                }

        t->tv_sec = dn / 1000000000;
        t->tv_nsec = dn % 1000000000;

}

/*
 * these macros come from the Utah Flux oskit...
 */


#define TIMESPEC2NANOSEC(t)     ((t)->tv_sec * 1000000000 + (t)->tv_nsec)
#define TIMESPEC2USEC(t)     ((t)->tv_sec * 1000000 + (t)->tv_nsec / 1000)
#define NULL_TIMESPEC(t)        ((t)->tv_sec = (t)->tv_nsec = 0, (t)->tsc = 0)

extern __inline__ void ADDNANO2TIMESPEC(const signed long n, struct timespec *t)  
{
        t->tv_nsec += n;
        t->tv_sec += t->tv_nsec / 1000000000;
        t->tv_nsec %= 1000000000;

        if (use_tsc) t->tsc += clk_per_msec * n / 1000000;
       
}
       
extern __inline__ void ADDUSEC2TIMESPEC(const signed long m, struct timespec *t)  
{      
       
        t->tv_nsec += m * 1000;
        t->tv_sec += t->tv_nsec / 1000000000;
        t->tv_nsec %= 1000000000;

        if (use_tsc) t->tsc += clk_per_msec * m / 1000;

}

extern __inline__ void SUBTIMESPEC(const struct timespec *s1,
                                   const struct timespec *s2,
                                   struct timespec *d)
{
        if (s1->tv_nsec >= s2->tv_nsec) {      
          d->tv_sec = s1->tv_sec - s2->tv_sec;  
          d->tv_nsec = s1->tv_nsec - s2->tv_nsec;            
        } else {                                
          d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
          d->tv_nsec = 1000000000 + s1->tv_nsec - s2->tv_nsec;
        }
       
        if (use_tsc) d->tsc = s1->tsc - s2->tsc + init_tsc;
       
}

extern __inline__ void ADDTIMESPEC(const struct timespec *s1,
                                   const struct timespec *s2,
                                   struct timespec *d)
{
       
        d->tv_sec  = s1->tv_sec + s2->tv_sec;
        d->tv_nsec = s1->tv_nsec + s2->tv_nsec;

        if (d->tv_nsec < 0) {
          d->tv_sec--;
          d->tv_nsec += 1000000000;
        } else if (d->tv_nsec >= 1000000000) {
          d->tv_sec++;
          d->tv_nsec -= 1000000000;
        }

        if (use_tsc) d->tsc = s1->tsc + s2->tsc - init_tsc;

}

#define TIMESPEC_A_LT_B(a,b)                                            \
        (                                                               \
        ((a)->tv_sec <  (b)->tv_sec) ||                                 \
        ((a)->tv_sec == (b)->tv_sec && (a)->tv_nsec < (b)->tv_nsec)     \
        )


#define TIMESPEC_A_GT_B(a,b)                                            \
        (                                                               \
        ((a)->tv_sec >  (b)->tv_sec) ||                                 \
        ((a)->tv_sec == (b)->tv_sec && (a)->tv_nsec > (b)->tv_nsec)     \
        )


#define TIMESPEC_A_EQ_B(a,b)                                            \
        ((a)->tv_sec == (b)->tv_sec && (a)->tv_nsec == (b)->tv_nsec)


#define TIMESPEC_A_NEQ_B(a,b)                                           \
        ((a)->tv_sec != (b)->tv_sec || (a)->tv_nsec != (b)->tv_nsec)


#define TIMESPEC_ASSIGN(t1,t2) \
        ((t1)->tv_sec = (t2)->tv_sec, (t1)->tv_nsec = (t2)->tv_nsec, (t1)->tsc = (t2)->tsc)


#if 0
#define PITSPEC2TIMESPEC(a,b) \
     ((b)->tv_nsec = (((DWORD)((a)->units) * 1000) / 1197) * 1000, \
        (b)->tv_sec = ((a)->gigas * 1197) / 1000) /*, \
        (b)->tv_sec += (b)->tv_nsec / 1000000000, \
        (b)->tv_nsec %= 1000000000)     */

#else
/*#define PITSPEC2TIMESPEC(a,b) \
     ((b)->tv_nsec = (((DWORD)((a)->units) * 1000) / 1197) * 1000, \
        (b)->tv_nsec += (((a)->gigas * 1197) % 1000) * 1000000, \
        (b)->tv_sec = ((a)->gigas * 1197) / 1000 , \
        (b)->tv_sec += (b)->tv_nsec / 1000000000, \
        (b)->tv_nsec %= 1000000000)*/

#define PITSPEC2TIMESPEC(a,b) \
     ((b)->tv_nsec = (((DWORD)((a)->units) * 1000) / 1197), \
        (b)->tv_nsec += (((a)->gigas * 1197) % 1000) * 1000, \
        (b)->tv_sec = ((a)->gigas * 1197) / 1000 , \
        (b)->tv_sec += (b)->tv_nsec / 1000000, \
        (b)->tv_nsec %= 1000000, \
        (b)->tv_nsec *= 1000)

#endif

TIME ll_gettime(int mode, struct timespec *tsres);

//Advanced Timer (advtimer.c)
void read_timespec(struct timespec *tspec);
void ll_init_advtimer(void);
void restore_CMOS(void);

#define TIME_PTICK      1
#define TIME_EXACT      2
#define TIME_NEW        3

END_DEF
#endif