degate 0.1.1
Module.h
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #ifndef __MODULE_H__
00023 #define __MODULE_H__
00024 
00025 #include <map>
00026 #include <tr1/memory>
00027 
00028 #include <LogicModelObjectBase.h>
00029 #include <LogicModel.h>
00030 #include <boost/optional.hpp>
00031 
00032 namespace degate {
00033 
00034   
00035   /**
00036    * Implements a container to build up higher level entities.
00037    */
00038 
00039   class Module : public LogicModelObjectBase {
00040 
00041     friend void determine_module_ports_for_root(LogicModel_shptr lmodel);
00042     friend class LogicModelImporter;
00043 
00044   public:
00045 
00046     typedef std::set<Module_shptr> module_collection;
00047     typedef std::set<Gate_shptr> gate_collection;
00048 
00049     /**
00050      * This map defines module ports.
00051      * A port is identified by a name. A module port is 'connected' to
00052      * a list of gate ports.
00053      */
00054     typedef std::map<std::string, /* port name */
00055                      GatePort_shptr> port_collection;
00056 
00057   private:
00058 
00059     module_collection modules;
00060     gate_collection gates;
00061     port_collection ports;
00062     
00063     std::string entity_name; // name of a type
00064     bool is_root;
00065     
00066   private:
00067 
00068     /**
00069      * @throw InvalidPointerException This exception is thrown if the parameter is a NULL pointer.
00070      */
00071     void move_gates_recursive(Module * dst_mod);
00072 
00073     void automove_gates();
00074 
00075     /**
00076      * Check if there is a gate is the current module or any child module
00077      * that has a gate port with the object ID \p oid .
00078      */
00079     bool exists_gate_port_recursive(object_id_t oid) const;
00080     GatePort_shptr lookup_gate_port_recursive(object_id_t oid) const;
00081 
00082     
00083     void add_module_port(std::string const& module_port_name, GatePort_shptr adjacent_gate_port);
00084 
00085     bool net_feeded_internally(Net_shptr net) const;
00086     bool net_completely_internal(Net_shptr net) const;
00087 
00088   public:
00089 
00090     /**
00091      * Construct a new module.
00092      */
00093 
00094     Module(std::string const& module_name = "", 
00095            std::string const& _entity_name = "", 
00096            bool is_root = false);
00097 
00098     /**
00099      * Destroy the module.
00100      */
00101 
00102     virtual ~Module();
00103 
00104     /**
00105      * Check if module is the main module.
00106      */
00107     
00108     bool is_main_module() const;
00109 
00110     /**
00111      * Set a module as main module.
00112      * This will set the new state only in the current module and will not disable
00113      * the root-node-state of any other module.
00114      */
00115     void set_main_module();
00116 
00117     /**
00118      * Set an identifier for the module type.
00119      */
00120 
00121     void set_entity_name(std::string const& name);
00122 
00123 
00124     /**
00125      * Get name of the entity type.
00126      */
00127 
00128     std::string get_entity_name() const;
00129 
00130 
00131     /**
00132      * Add a gate to a module.
00133      * @param gate The gate to add.
00134      * @param detect_ports Switch for enabling or disabling automatic module port detection.
00135      * @exception InvalidPointerException This exception is thrown, if \p gate is a NULL pointer.
00136      */
00137 
00138     void add_gate(Gate_shptr gate, bool detect_ports = true);
00139 
00140 
00141     /**
00142      * Remove a gate from a module.
00143      * This method even works if the gate is not a direct child. Afterwards
00144      * the gate is completely removed from the module hierarchy (if you run this
00145      * method for the "right" root node). In most cases
00146      * you want to add the gate to the module hierarchy again. Therefore you have
00147      * to call method add_gate() on your own.
00148      * @return Returns true if a module was removed, else false.
00149      * @see add_gate()
00150      * @exception InvalidPointerException This exception is thrown, if \p gate is a NULL pointer.
00151      */
00152 
00153     bool remove_gate(Gate_shptr gate);
00154 
00155 
00156     /**
00157      * Add a sub-module to a module.
00158      * @exception InvalidPointerException This exception is thrown, if \p gate is a NULL pointer.
00159      */
00160 
00161     void add_module(Module_shptr module);
00162 
00163     /**
00164      * Remove a submodule.
00165      * This method even works if the submodule is not a direct child. If you remove
00166      * a child module that contains gates, the gates are moved one layer above.
00167      * @return Returns true if a module was removed, else false.
00168      * @exception InvalidPointerException This exception is thrown, if \p gate is a NULL pointer.
00169      */
00170 
00171     bool remove_module(Module_shptr module);
00172 
00173 
00174     module_collection::iterator modules_begin();
00175     module_collection::iterator modules_end();
00176     gate_collection::iterator gates_begin();
00177     gate_collection::iterator gates_end();
00178     port_collection::iterator ports_begin();
00179     port_collection::iterator ports_end();
00180 
00181     module_collection::const_iterator modules_begin() const;
00182     module_collection::const_iterator modules_end() const;
00183     gate_collection::const_iterator gates_begin() const;
00184     gate_collection::const_iterator gates_end() const;
00185     port_collection::const_iterator ports_begin() const;
00186     port_collection::const_iterator ports_end() const;
00187 
00188 
00189     /**
00190      * Determine ports of a module.
00191      */
00192     void determine_module_ports();
00193 
00194 
00195     /**
00196      * Lookup a sub-module.
00197      * @param module_path Path to the module, e.g. :
00198      *   an absolute path "main_module/crypto/lfsr_a"
00199      *   an relative path "crypto/lfsr_a"
00200      * @return Returns the module. In case of lookup failure a NULL pointer is returned.
00201      */
00202 
00203     Module_shptr lookup_module(std::string const& module_path) const;
00204 
00205     /**
00206      * Set module port name
00207      */
00208     void set_module_port_name(std::string const& module_port_name, GatePort_shptr adjacent_gate_port);
00209 
00210     /**
00211      * Check if a module port name exists.
00212      */
00213     bool exists_module_port_name(std::string const& module_port_name) const;
00214 
00215 
00216     /**
00217      * Lookup the module port name.
00218      * @return Returns the module port name.
00219      */
00220 
00221     boost::optional<std::string> lookup_module_port_name(GatePort_shptr gate_port);
00222 
00223   private:
00224 
00225     /**
00226      * Internal implementation of lookup_module().
00227      * @param path_elements Reference to a list of path elements. List is modified.
00228      * @return Returns a valid pointer on success. Else a null pointer is returned.
00229      */
00230     Module_shptr lookup_module(std::list<std::string> & path_elements) const;
00231 
00232   };
00233 
00234 
00235   /**
00236    * Determine ports of a root module.
00237    * Note: It would b more nice to have this function as a member function of class Module. But
00238    *  this would intorduce a dependency of LogicModel. The lmodel is neccessary for looking up
00239    *  objects, because main module's ports are modelled by having a special emarker in the net.
00240    *  I have no idea how the main module's ports could be identified else. (Using all ports
00241    *  is obviously not an option.)
00242    */
00243   void determine_module_ports_for_root(LogicModel_shptr lmodel);
00244 
00245 
00246 }
00247 
00248 #endif