-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomputer.py
104 lines (82 loc) · 2.72 KB
/
computer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__appname__ = ""
__author__ = "Marco Sirabella"
__copyright__ = ""
__credits__ = ["Marco Sirabella"] # Authors and bug reporters
__license__ = "GPL"
__version__ = "1.0"
__maintainers__ = "Marco Sirabella"
__email__ = "[email protected]"
__status__ = "Prototype" # "Prototype", "Development" or "Production"
__module__ = ""
class Word:
def __init__(self, arch, bits=0b0000):
self.arch = arch
self._bits = bits # Our full data as an integer
@property
def bits(self):
return self._bits
@bits.setter
def bits(self, bits):
assert bits < self.arch ** 4
self._bits = bits
@property
def inst(self):#or instruction?
"""Return first half(instruction) of data as an integer"""
return int(format(self.bits, '0{}b'.format(self.arch * 2))[:self.arch], 2)
@property
def data(self):
"""Return second half(data) of data as an integer"""
return int(format(self.bits, '0{}b'.format(self.arch * 2))[self.arch:], 2)
class RAM:
"""A device containing many words of data(architecture squared)"""
def __init__(self, arch):
self.arch = arch
self.data = [Word(self.arch) for i in range(self.arch ** 2 // 2)] # Should i half it so i can reference registers
def reboot(self):
"""Wipe all values in ram and reinitialize"""
self.__init__(self.arch)
class CPU:
def __init__(self, arch):
self.ram = RAM(arch)
self.arch = arch
self.registers = [Word(self.arch) for i in range(self.arch // 2)] # Have half architecture as number of registers
self.counter = self.registers[-1] # Use last register as counter
self.iReg = self.registers[-2] # Second to last register as instructional register
self.iSet = (
self.nop,
self.load,
self.store,
self.add,
self.sub,
self.mul,
self.div,
self.nop, # Cuz 4-bit calc skips this for SOME reason
self.print,
self.input,
)
def fetch(self):
self.iReg.bits = self.ram.data[self.counter.bits].bits
def exec(self):
self.iSet[self.iReg.inst](self.iReg.data)
self.counter
# And now, the instruction sets
def nop(self, data):
return 0
def load(self, data):
pass
def store(self, data):
pass
def add(self, data):
pass
def sub(self, data):
pass
def mul(self, data):
pass
def div(self, data):
pass
def print(self, data):
print('yo')
def input(self, data):
pass