UrForth: redesigned compilation words to look nicer, and being easier to use
[urasm.git] / libs / rnd / ritman.zas
blob1c91d60d6cfe5a7d5482ef0a244330ad0ee844c4
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; generate random number
3 ;; note: use <rnd/marsaglia.zas> instead of this one!
4 ;; IN:
5 ;;   rndSeed: 4 bytes of shit
6 ;; OUT:
7 ;;   HL: 32-bit random
8 ;;   DE: either dead or uncomment the line to get 64-bit number
9 ;;   BC: dead
10 ;;   AF: dead
11 ;; WARNING:
12 ;;  be careful to not have all zeroes in rndSeed!
13 random:
14   ld    hl,(rndSeed1)
15   ld    d,l
16   add   hl,hl
17   add   hl,hl
18   ld    c,h
19   ld    hl,(rndSeed0)
20   ld    b,h
21   rl    b
22   ld    e,h
23   rl    e
24   rl    d
25   add   hl,bc
26   ld    (rndSeed0),hl
27   ld    hl,(rndSeed1)
28   adc   hl,de
29   res   7,h
30   ld    (rndSeed1),hl
31   jp    m,.done
32   ld    hl,rndSeed0
33 .gotzero:
34   inc   (hl)
35   inc   hl
36   jp    z,.gotzero
37 .done:
38   ld    hl,(rndSeed0)
39   ;ld   de,(rndSeed1)
40   ret
41 ; not-so-bad initial value
42 rndSeed0: defm "k8"
43 rndSeed1: defm "th"