Zeda

shuffle

Oct 7th, 2020
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shuffle:
  2. ;(seed) is a non-zero 16-bit value
  3. ;array is 52 bytes that will be populated with integers 0..51, shuffled.
  4.     xor a
  5. loop:
  6.     ld e,a
  7.  
  8. ;This xorhift routine was written by John Metcalf
  9. ;   http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html
  10.     ld hl,(seed)
  11.     ld a,h
  12.     rra
  13.     ld a,l
  14.     rra
  15.     xor h
  16.     ld h,a
  17.     ld a,l
  18.     rra
  19.     ld a,h
  20.     rra
  21.     xor l
  22.     ld l,a
  23.     xor h
  24.     ld h,a
  25.     ld (seed),hl
  26. ; End of the xorshift routine
  27.     ld a,r
  28.     add a,h
  29.     ld h,a
  30.  
  31.     ld a,e
  32.     push af
  33.     inc a
  34.     ex de,hl
  35.  
  36.     ld hl,0
  37.     ld b,8
  38.     ld c,l
  39.     add hl,hl
  40.     rla
  41.     jr nc,$+4
  42.     add hl,de
  43.     adc a,c
  44.     djnz $-6
  45. ;(array+A) ==> (array+index)
  46. ;index ==> (array+A)
  47. ;add A to array
  48.     ld e,a
  49.     ld d,b
  50.     pop af
  51.     ld l,a
  52.     ld h,b
  53.     ld bc,array
  54.     add hl,bc
  55.     ex de,hl
  56.     add hl,bc
  57.     ldi
  58.     dec hl
  59.     ld (hl),a
  60.     inc a
  61.     cp 52
  62.     jr nz,loop
  63.     ret
  64.  
Add Comment
Please, Sign In to add comment