Merge branch 'master' into gqtech

This commit is contained in:
ZipCPU 2018-06-07 07:45:22 -04:00
commit 1ed5c641c1
21 changed files with 1402 additions and 1103 deletions

View File

@ -47,12 +47,10 @@ DisableFormat: false
ExperimentalAutoDetectBinPacking: false ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories: IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Regex: '<.*>'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1 Priority: 1
- Regex: '.*'
Priority: 2
IndentCaseLabels: false IndentCaseLabels: false
IndentWidth: 4 IndentWidth: 4
IndentWrappedFunctionNames: false IndentWrappedFunctionNames: false

1
.gitignore vendored
View File

@ -18,3 +18,4 @@ CMakeCache.txt
.*.swp .*.swp
a.out a.out
*.json *.json
build/

View File

@ -21,6 +21,7 @@ Building
- Use CMake to generate the Makefiles (only needs to be done when `CMakeLists.txt` changes) - Use CMake to generate the Makefiles (only needs to be done when `CMakeLists.txt` changes)
- For a debug build, run `cmake -DCMAKE_BUILD_TYPE=Debug .` - For a debug build, run `cmake -DCMAKE_BUILD_TYPE=Debug .`
- For a debug build with HX1K support only, run ` cmake -DCMAKE_BUILD_TYPE=Debug -DICE40_HX1K_ONLY=1 .`
- For a release build, run `cmake .` - For a release build, run `cmake .`
- Use Make to run the build itself - Use Make to run the build itself
- For all targets, just run `make` - For all targets, just run `make`

View File

@ -18,4 +18,3 @@
*/ */
#include "design.h" #include "design.h"

View File

@ -20,12 +20,12 @@
#ifndef DESIGN_H #ifndef DESIGN_H
#define DESIGN_H #define DESIGN_H
#include <stdint.h>
#include <assert.h> #include <assert.h>
#include <vector> #include <stdint.h>
#include <string> #include <string>
#include <unordered_set>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
#include <vector>
// replace with proper IdString later // replace with proper IdString later
typedef std::string IdString; typedef std::string IdString;
@ -39,7 +39,8 @@ struct GraphicElement
{ {
// This will control colour, and there should be separate // This will control colour, and there should be separate
// visibility controls in some cases also // visibility controls in some cases also
enum { enum
{
// Wires entirely inside tiles, e.g. between switchbox and bels // Wires entirely inside tiles, e.g. between switchbox and bels
G_LOCAL_WIRES, G_LOCAL_WIRES,
// Standard inter-tile routing // Standard inter-tile routing
@ -55,7 +56,8 @@ struct GraphicElement
G_TILE_MISC, G_TILE_MISC,
} style; } style;
enum { enum
{
G_LINE, G_LINE,
G_BOX, G_BOX,
G_CIRCLE, G_CIRCLE,
@ -116,7 +118,8 @@ struct Design
{ {
struct Chip chip; struct Chip chip;
Design(ChipArgs args) : chip(args) { Design(ChipArgs args) : chip(args)
{
// ... // ...
} }

View File

@ -18,12 +18,15 @@
* *
*/ */
#include "design.h"
#include "chip.h" #include "chip.h"
#include "design.h"
#include "emb.h"
// include after design.h/chip.h
#include "pybindings.h" #include "pybindings.h"
// Required to determine concatenated module name (which differs for different archs) // Required to determine concatenated module name (which differs for different
// archs)
#define PASTER(x, y) x##_##y #define PASTER(x, y) x##_##y
#define EVALUATOR(x, y) PASTER(x, y) #define EVALUATOR(x, y) PASTER(x, y)
#define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME) #define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME)
@ -31,11 +34,73 @@
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) #define TOSTRING(x) STRINGIFY(x)
// Architecture-specific bindings should be created in the below function, which must be implemented in all // Architecture-specific bindings should be created in the below function, which
// architectures // must be implemented in all architectures
void arch_wrap_python(); void arch_wrap_python();
BOOST_PYTHON_MODULE (MODULE_NAME) { bool operator==(const PortRef &a, const PortRef &b)
{
return (a.cell == b.cell) && (a.port == b.port);
}
BOOST_PYTHON_MODULE(MODULE_NAME)
{
class_<GraphicElement>("GraphicElement")
.def_readwrite("style", &GraphicElement::style)
.def_readwrite("type", &GraphicElement::type)
.def_readwrite("x1", &GraphicElement::x1)
.def_readwrite("y1", &GraphicElement::y1)
.def_readwrite("x2", &GraphicElement::x2)
.def_readwrite("y2", &GraphicElement::y2)
.def_readwrite("text", &GraphicElement::text);
class_<PortRef>("PortRef")
.def_readwrite("cell", &PortRef::cell)
.def_readwrite("port", &PortRef::port);
class_<NetInfo, NetInfo *>("NetInfo")
.def_readwrite("name", &NetInfo::name)
.def_readwrite("driver", &NetInfo::driver)
.def_readwrite("users", &NetInfo::users)
.def_readwrite("attrs", &NetInfo::attrs)
.def_readwrite("wires", &NetInfo::wires);
WRAP_MAP(decltype(NetInfo::attrs), "IdStrMap");
class_<vector<PortRef>>("PortRefVector")
.def(vector_indexing_suite<vector<PortRef>>());
enum_<PortType>("PortType")
.value("PORT_IN", PORT_IN)
.value("PORT_OUT", PORT_OUT)
.value("PORT_INOUT", PORT_INOUT)
.export_values();
class_<PortInfo>("PortInfo")
.def_readwrite("name", &PortInfo::name)
.def_readwrite("net", &PortInfo::net)
.def_readwrite("type", &PortInfo::type);
class_<CellInfo, CellInfo *>("CellInfo")
.def_readwrite("name", &CellInfo::name)
.def_readwrite("type", &CellInfo::type)
.def_readwrite("ports", &CellInfo::ports)
.def_readwrite("attrs", &CellInfo::attrs)
.def_readwrite("params", &CellInfo::params)
.def_readwrite("bel", &CellInfo::bel)
.def_readwrite("pins", &CellInfo::pins);
WRAP_MAP(decltype(CellInfo::ports), "IdPortMap");
// WRAP_MAP(decltype(CellInfo::pins), "IdIdMap");
class_<Design, Design *>("Design", no_init)
.def_readwrite("chip", &Design::chip)
.def_readwrite("nets", &Design::nets)
.def_readwrite("cells", &Design::cells);
WRAP_MAP(decltype(Design::nets), "IdNetMap");
WRAP_MAP(decltype(Design::cells), "IdCellMap");
arch_wrap_python(); arch_wrap_python();
} }
@ -44,19 +109,37 @@ void arch_appendinittab()
PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME);
} }
void execute_python_file(const char *executable, const char* python_file) static wchar_t *program;
void init_python(const char *executable)
{ {
wchar_t *program = Py_DecodeLocale(executable, NULL); program = Py_DecodeLocale(executable, NULL);
if (program == NULL) { if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode executable filename\n"); fprintf(stderr, "Fatal error: cannot decode executable filename\n");
exit(1); exit(1);
} }
try try {
{
PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME);
emb::append_inittab();
Py_SetProgramName(program); Py_SetProgramName(program);
Py_Initialize(); Py_Initialize();
PyImport_ImportModule(TOSTRING(MODULE_NAME));
} catch (boost::python::error_already_set const &) {
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
}
void deinit_python()
{
Py_Finalize();
PyMem_RawFree(program);
}
void execute_python_file(const char *python_file)
{
try {
FILE *fp = fopen(python_file, "r"); FILE *fp = fopen(python_file, "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Fatal error: file not found %s\n", python_file); fprintf(stderr, "Fatal error: file not found %s\n", python_file);
@ -64,15 +147,9 @@ void execute_python_file(const char *executable, const char* python_file)
} }
PyRun_SimpleFile(fp, python_file); PyRun_SimpleFile(fp, python_file);
fclose(fp); fclose(fp);
} catch (boost::python::error_already_set const &) {
Py_Finalize();
PyMem_RawFree(program);
}
catch(boost::python::error_already_set const &)
{
// Parse and output the exception // Parse and output the exception
std::string perror_str = parse_python_exception(); std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl; std::cout << "Error in Python: " << perror_str << std::endl;
} }
} }

