SHARE
TWEET

Prism StableRandom.

luckytyphlosion Dec 4th, 2017 (edited) 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. StableRandom::
  2.     ; in: hl: pointer to 8-byte RNG state
  3.     ; out: a: random value; other registers preserved
  4.     push bc
  5.     push de
  6.     push hl
  7.     call .advance_left_register
  8.     call .advance_right_register
  9.     inc hl
  10.     inc hl
  11.     inc hl
  12.     call .advance_selector_register
  13.     pop hl
  14.     push hl
  15.     rlca
  16.     rlca
  17.     ld c, a
  18.     and 3
  19.     ld e, a
  20.     ld d, 0
  21.     add hl, de
  22.     ld b, [hl]
  23.     pop hl
  24.     push hl
  25.     ld e, 5
  26.     add hl, de
  27.     ld a, c
  28.     ld c, [hl]
  29.     rlca
  30.     rlca
  31.     and 3
  32.     call .combine_register_values
  33.     pop hl
  34.     pop de
  35.     pop bc
  36.     ret
  37.    
  38. .advance_left_register
  39.     ; in: hl: pointer to left register
  40.     ; out: hl: pointer to RIGHT register
  41.     ld a, [hli]
  42.     ld e, a
  43.     ld a, [hli]
  44.     ld d, a
  45.     ld a, [hli]
  46.     ld c, a
  47.     ld a, [hld]
  48.     ld b, a
  49.     or c
  50.     or d
  51.     or e
  52.     call z, .reseed_left_register
  53.     ld a, e
  54.     xor d
  55.     ld e, a
  56.     ld a, d
  57.     xor c
  58.     ld d, a
  59.     ld a, c
  60.     xor b
  61.     ld c, a
  62.     ld a, c
  63.     ld [hld], a
  64.     ld a, d
  65.     ld [hld], a
  66.     ld [hl], e
  67.     sla e
  68.     rl d
  69.     rl c
  70.     inc hl
  71.     ld a, [hl]
  72.     xor e
  73.     ld [hli], a
  74.     ld a, [hl]
  75.     xor d
  76.     ld [hli], a
  77.     ld a, [hl]
  78.     xor c
  79.     ld [hld], a
  80.     ld b, a
  81.     ld c, [hl]
  82.     sla c
  83.     rl b
  84.     sbc a
  85.     and 1
  86.     dec hl
  87.     xor [hl]
  88.     ld [hld], a
  89.     ld a, [hl]
  90.     xor b
  91.     ld [hli], a
  92.     inc hl
  93.     inc hl
  94.     inc hl
  95.     ret
  96.  
  97. .reseed_left_register
  98.     ; in: hl: pointer to left register + 2
  99.     ; out: hl preserved; bcde new seed
  100.     ld de, 5
  101.     push hl
  102.     add hl, de
  103.     call .advance_selector_register
  104.     ld b, a
  105.     call .advance_selector_register
  106.     ld c, a
  107.     call .advance_selector_register
  108.     ld d, a
  109.     call .advance_selector_register
  110.     ld e, a
  111.     pop hl
  112.     ret
  113.  
  114. .advance_right_register
  115.     ; in: hl: pointer to right register
  116.     ; out: hl preserved
  117.     ld a, [hli]
  118.     cp 210
  119.     jr c, .right_carry_OK
  120.     sub 210
  121. .right_carry_OK
  122.     ld d, a
  123.     ld a, [hli]
  124.     ld e, a
  125.     ld c, [hl]
  126.     or c
  127.     or d
  128.     jr z, .right_register_needs_reseed
  129.     ld a, c
  130.     and e
  131.     inc a
  132.     jr nz, .right_register_OK
  133.     ld a, d
  134.     cp 209
  135.     jr nz, .right_register_OK
  136. .right_register_needs_reseed
  137.     call .reseed_right_register
  138. .right_register_OK
  139.     ld a, e
  140.     ld [hld], a
  141.     push hl
  142.     ld b, 0
  143.     ld h, b
  144.     ld l, d
  145.     ld a, 210
  146.     rst AddNTimes
  147.     ld a, l
  148.     ld b, h
  149.     pop hl
  150.     ld [hld], a
  151.     ld [hl], b
  152.     ret
  153.  
  154. .reseed_right_register
  155.     ; in: hl: pointer to right register + 2
  156.     ; out: hl preserved, cde new seed
  157.     inc hl
  158.     call .advance_selector_register
  159.     ld c, a
  160.     call .advance_selector_register
  161.     ld d, a
  162.     call .advance_selector_register
  163.     ld e, a
  164.     dec hl
  165.     ret
  166.  
  167. .advance_selector_register
  168.     ; in: hl: pointer to selector register
  169.     ; out: all registers but a preserved; a = new selector
  170.     push bc
  171.     ld a, [hl]
  172.     ld b, 0
  173.     rra
  174.     rr b
  175.     rra
  176.     rr b
  177.     ld a, [hl]
  178.     swap a
  179.     rrca
  180.     and $f8
  181.     add a, b
  182.     add a, [hl]
  183.     add a, 29
  184.     ld [hl], a
  185.     pop bc
  186.     ret
  187.  
  188. .combine_register_values
  189.     and a
  190.     jr z, .add_registers
  191.     dec a
  192.     jr z, .xor_registers
  193.     dec a
  194.     jr z, .subtract_registers
  195.     ld a, c
  196.     sub b
  197.     ret
  198. .subtract_registers
  199.     ld a, b
  200.     sub c
  201.     ret
  202. .add_registers
  203.     ld a, b
  204.     add a, c
  205.     ret
  206. .xor_registers
  207.     ld a, b
  208.     xor c
  209.     ret
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top