Skip to content

Instantly share code, notes, and snippets.

@InputBlackBoxOutput
Created January 5, 2022 13:07
Show Gist options
  • Save InputBlackBoxOutput/0e421ed3df5755d425acee124ec50e21 to your computer and use it in GitHub Desktop.
Save InputBlackBoxOutput/0e421ed3df5755d425acee124ec50e21 to your computer and use it in GitHub Desktop.
Python wrapper for Yosys
import os, re, subprocess, json
class Yosys:
def __init__(self, path):
self.yosys = subprocess.Popen(
[path, '-Q', '-T'], universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
def process(self, filepath, save=False):
if not os.path.isfile(filepath):
raise Exception("File not found")
# Interact with yosys
out = self.yosys.communicate(f"read_verilog {filepath}\n proc\n opt\n json -aig")[0]
# Remove newline and comments
out = re.sub('\n', ' ', out)
out = re.sub('/\* +\d+ +\*/', ' ', out)
# Parse JSON string
netlist_json = json.loads(re.search("\{.*\}", out).group(0))
if save == True:
with open(filepath.split('.')[0]+".json", 'w') as json_file:
json.dump(netlist_json, json_file)
return netlist_json
def __del__(self):
self.yosys.kill()
if __name__ == '__main__':
y = Yosys("yosys/yosys.exe")
print(y.process(filepath="test.v", save=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment