Routines
| Prev: D2A4 | Up: Map | Next: D2ED |
D2AD: The random number generator
|
Steps a pointer ($A039) through memory and mixes the byte it finds with $A037. Used for chances, countdowns and greeting delays.
How it works:
1. Set the limit C to A and work out B, twice the limit (255 if that overflows).
2. Step the pointer at $A039 on through memory and fetch the byte it points at.
3. Mix that byte, and the byte after it, into the last result at $A037. If the answer equals the last result, step on and try again, so the same number never comes up twice running.
4. Halve the answer until it is no larger than B, then subtract C. The result is a signed number from -C to +C.
|
||||||||||||
| D2AD | PUSH IX | Save IX on the stack | ||||||||||
| D2AF | PUSH BC | Save BC on the stack | ||||||||||
| D2B0 | LD C,A | Copy A into C - C = the limit; B = twice it (255 if that overflows) | ||||||||||
| D2B1 | SLA A | Shift A left (double it) | ||||||||||
| D2B3 | JR NC,$D2B7 | If the carry flag is clear, skip ahead to D2B7 | ||||||||||
| D2B5 | LD A,$FF | Load A with $FF | ||||||||||
| D2B7 | LD B,A | Copy A into B | ||||||||||
| D2B8 | LD IX,$A039 | Point IX at $A039 in "Game variables" - Step the pointer at $A039 on through memory | ||||||||||
| D2BC | INC (IX+$01) | Add one to byte +1 of the record at IX | ||||||||||
| D2BF | JR NZ,$D2C4 | If byte +1 of the record at IX is not zero, skip ahead to D2C4 | ||||||||||
| D2C1 | INC (IX+$00) | Add one to the first byte of the record at IX | ||||||||||
| D2C4 | LD IX,($A039) | Read $A039 in "Game variables" into IX | ||||||||||
| D2C8 | LD A,($A037) | Read $A037 in "Game variables" into A - Mix the bytes it points at into the last result ($A037) | ||||||||||
| D2CB | ADC A,(IX+$00) | Add (IX+$00) and the carry to A | ||||||||||
| D2CE | ADD IX,DE | Add DE to IX | ||||||||||
| D2D0 | XOR (IX+$01) | Flip in A the bits that are set in byte +1 of the record at IX | ||||||||||
| D2D3 | PUSH HL | Save HL on the stack | ||||||||||
| D2D4 | LD HL,$A037 | Point HL at $A037 in "Game variables" | ||||||||||
| D2D7 | CP (HL) | Compare A with the byte at $A037 in "Game variables" - Never the same number twice running | ||||||||||
| D2D8 | POP HL | Restore HL from the stack | ||||||||||
| D2D9 | JR Z,$D2B8 | If the zero flag is set, go back to D2B8 | ||||||||||
| D2DB | LD ($A037),A | Store A in $A037 in "Game variables" | ||||||||||
| D2DE | CP B | Compare A with B - Halve it until it is no more than B | ||||||||||
| D2DF | JR C,$D2E8 | If A is below B, skip ahead to D2E8 | ||||||||||
| D2E1 | JR Z,$D2E8 | If A equals B, skip ahead to D2E8 | ||||||||||
| D2E3 | SRL A | Shift A right (halve it) | ||||||||||
| D2E5 | JP $D2DE | Go back to D2DE | ||||||||||
| D2E8 | SUB C | Subtract C from A - minus C | ||||||||||
| D2E9 | POP BC | Restore BC from the stack | ||||||||||
| D2EA | POP IX | Restore IX from the stack | ||||||||||
| D2EC | RET | Return to the caller | ||||||||||
| Prev: D2A4 | Up: Map | Next: D2ED |