kern

changeset 40:710739e33da8

- now read_rtc doesn't try to read weekday as it's probably wrong anyway, and doesn't set yearday either - mktime now fixes yearday and weekday - moved init_timer and init_rtc just before enabling the interrupts in main
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Jun 2011 15:45:21 +0300
parents 92297f65aaef
children 928b0ebfff4d
files run src/klibc/time.c src/main.c src/rtc.c src/rtc.h src/timer.c
diffstat 6 files changed, 17 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/run	Sun Jun 12 01:46:53 2011 +0300
     1.2 +++ b/run	Sun Jun 12 15:45:21 2011 +0300
     1.3 @@ -1,3 +1,3 @@
     1.4  #!/bin/sh
     1.5  
     1.6 -qemu -kernel kernel.elf
     1.7 +qemu -kernel kernel.elf -soundhw pcspk
     2.1 --- a/src/klibc/time.c	Sun Jun 12 01:46:53 2011 +0300
     2.2 +++ b/src/klibc/time.c	Sun Jun 12 15:45:21 2011 +0300
     2.3 @@ -9,6 +9,9 @@
     2.4  #define DAYSEC		(24 * HOURSEC)
     2.5  #define YEARDAYS(x)	(is_leap_year(x) ? 366 : 365)
     2.6  
     2.7 +/* 1-1-1970 was a thursday */
     2.8 +#define EPOCH_WDAY	4
     2.9 +
    2.10  static int is_leap_year(int yr);
    2.11  
    2.12  static int mdays[2][12] = {
    2.13 @@ -53,10 +56,16 @@
    2.14  	int year = 1970;
    2.15  	int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
    2.16  
    2.17 +	/* set correct yearday */
    2.18 +	tm->tm_yday = days;
    2.19 +
    2.20  	for(i=0; i<num_years; i++) {
    2.21  		days += YEARDAYS(year++);
    2.22  	}
    2.23  
    2.24 +	/* set wday correctly */
    2.25 +	tm->tm_wday = (days + EPOCH_WDAY) % 7;
    2.26 +
    2.27  	return (time_t)days * DAYSEC + tm->tm_hour * HOURSEC +
    2.28  		tm->tm_min * MINSEC + tm->tm_sec;
    2.29  }
    2.30 @@ -76,7 +85,7 @@
    2.31  	days = *tp / DAYSEC;
    2.32  	t = *tp % DAYSEC;
    2.33  
    2.34 -	tm->tm_wday = (days + 4) % 7;
    2.35 +	tm->tm_wday = (days + EPOCH_WDAY) % 7;
    2.36  
    2.37  	while(days >= (yrdays = YEARDAYS(year))) {
    2.38  		days -= yrdays;
     3.1 --- a/src/main.c	Sun Jun 12 01:46:53 2011 +0300
     3.2 +++ b/src/main.c	Sun Jun 12 15:45:21 2011 +0300
     3.3 @@ -55,14 +55,15 @@
     3.4  	init_segm();
     3.5  	init_intr();
     3.6  
     3.7 -	init_rtc();
     3.8 -	init_timer();
     3.9 -
    3.10  	/* initialize the physical memory manager */
    3.11  	init_mem(mbinf);
    3.12  	/* initialize paging and the virtual memory manager */
    3.13  	init_vm();
    3.14  
    3.15 +	/* initialize the timer and RTC */
    3.16 +	init_timer();
    3.17 +	init_rtc();
    3.18 +
    3.19  	/* initialization complete, enable interrupts */
    3.20  	enable_intr();
    3.21  
     4.1 --- a/src/rtc.c	Sun Jun 12 01:46:53 2011 +0300
     4.2 +++ b/src/rtc.c	Sun Jun 12 15:45:21 2011 +0300
     4.3 @@ -59,7 +59,6 @@
     4.4  	tm->tm_mday = read_reg(REG_DAY);
     4.5  	tm->tm_mon = read_reg(REG_MONTH);
     4.6  	tm->tm_year = read_reg(REG_YEAR);
     4.7 -	tm->tm_wday = read_reg(REG_WEEKDAY);
     4.8  
     4.9  	/* in 12hour mode, bit 7 means post-meridiem */
    4.10  	pm = tm->tm_hour & HOUR_PM_BIT;
    4.11 @@ -74,7 +73,6 @@
    4.12  		tm->tm_mday = BCD_TO_BIN(tm->tm_mday);
    4.13  		tm->tm_mon = BCD_TO_BIN(tm->tm_mon);
    4.14  		tm->tm_year = BCD_TO_BIN(tm->tm_year);
    4.15 -		tm->tm_wday = BCD_TO_BIN(tm->tm_wday);
    4.16  	}
    4.17  
    4.18  	/* make the year an offset from 1900 */
    4.19 @@ -95,8 +93,6 @@
    4.20  	}
    4.21  
    4.22  	tm->tm_mon -= 1;	/* we want months to start from 0 */
    4.23 -	tm->tm_wday -= 1;	/* we want weekdays to start from 0 */
    4.24 -	tm->tm_yday = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
    4.25  }
    4.26  
    4.27  static int read_reg(int reg)
     5.1 --- a/src/rtc.h	Sun Jun 12 01:46:53 2011 +0300
     5.2 +++ b/src/rtc.h	Sun Jun 12 15:45:21 2011 +0300
     5.3 @@ -1,6 +1,8 @@
     5.4  #ifndef _RTC_H_
     5.5  #define _RTC_H_
     5.6  
     5.7 +#include <time.h>
     5.8 +
     5.9  /* the time read from rtc during init */
    5.10  time_t start_time;
    5.11  
     6.1 --- a/src/timer.c	Sun Jun 12 01:46:53 2011 +0300
     6.2 +++ b/src/timer.c	Sun Jun 12 15:45:21 2011 +0300
     6.3 @@ -71,9 +71,7 @@
     6.4  	nticks++;
     6.5  
     6.6  	if(nticks % TICK_FREQ_HZ == 0) {
     6.7 -		/*printf("%lu sec\n", nticks / TICK_FREQ_HZ);*/
     6.8  		time_t t = time(0);
     6.9 -
    6.10  		printf("%s", asctime(gmtime(&t)));
    6.11  	}
    6.12  }