Subject: Re: [stella] entropy & randomness From: Kevin Horton <khorton@xxxxxxxxxxx> Date: Wed, 10 Oct 2001 18:31:33 -0500 |
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)?
> 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
That also could be useful behavior, for debugging... maybe you could take advantage of it by defining the random seed on the command line:
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
one-liner:
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 :)
- Archives (includes files) at http://www.biglist.com/lists/stella/archives/ Unsub & more at http://www.biglist.com/lists/stella/
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [stella] entropy & randomness, B. Watson | Thread | Re: [stella] entropy & randomness, R. Thavas |
Re: [stella] entropy & randomness, Kevin Horton | Date | [stella] Outlaw Reverse-Engineered, Manuel Polik |
Month |