View File

@ -20,67 +20,98 @@
#ifndef COMMON_PYBINDINGS_H #ifndef COMMON_PYBINDINGS_H
#define COMMON_PYBINDINGS_H #define COMMON_PYBINDINGS_H
#include <utility>
#include <stdexcept> #include "pycontainers.h"
#include <Python.h>
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp> #include <stdexcept>
#include <boost/python/suite/indexing/map_indexing_suite.hpp> #include <utility>
using namespace boost::python; using namespace boost::python;
/*
A wrapper for a Pythonised nextpnr Iterator. The actual class wrapped is a
pair<Iterator, Iterator> containing (current, end)
*/
template<typename T>
struct iterator_wrapper {
typedef decltype(*(std::declval<T>())) value_t;
static value_t next(std::pair<T, T> &iter) {
if (iter.first != iter.second) {
value_t val = *iter.first;
++iter.first;
return val;
} else {
PyErr_SetString(PyExc_StopIteration, "End of range reached");
boost::python::throw_error_already_set();
// Should be unreachable, but prevent control may reach end of non-void
throw std::runtime_error("unreachable");
}
}
static void wrap(const char *python_name) {
class_<std::pair<T, T>>(python_name, no_init)
.def("__next__", next);
}
};
/* /*
A wrapper for a nextpnr Range. Ranges should have two functions, begin() A wrapper to enable custom type/ID to/from string conversions
and end() which return iterator-like objects supporting ++, * and !=
Full STL iterator semantics are not required, unlike the standard Boost wrappers
*/ */
template <typename T> struct string_wrapper
template<typename T> {
struct range_wrapper { template <typename F> struct from_pystring_converter
typedef decltype(std::declval<T>().begin()) iterator_t; {
from_pystring_converter()
static std::pair<iterator_t, iterator_t> iter(T &range) { {
return std::make_pair(range.begin(), range.end()); converter::registry::push_back(&convertible, &construct,
} boost::python::type_id<T>());
static void wrap(const char *range_name, const char *iter_name) {
class_<T>(range_name, no_init)
.def("__iter__", iter);
iterator_wrapper<iterator_t>().wrap(iter_name);
}
}; };
#define WRAP_RANGE(t) range_wrapper<t##Range>().wrap(#t "Range", #t "Iterator") static void *convertible(PyObject *object)
{
return PyUnicode_Check(object) ? object : 0;
}
static void construct(PyObject *object,
converter::rvalue_from_python_stage1_data *data)
{
const wchar_t *value = PyUnicode_AsUnicode(object);
const std::wstring value_ws(value);
if (value == 0)
throw_error_already_set();
void *storage =
((boost::python::converter::rvalue_from_python_storage<T> *)
data)
->storage.bytes;
new (storage) T(fn(std::string(value_ws.begin(), value_ws.end())));
data->convertible = storage;
}
static F fn;
};
template <typename F> struct to_str_wrapper
{
static F fn;
std::string str(T &x) { return fn(x); }
};
template <typename F1, typename F2>
static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn)
{
from_pystring_converter<F2>::fn = from_str_fn;
from_pystring_converter<F2>();
to_str_wrapper<F1>::fn = to_str_fn;
class_<T>(type_name, no_init).def("__str__", to_str_wrapper<F1>::str);
};
};
void execute_python_file(const char *executable, const char* python_file);
std::string parse_python_exception(); std::string parse_python_exception();
template <typename Tn> void python_export_global(const char *name, Tn &x)
{
PyObject *m, *d;
m = PyImport_AddModule("__main__");
if (m == NULL)
return;
d = PyModule_GetDict(m);
try {
PyObject *p = incref(object(boost::ref(x)).ptr());
PyDict_SetItemString(d, name, p);
} catch (boost::python::error_already_set const &) {
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
std::terminate();
}
};
void init_python(const char *executable);
void deinit_python();
void execute_python_file(const char *python_file);
std::string parse_python_exception();
void arch_appendinittab(); void arch_appendinittab();
#endif /* end of include guard: COMMON_PYBINDINGS_HH */ #endif /* end of include guard: COMMON_PYBINDINGS_HH */

