Page 2 of 3

Re:Timer Interrupt

Posted: Tue Mar 23, 2004 7:35 am
by Pype.Clicker
well, i wasn't proposing a "method" ... just mentionning that the CMOS needs no update from software. Most "complete" operating systems use a mix of PIT and CMOS to get accurate timing: As the PIT cannot run at 100.0000Hz, it periodically (say, once a minute) check the CMOS value and updates the internal counter that the PIT-driven interrupt handler will increment ...

It will of course be preferable that the internal counter is *late* compared to the CMOS rather than finding it 'in advance' if we want to prevent some "backward timing" issues ...

Re:Timer Interrupt

Posted: Tue Mar 23, 2004 7:39 am
by DennisCGc
Pype.Clicker wrote: well, i wasn't proposing a "method" ... just mentionning that the CMOS needs no update from software. Most "complete" operating systems use a mix of PIT and CMOS to get accurate timing: As the PIT cannot run at 100.0000Hz, it periodically (say, once a minute) check the CMOS value and updates the internal counter that the PIT-driven interrupt handler will increment ...

It will of course be preferable that the internal counter is *late* compared to the CMOS rather than finding it 'in advance' if we want to prevent some "backward timing" issues ...
That is a METHOD, to provide the time.
My OS uses the CMOS for initialization, and the PIT for updating the time, and NOT the CMOS!
HE wanted to know what to do.
But what shoud I do if an timer interrupt occurs? Should I increment a counter? but than i have to convert bcd every time i would get the time.
(edited: the grammer ;) )

Re:Timer Interrupt

Posted: Tue Mar 23, 2004 7:49 am
by ich_will
Oh sorry Neo.

DennisCGc write that his timer interrupt occurs 1/100 sec, but how could I setup the timer to do it so? I think I have to use the CMOS register A, but wich value should I write to it? I know that the first four bits are for the periodic interupt rate. Which value should it have?

Re:Timer Interrupt

Posted: Tue Mar 23, 2004 7:56 am
by DennisCGc
ich_will wrote: Oh sorry Neo.

DennisCGc write that his timer interrupt occurs 1/100 sec, but how could I setup the timer to do it so? I think I have to use the CMOS register A, but wich value should I write to it? I know that the first four bits are for the periodic interupt rate. Which value should it have?
No, not the CMOS, but you'll have to program the PIT.

Code: Select all

mov  al,00110100b
mov  dx,0x43
out   dx,al
mov  ax,0x2e9b
mov  dx,0x40
out   dx,al
mov  al,ah
out   dx,al
that should work ;)

Re:Timer Interrupt

Posted: Wed Mar 24, 2004 11:38 am
by Neo
ich_will wrote: Oh sorry Neo.

DennisCGc write that his timer interrupt occurs 1/100 sec, but how could I setup the timer to do it so? I think I have to use the CMOS register A, but wich value should I write to it? I know that the first four bits are for the periodic interupt rate. Which value should it have?
you're confusing the PIT and the CMOS clock. The PIT is what is givin you the IRQ 0 and the CMOS is where youre reading the time from. Both are DIFFERENT. I suggest you read up on the PIT(Programmable Interval Timer) first. IIRC there are several good docs at OSRC and [url=http://www.osdever.net"]osdever.net[/url]

Re:Timer Interrupt

Posted: Thu Mar 25, 2004 9:20 am
by ich_will
Thanks I got it to work. If you know which values to write to the PIT its really easy.

Now I add everytime an interrupt occurse 1 to a counter, with this I can check small time intervals. I write 4 functions, one to return the current hour, minute and second. And one to return the current nanosecond count.

Re:Timer Interrupt

