Skip to content

Instantly share code, notes, and snippets.

@non
Created November 26, 2021 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non/85bacf2b5d7a148f8bace90c4ea2ff88 to your computer and use it in GitHub Desktop.
Save non/85bacf2b5d7a148f8bace90c4ea2ff88 to your computer and use it in GitHub Desktop.
( 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 }
%EMIT { #18 DEO }
%DIGIT { #00 SWP ;digits ADD2 LDA EMIT }
%SPACE { #20 EMIT }
%NEWLINE { #0a EMIT }
%ITERATIONS { #0100 }
|0000
@rng [ &x $2 &y $2 ]
|0100
#0001 #0001 ;init-rng JSR2 ( initialize rng )
#0000 ( set counter to 0 )
,&inner JMP ( skip first newline )
&outer
NEWLINE ( newline every 8 numbers )
&inner
;random-short JSR2 ;emit-short JSR2 SPACE ( emit random number )
INC2 DUP2 #0007 AND2 #0000 NEQ2 ,&inner JCN ( increment counter, maybe loop )
DUP2 ITERATIONS LTH2 ,&outer JCN ( see if we're done, or else loop )
POP
BRK
@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
@emit-short ( x* -> )
DUP #04 SFT DIGIT ( emit hi>>4 )
#0f AND DIGIT ( emit hi&f )
DUP #04 SFT DIGIT ( emit lo>>4 )
#0f AND DIGIT ( emit lo&f )
RTN
@digits
30 31 32 33 34 35 36 37
38 39 61 62 63 64 65 66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment