Debian Codegraph @

docs/codehandler.s

raw
1 #MIT License
2
3 #Copyright (c) 2010-2017 Nuke, brkirch, Y.S, Kenobi, gamemasterplc
4
5 #Permission is hereby granted, free of charge, to any person obtaining a copy
6 #of this software and associated documentation files (the "Software"), to deal
7 #in the Software without restriction, including without limitation the rights
8 #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 #copies of the Software, and to permit persons to whom the Software is
10 #furnished to do so, subject to the following conditions:
11
12 #The above copyright notice and this permission notice shall be included in all
13 #copies or substantial portions of the Software.
14
15 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 #SOFTWARE.
22
23 # Based off of codehandleronly.s from Gecko OS source code.
24 # Example command to build:
25 # powerpc-elf-gcc -mpowerpc -mpaired -mbig codehandler.s -nostartfiles -nodefaultlibs -nostdlib -T codehandler.ld -o codehandler.bin
26
27 .text
28 #Register Defines
29
30 .set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4
31 .set r5,5; .set r6,6; .set r7,7; .set r8,8; .set r9,9
32 .set r10,10; .set r11,11; .set r12,12; .set r13,13; .set r14,14
33 .set r15,15; .set r16,16; .set r17,17; .set r18,18; .set r19,19
34 .set r20,20; .set r21,21; .set r22,22; .set r23,23; .set r24,24
35 .set r25,25; .set r26,26; .set r27,27; .set r28,28; .set r29,29
36 .set r30,30; .set r31,31; .set f0,0; .set f2,2; .set f3,3
37
38 .globl _start
39
40 gameid:
41 .long 0,0
42 cheatdata:
43 .long frozenvalue
44 .space 39*4
45
46 # Warning, _strip_and_align expects cheat codes to start on 0x0 or 0x8.
47 # Properly compiling it will add a nop if needed.
48 _start:
49 stwu r1,-172(r1) # stores sp
50 stw r0,8(r1) # stores r0
51
52 mflr r0
53 stw r0,176(r1) # stores lr
54
55 mfcr r0
56 stw r0,12(r1) # stores cr
57
58 mfctr r0
59 stw r0,16(r1) # stores ctr
60
61 mfxer r0
62 stw r0,20(r1) # stores xer
63
64 stmw r3,24(r1) # saves r3-r31
65
66 mfmsr r25
67 stw r25,168(r1) # save msr
68
69 ori r26,r25,0x2000 #enable floating point ?
70 andi. r26,r26,0xF9FF
71 mtmsr r26
72
73 stfd f2,152(r1) # stores f2
74 stfd f3,160(r1) # stores f3
75
76 lis r31,cheatdata@h #0x8000
77
78 lis r20, 0xCC00
79 lhz r28, 0x4010(r20)
80
81 ori r21, r28, 0xFF
82 sth r21, 0x4010(r20) # disable MP3 memory protection
83
84 lis r15, codelist@h
85 ori r15, r15, codelist@l
86
87 ori r7, r31, cheatdata@l # set pointer for storing data (before the codelist)
88
89 lis r6,0x8000 # default base address = 0x80000000 (code handler)
90
91 mr r16,r6 # default pointer =0x80000000 (code handler)
92
93 li r8,0 # code execution status set to true (code handler)
94
95 lis r3,0x00D0
96 ori r3,r3,0xC0DE
97
98 lwz r4,0(r15)
99 cmpw r3,r4
100 bne- _exitcodehandler
101 lwz r4,4(r15)
102 cmpw r3,r4
103 bne- _exitcodehandler # lf no code list skip code handler
104 addi r15,r15,8
105 b _readcodes
106
107 _exitcodehandler:
108 sth r28,0x4010(r20) # restore memory protection value
109
110 lfd f2,152(r1) # loads f2
111 lfd f3,160(r1) # loads f3
112
113 lwz r25,168(r1)
114 mtmsr r25
115
116 lwz r0,176(r1)
117 mtlr r0 # restores lr
118
119 lwz r0,12(r1)
120 mtcr r0 # restores cr
121
122 lwz r0,16(r1)
123 mtctr r0 # restores ctr
124
125 lwz r0,20(r1)
126 mtxer r0 # restores xer
127
128 lmw r3,24(r1) # restores r3-r31
129
130 lwz r0,8(r1) # loads r0
131
132 addi r1,r1,172
133
134 isync
135
136 blr # return back to game
137
138 _readcodes:
139
140 lwz r3,0(r15) #load code address
141 lwz r4,4(r15) #load code value
142
143 addi r15,r15,8 #r15 points to next code
144
145 andi. r9,r8,1
146 cmpwi cr7,r9,0 #check code execution status in cr7. eq = true, ne = false
147
148 li r9,0 #Clears r9
149
150 rlwinm r10,r3,3,29,31 #r10 = extract code type, 3 bits
151 rlwinm r5,r3,7,29,31 #r5 = extract sub code type 3 bits
152
153 andis. r11,r3,0x1000 #test pointer
154 rlwinm r3,r3,0,7,31 #r3 = extract address in r3 (code type 0/1/2) #0x01FFFFFF
155
156 bne +12 #jump lf the pointer is used
157
158 rlwinm r12,r6,0,0,6 #lf pointer is not used, address = base address
159 b +8
160
161 mr r12,r16 #lf pointer is used, address = pointer
162
163 cmpwi cr4,r5,0 #compares sub code type with 0 in cr4
164
165 cmpwi r10,1
166 blt+ _write #code type 0 : write
167 beq+ _conditional #code type 1 : conditional
168
169 cmpwi r10,3
170 blt+ _ba_pointer #Code type 2 : base address operation
171
172 beq- _repeat_goto #Code type 3 : Repeat & goto
173
174 cmpwi r10,5
175 blt- _operation_rN #Code type 4 : rN Operation
176 beq+ _compare16_NM_counter #Code type 5 : compare [rN] with [rM]
177
178 cmpwi r10,7
179 blt+ _hook_execute #Code type 6 : hook, execute code
180
181 b _terminator_onoff_ #code type 7 : End of code list
182
183 _Write_32:
184 lwz r18, 0(r12) #Load data from registry that will be written to
185 cmpw r18, r5 #Is data to be written equal to the data in memory
186 beq+ +72 #Skip if yes
187 stw r5, 0(r12) #store opcode
188 li r9, 0 #safe, check r9 if more write_32's are linked.
189 b +48
190
191 _Write_08x:
192 lbzx r18, r9, r12
193 rlwinm r0, r4, 0, 24,31 #Clears any other data with r4's byte, for compare
194 cmpw r0, r18
195 beq+ +44
196 stbx r4,r9,r12
197 b +24
198
199 _Write_16x:
200 lhzx r18, r9, r12
201 rlwinm r0, r4, 0, 16,31 #Makes sure r4 is just a halfword for compare
202 cmpw r0, r18
203 beq+ +20
204 sthx r4,r9,r12
205 icbi r9, r12 #branch target. Clears cache. Need dcbf?
206 sync
207 isync
208 blr
209
210 #CT0=============================================================================
211 #write 8bits (0): 00XXXXXX YYYY00ZZ
212 #write 16bits (1): 02XXXXXX YYYYZZZZ
213 #write 32bits (2): 04XXXXXX ZZZZZZZZ
214 #string code (3): 06XXXXXX YYYYYYYY, d1d1d1d1 d2d2d2d2, d3d3d3d3 ....
215 #Serial Code (4): 08XXXXXX YYYYYYYY TNNNZZZZ VVVVVVVV
216
217 _write:
218 add r12,r12,r3 #address = (ba/po)+(XXXXXX)
219 cmpwi r5,3
220 beq- _write_string #r5 == 3, goto string code
221 bgt- _write_serial #r5 >= 4, goto serial code
222
223 bne- cr7,_readcodes #lf code execution set to false skip code
224
225 cmpwi cr4,r5,1 #compares sub code type and 1 in cr4
226
227 bgt- cr4,_write32 #lf sub code type == 2, goto write32
228
229 #lf sub code type = 0 or 1 (8/16bits)
230 rlwinm r10,r4,16,16,31 #r10 = extract number of times to write (16bits value)
231
232 _write816:
233 beq cr4,+16 #lf r5 = 1 then 16 bits write
234 bl _Write_08x
235 addi r9,r9,1
236 b +12
237 bl _Write_16x
238 addi r9, r9, 2
239 subic. r10,r10,1 #number of times to write -1
240 bge- _write816
241 b _readcodes
242
243 _write32:
244 rlwinm r12,r12,0,0,29 #32bits align adress
245 mr r5, r4
246 bl _Write_32
247 b _readcodes
248
249 _write_string: #endianess ?
250 mr r9,r4
251 mr r22, r4
252 bne- cr7,_skip_and_align #lf code execution is false, skip string code data
253
254 _stb:
255 subic. r9,r9,1 #r9 -= 1 (and compares r9 with 0)
256 blt- _skip_and_align #lf r9 < 0 then exit
257 lbzx r5,r9,r15
258 mr r4, r5
259 bl _Write_08x #loop until all the data has been written
260 mr r4, r22
261 b _stb
262
263 _write_serial:
264
265 addi r15,r15,8 #r15 points to the code after the serial code
266 bne- cr7,_readcodes #lf code execution is false, skip serial code
267
268 lwz r5,-8(r15) #load TNNNZZZZ
269 lwz r11,-4(r15) #r11 = load VVVVVVVV
270
271 rlwinm r17,r5,0,16,31 #r17 = ZZZZ
272 rlwinm r10,r5,16,20,31 #r10 = NNN (# of times to write -1)
273 rlwinm r5,r5,4,28,31 #r5 = T (0:8bits/1:16bits/2:32bits)
274
275 _loop_serial:
276 cmpwi cr5,r5,1
277 beq- cr5,+16 #lf 16bits
278 bgt+ cr5,+20 #lf 32bits
279
280 bl _Write_08x
281 b +40
282
283 bl _Write_16x #write serial halfword (CT04,T=1)
284 b +32
285
286 lwzx r18, r9, r12
287 cmpw r4, r18
288 beq+ +20
289 stwx r4,r9,r12 #write serial word (CT04,T>=2)
290 icbi r9, r12 #Invalidate Icache around real memory offset
291 sync
292 isync
293 add r4,r4,r11 #value +=VVVVVVVV
294 add r9,r9,r17 #address +=ZZZZ
295 subic. r10,r10,1
296 bge+ _loop_serial #loop until all the data has been written
297
298 b _readcodes
299
300 #CT1=============================================================================
301 #32bits conditional (0,1,2,3): 20XXXXXX YYYYYYYY
302 #16bits conditional (4,5,6,7): 28XXXXXX ZZZZYYYY
303
304 #PS : 31 bit of address = endlf.
305
306 _conditional:
307 rlwinm. r9,r3,0,31,31 #r10 = (bit31 & 1) (endlf enabled?)
308
309 beq +16 #jump lf endlf is not enabled
310
311 rlwinm r8,r8,31,1,31 #Endlf (r8>>1)
312 andi. r9,r8,1 #r9=code execution status
313 cmpwi cr7,r9,0 #check code execution status in cr7
314 cmpwi cr5,r5,4 #compares sub code type and 4 in cr5
315 cmpwi cr3,r10,5 #compares code type and 5 in cr3
316
317 rlwimi r8,r8,1,0,30 #r8<<1 and current execution status = old execution status
318 bne- cr7,_true_end #lf code execution is set to false -> exit
319
320 bgt cr3,_addresscheck2 #lf code type==6 -> address check
321 add r12,r12,r3 #address = (ba/po)+(XXXXXX)
322
323 blt cr3,+12 #jump lf code type <5 (==1)
324 blt cr5,_condition_sub #compare [rN][rM]
325 b _conditional16_2 #counter compare
326 bge cr5,_conditional16 #lf sub code type>=4 -> 16 bits conditional
327
328 _conditional32:
329 rlwinm r12,r12,0,0,29 #32bits align
330 lwz r11,0(r12)
331 b _condition_sub
332
333 _conditional16:
334 rlwinm r12,r12,0,0,30 #16bits align
335 lhz r11,0(r12)
336 _conditional16_2:
337 nor r9,r4,r4
338 rlwinm r9,r9,16,16,31 #r9 = extract mask
339 and r11,r11,r9 #r11 &= r9
340 rlwinm r4,r4,0,16,31 #r4 = extract data to check against
341
342 _condition_sub:
343 cmpl cr6,r11,r4 #Unsigned compare. r11=data at address, r4=YYYYYYYY
344 andi. r9,r5,3
345 beq _skip_NE #lf sub code (type & 3) == 0
346 cmpwi r9,2
347 beq _skip_LE #lf sub code (type & 3) == 2
348 bgt _skip_GE #lf sub code (type & 3) == 3
349
350 _skip_EQ:#1
351 bne- cr6,_true_end #CT21, CT25, CT29 or CT2D (lf !=)
352 b _skip
353
354 _skip_NE:#0
355 beq- cr6,_true_end #CT20, CT24, CT28 or CT2C (lf==)
356 b _skip
357
358 _skip_LE:#2
359 bgt- cr6,_true_end #CT22, CT26, CT2A or CT2E (lf r4>[])
360 b _skip
361
362 _skip_GE:#3
363 blt- cr6,_true_end #CT23, CT27, CT2B or CT2F (lf r4<r4)
364
365 _skip:
366 ori r8,r8,1 #r8|=1 (execution status set to false)
367 _true_end:
368 bne+ cr3,_readcodes #lf code type <> 5
369 blt cr5,_readcodes
370 lwz r11,-8(r15) #load counter
371 bne cr7,_clearcounter #lf previous code execution false clear counter
372 andi. r12,r3,0x8 #else lf clear counter bit not set increase counter
373 beq _increase_counter
374 andi. r12,r8,0x1 #else lf.. code result true clear counter
375 beq _clearcounter
376
377 _increase_counter:
378 addi r12,r11,0x10 #else increase the counter
379 rlwimi r11,r12,0,12,27 #update counter
380 b _savecounter
381
382 _clearcounter:
383 rlwinm r11,r11,0,28,11 #clear the counter
384 _savecounter:
385 stw r11,-8(r15) #save counter
386 b _readcodes
387
388 #CT2============================================================================
389
390 #load base address (0): 40TYZ00N XXXXXXXX = (load/add:T) ba from [(ba/po:Y)+XXXXXXXX(+rN:Z)]
391
392 #set base address (1): 42TYZ00N XXXXXXXX = (set/add:T) ba to (ba/po:Y)+XXXXXXXX(+rN:Z)
393
394 #store base address (2): 440Y0000 XXXXXXXX = store base address to [(ba/po)+XXXXXXXX]
395 #set base address to (3): 4600XXXX 00000000 = set base address to code address+XXXXXXXX
396 #load pointer (4): 48TYZ00N XXXXXXXX = (load/add:T) po from [(ba/po:Y)+XXXXXXXX(+rN:Z)]
397
398 #set pointer (5): 4ATYZ00N XXXXXXXX = (set/add:T) po to (ba/po:Y)+XXXXXXXX(+rN:Y)
399
400 #store pointer (6): 4C0Y0000 XXXXXXXX = store pointer to [(ba/po)+XXXXXXXX]
401 #set pointer to (7): 4E00XXXX 00000000 = set pointer to code address+XXXXXXXX
402
403 _ba_pointer:
404
405 bne- cr7,_readcodes
406
407 rlwinm r9,r3,2,26,29 #r9 = extract N, makes N*4
408
409 rlwinm r14,r3,16,31,31 #r3 = add ba/po flag bit (Y)
410 cmpwi cr3,r14,0
411
412 cmpwi cr4,r5,4 #cr4 = compare sub code type with 4 (ba/po)
413 andi. r14,r5,3 #r14 = sub code type and 3
414
415 cmpwi cr5,r14,2 #compares sub code type and 2
416
417 blt- cr5,_p01
418 beq- cr5,_p2 #sub code type 2
419
420 _p3:
421 extsh r4,r3
422 add r4,r4,r15 #r4=XXXXXXXX+r15 (code location in memory)
423 b _pend
424
425 _p01:
426
427 rlwinm. r5,r3,20,31,31 #r3 = rN use bit (Z)
428 beq +12 #flag is not set(=0), address = XXXXXXXX
429
430 lwzx r9,r7,r9 #r9 = load register N
431 add r4,r4,r9 #flag is set (=1), address = XXXXXXXX+rN
432
433 beq cr3,+8 #(Y) flag is not set(=0), address = XXXXXXXX (+rN)
434
435 add r4,r12,r4 #address = XXXXXXXX (+rN) + (ba/po)
436
437 cmpwi cr5,r14,1
438 beq cr5,+8 #address = (ba/po)+XXXXXXXX(+rN)
439 lwz r4,0(r4) #address = [(ba/po)+XXXXXXXX(+rN)]
440
441 rlwinm. r3,r3,12,31,31 #r5 = add/replace flag (T)
442 beq _pend #flag is not set (=0), (ba/po)= XXXXXXXX (+rN) + (ba/po)
443 bge cr4,+12
444 add r4,r4,r6 #ba += XXXXXXXX (+rN) + (ba/po)
445 b _pend
446 add r4,r4,r16 #po += XXXXXXXX (+rN) + (ba/po)
447 b _pend
448
449 _p2:
450 rlwinm. r5,r3,20,31,31 #r3 = rN use bit (Z)
451 beq +12 #flag is not set(=0), address = XXXXXXXX
452
453 lwzx r9,r7,r9 #r9 = load register N
454 add r4,r4,r9 #flag is set (=1), address = XXXXXXXX+rN
455
456 bge cr4,+12
457 stwx r6,r12,r4 #[(ba/po)+XXXXXXXX] = base address
458 b _readcodes
459 stwx r16,r12,r4 #[(ba/po)+XXXXXXXX] = pointer
460 b _readcodes
461
462 _pend:
463 bge cr4,+12
464 mr r6,r4 #store result to base address
465 b _readcodes
466 mr r16,r4 #store result to pointer
467 b _readcodes
468
469 #CT3============================================================================
470 #set repeat (0): 6000ZZZZ 0000000P = set repeat
471 #execute repeat (1): 62000000 0000000P = execute repeat
472 #return (2): 64S00000 0000000P = return (lf true/false/always)
473 #goto (3): 66S0XXXX 00000000 = goto (lf true/false/always)
474 #gosub (4): 68S0XXXX 0000000P = gosub (lf true/false/always)
475
476
477 _repeat_goto:
478 rlwinm r9,r4,3,25,28 #r9 = extract P, makes P*8
479 addi r9,r9,0x40 #offset that points to block P's
480 cmpwi r5,2 #compares sub code type with 2
481 blt- _repeat
482
483
484 rlwinm. r11,r3,10,0,1 #extract (S&3)
485 beq +20 #S=0, skip lf true, don't skip lf false
486 bgt +8
487 b _b_bl_blr_nocheck #S=2/3, always skip (code exec status turned to true)
488 beq- cr7,_readcodes #S=1, skip lf false, don't skip lf true
489 b _b_bl_blr_nocheck
490
491 _b_bl_blr:
492 bne- cr7,_readcodes #lf code execution set to false skip code
493
494 _b_bl_blr_nocheck:
495 cmpwi r5,3
496
497 bgt- _bl #sub code type >=4, bl
498 beq+ _b #sub code type ==3, b
499
500 _blr:
501 lwzx r15,r7,r9 #loads the next code address
502 b _readcodes
503
504 _bl:
505 stwx r15,r7,r9 #stores the next code address in block P's address
506 _b:
507 extsh r4,r3 #XXXX becomes signed
508 rlwinm r4,r4,3,9,28
509
510 add r15,r15,r4 #next code address +/-=line XXXX
511 b _readcodes
512
513 _repeat:
514 bne- cr7,_readcodes #lf code execution set to false skip code
515
516 add r5,r7,r9 #r5 points to P address
517 bne- cr4,_execute_repeat #branch lf sub code type == 1
518
519 _set_repeat:
520 rlwinm r4,r3,0,16,31 #r4 = extract NNNNN
521 stw r15,0(r5) #store current code address to [bP's address]
522 stw r4,4(r5) #store NNNN to [bP's address+4]
523
524 b _readcodes
525
526 _execute_repeat:
527 lwz r9,4(r5) #load NNNN from [M+4]
528 cmpwi r9,0
529 beq- _readcodes
530 subi r9,r9,1
531 stw r9,4(r5) #saves (NNNN-1) to [bP's address+4]
532 lwz r15,0(r5) #load next code address from [bP's address]
533 b _readcodes
534
535 #CT4============================================================================
536 #set/add to rN(0) : 80SY000N XXXXXXXX = rN = (ba/po) + XXXXXXXX
537 #load rN (1) : 82UY000N XXXXXXXX = rN = [XXXXXXXX] (offset support) (U:8/16/32)
538 #store rN (2) : 84UYZZZN XXXXXXXX = store rN in [XXXXXXXX] (offset support) (8/16/32)
539
540 #operation 1 (3) : 86TY000N XXXXXXXX = operation rN?XXXXXXXX ([rN]?XXXXXXXX)
541 #operation 2 (4) : 88TY000N 0000000M = operation rN?rM ([rN]?rM, rN?[rM], [rN]?[rM])
542
543 #copy1 (5) : 8AYYYYNM XXXXXXXX = copy YYYY bytes from [rN] to ([rM]+)XXXXXXXX
544 #copy2 (6) : 8CYYYYNM XXXXXXXX = copy YYYY bytes from ([rN]+)XXXXXX to [rM]
545
546
547 #for copy1/copy2, lf register == 0xF, base address is used.
548
549 #of course, sub codes types 0/1, 2/3 and 4/5 can be put together lf we need more subtypes.
550
551
552 _operation_rN:
553 bne- cr7,_readcodes
554
555 rlwinm r11,r3,2,26,29 #r11 = extract N, makes N*4
556 add r26,r7,r11 #1st value address = rN's address
557 lwz r9,0(r26) #r9 = rN
558
559 rlwinm r14,r3,12,30,31 #extracts S, U, T (3bits)
560
561 beq- cr4,_op0 #lf sub code type = 0
562
563 cmpwi cr4,r5,5
564 bge- cr4,_op56 #lf sub code type = 5/6
565
566 cmpwi cr4,r5,3
567 bge- cr4,_op34 #lf sub code type = 3/4
568
569 cmpwi cr4,r5,1
570
571 _op12: #load/store
572 rlwinm. r5,r3,16,31,31 #+(ba/po) flag : Y
573 beq +8 #address = XXXXXXXX
574 add r4,r12,r4
575
576 cmpwi cr6,r14,1
577 bne- cr4,_store
578
579 _load:
580 bgt+ cr6,+24
581 beq- cr6,+12
582
583 lbz r4,0(r4) #load byte at address
584 b _store_reg
585
586 lhz r4,0(r4) #load halfword at address
587 b _store_reg
588
589 lwz r4,0(r4) #load word at address
590 b _store_reg
591
592 _store:
593 rlwinm r19,r3,28,20,31 #r9=r3 ror 12 (N84UYZZZ)
594
595 mr r12, r4
596 mr r4, r9
597 mr r5, r9
598 li r9, 0
599 _storeloop:
600 bgt+ cr6,+32
601 beq- cr6,+16
602
603 bl _Write_08x #store byte at address
604 addi r12,r12,1
605 b _storeloopend
606
607 bl _Write_16x #store byte at address
608 addi r12,r12,2
609 b _storeloopend
610
611 bl _Write_32
612 addi r12,r12,4
613 _storeloopend:
614 subic. r19,r19,1
615 bge _storeloop
616 b _readcodes
617
618 _op0:
619 rlwinm. r5,r3,16,31,31 #+(ba/po) flag : Y
620 beq +8 #value = XXXXXXXX
621 add r4,r4,r12 #value = XXXXXXXX+(ba/po)
622
623 andi. r5,r14,1 #add flag : S
624 beq _store_reg #add flag not set (=0), rN=value
625 add r4,r4,r9 #add flag set (=1), rN=rN+value
626 b _store_reg
627
628 _op34: #operation 1 & 2
629 rlwinm r10,r3,16,30,31 #extracts Y
630
631 rlwinm r14,r4,2,26,29 #r14 = extract M (in r4), makes M*=4
632
633 add r19,r7,r14 #2nd value address = rM's address
634 bne cr4,+8
635 subi r19,r15,4 #lf CT3, 2nd value address = XXXXXXXX's address
636
637 lwz r4,0(r26) #1st value = rN
638
639 lwz r9,0(r19) #2nd value = rM/XXXXXXXX
640
641 andi. r11,r10,1 #lf [] for 1st value
642 beq +8
643 mr r26,r4
644
645 andi. r11,r10,2 #lf [] for 2nd value
646 beq +16
647 mr r19,r9
648 bne+ cr4,+8
649 add r19,r12,r19 #lf CT3, 2nd value address = XXXXXXXX+(ba/op)
650
651 rlwinm. r5,r3,12,28,31 #operation # flag : T
652
653 cmpwi r5,9
654 bge _op_float
655
656 _operation_bl:
657 bl _operation_bl_return
658
659 _op450:
660 add r4,r9,r4 #N + M
661 b _store_reg
662
663 _op451:
664 mullw r4,r9,r4 #N * M
665 b _store_reg
666
667 _op452:
668 or r4,r9,r4 #N | M
669 b _store_reg
670
671 _op453:
672 and r4,r9,r4 #N & M
673 b _store_reg
674
675 _op454:
676 xor r4,r9,r4 #N ^ M
677 b _store_reg
678
679 _op455:
680 slw r4,r9,r4 #N << M
681 b _store_reg
682
683 _op456:
684 srw r4,r9,r4 #N >> M
685 b _store_reg
686
687 _op457:
688 rlwnm r4,r9,r4,0,31 #N rol M
689 b _store_reg
690
691 _op458:
692 sraw r4,r9,r4 #N asr M
693
694 _store_reg:
695 stw r4,0(r26) #Store result in rN/[rN]
696 b _readcodes
697
698 _op_float:
699 cmpwi r5,0xA
700 bgt _readcodes
701
702 lfs f2,0(r26) #f2 = load 1st value
703 lfs f3,0(r19) #f3 = load 2nd value
704 beq- _op45A
705
706
707 _op459:
708 fadds f2,f3,f2 #N = N + M (float)
709 b _store_float
710
711 _op45A:
712 fmuls f2,f3,f2 #N = N * M (float)
713
714 _store_float:
715 stfs f2,0(r26) #Store result in rN/[rN]
716 b _readcodes
717
718 _operation_bl_return:
719 mflr r10
720 rlwinm r5,r5,3,25,28 #r5 = T*8
721 add r10,r10,r5 #jumps to _op5: + r5
722
723 lwz r4,0(r26) #load [rN]
724 lwz r9,0(r19) #2nd value address = rM/XXXXXXXX
725
726 mtlr r10
727 blr
728
729 #copy1 (5) : 8AYYYYNM XXXXXXXX = copy YYYY bytes from [rN] to ([rM]+)XXXXXXXX
730 #copy2 (6) : 8CYYYYNM XXXXXXXX = copy YYYY bytes from ([rN]+)XXXXXX to [rM]
731
732 _op56:
733
734 bne- cr7,_readcodes #lf code execution set to false skip code
735
736 rlwinm r9,r3,24,0,31 #r9=r3 ror 8 (NM8AYYYY, NM8CYYYY)
737 mr r14,r12 #r14=(ba/po)
738 bl _load_NM
739
740 beq- cr4,+12
741 add r17,r17,r4 #lf sub code type==0 then source+=XXXXXXXX
742 b +8
743 add r9,r9,r4 #lf sub code type==1 then destination+=XXXXXXXX
744
745 rlwinm. r4,r3,24,16,31 #Extracts YYYY, compares it with 0
746 li r5,0
747
748 _copy_loop:
749 beq _readcodes #Loop until all bytes have been copied.
750 lbzx r10,r5,r17
751 stbx r10,r5,r9
752 addi r5,r5,1
753 cmpw r5,r4
754 b _copy_loop
755
756 #===============================================================================
757 #This is a routine called by _memory_copy and _compare_NM_16
758
759 _load_NM:
760
761 cmpwi cr5,r10,4 #compare code type and 4(rn Operations) in cr5
762
763 rlwinm r17,r9,6,26,29 #Extracts N*4
764 cmpwi r17,0x3C
765 lwzx r17,r7,r17 #Loads rN value in r17
766 bne +8
767 mr r17,r14 #lf N==0xF then source address=(ba/po)(+XXXXXXXX, CT5)
768
769 beq cr5,+8
770 lhz r17,0(r17) #...and lf CT5 then N = 16 bits at [XXXXXX+base address]
771
772 rlwinm r9,r9,10,26,29 #Extracts M*4
773 cmpwi r9,0x3C
774 lwzx r9,r7,r9 #Loads rM value in r9
775 bne +8
776 mr r9,r14 #lf M==0xF then dest address=(ba/po)(+XXXXXXXX, CT5)
777
778 beq cr5,+8
779 lhz r9,0(r9) #...and lf CT5 then M = 16 bits at [XXXXXX+base address]
780
781 blr
782
783 #CT5============================================================================
784 #16bits conditional (0,1,2,3): A0XXXXXX NM00YYYY (unknown values)
785 #16bits conditional (4,5,6,7): A8XXXXXX ZZZZYYYY (counter)
786
787 #sub codes types 0,1,2,3 compare [rN] with [rM] (both 16bits values)
788 #lf register == 0xF, the value at [base address+XXXXXXXX] is used.
789
790 _compare16_NM_counter:
791 cmpwi r5,4
792 bge _compare16_counter
793
794 _compare16_NM:
795
796 mr r9,r4 #r9=NM00YYYY
797
798 add r14,r3,r12 #r14 = XXXXXXXX+(ba/po)
799
800 rlwinm r14,r14,0,0,30 #16bits align (base address+XXXXXXXX)
801
802 bl _load_NM #r17 = N's value, r9 = M's value
803
804 nor r4,r4,r4 #r4=!r4
805 rlwinm r4,r4,0,16,31 #Extracts !YYYY
806
807 and r11,r9,r4 #r3 = (M AND !YYYY)
808 and r4,r17,r4 #r4 = (N AND !YYYY)
809
810 b _conditional
811
812 _compare16_counter:
813 rlwinm r11,r3,28,16,31 #extract counter value from r3 in r11
814 b _conditional
815
816 #===============================================================================
817 #execute (0) : C0000000 NNNNNNNN = execute. End with 4E800020 00000000.
818 #hook1 (2) : C4XXXXXX NNNNNNNN = insert instructions at XXXXXX. Same as C2.
819 #hook2 (3) : C6XXXXXX YYYYYYYY = branch from XXXXXX to YYYYYY
820 #on/off (6) : CC000000 00000000 = on/off switch
821 #range check (7) : CE000000 XXXXYYYY = is ba/po in XXXX0000-YYYY0000
822
823 _hook_execute:
824 mr r26,r4 #r26 = 0YYYYYYY
825 rlwinm r4,r4,3,0,28 #r4 = NNNNNNNN*8 = number of lines (and not number of bytes)
826 bne- cr4,_hook_addresscheck #lf sub code type != 0
827 bne- cr7,_skip_and_align
828
829 _execute:
830 mtlr r15
831 blrl
832
833 _skip_and_align:
834 add r15,r4,r15
835 addi r15,r15,7
836 rlwinm r15,r15,0,0,28 #align 64-bit
837 b _readcodes
838
839 _hook_addresscheck:
840
841 cmpwi cr4,r5,3
842 bgt- cr4,_addresscheck1 #lf sub code type ==6 or 7
843 lis r5,0x4800
844 add r12,r3,r12
845 rlwinm r12,r12,0,0,29 #align address
846
847 bne- cr4,_hook1 #lf sub code type ==2
848
849 _hook2:
850 bne- cr7,_readcodes
851
852 rlwinm r4,r26,0,0,29 #address &=0x01FFFFFC
853
854 sub r4,r4,r12 #r4 = to-from
855 rlwimi r5,r4,0,6,29 #r5 = (r4 AND 0x03FFFFFC) OR 0x48000000
856 rlwimi r5,r3,0,31,31 #restore lr bit
857 bl _Write_32
858 b _readcodes
859
860 _hook1:
861 bne- cr7,_skip_and_align
862
863 sub r9,r15,r12 #r9 = to-from
864 rlwimi r5,r9,0,6,29 #r5 = (r9 AND 0x03FFFFFC) OR 0x48000000
865 bl _Write_32
866 addi r12,r12,4
867 add r11,r15,r4
868 subi r11,r11,4 #r11 = address of the last word of the hook1 code
869 sub r9,r12,r11
870 rlwimi r5,r9,0,6,29 #r5 = (r9 AND 0x03FFFFFC) OR 0x48000000
871 mr r12, r11
872 bl _Write_32
873 b _skip_and_align
874
875 _addresscheck1:
876 cmpwi cr4,r5,6
877 beq cr4,_onoff
878 b _conditional
879 _addresscheck2:
880
881 rlwinm r12,r12,16,16,31
882 rlwinm r4,r26,16,16,31
883 rlwinm r26,r26,0,16,31
884 cmpw r12,r4
885 blt _skip
886 cmpw r12,r26
887 bge _skip
888 b _readcodes
889
890 _onoff:
891 rlwinm r5,r26,31,31,31 #extracts old exec status (x b a)
892 xori r5,r5,1
893 andi. r3,r8,1 #extracts current exec status
894 cmpw r5,r3
895 beq _onoff_end
896 rlwimi r26,r8,1,30,30
897 xori r26,r26,2
898 rlwinm. r5,r26,31,31,31 #extracts b
899 beq +8
900
901 xori r26,r26,1
902
903 stw r26,-4(r15) #updates the code value in the code list
904
905 _onoff_end:
906 rlwimi r8,r26,0,31,31 #current execution status = a
907
908 b _readcodes
909
910 #===============================================================================
911 #Full terminator (0) = E0000000 XXXXXXXX = full terminator
912 #Endlfs/Else (1) = E2T000VV XXXXXXXX = endlfs (+else)
913 #End code handler = F0000000 00000000
914
915 _terminator_onoff_:
916 cmpwi r11,0 #lf code type = 0xF
917 beq _notTerminator
918 cmpwi r5,1
919 beq _asmTypeba
920 cmpwi r5,2
921 beq _asmTypepo
922 cmpwi r5,3
923 beq _patchType
924 b _exitcodehandler
925 _asmTypeba:
926 rlwinm r12,r6,0,0,6 # use base address
927 _asmTypepo:
928 rlwinm r23,r4,8,24,31 # extract number of half words to XOR
929 rlwinm r24,r4,24,16,31 # extract XOR checksum
930 rlwinm r4,r4,0,24,31 # set code value to number of ASM lines only
931 bne cr7,_goBackToHandler #skip code if code execution is set to false
932 rlwinm. r25,r23,0,24,24 # check for negative number of half words
933 mr r26,r12 # copy ba/po address
934 add r26,r3,r26 # add code offset to ba/po code address
935 rlwinm r26,r26,0,0,29 # clear last two bits to align address to 32-bit
936 beq _positiveOffset # if number of half words is negative, extra setup needs to be done
937 extsb r23,r23
938 neg r23,r23
939 mulli r25,r23,2
940 addi r25,r25,4
941 subf r26,r25,r26
942 _positiveOffset:
943 cmpwi r23,0
944 beq _endXORLoop
945 li r25,0
946 mtctr r23
947 _XORLoop:
948 lhz r27,4(r26)
949 xor r25,r27,r25
950 addi r26,r26,2
951 bdnz _XORLoop
952 _endXORLoop:
953 cmpw r24,r25
954 bne _goBackToHandler
955 b _hook_execute
956 _patchType:
957 rlwimi r8,r8,1,0,30 #r8<<1 and current execution status = old execution status
958 bne cr7,_exitpatch #lf code execution is set to false -> exit
959 rlwinm. r23,r3,22,0,1
960 bgt _patchfail
961 blt _copytopo
962 _runpatch:
963 rlwinm r30,r3,0,24,31
964 mulli r30,r30,2
965 rlwinm r23,r4,0,0,15
966 xoris r24,r23,0x8000
967 cmpwi r24,0
968 bne- _notincodehandler
969 ori r23,r23,0x3000
970 _notincodehandler:
971 rlwinm r24,r4,16,0,15
972 mulli r25,r30,4
973 subf r24,r25,r24
974 _patchloop:
975 li r25,0
976 _patchloopnext:
977 mulli r26,r25,4
978 lwzx r27,r15,r26
979 lwzx r26,r23,r26
980 addi r25,r25,1
981 cmplw r23,r24
982 bgt _failpatchloop
983 cmpw r25,r30
984 bgt _foundaddress
985 cmpw r26,r27
986 beq _patchloopnext
987 addi r23,r23,4
988 b _patchloop
989 _foundaddress:
990 lwz r3,-8(r15)
991 ori r3,r3,0x300
992 stw r3,-8(r15)
993 stw r23,-4(r15)
994 mr r16,r23
995 b _exitpatch
996 _failpatchloop:
997 lwz r3,-8(r15)
998 ori r3,r3,0x100
999 stw r3,-8(r15)
1000 _patchfail:
1001 ori r8,r8,1 #r8|=1 (execution status set to false)
1002 b _exitpatch
1003 _copytopo:
1004 mr r16,r4
1005 _exitpatch:
1006 rlwinm r4,r3,0,24,31 # set code to number of lines only
1007 _goBackToHandler:
1008 mulli r4,r4,8
1009 add r15,r4,r15 # skip the lines of the code
1010 b _readcodes
1011 _notTerminator:
1012
1013 _terminator:
1014 bne cr4,+12 #check lf sub code type == 0
1015 li r8,0 #clear whole code execution status lf T=0
1016 b +20
1017
1018 rlwinm. r9,r3,0,27,31 #extract VV
1019 # bne +8 #lf VV!=0
1020 # bne- cr7,+16
1021
1022 rlwinm r5,r3,12,31,31 #extract "else" bit
1023
1024 srw r8,r8,r9 #r8>>VV, meaning endlf VV lfs
1025
1026 rlwinm. r23,r8,31,31,31
1027 bne +8 # execution is false if code execution >>, so don't invert code status
1028 xor r8,r8,r5 #lf 'else' is set then invert current code status
1029
1030 _load_baseaddress:
1031 rlwinm. r5,r4,0,0,15
1032 beq +8
1033 mr r6,r5 #base address = r4
1034 rlwinm. r5,r4,16,0,15
1035 beq +8
1036 mr r16,r5 #pointer = r4
1037 b _readcodes
1038
1039 #===============================================================================
1040
1041 frozenvalue: #frozen value, then LR
1042 .long 0,0
1043 dwordbuffer:
1044 .long 0,0
1045 rem:
1046 .long 0
1047 bpbuffer:
1048 .long 0 #int address to bp on
1049 .long 0 #data address to bp on
1050 .long 0 #alignement check
1051 .long 0 #counter for alignement
1052
1053 regbuffer:
1054 .space 72*4
1055
1056 .align 3
1057
1058 codelist:
1059 .space 2*4
1060 .end