-
Notifications
You must be signed in to change notification settings - Fork 1
/
register.py
126 lines (97 loc) · 2.17 KB
/
register.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
DEBUG = False
LENGTH = 16
REGISTER_LENGTH = 2 * LENGTH
#todo
def add(r1, r2):
global DEBUG
global LENGTH
global REGISTER_LENGTH
if DEBUG:
print("add", "\t\t\t\t", r1, r2)
carry = 0
for i in range(REGISTER_LENGTH - 1, -1, -1):
val = int(r1[i] ^ r2[i] ^ carry)
carry = int((r1[i] and r2[i]) or (r1[i] and carry) or (r2[i] and carry))
r1[i] = val
return r1
def subtract(r1, r2):
global DEBUG
global LENGTH
global REGISTER_LENGTH
if DEBUG:
print("subtract", "\t\t\t\t",r1, r2)
temp = twos_complement(r2)
add(r1, temp)
return r1
def twos_complement(r):
global DEBUG
global LENGTH
global REGISTER_LENGTH
if DEBUG:
print("twos_complement", "\t\t\t\t", r)
temp = [0] * REGISTER_LENGTH
for i in range(REGISTER_LENGTH):
temp[i] = int(not r[i])
add(temp, [0] * (REGISTER_LENGTH - 1) + [1])
return temp
def shift_left(r, bit = 0):
global DEBUG
global LENGTH
global REGISTER_LENGTH
if DEBUG:
print("shift_left", "\t\t\t\t", r)
for i in range(REGISTER_LENGTH - 1):
r[i] = r[i + 1]
r[-1] = bit
def shift_right(r, bit = 0):
global DEBUG
global LENGTH
global REGISTER_LENGTH
if DEBUG:
print("shift_right", "\t\t\t\t", r)
for i in range(REGISTER_LENGTH - 1, -1, -1):
r[i] = r[i - 1]
r[0] = bit
def isNegative(r):
global DEBUG
global LENGTH
global REGISTER_LENGTH
return r[0] == 1
def store_in_register(x):
global DEBUG
global LENGTH
global REGISTER_LENGTH
r = [0] * REGISTER_LENGTH
if x < 0:
x *= -1
for i in range(REGISTER_LENGTH - 1, -1, -1):
r[REGISTER_LENGTH - i - 1] = x // (2**i)
x = x % (2**i)
r = twos_complement(r)
else:
for i in range(REGISTER_LENGTH - 1, -1, -1):
r[REGISTER_LENGTH - i - 1] = x // (2**i)
x = x % (2**i)
return r
def arithemetic_shift_right(A, Q, Q0):
global DEBUG
global LENGTH
global REGISTER_LENGTH
Q0 = Q[-1]
for i in range(REGISTER_LENGTH-1,0,-1):
Q[i] = Q[i-1]
Q[0] = A[-1]
for i in range(REGISTER_LENGTH-1,0,-1):
A[i] = A[i-1]
return Q0
def convert_decimal(R):
global DEBUG
global REGISTER_LENGTH
flag = 1
if isNegative(R):
R = twos_complement(R)[:]
flag = -1
ans = 0
for i in range(REGISTER_LENGTH):
ans += R[i] * (2 ** (REGISTER_LENGTH - i - 1))
return ans * flag