Code: Select all
while(1)
{
ppu_wait_frame();
rand16();
if(pad_trigger(0)&PAD_START) break;
}I'm still a noob when it comes to NES internals, so apologies if this is addressed somewhere that I've missed. It's tricky to search for "C"!
Moderator: Moderators
Code: Select all
while(1)
{
ppu_wait_frame();
rand16();
if(pad_trigger(0)&PAD_START) break;
}The NES is capable of running all standard PRNG algorithms, even modern ones. Most NES developers favour simpler ones so they can use them often and not eat up too many cyles, and the quality is sometimes not an issue (e.g. AI fuzzy logic doesn't usually require a high quality PRNG).Scoth42 wrote:I know getting anything close to true random numbers (or PRNG even) out of an NES is not really feasible
Code: Select all
;Galois random generator, found somewhere
;out: A random number 0..255
rand1:
lda <RAND_SEED
asl a
bcc @1
eor #$cf
@1:
sta <RAND_SEED
rts
rand2:
lda <RAND_SEED+1
asl a
bcc @1
eor #$d7
@1:
sta <RAND_SEED+1
rts
_rand8:
jsr rand1
jsr rand2
adc <RAND_SEED
rts
;unsigned int __fastcall__ rand16(void);
_rand16:
jsr rand1
tax
jsr rand2
rts
;void __fastcall__ set_rand(unsigned char seed);
_set_rand:
sta <RAND_SEED
stx <RAND_SEED+1
rtsCode: Select all
;
; 6502 16-bit Galois LFSR
;
; unsigned int __fastcall__ rand16(void);
_rand16:
asl <RAND_SEED
rol <RAND_SEED+1
bcc @1
lda <RAND_SEED
eor #$E7
sta <RAND_SEED
tax
lda <RAND_SEED+1
eor #$8D
sta <RAND_SEED+1
rts
@1:
ldx <RAND_SEED
lda <RAND_SEED+1
rts
//
// equivalent C++
//
static unsigned short int rand_seed = 1; // note: a value of 0 will kill the LFSR
unsigned short int rand16()
{
bool high_bit = (rand_seed & 0x8000) != 0;
rand_seed = rand_seed << 1;
if (high_bit)
{
rand_seed = rand_seed ^ 0x8DE7;
}
return rand_seed;
}I've just reuploaded a slightly updated version to http://iamscott.net/robotfindskitten.nes . Turns out I misunderstood my emulator's presentation of NTSC cutoff and it likely would have been screwed up. Also fixed a bug with robot getting to move out of bounds. This should be a good baseline version, although it's been suggested I look into proportional fonts for the NKOs. May or may not get to that; I'm fairly pleased with it as it is.tepples wrote:Thank you. It works in FCEUX. I noticed the 20K of NKIs in the ROM; how did you determine what to include and what to leave out?
So is it OK to make and sell copies of this game on the next community multicart?