| View previous topic :: View next topic |
| Author |
Message |
Raizor
Joined: 18 Jan 2004 Posts: 60 Location: out there
|
Posted: Thu Mar 24, 2005 1:57 am Post subject: broken random number generator |
|
|
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 |
|
 |
pixel
Joined: 30 Jan 2004 Posts: 791
|
Posted: Thu Mar 24, 2005 4:11 am Post subject: |
|
|
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 |
|
 |
Raizor
Joined: 18 Jan 2004 Posts: 60 Location: out there
|
Posted: Thu Mar 24, 2005 4:16 am Post subject: |
|
|
| Merci Pixel :) |
|
| Back to top |
|
 |
Saotome

Joined: 03 Apr 2004 Posts: 182
|
Posted: Thu Mar 24, 2005 6:40 am Post subject: |
|
|
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 |
|
 |
|