Page 2 of 2
Re:sleep or delay function help
Posted: Thu May 27, 2004 2:40 am
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
Re:sleep or delay function help
Posted: Thu May 27, 2004 3:05 am
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.
Re:sleep or delay function help
Posted: Thu May 27, 2004 2:26 pm
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
Re:sleep or delay function help
Posted: Thu May 27, 2004 11:12 pm
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.
Re:sleep or delay function help
Posted: Thu May 27, 2004 11:16 pm
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
Re:sleep or delay function help
Posted: Mon May 31, 2004 11:53 pm
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.
Re:sleep or delay function help
Posted: Tue Jun 01, 2004 3:12 am
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 ...
Re:sleep or delay function help
Posted: Wed Jun 02, 2004 1:02 pm
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.