Files
retroSoC/scripts/generate_tiny.py
T
maksyuki 3131762b35 feat: refactor tests for SRAM and CPU in Tiny R2 P4 flow
- Updated SRAM instance paths in  to reflect new hierarchy.
- Enhanced  to include additional source files and updated parameterized tests for SRAM configurations.
- Modified  to align with new SRAM structure and added a test for stale macro hierarchy.
- Introduced  to validate dual-port local-memory protocol and CPU debug/reset functionality, including comprehensive simulation checks.
2026-10-10 18:04:19 +08:00

210 lines
12 KiB
Python

#!/usr/bin/env python3
"""Generate Tiny integration bindings and absolute source lists from owned maps."""
from __future__ import annotations
import argparse
import json
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from scripts.rtl.generate_memory_map import generate as generate_memory_map # noqa: E402
from scripts.rtl.generate_pin_map import generate as generate_pin_map # noqa: E402
from scripts.setup_helpers import atomic_write # noqa: E402
TINY = ROOT / "rtl/tiny"
def read_topology(path: Path, memory_map: Path) -> dict:
document = json.loads(path.read_text())
regions = {r["symbol"] for r in json.loads(memory_map.read_text())["regions"]}
if document.get("schema_version") != 1 or document.get("dma_channels") != 4:
raise ValueError("Tiny requires topology schema 1 and four DMA channels")
if document.get("cpu") != {"module": "hazard3_cpu_2port", "local_ports": 2,
"data_outstanding": 1}:
raise ValueError("Tiny P4 requires independent I/D and one ordered data operation")
memory = document.get("main_sram", {})
if any(memory.get(key) != value for key, value in {
"groups": 4, "group_bytes": 32768, "macros_per_group": 8,
"macro_bytes": 4096, "base": "0x30000000",
"macro_instance": "u_soc.u_sram.gen_group[{group}].u_group.gen_bank[{bank}].u_ram.u_mem",
"external_attachment": "serialized-pre-P5",
}.items()):
raise ValueError("Tiny P4 SRAM geometry/attachment differs from its frozen contract")
if document.get("irq_vector_width") != 32 or document.get("external_irq_count") != 30:
raise ValueError("Tiny requires a 32-bit IRQ vector and 30 external inputs")
names: set[str] = set()
bits: set[int] = set()
for irq in document["interrupts"]:
if irq["name"] in names or irq["core_bit"] in bits:
raise ValueError("duplicate Tiny interrupt")
if not 0 <= irq["core_bit"] < 32:
raise ValueError("Tiny IRQ outside vector")
names.add(irq["name"])
bits.add(irq["core_bit"])
assigned: set[str] = set()
for target in document["apb_targets"]:
for region in target["regions"]:
if region not in regions or region in assigned:
raise ValueError(f"invalid or duplicate APB region {region}")
assigned.add(region)
if assigned != {r for r in regions if r.startswith("APB4_")}:
raise ValueError("Tiny topology must route every APB region")
if len(document["apb_targets"]) != 16:
raise ValueError("Tiny's fixed APB bridge requires sixteen targets")
target_names = [target["name"] for target in document["apb_targets"]]
if len(set(target_names)) != 16 or any(re.fullmatch(r"[a-z][a-z0-9_]*", name) is None
for name in target_names):
raise ValueError("Tiny APB target names must be unique identifiers")
if sorted(item["pin"] for item in document["gpio_alt_functions"]) != list(range(32)):
raise ValueError("Tiny GPIO map must contain all 32 pins exactly once")
return document
def platform_values(pdk: str, have_pll: bool = False) -> dict[str, int]:
if (pdk, have_pll) not in (("IHP130", False), ("ICS55", True)):
raise ValueError("unsupported Tiny PDK/PLL combination")
return {"technology": 0x02040037 if pdk == "ICS55" else 0x02010082,
"features0": 0x7FFE | int(have_pll), "pll_present": int(have_pll),
"gpio_pulls": int(pdk == "ICS55")}
def generate_bindings(document: dict, output: Path, pdk: str = "IHP130",
have_pll: bool = False) -> None:
platform = platform_values(pdk, have_pll)
comment = "// Generated by scripts/generate_tiny.py; do not edit.\n"
irq = [comment, "always_comb begin", " s_irq = '0;"]
header = ["#ifndef RETROSOC_GENERATED_IRQ_METADATA_H",
"#define RETROSOC_GENERATED_IRQ_METADATA_H", "#include <stdint.h>",
"#define RS_SOC_IRQ_VECTOR_WIDTH UINT32_C(32)",
"#define RS_SOC_EXTERNAL_IRQ_COUNT UINT32_C(30)",
f"#define RS_SOC_ALLOCATED_IRQ_COUNT UINT32_C({len(document['interrupts']) - 2})"]
for item in document["interrupts"]:
bit, name = item["core_bit"], item["name"].upper()
irq.append(f" s_irq[{bit}] = {item['signal']};")
header.append(f"#define RS_SOC_IRQ_{name} UINT32_C({bit})")
if bit >= 2:
header.append(f"#define RS_SOC_EXT_IRQ_{name} UINT32_C({bit - 2})")
irq.append("end\n")
header.append("#endif\n")
atomic_write(output / "rtl/tiny_irq.svh", "\n".join(irq))
atomic_write(output / "include/retrosoc/generated/irq_metadata.h", "\n".join(header))
atomic_write(output / "include/retrosoc/generated/user_extensions.h",
"#ifndef RETROSOC_GENERATED_USER_EXTENSIONS_H\n"
"#define RETROSOC_GENERATED_USER_EXTENSIONS_H\n"
"#define RS_SOC_USER_CORE_COUNT 0U\n#define RS_SOC_USER_IP_COUNT 0U\n"
"#define RS_SOC_HAS_USER_CORES 0U\n#define RS_SOC_HAS_USER_IP_MUX 0U\n#endif\n")
atomic_write(output / "include/retrosoc/generated/soc_features.h",
"#ifndef RETROSOC_GENERATED_SOC_FEATURES_H\n"
"#define RETROSOC_GENERATED_SOC_FEATURES_H\n"
"#define RS_SOC_TINY 1\n#define RS_SOC_HAS_PSRAM 0U\n"
"#define RS_SOC_DMA_CHANNELS 4U\n#define RS_SOC_ID 0x54494E59U\n"
f"#define RS_SOC_TECHNOLOGY_ID 0x{platform['technology']:08X}U\n"
f"#define RS_SOC_PLL_PRESENT {platform['pll_present']}U\n"
f"#define RS_SOC_GPIO_HAS_PULLS {platform['gpio_pulls']}U\n#endif\n")
atomic_write(output / "rtl/tiny_platform.svh", comment +
f"`define RETROSOC_TINY__TECHNOLOGY 32'h{platform['technology']:08x}\n"
f"`define RETROSOC_TINY__FEATURES0 32'h{platform['features0']:08x}\n")
gpio = [comment]
for function in document["gpio_alt_functions"]:
pin = function["pin"]
for mode in ("alt0", "alt1"):
value = function[mode]
gpio.extend(f"assign {signal} = u_gpio_if.di_i[{pin}];" for signal in value["inputs"])
gpio.extend((f"assign u_gpio_if.{mode}_do_i[{pin}] = {value['do']};",
f"assign u_gpio_if.{mode}_oe_i[{pin}] = {value['oe']};"))
atomic_write(output / "rtl/tiny_gpio.svh", "\n".join(gpio) + "\n")
ports, declarations, routes, decode = [], [comment], [comment], [comment]
reset_assignments = []
for index, target in enumerate(document["apb_targets"]):
name = target["name"]
ports.append(f"apb4_if.master {name}")
declarations.append(f"logic s_{name}_rst_n;")
declarations.append(f"apb4_if u_{name}_apb4_if (.pclk(clk_i), .presetn(s_{name}_rst_n));")
reset_assignments.append(f"assign s_{name}_rst_n = s_leaf_rst_n[{index + 1}];")
routes.append(f".{name}(u_{name}_apb4_if)")
decode.append(f"if ({' || '.join(f'`SOC_ADDR_IS_{r}(s_addr_q)' for r in target['regions'])}) "
f"s_select = {len(document['apb_targets'])}'b{1 << index:016b};")
atomic_write(output / "rtl/tiny_apb_ports.svh", ",\n".join(ports) + "\n")
atomic_write(output / "rtl/tiny_apb_interfaces.svh", "\n".join(declarations + reset_assignments) + "\n")
atomic_write(output / "rtl/tiny_apb_connections.svh", ",\n".join(routes[1:]) + "\n")
atomic_write(output / "rtl/tiny_apb_decode.svh", "\n".join(decode) + "\n")
wiring = [comment]
for index, target in enumerate(document["apb_targets"]):
name = target["name"]
for field, value in {"paddr": "s_addr_q", "pwrite": "s_write_q",
"pwdata": "s_wdata_q", "pstrb": "s_wstrb_q", "pprot": "3'd0",
"psel": f"s_select[{index}] && s_access",
"penable": "s_enable"}.items():
wiring.append(f"assign {name}.{field} = {value};")
wiring.extend((f"assign s_ready[{index}] = {name}.pready;",
f"assign s_error[{index}] = {name}.pslverr;",
f"assign s_rdata[{index}] = {name}.prdata;"))
atomic_write(output / "rtl/tiny_apb_wiring.svh", "\n".join(wiring) + "\n")
def generate_filelists(output: Path, generated: Path, defines: list[str],
pdk: str = "IHP130", simulator: str = "VERILATOR",
synthesis: str = "NONE") -> None:
prefixes = {"top": TINY / "top", "dv": TINY / "dv", "ip": ROOT / "rtl/ip",
"tech": ROOT / "rtl/tech", "clusterip": ROOT / "rtl/managed/clusterip",
"hazard3": ROOT / "rtl/managed/hazard3/hdl", "physical": ROOT / "physical",
"cache": ROOT / ".cache"}
suffix = "_yosys" if synthesis == "YOSYS" else "_verilator" if simulator == "VERILATOR" else ""
pdk_name = "pdk_ihp130.fl" if pdk == "IHP130" else f"pdk_ics55{suffix}.fl"
templates = [*sorted((TINY / "filelist").glob("*.fl")),
ROOT / "rtl/filelist" / pdk_name]
for template in templates:
content = template.read_text()
for prefix, directory in prefixes.items():
content = re.sub(rf"(?<![^\s+])/{prefix}(?=/|\s|$)", str(directory), content)
if template.name == pdk_name and pdk == "ICS55":
# The shared tc_sram file also declares an unused 16 KiB wrapper.
# Keep its official interface visible to parsers; Tiny must never
# instantiate it (the mapped-netlist audit rejects that case).
content = content.replace("ics55_ecos_sram_4096x32_m8_core.v",
"ics55_ecos_sram_4096x32_m8_stub.v")
atomic_write(output / template.name, content)
if template.name == pdk_name:
if pdk == "ICS55" and synthesis != "YOSYS":
content += str(ROOT / ".cache/retrosoc/sources/ics55_ecos_pll/verilog/PLL_TOP.behavioral.v") + "\n"
atomic_write(output / "pdk_selected.fl", content)
include = [TINY / "dv", generated / "rtl", generated / "memory_map/rtl", generated / "pin_map/rtl",
generated / "archinfo"]
atomic_write(output / "def.fl", " ".join([*defines, *(f"+incdir+{p}" for p in include)]) + "\n")
atomic_write(output / "pdk_local.fl", "")
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path)
parser.add_argument("--filelists", type=Path)
parser.add_argument("--define", action="append", default=[])
parser.add_argument("--check", action="store_true")
parser.add_argument("--pdk", choices=("IHP130", "ICS55"), default="IHP130")
parser.add_argument("--simulator", choices=("IVERILOG", "VERILATOR"), default="VERILATOR")
parser.add_argument("--synthesis", choices=("NONE", "YOSYS"), default="NONE")
args = parser.parse_args()
document = read_topology(TINY / "integration/soc_topology.json",
TINY / "address_map/memory_map.json")
if args.check:
return 0
if args.output is None or args.filelists is None:
parser.error("--output and --filelists are required unless --check is used")
if any(not value.startswith("+define+") for value in args.define):
parser.error("--define requires a +define+ token")
output = args.output.resolve()
generate_memory_map(TINY / "address_map/memory_map.json", output / "memory_map", "YES", 128)
generate_pin_map(TINY / "pin_map/pin_map.json", output / "pin_map", read_while_driving=True)
generate_bindings(document, output, args.pdk, "+define+HAVE_PLL" in args.define)
generate_filelists(args.filelists.resolve(), output, args.define,
args.pdk, args.simulator, args.synthesis)
return 0
if __name__ == "__main__":
raise SystemExit(main())