-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.py
130 lines (110 loc) · 3.63 KB
/
processor.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
# MOHAMED NASRAOUI
# Global variable storing register indices in reverse order
global RegIndex
RegIndex = [3, 2, 1, 0]
# 1 Bit Register
class Bit():
def __init__(self):
self.data = 0
# Method to store values into the register
def STORE(self, Value):
self.data = Value
# Method to clear the register
def CLEAR(self):
self.data = 0
# Register class representing a generic register
class Register():
def __init__(self):
self.data = [0]*4
# Method to store values into the register
def STORE(self, Value):
for i in RegIndex:
self.data[i] = Value[i]
# Method to clear the register
def CLEAR(self):
for i in RegIndex:
self.data[i] = 0
# Arithmetic Logic Unit class representing the ALU component
class ALU():
def __init__(self):
self.carry = 0
self.result = [0]*4
self.ZEROFLAG = 0
# Method to perform addition operation
def ADD(self, a, b):
for i in RegIndex:
SumBit = a[i] + b[i] + self.carry
self.result[i] = SumBit%2
self.carry = SumBit//2
# Method to perform subtraction operation
def SUBTRACT(self, a, b):
for i in RegIndex:
SumBit = a[i] - b[i] + self.carry
self.result[i] = SumBit%2
self.carry = SumBit//2
# Method to perform comparison
def COMPARE(self, a, b):
self.SUBTRACT(a, b)
if self.result != [0]*4:
self.ZEROFLAG = 1
# Method to clear the ALU
def CLEAR(self):
for i in RegIndex:
self.result[i] = 0
self.carry = 0
self.ZEROFLAG = 0
# Processor class representing the main processor
class Processor():
def __init__(self):
self.AX = Register()
self.BX = Register()
self.CX = Register()
self.DX = Register()
self.carry = Bit()
self.ZEROFLAG = Bit()
self.ALU = ALU()
# Method to clear all registers and ALU
def CLEAR_ALL(self):
self.AX.CLEAR()
self.BX.CLEAR()
self.CX.CLEAR()
self.DX.CLEAR()
self.carry.CLEAR()
self.ZEROFLAG.CLEAR()
self.ALU.CLEAR()
# Method to perform addition operation
def P_ADD(self):
self.ALU.ADD(self.AX.data, self.BX.data)
self.CX.STORE(self.ALU.result)
self.carry.data = self.ALU.carry
# Method to perform subtraction operation
def P_SUBTRACT(self):
self.ALU.SUBTRACT(self.AX.data, self.BX.data)
self.CX.STORE(self.ALU.result)
self.carry.data = self.ALU.carry
# Method to perform comparison
def P_CMP(self):
self.ALU.COMPARE(self.AX.data, self.BX.data)
self.CX.STORE(self.ALU.result)
self.carry.data = self.ALU.carry
self.ZEROFLAG.data = self.ALU.ZEROFLAG
# Method to store values into a register
def P_STORE(self, Value, Reg):
for i in RegIndex:
Reg.data[i] = Value[i]
# Method to get values from a register
def P_GET(self, Reg):
return Reg.data
# Method representing a clock cycle
def CLOCK_CYCLE(self):
if self.DX.data == [0]*4 :
self.CLEAR_ALL()
elif self.DX.data == [0, 0, 0, 1] :
self.P_ADD()
elif self.DX.data == [0, 0, 1, 0] :
self.P_SUBTRACT()
elif self.DX.data == [0, 0, 1, 1] :
self.P_CMP()
# Method to get data from all registers
def GET_ALL(self):
return self.AX.data, self.BX.data, self.CX.data, self.DX.data, self.carry.data, self.ZEROFLAG.data