Prev: 40056 Up: Map Next: 40168
40095: Random Number
Input
A Random seed?
Output
A Random number
GetRandomNum 40095 CALL CalcRandom Call CalcRandom.
40098 BIT 7,A Return if bit 7 is set.
40100 RET Z
40101 NEG Negate the accumulator.
40103 RET Return.
This entry point is used by the routine at Action_Attack.
CalcRandom 40104 PUSH IX Stash IX and BC on the stack.
40106 PUSH BC
40107 LD C,A C=A.
40108 SLA A A=A * $02.
40110 JR NC,GetRandomNum_0 Jump to GetRandomNum_0 if there's no carry over.
40112 LD A,255 Else, set A=255.
GetRandomNum_0 40114 LD B,A B=A.
GetRandomNum_1 40115 LD IX,46866 Increase the LSB of the RandomCounter by one.
40119 INC (IX+1)
40122 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.
40124 INC (IX+0) Increase the MSB of the RandomCounter by one.
GetRandomNum_2 40127 LD IX,(46866) IX=RandomCounter.
40131 LD A,(46862) A=RandomSeed.
40134 ADC A,(IX+0)
40137 ADD IX,DE
40139 XOR (IX+1)
40142 PUSH HL Stash HL on the stack.
40143 LD HL,46862 Compare RandomSeed against A.
40146 CP (HL)
40147 POP HL Restore HL from the stack.
40148 JR Z,GetRandomNum_1 If the result of the comparison was zero, jump back to GetRandomNum_1 to try again.
40150 LD (46862),A Write A to RandomSeed.
GetRandomNum_3 40153 CP B
40154 JR C,GetRandomNum_4
40156 JR Z,GetRandomNum_4
40158 SRL A
40160 JP GetRandomNum_3
GetRandomNum_4 40163 SUB C
40164 POP BC Restore BC and IX from the stack.
40165 POP IX
40167 RET Return.
Prev: 40056 Up: Map Next: 40168