Page 1 of 1
Using sin and rand functions
Posted: Tue Apr 20, 2004 7:15 am
by C-Be
Hi,
i have a probably very easy starter question, but i just can't figure it out on my own.
I have installed the ps2 dev environment and have writen some code, that runs fine on the ps2 emulator (haven't gotten it to run on my ps2 for now) but now i want to use some functions such as sin and rand but including math.h and stdlib.h doesn't work. Do i have to install newlib or use the compiler flag -lm -lc etc?
Because that doen't seem to work. I just used the code from the "Dreamtime's PS2DEV Tutorial #1" and had hoped i could just use rand() etc.
If someone could help me out i really appreciate it.
tnx,
Siebe
Posted: Tue Apr 20, 2004 8:11 am
by Saotome
instead of rand() you could use the
"mersenne twister"
its faster (4 times faster than the standard rand()),
and you get better "random" values (longer period) than with the standart function (read in the link above).
Posted: Tue Apr 20, 2004 9:10 am
by mrbrown
Yes, you need to install newlib and pass -lm and -lc on the linker command line. Also, it's better to use the single-precision versions of the math routines (sinf() instead of sin()), as the compiler has to emulate double-precision floats for the PS2.
Posted: Tue Apr 20, 2004 6:55 pm
by C-Be
Thanks for the help.
i actually am using another random function somewhat like mersenne twister, but i need a first really random number or else every time i run the program al randoms are the same for every run. So i want 1x rand() and then 8000x the other random().
I got the sinf() working with newlib and the -lm -lc linker commands but now when i use rand() i get the following:
c:/ps2dev/newlib/lib/libc.a(rand.o): In function `rand':
/home/Nickvv/source/ps2-build/newlib-build/mips64r5900-ps2-elf/newlib/libc/stdli
b/../../../../../../ps2/newlib-1.10.0/newlib/libc/stdlib/rand.c:84: undefined re
ference to `__muldi3'
Does anybody know how to fix this or what i am doing wrong?
Thanks,
Siebe
Posted: Tue Apr 20, 2004 8:49 pm
by pixel
Add -lgcc to your linking phase.
Posted: Tue Apr 20, 2004 9:55 pm
by blackdroid
just for the record, the vector units have rinit, rnext, rget.. ( prepend a v if you want cop2 to use them ).
Posted: Wed Apr 21, 2004 12:30 am
by C-Be
I had tried -lgcc already but it doesn't help.
I put it in front of the -lm and -lc, thats where it should be right?
Posted: Wed Apr 21, 2004 2:11 am
by mrbrown
No, it needs to be last.
Posted: Wed Apr 21, 2004 2:14 am
by mrbrown
Also, you can use the PS2's RTC as an intital random seed. See libcdvd in ps2lib for the function (cdReadClock(), IIRC).
Posted: Wed Apr 21, 2004 6:07 am
by C-Be
Hey, that worked, amazing:
mrbrown, you the man! :)
I have got one other small question.
I tested the code i wrote on the ps2 emulator only (i don't have my ps2 at hand all the time :( ) and it worked just fine on it. But when i tried to upload it with InLink using Pukklink on my ps2 it gave the following error:
Exception handler: Address store exception
\par
\par Cause 60008014 BadVAddr 003f46d4 Status 70030c13 EPC 002040b4
\par
\par zero: 00000000000000000000000000000000 t8: 00000000000000000000000000000000
\par at: 00000000000000000000000000000001 t9: 00000000000000000000000000000000
\par v0: 00000000000000000000000000204740 s0: 00000000000000000000000000000000
\par v1: 00000000000000000000000000000190 s1: 00000000000000000000000000000000
\par a0: 00000000000000000000000000000000 s2: 00000000000000000000000000000000
\par a1: 00000000000000000000000000204744 s3: 00000000000000000000000000000000
\par a2: 0000000000000000FFFFFFFF80017B88 s4: 00000000000000000000000000000000
\par a3: 00000000000000000000000000000000 s5: 00000000000000000000000000000000
\par t0: 00000000000000000000000000001000 s6: 00000000000000000000000000000000
\par t1: 00000000000000000000000000000000 s7: 00000000000000000000000000000000
\par t2: 00000000000000000000000000000000 k0: 00000000000000000000000000000000
\par t3: 00000000000000000000000000000000 k1: 00000020810200040000000000000000
\par t4: 00000000000000000000000000000000 gp: 0000000000000000000000000020C6F0
\par t5: 00000000000000000000000000000000 sp: 000000000000000000000000003F46A4
\par t6: 00000000000000000000000000000000 fp: 000000000000000000000000000D6B60
\par t7: 00000000000000000000000000000000 ra: 0000000000000000000000000020425C
I havent got a clue as to what i did wrong. Must be some programming error, but what? Or should i change some stuff to make it work with pukklink etc?
Posted: Wed Apr 21, 2004 6:54 am
by Drakonite
Those types of errors usually occur from trying to write to memory that either doesn't exist (i.e. just past end of RAM) or you don't have permission to write to (i.e. kernel memory)
Posted: Wed Apr 21, 2004 9:24 am
by mrbrown
One of the best methods of tracking down bugs is to get someone else to look at your code. Try to track down where your program breaks by sprinkling printf() around to get a basic trace of your program's execution. Once you pinpoint the line (or lines) that are broken, respond in this thread with the code or post it somewhere.
If the line(s) refer to other variables or functions then you should post those too.
Other than that, we can't help you if we don't know what's wrong :P.
Posted: Wed Apr 21, 2004 10:43 am
by mharris
mrbrown wrote:Try to track down where your program breaks by sprinkling printf() around to get a basic trace of your program's execution.
This is OT, but that reminded me of a problem I've run into...
Since ps2link apparently uses UDP to send its prints to the host, I've run into some issues where print statements come out in the wrong order (and some packets are disappearing entirely). That's an additional challenge...
Has anyone else run into the same thing? I probably don't have the latest of anything (ps2lib, ps2link, pksh, etc.) so maybe it's already been addressed.
Posted: Wed Apr 21, 2004 3:57 pm
by blackdroid
looks like stack trashing ( sp register is the closest to BadVAddr ) .. the address is not dividable by 8, and storing a doubleworld will cause an exception. you can do an ee-objdump on your elf to see where the exception is generated ( check the EPC address ).
Posted: Wed Apr 21, 2004 6:00 pm
by C-Be
Using printf() is a good idea. They will show up in the InLink log window i presume. I was going to commend out code until it started working, but i will try the printf() first.
Unfortunally I am stuck to the emulator for a few days before i can test it on a real ps2. But when i narrowed it down but can't solve it (i hope i can offcourse) i will post it up here.
I tried to remove/avoid all use of doubles, i hope this helps (?)
And thanks again for all the help :)