Author Topic: How do I read the computer's clock directly?  (Read 14402 times)

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
How do I read the computer's clock directly?
« on: December 07, 2010, 08:40:13 PM »
I'm Trying to make a OS independent of DOS so I was wondering how to read the clock directly?

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: How do I read the computer's clock directly?
« Reply #1 on: December 07, 2010, 10:03:03 PM »
This has less to do with how to use NASM and more to do with OS development.

Please visit OSDev.org for OS development related information.

To help, what you are looking for is information about the RTC.

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: How do I read the computer's clock directly?
« Reply #2 on: December 08, 2010, 01:49:52 PM »
Ok I'll take a look, thanks.

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: How do I read the computer's clock directly?
« Reply #3 on: December 08, 2010, 10:09:38 PM »
I have looked at the page, but I did not see anything on how to get the system clock directly.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do I read the computer's clock directly?
« Reply #4 on: December 09, 2010, 05:39:37 AM »
If you're still in 16-bit mode, int 15h. If not, port 70h.

Best,
Frank


Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: How do I read the computer's clock directly?
« Reply #5 on: December 09, 2010, 01:38:20 PM »
Ok, thanks do you happen to know how to get the time from port 70h? When I read port 70h what does it return and how is the time  formated?
« Last Edit: December 09, 2010, 01:40:39 PM by ThatGuy22 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do I read the computer's clock directly?
« Reply #6 on: December 09, 2010, 04:29:50 PM »
You don't read from port 70h, you write to port 70h and then read (write) to port 71h, I believe. What do you write? That link Keith gave you:

http://www.nondot.org/sabre/os/files/MiscHW/RealtimeClockFAQ.txt

runs it down as well as anything I've seen - better! Some ports work this way, and some don't... They call 'em "registers" which might be confusing. Think of 'em as "sub-ports". You write the register number to one port, and read/write the "answer" to the register selected in the next (usually?) port. Sometimes there's a separate "status" port that you have to check for "not busy" or "buffer empty" or such. You may need to check for "update in progress"  before you read time/date - apparently that's "status register 0Ah", bit 7. For a simple experiment, try something like...

Code: [Select]
mov dx, 70h

top:
mov al, 0 ; current second "register"
out dx, al
inc dx ; port 71h
in al, dx
call show_al_as_hex ; you provide
dec dx ; back to port 70h
jmp top

See if it "updates" every second, and what kind of garbage it hands you when "update in progress" is happening. Break out of it with control-alt-delete or pull the plug, whatever.

If you need to avoid "update in progress", you may want to loop until "update in progress" is true (bit 7 set), then loop until bit 7 is clear, then go read the clock (so you don't start just before "update in progress" goes off). May not be a problem.

As to the format, apparently BCD (Binary Coded Decimal) is the default - one decimal digit per nibble - display it as if it were hex (you just never see 'A' - 'F'). You can apparently change this to "binary" with some of the upper "registers"...

I wouldn't write anything to port 71h until you're sure you've got "read" down pat. :)

I don't think I've ever done anything with the clock with ports, but I think that's how it goes... Let us know!

Best,
Frank


Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: How do I read the computer's clock directly?
« Reply #7 on: December 09, 2010, 05:25:25 PM »
Ok, If I get it to work I will tell you, thanks. If I don't get it to work I may have to give up on it and try something else until I understand ports a little better than I do now.