Prev: 44284 Up: Map Next: 44340
44325: Generate Random Number
Returns a number from 0 up to, but not including, the limit passed in A. The Z80 Memory Refresh Register supplies the walk count: the routine counts down inside that limit once per refresh tick, wrapping to the original limit when it hits zero, then decreases the result by one more before returning. Nothing in the game calls it.
Input
A Exclusive upper bound
Output
A The generated number
Preserve BC, keep the limit, and read the refresh register for the walk count.
GenerateRandomNumber 44325 PUSH BC Stash BC on the stack.
44326 LD C,A Copy the exclusive upper bound into C.
44327 LD A,R Read the Memory Refresh Register into A.
44329 LD B,A Use that refresh value as the walk counter in B.
44330 LD A,C Restore the exclusive upper bound into A.
Count down inside the limit; when it wraps, reload the original bound from C.
GenerateRandomNumber_Loop 44331 DEC A Decrease the running value in A by one.
44332 JR NZ,GenerateRandomNumber_Next Jump to GenerateRandomNumber_Next if the running value is not zero.
44334 LD A,C Reload the exclusive upper bound from C into A.
GenerateRandomNumber_Next 44335 DJNZ GenerateRandomNumber_Loop Decrease the walk counter by one and loop back to GenerateRandomNumber_Loop until the refresh count is spent.
44337 DEC A Decrease the running value in A by one for the final result.
44338 POP BC Restore BC from the stack.
44339 RET Return.
Prev: 44284 Up: Map Next: 44340