1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 /* Bra.c -- Converters for RISC code
28 2008-10-04 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
35 SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
42 for (i = 0; i <= size; i += 4)
44 if (data[i + 3] == 0xEB)
47 UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]);
50 dest = ip + (UInt32)i + src;
52 dest = src - (ip + (UInt32)i);
54 data[i + 2] = (Byte)(dest >> 16);
55 data[i + 1] = (Byte)(dest >> 8);
56 data[i + 0] = (Byte)dest;
62 SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
69 for (i = 0; i <= size; i += 2)
71 if ((data[i + 1] & 0xF8) == 0xF0 &&
72 (data[i + 3] & 0xF8) == 0xF8)
76 (((UInt32)data[i + 1] & 0x7) << 19) |
77 ((UInt32)data[i + 0] << 11) |
78 (((UInt32)data[i + 3] & 0x7) << 8) |
83 dest = ip + (UInt32)i + src;
85 dest = src - (ip + (UInt32)i);
88 data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
89 data[i + 0] = (Byte)(dest >> 11);
90 data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
91 data[i + 2] = (Byte)dest;
98 SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
104 for (i = 0; i <= size; i += 4)
106 if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
108 UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) |
109 ((UInt32)data[i + 1] << 16) |
110 ((UInt32)data[i + 2] << 8) |
111 ((UInt32)data[i + 3] & (~3));
115 dest = ip + (UInt32)i + src;
117 dest = src - (ip + (UInt32)i);
118 data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3));
119 data[i + 1] = (Byte)(dest >> 16);
120 data[i + 2] = (Byte)(dest >> 8);
128 SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
134 for (i = 0; i <= size; i += 4)
136 if (((data[i] == 0x40) && ((data[i + 1] & 0xC0) == 0x00)) ||
137 ((data[i] == 0x7F) && ((data[i + 1] & 0xC0) == 0xC0)))
140 ((UInt32)data[i + 0] << 24) |
141 ((UInt32)data[i + 1] << 16) |
142 ((UInt32)data[i + 2] << 8) |
143 ((UInt32)data[i + 3]);
150 dest = src - (ip + i);
153 dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
155 data[i + 0] = (Byte)(dest >> 24);
156 data[i + 1] = (Byte)(dest >> 16);
157 data[i + 2] = (Byte)(dest >> 8);
158 data[i + 3] = (Byte)dest;