2020-05-22 10:50:45 -06:00
|
|
|
from .._yosys 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
|
|
|
|
|
|
|
|
2019-08-21 16:14:33 -06:00
|
|
|
def _convert_rtlil_text(rtlil_text, *, strip_internal_attrs=False, write_verilog_opts=()):
|
2020-05-22 10:50:45 -06:00
|
|
|
# this version requirement needs to be synchronized with the one in setup.py!
|
|
|
|
yosys = find_yosys(lambda ver: ver >= (0, 9))
|
|
|
|
yosys_version = yosys.version()
|
2019-08-19 17:28:33 -06:00
|
|
|
|
2019-04-22 08:08:01 -06:00
|
|
|
attr_map = []
|
2019-09-24 08:54:22 -06:00
|
|
|
if strip_internal_attrs:
|
|
|
|
attr_map.append("-remove generator")
|
|
|
|
attr_map.append("-remove top")
|
2019-04-22 08:08:01 -06:00
|
|
|
attr_map.append("-remove src")
|
2019-09-24 08:54:22 -06:00
|
|
|
attr_map.append("-remove nmigen.hierarchy")
|
|
|
|
attr_map.append("-remove nmigen.decoding")
|
2019-04-22 08:08:01 -06:00
|
|
|
|
2020-05-22 10:50:45 -06:00
|
|
|
return yosys.run(["-q", "-"], """
|
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-10-28 04:11:41 -06:00
|
|
|
{prune}delete w:$verilog_initial_trigger
|
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-09-24 08:54:22 -06:00
|
|
|
attrmap {attr_map}
|
|
|
|
attrmap -modattr {attr_map}
|
2019-08-21 16:14:33 -06:00
|
|
|
write_verilog -norename {write_verilog_opts}
|
2019-09-24 08:54:22 -06:00
|
|
|
""".format(rtlil_text,
|
2020-05-22 10:50:45 -06:00
|
|
|
# Yosys 0.9 release has buggy proc_prune.
|
|
|
|
prune="# " if yosys_version < (0, 9, 231) else "",
|
2019-09-24 08:54:22 -06:00
|
|
|
attr_map=" ".join(attr_map),
|
2019-08-21 16:14:33 -06:00
|
|
|
write_verilog_opts=" ".join(write_verilog_opts),
|
2020-06-11 10:12:52 -06:00
|
|
|
),
|
|
|
|
# At the moment, Yosys always shows a warning indicating that not all processes can be
|
|
|
|
# translated to Verilog. We carefully emit only the processes that *can* be translated, and
|
|
|
|
# squash this warning. Once Yosys' write_verilog pass is fixed, we should remove this.
|
|
|
|
ignore_warnings=True)
|
2019-08-19 13:27:02 -06:00
|
|
|
|
|
|
|
|
2019-09-24 08:54:22 -06:00
|
|
|
def convert_fragment(*args, strip_internal_attrs=False, **kwargs):
|
|
|
|
rtlil_text, name_map = rtlil.convert_fragment(*args, **kwargs)
|
|
|
|
return _convert_rtlil_text(rtlil_text, strip_internal_attrs=strip_internal_attrs), name_map
|
2019-08-19 13:27:02 -06:00
|
|
|
|
|
|
|
|
2019-09-24 08:54:22 -06:00
|
|
|
def convert(*args, strip_internal_attrs=False, **kwargs):
|
|
|
|
rtlil_text = rtlil.convert(*args, **kwargs)
|
|
|
|
return _convert_rtlil_text(rtlil_text, strip_internal_attrs=strip_internal_attrs)
|