Skip to content

Commit

Permalink
Merge pull request #2123 from trabucayre/vhd2vconverter_libraries
Browse files Browse the repository at this point in the history
build/vhd2v_converter.py: allows users to pass a list of libraries files to compile before convert HDL.
  • Loading branch information
enjoy-digital authored Nov 12, 2024
2 parents f056f37 + f731b36 commit d7a8743
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions litex/build/vhd2v_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ class VHD2VConverter(Module):
force use of GHDL even if the platform supports VHDL
_ghdl_opts: str
options to pass to ghdl
_libraries: list of str or tuple
list of libraries (library_name, library_path) to compile before conversion.
"""
def __init__(self, platform, top_entity, build_dir,
work_package = None,
force_convert = False,
add_instance = False,
params = dict(),
files = list()):
files = list(),
libraries = list()):
"""
constructor (see class attributes)
"""
Expand All @@ -62,12 +65,15 @@ def __init__(self, platform, top_entity, build_dir,
self._force_convert = force_convert
self._add_instance = add_instance
self._work_package = work_package
self._libraries = list()

self._ghdl_opts = ["--std=08", "--no-formal"]

if work_package is not None:
self._ghdl_opts.append(f"--work={self._work_package}")

self.add_libraries(libraries)

def add_source(self, filename):
"""
append the source list with the path + name of a file
Expand All @@ -90,6 +96,24 @@ def add_sources(self, path, *filenames):
"""
self._sources += [os.path.join(path, f) for f in filenames]

def add_libraries(self, libraries=[]):
"""
append the library list with a list of tuple (work, file).
Parameters
==========
libraries: list of str or tuple
when str a vhdl library full path, when tuple the work package name
and the vhdl libary path
"""
for lib in libraries:
# when lib is a str -> convert to a tupple based on lib name
if type(lib) == str:
work_pkg = os.path.splitext(os.path.basename(lib))[0]
lib = (work_pkg, lib)
elif type(lib) != tuple:
raise OSError(f"{lib} must a string or a set")
self._libraries.append(lib)

def do_finalize(self):
"""
- convert vhdl to verilog when toolchain can't deal with VHDL or
Expand All @@ -106,6 +130,18 @@ def do_finalize(self):
for file in self._sources:
self._platform.add_source(file, library=self._work_package)
else: # platform is only able to synthesis verilog -> convert vhdl to verilog
import subprocess
from litex.build import tools

# First: compile external libraries (if requested)
for lib in self._libraries:
(work_pkg, filename) = lib
cmd = ["ghdl", "-a", "--std=08", f"--work={work_pkg}", filename]
print(cmd)
s = subprocess.run(cmd)
if s.returncode:
raise OSError(f"Unable to compile {filename}, please check your GHDL install.")

# check if more than one core is instanciated
# if so -> append with _X
# FIXME: better solution ?
Expand All @@ -132,9 +168,6 @@ def do_finalize(self):
cmd += self._sources
cmd += ["-e", self._top_entity]

import subprocess
from litex.build import tools

with open(verilog_out, 'w') as output:
s = subprocess.run(cmd, stdout=output)
if s.returncode:
Expand Down

0 comments on commit d7a8743

Please sign in to comment.