forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using sin and rand functions

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
C-Be



Joined: 15 Apr 2004
Posts: 5

PostPosted: Tue Apr 20, 2004 7:15 am    Post subject: Using sin and rand functions Reply with quote

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
Back to top
View user's profile Send private message
Saotome



Joined: 03 Apr 2004
Posts: 182

PostPosted: Tue Apr 20, 2004 8:11 am    Post subject: Reply with quote

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).
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Tue Apr 20, 2004 9:10 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
C-Be



Joined: 15 Apr 2004
Posts: 5

PostPosted: Tue Apr 20, 2004 6:55 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
pixel



Joined: 30 Jan 2004
Posts: 791

PostPosted: Tue Apr 20, 2004 8:49 pm    Post subject: Reply with quote

Add -lgcc to your linking phase.
_________________
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
Back to top
View user's profile Send private message
blackdroid



Joined: 17 Jan 2004
Posts: 564
Location: Sweden

PostPosted: Tue Apr 20, 2004 9:55 pm    Post subject: Reply with quote

just for the record, the vector units have rinit, rnext, rget.. ( prepend a v if you want cop2 to use them ).
_________________
Kung VU
Back to top
View user's profile Send private message Visit poster's website
C-Be



Joined: 15 Apr 2004
Posts: 5

PostPosted: Wed Apr 21, 2004 12:30 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Wed Apr 21, 2004 2:11 am    Post subject: Reply with quote

No, it needs to be last.
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Wed Apr 21, 2004 2:14 am    Post subject: Reply with quote

Also, you can use the PS2's RTC as an intital random seed. See libcdvd in ps2lib for the function (cdReadClock(), IIRC).
Back to top
View user's profile Send private message
C-Be



Joined: 15 Apr 2004
Posts: 5

PostPosted: Wed Apr 21, 2004 6:07 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Wed Apr 21, 2004 6:54 am    Post subject: Reply with quote

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)
_________________
Shoot Pixels Not People!
Makeshift Development
Back to top
View user's profile Send private message Visit poster's website
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Wed Apr 21, 2004 9:24 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
mharris



Joined: 25 Jan 2004
Posts: 155
Location: Annapolis, MD, USA

PostPosted: Wed Apr 21, 2004 10:43 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
blackdroid



Joined: 17 Jan 2004
Posts: 564
Location: Sweden

PostPosted: Wed Apr 21, 2004 3:57 pm    Post subject: Reply with quote

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 ).
_________________
Kung VU
Back to top
View user's profile Send private message Visit poster's website
C-Be



Joined: 15 Apr 2004
Posts: 5

PostPosted: Wed Apr 21, 2004 6:00 pm    Post subject: Reply with quote

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 :)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group