UrForth: properly mark scattered colon words
[urasm.git] / libs / rnd / lsfrlcg.zas
blob4ca8a6f0f4090fd7513d6aeee1e6ab6147fcadee
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; generate random number
3 ;; this is quite fast, quality pseudo-random number generator
4 ;; it combines a 16-bit Linear Feedback Shift Register and a 16-bit LCG
5 ;; cycle: 4,294,901,760 (almost 4.3 billion)
6 ;; IN:
7 ;;   rndSeed: 4 bytes of shit (second word should not be zero!)
8 ;; OUT:
9 ;;   HL: 16-bit random
10 ;;   BC: dead (result of the LCG, so not that great of quality)
11 ;;   AF: dead
12 ;; WARNING:
13 ;;  be careful to not have all zeroes in rndSeed!
14 random:
15   ld    hl,"k8"
16 rndSeed0 equ $-2
17   ld    b,h
18   ld    c,l
19   add   hl,hl
20   add   hl,hl
21   inc   l
22   add   hl,bc
23   ld    (rndSeed0),hl
24   ld    hl,"th"
25 rndSeed1 equ $-2
26   add   hl,hl
27   sbc   a,a
28   and   %00101101
29   xor   l
30   ld    l,a
31   ld    (rndSeed1),hl
32   add   hl,bc
33   ret