2 ; Randum number generator
4 ; Written and donated by Sidney Cadot - sidney@ch.twi.tudelft.nl
5 ; 2016-11-07, modified by Brad Smith
7 ; May be distributed with the cc65 runtime using the same license.
11 ; void srand (unsigned seed);
14 ; Multiplier must be 1 (mod 4)
15 ; Added value must be 1 (mod 2)
16 ; This guarantees max. period (2**32)
17 ; The lowest bits have poor entropy and
18 ; exhibit easily detectabl patterns, so
19 ; only the upper bits 16-22 and 24-31 of the
20 ; 4-byte state are returned.
22 ; The best 8 bits, 24-31 are returned in the
23 ; low byte A to provide the best entropy in the
24 ; most commonly used part of the return value.
31 ; The seed. When srand() is not called, the C standard says that that rand()
32 ; should behave as if srand() was called with an argument of 1 before.
38 lda rand+0 ; SEED *= $01010101
46 lda rand+0 ; SEED += $31415927
55 and #$7f ; Suppress sign bit (make it positive)
60 rts ; return bit (16-22,24-31) in (X,A)
62 _srand: sta rand+0 ; Store the seed
65 sta rand+2 ; Set MSW to zero