129
common/pycontainers.h Normal file
View File

@ -0,0 +1,129 @@
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2018 Clifford Wolf <clifford@clifford.at>
* Copyright (C) 2018 David Shah <dave@ds0.me>
*
* 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 COMMON_PYCONTAINERS_H
#define COMMON_PYCONTAINERS_H
#include <utility>
#include <stdexcept>
#include <type_traits>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
using namespace boost::python;
/*
A wrapper for a Pythonised nextpnr Iterator. The actual class wrapped is a
pair<Iterator, Iterator> containing (current, end)
*/
template<typename T, typename P>
struct iterator_wrapper {
typedef decltype(*(std::declval<T>())) value_t;
static value_t next(std::pair <T, T> &iter) {
if (iter.first != iter.second) {
value_t val = *iter.first;
++iter.first;
return val;
} else {
PyErr_SetString(PyExc_StopIteration, "End of range reached");
boost::python::throw_error_already_set();
// Should be unreachable, but prevent control may reach end of non-void
throw std::runtime_error("unreachable");
}
}
static void wrap(const char *python_name) {
class_ < std::pair < T, T >> (python_name, no_init)
.def("__next__", next, P());
}
};
/*
A wrapper for a nextpnr Range. Ranges should have two functions, begin()
and end() which return iterator-like objects supporting ++, * and !=
Full STL iterator semantics are not required, unlike the standard Boost wrappers
*/
template<typename T, typename P = return_value_policy<return_by_value>>
struct range_wrapper {
typedef decltype(std::declval<T>().begin()) iterator_t;
static std::pair <iterator_t, iterator_t> iter(T &range) {
return std::make_pair(range.begin(), range.end());
}
static void wrap(const char *range_name, const char *iter_name) {
class_<T>(range_name, no_init)
.def("__iter__", iter);
iterator_wrapper<iterator_t, P>().wrap(iter_name);
}
typedef iterator_wrapper<iterator_t, P> iter_wrap;
};
#define WRAP_RANGE(t) range_wrapper<t##Range>().wrap(#t "Range", #t "Iterator")
/*
Wrapper for a map, either an unordered_map, regular map or dict
*/
inline void KeyError() { PyErr_SetString(PyExc_KeyError, "Key not found"); }
template<typename T>
struct map_wrapper {
typedef typename std::remove_cv<typename std::remove_reference<typename T::key_type>::type>::type K;
typedef typename T::mapped_type V;
typedef typename T::value_type KV;
static V &get(T &x, K const &i) {
if (x.find(i) != x.end()) return x.at(i);
KeyError();
std::terminate();
}
static void set(T &x, K const &i, V const &v) {
x[i] = v;
}
static void del(T const &x, K const &i) {
if (x.find(i) != x.end()) x.erase(i);
else KeyError();
std::terminate();
}
static void wrap(const char *map_name, const char *kv_name, const char *iter_name) {
class_<KV>(kv_name)
.def_readonly("first", &KV::first)
.def_readwrite("second", &KV::second);
typedef range_wrapper<T, return_value_policy<copy_non_const_reference>> rw;
typename rw::iter_wrap().wrap(iter_name);
class_<T>(map_name, no_init)
.def("__iter__", rw::iter)
.def("__len__", &T::size)
.def("__getitem__", get, return_internal_reference<>())
.def("__setitem__", set, with_custodian_and_ward<1,2>());
}
};
#define WRAP_MAP(t, name) map_wrapper<t>().wrap(#name, #name "KeyValue", #name "Iterator")
#endif

View File

@ -57,5 +57,6 @@ bool check_all_nets_driven(Design *design) {
} }
if (debug) log_info(" Verified!\n"); if (debug) log_info(" Verified!\n");
return true;
} }

View File

