Download chips/openroad_flow_scripts/flow/scripts/memories/schema.py from STAR-future/Trinity-SciData: direct link, hf CLI and curl.
- Browser
- Download file 10.2 kB
-
https://huggingface.co/datasets/STAR-future/Trinity-SciData/resolve/main/chips/openroad_flow_scripts/flow/scripts/memories/schema.py
- Command line
-
hf download hf://datasets/STAR-future/Trinity-SciData/chips/openroad_flow_scripts/flow/scripts/memories/schema.py
-
curl -L -o schema.py https://huggingface.co/datasets/STAR-future/Trinity-SciData/resolve/main/chips/openroad_flow_scripts/flow/scripts/memories/schema.py
10.2 kB
| #!/usr/bin/env python3 | |
| """The `.memories` file format: a JSON inventory of memory macros. | |
| One `.memories` file (and the flow-generated `memories.json`) describes | |
| memories at the module boundary: geometry, the full pin list with each | |
| pin's function, the behavioral model the RTL provides, and whether the | |
| memory is converted to a macro (`idiomatic`). | |
| The file-based handoff is deliberate: everything downstream — synthesis | |
| blackboxing, liberty/LEF consumption in later stages, build systems that | |
| declare flow artifacts as transitive dependencies — keys off these files | |
| rather than off in-process state. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| from dataclasses import asdict, dataclass, field | |
| from pathlib import Path | |
| SCHEMA_VERSION = 1 | |
| PIN_FUNCTIONS = ("addr", "en", "wmode", "mask", "data_in", "data_out", "clk") | |
| DIRECTIONS = ("input", "output") | |
| class SchemaError(ValueError): | |
| pass | |
| class Pin: | |
| name: str | |
| direction: str # "input" / "output" | |
| width: int | |
| port_id: str # "R0", "W1", "RW0", ... | |
| function: str # one of PIN_FUNCTIONS | |
| class Memory: | |
| name: str | |
| rows: int = 0 | |
| bits: int = 0 | |
| addr_w: int = 0 | |
| read_ports: int = 0 | |
| write_ports: int = 0 | |
| rw_ports: int = 0 | |
| # Total width of the per-port write mask, 0 if unmasked. Subword-split | |
| # mask pins (`W0_mask_0..3`) contribute one lane each. | |
| mask_lanes: int = 0 | |
| # Bits per mask lane, 0 for the even split (bits / mask_lanes). A | |
| # ragged last lane needs it: tc_sram's byte enables over a 25-bit | |
| # word are 8, 8, 8 and 1 bit wide (mask_lanes 4, mask_lane_width 8). | |
| mask_lane_width: int = 0 | |
| # Read ports yosys inferred without a clock ($mem_v2 RD_CLK_ENABLE=0). | |
| # An SRAM macro reads on a clock edge, so one of these rules the memory | |
| # out of conversion; see idiomatic.py. | |
| comb_read_ports: int = 0 | |
| # Where the pin list came from. "firtool" when every port of the | |
| # behavioral module follows firtool's <R|W|RW><n>_<function> naming and | |
| # the pins are the module's own ports; "" when they were synthesized | |
| # from the yosys port counts. | |
| port_convention: str = "" | |
| pins: list[Pin] = field(default_factory=list) | |
| # The module whose RTL body simulates this memory: {"file", "module"}. | |
| behavioral_model: dict | None = None | |
| # True when FakeRAM converts the memory to an SRAM macro (.lib/.lef | |
| # emitted and the module blackboxed); see `kind` for what the flow | |
| # does with the memory in the end. | |
| idiomatic: bool = False | |
| reason: str = "" | |
| # What the memory becomes: "fakeram" (an SRAM macro), "regfile" (a | |
| # register file generate_ram builds from `spec`) or "flops". | |
| kind: str = "flops" | |
| # "detected" (yosys inferred it), "listed" (AUTO_MEMORIES_REGFILES | |
| # names it) or "override" (an ADDITIONAL_MEMORIES file). | |
| source: str = "detected" | |
| # The register-file spec, for kind "regfile". | |
| spec: str = "" | |
| def total_ports(self) -> int: | |
| return self.read_ports + self.write_ports + self.rw_ports | |
| def to_dict(self) -> dict: | |
| return asdict(self) | |
| def _pin_from_dict(d: dict) -> Pin: | |
| if not isinstance(d, dict): | |
| raise SchemaError("expected a dictionary for pin record") | |
| try: | |
| pin = Pin( | |
| name=d["name"], | |
| direction=d["direction"], | |
| width=int(d["width"]), | |
| port_id=d["port_id"], | |
| function=d["function"], | |
| ) | |
| except KeyError as e: | |
| raise SchemaError(f"pin record missing field {e}") from e | |
| if pin.direction not in DIRECTIONS: | |
| raise SchemaError(f"pin {pin.name}: bad direction '{pin.direction}'") | |
| if pin.function not in PIN_FUNCTIONS: | |
| raise SchemaError( | |
| f"pin {pin.name}: unknown function " | |
| f"'{pin.function}' (expected one of " | |
| f"{', '.join(PIN_FUNCTIONS)})" | |
| ) | |
| if pin.width < 1: | |
| raise SchemaError(f"pin {pin.name}: width must be >= 1") | |
| if not pin.port_id: | |
| raise SchemaError(f"pin {pin.name}: empty port_id") | |
| return pin | |
| def memory_from_dict(d: dict) -> Memory: | |
| if not isinstance(d, dict): | |
| raise SchemaError("expected a dictionary for memory record") | |
| if "name" not in d: | |
| raise SchemaError("memory record missing 'name'") | |
| mem = Memory(name=d["name"]) | |
| for key in ( | |
| "rows", | |
| "bits", | |
| "addr_w", | |
| "read_ports", | |
| "write_ports", | |
| "rw_ports", | |
| "mask_lanes", | |
| "mask_lane_width", | |
| "comb_read_ports", | |
| ): | |
| if key in d: | |
| setattr(mem, key, int(d[key])) | |
| if "port_convention" in d: | |
| mem.port_convention = str(d["port_convention"]) | |
| if "pins" in d: | |
| mem.pins = [_pin_from_dict(p) for p in d["pins"]] | |
| if "behavioral_model" in d: | |
| mem.behavioral_model = d["behavioral_model"] | |
| if "idiomatic" in d: | |
| mem.idiomatic = bool(d["idiomatic"]) | |
| if "reason" in d: | |
| mem.reason = str(d["reason"]) | |
| for key in ("kind", "source", "spec"): | |
| if key in d: | |
| setattr(mem, key, str(d[key])) | |
| return mem | |
| def load(path: Path) -> list[Memory]: | |
| """Load a .memories / memories.json file. | |
| We expect the JSON file to be valid and readable. If it is not, | |
| uncaught exceptions (e.g. FileNotFoundError, JSONDecodeError) provide | |
| full detailed stack traces and debug information. | |
| """ | |
| doc = json.loads(path.read_text()) | |
| if not isinstance(doc, dict) or "memories" not in doc: | |
| raise SchemaError(f"{path}: expected an object with a 'memories' list") | |
| version = doc.get("version", SCHEMA_VERSION) | |
| if version != SCHEMA_VERSION: | |
| raise SchemaError(f"{path}: unsupported schema version {version}") | |
| return [memory_from_dict(m) for m in doc["memories"]] | |
| def dump(memories: list[Memory], path: Path, platform: str) -> None: | |
| doc = { | |
| "version": SCHEMA_VERSION, | |
| "platform": platform, | |
| "memories": [m.to_dict() for m in sorted(memories, key=lambda m: m.name)], | |
| } | |
| path.write_text(json.dumps(doc, indent=2) + "\n") | |
| def merge(detected: list[Memory], overrides: list[Memory]) -> list[Memory]: | |
| """Merge user-supplied entries onto the detected set. | |
| An override naming a detected memory replaces only the fields it | |
| carries (geometry and pins are kept from detection unless the | |
| override provides its own). An override naming an unknown memory is | |
| taken whole — it must then carry pins and geometry itself to be | |
| emittable. | |
| """ | |
| by_name = {m.name: m for m in detected} | |
| for o in overrides: | |
| base = by_name.get(o.name) | |
| if base is None: | |
| by_name[o.name] = o | |
| continue | |
| if o.pins: | |
| base.pins = o.pins | |
| for key in ( | |
| "rows", | |
| "bits", | |
| "addr_w", | |
| "read_ports", | |
| "write_ports", | |
| "rw_ports", | |
| "mask_lanes", | |
| "mask_lane_width", | |
| "comb_read_ports", | |
| ): | |
| value = getattr(o, key) | |
| if value: | |
| setattr(base, key, value) | |
| if o.port_convention: | |
| base.port_convention = o.port_convention | |
| if o.behavioral_model is not None: | |
| base.behavioral_model = o.behavioral_model | |
| if o.idiomatic: | |
| base.idiomatic = True | |
| if o.reason: | |
| base.reason = o.reason | |
| return list(by_name.values()) | |
| def lane_width_check(name: str, bits: int, lanes: int, width: int) -> None: | |
| """Raise SchemaError unless `lanes` mask lanes cover `bits` bits. | |
| Every lane is `width` bits wide but the last, which holds the rest: | |
| lanes * width >= bits > (lanes - 1) * width. Without a width the lanes | |
| split the word evenly. | |
| """ | |
| if not lanes: | |
| if width: | |
| raise SchemaError(f"memory {name}: mask_lane_width without mask_lanes") | |
| return | |
| if not width: | |
| if bits % lanes: | |
| raise SchemaError( | |
| f"memory {name}: {bits} bits are not {lanes} even mask lanes; " | |
| f"give mask_lane_width for a ragged last lane" | |
| ) | |
| return | |
| if not lanes * width >= bits > (lanes - 1) * width: | |
| raise SchemaError( | |
| f"memory {name}: {lanes} mask lanes of {width} bits do not cover " | |
| f"{bits} bits with a last lane of 1 to {width}" | |
| ) | |
| def mask_check(mem: Memory) -> None: | |
| """Raise SchemaError unless every mask pin is `mask_lanes` wide. | |
| FakeRAM counts the lanes from the mask pin's width, never from | |
| mask_lanes, and an override merged onto a detected memory keeps the | |
| detected pins: an override that changes mask_lanes must bring a | |
| mask pin to match. | |
| """ | |
| masks = [p for p in mem.pins if p.function == "mask"] | |
| if mem.mask_lanes and not masks: | |
| raise SchemaError( | |
| f"memory {mem.name}: mask_lanes is {mem.mask_lanes} but no pin " | |
| f"has function mask" | |
| ) | |
| for p in masks: | |
| if p.width != mem.mask_lanes: | |
| raise SchemaError( | |
| f"memory {mem.name}: mask pin {p.name} is {p.width} bits wide " | |
| f"but mask_lanes is {mem.mask_lanes}" | |
| ) | |
| def validate_emittable(mem: Memory) -> None: | |
| """Raise SchemaError unless `mem` carries enough to emit .lib/.lef.""" | |
| if not mem.pins: | |
| raise SchemaError(f"memory {mem.name}: no pins") | |
| if mem.rows < 1 or mem.bits < 1: | |
| raise SchemaError( | |
| f"memory {mem.name}: rows ({mem.rows}) and bits ({mem.bits}) " | |
| f"must be >= 1" | |
| ) | |
| lane_width_check(mem.name, mem.bits, mem.mask_lanes, mem.mask_lane_width) | |
| mask_check(mem) | |
| port_ids = {p.port_id for p in mem.pins} | |
| for port_id in port_ids: | |
| functions = {p.function for p in mem.pins if p.port_id == port_id} | |
| if "clk" not in functions: | |
| raise SchemaError(f"memory {mem.name}: port {port_id} has no clk pin") | |
| if "addr" not in functions: | |
| raise SchemaError(f"memory {mem.name}: port {port_id} has no addr pin") | |
| if not functions & {"data_in", "data_out"}: | |
| raise SchemaError(f"memory {mem.name}: port {port_id} has no data pin") | |