0

デジタル論理回路をシミュレートする方法を学んでいます。ここでは、最初の試みのソース コードを示します。AND、OR、NOT ゲートで構成される回路をシミュレートするための小さなプログラムです。

このコードは、ループのない回路でうまく機能します。回路ループが導入されると、無限再帰によりスタック オーバーフローが発生します。このバグを取り除くのを手伝ってください。

これは趣味のプロジェクトであり、どんな助けも大歓迎です。

ソースコード :

#include <cstdlib>
#include <iostream>
using namespace std;

class LogicGate
{
    int type;//gate type: 0 for NOT, 1 for OR, 2 for AND
  //pins 
  bool ina;//input a
  bool inb;//input b::this pin is not used for NOT gate
  bool outc;//output

  //fan-in
  LogicGate* ga;//gate connected to input a
  LogicGate* gb;//gate connected to input b

  //fan-out
  LogicGate* gc;//gate connected to output 
  int gctarget;//target input to which the gate "gc" is connected, 0 for input a, 1 for input c 



public:
    char* name;
    LogicGate()
    { 
        ina = inb = outc = false;
        ga = gb = gc = (LogicGate*)0;
        type = 0;
    }
    LogicGate(bool a, bool b)
    { 
        ina = a; inb = b; outc = false;
        ga = gb = gc = (LogicGate*)0;
        type = 0;
    }

    //set logic
    void settype(int t){if(t>=0&&t<3)type=t;else type=0;}

    //set input
    void seta(bool a){ ina = a; }
    void setb(bool b){ inb = b; }
    void setab(bool a, bool b){ina = a; inb = b; }

    //connect gate
    void cona(LogicGate* cga){ ga = cga; }
    void conb(LogicGate* cgb){ gb = cgb; }
    void conab(LogicGate* cga, LogicGate* cgb){ ga = cga; gb = cgb; }

    //connect the output of this gate to another gate's input
    void chainc(LogicGate* cgc, int target)
    { 
        gc = cgc; 
        gctarget = target;
        if(target==0) cgc->cona(this); else cgc->conb(this);
    }

    //calculate output
    bool calcout()
    {
        //if the input is not available make it available by forcing the connected gates to calculate
        if(ga){ ina = ga->calcout(); } //BUG:this may cause Stack overflow for circuits with loops
        if(gb){ inb = gb->calcout(); }//BUG:this may cause Stack overflow for circuits with loops

        //do the logic when inputs are available
        switch(type)
        {
        case 0:
            outc = !ina; break;
        case 1:
            outc = ina || inb; break;
        case 2:
            outc = ina && inb; break;
        }

        //if a gate is connected to output pin transfer the output value to the target input pin of the gate 
        if(gc){
            if(gctarget==0){
                gc->seta(outc);
            }else{
                gc->setb(outc);
            }
        }

        //for debugging only
        cout<<name<<" outputs "<<outc<<endl;
        return outc;
    }


};
int main(int argc, char *argv[])
{   
    LogicGate x,z;

    //AND gate
    z.settype(2);
    z.seta(false);
    z.setb(true);
    z.name = "ZZZ";


    //OR gate
    x.settype(1);
    x.cona(&z); // take one input from AND gate's output
    x.setb(true);
    x.name = "XXX";

    //z.cona(&x);// take one input from OR gate's output to make a loop:: results in stack overflow
    x.chainc(&z,0);//connect the output to AND gate's input "a" to form loop:: results in stack overflow 

    cout<<"final output"<<x.calcout()<<endl;

    return 0;
}
4

1 に答える 1

0

ここでの問題は、無限にループしていることです。プログラムは、実際の論理ゲートとは多少異なる動作をします。ここには 2 つの可能性があります。

1) サイクルの実施

CPUのように実装できます。calcout の呼び出しは、1 つのゲートの出力のみを計算し、次のゲートに反復します。ゲート用の Container クラスを作成できます。

class GateContainer
{
    //Contains all gates of your "circuit"
    std::vector<LogicalGate> allGates;

    //Contains all current gates to be processed
    std::queue<LogicalGate*> currentGates;

    void nextStep();
}

nextStep 関数は次のようになります。

void GateContainer::nextStep()
{
    //Get first gate from queue
    LogicalGate *current = currentGates.front();
    currentGates.pop();

    //Do all the calculations with th current gate

    //Push the gate connected to the Output to the list
    currentGates.push(current->gc);
}

このコードはテストされておらず、エラー チェックが必要な場合もあります。

2) ループをキャッチしてみる

でループをキャッチすることもできますcalcout。これは、LogicalGate でフラグを作成し、calcout を呼び出す前に毎回リセットすることで実現できます。

class LogicalGate
{
    ...
    bool calculated;
    ...
}

呼び出す前に、すべてのゲートに対してcalcout()計算を設定する必要があります。false次に、calcout は次のようになります。

bool calcout()
{
    calculated = true;

    if(ga && !ga->calculated){ ina = ga->calcout(); }
    if(gb && !ga->calculated){ inb = gb->calcout(); }

    ...
}
于 2015-06-23T11:31:53.243 に答える