Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; This is a disection of the pieces of code determining the witch's position
- ; in Super Mario Land 2.
- ; The game runs on a Sharp LR35902, which is similar to a Z80 processor
- ; and uses mostly the same opcodes with a few additions and omissions.
- ; The position of the witch lives at memory address
- ; WITCH_LEFT_RIGHT = 0xafc9.
- ; A 1 in this byte means left, a 0 means right.
- ; This can be found out with a debugger.
- ; Before you enter the room, this piece of code gets executed:
- ...
- rom3::6c40 21 00 af LD HL,DAT_af00 ; load address 0xaf00 into register HL
- rom3::6c43 06 00 LD B,0x0 ; set register B to 0
- rom3::6c45 af XOR A ; set A to zero
- LOOP
- rom3::6c46 22 LD (HL+),A ; set byte at address HL to zero, and increment HL
- rom3::6c47 05 DEC B ; decrement register B
- rom3::6c48 20 fc JR NZ,LOOP ; if B is not zero, repeat loop
- rom3::6c4a c9 RET
- ...
- ; This sets each byte between memory addresses 0xaf00 and 0xafff to zero
- ; in preparation for the witch, which includes her position at 0xafc9.
- ; This means that before you enter the room, the witch is on the right.
- ; After that, the only place in the code that writes to this address is here:
- ...
- next_witch_position
- rom2::57fa 3e 70 LD A,0x70
- rom2::57fc e0 d9 LDH (offset DAT_ffd9),A
- rom2::57fe fa 04 ff LD A,(DIV) ; load current DIV-timer value
- rom2::5801 e6 03 AND 0x3 ; only look at lowest two bits
- rom2::5803 28 08 JR Z,stay_the_same ; if lower two bits are zero: position stays the same
- switch_witch_position ; else: witch changes position
- rom2::5805 fa c9 af LD A,(WITCH_LEFT_RIGHT) ; load current witch position
- rom2::5808 ee 01 XOR 0x1 ; exchange left<->right
- rom2::580a ea c9 af LD (WITCH_LEFT_RIGHT),A ; save new witch position
- stay_the_same
- ...
- ; It loads the current value of a hardware timer (DIV) that updates 16384 times
- ; a second, and looks at the lowest two bits.
- ; This means that there are only 4 values to consider:
- ; 0, 1, 2 and 3.
- ; If it's zero (25% chance), the position doesn't change,
- ; if it's 1, 2 or 3 (75% chance), her position does change.
- ;
- ; This yields the following percentages which should agree with empirical data:
- ; LRL: 42.1875%
- ; RLR: 14.0625%
- ; LLR: 14.0625%
- ; LRR: 14.0625%
- ; LLL: 4.6875%
- ; RRL: 4.6875%
- ; RLL: 4.6875%
- ; RRR: 1.5625%
- ;
- ; Technically this means that the witch position doesn't depend on anything
- ; that happens during the fight, as it is only dependent on the Game Boy's
- ; hardware timer, but even small differences during the fight probably lead to
- ; big differences due to its high frequency of updating.
- ; In any case, manipulating RNG by humans should be fairly tricky, if not impossible.
- ; After patching the ROM at this position:
- rom2::5801 e6 03 AND 0x3
- ; to
- rom2::5801 af XOR A
- rom2::5802 00 NOP
- ; which always sets the read timer value to zero, the witch will never
- ; change position and always appear on the right side.
- ; Conversely, if you patch it to be
- rom2::5801 af XOR A
- rom2::5802 3c INC A
- ; which always sets the read timer value to one (aka something non-zero),
- ; then the witch will always change position, which makes me very confident
- ; that this is indeed the only piece of code that influences the witch's position.
Add Comment
Please, Sign In to add comment