搜尋此網誌

2012年8月22日 星期三

BIOS - Get System time by seconds from 1970


/*The example would use in/out port to get system time(year/month/day/hour/minute/second), and transfer them into seconds from 1970.*/

u32 get_system_time(void)
{
       u16 year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0;
       u16 year0, mon0;
       u16 tmp = 10;
       u32 total;

   __asm{
        pusha
        mov al, 00h
        out 70h, al
        in al, 71h
        mov bl, al
        and bl, 0Fh
        shr al, 4
        mov ah, 00h
        mul tmp
        add al, bl
        mov sec, ax

        mov al, 02h
        out 70h, al
        in al, 71h
        mov bl, al
        and bl, 0Fh
        shr al, 4
        mov ah, 00h
        mul tmp
        add al, bl
        mov min, ax

        mov al, 04h
        out 70h, al
        in al, 71h
        mov bl, al
        and bl, 0Fh
        shr al, 4
        mov ah, 00h
        mul tmp
        add al, bl
        mov hour, ax

        mov al, 07h
        out 70h, al
        in al, 71h
        mov bl, al
        and bl, 0Fh
        shr al, 4
        mov ah, 00h
        mul tmp
        add al, bl
        mov day, ax

        mov al, 08h
        out 70h, al
        in al, 71h
        mov bl, al
        and bl, 0Fh
        shr al, 4
        mov ah, 00h
        mul tmp
        add al, bl
        mov mon, ax

        mov al, 09h
        out 70h, al
        in al, 71h
        mov bl, al
        and bl, 0Fh
        shr al, 4
        mov ah, 00h
        mul tmp
        add al, bl
        mov bx, ax
        add bx, 2000
        mov year, bx

        popa
    }

    mon0 = mon;
    year0 = year;

    /* 1..12 -> 11,12,1..10 */
    if (0 >= (int) (mon -= 2)) {
mon += 12; /* Puts Feb last since it has leap day */
year -= 1;
    }

    total = (u32)((u32)((u32)((u32)
        (year / 4 - year / 100 + year/400 + 367 * mon / 12 + day) +
        (u32)year * 365 - (u32)719499
            )*24 + hour /* now have hours */
            )*60 + min /* now have minutes */
        )*60 + sec; /* finally seconds */

    return total;
}

沒有留言:

張貼留言