aaaaaa123456789

StableRandom's original gbz80 code

Aug 7th, 2018
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; This code was originally found in Prism. Originally written on 2016-12-18; updated on 2018-08-18 with a bug fix.
  2. ; Since I never bothered with a license header, I'm hereby placing it in the public domain as of 2019-01-04.
  3.  
  4. StableRandom::
  5. ; in: hl: pointer to 8-byte RNG state
  6. ; out: a: random value; other registers preserved
  7. push bc
  8. push de
  9. push hl
  10. call .advance_left_register
  11. call .check_right_register_cycles
  12. call .advance_right_register
  13. inc hl
  14. inc hl
  15. inc hl
  16. call .advance_selector_register
  17. pop hl
  18. push hl
  19. rlca
  20. rlca
  21. ld c, a
  22. and 3
  23. ld e, a
  24. ld d, 0
  25. add hl, de
  26. ld b, [hl]
  27. pop hl
  28. push hl
  29. ld e, 5
  30. add hl, de
  31. ld a, c
  32. ld c, [hl]
  33. rlca
  34. rlca
  35. and 3
  36. call .combine_register_values
  37. pop hl
  38. pop de
  39. pop bc
  40. ret
  41.  
  42. .advance_left_register
  43. ; in: hl: pointer to left register
  44. ; out: hl: pointer to RIGHT register
  45. ld a, [hli]
  46. ld e, a
  47. ld a, [hli]
  48. ld d, a
  49. ld a, [hli]
  50. ld c, a
  51. ld a, [hld]
  52. ld b, a
  53. or c
  54. or d
  55. or e
  56. call z, .reseed_left_register
  57. ld a, e
  58. xor d
  59. ld e, a
  60. ld a, d
  61. xor c
  62. ld d, a
  63. ld a, c
  64. xor b
  65. ld c, a
  66. ld a, c
  67. ld [hld], a
  68. ld a, d
  69. ld [hld], a
  70. ld [hl], e
  71. sla e
  72. rl d
  73. rl c
  74. inc hl
  75. ld a, [hl]
  76. xor e
  77. ld [hli], a
  78. ld a, [hl]
  79. xor d
  80. ld [hli], a
  81. ld a, [hl]
  82. xor c
  83. ld [hld], a
  84. ld b, a
  85. ld c, [hl]
  86. sla c
  87. rl b
  88. sbc a
  89. and 1
  90. dec hl
  91. xor [hl]
  92. ld [hld], a
  93. ld a, [hl]
  94. xor b
  95. ld [hli], a
  96. inc hl
  97. inc hl
  98. inc hl
  99. ret
  100.  
  101. .reseed_left_register
  102. ; in: hl: pointer to left register + 2
  103. ; out: hl preserved; bcde new seed
  104. ld de, 5
  105. push hl
  106. add hl, de
  107. call .advance_selector_register
  108. ld b, a
  109. call .advance_selector_register
  110. ld c, a
  111. call .advance_selector_register
  112. ld d, a
  113. call .advance_selector_register
  114. ld e, a
  115. pop hl
  116. inc hl
  117. ld a, b
  118. ld [hld], a ;only b needs to be written back, since the rest will be handled by the main function
  119. ret
  120.  
  121. .check_right_register_cycles
  122. ; in: hl: pointer to right register
  123. ; out: hl preserved
  124. inc hl
  125. inc hl
  126. ld a, [hld]
  127. ld c, a
  128. ld a, [hld]
  129. ld d, a
  130. or c
  131. ld e, [hl]
  132. push hl
  133. jr z, .check_long_cycles
  134. ld hl, .right_register_short_cycles - 1
  135. ld b, (.right_register_short_cycles_end - .right_register_short_cycles) / 3 + 1
  136. .short_cycle_handle_loop
  137. inc hl
  138. dec b
  139. jr z, .pop_ret
  140. .short_cycle_loop
  141. ld a, [hli]
  142. cp c
  143. ld a, [hli]
  144. jr nz, .short_cycle_handle_loop
  145. cp d
  146. jr nz, .short_cycle_handle_loop
  147. ld a, e
  148. cp [hl]
  149. jr nz, .short_cycle_handle_loop
  150. inc hl
  151. .copy_right_register_state
  152. ld a, [hli]
  153. ld c, a
  154. ld a, [hli]
  155. ld b, [hl]
  156. pop hl
  157. inc hl
  158. ld [hli], a
  159. ld a, c
  160. ld [hld], a
  161. dec hl
  162. ld [hl], b
  163. ret
  164.  
  165. .check_long_cycles
  166. ld hl, .right_register_long_cycles
  167. .long_cycle_loop
  168. ld a, [hli]
  169. and a
  170. jr z, .pop_ret
  171. cp e
  172. jr nz, .long_cycle_loop
  173. ld a, [hl]
  174. and a
  175. jr z, .start_short_cycles
  176. pop hl
  177. ld [hl], a
  178. ret
  179.  
  180. .start_short_cycles
  181. pop hl
  182. push hl
  183. inc hl
  184. inc hl
  185. inc hl
  186. call .advance_selector_register
  187. ld hl, .right_register_short_cycles
  188. jr .copy_right_register_state
  189.  
  190. .pop_ret
  191. pop hl
  192. ret
  193.  
  194. .right_register_short_cycles
  195. db $72, $4f, $9f
  196. db $7b, $1a, $7b
  197. db $84, $e5, $56
  198. db $8d, $b0, $32
  199. .right_register_short_cycles_end
  200. db 0, 0
  201. .right_register_long_cycles
  202. db 1, 2, 4, 8, 13, 17, 23, 26, 29, 58
  203. db 0
  204.  
  205. .advance_right_register
  206. ; in: hl: pointer to right register
  207. ; out: hl preserved
  208. ld a, [hli]
  209. cp 210
  210. jr c, .right_carry_OK
  211. sub 210
  212. .right_carry_OK
  213. ld d, a
  214. ld a, [hli]
  215. ld e, a
  216. ld c, [hl]
  217. or c
  218. or d
  219. jr z, .right_register_needs_reseed
  220. ld a, c
  221. and e
  222. inc a
  223. jr nz, .right_register_OK
  224. ld a, d
  225. cp 209
  226. jr nz, .right_register_OK
  227. .right_register_needs_reseed
  228. call .reseed_right_register
  229. .right_register_OK
  230. ld a, e
  231. ld [hld], a
  232. push hl
  233. ld b, 0
  234. ld h, b
  235. ld l, d
  236. ld a, 210
  237. rst AddNTimes
  238. ld a, l
  239. ld b, h
  240. pop hl
  241. ld [hld], a
  242. ld [hl], b
  243. ret
  244.  
  245. .reseed_right_register
  246. ; in: hl: pointer to right register + 2
  247. ; out: hl preserved, cde new seed
  248. inc hl
  249. call .advance_selector_register
  250. ld c, a
  251. call .advance_selector_register
  252. ld d, a
  253. sub 210
  254. jr c, .carry_reseed_OK
  255. ld d, a
  256. .carry_reseed_OK
  257. call .advance_selector_register
  258. ld e, a
  259. dec hl
  260. ret
  261.  
  262. .advance_selector_register
  263. ; in: hl: pointer to selector register
  264. ; out: all registers but a preserved; a = new selector
  265. push bc
  266. ld a, [hl]
  267. ld b, 0
  268. rra
  269. rr b
  270. rra
  271. rr b
  272. ld a, [hl]
  273. swap a
  274. rrca
  275. and $f8
  276. add a, b
  277. add a, [hl]
  278. add a, 29
  279. ld [hl], a
  280. pop bc
  281. ret
  282.  
  283. .combine_register_values
  284. and a
  285. jr z, .add_registers
  286. dec a
  287. jr z, .xor_registers
  288. dec a
  289. jr z, .subtract_registers
  290. ld a, c
  291. sub b
  292. ret
  293. .subtract_registers
  294. ld a, b
  295. sub c
  296. ret
  297. .add_registers
  298. ld a, b
  299. add a, c
  300. ret
  301. .xor_registers
  302. ld a, b
  303. xor c
  304. ret
RAW Paste Data Copied