broken random number generator

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
Raizor
Posts: 60
Joined: Sun Jan 18, 2004 12:27 am
Location: out there

broken random number generator

Post 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
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post 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
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.
Raizor
Posts: 60
Joined: Sun Jan 18, 2004 12:27 am
Location: out there

Post by Raizor »

Merci Pixel :)
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post 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
infj
Post Reply