Re: [stella] entropy & randomness

Subject: Re: [stella] entropy & randomness
From: "B. Watson" <atari@xxxxxxxxxxxxxx>
Date: Tue, 9 Oct 2001 11:34:52 -0400 (EDT)
On Tue, 9 Oct 2001, Kevin Horton wrote:

> As for seeding it, I suggest this method.  It seems to work very effectively.
> Note I also initialize the 2600 at the same time by writing 000h to all 
> zeropage addresses, and I set the SP to 0ffh.
> init2600:         ldx #000h     ;start at byte 00000h in RAM
> ir_loop:          txa
>                    eor #03h      ;prevent XORing regs with themselves
>                    and #03h      ;make sure all 4 regs get a chance at being
>                    tay           ;seeded
>                    lda 000h,x
>                    eor rand0,y   ;xor is best for this since all bits have an
>                    sta rand0,y   ;equal chance at being flipped.
>                    eor lda #0    ;load #00h into RAM
>                    sta 000h,x
>                    txs           ;set stack to 0ffh at end of loop :-)
>                    inx
>                    bne ir_loop
>                    lda rand0
>                    ora rand1
>                    ora rand2
>                    ora rand3     ;check for 000000000h state. bad juju if so
>                    bne no_prob
>                    inc rand2     ;set rand2 to 001h.
> no_prob:          rts
> Basically, the routine grabs whatever happens to be in the TIA regs, and in 
> RAM and XORs it with the random # registers.  Note that the eor #03h is 
> very important, since it prevents the registers XORing themselves. This 
> would set them to 00h. After it grabs what happens to be in RAM/TIA, it 
> writes 00h to the location, and sets the stack.  Since x will be 0ffh 
> before the loop ends, we get a "free" way to set the stack.  Cute, 
> hunh.  As a bonus, X will = 0 which may be useful on return from this routine.

Very cute. May I steal^H^H^H^H^Hborrow that code for a game?

Hmm, if the RAM also holds random junk on powerup, wouldn't it be enough to
just use them as-is (after checking to make sure they didn't all happen to
be zero)?

> After that, the random # register is checked for all 0's.  This is of 
> course the "bad" state, and must be avoided.  Setting rand2 to 001h is good 
> enough, because after 30 or so iterations (half a second of playing time) 
> it is good and random again.  The odds of this happening are vanishingly 
> small, however, except maybe on an emulator (which may initalize RAM to all 
> 000h  Even so, the TIA addresses shouldn't all read 00h).

Oddly enough, xstella doesn't init all RAM to zero... apparently it does
put random garbage in there, because I had forgotten the BNE in the zeroing
loop at the beginning of the cart, and the result was, the `suicide mission'
LSFR started with a different seed each time the emulator was run...

> A very good example of an LFSR getting stuck at the all 0's state is 
> Fraction Fever for Colecovision.  Play it on ColEmDOS.  The LFSR is never 
> initialized on startup, and ColEmDOS initalizes all RAM to 000h.  As a 
> result, nothing is random, and all the platforms are the same :-).

That also could be useful behavior, for debugging... maybe you could take
advantage of it by defining the random seed on the command line:

#for debugging:

dasm file.asm -ofile.bin -f3 -v3 -DRAND_SEED=0

#for production:

dasm file.asm -ofile.bin -f3 -v3 -DRAND_SEED=13939

...then in the code:

	lda #<RAND_SEED
	sta random
	lda #>RAND_SEED
	sta random+1

or such... then when debugging, the game would play the same way each time,
which would let you concentrate on the graphics or sound or whatever while the
game was running, and not get killed :)

I think Infocom did something like this with the Z-code engine.

Couple of questions... I'd like to include this piece more-or-less verbatim in
the V2 stelladoc, is that ok with you? Also, what assembler is your code written
for? DASM doesn't seem to like 000h as a constant... so I run it thru this

perl -pe '1 while s/^([^;]*?)([a-fA-F\d])*h/$1\$$2/'

and all is well. So what do I call my perl program? <something>2dasm... having
typed that horrible mess once, there's no way I'm going to remember it or type
it again :)



If a trainstation is the place where trains stop, what is a workstation?

Archives (includes files) at
Unsub & more at

Current Thread