Reorganized emulator and added simple tty device

This commit is contained in:
Kyler 2024-02-23 22:51:33 -07:00
parent a7d026190e
commit 4abfc941bf
3 changed files with 42 additions and 4 deletions

9
emulator/__init__.py Normal file
View File

@ -0,0 +1,9 @@
# Kyler Olsen
# Feb 2024
from .emulator import Computer, Memory
__all__ = [
'Computer',
'Memory',
]

13
emulator/devices.py Normal file
View File

@ -0,0 +1,13 @@
# Kyler Olsen
# Feb 2024
from .emulator import Device
class tty(Device):
def __setitem__(self, index: int, value: int):
if index & 0xf == 0xe:
print(value)
elif index & 0xf == 0xf:
print(chr(value), end='')

View File

@ -9,9 +9,9 @@ class Device:
_start: int
_end: int
def __init__(self, start: int, end: int):
def __init__(self, start: int, end: int | None = None):
self._start = start
self._end = end
self._end = end or start
def __contains__(self, value: int) -> bool:
return self._start <= value <= self._end
@ -30,9 +30,13 @@ class Memory:
_ram: list[int]
def __init__(self, rom: list[int], devices: list[Device]) -> None:
def __init__(
self,
rom: list[int],
devices: list[Device] | None = None,
) -> None:
self._rom = [0] * 0x700
self._devices = devices[:]
self._devices = (devices or list())[:]
self._ram = [0] * 0x1000
for i, data in enumerate(rom):
@ -70,6 +74,18 @@ class Memory:
else:
raise IndexError
@staticmethod
def load_rom_file(filename: str) -> list[int]:
rom: list[int] = []
with open(filename, 'b') as file:
while file:
incoming = file.read(3)
rom.append(incoming[0] << 4 | ((incoming[1] & 0xf0) >> 4))
rom.append(((incoming[1] & 0xf) << 8) | incoming[2])
return rom
class Computer: