-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.py
81 lines (63 loc) · 1.99 KB
/
flags.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
class Flags(object):
def __init__(self):
self._flags = 0
self.FLAG_SIGN = 1 << 7
self.FLAG_ZERO = 1 << 6
self.FLAG_ACARRY = 1 << 4
self.FLAG_PARITY = 1 << 2
self.FLAG_CARRY = 1 << 0
self._flags |= 1 << 1
def set_sign(self, on=True):
if on:
self._flags |= self.FLAG_SIGN
else:
self._flags &= ~self.FLAG_SIGN
def set_zero(self, value):
zero = value == 0
if zero:
self._flags |= self.FLAG_ZERO
else:
self._flags &= ~self.FLAG_ZERO
def set_acarry(self, original, intermediate):
carry = ((1 ^ intermediate) ^ original) & 0x10
if carry:
self._flags |= self.FLAG_ACARRY
else:
self._flags &= ~self.FLAG_ACARRY
def set_parity(self, value):
#parity = not sum([value & (1<<i) > 0 for i in range(8)]) % 2
parity = value % 2
if parity:
self._flags &= ~self.FLAG_PARITY
else:
self._flags |= self.FLAG_PARITY
def set_carry(self, res, is_word=False):
if is_word:
carry = res > 0xffff
else:
carry = res > 0xff
if carry:
self._flags |= self.FLAG_CARRY
else:
self._flags &= ~self.FLAG_CARRY
def set_carry_raw(self, value):
if value:
self._flags |= self.FLAG_CARRY
else:
self._flags &= ~self.FLAG_CARRY
def get_sign(self):
return bool(self._flags & self.FLAG_SIGN)
def get_zero(self):
return bool(self._flags & self.FLAG_ZERO)
def get_acarry(self):
return bool(self._flags & self.FLAG_ACARRY)
def get_parity(self):
return bool(self._flags & self.FLAG_PARITY)
def get_carry(self):
return bool(self._flags & self.FLAG_CARRY)
def set_flag(self, value):
self._flags = value
def flags(self):
return self._flags
def __repr__(self):
return bin(self._flags)