From 16ced88d63f4701125686dde586c272f700cacbe Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 4 Jun 2019 09:13:24 +0000 Subject: [PATCH] build.run: simplify using build products locally, e.g. for programming. --- nmigen/build/run.py | 21 +++++++++++++++++++++ nmigen/vendor/fpga/lattice_ice40.py | 27 +++++++++------------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/nmigen/build/run.py b/nmigen/build/run.py index d4cc6dc..16992f9 100644 --- a/nmigen/build/run.py +++ b/nmigen/build/run.py @@ -1,7 +1,9 @@ from collections import OrderedDict +from contextlib import contextmanager import os import sys import subprocess +import tempfile import zipfile @@ -60,3 +62,22 @@ class BuildProducts: assert mode in "bt" with open(os.path.join(self._root, filename), "r" + mode) as f: return f.read() + + @contextmanager + def extract(self, *filenames): + files = [] + try: + for filename in filenames: + file = tempfile.NamedTemporaryFile(prefix="nmigen_", suffix="_" + filename) + files.append(file) + file.write(self.get(filename)) + + if len(files) == 0: + return (yield) + elif len(files) == 1: + return (yield files[0].name) + else: + return (yield [file.name for file in files]) + finally: + for file in files: + file.close() diff --git a/nmigen/vendor/fpga/lattice_ice40.py b/nmigen/vendor/fpga/lattice_ice40.py index 8434fc8..7355f6d 100644 --- a/nmigen/vendor/fpga/lattice_ice40.py +++ b/nmigen/vendor/fpga/lattice_ice40.py @@ -96,6 +96,7 @@ class LatticeICE40Platform(TemplatedPlatform): """, r""" {{get_tool("icepack")}} + {{verbose("-v")}} {{name}}.asc {{name}}.bin """ @@ -298,34 +299,24 @@ class IceStormProgrammerMixin: "specify it using .build(..., program_opts={\"mode\": \"\"})" .format(mode)) - iceprog = os.environ.get("ICEPROG", "iceprog") - bitstream = products.get("{}.bin".format(name)) + iceprog = os.environ.get("ICEPROG", "iceprog") if mode == "sram": options = ["-S"] if mode == "flash": options = [] - with tempfile.NamedTemporaryFile(prefix="nmigen_iceprog_", - suffix=".bin") as bitstream_file: - bitstream_file.write(bitstream) - subprocess.run([iceprog, *options, bitstream_file.name], check=True) + with products.extract("{}.bin".format(name)) as bitstream_filename: + subprocess.run([iceprog, *options, bitstream_filename], check=True) class IceBurnProgrammerMixin: def toolchain_program(self, products, name): - iceburn = os.environ.get("ICEBURN", "iCEburn") - bitstream = products.get("{}.bin".format(name)) - with tempfile.NamedTemporaryFile(prefix="nmigen_iceburn_", - suffix=".bin") as bitstream_file: - bitstream_file.write(bitstream) - subprocess.run([iceburn, "-evw", bitstream_file.name], check=True) + iceburn = os.environ.get("ICEBURN", "iCEburn") + with products.extract("{}.bin".format(name)) as bitstream_filename: + subprocess.run([iceburn, "-evw", bitstream_filename], check=True) class TinyProgrammerMixin: def toolchain_program(self, products, name): tinyprog = os.environ.get("TINYPROG", "tinyprog") - options = ["-p"] - bitstream = products.get("{}.bin".format(name)) - with tempfile.NamedTemporaryFile(prefix="nmigen_tinyprog_", - suffix=".bin") as bitstream_file: - bitstream_file.write(bitstream) - subprocess.run([tinyprog, *options, bitstream_file.name], check=True) + with products.extract("{}.bin".format(name)) as bitstream_filename: + subprocess.run([tinyprog, "-p", bitstream_filename], check=True) -- 2.30.2