Page 1 of 1

broken random number generator

Posted: Thu Mar 24, 2005 1:57 am
by Raizor
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

Posted: Thu Mar 24, 2005 4:11 am
by pixel
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: Select all

__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: Select all

__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

Posted: Thu Mar 24, 2005 4:16 am
by Raizor
Merci Pixel :)

Posted: Thu Mar 24, 2005 6:40 am
by Saotome
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