-
Notifications
You must be signed in to change notification settings - Fork 53
/
decryption.asm
87 lines (69 loc) · 2.52 KB
/
decryption.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;--------------------------------------------------------------------------------
LoadStaticDecryptionKey:
PHB : PHA : PHX : PHY : PHP
REP #$30 ; set 16-bit accumulator & index registers
LDX.w #StaticDecryptionKey ; Source
LDY.w #KeyBase ; Target
LDA.w #$000F ; Length
MVN $307F
PLP : PLY : PLX : PLA : PLB
RTL
;--------------------------------------------------------------------------------
;--------------------------------------------------------------------------------
RetrieveValueFromEncryptedTable:
;Requires 16-bit A and X/Y
;$00-$02 are long address of start of table
;A is desired byte offset into table
;Returns result in A
PHX : PHY
PHA
LDY.b Scrap04 : PHY : LDY.b Scrap06 : PHY : LDY.b Scrap08 : PHY
LDY.b Scrap0A : PHY : LDY.b Scrap0C : PHY : LDY.b Scrap0E : PHY
AND.w #$FFF8 : TAY
LDA.b [$00], Y : STA.l CryptoBuffer : INY #2
LDA.b [$00], Y : STA.l CryptoBuffer+2 : INY #2
LDA.b [$00], Y : STA.l CryptoBuffer+4 : INY #2
LDA.b [$00], Y : STA.l CryptoBuffer+6
LDA.w #$0002 : STA.b Scrap04 ;set block size
JSL.l XXTEA_Decode
PLA : STA.b Scrap0E : PLA : STA.b Scrap0C : PLA : STA.b Scrap0A
PLA : STA.b Scrap08 : PLA : STA.b Scrap06 : PLA : STA.b Scrap04
PLA : AND.w #$0007 : TAX
LDA.l CryptoBuffer, X
PHA
LDA.w #$0000
STA.l CryptoBuffer
STA.l CryptoBuffer+2
STA.l CryptoBuffer+4
STA.l CryptoBuffer+6
PLA
PLY : PLX
RTL
;--------------------------------------------------------------------------------
ChestData = $01E96C
ChestDataPayload = $01EABC ; ChestData+$0150
;--------------------------------------------------------------------------------
GetChestData:
LDA.l IsEncrypted : BNE .encrypted
INC.b Scrap0E : LDX.w #$FFFD ; what we wrote over
JML.l Dungeon_OpenKeyedObject_nextChest
.encrypted
INC.b Scrap0E : LDX.w #$FFFE
.nextChest
INX #2 : CPX.w #$0150 : BEQ .couldntFindChest
LDA.l ChestData, X : AND.w #$7FFF : CMP.b RoomIndex : BNE .nextChest
DEC.b Scrap0E : BNE .nextChest
LDA.b Scrap00 : PHA : LDA.b Scrap02 : PHA
LDA.w #ChestDataPayload : STA.b Scrap00
LDA.w #ChestDataPayload>>16 : STA.b Scrap02
TXA : LSR
JSL RetrieveValueFromEncryptedTable
STA.b Scrap0C
PLA : STA.b Scrap02 : PLA : STA.b Scrap00
LDA.l ChestData, X : ASL A : BCC .smallChest
JML.l Dungeon_OpenKeyedObject_bigChest ;(bank01.asm line #13783)
.smallChest
JML.l Dungeon_OpenKeyedObject_smallChest
.couldntFindChest
JML.l Dungeon_OpenKeyedObject_couldntFindChest
;--------------------------------------------------------------------------------