degate  0.1.2
LogicModel.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   Copyright 2012 Robert Nitsch
00007 
00008   Degate is free software: you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation, either version 3 of the License, or
00011   any later version.
00012 
00013   Degate is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   along with degate. If not, see <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifndef __LOGICMODEL_H__
00024 #define __LOGICMODEL_H__
00025 
00026 #include <globals.h>
00027 #include <LogicModelObjectBase.h>
00028 #include <PlacedLogicModelObject.h>
00029 #include <Net.h>
00030 #include <Layer.h>
00031 
00032 #include <Rectangle.h>
00033 
00034 #include <Via.h>
00035 #include <Wire.h>
00036 #include <DeepCopyable.h>
00037 #include <EMarker.h>
00038 #include <Gate.h>
00039 #include <GatePort.h>
00040 #include <GateTemplate.h>
00041 #include <GateTemplatePort.h>
00042 #include <GateLibrary.h>
00043 #include <Annotation.h>
00044 #include <Module.h>
00045 
00046 #include <memory>
00047 #include <set>
00048 #include <map>
00049 #include <sstream>
00050 #include <iostream>
00051 
00052 namespace degate {
00053 
00054   /**
00055    * This class represents the logic model.
00056    *
00057    * Note: This class is central in degate. So it tends to be somehow bloated.
00058    * Convenience methods should go into LogicModelHelper.h
00059    *
00060    * @todo implement a move_object
00061    */
00062   class LogicModel : public DeepCopyable {
00063 
00064   public:
00065 
00066     typedef std::map<object_id_t, PlacedLogicModelObject_shptr> object_collection;
00067     typedef std::map<object_id_t, Net_shptr> net_collection;
00068     typedef std::map<object_id_t, Annotation_shptr> annotation_collection;
00069     typedef std::map<object_id_t, Via_shptr > via_collection;
00070 
00071     typedef std::vector<Layer_shptr> layer_collection;
00072     typedef std::map<object_id_t, Gate_shptr > gate_collection;
00073     typedef std::map<object_id_t, Wire_shptr > wire_collection;
00074     typedef std::map<object_id_t, EMarker_shptr> emarker_collection;
00075     
00076   private:
00077 
00078     BoundingBox bounding_box;
00079 
00080     layer_collection layers; // x
00081     Layer_shptr current_layer;
00082 
00083     std::shared_ptr<GateLibrary> gate_library; // x
00084 
00085     gate_collection gates;
00086     wire_collection wires;
00087     via_collection vias;
00088     emarker_collection emarkers;
00089     annotation_collection annotations;
00090     net_collection nets;
00091     Module_shptr main_module;
00092 
00093     /**
00094      * Contains any placeable object.
00095      */
00096     object_collection objects;
00097 
00098 
00099     /**
00100      * Counter to generate new object IDs.
00101      */
00102     object_id_t object_id_counter;
00103 
00104 
00105     /**
00106      * List of remote objects, that were deleted from the logic model.
00107      */
00108     std::list<object_id_t> removed_remote_oids;
00109 
00110     typedef std::map<object_id_t, object_id_t> roid_mapping_t;
00111 
00112     /**
00113      * Mapping from remote OIDs to local OIDs.
00114      */
00115     roid_mapping_t roid_mapping;
00116 
00117     diameter_t port_diameter; 
00118 
00119   private:
00120 
00121     /**
00122      * Get a layer. Create the layer if it doesn't exists.
00123      * @see get_layer
00124      */
00125     Layer_shptr get_create_layer(layer_position_t pos);
00126 
00127     /**
00128      * Add a wire into the logic model. If the layer doesn't exists, the layer is created implicitly.
00129      * If the wire has no object ID, a new object ID for the wire is generated.
00130      * @param layer_pos The layer position (starting at 0).
00131      * @param o A shared pointer to the object.
00132      */
00133 
00134     void add_wire(int layer_pos, Wire_shptr o);
00135 
00136     /**
00137      * Add a via into the logic model. If the layer doesn't exists, the layer is created implicitly.
00138      * If the via has no object ID, a new object ID for the via is generated.
00139      * @param layer_pos The layer position (starting at 0).
00140      * @param o A shared pointer to the object.
00141      */
00142 
00143     void add_via(int layer_pos, Via_shptr o);
00144 
00145     /**
00146      * Add an emarker into the logic model. If the layer doesn't exists, the layer is created implicitly.
00147      * If the emarker has no object ID, a new object ID for the emarker is generated.
00148      * @param layer_pos The layer position (starting at 0).
00149      * @param o A shared pointer to the object.
00150      */
00151 
00152     void add_emarker(int layer_pos, EMarker_shptr o);
00153 
00154     /**
00155      * Add an annotation into the logic model. If the layer doesn't exists, the layer is created implicitly.
00156      * If the annotation has no object ID, a new object ID for the via is generated.
00157      * @param layer_pos The layer position (starting at 0).
00158      * @param o A shared pointer to the object.
00159      */
00160 
00161     void add_annotation(int layer_pos, Annotation_shptr o);
00162 
00163 
00164     /**
00165      * Add a gate into the logic model. If the layer doesn't exists, the layer is
00166      * created implicitly.
00167      * If the gate has no object ID, a new object ID for the gate is generated.
00168      * Port are implicitly added, too.
00169      * @param layer_pos The layer position (starting at 0).
00170      * @param o A shared pointer to the object.
00171      */
00172 
00173     void add_gate(int layer_pos, Gate_shptr o);
00174 
00175 
00176     /**
00177      * Remove all ports from the logic model for a given gate.
00178      * @param o A shared pointer to the object.
00179      */
00180 
00181     void remove_gate_ports(Gate_shptr o);
00182 
00183     /**
00184      * Remove a gate from the logic model.
00185      * This effects the module hierarchy, too.
00186      * @param o A shared pointer to the object.
00187      */
00188 
00189     void remove_gate(Gate_shptr o);
00190 
00191     /**
00192      * Remove a wire from the logic model.
00193      * @param o A shared pointer to the object.
00194      */
00195 
00196     void remove_wire(Wire_shptr o);
00197 
00198     /**
00199      * Remove a via from the logic model.
00200      * @param o A shared pointer to the object.
00201      */
00202 
00203     void remove_via(Via_shptr o);
00204 
00205     /**
00206      * Remove an emarker from the logic model.
00207      * @param o A shared pointer to the object.
00208      */
00209 
00210     void remove_emarker(EMarker_shptr o);
00211 
00212     /**
00213      * Remove an annotation from the logic model.
00214      * @param o A shared pointer to the object.
00215      */
00216 
00217     void remove_annotation(Annotation_shptr o);
00218 
00219 
00220     /**
00221      * Remove an onject from the logic model and control if the operation
00222      * should be remembered in delete log.
00223      */
00224     void remove_object(PlacedLogicModelObject_shptr o,  bool add_to_remove_list);
00225 
00226 
00227     /**
00228      * Create a new layer ID.
00229      */
00230     layer_id_t get_new_layer_id();
00231 
00232     bool exists_layer_id(layer_collection const& layers, layer_id_t lid) const;
00233 
00234   public:
00235 
00236     /**
00237      * The constructor for the logic model.
00238      */
00239 
00240     LogicModel(unsigned int width, unsigned int height, unsigned int layers = 0);
00241 
00242 
00243     /**
00244      * The destructor.
00245      */
00246 
00247     virtual ~LogicModel();
00248 
00249     //@{
00250     DeepCopyable_shptr cloneShallow() const;
00251     void cloneDeepInto(DeepCopyable_shptr destination, oldnew_t *oldnew) const;
00252     //@}
00253     
00254     /**
00255      * Get the width of logic model.
00256      */
00257 
00258     unsigned int get_width() const;
00259 
00260     /**
00261      * Get the height of logic model.
00262      */
00263 
00264     unsigned int get_height() const;
00265 
00266 
00267     /**
00268      * Get a new unique logic model object ID.
00269      */
00270 
00271     object_id_t get_new_object_id();
00272 
00273 
00274     /**
00275      * Lookup an object from the logic model for a given object ID.
00276      * @exception CollectionLookupException Is thrown if there is
00277      *   no logic model object with that object ID.
00278      * @return Returns a shared pointer to the logic model.
00279      */
00280 
00281     PlacedLogicModelObject_shptr get_object(object_id_t object_id);
00282 
00283 
00284     /**
00285      * Add a generic logic model object into the logic model. If the layer doesn't
00286      * exists, the layer is created implicitly.
00287      * If the object has no object ID, a new object ID is generated.
00288      *
00289      * This method will set a reference to the layer, where the object is placed,
00290      * into the object, such that each placed object knows, where on which
00291      * layer it is placed.
00292      *
00293      * @param layer_pos The layer position (starting at 0).
00294      * @param o A shared pointer to the object.
00295      * @exception DegateLogicException This exception is thrown, if an object with the
00296      *            same object ID is already in the logic model.
00297      */
00298 
00299     void add_object(int layer_pos, PlacedLogicModelObject_shptr o);
00300 
00301     void add_object(Layer_shptr layer, PlacedLogicModelObject_shptr o) {
00302       add_object(layer->get_layer_pos(), o);
00303     }
00304 
00305 
00306     /**
00307      * Remove a generic logic model object from the logic model.
00308      * @param o A shared pointer to the object.
00309      * @todo fix it
00310      */
00311 
00312     void remove_object(PlacedLogicModelObject_shptr o);
00313 
00314     /**
00315      * Remove a remote object.
00316      * @exception InvalidObjectIDException This exception is thrown, if remote_id is invalid.
00317      */
00318 
00319     void remove_remote_object(object_id_t remote_id);
00320 
00321 
00322     /**
00323      * Add a gate template to the gate library and update the logic model.
00324      * All placed gates that already reference this gate template via an ID become updated.
00325      * If the template has no object ID defined, a new object ID for the template is set.
00326      * @exception DegateLogicException Is thrown if there is no gate library.
00327      * @see GateLibrary::add_gate_template
00328      *
00329      * @todo Implement update.
00330      * @todo should call add_object() for the port
00331      */
00332 
00333     void add_gate_template(GateTemplate_shptr tmpl);
00334 
00335     /**
00336      * Remove a gate template from the gate library and all placed gates that refer
00337      * to the template. This will destroy gate ports as well.
00338      *
00339      * @see add_gate_template()
00340      * @see GateLibrary::remove_gate_template()
00341      * @see remove_object()
00342      * @exception DegateLogicException Is thrown, if there is no gate library.
00343      * @todo should call remove_object() for the port
00344      * @todo The method name does not express, that the gates are removed as well.
00345      *     The method should be named remove_template_and_gates() and remove_gate_template()
00346      *     should only unreference and destroy the template.
00347      */
00348 
00349     void remove_gate_template(GateTemplate_shptr tmpl);
00350 
00351     /**
00352      * Remove the reference to a certain gate template from all gates.
00353      * This will destroy gate ports as well.
00354      * @exception DegateLogicException Is thrown, if there is no gate library.
00355      * @see Gate::remove_template()
00356      * @todo should call remove_object() for the port
00357      */
00358 
00359     void remove_template_references(GateTemplate_shptr tmpl);
00360 
00361 
00362     /**
00363      * Remove all gates, that reference a template. The template is preserved.
00364      * It will remove the gate ports as well.
00365      */
00366 
00367     void remove_gates_by_template_type(GateTemplate_shptr tmpl);
00368 
00369     /**
00370      * Add a template port to a gate template and make relevant updates in the logic model.
00371      * If you add or remove ports from a gate template, the "real" gates of that type must be updated.
00372      * The corresponding ports must be added to or removed from the "real" gates.
00373      * @todo should call add_object() for the port
00374      */
00375 
00376     void add_template_port_to_gate_template(GateTemplate_shptr gate_template,
00377                                             GateTemplatePort_shptr template_port);
00378 
00379 
00380     /**
00381      * Remove a template port from a gate template and make relevant
00382      * updates in the logic model. If you add or remove ports from a
00383      * gate template, the "real" gates of that type must be updated.
00384      * The corresponding ports must be added to or removed from the
00385      * "real" gates. This method does this.
00386      * @todo should call add_object() for the port.
00387      */
00388 
00389     void remove_template_port_from_gate_template(GateTemplate_shptr gate_template,
00390                                                  GateTemplatePort_shptr template_port);
00391 
00392 
00393     /**
00394      * Add an empty layer.
00395      * @exception DegateLogicException Throws an exception, if you add a layer
00396      *            on a position where another layer object is already stored.
00397      *            If the layer that should be added already contains logic model objects, this
00398      *            exception is thrown, too.
00399      */
00400 
00401     void add_layer(layer_position_t pos, Layer_shptr new_layer);
00402 
00403     /**
00404      * Add an empty layer.
00405      * @see add_layer()
00406      */
00407 
00408     void add_layer(layer_position_t pos);
00409 
00410     /**
00411      * Get a layer by its posiion index.
00412      */
00413 
00414     Layer_shptr get_layer(layer_position_t pos);
00415 
00416     /**
00417      * Get a layer by its ID.
00418      * @exception CollectionLookupException This exception is thrown, if there is no matching layer.
00419      */
00420     Layer_shptr get_layer_by_id(layer_id_t lid);
00421 
00422 
00423     /**
00424      * Remove a layer.
00425      * @see remove_layer()
00426      */
00427 
00428     void remove_layer(layer_position_t pos);
00429 
00430     /**
00431      * Set layers.
00432      */
00433 
00434     void set_layers(layer_collection layers);
00435 
00436     /**
00437      * Remove a layer from the logic model.
00438      * A layer contains logical objects. These object are referred in other parts
00439      * of the logic model. References in other parts must be cleaned as well.
00440      * @todo Implement this.
00441      */
00442 
00443     void remove_layer(Layer_shptr layer);
00444 
00445     /**
00446      * Set the current layer you are working on.
00447      */
00448 
00449     void set_current_layer(layer_position_t pos);
00450 
00451     /**
00452      * Get the current layer.
00453      */
00454 
00455     Layer_shptr get_current_layer();
00456 
00457 
00458     /**
00459      * Get the current gate library for this logic model.
00460      */
00461 
00462     GateLibrary_shptr get_gate_library();
00463 
00464     /**
00465      * Set the gate library.
00466      * @todo: Implement updates on the logic model.
00467      */
00468 
00469     void set_gate_library(GateLibrary_shptr new_gate_lib);
00470 
00471 
00472     /**
00473      * Add a net to the logic model.
00474      * If the net object has no object ID, a new object ID is set to the net.
00475      * @exception InvalidPointerException Is thrown, if an invalid pointer was
00476      *   passed as argument.
00477      */
00478 
00479     void add_net(Net_shptr net);
00480 
00481 
00482     /**
00483      * Get a net from the logic model.
00484      * @exception CollectionLookupException Is thrown if there is
00485      *   no logic model object with that object ID.     
00486      */
00487 
00488     Net_shptr get_net(object_id_t net_id);
00489 
00490     /**
00491      * Remove a net from the logic model.
00492      * @exception InvalidPointerException Is thrown, if an invalid pointer was
00493      *   passed as argument.
00494      * @exception CollectionLookupException Is thrown if there is
00495      *   no logic model object with that object ID.
00496      */
00497 
00498     void remove_net(Net_shptr net);
00499 
00500 
00501     /**
00502      * Get a iterator to iterate over all placeable objects.
00503      */
00504 
00505     object_collection::iterator objects_begin();
00506 
00507 
00508     /**
00509      * Get an end iterator for the iteration over all placeable objects.
00510      */
00511 
00512     object_collection::iterator objects_end();
00513 
00514 
00515     /**
00516      * Get a iterator to iterate over all gates.
00517      */
00518 
00519     gate_collection::iterator gates_begin();
00520 
00521 
00522     /**
00523      * Get an end iterator for the iteration over all gates.
00524      */
00525 
00526     gate_collection::iterator gates_end();
00527 
00528     /**
00529      * Get a iterator to iterate over all vias.
00530      */
00531 
00532     via_collection::iterator vias_begin();
00533 
00534     /**
00535      * Get an end iterator for the iteration over all vias.
00536      */
00537 
00538     via_collection::iterator vias_end();
00539 
00540     /**
00541      * Get a iterator to iterate over all placeable objects.
00542      */
00543 
00544     layer_collection::iterator layers_begin();
00545 
00546     /**
00547      * Get an end iterator for the iteration over all placeable objects.
00548      */
00549 
00550     layer_collection::iterator layers_end();
00551 
00552 
00553     /**
00554      * Get a iterator to iterate over all nets.
00555      */
00556 
00557     net_collection::iterator nets_begin();
00558 
00559 
00560     /**
00561      * Get an end iterator for the iteration over all nets.
00562      */
00563 
00564     net_collection::iterator nets_end();
00565 
00566 
00567     /**
00568      * Get a iterator to iterate over all annotations.
00569      */
00570 
00571     annotation_collection::iterator annotations_begin();
00572 
00573 
00574     /**
00575      * Get an end iterator for the iteration over all annotations.
00576      */
00577 
00578     annotation_collection::iterator annotations_end();
00579 
00580 
00581     /**
00582      * Print the content of the logic model into an ostream.
00583      */
00584     void print(std::ostream & os = std::cout);
00585 
00586 
00587     /**
00588      * Get number of layers.
00589      */
00590     unsigned int get_num_layers() const;
00591 
00592 
00593     /**
00594      * Compare ports of a gate with template ports of its associated
00595      * template and update them.
00596      */
00597 
00598     void update_ports(Gate_shptr gate);
00599 
00600     /**
00601      * Compare ports of all gates that reference a given template
00602      * and update them.
00603      */
00604 
00605     void update_ports(GateTemplate_shptr gate_template);
00606 
00607 
00608     /**
00609      * Get the main module.
00610      */
00611 
00612     Module_shptr get_main_module() const;
00613 
00614     /**
00615      * Set main module.
00616      */
00617     void set_main_module(Module_shptr main_module);
00618 
00619     /**
00620      *
00621      */
00622     void reset_removed_remote_objetcs_list();
00623 
00624     std::list<object_id_t> const & get_removed_remote_objetcs_list();
00625 
00626     void update_roid_mapping(object_id_t remote_oid, object_id_t local_oid);
00627 
00628     object_id_t get_local_oid_for_roid(object_id_t remote_oid);
00629 
00630 
00631     /**
00632      * Set default gate port diameter.
00633      */
00634     void set_default_gate_port_diameter(diameter_t port_diameter);
00635 
00636   };
00637 
00638 
00639 
00640 }
00641 
00642 #endif