2018-12-11 13:50:56 -07:00
|
|
|
import os
|
2019-08-19 17:28:33 -06:00
|
|
|
import re
|
2018-12-11 13:50:56 -07:00
|
|
|
import subprocess
|
|
|
|
|
2019-08-28 05:32:18 -06:00
|
|
|
from .._toolchain import *
|
2018-12-11 13:50:56 -07:00
|
|
|
from . import rtlil
|
|
|
|
|
|
|
|
|
2019-08-19 13:27:02 -06:00
|
|
|
__all__ = ["YosysError", "convert", "convert_fragment"]
|
2018-12-11 13:50:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
class YosysError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-08-19 17:28:33 -06:00
|
|
|
def _yosys_version():
|
2019-08-28 05:32:18 -06:00
|
|
|
yosys_path = get_tool("yosys")
|
2019-01-13 01:10:23 -07:00
|
|
|
try:
|
2019-08-28 05:32:18 -06:00
|
|
|
version = subprocess.check_output([yosys_path, "-V"], encoding="utf-8")
|
2019-01-13 01:10:23 -07:00
|
|
|
except FileNotFoundError as e:
|
|
|
|
if os.getenv("YOSYS"):
|
|
|
|
raise YosysError("Could not find Yosys in {} as specified via the YOSYS environment "
|
|
|
|
"variable".format(os.getenv("YOSYS"))) from e
|
2019-08-28 05:32:18 -06:00
|
|
|
elif yosys_path == "yosys":
|
2019-01-13 01:10:23 -07:00
|
|
|
raise YosysError("Could not find Yosys in PATH. Place `yosys` in PATH or specify "
|
|
|
|
"path explicitly via the YOSYS environment variable") from e
|
2019-08-28 05:32:18 -06:00
|
|
|
else:
|
|
|
|
raise
|
2019-01-13 01:10:23 -07:00
|
|
|
|
2019-08-26 03:35:37 -06:00
|
|
|
m = re.match(r"^Yosys ([\d.]+)(?:\+(\d+))?", version)
|
|
|
|
tag, offset = m[1], m[2] or 0
|
2019-08-19 17:28:33 -06:00
|
|
|
return tuple(map(int, tag.split("."))), offset
|
|
|
|
|
|
|
|
|
|
|
|
def _convert_il_text(il_text, strip_src):
|
|
|
|
version, offset = _yosys_version()
|
2019-08-26 03:35:37 -06:00
|
|
|
if version < (0, 9):
|
2019-08-19 17:28:33 -06:00
|
|
|
raise YosysError("Yosys %d.%d is not suppored", *version)
|
|
|
|
|
2019-04-22 08:08:01 -06:00
|
|
|
attr_map = []
|
|
|
|
if strip_src:
|
|
|
|
attr_map.append("-remove src")
|
|
|
|
|
2019-08-19 17:28:33 -06:00
|
|
|
script = """
|
2018-12-12 21:51:15 -07:00
|
|
|
# Convert nMigen's RTLIL to readable Verilog.
|
2018-12-11 13:50:56 -07:00
|
|
|
read_ilang <<rtlil
|
|
|
|
{}
|
|
|
|
rtlil
|
2019-08-26 03:35:37 -06:00
|
|
|
{prune}proc_prune
|
2018-12-11 13:50:56 -07:00
|
|
|
proc_init
|
|
|
|
proc_arst
|
|
|
|
proc_dff
|
|
|
|
proc_clean
|
2018-12-20 18:55:59 -07:00
|
|
|
memory_collect
|
2019-04-22 08:08:01 -06:00
|
|
|
attrmap {}
|
2018-12-21 17:53:40 -07:00
|
|
|
write_verilog -norename
|
2019-08-26 03:35:37 -06:00
|
|
|
""".format(il_text, " ".join(attr_map),
|
|
|
|
prune="# " if version == (0, 9) and offset == 0 else "")
|
2019-08-19 17:28:33 -06:00
|
|
|
|
|
|
|
popen = subprocess.Popen([os.getenv("YOSYS", "yosys"), "-q", "-"],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
encoding="utf-8")
|
|
|
|
verilog_text, error = popen.communicate(script)
|
2018-12-11 13:50:56 -07:00
|
|
|
if popen.returncode:
|
|
|
|
raise YosysError(error.strip())
|
|
|
|
else:
|
|
|
|
return verilog_text
|
2019-08-19 13:27:02 -06:00
|
|
|
|
|
|
|
|
|
|
|
def convert_fragment(*args, strip_src=False, **kwargs):
|
|
|
|
il_text = rtlil.convert_fragment(*args, **kwargs)
|
|
|
|
return _convert_il_text(il_text, strip_src)
|
|
|
|
|
|
|
|
|
|
|
|
def convert(*args, strip_src=False, **kwargs):
|
|
|
|
il_text = rtlil.convert(*args, **kwargs)
|
|
|
|
return _convert_il_text(il_text, strip_src)
|