litex/migen/sim/icarus.py

44 lines
1.3 KiB
Python
Raw Normal View History

2012-03-23 11:41:30 -04:00
# Copyright (C) 2012 Vermeer Manufacturing Co.
# License: GPLv3 with additional permissions (see README).
2012-03-05 14:31:41 -05:00
import subprocess
2012-03-06 08:20:26 -05:00
import os
import time
2012-03-05 14:31:41 -05:00
def _str2file(filename, contents):
f = open(filename, "w")
f.write(contents)
f.close()
class Runner:
def __init__(self, options=None, extra_files=None, top_file="migensim_top.v", dut_file="migensim_dut.v", vvp_file=None, keep_files=False):
2012-03-05 14:31:41 -05:00
if extra_files is None: extra_files = []
if vvp_file is None: vvp_file = dut_file + "vp"
if options is None: options = []
self.options = options
2012-03-06 08:20:26 -05:00
self.extra_files = extra_files
2012-03-05 14:31:41 -05:00
self.top_file = top_file
self.dut_file = dut_file
self.vvp_file = vvp_file
2012-03-06 08:20:26 -05:00
self.keep_files = keep_files
2012-03-05 14:31:41 -05:00
def start(self, c_top, c_dut):
_str2file(self.top_file, c_top)
_str2file(self.dut_file, c_dut)
subprocess.check_call(["iverilog", "-o", self.vvp_file] + self.options + [self.top_file, self.dut_file] + self.extra_files)
self.process = subprocess.Popen(["vvp", "-mmigensim", "-Mvpi", self.vvp_file])
2012-03-06 08:20:26 -05:00
def close(self):
2012-03-06 09:00:02 -05:00
if hasattr(self, "process"):
self.process.terminate()
if self.process.poll() is None:
time.sleep(.1)
self.process.kill()
2012-03-06 09:00:02 -05:00
self.process.wait()
2012-03-06 08:20:26 -05:00
if not self.keep_files:
for f in [self.top_file, self.dut_file, self.vvp_file]:
try:
os.remove(f)
except OSError:
pass