Getting Real-Clock Time

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
Marine
Posts:2
Joined:Mon Mar 24, 2008 3:31 am
Getting Real-Clock Time

Post by Marine » Mon Mar 24, 2008 3:44 am

Hey,

Love your tutorials, definately useful in gaining knowledge in ASM and how the internal cogs of modern PC's work (Computing class in college only goes so far)

However, i want to be able to make a timer, and my kernel to sleep for a few seconds when loading (So you see my load screen rather than it flash off :P)

I have tried the following in my kernel Main() function (just to test it), and it resets bochs.

Code: Select all

__asm
{
   mov ah,00h
   int 1Ah
}

Is there a better way to make a timer/sleep method, or is this the right way just not working properly? :D

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Post by Andyhhp » Mon Mar 24, 2008 3:11 pm

Hi,

That piece of code wont work because in the kernel, you are in protected mode so all interrupts cause triple fault (sounds like what you are getting with bochs) until you write your own interrupt table and handlers.

Also, it seems that you are trying to use a DOS interrupt not a BIOS one (cant remember where the split is so this might not be accurate). This means that it will not work unless you are using DOS as an OS (which you arnt).

My suggestions are either:

1) write interrupt handlers and learn how to use the Programmable Interrupt Timer to signal when a certain time has elapsed.

2) If you have a Pentium Processor or later, you can use the RDTSC (ReaD TimeStamp Counter) instruction. There is information about its complete use in the Intel Reference manuals (253666 and 253667). Basically, it is a counter that is incremented on every clock tick.

e.g.

Code: Select all

wait_time: dq 0;reserve 64bits for value
...
rdtsc ;loads edx:eax with timestamp
add eax,0x3b9aca00 ;add 1billion to tsc value (1 second on a 1Ghz processor - you will have to alter this value to get a decent time)
adc edx,0 ;make sure any carry is accounted for

mov dword [wait_time],edx
mov dword [wait_time+4],eax ;store values

wait_loop:
rdtsc
cmp edx,dword [wait_time]
jl wait_loop
cmp eax,dword [wait_time+4]
jl wait_loop

;Your specified time has now elapsed
Note: This is untested code but its meaning should be clear.

Hope this helps,

Andrew
Image

Marine
Posts:2
Joined:Mon Mar 24, 2008 3:31 am

Post by Marine » Mon Mar 24, 2008 4:57 pm

Thanks, i thought it would be something like that stopping me :D

I'll have a look at interrupt tables right now.

Post Reply