Page 1 of 1

System clock

Posted: Sun Nov 20, 2005 12:00 am
by daman
How would I get the current time on the computers clock for my OS through c?

Re: System clock

Posted: Sun Nov 20, 2005 12:00 am
by earlz
16bit or 32bit?
16bit you use the
Int 0x1A
Entry:
AH = 00h
Return:
CX = high (most signif.) portion of clock count
DX = low (least signif.) portion of clock count
AL = 0 if clock was read or written (via AH=0,1)
within the current 24-hour period
Otherwise, AL > 0

im not sure about how to interpret cx and dx to hour:minute:second though

32bit:
I have no idea

Re: System clock

Posted: Mon Nov 21, 2005 12:00 am
by bubach
here's my code, as it's just simple in's and out's you should be able to translate it to C yourself.

Code: Select all

;----------------------------------------------------------;
; BOS kernel                     Christoffer Bubach, 2005. ;
;----------------------------------------------------------;
;                                                          ;
;     To get stuff ( time & date ) from CMOS memory.       ;
;                                                          ;
;----------------------------------------------------------;


;-----------------------------------;
;  variables containing CMOS data   ;
;-----------------------------------;
	  century	  db   0		      ; latest century,
	  year		  db   0		      ; year,
	  month 	  db   0		      ; month,
	  day		  db   0		      ; day (1 = sunday),
	  hour		  db   0		      ; hour,
	  minute	  db   0		      ; minute and
	  second	  db   0		      ; second read in from CMOS.


;-------------------------;
;  save info from CMOS    ;
;-------------------------;
get_cmos_data:
	  push	  ax

	  mov	  al, 0x00			      ; get the "second" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [second], al			      ; save it.

	  mov	  al, 0x02			      ; get the "minute" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [minute], al

	  mov	  al, 0x04			      ; get the "hour" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [hour], al

	  mov	  al, 0x07			      ; get the "day" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [day], al

	  mov	  al, 0x08			      ; get the "month" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [month], al

	  mov	  al, 0x09			      ; get the "year" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [year], al

	  mov	  al, 0x32			      ; get the "century" byte
	  out	  0x70, al
	  in	  al, 0x71
	  mov	  [century], al

	  pop	  ax
	  ret

;------------------------------------------------;
;   calculate binary from BCD                    ;
;                              in:  al = BCD     ;
;                              out: al = bin     ;
;------------------------------------------------;
BCD2bin:
	  push	  ebx
	  mov	  bl, al			      ; bl = al mod 16
	  and	  bl, 0x0F
	  shr	  al, 4 			      ; al = al / 16
	  mov	  bh, 10
	  mul	  bh				      ; multiply by 10
	  add	  al, bl			      ; add in low nib
	  pop	  ebx
	  ret


;------------------------------------------------;
;    calculate ASCII from BCD                    ;
;                              in:  al = BCD     ;
;                              out: ax = ASCII   ;
;------------------------------------------------;
BCD2ascii:
	  push	  ecx
	  mov	  ah, al
	  and	  ax, 0xF00F			      ; mask bits
	  shr	  ah, 4 			      ; right shift ah to get unpacked BCD
	  or	  ax, 0x3030			      ; combine with 30 to get ASCII
	  xchg	  ah, al			      ; swap for ASCII storage convention
	  pop	  ecx
	  ret
sorry for the long post, thought you might need the BCD converting functions...

Re: System clock

Posted: Mon Nov 21, 2005 12:00 am
by mrkaktus
You shold better set bit 5 in CMOS B Register and write a INT that will handle IRQ8 which will be called every second. In that INT you will read time and date and store them in system variables. I'm doing in such way in my OS :).

Re: System clock

Posted: Mon Nov 21, 2005 12:00 am
by earlz
actually its irq1 but by default it is mapped to interrupt number 8 but in protected mode it interferes with exceptions, so you have to remap it

Re: System clock

Posted: Mon Nov 21, 2005 12:00 am
by carbonBased
Where can I get a listing of CMOS registers, btw?

Thanks,
Jeff

Re: System clock

Posted: Tue Nov 22, 2005 12:00 am
by mrkaktus
Hmmm, maybe you should invest some money in book like "PC Anatomy". We have such book in Poland it is great. So I think that there where you are living should be something like that also.

And for now , here you have some CMOS map:

http://www.gilanet.com/ohlandl/config/c ... sters.html

:)

Re: System clock

Posted: Tue Nov 22, 2005 12:00 am
by JAAman
well, actually it is IRQ0 and IRQ8, they are both clock interupts, though IRQ8 has a lower priority

most people just keep track of the time within the OS, and only read the clock occasionally, for acuracy purposes

they would keep a counter and inc it on each clock, then inc the timer.sec when it reaches the correct value (clocks/sec)

the reason for this is that retreaving the values from CMOS is extreamly slow (on some systems it may take longer than the time between clock tics! -- which will cause your OS to be very unresponsive)

Re: System clock

Posted: Tue Nov 22, 2005 12:00 am
by mrkaktus
hckr83 you wrote: "actually its irq1 but...". From where you get that it is IRQ1 ???

Re: System clock

Posted: Tue Nov 22, 2005 12:00 am
by carbonBased
From what I can recall:

IRQ0 -> System timer
IRQ1 -> Keyboard
IRQ2 -> ??
IRQ3 -> Serial
IRQ4 -> Serial
IRQ5,6 -> ??
IRQ7 -> Printer
IRQ8 -> CMOS RTC

So, yeah, the IRQ1 comment is off... but may have just been a typo.

IRQ0, without remapping, is set to INT8... but, on most OSs I've seen, that's fairly irrelevant, as IRQ0 is used for task switching (could also be used to keep the time, though).

Re: System clock

Posted: Tue Nov 22, 2005 12:00 am
by earlz
oops i meant irq0
cause if i though it was irq1 my uptime counter on my os would be way off

Re: System clock

Posted: Tue Nov 22, 2005 12:00 am
by JAAman
carbonBased:
IRQ2 -> Chain to Slave PIC


afaik, most OSs do use IRQ0 also to track time (reading occasionally to maintain acuracy) -- though i've seen at least one person who uses IRQ8 to update OS time