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 

broken random number generator

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



Joined: 18 Jan 2004
Posts: 60
Location: out there

PostPosted: Thu Mar 24, 2005 1:57 am    Post subject: broken random number generator Reply with quote

I've been using a random number seeder for a while and recently found out it's returning the same number each time:

void Randomize()
{
register long ret = 0;
__asm__ volatile("mfc0 %0, $1" : :"r"(ret));
__asm__ volatile("sync.p");
SeedGenerator(ret);
}

SeedGenerator is getting called with the same value for 'ret' each time this is run. Anyone have any ideas what I'm doing wrong please?

thanks,

Raizor
Back to top
View user's profile Send private message
pixel



Joined: 30 Jan 2004
Posts: 791

PostPosted: Thu Mar 24, 2005 4:11 am    Post subject: Reply with quote

First, having a look at the generated code could be an idea. Always doing that is great to understand what gcc does.

Second, do you really want $1 ? Not $9 ? CPU clock that is.

Third, you should merge the two asm blocs:

Code:
__asm__ volatile("mfc0 %0, $1\nsync.p" ...)


Fourth, you need to say to gcc you want ret to be an *output*. You're putting it as the input field. The correct syntax would finally be:

Code:
__asm__ volatile("mfc0 %0, $1\nsync.p" : "=r"(ret));


(notice that: 1) it's not in the same : bloc and 2) the presence of the = to say it's an output)


The last but not the least, there's cpu_ticks in the timer.c's of ps2sdk's kernel which basically does what you asked for :P
_________________
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
Raizor



Joined: 18 Jan 2004
Posts: 60
Location: out there

PostPosted: Thu Mar 24, 2005 4:16 am    Post subject: Reply with quote

Merci Pixel :)
Back to top
View user's profile Send private message
Saotome



Joined: 03 Apr 2004
Posts: 182

PostPosted: Thu Mar 24, 2005 6:40 am    Post subject: Reply with quote

if you are reading cop0 registers you don't need the sync.p

only if you're writing to cop0:
EE Core Instruction Set Manual wrote:
To guarantee COP0 register update, it is necessary to place a SYNC.P instruction after the MTC0 instruction...


saves 4 bytes, and maybe some cycles ;P
_________________
infj
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