-
Notifications
You must be signed in to change notification settings - Fork 0
/
eflags.cpp
138 lines (108 loc) · 3.67 KB
/
eflags.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
#include "Emulator.h"
template uint32_t Emulator::update_eflags_and(uint32_t v1, uint32_t v2);
template uint32_t Emulator::update_eflags_and(uint16_t v1, uint32_t v2);
template uint32_t Emulator::update_eflags_and(uint8_t v1, uint32_t v2);
template <class T> uint32_t Emulator::update_eflags_and(T v1, uint32_t v2){
T result;
uint8_t size;
v2 = (T)v2;
result = (uint64_t)v1 & v2;
size = sizeof(T)*8;
SetCarry(0);
SetParity(chk_parity(result & 0xff));
SetZero(!result);
SetSign(result >> (size -1) & 1);
SetOverflow(0);
return eflags.reg32;
}
template uint32_t Emulator::update_eflags_add(uint32_t v1, uint32_t v2);
template uint32_t Emulator::update_eflags_add(uint16_t v1, uint32_t v2);
template uint32_t Emulator::update_eflags_add(uint8_t v1, uint32_t v2);
template <class T> uint32_t Emulator::update_eflags_add(T v1, uint32_t v2){
bool s1, s2, sr;
uint64_t result;
uint8_t size;
v2 = (T)v2;
result = (uint64_t)v1 + v2;
size = sizeof(T)*8;
s1 = v1 >> (size-1);
s2 = v2 >> (size-1);
sr = (result >> (size-1)) & 1;
SetCarry(result >> size);
SetParity(chk_parity(result & 0xff));
SetZero(!result);
SetSign(sr);
SetOverflow(!(s1^s2) && s1^sr);
return eflags.reg32;
}
template uint32_t Emulator::update_eflags_sub(uint32_t v1, uint32_t v2);
template uint32_t Emulator::update_eflags_sub(uint16_t v1, uint32_t v2);
template uint32_t Emulator::update_eflags_sub(uint8_t v1, uint32_t v2);
template <class T> uint32_t Emulator::update_eflags_sub(T v1, uint32_t v2){
bool s1, s2, sr;
uint64_t result;
uint8_t size;
v2 = (T)v2;
result = (uint64_t)v1 - v2;
size = sizeof(T)*8;
s1 = v1 >> (size-1);
s2 = v2 >> (size-1);
sr = (result >> (size-1)) & 1;
SetCarry(result >> size);
SetParity(chk_parity(result & 0xff));
SetZero(result == 0);
SetSign(sr);
SetOverflow(s1^s2 && s1^sr);
return eflags.reg32;
}
template uint32_t Emulator::update_eflags_shl(uint32_t v, uint8_t c);
template uint32_t Emulator::update_eflags_shl(uint16_t v, uint8_t c);
template uint32_t Emulator::update_eflags_shl(uint8_t v, uint8_t c);
template <class T> uint32_t Emulator::update_eflags_shl(T v, uint8_t c){
T result;
uint8_t size;
result = v << c;
size = sizeof(T)*8;
SetCarry((v >> (size-c)) & 1);
SetParity(chk_parity(result & 0xff));
SetZero(!result);
SetSign((result >> (size-1)) & 1);
if(c==1)
SetOverflow(((v >> (size-1)) & 1) ^ ((v >> (size-2)) & 1));
return eflags.reg32;
}
template uint32_t Emulator::update_eflags_shr(uint32_t v, uint8_t c);
template uint32_t Emulator::update_eflags_shr(uint16_t v, uint8_t c);
template uint32_t Emulator::update_eflags_shr(uint8_t v, uint8_t c);
template <class T> uint32_t Emulator::update_eflags_shr(T v, uint8_t c){
T result;
uint8_t size;
result = v >> c;
size = sizeof(T)*8;
SetCarry((v >> (c-1)) & 1);
SetParity(chk_parity(result & 0xff));
SetZero(!result);
SetSign((result >> (size-1)) & 1);
if(c==1)
SetOverflow((v >> (size-1)) & 1);
return eflags.reg32;
}
template uint32_t Emulator::update_eflags_imul(int32_t v1, int32_t v2);
template uint32_t Emulator::update_eflags_imul(int16_t v1, int32_t v2);
template uint32_t Emulator::update_eflags_imul(int8_t v1, int32_t v2);
template <class T> uint32_t Emulator::update_eflags_imul(T v1, int32_t v2){
int64_t result;
uint8_t size;
v2 = (T)v2;
result = (int64_t)v1 * v2;
size = sizeof(T)*8;
SetCarry((result >> size) != -1);
SetOverflow((result >> size) != -1);
return eflags.reg32;
}
bool Emulator::chk_parity(uint8_t v){
bool p = true;
for(int i=0; i<8; i++)
p ^= (v>>i) & 1;
return p;
}