-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (53 loc) · 1.73 KB
/
main.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
from __future__ import annotations
import sys
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Callable
from circuits.utils.logger import Logger
from circuits.components.bus import Bus
from circuits.components.memory import RAM
from circuits.components.processor import CPU
from circuits.components.memory import Latch
def create_latch(charge: bool = False):
return Latch(charge)
with open(sys.argv[1], "r") as file:
lines = file.readlines()
image = [[create_latch(x == "1") for x in row.strip("\n")] for row in lines]
print(image)
LOGGER = Logger()
class Computer:
def __init__(
self,
*,
logger: Callable[[str, str], None],
):
self.clock = 1
self.__log = lambda *x, **y: logger(*x, origin=__name__, **y)
self.__data_lines = Bus()
self.__address_lines = Bus(5)
self.__primary_memory = RAM(
self.__data_lines,
self.__address_lines,
logger=LOGGER.get_logger("RAM"),
)
self.__primary_memory.set_memory_image(image)
hooks_fetch = (self.__primary_memory.decode_hook,)
self.__processing_unit = CPU(
self.__data_lines,
self.__address_lines,
clock=self.clock,
memory_module=self.__primary_memory,
logger=LOGGER.get_logger("CPU"),
hooks_fetch=hooks_fetch,
)
def run(self):
while True:
self.__processing_unit.cycle()
def main():
computer = Computer(logger=LOGGER.get_logger("COM"))
computer.run()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass