5 #include "pybind11/operators.h" 
    6 #include "pybind11/pybind11.h" 
    7 #include "pybind11/stl.h" 
    8 #include "pybind11/stl_bind.h" 
   13 namespace py = pybind11;
 
   20 #ifdef PYBIND11_MODULE 
   23         m.doc() = 
"Plugin to automatically generate FSM state transition graphs for given FSMs.";
 
   27         py::module m(
"solve_fsm", 
"Plugin to automatically generate FSM state transition graphs for given FSMs.");
 
   30         py::class_<SolveFsmPlugin, RawPtrWrapper<SolveFsmPlugin>, 
BasePluginInterface> py_solve_fsm(
 
   31             m, 
"SolveFsmPlugin", R
"(This class provides an interface to integrate FSM solving as a plugin within the HAL framework.)"); 
   33             The name of the plugin. 
   39             Get the name of the plugin. 
   41             :returns: Plugin name. 
   46             The version of the plugin. 
   52             Get the version of the plugin. 
   54             :returns: Plugin version. 
   59             The description of the plugin. 
   65             Get the description of the plugin. 
   67             :returns: The description of the plugin. 
   72             "solve_fsm_brute_force",
 
   73             [](
Netlist* nl, 
const std::vector<Gate*>& state_reg, 
const std::vector<Gate*>& transition_logic, 
const std::string& 
graph_path = 
"")
 
   74                 -> std::optional<std::map<
u64, std::map<u64, BooleanFunction>>> {
 
   82                     log_error(
"python_context", 
"{}", res.get_error().get());
 
   88             py::arg(
"transition_logic"),
 
   89             py::arg(
"graph_path") = std::string(
""),
 
   91                 Generate the state transition graph of a given FSM using brute force. 
   92                 The result is a map from each state of the FSM to all of its transitions. 
   93                 A transition is given as each successor state as well as the Boolean condition that needs to be fulfilled for the transition to take place. 
   94                 Optionally also produces a DOT file representing the state transition graph. 
   96                 :param hal_py.Netlist nl: The netlist to operate on. 
   97                 :param list[hal_py.Gate] state_reg: A list of flip-flop gates that make up the state register of the FSM. 
   98                 :param list[hal_py.Gate] transition_logic: A list of combinational gates that make up the transition logic of the FSM. 
   99                 :param pathlib.Path graph_path: File path at which to store the DOT state transition graph. No file is created if the path is left empty. Defaults to an empty path. 
  100                 :returns: A dict from each state to its successor states as well as the condition for the respective transition to be taken on success, `None` otherwise. 
  101                 :rtype: dict[int,dict[int,hal_py.BooleanFunction]] or None 
  107                const std::vector<Gate*>& state_reg,
 
  108                const std::vector<Gate*>& transition_logic,
 
  111                const u32 timeout                          = 600000) -> std::optional<std::map<
u64, std::map<u64, BooleanFunction>>> {
 
  119                     log_error(
"python_context", 
"{}", res.get_error().get());
 
  124             py::arg(
"state_reg"),
 
  125             py::arg(
"transition_logic"),
 
  126             py::arg(
"initial_state") = std::map<Gate*, bool>(),
 
  127             py::arg(
"graph_path")    = 
"",
 
  128             py::arg(
"timeout")       = 600000,
 
  130                 Generate the state transition graph of a given FSM using SMT solving. 
  131                 The result is a map from each state of the FSM to all of its transitions. 
  132                 A transition is given as each successor state as well as the Boolean condition that needs to be fulfilled for the transition to take place. 
  133                 Optionally also produces a DOT file representing the state transition graph. 
  135                 :param hal_py.Netlist nl: The netlist to operate on. 
  136                 :param list[hal_py.Gate] state_reg: A list of flip-flop gates that make up the state register of the FSM. 
  137                 :param list[hal_py.Gate] transition_logic: A list of combinational gates that make up the transition logic of the FSM. 
  138                 :param dict[hal_py.Gate,bool] initial_state: A dict from the state register flip-flops to their initial (Boolean) value. If an empty dict is provided, the initial state is set to 0. Defaults to an empty dict. 
  139                 :param pathlib.Path graph_path: File path at which to store the DOT state transition graph. No file is created if the path is left empty. Defaults to an empty path. 
  140                 :param int timeout: Timeout for the underlying SAT solvers. Defaults to 600000 ms. 
  141                 :returns: A dict from each state to its successor states as well as the condition for the respective transition to be taken on success, `None` otherwise. 
  142                 :rtype: dict[int,dict[int,hal_py.BooleanFunction]] or None 
  146             "generate_dot_graph",
 
  147             [](
const std::vector<Gate*>& state_reg,
 
  148                const std::map<
u64, std::map<u64, BooleanFunction>>& transitions,
 
  150                const u32 max_condition_length = 128,
 
  151                const u32 base                 = 10) -> std::optional<std::string> {
 
  159                     log_error(
"python_context", 
"{}", res.get_error().get());
 
  163             py::arg(
"state_reg"),
 
  164             py::arg(
"transitions"),
 
  165             py::arg(
"graph_path")           = std::string(),
 
  166             py::arg(
"max_condition_length") = 128,
 
  167             py::arg(
"base")                 = 10,
 
  169             Generates the state graph of a finite state machine from the transitions of that fsm. 
  171             :param list[hal_py.Gate] state_reg: Vector contianing the state registers. 
  172             :param dict[int, dict[int, hal_py.BooleanFunction]] transitions: Transitions of the fsm given as a map from origin state to all possible successor states and the corresponding condition. 
  173             :param str graph_path: Path to the location where the state graph is saved in dot format. 
  174             :param int max_condition_length: The maximum character length that is printed for boolean functions representing the conditions. 
  175             :param int base: The base with that the states are formatted and printed. 
  176             :returns: A string representing the dot graph. 
  180 #ifndef PYBIND11_MODULE 
std::string get_name() const override
Get the name of the plugin.
std::string get_description() const override
Get a short description of the plugin.
std::string get_version() const override
Get the version of the plugin.
#define log_error(channel,...)
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< std::string > generate_dot_graph(const std::vector< Gate * > &state_reg, const std::map< u64, std::map< u64, BooleanFunction >> &transitions, const std::filesystem::path &graph_path="", const u32 max_condition_length=128, const u32 base=10)
Result< std::map< u64, std::map< u64, BooleanFunction > > > solve_fsm(Netlist *nl, const std::vector< Gate * > &state_reg, const std::vector< Gate * > &transition_logic, const std::map< Gate *, bool > &initial_state={}, const std::filesystem::path &graph_path="", const u32 timeout=600000)
Result< std::map< u64, std::map< u64, BooleanFunction > > > solve_fsm_brute_force(Netlist *nl, const std::vector< Gate * > &state_reg, const std::vector< Gate * > &transition_logic, const std::filesystem::path &graph_path="")
This file contains functions to generate the state transition graph of a given FSM.