Prev: 9C78 Up: Map Next: 9CE8
9C9F: Random Number
Input
A Random seed?
Output
A Random number
GetRandomNum 9C9F CALL CalcRandom Call CalcRandom.
9CA2 BIT 7,A Return if bit 7 is set.
9CA4 RET Z
9CA5 NEG Negate the accumulator.
9CA7 RET Return.
This entry point is used by the routine at Action_Attack.
CalcRandom 9CA8 PUSH IX Stash IX and BC on the stack.
9CAA PUSH BC
9CAB LD C,A C=A.
9CAC SLA A A=A * $02.
9CAE JR NC,GetRandomNum_0 Jump to GetRandomNum_0 if there's no carry over.
9CB0 LD A,$FF Else, set A=FF.
GetRandomNum_0 9CB2 LD B,A B=A.
GetRandomNum_1 9CB3 LD IX,$B712 Increase the LSB of the RandomCounter by one.
9CB7 INC (IX+$01)
9CBA JR NZ,GetRandomNum_2 If the LSB has not "rolled over" to $00, skip updating the MSB on the following line. Jump to GetRandomNum_0.
9CBC INC (IX+$00) Increase the MSB of the RandomCounter by one.
GetRandomNum_2 9CBF LD IX,($B712) IX=RandomCounter.
9CC3 LD A,($B70E) A=RandomSeed.
9CC6 ADC A,(IX+$00)
9CC9 ADD IX,DE
9CCB XOR (IX+$01)
9CCE PUSH HL Stash HL on the stack.
9CCF LD HL,$B70E Compare RandomSeed against A.
9CD2 CP (HL)
9CD3 POP HL Restore HL from the stack.
9CD4 JR Z,GetRandomNum_1 If the result of the comparison was zero, jump back to GetRandomNum_1 to try again.
9CD6 LD ($B70E),A Write A to RandomSeed.
GetRandomNum_3 9CD9 CP B
9CDA JR C,GetRandomNum_4
9CDC JR Z,GetRandomNum_4
9CDE SRL A
9CE0 JP GetRandomNum_3
GetRandomNum_4 9CE3 SUB C
9CE4 POP BC Restore BC and IX from the stack.
9CE5 POP IX
9CE7 RET Return.
Prev: 9C78 Up: Map Next: 9CE8