2018-12-11 13:50:56 -07:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from . import rtlil
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["convert"]
|
|
|
|
|
|
|
|
|
|
|
|
class YosysError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-04-22 08:08:01 -06:00
|
|
|
def convert(*args, strip_src=False, **kwargs):
|
2019-01-13 01:10:23 -07:00
|
|
|
try:
|
|
|
|
popen = subprocess.Popen([os.getenv("YOSYS", "yosys"), "-q", "-"],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
encoding="utf-8")
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
raise YosysError("Could not find Yosys in PATH. Place `yosys` in PATH or specify "
|
|
|
|
"path explicitly via the YOSYS environment variable") from e
|
|
|
|
|
2019-04-22 08:08:01 -06:00
|
|
|
attr_map = []
|
|
|
|
if strip_src:
|
|
|
|
attr_map.append("-remove src")
|
|
|
|
|
2018-12-11 13:50:56 -07:00
|
|
|
il_text = rtlil.convert(*args, **kwargs)
|
|
|
|
verilog_text, error = popen.communicate("""
|
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
|
|
|
|
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-04-22 08:08:01 -06:00
|
|
|
""".format(il_text, " ".join(attr_map)))
|
2018-12-11 13:50:56 -07:00
|
|
|
if popen.returncode:
|
|
|
|
raise YosysError(error.strip())
|
|
|
|
else:
|
|
|
|
return verilog_text
|