Skip to content

Commit b450a00

Browse files
authored
Merge pull request #64 from ndyashas/develop
Feature addition: Verilog code in Python strings along with verilog files.
2 parents 8ea415d + 153b459 commit b450a00

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

pyverilog/vparser/preprocessor.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from __future__ import print_function
2525
import sys
2626
import os
27+
import tempfile
2728
import subprocess
2829

2930

@@ -33,7 +34,28 @@ def __init__(self, filelist, outputfile='pp.out', include=None, define=None):
3334
if not isinstance(filelist, (tuple, list)):
3435
filelist = list(filelist)
3536

36-
self.filelist = filelist
37+
# Elements in `filelist` can either be raw Verilog files, or Verilog code
38+
# in python string. The following loop iterates through these `sources`,
39+
# and normalizes all of them into files.
40+
#
41+
# For Verilog code in python string, the contents of the string is stored
42+
# in a temporary file for further use with `iverilog`.
43+
self.temp_files_paths = []
44+
self.filelist = []
45+
46+
for source in filelist:
47+
# If `source` is verilog code in python strings
48+
if not os.path.isfile(source):
49+
temp_fd, temp_path = tempfile.mkstemp(prefix="pyverilog_temp_", suffix=".v")
50+
with open(temp_fd, 'w') as f:
51+
f.write(source)
52+
53+
self.temp_files_paths.append(temp_path)
54+
55+
else: # else if it is normal verilog file path
56+
self.filelist.append(source)
57+
58+
self.filelist += self.temp_files_paths
3759

3860
iverilog = os.environ.get('PYVERILOG_IVERILOG')
3961
if iverilog is None:
@@ -63,6 +85,10 @@ def preprocess(self):
6385
cmd = self.iv + list(self.filelist)
6486
subprocess.call(cmd)
6587

88+
# Removing the temporary files that were created
89+
for temp_file_path in self.temp_files_paths:
90+
os.remove(temp_file_path)
91+
6692

6793
def preprocess(
6894
filelist,

0 commit comments

Comments
 (0)