Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created June 5, 2026 17:03
Show Gist options
  • Select an option

  • Save mypy-play/c4001350eeccdd09664184aa6250785f to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/c4001350eeccdd09664184aa6250785f to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Literal
Address = int
Data = int
Operation = Literal["read", "write"]
class AddressBus:
def __init__(self) -> None:
self._address: Address = 0x0000
def read(self) -> Address:
return self._address
def write(self, address: Address) -> None:
self._address = address
class DataBus:
def __init__(self) -> None:
self._data: Data = 0x0000
def read(self) -> Data:
return self._data
def write(self, data: Data) -> None:
self._data = data
class ControlBus:
def __init__(self) -> None:
self._operation: Operation = "read"
def read(self) -> Operation:
return self._operation
def write(self, operation: Operation) -> None:
self._oepration = operation
class Memory:
def __init__(self) -> None:
self._memory: dict[Address, Data] = {}
def read(self, address: Address) -> Data:
return self._memory[address]
def write(self, address: Address, data: Data) -> None:
self._memory[address] = data
class Processor:
def __init__(self) -> None:
self.address_bus = AddressBus()
self.data_bus = DataBus()
self.control_bus = ControlBus()
def read(self, mem: Memory, address: Address) -> Data:
self.address_bus.write(address)
self.control_bus.write("read")
return mem.read(self.address_bus.read())
def test() -> None:
cpu = Processor()
mem = Memory()
cpu.read(mem, 0x0001)
cpu.write(mem, 0x0001, 0x0000)
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment