sleep or delay function help

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
tic tac

Re:sleep or delay function help

Post by tic tac »

since processor cycling is really in these days,
there is a good chance that 64-bit value
overflows many times per sec
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:sleep or delay function help

Post by Candy »

the code poet wrote: since processor cycling is really in these days,
there is a good chance that 64-bit value
overflows many times per sec
Why can't you just use a single name?

Ontopic: before a 64-bit value overflows with a processor running at 4 GHZ, you're running it for over 140 years. I seriously doubt you will see that happen.
EclipseOS

Re:sleep or delay function help

Post by EclipseOS »

Okay I tried the above codes and here's my problem: The sleep doesn't seem to wait the full second. Here's my code:

void sleep(int secs)
{
int x;
disable();
while (secs--)
{
x = bios_call(0);
while (bios_call(0) == x);
}
enable();
return;
}

int get_frequency()
{
unsigned int slow, shigh;
unsigned int elow, ehigh;
unsigned long start=0, end=0, result;
asm
(
"rdtsc\t\n"
:"=a"(slow), "=d"(shigh)
:
);
sleep(1);
asm
(
"rdtsc\t\n"
:"=a"(elow), "=d"(ehigh)
:
);
start |= shigh;
start <<= 32;
start |= slow;
end |= ehigh;
end <<= 32;
end |= elow;
result = ((end - start)/1)/1000000;
return result;
}

void tell_frequency()
{
int result = get_frequency();
char mhz[20];
itoa(result, mhz, 10);
cout << "<" << mhz << " MHZ" << "> ";
return;
}


I had to get rid of an extra "long" in the get_frequency() because DJGPP wouldn't let me compile it. It kept saying:

kernel.o(.text+0x12d2):kernel.cpp: undefined reference to `___udivdi3'

the sleep() waits the second if it doesn't have anything else to do after it is run, like:

void one()
{
sleep(1);
return;
}

The above works fine, but if I do:

void one()
{
sleep(1);
outportb (0x64, 0xfe); (reboots computer)
return;
}

It doesn't work. PLEASE HELP!

thanx
virusx

Re:sleep or delay function help

Post by virusx »

hi,
I think djgpp does not let 64 bit operations. I had a similar experience in GCC (linux).
Use Candy's method it is better.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:sleep or delay function help

Post by Candy »

virusx wrote: hi,
I think djgpp does not let 64 bit operations. I had a similar experience in GCC (linux).
Use Candy's method it is better.
you won't solve the problem with the thing I've suggested, the only thing that I actually did was decrease your stack footprint slightly (16 bytes) and help GCC not generate extra opcodes. You will still get the div error.

Note, if you use a <4.3 GHZ processor you won't notice the difference between a 32-bit divide and a 64-bit divide. Also, you could consider using the FP unit for the divide, since it's better at doing huge numbers with fair precision.

((end - start) * 1.0) / 1000000.0
EclipseOS

Re:sleep or delay function help

Post by EclipseOS »

Okay, I still can't get it to work. I did get it to detect the right amount in MHZ, but it only does it when it feels like it. There's somthing wrong with my sleep function. Could someone post a better way to sleep other than the way that I did it? Thanx.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:sleep or delay function help

Post by Pype.Clicker »

you may wish to read How do i tell CPU speed? in the WikiFAQ.

Among other things, you should make sure sleep(1) was issued at the start of a clock period by sleep()ing *before* the first RDTSC aswell ...
EclipseOS

Re:sleep or delay function help

Post by EclipseOS »

Okay I added one sleep(1) before the first rdtsc, but it only changed it to find 0 Mhz, so I added another second before the first rdtsc and it worked, only on my computer and my dads which are 1000 mhz and above. I tried it on a 366 Mhz celeron and it reported 0 Mhz. I'm not exactly sure what is going on, but I've decided to write a different sleep function because I think that it's the one at fault. I've been thinking of using the PIT, but I don;t know where to start. You all are probaby getting tired of me asking so many questions, but bare with me.
Post Reply