nxu/rng.tal

58 lines
1.4 KiB
Tal

( rng.tal )
( simple 16-bit xorshift RNG )
( based on http://b2d-f9r.blogspot.com/2010/08/16-bit-xorshift-rng-now-with-more.html )
%<<5 { #50 SFT2 }
%>>1 { #01 SFT2 }
%>>3 { #03 SFT2 }
%RTN { JMP2r }
( devices )
|00 @System [ &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 &debug $1 &halt $1 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|b0 @DateTime [ &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 ]
|0000
@rng [ &x $2 &y $2 ]
|0100
#0001 #0001 ;init-rng JSR2 ( initialize rng )
#0fff .System/r DEO2
#0f80 .System/g DEO2
#0000 .System/b DEO2
;on-frame .Screen/vector DEO2
BRK
@on-frame ( -> )
#c0 ( 64 iterations before #00 )
&loop
;draw-pixel JSR2
INC
DUP ,&loop JCN
POP
BRK
@draw-pixel ( -> )
;random-short JSR2
#00 SWP .Screen/x DEO2
#00 SWP .Screen/y DEO2
.Screen/pixel DEI
INC .Screen/pixel DEO
( #03 .Screen/pixel DEO )
RTN
@init-rng ( x* y* -> )
#0001 ORA2 .rng/y STZ2 ( y <- [y|1] need non-zero )
#0001 ORA2 .rng/x STZ2 ( x <- [x|1] need non-zero )
RTN
@random-short ( -> val* )
.rng/x LDZ2 DUP2 <<5 EOR2 ( tmp: x^[x<<5] )
.rng/y LDZ2 DUP2 .rng/x STZ2 ( tmp y )
DUP2 >>1 EOR2 ( tmp y^[y>>1] )
SWP2 DUP2 >>3 EOR2 ( y^[y>>1] tmp^[tmp>>3] )
EOR2 DUP2 .rng/y STZ2 ( y^[y>>1] ^ tmp^[tmp>>3] )
RTN