@ -19,6 +19,4 @@
#include "chip.h" #include "chip.h"
Chip::Chip(ChipArgs) Chip::Chip(ChipArgs) {}
{
}

View File

@ -17,9 +17,9 @@
* *
*/ */
#include <QApplication>
#include "design.h" #include "design.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View File

@ -18,10 +18,10 @@
* *
*/ */
#include "design.h"
#include "chip.h" #include "chip.h"
#include "design.h"
// include after design.h/chip.h
#include "pybindings.h" #include "pybindings.h"
void arch_wrap_python() { void arch_wrap_python() { class_<ChipArgs>("ChipArgs"); }
class_<ChipArgs>("ChipArgs");
}

View File

@ -11,9 +11,6 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow) ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
emb::append_inittab();
arch_appendinittab();
Py_Initialize();
PyImport_ImportModule("emb"); PyImport_ImportModule("emb");
write = [this] (std::string s) { write = [this] (std::string s) {
@ -27,7 +24,6 @@ MainWindow::MainWindow(QWidget *parent) :
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
Py_Finalize();
delete ui; delete ui;
} }

View File

@ -47,7 +47,9 @@ BelType belTypeFromId(IdString id)
IdString PortPinToId(PortPin type) IdString PortPinToId(PortPin type)
{ {
#define X(t) if (type == PIN_##t) return #t; #define X(t) \
if (type == PIN_##t) \
return #t;
X(IN_0) X(IN_0)
X(IN_1) X(IN_1)
@ -161,7 +163,9 @@ IdString PortPinToId(PortPin type)
PortPin PortPinFromId(IdString id) PortPin PortPinFromId(IdString id)
{ {
#define X(t) if (id == #t) return PIN_##t; #define X(t) \
if (id == #t) \
return PIN_##t;
X(IN_0) X(IN_0)
X(IN_1) X(IN_1)
@ -277,6 +281,12 @@ PortPin PortPinFromId(IdString id)
Chip::Chip(ChipArgs args) Chip::Chip(ChipArgs args)
{ {
#ifdef ICE40_HX1K_ONLY
if (args.type == ChipArgs::HX1K) {
chip_info = chip_info_1k;
return;
}
#else
if (args.type == ChipArgs::LP384) { if (args.type == ChipArgs::LP384) {
chip_info = chip_info_384; chip_info = chip_info_384;
return; return;
@ -293,6 +303,7 @@ Chip::Chip(ChipArgs args)
fprintf(stderr, "Unsupported chip type\n"); fprintf(stderr, "Unsupported chip type\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#endif
abort(); abort();
} }
@ -394,15 +405,17 @@ vector<GraphicElement> Chip::getBelGraphics(BelId bel) const
el.type = GraphicElement::G_BOX; el.type = GraphicElement::G_BOX;
el.x1 = chip_info.bel_data[bel.index].x + 0.1; el.x1 = chip_info.bel_data[bel.index].x + 0.1;
el.x2 = chip_info.bel_data[bel.index].x + 0.9; el.x2 = chip_info.bel_data[bel.index].x + 0.9;
el.y1 = chip_info.bel_data[bel.index].y + 0.10 + (chip_info.bel_data[bel.index].z) * (0.8/8); el.y1 = chip_info.bel_data[bel.index].y + 0.10 +
el.y2 = chip_info.bel_data[bel.index].y + 0.18 + (chip_info.bel_data[bel.index].z) * (0.8/8); (chip_info.bel_data[bel.index].z) * (0.8 / 8);
el.y2 = chip_info.bel_data[bel.index].y + 0.18 +
(chip_info.bel_data[bel.index].z) * (0.8 / 8);
el.z = 0; el.z = 0;
ret.push_back(el); ret.push_back(el);
} }
if (bel_type == TYPE_SB_IO) { if (bel_type == TYPE_SB_IO) {
if (chip_info.bel_data[bel.index].x == 0 || chip_info.bel_data[bel.index].x == chip_info.width-1) if (chip_info.bel_data[bel.index].x == 0 ||
{ chip_info.bel_data[bel.index].x == chip_info.width - 1) {
GraphicElement el; GraphicElement el;
el.type = GraphicElement::G_BOX; el.type = GraphicElement::G_BOX;
el.x1 = chip_info.bel_data[bel.index].x + 0.1; el.x1 = chip_info.bel_data[bel.index].x + 0.1;
@ -416,9 +429,7 @@ vector<GraphicElement> Chip::getBelGraphics(BelId bel) const
} }
el.z = 0; el.z = 0;
ret.push_back(el); ret.push_back(el);
} } else {
else
{
GraphicElement el; GraphicElement el;
el.type = GraphicElement::G_BOX; el.type = GraphicElement::G_BOX;
if (chip_info.bel_data[bel.index].z == 0) { if (chip_info.bel_data[bel.index].z == 0) {
@ -468,8 +479,7 @@ vector<GraphicElement> Chip::getFrameGraphics() const
vector<GraphicElement> ret; vector<GraphicElement> ret;
for (int x = 0; x <= chip_info.width; x++) for (int x = 0; x <= chip_info.width; x++)
for (int y = 0; y <= chip_info.height; y++) for (int y = 0; y <= chip_info.height; y++) {
{
GraphicElement el; GraphicElement el;
el.type = GraphicElement::G_LINE; el.type = GraphicElement::G_LINE;
el.x1 = x - 0.05, el.x2 = x + 0.05, el.y1 = y, el.y2 = y, el.z = 0; el.x1 = x - 0.05, el.x2 = x + 0.05, el.y1 = y, el.y2 = y, el.z = 0;

View File

@ -159,7 +159,6 @@ PortPin PortPinFromId(IdString id);
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
struct BelInfoPOD struct BelInfoPOD
{ {
const char *name; const char *name;
@ -213,9 +212,7 @@ struct BelId
{ {
int32_t index = -1; int32_t index = -1;
bool nil() const { bool nil() const { return index < 0; }
return index < 0;
}
bool operator==(const BelId &other) const { return index == other.index; } bool operator==(const BelId &other) const { return index == other.index; }
bool operator!=(const BelId &other) const { return index != other.index; } bool operator!=(const BelId &other) const { return index != other.index; }
@ -225,9 +222,7 @@ struct WireId
{ {
int32_t index = -1; int32_t index = -1;
bool nil() const { bool nil() const { return index < 0; }
return index < 0;
}
bool operator==(const WireId &other) const { return index == other.index; } bool operator==(const WireId &other) const { return index == other.index; }
bool operator!=(const WireId &other) const { return index != other.index; } bool operator!=(const WireId &other) const { return index != other.index; }
@ -237,9 +232,7 @@ struct PipId
{ {
int32_t index = -1; int32_t index = -1;
bool nil() const { bool nil() const { return index < 0; }
return index < 0;
}
bool operator==(const PipId &other) const { return index == other.index; } bool operator==(const PipId &other) const { return index == other.index; }
bool operator!=(const PipId &other) const { return index != other.index; } bool operator!=(const PipId &other) const { return index != other.index; }
@ -251,8 +244,7 @@ struct BelPin
PortPin pin; PortPin pin;
}; };
namespace std namespace std {
{
template <> struct hash<BelId> template <> struct hash<BelId>
{ {
std::size_t operator()(const BelId &bel) const noexcept std::size_t operator()(const BelId &bel) const noexcept
@ -285,9 +277,13 @@ struct BelIterator
int cursor; int cursor;
void operator++() { cursor++; } void operator++() { cursor++; }
bool operator!=(const BelIterator &other) const { return cursor != other.cursor; } bool operator!=(const BelIterator &other) const
{
return cursor != other.cursor;
}
BelId operator*() const { BelId operator*() const
{
BelId ret; BelId ret;
ret.index = cursor; ret.index = cursor;
return ret; return ret;
@ -308,9 +304,13 @@ struct BelPinIterator
BelPortPOD *ptr = nullptr; BelPortPOD *ptr = nullptr;
void operator++() { ptr++; } void operator++() { ptr++; }
bool operator!=(const BelPinIterator &other) const { return ptr != other.ptr; } bool operator!=(const BelPinIterator &other) const
{
return ptr != other.ptr;
}
BelPin operator*() const { BelPin operator*() const
{
BelPin ret; BelPin ret;
ret.bel.index = ptr->bel_index; ret.bel.index = ptr->bel_index;
ret.pin = ptr->port; ret.pin = ptr->port;
@ -332,9 +332,13 @@ struct WireIterator
int cursor = -1; int cursor = -1;
void operator++() { cursor++; } void operator++() { cursor++; }
bool operator!=(const WireIterator &other) const { return cursor != other.cursor; } bool operator!=(const WireIterator &other) const
{
return cursor != other.cursor;
}
WireId operator*() const { WireId operator*() const
{
WireId ret; WireId ret;
ret.index = cursor; ret.index = cursor;
return ret; return ret;
@ -355,9 +359,13 @@ struct AllPipIterator
int cursor = -1; int cursor = -1;
void operator++() { cursor++; } void operator++() { cursor++; }
bool operator!=(const AllPipIterator &other) const { return cursor != other.cursor; } bool operator!=(const AllPipIterator &other) const
{
return cursor != other.cursor;
}
PipId operator*() const { PipId operator*() const
{
PipId ret; PipId ret;
ret.index = cursor; ret.index = cursor;
return ret; return ret;
@ -378,9 +386,13 @@ struct PipIterator
int *cursor = nullptr; int *cursor = nullptr;
void operator++() { cursor++; } void operator++() { cursor++; }
bool operator!=(const PipIterator &other) const { return cursor != other.cursor; } bool operator!=(const PipIterator &other) const
{
return cursor != other.cursor;
}
PipId operator*() const { PipId operator*() const
{
PipId ret; PipId ret;
ret.index = *cursor; ret.index = *cursor;
return ret; return ret;
@ -398,7 +410,8 @@ struct PipRange
struct ChipArgs struct ChipArgs
{ {
enum { enum
{
NONE, NONE,
LP384, LP384,
LP1K, LP1K,
@ -429,17 +442,11 @@ struct Chip
return chip_info.bel_data[bel.index].name; return chip_info.bel_data[bel.index].name;
} }
void bindBel(BelId bel, IdString cell) void bindBel(BelId bel, IdString cell) {}
{
}
void unbindBel(BelId bel) void unbindBel(BelId bel) {}
{
}
bool checkBelAvail(BelId bel) const bool checkBelAvail(BelId bel) const {}
{
}
BelRange getBels() const BelRange getBels() const
{ {
@ -477,7 +484,8 @@ struct Chip
assert(!wire.nil()); assert(!wire.nil());
if (chip_info.wire_data[wire.index].bel_uphill.bel_index >= 0) { if (chip_info.wire_data[wire.index].bel_uphill.bel_index >= 0) {
ret.bel.index = chip_info.wire_data[wire.index].bel_uphill.bel_index; ret.bel.index =
chip_info.wire_data[wire.index].bel_uphill.bel_index;
ret.pin = chip_info.wire_data[wire.index].bel_uphill.port; ret.pin = chip_info.wire_data[wire.index].bel_uphill.port;
} }
@ -489,7 +497,8 @@ struct Chip
BelPinRange range; BelPinRange range;
assert(!wire.nil()); assert(!wire.nil());
range.b.ptr = chip_info.wire_data[wire.index].bels_downhill; range.b.ptr = chip_info.wire_data[wire.index].bels_downhill;
range.e.ptr = range.b.ptr + chip_info.wire_data[wire.index].num_bels_downhill; range.e.ptr =
range.b.ptr + chip_info.wire_data[wire.index].num_bels_downhill;
return range; return range;
} }
@ -503,17 +512,11 @@ struct Chip
return chip_info.wire_data[wire.index].name; return chip_info.wire_data[wire.index].name;
} }
void bindWire(WireId bel, IdString net) void bindWire(WireId bel, IdString net) {}
{
}
void unbindWire(WireId bel) void unbindWire(WireId bel) {}
{
}
bool checkWireAvail(WireId bel) const bool checkWireAvail(WireId bel) const {}
{
}
WireRange getWires() const WireRange getWires() const
{ {
@ -530,22 +533,18 @@ struct Chip
IdString getPipName(PipId pip) const IdString getPipName(PipId pip) const
{ {
assert(!pip.nil()); assert(!pip.nil());
std::string src_name = chip_info.wire_data[chip_info.pip_data[pip.index].src].name; std::string src_name =
std::string dst_name = chip_info.wire_data[chip_info.pip_data[pip.index].dst].name; chip_info.wire_data[chip_info.pip_data[pip.index].src].name;
std::string dst_name =
chip_info.wire_data[chip_info.pip_data[pip.index].dst].name;
return src_name + "->" + dst_name; return src_name + "->" + dst_name;
} }
void bindPip(PipId bel, IdString net) void bindPip(PipId bel, IdString net) {}
{
}
void unbindPip(PipId bel) void unbindPip(PipId bel) {}
{
}
bool checkPipAvail(PipId bel) const bool checkPipAvail(PipId bel) const {}
{
}
AllPipRange getPips() const AllPipRange getPips() const
{ {
@ -584,7 +583,8 @@ struct Chip
PipRange range; PipRange range;
assert(!wire.nil()); assert(!wire.nil());
range.b.cursor = chip_info.wire_data[wire.index].pips_downhill; range.b.cursor = chip_info.wire_data[wire.index].pips_downhill;
range.e.cursor = range.b.cursor + chip_info.wire_data[wire.index].num_downhill; range.e.cursor =
range.b.cursor + chip_info.wire_data[wire.index].num_downhill;
return range; return range;
} }
@ -593,7 +593,8 @@ struct Chip
PipRange range; PipRange range;
assert(!wire.nil()); assert(!wire.nil());
range.b.cursor = chip_info.wire_data[wire.index].pips_uphill; range.b.cursor = chip_info.wire_data[wire.index].pips_uphill;
range.e.cursor = range.b.cursor + chip_info.wire_data[wire.index].num_uphill; range.e.cursor =
range.b.cursor + chip_info.wire_data[wire.index].num_uphill;
return range; return range;
} }

View File

@ -1,4 +1,12 @@
if(ICE40_HX1K_ONLY)
set(devices 1k)
foreach (target ${family_targets})
target_compile_definitions(${target} PRIVATE ICE40_HX1K_ONLY=1)
endforeach (target)
else()
set(devices 384 1k 5k 8k) set(devices 384 1k 5k 8k)
endif()
set(DB_PY ${CMAKE_CURRENT_SOURCE_DIR}/ice40/chipdb.py) set(DB_PY ${CMAKE_CURRENT_SOURCE_DIR}/ice40/chipdb.py)
file(MAKE_DIRECTORY ice40/chipdbs/) file(MAKE_DIRECTORY ice40/chipdbs/)
add_library(ice40_chipdb OBJECT ice40/chipdbs/) add_library(ice40_chipdb OBJECT ice40/chipdbs/)

View File

@ -16,15 +16,15 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* *
*/ */
#include "design.h"
#include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <iostream>
#include <fstream>
#include "version.h"
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include "pybindings.h" #include <fstream>
#include <iostream>
#include "design.h"
#include "jsonparse.h" #include "jsonparse.h"
#include "mainwindow.h"
#include "pybindings.h"
#include "version.h"
void svg_dump_el(const GraphicElement &el) void svg_dump_el(const GraphicElement &el)
{ {
@ -32,20 +32,24 @@ void svg_dump_el(const GraphicElement &el)
std::string style = "stroke=\"black\" stroke-width=\"0.1\" fill=\"none\""; std::string style = "stroke=\"black\" stroke-width=\"0.1\" fill=\"none\"";
if (el.type == GraphicElement::G_BOX) { if (el.type == GraphicElement::G_BOX) {
std::cout << "<rect x=\"" << (offset + scale*el.x1) << "\" y=\"" << (offset + scale*el.y1) << std::cout << "<rect x=\"" << (offset + scale * el.x1) << "\" y=\""
"\" height=\"" << (scale*(el.y2-el.y1)) << "\" width=\"" << (scale*(el.x2-el.x1)) << "\" " << style << "/>\n"; << (offset + scale * el.y1) << "\" height=\""
<< (scale * (el.y2 - el.y1)) << "\" width=\""
<< (scale * (el.x2 - el.x1)) << "\" " << style << "/>\n";
} }
if (el.type == GraphicElement::G_LINE) { if (el.type == GraphicElement::G_LINE) {
std::cout << "<line x1=\"" << (offset + scale*el.x1) << "\" y1=\"" << (offset + scale*el.y1) << std::cout << "<line x1=\"" << (offset + scale * el.x1) << "\" y1=\""
"\" x2=\"" << (offset + scale*el.x2) << "\" y2=\"" << (offset + scale*el.y2) << "\" " << style << "/>\n"; << (offset + scale * el.y1) << "\" x2=\""
<< (offset + scale * el.x2) << "\" y2=\""
<< (offset + scale * el.y2) << "\" " << style << "/>\n";
} }
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
namespace po = boost::program_options; namespace po = boost::program_options;
int rc = 0;
std::string str; std::string str;
po::options_description options("Allowed options"); po::options_description options("Allowed options");
@ -53,8 +57,10 @@ int main(int argc, char *argv[])
options.add_options()("test", "just a check"); options.add_options()("test", "just a check");
options.add_options()("gui", "start gui"); options.add_options()("gui", "start gui");
options.add_options()("svg", "dump SVG file"); options.add_options()("svg", "dump SVG file");
options.add_options()("file", po::value<std::string>(), "python file to execute"); options.add_options()("run", po::value<std::vector<std::string>>(),
options.add_options()("json", po::value<std::string>(), "JSON design file to ingest"); "python file to execute");
options.add_options()("json", po::value<std::string>(),
"JSON design file to ingest");
options.add_options()("version,v", "show version"); options.add_options()("version,v", "show version");
options.add_options()("lp384", "set device type to iCE40LP384"); options.add_options()("lp384", "set device type to iCE40LP384");
options.add_options()("lp1k", "set device type to iCE40LP1K"); options.add_options()("lp1k", "set device type to iCE40LP1K");
@ -64,76 +70,96 @@ int main(int argc, char *argv[])
options.add_options()("up5k", "set device type to iCE40UP5K"); options.add_options()("up5k", "set device type to iCE40UP5K");
po::positional_options_description pos; po::positional_options_description pos;
pos.add("file", -1); pos.add("run", -1);
po::variables_map vm; po::variables_map vm;
try { try {
po::parsed_options parsed = po::command_line_parser(argc, argv). po::parsed_options parsed = po::command_line_parser(argc, argv)
options(options). .options(options)
positional(pos). .positional(pos)
run(); .run();
po::store(parsed, vm); po::store(parsed, vm);
po::notify(vm); po::notify(vm);
} }
catch(std::exception& e) catch (std::exception &e) {
{
std::cout << e.what() << "\n"; std::cout << e.what() << "\n";
return 1; return 1;
} }
if (vm.count("help") || argc == 1) if (vm.count("help") || argc == 1) {
{ help:
std::cout << basename(argv[0]) << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n"; std::cout << basename(argv[0])
<< " -- Next Generation Place and Route (git "
"sha1 " GIT_COMMIT_HASH_STR ")\n";
std::cout << "\n"; std::cout << "\n";
std::cout << options << "\n"; std::cout << options << "\n";
return 1; return argc != 1;
} }
if (vm.count("version")) if (vm.count("version")) {
{
std::cout << basename(argv[0]) std::cout << basename(argv[0])
<< " -- Next Generation Place and Route (git sha1 " << " -- Next Generation Place and Route (git "
GIT_COMMIT_HASH_STR ")\n"; "sha1 " GIT_COMMIT_HASH_STR ")\n";
return 1; return 1;
} }
ChipArgs chipArgs; ChipArgs chipArgs;
chipArgs.type = ChipArgs::HX1K;
if (vm.count("lp384")) if (vm.count("lp384")) {
if (chipArgs.type != ChipArgs::NONE)
goto help;
chipArgs.type = ChipArgs::LP384; chipArgs.type = ChipArgs::LP384;
if (vm.count("lp1k"))
chipArgs.type = ChipArgs::LP1K;
if (vm.count("lp8k"))
chipArgs.type = ChipArgs::LP8K;
if (vm.count("hx1k"))
chipArgs.type = ChipArgs::HX1K;
if (vm.count("hx8k"))
chipArgs.type = ChipArgs::HX8K;
if (vm.count("up5k"))
chipArgs.type = ChipArgs::UP5K;
Design design(chipArgs);
if (vm.count("gui"))
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
} }
if (vm.count("test")) if (vm.count("lp1k")) {
{ if (chipArgs.type != ChipArgs::NONE)
goto help;
chipArgs.type = ChipArgs::LP1K;
}
if (vm.count("lp8k")) {
if (chipArgs.type != ChipArgs::NONE)
goto help;
chipArgs.type = ChipArgs::LP8K;
}
if (vm.count("hx1k")) {
if (chipArgs.type != ChipArgs::NONE)
goto help;
chipArgs.type = ChipArgs::HX1K;
}
if (vm.count("hx8k")) {
if (chipArgs.type != ChipArgs::NONE)
goto help;
chipArgs.type = ChipArgs::HX8K;
}
if (vm.count("up5k")) {
if (chipArgs.type != ChipArgs::NONE)
goto help;
chipArgs.type = ChipArgs::UP5K;
}
if (chipArgs.type == ChipArgs::NONE)
chipArgs.type = ChipArgs::HX1K;
#ifdef ICE40_HX1K_ONLY
if (chipArgs.type != ChipArgs::HX1K) {
std::cout << "This version of nextpnr-ice40 is built with HX1K-support "
"only.\n";
return 1;
}
#endif
Design design(chipArgs);
init_python(argv[0]);
python_export_global("design", design);
if (vm.count("test")) {
int bel_count = 0, wire_count = 0, pip_count = 0; int bel_count = 0, wire_count = 0, pip_count = 0;
std::cout << "Checking bel names.\n"; std::cout << "Checking bel names.\n";
@ -164,7 +190,8 @@ int main(int argc, char *argv[])
for (auto dst : design.chip.getWires()) { for (auto dst : design.chip.getWires()) {
for (auto uphill_pip : design.chip.getPipsUphill(dst)) { for (auto uphill_pip : design.chip.getPipsUphill(dst)) {
bool found_downhill = false; bool found_downhill = false;
for (auto downhill_pip : design.chip.getPipsDownhill(design.chip.getPipSrcWire(uphill_pip))) { for (auto downhill_pip : design.chip.getPipsDownhill(
design.chip.getPipSrcWire(uphill_pip))) {
if (uphill_pip == downhill_pip) { if (uphill_pip == downhill_pip) {
assert(!found_downhill); assert(!found_downhill);
found_downhill = true; found_downhill = true;
@ -178,7 +205,8 @@ int main(int argc, char *argv[])
for (auto dst : design.chip.getWires()) { for (auto dst : design.chip.getWires()) {
for (auto downhill_pip : design.chip.getPipsDownhill(dst)) { for (auto downhill_pip : design.chip.getPipsDownhill(dst)) {
bool found_uphill = false; bool found_uphill = false;
for (auto uphill_pip : design.chip.getPipsUphill(design.chip.getPipDstWire(downhill_pip))) { for (auto uphill_pip : design.chip.getPipsUphill(
design.chip.getPipDstWire(downhill_pip))) {
if (uphill_pip == downhill_pip) { if (uphill_pip == downhill_pip) {
assert(!found_uphill); assert(!found_uphill);
found_uphill = true; found_uphill = true;
@ -191,9 +219,9 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
if (vm.count("svg")) if (vm.count("svg")) {
{ std::cout << "<svg xmlns=\"http://www.w3.org/2000/svg\" "
std::cout << "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"; "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
for (auto bel : design.chip.getBels()) { for (auto bel : design.chip.getBels()) {
std::cout << "<!-- " << design.chip.getBelName(bel) << " -->\n"; std::cout << "<!-- " << design.chip.getBelName(bel) << " -->\n";
for (auto &el : design.chip.getBelGraphics(bel)) for (auto &el : design.chip.getBelGraphics(bel))
@ -205,19 +233,27 @@ int main(int argc, char *argv[])
std::cout << "</svg>\n"; std::cout << "</svg>\n";
} }
if (vm.count("json")) if (vm.count("json")) {
{
std::string filename = vm["json"].as<std::string>(); std::string filename = vm["json"].as<std::string>();
std::istream *f = new std::ifstream(filename); std::istream *f = new std::ifstream(filename);
parse_json_file(f, filename, &design); parse_json_file(f, filename, &design);
} }
if (vm.count("file")) if (vm.count("run")) {
{ std::vector<std::string> files =
std::string filename = vm["file"].as<std::string>(); vm["run"].as<std::vector<std::string>>();
execute_python_file(argv[0],filename.c_str()); for (auto filename : files)
execute_python_file(filename.c_str());
} }
return 0; if (vm.count("gui")) {
QApplication a(argc, argv);
MainWindow w;
w.show();
rc = a.exec();
}
deinit_python();
return rc;
} }

View File

@ -18,13 +18,15 @@
* *
*/ */
#include "design.h"
#include "chip.h" #include "chip.h"
#include "design.h"
// include after design.h/chip.h
#include "pybindings.h" #include "pybindings.h"
void arch_wrap_python() { void arch_wrap_python()
class_<ChipArgs>("ChipArgs") {
.def_readwrite("type", &ChipArgs::type); class_<ChipArgs>("ChipArgs").def_readwrite("type", &ChipArgs::type);
enum_<decltype(std::declval<ChipArgs>().type)>("iCE40Type") enum_<decltype(std::declval<ChipArgs>().type)>("iCE40Type")
.value("NONE", ChipArgs::NONE) .value("NONE", ChipArgs::NONE)

6
python/dump_design.py Normal file
View File

@ -0,0 +1,6 @@
# Run ./nextpnr-ice40 --json ice40/blinky.json --run python/dump_design.py
for cell in sorted(design.cells, key=lambda x: x.first):
print("Cell {} : {}".format(cell.first, cell.second.type))
for port in sorted(cell.second.ports, key=lambda x: x.first):
dir = (" <-- ", " --> ", " <-> ")[int(port.second.type)]
print(" {} {} {}".format(port.first, dir, port.second.net.name))

2
python/functions.py Normal file
View File

@ -0,0 +1,2 @@
def test_function():
print("Hello World!")