home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11560 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.9 KB  |  160 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cs.utexas.edu!asuvax!ukma!vlsi!starbase.spd.louisville.edu!aldavi01
  3. From: aldavi01@starbase.spd.louisville.edu (Arlie Davis)
  4. Subject: How to solve problem with simulating logic gates with classes?
  5. Message-ID: <aldavi01.712136947@starbase.spd.louisville.edu>
  6. Sender: news@vlsi.louisville.edu (Network News System)
  7. Nntp-Posting-Host: starbase.spd.louisville.edu
  8. Organization: University of Louisville
  9. Date: Sun, 26 Jul 1992 07:49:07 GMT
  10. Lines: 148
  11.  
  12. I'm trying to write a set of classes to simulate a network of logic gates,
  13. using a base class Gate, with a single virtual member: "int evaluate (void)
  14. const = 0".  The derived classes (NandGate, XorGate, etc...) have references
  15. to their input objects ("const Gate& a;" in derived class).  There are
  16. several "source" gates: IntGate (which is a wrapper for a const reference
  17. to an integer) and InputGate (which calls a given function).
  18.  
  19. Everything works fine, everything works great, but only for simple logic
  20. circuits, where "simple" means "no feedback".  No flip-flops, no memory
  21. circuits at all.  Sure, I can declare a flip-flop, but only globally:
  22.  
  23. InputGate set;
  24. InputGate reset;
  25.  
  26. extern NandGate set_gate;
  27. extern NandGate reset_gate;
  28.  
  29. NandGate set_gate (set, reset_gate);
  30. NandGate reset_gate (reset, set_gate);
  31.  
  32. However, any attempt to call evaluate for either of the above [re]set_gate
  33. objects always results in the process dying with failed stack growth --
  34. in other words, unbounded recursion.  :(
  35.  
  36. Has anyone written a base class for doing something similar?  Does a public
  37. library have a decent implementation of this, that allows flip-flops and
  38. circuits with self-reference?  (Am I reinventing the wheel?)
  39.  
  40. Any help is greatly appreciated.  If there is sufficient response, I'll
  41. make a stab at a summary.  (Short) source code follows:
  42.  
  43. ////// logic.h
  44.  
  45. class Gate {
  46.   public:
  47.   virtual int evaluate (void) const = 0;
  48.   operator int (void) const { return evaluate(); }
  49.   };
  50.  
  51. class AndGate : public Gate {
  52.   const Gate& a;
  53.   const Gate& b;
  54.  
  55.   public:
  56.   AndGate (const Gate& aa, const Gate& bb) : a (aa), b (bb) {}
  57.   virtual int evaluate (void) const;
  58.   };
  59.  
  60. class OrGate : public Gate {
  61.   const Gate& a;
  62.   const Gate& b;
  63.  
  64.   public:
  65.   OrGate (const Gate& aa, const Gate& bb) : a (aa), b (bb) {}
  66.   virtual int evaluate (void) const;
  67.   };
  68.  
  69. class NotGate : public Gate {
  70.   const Gate& a;
  71.  
  72.   public:
  73.   NotGate (const Gate& aa) : a (aa) {}
  74.   virtual int evaluate (void) const;
  75.   };
  76.  
  77. class NandGate : public Gate {
  78.   const Gate& a;
  79.   const Gate& b;
  80.  
  81.   public:
  82.   NandGate (const Gate& aa, const Gate& bb) : a (aa), b (bb) {}
  83.   virtual int evaluate (void) const;
  84.   };
  85.  
  86. class NorGate : public Gate {
  87.   const Gate& a;
  88.   const Gate& b;
  89.  
  90.   public:
  91.   NorGate (const Gate& aa, const Gate& bb) : a (aa), b (bb) {}
  92.   virtual int evaluate (void) const;
  93.   };
  94.  
  95. class InputGate : public Gate {
  96.   int (*function) (void);
  97.   public:
  98.   InputGate (int (*f) (void)) : function (f) {}
  99.   virtual int evaluate (void) const;
  100.   };
  101.  
  102. class IntGate : public Gate {
  103.   const int& i;
  104.   public:
  105.   IntGate (const int& ii) : i (ii) {}
  106.   virtual int evaluate (void) const;
  107.   };
  108.  
  109. ////// logic.cc
  110.  
  111. #include "logic.h"
  112.  
  113. int AndGate::evaluate (void) const
  114.   {
  115.   if (!a.evaluate()) return 0;
  116.   if (!b.evaluate()) return 0;
  117.   return 1;
  118.   }
  119.  
  120. int OrGate::evaluate (void) const
  121.   {
  122.   if (a.evaluate()) return 1;
  123.   if (b.evaluate()) return 1;
  124.   return 0;
  125.   }
  126.  
  127. int NotGate::evaluate (void) const
  128.   {
  129.   return !a.evaluate();
  130.   }
  131.  
  132. int NandGate::evaluate (void) const
  133.   {
  134.   if (!a.evaluate()) return 1;
  135.   if (!b.evaluate()) return 1;
  136.   return 0;
  137.   }
  138.  
  139. int NorGate::evaluate (void) const
  140.   {
  141.   if (a.evaluate()) return 0;
  142.   if (b.evaluate()) return 0;
  143.   return 1;
  144.   }
  145.  
  146. int InputGate::evaluate (void) const
  147.   {
  148.   return (*function)();
  149.   }
  150.  
  151. int IntGate::evaluate (void) const
  152.   {
  153.   return i;
  154.   }
  155.  
  156. ////// end of example source
  157.  
  158. -- 
  159. lrwx------   1 aldavi01 emacsstu       9 Jun  6 12:43 .signature -> /dev/null
  160.