Rework hexdump and Interpreter.dump methods
This commit is contained in:
@@ -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[:]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user