2015-10-14 09:27:42 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2024-04-08 22:33:50 -05:00
|
|
|
from __future__ import annotations
|
2015-10-14 09:27:42 -05:00
|
|
|
import fileinput
|
|
|
|
import json
|
2024-04-11 20:57:51 -05:00
|
|
|
from pathlib import Path
|
2015-10-14 09:27:42 -05:00
|
|
|
|
2024-04-08 22:33:50 -05:00
|
|
|
class SimHelper:
|
|
|
|
name: str = ""
|
2024-04-10 18:15:24 -05:00
|
|
|
title: str = ""
|
2024-04-08 22:33:50 -05:00
|
|
|
ports: str = ""
|
2024-04-11 20:57:51 -05:00
|
|
|
source: str = ""
|
2024-04-08 22:33:50 -05:00
|
|
|
desc: list[str]
|
|
|
|
code: list[str]
|
2024-05-20 19:16:32 -05:00
|
|
|
group: str = ""
|
2024-04-08 22:33:50 -05:00
|
|
|
ver: str = "1"
|
2024-09-04 23:51:26 -05:00
|
|
|
tags: list[str]
|
2015-10-14 09:27:42 -05:00
|
|
|
|
2024-04-08 22:33:50 -05:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.desc = []
|
2024-09-04 23:51:26 -05:00
|
|
|
self.tags = []
|
2024-04-08 22:33:50 -05:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2024-04-11 20:57:51 -05:00
|
|
|
printed_fields = [
|
2024-05-20 19:16:32 -05:00
|
|
|
"name", "title", "ports", "source", "desc", "code", "group", "ver",
|
2024-09-04 23:51:26 -05:00
|
|
|
"tags",
|
2024-04-11 20:57:51 -05:00
|
|
|
]
|
|
|
|
# generate C++ struct
|
2024-05-15 18:32:14 -05:00
|
|
|
val = f"cell_help[{json.dumps(self.name)}] = "
|
|
|
|
val += "{\n"
|
2024-04-11 20:57:51 -05:00
|
|
|
for field in printed_fields:
|
|
|
|
field_val = getattr(self, field)
|
|
|
|
if isinstance(field_val, list):
|
|
|
|
field_val = "\n".join(field_val)
|
2024-05-20 19:16:32 -05:00
|
|
|
field_val = field_val.strip()
|
2024-04-11 20:57:51 -05:00
|
|
|
val += f' {json.dumps(field_val)},\n'
|
2024-04-08 22:33:50 -05:00
|
|
|
val += "};\n"
|
|
|
|
return val
|
|
|
|
|
2024-04-11 22:48:47 -05:00
|
|
|
def simcells_reparse(cell: SimHelper):
|
|
|
|
# cut manual signature
|
|
|
|
cell.desc = cell.desc[3:]
|
|
|
|
|
|
|
|
# code-block truth table
|
|
|
|
new_desc = []
|
|
|
|
indent = ""
|
|
|
|
for line in cell.desc:
|
|
|
|
if line.startswith("Truth table:"):
|
|
|
|
indent = " "
|
|
|
|
new_desc.pop()
|
|
|
|
new_desc.extend(["::", ""])
|
|
|
|
new_desc.append(indent + line)
|
|
|
|
cell.desc = new_desc
|
|
|
|
|
|
|
|
# set version
|
|
|
|
cell.ver = "2a"
|
|
|
|
|
2024-04-08 22:33:50 -05:00
|
|
|
simHelper = SimHelper()
|
2015-10-14 09:27:42 -05:00
|
|
|
|
|
|
|
for line in fileinput.input():
|
2024-04-08 22:33:50 -05:00
|
|
|
line = line.rstrip()
|
|
|
|
# special comments
|
2015-10-14 09:27:42 -05:00
|
|
|
if line.startswith("//-"):
|
2024-04-08 22:33:50 -05:00
|
|
|
simHelper.desc.append(line[4:] if len(line) > 4 else "")
|
|
|
|
elif line.startswith("//* "):
|
|
|
|
_, key, val = line.split(maxsplit=2)
|
|
|
|
setattr(simHelper, key, val)
|
|
|
|
|
|
|
|
# code parsing
|
2015-10-14 09:27:42 -05:00
|
|
|
if line.startswith("module "):
|
2024-04-08 22:33:50 -05:00
|
|
|
clean_line = line[7:].replace("\\", "").replace(";", "")
|
|
|
|
simHelper.name, simHelper.ports = clean_line.split(maxsplit=1)
|
|
|
|
simHelper.code = []
|
2024-04-11 20:57:51 -05:00
|
|
|
short_filename = Path(fileinput.filename()).name
|
|
|
|
simHelper.source = f'{short_filename}:{fileinput.filelineno()}'
|
2015-10-16 19:22:42 -05:00
|
|
|
elif not line.startswith("endmodule"):
|
|
|
|
line = " " + line
|
2024-04-08 22:33:50 -05:00
|
|
|
try:
|
|
|
|
simHelper.code.append(line.replace("\t", " "))
|
|
|
|
except AttributeError:
|
|
|
|
# no module definition, ignore line
|
|
|
|
pass
|
2015-10-14 09:27:42 -05:00
|
|
|
if line.startswith("endmodule"):
|
2024-04-11 22:48:47 -05:00
|
|
|
short_filename = Path(fileinput.filename()).name
|
|
|
|
if simHelper.ver == "1" and short_filename == "simcells.v":
|
|
|
|
# default simcells parsing
|
|
|
|
simcells_reparse(simHelper)
|
2024-05-20 19:16:32 -05:00
|
|
|
|
|
|
|
# check help
|
2024-05-26 19:47:47 -05:00
|
|
|
if simHelper.desc and simHelper.ver == "1" and short_filename == "simlib.v" and simHelper.desc[1].startswith(' '):
|
2024-05-20 19:16:32 -05:00
|
|
|
simHelper.desc.pop(1)
|
|
|
|
|
|
|
|
# check group
|
2024-09-04 23:20:13 -05:00
|
|
|
assert simHelper.group, f"techlibs/common/{simHelper.source}: {simHelper.name} cell missing group"
|
2024-05-20 19:16:32 -05:00
|
|
|
|
|
|
|
# dump
|
2024-04-08 22:33:50 -05:00
|
|
|
print(simHelper)
|
2024-05-20 19:16:32 -05:00
|
|
|
# new
|
2024-04-08 22:33:50 -05:00
|
|
|
simHelper = SimHelper()
|
2015-10-14 09:27:42 -05:00
|
|
|
|