UrForth: properly mark scattered colon words
[urasm.git] / libs / rnd / lsfrlcg32s.zas
blob6d8dc8e360edc522a136f6fb3311478969ecff8e
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; generate random number
3 ;; this is quite fast, quality pseudo-random number generator
4 ;; it uses 32-bit seeds (and still returns a 16-bit result)
5 ;; an advantage here is that they've been tested and passed randomness
6 ;;  tests (all of thee ones offered by CAcert labs)
7 ;; it combines a 32-bit Linear Feedback Shift Register and a 32-bit LCG
8 ;; it has a period of 18,446,744,069,414,584,320 (roughly 18.4 quintillion)
9 ;; LFSR taps: 0,2,6,7  = 11000101
11 ;; IN:
12 ;;   rndSeed0: lower 2 bytes of the first seed
13 ;;   rndSeed1: upper 2 bytes of the first seed
14 ;;   rndSeed2: lower 2 bytes of the second seed
15 ;;   rndSeed3: upper 2 bytes of the second seed
16 ;;   WARNING! second seed must not be zero
17 ;; OUT:
18 ;;   HL: 16-bit random
19 ;;   BC,DE: DEAD (can be used as lower quality values, but are not independent of HL)
20 ;;   AF: dead
21 random:
22   ld    hl,12345
23 rndSeed0 equ $-2
24   ld    de,6789
25 rndSeed1 equ $-2
26   ld    b,h
27   ld    c,l
28   add   hl,hl
29   rl    e
30   rl    d
31   add   hl,hl
32   rl    e
33   rl    d
34   inc   l
35   add   hl,bc
36   ld    (rndSeed0),hl
37   ld    hl,(rndSeed1)
38   adc   hl,de
39   ld    (rndSeed1),hl
40   ex    de,hl
41   ld    hl,9876
42 rndSeed2 equ $-2
43   ld    bc,54321
44 rndSeed3 equ $-2
45   add   hl,hl
46   rl    c
47   rl    b
48   ld    (rndSeed3),bc
49   sbc   a,a
50   and   %11000101
51   xor   l
52   ld    l,a
53   ld    (rndSeed2),hl
54   ex    de,hl
55   add   hl,bc
56   ret