Prev: 44925 Up: Map Next: 46761
46700: Random Number
Input
A Maximum value, clamped to bits 0-5
Output
A Random value in that range
Random_Number 46700 LD H,A Set the high bound from A.
46701 LD L,0 Set the low bound to 0.
This entry point is used by the routines at Run_Other_Phase, Initialise_Object_Slots, Clear_Work_Buffers and Update_Sprite_Slots.
Use the range already in HL.
Random_Number_Range 46703 LD A,L Fetch the low bound.
46704 AND %00111111 Keep only bits 0-5.
46706 LD L,A Store the low bound.
46707 LD A,H Fetch the high bound.
46708 AND %00111111 Keep only bits 0-5.
46710 CP L Jump to Random_Number_Setup if the high bound is at least the low bound.
46711 JR NC,Random_Number_Setup
46713 LD H,L Copy the low bound to H.
46714 LD L,A Store the smaller value as the low bound.
Random_Number_Setup 46715 EX DE,HL Copy the range to DE.
46716 INC D Increment the high bound by one.
46717 LD HL,(46761) Fetch *Random_Seed.
46720 LD H,2 Force the pointer into ROM.
46722 LD A,R Add the R register to the low byte.
46724 ADD A,L
46725 LD L,A
46726 PUSH IX Stash IX on the stack.
46728 LD IX,46761 Point IX at Random_Seed.
Pick a value in range.
Random_Number_Pick 46732 LD A,(HL) Mix the ROM byte with *Random_Seed.
46733 XOR (IX+0)
46736 AND %00111111 Keep only bits 0-5.
46738 INC L Advance the ROM pointer.
46739 CP E Jump to Random_Number_Pick if the value is less than the low bound.
46740 JR C,Random_Number_Pick
46742 CP D Jump to Random_Number_Pick if the value is not less than the high bound.
46743 JR NC,Random_Number_Pick
46745 LD (46761),HL Write the ROM pointer to *Random_Seed.
46748 POP IX Restore IX from the stack.
46750 PUSH AF Stash the random value on the stack.
46751 LD A,(23659) Restart if *ATTR_T is not 13.
46754 CP 13
46756 JP NZ,0
46759 POP AF Restore the random value from the stack.
46760 RET Return.
Prev: 44925 Up: Map Next: 46761