srne-mqtt/draw_memory_map.py
2021-11-02 23:43:16 +01:00

65 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
from typing import Iterable
from table_drawing import table
def memory_table(data: Iterable[int], start: int = 0, wordsize: int = 2):
data_iter = iter(data)
data_rows = []
position = start
try:
while True:
try:
row_id = position
row = []
for _ in range(16):
try:
cell = []
for _ in range(wordsize):
d = data_iter.__next__() & 0xFF
cell.append(d)
position += 1
finally:
if cell:
row.append(cell)
finally:
if row:
row_hex = [" ".join([f"{n:02X}" for n in cell]) for cell in row]
row_ascii = [
" ".join(
[
" " + chr(n) if chr(n).isprintable() else " "
for n in cell
]
)
for cell in row
]
row_head = f"{row_id:04X}"[:3] + "·"
data_rows.append([row_head] + row_hex)
if " ".join(row_ascii).strip():
data_rows.append([""] + row_ascii)
except StopIteration:
pass
headers = [""] + [
("" if wordsize % 2 else " ") + "···{:01X}".format(x) for x in range(16)
]
return table(
data_rows,
headers=headers,
justify="^",
header_interval=8 * 2,
)
data = list(range(256))
print(memory_table(data, wordsize=1))
#
# rows = [[f"{x:03X}·"] for x in range(32)]
#