2018-06-25 10:08:29 -05:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
|
|
|
|
#include "yosys.h"
|
|
|
|
#include <boost/python.hpp>
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
namespace YOSYS_PYTHON {
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
struct Design;
|
|
|
|
struct Module;
|
|
|
|
struct Cell;
|
|
|
|
struct Wire;
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
void yosys_setup()
|
|
|
|
{
|
|
|
|
Yosys::log_streams.push_back(&std::cout);
|
|
|
|
Yosys::log_error_stderr = true;
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
Yosys::yosys_setup();
|
|
|
|
Yosys::yosys_banner();
|
|
|
|
}
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
void run(std::string command)
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-06-28 08:07:21 -05:00
|
|
|
Yosys::run_pass(command);
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
2018-06-28 08:07:21 -05:00
|
|
|
|
|
|
|
void yosys_shutdown()
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-06-28 08:07:21 -05:00
|
|
|
Yosys::yosys_shutdown();
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
struct Cell
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
unsigned int id;
|
2018-06-28 08:07:21 -05:00
|
|
|
|
|
|
|
Cell(Yosys::RTLIL::Cell* ref)
|
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
this->id = ref->hashidx_;
|
2018-06-28 08:07:21 -05:00
|
|
|
}
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-07-09 08:48:06 -05:00
|
|
|
Yosys::RTLIL::Cell* get_cpp_obj() const
|
2018-06-28 08:07:21 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
return Yosys::RTLIL::Cell::get_all_cells()->at(this->id);
|
2018-06-28 08:07:21 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &ostr, const Cell &cell)
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
if(cell.get_cpp_obj() != NULL)
|
|
|
|
ostr << "Cell with name " << cell.get_cpp_obj()->name.c_str();
|
|
|
|
else
|
|
|
|
ostr << "deleted cell";
|
2018-06-28 08:07:21 -05:00
|
|
|
return ostr;
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
struct Wire
|
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
unsigned int id;
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
Wire(Yosys::RTLIL::Wire* ref)
|
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
this->id = ref->hashidx_;
|
2018-06-28 08:07:21 -05:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:48:06 -05:00
|
|
|
Yosys::RTLIL::Wire* get_cpp_obj() const
|
2018-06-28 08:07:21 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
return Yosys::RTLIL::Wire::get_all_wires()->at(this->id);
|
2018-06-28 08:07:21 -05:00
|
|
|
}
|
|
|
|
};
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
std::ostream &operator<<(std::ostream &ostr, const Wire &wire)
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
if(wire.get_cpp_obj() != NULL)
|
|
|
|
ostr << "Wire with name " << wire.get_cpp_obj()->name.c_str();
|
|
|
|
else
|
|
|
|
ostr << "deleted wire";
|
2018-06-28 08:07:21 -05:00
|
|
|
return ostr;
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
struct Module
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
unsigned int id;
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
Module(Yosys::RTLIL::Module* ref)
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
this->id = ref->hashidx_;
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:48:06 -05:00
|
|
|
Yosys::RTLIL::Module* get_cpp_obj() const
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
return Yosys::RTLIL::Module::get_all_modules()->at(this->id);
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
boost::python::list get_cells()
|
|
|
|
{
|
|
|
|
Yosys::RTLIL::Module* cpp_mod = this->get_cpp_obj();
|
|
|
|
boost::python::list result;
|
|
|
|
if(cpp_mod == NULL)
|
|
|
|
return result;
|
|
|
|
for(auto &cell_it : cpp_mod->cells_)
|
|
|
|
{
|
|
|
|
result.append(new Cell(cell_it.second));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
boost::python::list get_wires()
|
|
|
|
{
|
|
|
|
Yosys::RTLIL::Module* cpp_mod = this->get_cpp_obj();
|
|
|
|
boost::python::list result;
|
|
|
|
if(cpp_mod == NULL)
|
|
|
|
return result;
|
|
|
|
for(auto &wire_it : cpp_mod->wires_)
|
|
|
|
{
|
|
|
|
result.append(new Wire(wire_it.second));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
std::ostream &operator<<(std::ostream &ostr, const Module &module)
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
if(module.get_cpp_obj() != NULL)
|
|
|
|
ostr << "Module with name " << module.get_cpp_obj()->name.c_str();
|
|
|
|
else
|
|
|
|
ostr << "deleted module";
|
2018-06-28 08:07:21 -05:00
|
|
|
return ostr;
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
struct Design
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-06-28 08:07:21 -05:00
|
|
|
unsigned int hashid;
|
|
|
|
|
|
|
|
Design(unsigned int hashid)
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-06-28 08:07:21 -05:00
|
|
|
this->hashid = hashid;
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
Design()
|
2018-06-25 10:08:29 -05:00
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
Yosys::RTLIL::Design* new_design = new Yosys::RTLIL::Design();
|
|
|
|
this->hashid = new_design->hashidx_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Yosys::RTLIL::Design* get_cpp_obj()
|
|
|
|
{
|
|
|
|
return Yosys::RTLIL::Design::get_all_designs()->at(hashid);
|
2018-06-25 10:08:29 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
boost::python::list get_modules()
|
|
|
|
{
|
2018-07-09 08:48:06 -05:00
|
|
|
Yosys::RTLIL::Design* cpp_design = get_cpp_obj();
|
2018-06-28 08:07:21 -05:00
|
|
|
boost::python::list result;
|
2018-07-09 08:48:06 -05:00
|
|
|
if(cpp_design == NULL)
|
|
|
|
{
|
2018-06-28 08:07:21 -05:00
|
|
|
return result;
|
2018-07-09 08:48:06 -05:00
|
|
|
}
|
|
|
|
for(auto &mod_it : cpp_design->modules_)
|
2018-06-28 08:07:21 -05:00
|
|
|
{
|
|
|
|
result.append(new Module(mod_it.second));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-07-09 09:01:56 -05:00
|
|
|
|
|
|
|
void run(std::string command)
|
|
|
|
{
|
|
|
|
Yosys::RTLIL::Design* cpp_design = get_cpp_obj();
|
|
|
|
if(cpp_design != NULL)
|
|
|
|
Yosys::run_pass(command, cpp_design);
|
|
|
|
}
|
2018-06-28 08:07:21 -05:00
|
|
|
};
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
std::ostream &operator<<(std::ostream &ostr, const Design &design)
|
|
|
|
{
|
|
|
|
ostr << "Design with id " << design.hashid;
|
|
|
|
return ostr;
|
|
|
|
}
|
2018-06-25 10:08:29 -05:00
|
|
|
|
2018-07-09 08:48:06 -05:00
|
|
|
unsigned int get_active_design_id()
|
|
|
|
{
|
|
|
|
Yosys::RTLIL::Design* active_design = Yosys::yosys_get_design();
|
|
|
|
if(active_design != NULL)
|
|
|
|
{
|
|
|
|
return active_design->hashidx_;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-28 08:07:21 -05:00
|
|
|
BOOST_PYTHON_MODULE(libyosys)
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
|
|
|
|
|
|
|
class_<Design>("Design", init<unsigned int>())
|
2018-07-09 09:01:56 -05:00
|
|
|
.def(boost::python::self_ns::str(boost::python::self_ns::self))
|
|
|
|
.def(boost::python::self_ns::repr(boost::python::self_ns::self))
|
2018-06-28 08:07:21 -05:00
|
|
|
.def(init<>())
|
|
|
|
.def("get_modules", &Design::get_modules)
|
2018-07-09 09:01:56 -05:00
|
|
|
.def("run",&Design::run)
|
2018-06-28 08:07:21 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
class_<Module>("Module", no_init)
|
|
|
|
.def(boost::python::self_ns::str(boost::python::self_ns::self))
|
|
|
|
.def(boost::python::self_ns::repr(boost::python::self_ns::self))
|
|
|
|
.def("get_cells", &Module::get_cells)
|
|
|
|
.def("get_wires", &Module::get_wires)
|
|
|
|
;
|
|
|
|
|
|
|
|
class_<Cell>("Cell", no_init)
|
|
|
|
.def(boost::python::self_ns::str(boost::python::self_ns::self))
|
|
|
|
.def(boost::python::self_ns::repr(boost::python::self_ns::self))
|
|
|
|
;
|
|
|
|
|
|
|
|
class_<Wire>("Wire", no_init)
|
|
|
|
.def(boost::python::self_ns::str(boost::python::self_ns::self))
|
|
|
|
.def(boost::python::self_ns::repr(boost::python::self_ns::self))
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
def("yosys_setup",yosys_setup);
|
|
|
|
def("run",run);
|
2018-07-09 08:48:06 -05:00
|
|
|
def("get_active_design_id",get_active_design_id);
|
2018-06-28 08:07:21 -05:00
|
|
|
def("yosys_shutdown",yosys_shutdown);
|
|
|
|
}
|
2018-06-25 10:08:29 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|