Guest User

SML2 witch position

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