Rework hexdump and Interpreter.dump methods
This commit is contained in:
@@ -3,6 +3,7 @@ from inspect import getmembers
|
|||||||
from typing import TypeAlias
|
from typing import TypeAlias
|
||||||
from traceback import print_exception
|
from traceback import print_exception
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
from colorama import Fore, Style, Back
|
||||||
|
|
||||||
from .util import hexdump
|
from .util import hexdump
|
||||||
|
|
||||||
@@ -68,12 +69,54 @@ class Interpreter:
|
|||||||
print_exception(e)
|
print_exception(e)
|
||||||
|
|
||||||
def dump(self):
|
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(
|
hexdump(
|
||||||
data=self._memory.dump(),
|
data=self._memory.dump(),
|
||||||
program_counter=self._registers.program_counter,
|
addresses={
|
||||||
memory_pointer=self._registers.memory_pointer
|
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:
|
def load_program(self, program: bytearray) -> None:
|
||||||
self._memory[: len(program)] = program[:]
|
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)
|
# Initialize colorama (especially important on Windows)
|
||||||
init()
|
init()
|
||||||
@@ -6,9 +6,8 @@ init()
|
|||||||
|
|
||||||
def hexdump(
|
def hexdump(
|
||||||
data: bytearray,
|
data: bytearray,
|
||||||
|
addresses: dict[int, str],
|
||||||
width: int = 16,
|
width: int = 16,
|
||||||
program_counter: int = None,
|
|
||||||
memory_pointer: int = None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Print a hex+ASCII memory dump of the given bytearray.
|
Print a hex+ASCII memory dump of the given bytearray.
|
||||||
@@ -24,13 +23,7 @@ def hexdump(
|
|||||||
for j, byte in enumerate(chunk):
|
for j, byte in enumerate(chunk):
|
||||||
addr = i + j
|
addr = i + j
|
||||||
|
|
||||||
# Apply color based on PC or MP
|
style = addresses.get(addr, "")
|
||||||
if addr == program_counter:
|
|
||||||
style = Style.BRIGHT + Fore.RED
|
|
||||||
elif addr == memory_pointer:
|
|
||||||
style = Style.BRIGHT + Fore.BLUE
|
|
||||||
else:
|
|
||||||
style = ""
|
|
||||||
reset = Style.RESET_ALL if style else ""
|
reset = Style.RESET_ALL if style else ""
|
||||||
|
|
||||||
# Hex and ASCII views
|
# Hex and ASCII views
|
||||||
|
|||||||
Reference in New Issue
Block a user