warmCabin

Mega Man 2 - Password reading code

Jun 5th, 2021 (edited)
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. read_password:
  2. ; These passwords were cracked long ago.
  3. ; Essentially, row A corresponds to E-tanks,
  4. ; rows B-E correspond to robot defeated flags.
  5. ; Put a ball in row A to get 1 - column number E tanks.
  6. ;
  7. ; Each robot master has a set position for alive and defeated;
  8. ; these are encoded in the two password_flags tables.
  9. ; The positions are offset by how many E-tanks you have.
  10. ; All 8 robots must be accounted for in a password.
  11.  0D:A3E3: 20 E9 A8  JSR update_password_graphics
  12. ; Black out cursor
  13.  0D:A3E6: A9 0F     LDA #$0F
  14.  0D:A3E8: 8D 6C 03  STA palette_mirror[16]
  15. ; Loop to determine which column in row A is filled.
  16.  0D:A3EB: A2 00     LDX #0
  17. row_A_check:
  18.  0D:A3ED: BD 20 04  LDA sprite_flags,X
  19.  0D:A3F0: D0 05     BNE row_A_slot_determined
  20.  0D:A3F2: E8        INX
  21.  0D:A3F3: E0 04     CPX #4
  22.  0D:A3F5: D0 F6     BNE row_A_check
  23. ; Interesting bug.
  24. ; If you don't put a ball in row A, it will assume
  25. ; one is in A5. This results in 256 * 12 unintended passwords.
  26. ; The only thing you can do is mark a robot as simultaneously
  27. ; alive and dead, which just means he counts as defeated
  28. ; in your savegame.
  29. ; Could add JMP password_invalid here to fix the bug.
  30. row_A_slot_determined:
  31. ; X = which slot it's in.
  32. ; $04 will now serve as number of E tanks.
  33.  0D:A3F7: 86 04     STX $04
  34. ; Increment X by 5 to start checking at the row below.
  35. ; e.g. A3 becomes B3
  36.  0D:A3F9: 8A        TXA
  37.  0D:A3FA: 18        CLC
  38.  0D:A3FB: 69 05     ADC #5
  39.  0D:A3FD: AA        TAX
  40. ; $01 = number of slots checked
  41. ; $02 = Robot masters beaten (will be written to weapons_unlocked)
  42. ; $03 = Robot masters NOT beaten (used for validation)
  43.  0D:A3FE: A9 00     LDA #0
  44.  0D:A400: 85 01     STA $01
  45.  0D:A402: 85 02     STA $02
  46.  0D:A404: 85 03     STA $03
  47. main_password_loop:
  48. ; Loops 20 times starting directly below the first
  49. ; filled row A slot. Guaranteed to hit B1 - E5.
  50.  0D:A406: BD 20 04  LDA sprite_flags,X
  51.  0D:A409: F0 11     BEQ no_ball_here
  52. ; Get flags and OR them into $02 or $03
  53. ; password_flags stores which robot master this slot corresponds to.
  54. ; password_flags_types indicates whether he's defeated (0)
  55. ; or undefeated (1)
  56. ; Many of the flags are 0, indicating this slot corresponds
  57. ; to nothing. You could actually jump straight to password_invalid
  58. ; if you see a 0 here.
  59.  0D:A40B: A4 01     LDY $01
  60.  0D:A40D: B9 C2 AF  LDA password_flags,Y
  61.  0D:A410: 48        PHA
  62.  0D:A411: B9 D6 AF  LDA password_flag_types,Y
  63.  0D:A414: A8        TAY
  64.  0D:A415: 68        PLA
  65.  0D:A416: 19 02 00  ORA $0002,Y
  66.  0D:A419: 99 02 00  STA $0002,Y
  67. no_ball_here:
  68.  0D:A41C: E8        INX
  69.  0D:A41D: E0 19     CPX #25
  70.  0D:A41F: D0 02     BNE +
  71. ; Past the end of the ball slots. Go back to B1.
  72.  0D:A421: A2 05     LDX #5
  73. +:
  74.  0D:A423: E6 01     INC $01
  75.  0D:A425: A5 01     LDA $01
  76. ; Loop 20 times.
  77.  0D:A427: C9 14     CMP #20
  78.  0D:A429: D0 DB     BNE main_password_loop
  79. ; Check if password is valid.
  80. ; $02 is what weapons we have unlocked.
  81. ; $03 is what we DON'T have unlocked.
  82. ; All 8 robots must be accounted for.
  83.  0D:A42B: A5 02     LDA $02
  84.  0D:A42D: 05 03     ORA $03
  85.  0D:A42F: C9 FF     CMP #$FF
  86.  0D:A431: D0 03     BNE password_invalid
  87.  0D:A433: 4C 7B A4  JMP password_valid
  88.  
Add Comment
Please, Sign In to add comment