-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathemu.cpp
307 lines (272 loc) · 8.26 KB
/
emu.cpp
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
#include "m65816.hpp"
#include "bt.hpp"
static bool flow;
//------------------------------------------------------------------------
inline void doImmdValue(void)
{
doImmd(cmd.ea);
}
//----------------------------------------------------------------------
static void handle_operand(op_t &x, bool read_access)
{
ea_t ea;
dref_t dreftype;
switch ( x.type )
{
case o_void:
case o_reg:
break;
case o_imm:
QASSERT(557, read_access);
dreftype = dr_O;
MAKE_IMMD:
doImmdValue();
if ( isOff(uFlag, x.n) )
ua_add_off_drefs(x, dreftype);
break;
case o_displ:
dreftype = read_access ? dr_R : dr_W;
switch ( x.phrase )
{
case rD: // "dp"
case rDX: // "dp, X"
case rDY: // "dp, Y"
case riDX: // "(dp, X)"
case rDi: // "(dp,n)"
case rDiL: // "long(dp,n)"
case rDiY: // "(dp,n), Y"
case rDiLY: // "long(dp,n), Y"
{
sel_t dp = get_segreg(cmd.ea, rD);
if ( dp != BADSEL )
{
ea_t orig_ea = dp + x.addr;
ea = xlat(orig_ea);
goto MAKE_DREF;
}
else
{
goto MAKE_IMMD;
}
}
case rAbsi: // "(abs)"
case rAbsX: // "abs, X"
case rAbsY: // "abs, Y"
case rAbsiL: // "long(abs)"
ea = toEA(dataSeg_op(x.n), x.addr);
goto MAKE_DREF;
case rAbsXi: // "(abs,X)"
ea = toEA(codeSeg(cmd.ea, x.n), x.addr); // jmp, jsr
goto MAKE_DREF;
case rAbsLX: // "long abs, X"
ea = x.addr;
goto MAKE_DREF;
default:
goto MAKE_IMMD;
}
case o_mem:
case o_mem_far:
ea = calc_addr(x);
MAKE_DREF:
ua_dodata2(x.offb, ea, x.dtyp);
if ( !read_access )
doVar(ea);
ua_add_dref(x.offb, ea, read_access ? dr_R : dr_W);
break;
case o_near:
case o_far:
{
ea_t orig_ea;
ea = calc_addr(x, &orig_ea);
if ( cmd.itype == M65816_per )
{
ua_add_dref(x.offb, ea, dr_O);
}
else
{
bool iscall = InstrIsSet(cmd.itype, CF_CALL);
cref_t creftype = x.type == o_near
? iscall ? fl_CN : fl_JN
: iscall ? fl_CF : fl_JF;
ua_add_cref(x.offb, ea, creftype);
if ( flow && iscall )
flow = func_does_return(ea);
}
}
break;
default:
INTERR(558);
}
}
//----------------------------------------------------------------------
/**
* Get what is known of the status flags register,
* at address 'ea'.
*
* ea : The effective address.
*
* returns : A 9-bit value, composed with what is known of the
* status register at the 'ea' effective address. Its
* layout is the following:
* +----------------------------------------------------------------+
* | 0 | 0 | 0 | 0 | 0 | 0 | 0 | e || n | v | m | x | d | i | z | c |
* +----------------------------------------------------------------+
* 15 7 0
* Note that a 16-bit value is returned, in order to
* take the emulation-mode flag into consideration.
*/
static uint16 get_cpu_status(ea_t ea)
{
return (get_segreg(ea, rFe) << 8) | (get_segreg(ea, rFm) << 5) | (get_segreg(ea, rFx) << 4);
}
//----------------------------------------------------------------------
int idaapi emu(void)
{
uint32 Feature = cmd.get_canon_feature();
flow = ((Feature & CF_STOP) == 0);
if ( Feature & CF_USE1 ) handle_operand(cmd.Op1, 1);
if ( Feature & CF_USE2 ) handle_operand(cmd.Op2, 1);
if ( Feature & CF_CHG1 ) handle_operand(cmd.Op1, 0);
if ( Feature & CF_CHG2 ) handle_operand(cmd.Op2, 0);
if ( Feature & CF_JUMP )
QueueSet(Q_jumps, cmd.ea);
if ( flow )
ua_add_cref(0,cmd.ea+cmd.size,fl_F);
uint8 code = get_byte(cmd.ea);
const struct opcode_info_t &opinfo = get_opcode_info(code);
if ( opinfo.itype == M65816_jmp || opinfo.itype == M65816_jsr )
{
if ( opinfo.addr == ABS_INDIR
|| opinfo.addr == ABS_INDIR_LONG
|| opinfo.addr == ABS_IX_INDIR )
{
QueueSet(Q_jumps,cmd.ea);
}
}
#if 0
switch ( opinfo.addr )
{
case ABS_LONG_IX:
{
ea_t orig_ea = cmd.Op1.addr;
ea_t ea = xlat(orig_ea);
bool read_access;
if ( cmd.itype == M65816_sta )
read_access = false;
else
read_access = true;
if ( !read_access )
doVar(ea);
ua_add_dref(cmd.Op1.offb, ea, read_access ? dr_R : dr_W);
break;
}
case DP:
{
bool read_access;
if ( cmd.itype == M65816_tsb || cmd.itype == M65816_asl || cmd.itype == M65816_trb
|| cmd.itype == M65816_rol || cmd.itype == M65816_lsr || cmd.itype == M65816_ror
|| cmd.itype == M65816_dec || cmd.itype == M65816_inc )
read_access = false;
else
read_access = true;
int32 val = backtrack_value(cmd.ea, 2, BT_DP);
if ( val != -1 )
{
ea_t orig_ea = val + cmd.Op1.addr;
ea_t ea = xlat(orig_ea);
ua_dodata2(cmd.Op1.offb, ea, cmd.Op1.dtyp);
if ( !read_access )
doVar(ea);
ua_add_dref(cmd.Op1.offb, ea, read_access ? dr_R : dr_W);
}
}
break;
}
#endif
switch ( cmd.itype )
{
case M65816_sep:
case M65816_rep:
{
// Switching 8 -> 16 bits modes.
uint8 flag_data = get_byte(cmd.ea + 1);
uint8 m_flag = flag_data & 0x20;
uint8 x_flag = flag_data & 0x10;
uint8 val = (cmd.itype == M65816_rep) ? 0 : 1;
if ( m_flag )
split_srarea(cmd.ea + 2, rFm, val, SR_auto);
if ( x_flag )
split_srarea(cmd.ea + 2, rFx, val, SR_auto);
}
break;
case M65816_xce:
{
// Switching to native mode?
uint8 prev = get_byte(cmd.ea - 1);
const struct opcode_info_t &opinf = get_opcode_info(prev);
if ( opinf.itype == M65816_clc )
split_srarea(cmd.ea + 1, rFe, 0, SR_auto);
else if ( opinf.itype == M65816_sec )
split_srarea(cmd.ea + 1, rFe, 1, SR_auto);
}
break;
case M65816_jmp:
case M65816_jml:
case M65816_jsl:
case M65816_jsr:
{
if ( cmd.Op1.full_target_ea )
{
ea_t ftea = cmd.Op1.full_target_ea;
if ( cmd.itype != M65816_jsl && cmd.itype != M65816_jml )
ftea = toEA(codeSeg(ftea, 0), ftea);
else
ftea = xlat(ftea);
split_srarea(ftea, rFm, get_segreg(cmd.ea, rFm), SR_auto);
split_srarea(ftea, rFx, get_segreg(cmd.ea, rFx), SR_auto);
split_srarea(ftea, rFe, get_segreg(cmd.ea, rFe), SR_auto);
split_srarea(ftea, rPB, ftea >> 16, SR_auto);
split_srarea(ftea, rB, get_segreg(cmd.ea, rB), SR_auto);
split_srarea(ftea, rDs, get_segreg(cmd.ea, rDs), SR_auto);
split_srarea(ftea, rD, get_segreg(cmd.ea, rD), SR_auto);
}
}
break;
case M65816_plb:
{
int32 val = backtrack_value(cmd.ea, 1, BT_STACK);
if ( val != -1 )
{
split_srarea(cmd.ea + cmd.size, rB, val, SR_auto);
split_srarea(cmd.ea + cmd.size, rDs, val << 12, SR_auto);
}
}
break;
case M65816_pld:
{
int32 val = backtrack_value(cmd.ea, 2, BT_STACK);
if ( val != -1 )
split_srarea(cmd.ea + cmd.size, rD, val, SR_auto);
}
break;
case M65816_plp:
{
// Ideally, should pass another parameter, specifying when to stop
// backtracking.
// For example, in order to avoid this:
// PHP
// PLP <-- this one is causing interference
// (dunno if that even happens, though)
// PLP
ea_t ea = backtrack_prev_ins(cmd.ea, M65816_php);
if ( ea != BADADDR )
{
uint16 p = get_cpu_status(ea);
split_srarea(cmd.ea + cmd.size, rFm, (p >> 5) & 0x1, SR_auto);
split_srarea(cmd.ea + cmd.size, rFx, (p >> 4) & 0x1, SR_auto);
}
}
break;
}
return 1;
}