From c88e37e8bbb26d19c9323b0ff9566716771cffd6 Mon Sep 17 00:00:00 2001 From: David Shah Date: Mon, 3 Feb 2020 16:56:56 +0000 Subject: [PATCH] xilinx: Add Python bindings Signed-off-by: David Shah --- CMakeLists.txt | 2 +- common/arch_pybindings_shared.h | 11 +--- ecp5/arch_pybindings.cc | 4 ++ ice40/arch_pybindings.cc | 4 ++ xilinx/arch_pybindings.cc | 78 ++++++++++++++++++++++++++ xilinx/arch_pybindings.h | 97 +++++++++++++++++++++++++++++++++ 6 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 xilinx/arch_pybindings.cc create mode 100644 xilinx/arch_pybindings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 70da8f38..9d0cf142 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.3) project(nextpnr) option(BUILD_GUI "Build GUI" OFF) -option(BUILD_PYTHON "Build Python Integration" OFF) +option(BUILD_PYTHON "Build Python Integration" ON) option(BUILD_TESTS "Build GUI" OFF) option(BUILD_HEAP "Build HeAP analytic placer" ON) option(USE_OPENMP "Use OpenMP to accelerate analytic placer" ON) diff --git a/common/arch_pybindings_shared.h b/common/arch_pybindings_shared.h index 89a61dad..a11991b5 100644 --- a/common/arch_pybindings_shared.h +++ b/common/arch_pybindings_shared.h @@ -91,11 +91,11 @@ fn_wrapper_1a, conv_from_str>::def_wrap(ctx_cls, "getConflictingPipNet"); -fn_wrapper_1a, +fn_wrapper_1a, conv_from_str>::def_wrap(ctx_cls, "getPipsDownhill"); -fn_wrapper_1a, +fn_wrapper_1a, conv_from_str>::def_wrap(ctx_cls, "getPipsUphill"); -fn_wrapper_1a, +fn_wrapper_1a, conv_from_str>::def_wrap(ctx_cls, "getWireAliases"); fn_wrapper_1a, @@ -105,11 +105,6 @@ fn_wrapper_1a, conv_from_str>::def_wrap(ctx_cls, "getPipDelay"); -fn_wrapper_1a, - pass_through>::def_wrap(ctx_cls, "getPackagePinBel"); -fn_wrapper_1a, - conv_from_str>::def_wrap(ctx_cls, "getBelPackagePin"); - fn_wrapper_0a>::def_wrap( ctx_cls, "getChipName"); fn_wrapper_0a>::def_wrap(ctx_cls, diff --git a/ecp5/arch_pybindings.cc b/ecp5/arch_pybindings.cc index 951745af..328b1b11 100644 --- a/ecp5/arch_pybindings.cc +++ b/ecp5/arch_pybindings.cc @@ -58,6 +58,10 @@ void arch_wrap_python() readonly_wrapper>::def_wrap(belpin_cls, "bel"); readonly_wrapper>::def_wrap(belpin_cls, "pin"); + typedef PipRange AliasPipRange; + typedef PipRange UphillPipRange; + typedef PipRange DownhillPipRange; + #include "arch_pybindings_shared.h" WRAP_RANGE(Bel, conv_to_str); diff --git a/ice40/arch_pybindings.cc b/ice40/arch_pybindings.cc index e2022091..90589b96 100644 --- a/ice40/arch_pybindings.cc +++ b/ice40/arch_pybindings.cc @@ -66,6 +66,10 @@ void arch_wrap_python() readonly_wrapper>::def_wrap(belpin_cls, "bel"); readonly_wrapper>::def_wrap(belpin_cls, "pin"); + typedef PipRange AliasPipRange; + typedef PipRange UphillPipRange; + typedef PipRange DownhillPipRange; + #include "arch_pybindings_shared.h" WRAP_RANGE(Bel, conv_to_str); diff --git a/xilinx/arch_pybindings.cc b/xilinx/arch_pybindings.cc new file mode 100644 index 00000000..b0b01042 --- /dev/null +++ b/xilinx/arch_pybindings.cc @@ -0,0 +1,78 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2020 David Shah + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#ifndef NO_PYTHON + +#include "arch_pybindings.h" +#include "nextpnr.h" +#include "pybindings.h" + +NEXTPNR_NAMESPACE_BEGIN + +void arch_wrap_python() +{ + using namespace PythonConversion; + class_("ArchArgs"); + + class_("BelId").def_readwrite("index", &BelId::index); + + class_("WireId").def_readwrite("index", &WireId::index); + + class_("PipId").def_readwrite("index", &PipId::index); + + class_("BelPin").def_readwrite("bel", &BelPin::bel).def_readwrite("pin", &BelPin::pin); + + auto arch_cls = class_, boost::noncopyable>("Arch", init()); + auto ctx_cls = class_, boost::noncopyable>("Context", no_init) + .def("checksum", &Context::checksum) + .def("pack", &Context::pack) + .def("place", &Context::place) + .def("route", &Context::route); + + fn_wrapper_2a, + addr_and_unwrap, conv_from_str>::def_wrap(ctx_cls, "isValidBelForCell"); + + typedef std::unordered_map> CellMap; + typedef std::unordered_map> NetMap; + typedef std::unordered_map AliasMap; + typedef std::unordered_map HierarchyMap; + + auto belpin_cls = class_>("BelPin", no_init); + readonly_wrapper>::def_wrap(belpin_cls, "bel"); + readonly_wrapper>::def_wrap(belpin_cls, "pin"); + + typedef UphillPipRange AliasPipRange; + +#include "arch_pybindings_shared.h" + + WRAP_RANGE(Bel, conv_to_str); + WRAP_RANGE(Wire, conv_to_str); + WRAP_RANGE(AllPip, conv_to_str); + WRAP_RANGE(UphillPip, conv_to_str); + WRAP_RANGE(DownhillPip, conv_to_str); + WRAP_RANGE(BelPin, wrap_context); + + WRAP_MAP_UPTR(CellMap, "IdCellMap"); + WRAP_MAP_UPTR(NetMap, "IdNetMap"); + WRAP_MAP(HierarchyMap, wrap_context, "HierarchyMap"); +} + +NEXTPNR_NAMESPACE_END + +#endif // NO_PYTHON diff --git a/xilinx/arch_pybindings.h b/xilinx/arch_pybindings.h new file mode 100644 index 00000000..0ce37906 --- /dev/null +++ b/xilinx/arch_pybindings.h @@ -0,0 +1,97 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2020 David Shah + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ +#ifndef ARCH_PYBINDINGS_H +#define ARCH_PYBINDINGS_H +#ifndef NO_PYTHON + +#include "nextpnr.h" +#include "pybindings.h" + +NEXTPNR_NAMESPACE_BEGIN + +namespace PythonConversion { + +template <> struct string_converter +{ + BelId from_str(Context *ctx, std::string name) { return ctx->getBelByName(ctx->id(name)); } + + std::string to_str(Context *ctx, BelId id) + { + if (id == BelId()) + throw bad_wrap(); + return ctx->getBelName(id).str(ctx); + } +}; + +template <> struct string_converter +{ + WireId from_str(Context *ctx, std::string name) { return ctx->getWireByName(ctx->id(name)); } + + std::string to_str(Context *ctx, WireId id) + { + if (id == WireId()) + throw bad_wrap(); + return ctx->getWireName(id).str(ctx); + } +}; + +template <> struct string_converter +{ + WireId from_str(Context *ctx, std::string name) { return ctx->getWireByName(ctx->id(name)); } + + std::string to_str(Context *ctx, WireId id) + { + if (id == WireId()) + throw bad_wrap(); + return ctx->getWireName(id).str(ctx); + } +}; + +template <> struct string_converter +{ + PipId from_str(Context *ctx, std::string name) { return ctx->getPipByName(ctx->id(name)); } + + std::string to_str(Context *ctx, PipId id) + { + if (id == PipId()) + throw bad_wrap(); + return ctx->getPipName(id).str(ctx); + } +}; + +template <> struct string_converter +{ + BelPin from_str(Context *ctx, std::string name) + { + NPNR_ASSERT_FALSE("string_converter::from_str not implemented"); + } + + std::string to_str(Context *ctx, BelPin pin) + { + if (pin.bel == BelId()) + throw bad_wrap(); + return ctx->getBelName(pin.bel).str(ctx) + "/" + pin.pin.str(ctx); + } +}; + +} // namespace PythonConversion + +NEXTPNR_NAMESPACE_END +#endif +#endif