rivet/structure.py

39 lines
1.3 KiB
Python
Raw Permalink Normal View History

2023-11-24 13:34:44 +00:00
from __future__ import annotations
2024-03-24 10:33:34 +00:00
2023-11-24 13:34:44 +00:00
from range import Range
2024-03-24 10:33:34 +00:00
2023-11-24 13:34:44 +00:00
class Structure:
def __init__(self,
name: str,
bits: int,
2024-04-12 21:33:09 +00:00
ranges: dict[tuple[int, int], Range],
2023-11-24 13:34:44 +00:00
start: int = 0) -> None:
2024-04-12 21:33:09 +00:00
self.name: str = name
self.bits: int = bits
self.ranges: dict[tuple[int, int], Range] = ranges
self.start: int = start
@staticmethod
2023-11-24 13:34:44 +00:00
def load(id_: str, data: dict) -> Structure:
ranges = {}
for rSpan, rData in data["ranges"].items():
rStart, rEnd = Range.parseSpan(str(rSpan))
ranges[(rStart, rEnd)] = Range.load(rStart, rEnd, rData)
for range_ in ranges.values():
if range_.values is not None and range_.dependsOn is not None:
bits = ranges[range_.dependsOn].bits
values = {}
for v, d in range_.values.items():
v = str(int(v)).zfill(bits)
values[v] = d
range_.values = values
2023-11-24 17:14:13 +00:00
return Structure(id_, int(data["bits"]), ranges, data.get("start", 0))
2023-11-24 13:34:44 +00:00
def getSortedRanges(self) -> list[Range]:
ranges = self.ranges.values()
2024-04-12 21:33:09 +00:00
return list(sorted(ranges, key=lambda r: r.end))