dsforth: added "-1", "-2", "-4" constants
[urasm.git] / libs / marsaglia_rnd.zas
blob4c2690d89c2d3992d93554a6bebbcc8502e40850
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; generate random number
3 ;; IN:
4 ;;   rndSeed: 4 bytes of shit
5 ;; OUT:
6 ;;   HL: 32-bit random
7 ;;   DE: either dead or uncomment the line to get 64-bit number
8 ;;   BC: dead
9 ;; WARNING:
10 ;;  be careful to not have all zeroes in rndSeed!
12 ;;  taken from http://www.worldofspectrum.org/forums/showthread.php?t=23070
13 ;;  originad code by Patrik Rak
14 random:
15   ld  hl,(rndSeed)    ; xz -> yw
16   ld  de,(rndSeed+2)  ; yw -> zt
17   ld  (rndSeed),de  ; x = y, z = w
18   ld  a,e         ; w = w ^ ( w << 3 )
19   add a,a
20   add a,a
21   add a,a
22   xor e
23   ld  e,a
24   ld  a,h         ; t = x ^ (x << 1)
25   add a,a
26   xor h
27   ld  d,a
28   rra             ; t = t ^ (t >> 1) ^ w
29   xor d
30   xor e
31   ld  h,l         ; y = z
32   ld  l,a         ; w = t
33   ld  (rndSeed+2),hl
34   ret
35 rndSeed: defw #a280, #c0de