-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyFlags.py
176 lines (153 loc) · 6.82 KB
/
PyFlags.py
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
#!/usr/bin/env python
########################################################################
#
# PyEmu: scriptable x86 emulator
#
# Cody Pierce - [email protected] - 2007
#
# License: None
#
########################################################################
'''
This is a new class for setting flags. It is used by PyCPU to set
flags per instruction. This was shamelessly ripped from Bochs
http://bochs.sourceforge.net/
'''
class PyFlags:
parity_lookup_table = [1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1]
def __init__(self, mnemonic, op1, op2, result, size):
assert isinstance(mnemonic, str)
assert isinstance(op1, long) or isinstance(op1, int)
assert isinstance(op2, long) or isinstance(op2, int)
assert isinstance(result, long) or isinstance(result, int)
assert isinstance(size, long) or isinstance(size, int)
self.mnemonic = mnemonic
self.op1 = op1
self.op2 = op2
self.result = result
self.size = size
self.bit_count = self.size * 8
self.mask = (2 ** (self.bit_count) - 1)
self.sign_mask = (self.mask + 1) / 2
def get_CF(self):
if self.mnemonic == "ADD":
cf = int(self.result < self.op1)
elif self.mnemonic == "ADC":
# used only if CF = 1
cf = int(self.result <= self.op1)
elif self.mnemonic in ["SUB", "CMP"]:
cf = int(self.op1 < self.op2)
elif self.mnemonic == "SBB":
# used only if CF = 1
cf = int((self.op1 < self.result) or (self.op2 == self.mask))
elif self.mnemonic == "NEG":
cf = int(self.result != 0)
elif self.mnemonic == "LOGIC":
cf = 0
elif self.mnemonic == "SAR":
if self.op2 < self.bit_count:
cf = int((self.op1 >> (self.op2 - 1)) & 0x01)
else:
cf = int((self.op1 & self.sign_mask) > 0)
elif self.mnemonic in ["SHR", "SHRD"]:
cf = int((self.op1 >> (self.op2 - 1)) & 0x01)
elif self.mnemonic in ["SHL", "SAL"]:
if self.op2 <= self.bit_count:
cf = int((self.op1 >> (self.bit_count - self.op2)) & 0x01)
else:
cf = 0
elif self.mnemonic == "IMUL":
cf = int(not ((self.op1 < self.sign_mask and self.op2 == 0) or ((self.op1 & self.sign_mask) and self.op2 == self.mask)))
elif self.mnemonic == "MUL":
cf = int(self.op2 != 0)
else:
cf = None
return cf
def get_AF(self):
if self.mnemonic in ["ADD", "ADC", "SUB", "CMP", "SBB"]:
af = int(((self.op1 ^ self.op2) ^ self.result) & 0x10)
elif self.mnemonic == "NEG":
af = int((self.result & 0x0f) != 0)
elif self.mnemonic == "INC":
af = int((self.result & 0x0f) == 0)
elif self.mnemonic == "DEC":
af = int((self.result & 0x0f) == 0x0f)
else:
af = None
return af
def get_ZF(self):
if self.mnemonic in ["LOGIC", "ADD", "ADC", "SUB", "CMP", "SBB", "NEG", "SAR", "SHR", "SHL", "SAR", "INC", "DEC"]:
zf = int(self.result == 0)
elif self.mnemonic in ["IMUL", "MUL"]:
zf = int(self.op1 == 0)
else:
zf = None
return zf
def get_SF(self):
if self.mnemonic in ["LOGIC", "ADD", "ADC", "SUB", "CMP", "SBB", "NEG", "SAR", "SHR", "SHL", "SAL", "INC", "DEC"]:
sf = int(self.result >= self.sign_mask)
elif self.mnemonic in ["IMUL", "MUL"]:
sf = int(self.op1 >= self.sign_mask)
else:
sf = None
return sf
def get_OF_ADD(self):
return int(((~((self.op1) ^ (self.op2)) & ((self.op2) ^ (self.result))) & (self.sign_mask)) != 0)
def get_OF_SUB(self):
return int(((((self.op1) ^ (self.op2)) & ((self.op1) ^ (self.result))) & (self.sign_mask)) != 0)
def get_OF(self):
if self.mnemonic in ["ADD", "ADC"]:
of = self.get_OF_ADD()
elif self.mnemonic in ["SUB", "CMP", "SBB"]:
of = self.get_OF_SUB()
elif self.mnemonic == "NEG":
of = int(self.result == self.sign_mask)
elif self.mnemonic in ["LOGIC", "SAR"]:
of = 0
elif self.mnemonic == "SHR":
if self.op2 == 1:
of = int(self.op1 >= self.sign_mask)
else:
of = 0
elif self.mnemonic == "SHRD":
of = int((((self.result << 1) ^ self.result) & self.sign_mask) > 0)
elif self.mnemonic in ["SHL", "SAL"]:
if self.op2 == 1:
of = int(((self.op1 ^ self.result) & self.sign_mask) > 0)
else:
of = int((((self.op1 << (self.op2 - 1)) ^ self.result) & self.sign_mask) > 0)
elif self.mnemonic == "IMUL":
of = int(not ((self.op1 < self.sign_mask and self.op2 == 0) or ((self.op1 & self.sign_mask) and self.op2 == self.mask)))
elif self.mnemonic == "MUL":
of = int(self.op2 != 0)
elif self.mnemonic == "INC":
of = int(self.result == self.sign_mask)
elif self.mnemonic == "DEC":
of = int(self.result == self.sign_mask - 1)
else:
of = None
return of
def get_PF(self):
if self.mnemonic in ["LOGIC", "ADD", "ADC", "SUB", "CMP", "SBB", "NEG", "SAR", "SHR", "SHL", "SAR", "INC", "DEC"]:
pf = self.parity_lookup_table[self.result & 0xff]
elif self.mnemonic in ["IMUL", "MUL"]:
pf = self.parity_lookup_table[self.op1 & 0xff]
else:
pf = None
return pf
# End PyFlags