-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynaCodes.h
218 lines (179 loc) · 5.6 KB
/
DynaCodes.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//#define __DYNALOG__
#define MAX_LINKS 400
#define MAX_LOCS 0x5000
//#define BAD_PC 0x802020EC
//#define BAD_PC 0x80200414
#define BAD_PC 0//0x8020065C
// Globals and Defines
typedef union {
DWORD * udwPtr;
WORD * uwPtr;
BYTE * ubPtr;
void * ptr;
} BUFFER_POINTER;
typedef struct {
u32 target;
u8 *mem;
} LinkInfo;
extern BUFFER_POINTER x86Buff, x86BlockPtr;
extern LinkInfo LinkTable[MAX_LINKS];
extern LinkInfo JumpTable[MAX_LOCS];
extern u32 LinkLocs;
extern u32 LinkCnt;
extern FILE *dynaLogFile;
extern u32 compStart, compEnd; // Compiler Range for determining if Branch/Jump should be local
extern u8 *rdram;
extern u8 *startPtr; // Start of current compiled block
extern bool blockFailed;
extern u32 JalEntry;
// Function Definitions
unsigned long GenerateCRC (unsigned char *data, int size);
void OpcodeLookup (DWORD addy, char *out);
u32 *R4KDyna_RecompileOpcode (u32 *op);
extern void (*r4300i[0x40])();
extern void (*special[0x40])();
extern void (*regimm[0x20])();
extern void (*MmuSpecial[0x40])();
extern void (*MmuNormal[0x20])();
extern void (*FpuCommands[0x40])();
extern void CheckInterrupts(void);
inline void DynaLog(char * Message, ...) {
#ifdef __DYNALOG__
char Msg[512];
va_list ap;
va_start(ap, Message);
vsprintf(Msg, Message, ap);
va_end(ap);
fprintf(dynaLogFile, "%s\n", Msg);
#endif
}
/*************************** x86 ***************************/
__inline void PushFD() {
DynaLog(" pushfd");
*(x86BlockPtr.ubPtr++) = 0x9C;
}
__inline void PopFD() {
DynaLog(" popfd");
*(x86BlockPtr.ubPtr++) = 0x9D;
}
__inline void Lahf () {
DynaLog(" lahf");
*(x86BlockPtr.ubPtr++) = 0x9F;
}
__inline void Sahf () {
DynaLog(" sahf");
*(x86BlockPtr.ubPtr++) = 0x9E;
}
__inline void SetBranch8b(void * JumpByte, void * Destination) {
/* calculate 32-bit relative offset */
signed int n = (BYTE*)Destination - ((BYTE*)JumpByte + 1);
*(BYTE*)(JumpByte) = (BYTE)n;
}
__inline void SetBranch32b(void * JumpByte, void * Destination) {
*(DWORD*)(JumpByte) = (DWORD)((BYTE*)Destination - (BYTE*)((DWORD*)JumpByte + 1));
}
__inline void MoveConstToVariable (DWORD Const, void * Variable, char * VariableName) {
DynaLog(" mov [%s], 0x%08X", VariableName, Const);
*(x86BlockPtr.uwPtr++) = 0x05C7;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
*(x86BlockPtr.udwPtr++) = Const;
}
__inline void CallFunctionDirect(void * FunctAddress, char * FunctName) {
DynaLog(" call %s", FunctName);
*(x86BlockPtr.ubPtr++) = 0xE8;
*(x86BlockPtr.udwPtr++) = (DWORD)FunctAddress-(DWORD)x86BlockPtr.ptr - 4;
}
__inline void AddConstToVariable (DWORD Const, void * Variable, char * VariableName) {
DynaLog(" add [%s], 0x%08X", VariableName, Const);
*(x86BlockPtr.uwPtr++) = 0x0581;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
*(x86BlockPtr.udwPtr++) = Const;
}
__inline void SubConst8ToVariable (BYTE Const, void * Variable, char * VariableName) {
DynaLog(" sub [%s], 0x%08X", VariableName, Const);
*(x86BlockPtr.uwPtr++) = 0x2D83;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
*(x86BlockPtr.ubPtr++) = Const;
}
__inline void Ret(void) {
DynaLog(" ret");
*(x86BlockPtr.ubPtr++) = 0xC3;
}
__inline void MoveConstToEax(DWORD constant) {
DynaLog(" mov eax, 0%08Xh", constant);
*(x86BlockPtr.ubPtr++) = 0xB8;
*(x86BlockPtr.udwPtr++) = (DWORD)constant;
}
__inline void MoveVariableToEax(void *Variable, char *VariableName) {
DynaLog(" mov eax, [%s]", VariableName);
*(x86BlockPtr.uwPtr++) = 0x058B;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
}
__inline void MoveEaxToVariable(void * Variable, char * VariableName) {
DynaLog(" mov dword ptr [%s], eax",VariableName);
*(x86BlockPtr.uwPtr++) = 0x0589;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
}
__inline void CompConstToVariable(DWORD Const, void * Variable, char * VariableName) {
DynaLog(" cmp dword ptr [%s], 0x%X",VariableName, Const);
*(x86BlockPtr.uwPtr++) = 0x3D81;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
*(x86BlockPtr.udwPtr++) = Const;
}
__inline void CompEaxToVariable(void * Variable, char * VariableName) {
DynaLog(" cmp eax, dword ptr [%s]",VariableName);
*(x86BlockPtr.uwPtr++) = 0x053B;
*(x86BlockPtr.udwPtr++) = (DWORD)Variable;
}
__inline void CompConstToEax(DWORD Const) {
DynaLog(" cmp eax, 0x%08X", Const);
*(x86BlockPtr.uwPtr++) = 0xF881;
*(x86BlockPtr.udwPtr++) = (DWORD)Const;
}
__inline void JeLabel8(char * Label, BYTE Value) {
DynaLog(" je $%s", Label);
*(x86BlockPtr.ubPtr++) = 0x74;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JneLabel8(char * Label, BYTE Value) {
DynaLog(" jne $%s", Label);
*(x86BlockPtr.ubPtr++) = 0x75;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JlLabel8(char * Label, BYTE Value) {
DynaLog(" jl $%s", Label);
*(x86BlockPtr.ubPtr++) = 0x7C;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JgeLabel8(char * Label, BYTE Value) {
DynaLog(" jge $%s", Label);
*(x86BlockPtr.ubPtr++) = 0x7D;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JleLabel8(char * Label, BYTE Value) {
DynaLog(" jle $%s", Label);
*(x86BlockPtr.ubPtr++) = 0x7E;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JgLabel8(char * Label, BYTE Value) {
DynaLog(" jg $%s", Label);
*(x86BlockPtr.ubPtr++) = 0x7F;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JmpLabel8(char * Label, BYTE Value) {
DynaLog(" jmp $%s", Label);
*(x86BlockPtr.ubPtr++) = 0xEB;
*(x86BlockPtr.ubPtr++) = Value;
}
__inline void JumpEax(void) {
DynaLog(" jmp eax");
*(x86BlockPtr.uwPtr++) = 0xe0ff;
}
__inline void JmpIndirectLabel32(char * Label,DWORD location) {
DynaLog(" jmp dword ptr [%s]", Label);
*(x86BlockPtr.uwPtr++) = 0x25ff;
*(x86BlockPtr.udwPtr++) = location;
}
__inline void Int3 () {
*(x86BlockPtr.ubPtr++) = 0xCC;
}