-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpreter_branch.h
337 lines (274 loc) · 9.24 KB
/
interpreter_branch.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright 2019-2020 Hydr8gon
This file is part of NooDS.
NooDS is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NooDS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef INTERPRETER_BRANCH
#define INTERPRETER_BRANCH
#include "core.h"
FORCE_INLINE void Interpreter::bx(uint32_t opcode) // BX Rn
{
// Decode the operand
uint32_t op0 = *registers[opcode & 0x0000000F];
// Branch to address and switch to THUMB if bit 0 is set
if (op0 & BIT(0))
{
cpsr |= BIT(5);
*registers[15] = (op0 & ~1) + 2;
}
else
{
*registers[15] = (op0 & ~3) + 4;
}
}
FORCE_INLINE void Interpreter::blxReg(uint32_t opcode) // BLX Rn
{
if (cpu == 1) return; // ARM9 exclusive
// Decode the operand
uint32_t op0 = *registers[opcode & 0x0000000F];
// Branch to address with link and switch to THUMB if bit 0 is set
*registers[14] = *registers[15] - 4;
if (op0 & BIT(0))
{
cpsr |= BIT(5);
*registers[15] = (op0 & ~1) + 2;
}
else
{
*registers[15] = (op0 & ~3) + 4;
}
}
FORCE_INLINE void Interpreter::b(uint32_t opcode) // B label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(23)) ? 0xFC000000 : 0) | ((opcode & 0x00FFFFFF) << 2);
// Branch to offset
*registers[15] += op0 + 4;
}
FORCE_INLINE void Interpreter::bl(uint32_t opcode) // BL label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(23)) ? 0xFC000000 : 0) | ((opcode & 0x00FFFFFF) << 2);
// Branch to offset with link
*registers[14] = *registers[15] - 4;
*registers[15] += op0 + 4;
}
FORCE_INLINE void Interpreter::blx(uint32_t opcode) // BLX label
{
if (cpu == 1) return; // ARM9 exclusive
// Decode the operand
uint32_t op0 = ((opcode & BIT(23)) ? 0xFC000000 : 0) | ((opcode & 0x00FFFFFF) << 2) | ((opcode & BIT(24)) >> 23);
// Branch to offset with link and switch to THUMB
*registers[14] = *registers[15] - 4;
*registers[15] += op0 + 2;
cpsr |= BIT(5);
}
FORCE_INLINE void Interpreter::swi() // SWI #i
{
// Software interrupt
uint32_t cpsrOld = cpsr;
setMode(0x13); // Supervisor
*spsr = cpsrOld;
cpsr |= BIT(7);
*registers[14] = *registers[15] - 4;
*registers[15] = ((cpu == 0) ? core->cp15.getExceptionAddr() : 0x00000000) + 0x08 + 4;
}
FORCE_INLINE void Interpreter::bxRegT(uint16_t opcode) // BX Rs
{
// Decode the operand
uint32_t op0 = *registers[(opcode & 0x0078) >> 3];
// Branch to address and switch to ARM mode if bit 0 is cleared
if (op0 & BIT(0))
{
*registers[15] = (op0 & ~1) + 2;
}
else
{
cpsr &= ~BIT(5);
*registers[15] = (op0 & ~3) + 4;
}
}
FORCE_INLINE void Interpreter::blxRegT(uint16_t opcode) // BLX Rs
{
if (cpu == 1) return; // ARM9 exclusive
// Decode the operand
uint32_t op0 = *registers[(opcode & 0x0078) >> 3];
// Branch to address with link and switch to ARM mode if bit 0 is cleared
*registers[14] = *registers[15] - 1;
if (op0 & BIT(0))
{
*registers[15] = (op0 & ~1) + 2;
}
else
{
cpsr &= ~BIT(5);
*registers[15] = (op0 & ~3) + 4;
}
}
FORCE_INLINE void Interpreter::beqT(uint16_t opcode) // BEQ label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if equal
if (cpsr & BIT(30))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bneT(uint16_t opcode) // BNE label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if not equal
if (!(cpsr & BIT(30)))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bcsT(uint16_t opcode) // BCS label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if carry set
if (cpsr & BIT(29))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bccT(uint16_t opcode) // BCC label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if carry clear
if (!(cpsr & BIT(29)))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bmiT(uint16_t opcode) // BMI label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if negative
if (cpsr & BIT(31))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bplT(uint16_t opcode) // BPL label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if positive
if (!(cpsr & BIT(31)))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bvsT(uint16_t opcode) // BVS label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if overflow set
if (cpsr & BIT(28))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bvcT(uint16_t opcode) // BVC label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if overflow clear
if (!(cpsr & BIT(28)))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bhiT(uint16_t opcode) // BHI label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if higher
if ((cpsr & BIT(29)) && !(cpsr & BIT(30)))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::blsT(uint16_t opcode) // BLS label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if lower or same
if (!(cpsr & BIT(29)) || (cpsr & BIT(30)))
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bgeT(uint16_t opcode) // BGE label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if signed greater or equal
if ((cpsr & BIT(31)) == (cpsr & BIT(28)) << 3)
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bltT(uint16_t opcode) // BLT label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if signed less than
if ((cpsr & BIT(31)) != (cpsr & BIT(28)) << 3)
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bgtT(uint16_t opcode) // BGT label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if signed greater than
if (!(cpsr & BIT(30)) && (cpsr & BIT(31)) == (cpsr & BIT(28)) << 3)
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bleT(uint16_t opcode) // BLE label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(7)) ? 0xFFFFFE00 : 0) | ((opcode & 0x00FF) << 1);
// Branch to offset if signed less or equal
if ((cpsr & BIT(30)) || (cpsr & BIT(31)) != (cpsr & BIT(28)) << 3)
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::bT(uint16_t opcode) // B label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(10)) ? 0xFFFFF000 : 0) | ((opcode & 0x07FF) << 1);
// Branch to offset
*registers[15] += op0 + 2;
}
FORCE_INLINE void Interpreter::blSetupT(uint16_t opcode) // BL/BLX label
{
// Decode the operand
uint32_t op0 = ((opcode & BIT(10)) ? 0xFFFFF000 : 0) | ((opcode & 0x07FF) << 1);
// Set the upper 11 bits of the target address for a long BL/BLX
*registers[14] = *registers[15] + (op0 << 11);
}
FORCE_INLINE void Interpreter::blOffT(uint16_t opcode) // BL label
{
// Decode the operand
uint32_t op0 = (opcode & 0x07FF) << 1;
// Long branch to offset with link
uint32_t ret = *registers[15] - 1;
*registers[15] = ((*registers[14] + op0) & ~1) + 2;
*registers[14] = ret;
}
FORCE_INLINE void Interpreter::blxOffT(uint16_t opcode) // BLX label
{
if (cpu == 1) return; // ARM9 exclusive
// Decode the operand
uint32_t op0 = (opcode & 0x07FF) << 1;
// Long branch to offset with link and switch to ARM mode
cpsr &= ~BIT(5);
uint32_t ret = *registers[15] - 1;
*registers[15] = ((*registers[14] + op0) & ~3) + 4;
*registers[14] = ret;
}
FORCE_INLINE void Interpreter::swiT() // SWI #i
{
// Software interrupt
uint32_t cpsrOld = cpsr;
setMode(0x13); // Supervisor
*spsr = cpsrOld;
cpsr &= ~BIT(5);
cpsr |= BIT(7);
*registers[14] = *registers[15] - 2;
*registers[15] = ((cpu == 0) ? core->cp15.getExceptionAddr() : 0x00000000) + 0x08 + 4;
}
#endif // INTERPRETER_BRANCH