From 164d1069d002202edf0314c52ad049b0a32061e0 Mon Sep 17 00:00:00 2001 From: Cormac Shannon Date: Tue, 15 Apr 2025 23:29:57 +0100 Subject: [PATCH] Rework hexdump and Interpreter.dump methods --- src/interpreter/__interpreter.py | 49 ++++++++++++++++++++++++++++++-- src/interpreter/util/hexdump.py | 13 ++------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/interpreter/__interpreter.py b/src/interpreter/__interpreter.py index 2a42470..fde294d 100644 --- a/src/interpreter/__interpreter.py +++ b/src/interpreter/__interpreter.py @@ -3,6 +3,7 @@ from inspect import getmembers from typing import TypeAlias from traceback import print_exception from pprint import pprint +from colorama import Fore, Style, Back from .util import hexdump @@ -68,12 +69,54 @@ class Interpreter: print_exception(e) def dump(self): + colors = [ + Style.BRIGHT + Fore.RED, + Style.BRIGHT + Fore.GREEN, + Style.BRIGHT + Fore.YELLOW, + Style.BRIGHT + Fore.BLUE, + Style.BRIGHT + Fore.MAGENTA, + Style.BRIGHT + Fore.CYAN, + Style.BRIGHT + Back.RED, + Style.BRIGHT + Back.GREEN, + Style.BRIGHT + Back.YELLOW, + Style.BRIGHT + Back.BLUE, + Style.BRIGHT + Back.MAGENTA, + Style.BRIGHT + Back.CYAN, + ] + + _ = vars(self._registers) + reg_names, reg_addrs = _.keys(), _.values() + + print("Memory Registers") + for i, (name, color) in enumerate( + zip(reg_names, colors, strict=False), + start=1, + ): + print( + color, + name.ljust(20), + end=Style.RESET_ALL, + sep="\n" if i % 4 == 0 else "", + ) + print() + + print("\nMemory hexdump") hexdump( data=self._memory.dump(), - program_counter=self._registers.program_counter, - memory_pointer=self._registers.memory_pointer + addresses={ + address: color + for address, color in zip(reg_addrs, colors, strict=False) + }, + ) + + print("\nOpcodes") + pprint( + { + opcode: func + for opcode, func in enumerate(self.__operations) + if func is not None + } ) - pprint(self.__operations) def load_program(self, program: bytearray) -> None: self._memory[: len(program)] = program[:] diff --git a/src/interpreter/util/hexdump.py b/src/interpreter/util/hexdump.py index 60f9788..0f59d1a 100644 --- a/src/interpreter/util/hexdump.py +++ b/src/interpreter/util/hexdump.py @@ -1,4 +1,4 @@ -from colorama import Fore, Style, init +from colorama import Style, init # Initialize colorama (especially important on Windows) init() @@ -6,9 +6,8 @@ init() def hexdump( data: bytearray, + addresses: dict[int, str], width: int = 16, - program_counter: int = None, - memory_pointer: int = None, ): """ Print a hex+ASCII memory dump of the given bytearray. @@ -24,13 +23,7 @@ def hexdump( for j, byte in enumerate(chunk): addr = i + j - # Apply color based on PC or MP - if addr == program_counter: - style = Style.BRIGHT + Fore.RED - elif addr == memory_pointer: - style = Style.BRIGHT + Fore.BLUE - else: - style = "" + style = addresses.get(addr, "") reset = Style.RESET_ALL if style else "" # Hex and ASCII views