sleep(seconds/milliseconds) ?
sleep(seconds/milliseconds) ?
Is there a function with PS2SDK, similar to sleep, that will allow me to create a delay that DOESN'T depend on the processor's clock cycles, but instead on the PS2's internal clock?
I may be lazy, but I can...zzzZZZzzzZZZzzz...
-
- Posts: 202
- Joined: Wed Aug 09, 2006 1:00 am
I don't think there is. You could use the timed delay functions from http://forums.ps2dev.org/viewtopic.php?t=2842 pretty easily. uLE also uses a timer that uses T0.
Whats the difference there? I dont think the EE has an internal clock. It does have internal timers. I have recently implemented the clock() function and plan to check it in soon. I was then going to look at a sleep() function, prefereably one that uses signals and interrupts rather than busy waits.Is there a function with PS2SDK, similar to sleep, that will allow me to create a delay that DOESN'T depend on the processor's clock cycles, but instead on the PS2's internal clock?
ragnarok2040: Yes, I had JUST found that when I checked back here, and saw your post :) Thanks; I'll dig through that. It looks like it should suffice.
radad: Cool; let us all know when you've uploaded it (or added it to the SVN?).
I just need a reliable timing system, and depending entirely on clock cycles or graphic refreshes seems inefficient, and, well...unreliable, heh.
I'll check into that thread, however. It looks to be promising. Thanks for the help, guys.
radad: Cool; let us all know when you've uploaded it (or added it to the SVN?).
I just need a reliable timing system, and depending entirely on clock cycles or graphic refreshes seems inefficient, and, well...unreliable, heh.
I'll check into that thread, however. It looks to be promising. Thanks for the help, guys.
I may be lazy, but I can...zzzZZZzzzZZZzzz...
I have added my implemention of clock() to SVN: http://svn.ps2dev.org/listing.php?repna ... =1505&sc=0.
You could use this to write a crude sleep:
It uses EE timer 1 by default. EE timer 0 is been used by the profiling code. Timers are a rare resource so this may need to managed better in the future if there are more needs.
I did a small test to see if you could add two interrupt handlers for the same interrupt but it didnt seem to work. I was hoping because they get their own id the kernel would manage the chaining of them. There is also a 'next' parameter in the add interrupt call that I thought might be a way chaining them but that didnt work either.
You could use this to write a crude sleep:
Code: Select all
unisgned int sleep(unisgned int secs)
{
clock_t w = clock() + secs * CLOCKS_PER_SEC;
while (clock() < w)
;
return 0;
}
I did a small test to see if you could add two interrupt handlers for the same interrupt but it didnt seem to work. I was hoping because they get their own id the kernel would manage the chaining of them. There is also a 'next' parameter in the add interrupt call that I thought might be a way chaining them but that didnt work either.
I seem to recall that if you pass 0 as the 'next' parameter the handler set as the first handler in the chain, if you pass -1 the handler is added at the end of the chain of interrupt handlers.radad wrote:There is also a 'next' parameter in the add interrupt call that I thought might be a way chaining them but that didnt work either.
Code: Select all
AddIntcHandler(INTC_XXX, handler1, 0) <- first
AddIntcHandler(INTC_XXX, handler2, -1) <- second
AddIntcHandler(INTC_XXX, handler3, -1) <- third
So if you pass 0 as the 'next' parameters does that wipe out the existing chain?
I have implemented the sleep() function now: http://svn.ps2dev.org/listing.php?repna ... =1506&sc=0
It uses a semaphore to block on and sets up an alarm to periodically check. What I wasnt sure of was what period to check. To get higher accuracy you want to check more often but you dont want to overload the cpu just with the checking. I settled on 600 H-SYNCs, so it is slightly different on PAL vs NTSC but I dont think it should matter.
I have implemented the sleep() function now: http://svn.ps2dev.org/listing.php?repna ... =1506&sc=0
It uses a semaphore to block on and sets up an alarm to periodically check. What I wasnt sure of was what period to check. To get higher accuracy you want to check more often but you dont want to overload the cpu just with the checking. I settled on 600 H-SYNCs, so it is slightly different on PAL vs NTSC but I dont think it should matter.
radad: After grabbing those files and placing them in the libc directory of ps2sdksrc, in their proper places, I tried to compile libc, but got the error:
I only see CLOCKS_PER_SEC mentioned in the clock test, so I'm guessing you forgot to declare this?
Code: Select all
src/unistd.c: In function `sleep':
src/unistd.c:88: `CLOCKS_PER_SEC' undeclared (first use in this function)
src/unistd.c:88: (Each undeclared identifier is reported only once
src/unistd.c:88: for each function it appears in.)
make: *** [obj/sleep.o] Error 1
I may be lazy, but I can...zzzZZZzzzZZZzzz...
Ah, OK. For now, I just copied everything inside libc/include to ps2sdk/ee/include. I'll keep make release in mind next time.
I've tried tossing together a simple test of this sleep function, but it doesn't seem to be working as planned. Perhaps you can point out my mistake faster than I can find it myself...
Once it prints "test!" to the screen, it stops, forever. I'll tinker around with it, but a nudge in the right direction would be greatly appreciated.
I've tried tossing together a simple test of this sleep function, but it doesn't seem to be working as planned. Perhaps you can point out my mistake faster than I can find it myself...
Code: Select all
#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <stdio.h>
#include <debug.h>
#include <time.h>
unsigned int sleep(unsigned int secs)
{
clock_t w = clock() + secs * CLOCKS_PER_SEC;
while (clock() < w)
;
return 0;
}
int main(int argc, char *argv[])
{
SifInitRpc(0);
init_scr();
scr_printf("test!\n");
sleep(10);
scr_printf("Hello, world!\n");
printf("Hello, world!\n");
while(1) {}
return 0;
}
I may be lazy, but I can...zzzZZZzzzZZZzzz...
sleep() is in the ps2sdk as well so you dont need to use that version anymore. Anyway didnt you get a duplicate definition warning?
You may have the issue with the libc init I mentioned in the crt0 thread. Try printing out clock() before you call sleep:
If this prints zero then libc wasnt initialized.
You may have the issue with the libc init I mentioned in the crt0 thread. Try printing out clock() before you call sleep:
Code: Select all
printf("clock: %d\n", (int) clock());
Indeed, it returns 0 (which means clock will always be less than w, unless w == 0). I'm not exactly following the solution in that thread. What would I need to do to either...
a. Call libc, which would at least allow it to work for the moment, or
b. Actually fix the problem in PS2SDK?
a. Call libc, which would at least allow it to work for the moment, or
b. Actually fix the problem in PS2SDK?
I may be lazy, but I can...zzzZZZzzzZZZzzz...
The easiest way just to get clock working is to call:
It wont hurt if it is called twice, just resets clock() to zero.
The real fix is a new crt0.s but its not checked in yet.
Code: Select all
void _ps2sdk_time_init(void);
The real fix is a new crt0.s but its not checked in yet.
Adding void _ps2sdk_time_init(void); doesn't seem to fix the problem (I've declared it above main(), but I've also tried it within main(), and within the little sleep() function)...
So you've created a new crt0.s, but it's not up yet? I'll be eagerly awaiting it.
So you've created a new crt0.s, but it's not up yet? I'll be eagerly awaiting it.
I may be lazy, but I can...zzzZZZzzzZZZzzz...
You need to call it too:
Code: Select all
void _ps2sdk_time_init(void);
....
int main(int argc, char *argv[])
{
_ps2sdk_time_init();
SifInitRpc(0);
....
I see. However, I'm still returning 0 for clock, and halting thereafter:
Thanks for being patient. I'm still fairly new to C, and PS2Dev, though I'm a relatively fast learner, considering the limited amount of free time I have.
Code: Select all
#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <stdio.h>
#include <debug.h>
#include <time.h>
unsigned int sleep(unsigned int secs)
{
clock_t w = clock() + secs * CLOCKS_PER_SEC;
while (clock() < w)
;
return 0;
}
void _ps2sdk_time_init(void);
int main(int argc, char *argv[])
{
_ps2sdk_time_init();
SifInitRpc(0);
init_scr();
scr_printf("test!\n");
scr_printf("clock: %d\n", (int) clock());
sleep(1);
scr_printf("Hello, world!\n");
while(1) {}
return 0;
}
I may be lazy, but I can...zzzZZZzzzZZZzzz...
I get no warnings nor errors, and the regress tests compile properly, but seem to do nothing (I haven't really taken a look to see if they're supposed to output anything, so perhaps they're functioning as planned).
I'll spend some time pieces everything apart, and see if I can figure out the problem. Thanks for your help, and let me know when you check the new crt0.s in.
I'll spend some time pieces everything apart, and see if I can figure out the problem. Thanks for your help, and let me know when you check the new crt0.s in.
I may be lazy, but I can...zzzZZZzzzZZZzzz...
-
- Posts: 202
- Joined: Wed Aug 09, 2006 1:00 am
I tested out your problem LBGSHI and it's definitely the crt0.s. Your code works fine with the new one. If you want to try out the new crt0.s, I separated my latest changes into separate patches.
http://forums.ps2dev.org/viewtopic.php?t=11546#78544
To apply:orwhile you're in the root directory of ps2sdk's src or $PS2SDKSRC directory.
http://forums.ps2dev.org/viewtopic.php?t=11546#78544
To apply:
Code: Select all
patch -p0 < crt0.patch
Code: Select all
cat crt0.patch | patch -p0