aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_emu
diff options
context:
space:
mode:
Diffstat (limited to 'atk16_emu')
-rw-r--r--atk16_emu/__init__.py1
-rw-r--r--atk16_emu/cli.py13
-rw-r--r--atk16_emu/emu.py16
-rw-r--r--atk16_emu/test/test_opcode_alr.py4
-rw-r--r--atk16_emu/test/test_opcode_hlt.py4
-rw-r--r--atk16_emu/test/utils.py25
6 files changed, 21 insertions, 42 deletions
diff --git a/atk16_emu/__init__.py b/atk16_emu/__init__.py
index e69de29..a314994 100644
--- a/atk16_emu/__init__.py
+++ b/atk16_emu/__init__.py
@@ -0,0 +1 @@
+from .emu import Machine \ No newline at end of file
diff --git a/atk16_emu/cli.py b/atk16_emu/cli.py
index 113eb87..7aa4b6b 100644
--- a/atk16_emu/cli.py
+++ b/atk16_emu/cli.py
@@ -51,15 +51,4 @@ machine.run_until_halted()
print("Machine halted.")
print("===============")
-for i in range(8):
- reg_name = f"r{chr(ord('a') + i)}"
- value = getattr(machine, reg_name).value
- print(f"{reg_name.upper()}: 0x{value:>04x} ({value})")
-
-print(f"PC: 0x{machine.pc.value:>04x} ({machine.pc.value})")
-print(f"FR: carry={machine.fr.carry}\n "
- f"overflow={machine.fr.overflow}\n "
- f"zero={machine.fr.zero}\n "
- f"sign={machine.fr.sign}")
-for i in range(8):
- print(f"RAM[{i}]: 0x{machine.ram.read(i):>04x} ({machine.ram.read(i)})")
+machine.print_state_summary()
diff --git a/atk16_emu/emu.py b/atk16_emu/emu.py
index 1596222..591eeb5 100644
--- a/atk16_emu/emu.py
+++ b/atk16_emu/emu.py
@@ -184,7 +184,7 @@ class Machine:
self.running = True
def run_until_halted(self):
- "Run the machine until HLT instruction is encountered"
+ "Run the self until HLT instruction is encountered"
self.running = True
while self.running:
self.step()
@@ -324,3 +324,17 @@ class Machine:
case 0b1111: return HLT()
raise ValueError(f"Invalid instruction: {instr:>016b} ({instr:>04x})")
+
+ def print_state_summary(self):
+ for i in range(8):
+ reg_name = f"r{chr(ord('a') + i)}"
+ value = getattr(self, reg_name).value
+ print(f"{reg_name.upper()}: 0x{value:>04x} ({value})")
+
+ print(f"PC: 0x{self.pc.value:>04x} ({self.pc.value})")
+ print(f"FR: carry={self.fr.carry}\n "
+ f"overflow={self.fr.overflow}\n "
+ f"zero={self.fr.zero}\n "
+ f"sign={self.fr.sign}")
+ for i in range(8):
+ print(f"RAM[{i}]: 0x{self.ram.read(i):>04x} ({self.ram.read(i)})") \ No newline at end of file
diff --git a/atk16_emu/test/test_opcode_alr.py b/atk16_emu/test/test_opcode_alr.py
index e7fe1a7..9a34845 100644
--- a/atk16_emu/test/test_opcode_alr.py
+++ b/atk16_emu/test/test_opcode_alr.py
@@ -1,5 +1,5 @@
-from ..emu import Machine
-from .utils import make_rom
+from atk16_emu import Machine
+from test.utils import make_rom
def test_alr_add_small_unsigned():
machine = Machine()
diff --git a/atk16_emu/test/test_opcode_hlt.py b/atk16_emu/test/test_opcode_hlt.py
index 6991042..4d3d5c8 100644
--- a/atk16_emu/test/test_opcode_hlt.py
+++ b/atk16_emu/test/test_opcode_hlt.py
@@ -1,5 +1,5 @@
-from ..emu import Machine
-from .utils import make_rom
+from atk16_emu import Machine
+from test.utils import make_rom
def test_hlt():
machine = Machine()
diff --git a/atk16_emu/test/utils.py b/atk16_emu/test/utils.py
deleted file mode 100644
index 0d40d54..0000000
--- a/atk16_emu/test/utils.py
+++ /dev/null
@@ -1,25 +0,0 @@
-def pad_bytearray(to_length: int, ba: bytearray) -> bytearray:
- """
- Pads a bytearray with zeros until it reaches a specified length.
-
- :param to_length: Target length in bytes.
- :param ba: The bytearray to be padded.
- :return: The padded bytearray.
- """
- padding_size = to_length - len(ba)
- if padding_size > 0:
- ba.extend(b'\x00' * padding_size)
- return ba
-
-def make_rom(words: list[int]) -> bytearray:
- """
- Constructs a ROM image from a list of 16-bit words.
-
- :param words: The list of 16-bit words.
- :return: The ROM image as a bytearray.
- """
- bytes = []
- for word in words:
- bytes.append((word >> 8) & 0xFF)
- bytes.append(word & 0xFF)
- return pad_bytearray(64 * 1024, bytearray(bytes)) \ No newline at end of file