-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
114 lines (95 loc) · 2.25 KB
/
utils.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
def signed(unsigned):
return unsigned-256 if unsigned > 127 else unsigned
def unsigned(signed):
return signed+256 if signed < 0 else signed
def splitHex(hex):
return (hex >> 8), (hex & 0xFF)
def joinHex(high, low):
return (high << 8) | low
def BIT(n, b, F):
F[7] = int((n & (1 << b)) == 0)
F[5:7] = [1, 0]
return F
def SLA(n, F):
F[4] = int((n & (1 << 7)) != 0)
n = (n << 1) & 0xff
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F
def SRA(n, F):
F[4] = int((n & 1) != 0)
n = (n&(1<<7))|(n >> 1)
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F
def SWAP(n, F):
n = ((n & 0x0f) << 4) | ((n & 0xf0) >> 4)
F[7] = int(n == 0)
F[4:7] = [0, 0, 0]
return n, F
def SRL(n, F):
F[4] = int((n & 1) != 0)
n = (n >> 1) & 0x7f
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F
def RES(n, b):
n &= ~(1 << b)
return n
def SET(n, b):
n |= (1 << b)
return n
def ADDAR8(a, b, F):
tmp= a + b
F[7] = int((tmp & 0xFF) == 0)
F[6] = 0
F[5] = int((((a&0xF)+(b&0xF))&0x10)==0x10)
F[4] = int(tmp>0xFF)
return tmp & 0xFF, F
def ADCAR8(a, b, F):
tmp = a + b + F[4]
F[7] = int((tmp & 0xFF) == 0)
F[6] = 0
F[5] = int((((a&0xF)+(b&0xF)+F[4])&0x10)==0x10)
F[4] = int(tmp>0xFF)
return tmp & 0xFF, F
def SUBAR8(a, b, F):
tmp = a - b
F[7] = int((tmp & 0xFF) == 0)
F[6] = 1
F[5] = int((((a&0xF)-(b&0xF))&0x10)==0x10)
F[4] = int(tmp<0)
return tmp & 0xFF, F
def SBCAR8(a, b, F):
tmp = a - b - F[4]
F[7] = int((tmp & 0xFF) == 0)
F[6] = 1
F[5] = int((((a&0xF)-(b&0xF)-F[4])&0x10)==0x10)
F[4] = int(tmp<0)
return tmp & 0xFF, F
def RLC(n, F):
F[4] = int((n & (1 << 7)) == (1 << 7))
n = ((n << 1) & 0xff) | F[4]
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F
def RRC(n, F):
F[4] = int((n & 1) == 1)
n = (n >> 1) | (F[4] << 7)
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F
def RL(n, F):
tmp = int((n & (1 << 7)) == (1 << 7))
n = ((n << 1) & 0xff) | F[4]
F[4] = tmp
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F
def RR(n, F):
tmp = int((n & 1) == 1)
n = (n >> 1) | (F[4] << 7)
F[4] = tmp
F[7] = int(n == 0)
F[5:7] = [0, 0]
return n, F