Posted: Thu Mar 25, 2004 9:22 am
by DennisCGc
ich_will wrote: Thanks I got it to work. If you know which values to write to the PIT its really easy.
Okay, don't forget to add a copyright sign:
Timer by DennisCGc (C) ;D ;)
(kidding, else I wasn't on this forum ;) )
Glad to hear it works.

Now I add everytime an interrupt occurse 1 to a counter, with this I can check small time intervals. I write 4 functions, one to return the current hour, minute and second. And one to return the current nanosecond count.
In my OS, you just call: get time, and you get the time in hours, minutes, seconds, and milliseconds.

Re:Timer Interrupt

Posted: Thu Mar 25, 2004 10:39 am
by ASHLEY4
Okay, don't forget to add a copyright sign:
Timer by DennisCGc (C) ;D ;)
(kidding, else I wasn't on this forum ;) )
Glad to hear it works.
DennisCGc you are definitely a Bill Gate's in the making ;) .

ASHLEY4.

Re:Timer Interrupt

Posted: Thu Mar 25, 2004 1:08 pm
by Neo
ASHLEY4 wrote:
Okay, don't forget to add a copyright sign:
Timer by DennisCGc (C) ;D ;)
(kidding, else I wasn't on this forum ;) )
Glad to hear it works.
DennisCGc you are definitely a Bill Gate's in the making ;) .

ASHLEY4.
yes definitely he sounds like a BILL .....
;)

Re:Timer Interrupt

Posted: Fri Mar 26, 2004 11:46 am
by DennisCGc
Neo wrote:
ASHLEY4 wrote:
Okay, don't forget to add a copyright sign:
Timer by DennisCGc (C) ;D ;)
(kidding, else I wasn't on this forum ;) )
Glad to hear it works.
DennisCGc you are definitely a Bill Gate's in the making ;) .

ASHLEY4.
yes definitely he sounds like a BILL .....
;)
I was just kidding, why should he add a copyright sign with my name on it ? ::)
And another thing: don't swear! I'm not Bill Gates ;D

Re:Timer Interrupt

Posted: Sun Mar 28, 2004 4:06 am
by mr. x
It seems that my counter counts 10 seconds in one real second.. :/

Re:Timer Interrupt

Posted: Sun Mar 28, 2004 6:51 am
by ich_will
In my init_timer function I add and convert all (year, month, days, hour, minute, second) to a counter, it'll increment one time a second. But I have problems to get days, month and year form the counter.

Code: Select all

  // initialize counter secs
  secs = year;
  secs *= 365; // convert years to days
  secs += month_to_days[month-1];  // convert current month to days of year
  secs += day;
  secs *= 24;  // convert days to hours
  secs += hour;
  secs *= 60;  // convert hours to minutes
  secs += minute;
  secs *= 60;  // convert minutes to seconds
  secs += second;
this functions work:

Code: Select all

unsigned char get_hour(){ return (secs/60/60)%24; }

unsigned char get_min(){ return (secs/60)%60; }

unsigned char get_sec(){ return secs%60;}
but what should I do if I want to get the days:

(secs/60/60/24)%31 // what should I use for a value instead
// of 31 this value depends not on the days of month

it failes

Re:Timer Interrupt

Posted: Sun Mar 28, 2004 7:07 am
by ich_will
Oh I forgot to send the month_to_days array

Code: Select all

// copied from mobius OS
static const unsigned short month_to_days[12] = {
/*  jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
0,
31,
31 + 28,
31 + 28 + 31,
31 + 28 + 31 + 30,
31 + 28 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
};

Re:Timer Interrupt

Posted: Sun Mar 28, 2004 10:00 am
by Schol-R-LEA
I was going to complain that this was inelegant, since it means you have to have the number of days in a given month twice in the source code, but then it occurred to me that a function which would calculate this at runtime would not merely be slower, but also take up a good deal more than 24 bytes. Fine. However, I could point out that it does not account for leap years...

Re:Timer Interrupt

Posted: Sun Mar 28, 2004 10:29 am
by Candy
Schol-R-LEA wrote: However, I could point out that it does not account for leap years...
Adding leapyears would augment the function to be special-cased, so you might as well check the month and if it's larger than feb then you add 1 to the day count. It's not nice but at least it keeps days orthogonal to years (IE you don't have to modify the days to keep the years in sync, & vice versa)

If properly coded that change could fit in 5 bytes (2 for test, 2 for jne and 1 for inc) as opposed to the additional 24 or 72 bytes for a bigger table (not including the extra math for reading that table properly, where the table is 24 bytes larger for just a leapyear-lookup and 72 bytes larger for a full 4-year lookup).

One more thing, do you consider calculating summer- and wintertime in Windows-style or in Unix-style (IE: do you adjust the cmos clock to adjust for winter- or summertime or do you add an hour yourself during wintertime)?

Another question, has anybody got experience with using windows for anything that checks dates while going to wintertime? AFAIK, you'd get dates & times an hour in the future, so some software should trip :).