Beispiel #1
0
        static void TestRing(bool oscillation)
        {
            Circuit c = new Circuit();

            Gates.BasicGates.Not mnot1 = new Gates.BasicGates.Not();
            Gates.BasicGates.Not mnot2 = new Gates.BasicGates.Not();
            Gates.BasicGates.Not mnot3 = new Gates.BasicGates.Not();

            c.Add(mnot1);
            c.Add(mnot2);
            c.Add(mnot3);

            mnot3.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mnot3_PropertyChanged);

            mnot1[0] = false;

            c[new Terminal(0, mnot2)] = new Terminal(0, mnot1);
            c[new Terminal(0, mnot3)] = new Terminal(0, mnot2);
            if (oscillation)
            {
                c[new Terminal(0, mnot1)] = new Terminal(0, mnot3);
            }


            Thread.Sleep(1000);
            c.Disconnect(new Terminal(0, mnot1));
        }
Beispiel #2
0
    // private VoltageSource AddVoltageSource(Circuit ckt)
    // {
    //     VoltageSource source = new VoltageSource("PS", "PSPOS", "0", 0);


    //     for (int i = 0; i < _powerSupply.transform.childCount; i++)
    //     {
    //         var child = _powerSupply.transform.GetChild(i);
    //         if (child.CompareTag("BBSlot"))
    //         {
    //             if (child.GetComponent<Slot>().itemPlaced != null && !child.GetComponent<Slot>().slotChecked)
    //             {
    //                 AttachPowerSupply(child, ckt);
    //             }
    //         }
    //     }
    //     return source;
    // }

    private void AddPowerSupplyConnection(string pos, string neg, Circuit ckt)
    {
        bool isGroundConnected = false;

        if (Globals.PSConnections.TryGetValue(Globals.PowerSupplyInput.ground, out Globals.BananaPlugs groundConnection))
        {
            if (!groundConnection.Equals(Globals.BananaPlugs.noConnection))
            {
                isGroundConnected = true;
            }
        }

        if (pos == Globals.PowerSupplyInput.ground.ToString())
        {
            pos = "0";
        }
        else if (pos == Globals.PowerSupplyInput.voltageSource.ToString() && isGroundConnected)
        {
            Debug.Log("Adding PS Voltage Source: " + pos + "PS" + " " + pos + " " + "0" + " " + _powerSupplyMachine.GetComponent <PSSelect>().voltage);
            VoltageSource voltSource = new VoltageSource(pos + "PS", pos, "0", _powerSupplyMachine.GetComponent <PSSelect>().voltage);
            voltageSource = voltSource;
            ckt.Add(voltSource);
        }
        else if (pos == Globals.PowerSupplyInput.currentSource.ToString() && isGroundConnected)
        {
            Debug.Log("Adding PS Current Source: " + pos + "PS" + " " + pos + " " + "0" + " " + _powerSupplyMachine.GetComponent <PSSelect>().current);
            CurrentSource currSource = new CurrentSource(pos + "PS", pos, "0", _powerSupplyMachine.GetComponent <PSSelect>().current);
            currentSource = currSource;
            ckt.Add(currSource);
        }

        Debug.Log("Adding Wire: " + pos + " " + pos + " " + neg + " " + 0);
        ckt.Add(new Resistor(pos, pos, neg, 0));
    }
Beispiel #3
0
        public void CircuitValidatesLooped()
        {
            // Later change to importing the circuit from file
            var c  = new Circuit();
            var i1 = new InputNode();

            var n1 = new NotNode();
            var n2 = new NotNode();
            var n3 = new NotNode();

            c.AddInput(i1, NodeCurrent.None);
            c.Add(i1);
            c.Add(n1);
            c.Add(n2);
            c.Add(n3);

            i1.AddOutput(n1);
            n1.AddOutput(n2);
            n2.AddOutput(n3);
            n3.AddOutput(n1);


            Assert.ThrowsException <CircuitInvalidException>(() =>
            {
                c.Accept(new CircuitLoopValidatorVisitor());
            });
        }
Beispiel #4
0
    private void HandleSelfBananaConnections(int i, Circuit ckt)
    {
        switch (i)
        {
        case (0):
            foreach (String connection in B0Connections.Distinct().ToList())
            {
                Debug.Log("Adding wire:" + "B" + i + connection + " B" + i + " " + connection);
                ckt.Add(new Resistor("B" + i + connection, "B" + i, connection, 0));
                ActivateBananaSlot(connection);
            }
            break;

        case (1):
            foreach (String connection in B1Connections.Distinct().ToList())
            {
                Debug.Log("Adding wire:" + "B" + i + connection + " B" + i + " " + connection);
                ckt.Add(new Resistor("B" + i + connection, "B" + i, connection, 0));
                ActivateBananaSlot(connection);
            }
            break;


        case (2):
            foreach (String connection in B2Connections.Distinct().ToList())
            {
                Debug.Log("Adding wire:" + "B" + i + connection + " B" + i + " " + connection);
                ckt.Add(new Resistor("B" + i + connection, "B" + i, connection, 0));
                ActivateBananaSlot(connection);
            }
            break;


        case (3):
            foreach (String connection in B3Connections.Distinct().ToList())
            {
                Debug.Log("Adding wire:" + "B" + i + connection + " B" + i + " " + connection);
                ckt.Add(new Resistor("B" + i + connection, "B" + i, connection, 0));
                ActivateBananaSlot(connection);
            }
            break;

        case (4):
            foreach (String connection in B4Connections.Distinct().ToList())
            {
                Debug.Log("Adding wire:" + "B" + i + connection + " B" + i + " " + connection);
                ckt.Add(new Resistor("B" + i + connection, "B" + i, connection, 0));
                ActivateBananaSlot(connection);
            }
            break;

        default:
            Debug.Log("idk");
            break;
        }
    }
Beispiel #5
0
        static void TestIC1()
        {
            Circuit c   = new Circuit();
            IC      nor = Nor();

            c.Add(nor);

            nor.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(nor_PropertyChanged);


            nor[0] = false;
            nor[1] = false;
            Thread.Sleep(100);
            Console.Out.WriteLine("--");
            nor[0] = true;
            Thread.Sleep(100);
            Console.Out.WriteLine("--");
            nor[1] = true;
            Thread.Sleep(100);
            Console.Out.WriteLine("--");
            nor[0] = false;
            Thread.Sleep(100);
            Console.Out.WriteLine("--");
            nor[1] = false;
        }
Beispiel #6
0
        public void When_DiodeRectifier_Expect_Spice3f5Reference()
        {
            /*
             * Pulsed voltage source towards a resistive voltage divider between 0V and 5V
             * Output voltage is expected to behavior like the reference
             */
            // Build circuit
            var ckt = new Circuit();

            ckt.Add(
                new VoltageSource("V1", "in", "0", new Pulse(0, 5, 1e-6, 10e-9, 10e-9, 1e-6, 2e-6)),
                new VoltageSource("Vsupply", "vdd", "0", 5.0),
                new Resistor("R1", "vdd", "out", 10.0e3),
                new Resistor("R2", "out", "0", 10.0e3),
                CreateDiode("D1", "in", "out", "1N914"),
                CreateDiodeModel("1N914", "Is = 2.52e-9 Rs = 0.568 N = 1.752 Cjo = 4e-12 M = 0.4 tt = 20e-9")
                );

            // Create simulation
            var tran = new Transient("tran", 1e-9, 10e-6);

            // Create exports
            Export <double>[] exports = { new RealVoltageExport(tran, "out") };

            // Create references
            double[][] references =
            {
                new[] { 2.499987387600927e+00, 2.499987387600927e+00, 2.499987387600927e+00, 2.499987387600931e+00, 2.499987387600930e+00, 2.499987387600930e+00, 2.499987387600929e+00, 2.499987387600930e+00, 2.499987387600933e+00, 2.499987387600932e+00, 2.499987387600934e+00, 2.499987387600934e+00, 2.499987387600928e+00, 2.499987387600928e+00, 2.499987387600927e+00, 2.499987387600927e+00, 2.499987387600926e+00, 2.499987387600928e+00, 2.499987387600927e+00, 2.499987387600927e+00, 2.499987387600927e+00, 2.961897877874934e+00, 3.816739529558831e+00, 5.191526587205253e+00, 6.033962809915079e+00, 5.858560892037079e+00, 5.542221840643947e+00, 5.047948278928924e+00, 4.566221446174492e+00, 4.496222403679634e+00, 4.470638440255675e+00, 4.461747124949804e+00, 4.458828418109562e+00, 4.458043432344522e+00, 4.458104168570753e+00, 4.458101940017489e+00, 4.458103007267981e+00, 4.458102313327546e+00, 4.458102774372732e+00, 4.458102484881135e+00, 3.958696587137276e+00, 2.960963748330885e+00, 9.726613059613628e-01, -5.081214667849954e-01, -5.008294438061146e-01, -4.823457266305946e-01, -4.093596340082378e-01, 2.927533877100476e-01, 1.580990201373001e+00, 2.297731671229221e+00, 2.465955512323740e+00, 2.496576390164448e+00, 2.500278606332533e+00, 2.499872222320282e+00, 2.500061542030393e+00, 2.499929263366373e+00, 2.500032946810125e+00, 2.499951677124709e+00, 2.500009659840817e+00, 2.961918369398067e+00, 3.816756624765787e+00, 5.191537959402029e+00, 6.033971024560395e+00, 5.858568428923525e+00, 5.542228175717445e+00, 5.047952819852995e+00, 4.566222065264992e+00, 4.496222553506734e+00, 4.470638537994252e+00, 4.461747150642569e+00, 4.458828425715773e+00, 4.458043431871857e+00, 4.458104168387089e+00, 4.458101940091086e+00, 4.458103007220202e+00, 4.458102313359292e+00, 4.458102774351643e+00, 4.458102484892605e+00, 3.958696587148540e+00, 2.960963748341582e+00, 9.726613059724690e-01, -5.081214667705706e-01, -5.008294437900119e-01, -4.823457266089376e-01, -4.093596339411046e-01, 2.927533879025108e-01, 1.580990201486391e+00, 2.297731671249843e+00, 2.465955512329101e+00, 2.496576390165203e+00, 2.500278606332566e+00, 2.499872222320265e+00, 2.500061542030405e+00, 2.499929263366364e+00, 2.500032946810133e+00, 2.499951677124703e+00, 2.500009659840779e+00, 2.961918369398032e+00, 3.816756624765757e+00, 5.191537959401995e+00, 6.033971024560496e+00, 5.858568428923665e+00, 5.542228175717561e+00, 5.047952819853082e+00, 4.566222065265002e+00, 4.496222553506737e+00, 4.470638537994254e+00, 4.461747150642569e+00, 4.458828425715771e+00, 4.458043431871858e+00, 4.458104168387090e+00, 4.458101940091087e+00, 4.458103007220201e+00, 4.458102313359291e+00, 4.458102774351643e+00, 4.458102484892708e+00, 3.958696587148749e+00, 2.960963748341791e+00, 9.726613059726750e-01, -5.081214667704262e-01, -5.008294437900225e-01, -4.823457266089518e-01, -4.093596339411494e-01, 2.927533879023823e-01, 1.580990201486315e+00, 2.297731671249816e+00, 2.465955512329098e+00, 2.496576390165200e+00, 2.500278606332570e+00, 2.499872222320263e+00, 2.500061542030407e+00, 2.499929263366362e+00, 2.500032946810134e+00, 2.499951677124702e+00, 2.500009659840780e+00, 2.961918369398029e+00, 3.816756624765742e+00, 5.191537959401949e+00, 6.033971024560461e+00, 5.858568428923622e+00, 5.542228175717498e+00, 5.047952819852998e+00, 4.566222065264983e+00, 4.496222553506735e+00, 4.470638537994251e+00, 4.461747150642568e+00, 4.458828425715771e+00, 4.458043431871856e+00, 4.458104168387091e+00, 4.458101940091087e+00, 4.458103007220201e+00, 4.458102313359291e+00, 4.458102774351643e+00, 4.458102484892708e+00, 3.958696587148324e+00, 2.960963748341793e+00, 9.726613059726769e-01, -5.081214667704226e-01, -5.008294437900186e-01, -4.823457266089463e-01, -4.093596339411322e-01, 2.927533879024313e-01, 1.580990201486346e+00, 2.297731671249819e+00, 2.465955512329098e+00, 2.496576390165201e+00, 2.500278606332567e+00, 2.499872222320263e+00, 2.500061542030407e+00, 2.499929263366362e+00, 2.500032946810135e+00, 2.499951677124702e+00, 2.500009659840780e+00, 2.961918369398421e+00, 3.816756624765698e+00, 5.191537959401959e+00, 6.033971024560470e+00, 5.858568428923640e+00, 5.542228175717541e+00, 5.047952819853064e+00, 4.566222065265000e+00, 4.496222553506738e+00, 4.470638537994254e+00, 4.461747150642569e+00, 4.458828425715772e+00, 4.458043431871857e+00, 4.458104168387089e+00, 4.458101940091087e+00, 4.458103007220202e+00, 4.458102313359291e+00, 4.458102774351643e+00, 4.458102489285038e+00 }
            };

            // Run test
            AnalyzeTransient(tran, ckt, exports, references);
            DestroyExports(exports);
        }
Beispiel #7
0
        public void When_DiodeDC_Expect_Spice3f5Reference()
        {
            /*
             * DC voltage shunted by a diode
             * Current is to behave like the reference
             */

            // Build circuit
            var ckt = new Circuit();

            ckt.Add(
                CreateDiode("D1", "OUT", "0", "1N914"),
                CreateDiodeModel("1N914", "Is=2.52e-9 Rs=0.568 N=1.752 Cjo=4e-12 M=0.4 tt=20e-9"),
                new VoltageSource("V1", "OUT", "0", 0.0)
                );

            // Create simulation
            var dc = new DC("DC", "V1", -1.0, 1.0, 10e-3);

            // Create exports
            Export <double>[] exports = { new RealPropertyExport(dc, "V1", "i") };

            // Create reference
            double[][] references =
            {
                new[] { 2.520684772022719e-09, 2.520665232097485e-09, 2.520645248083042e-09, 2.520624819979389e-09, 2.520603725741921e-09, 2.520582409459848e-09, 2.520560649088566e-09, 2.520538000538863e-09, 2.520515129944556e-09, 2.520491593216434e-09, 2.520467612399102e-09, 2.520442965447955e-09, 2.520417652362994e-09, 2.520391229055008e-09, 2.520364583702417e-09, 2.520336828126801e-09, 2.520307962328161e-09, 2.520278874484916e-09, 2.520248454374041e-09, 2.520216701995537e-09, 2.520184505527823e-09, 2.520150754747874e-09, 2.520115449655691e-09, 2.520079700474298e-09, 2.520041952891461e-09, 2.520002650996389e-09, 2.519962460922898e-09, 2.519919828358752e-09, 2.519875419437767e-09, 2.519829456204548e-09, 2.519781050480674e-09, 2.519730646355356e-09, 2.519677799739384e-09, 2.519621844498943e-09, 2.519563668812452e-09, 2.519502162456888e-09, 2.519437547476855e-09, 2.519369379783143e-09, 2.519297437331147e-09, 2.519221276031658e-09, 2.519140673840070e-09, 2.519055408711779e-09, 2.518964592468365e-09, 2.518868003065222e-09, 2.518765085390839e-09, 2.518655506378309e-09, 2.518538155804606e-09, 2.518412922647428e-09, 2.518278474639146e-09, 2.518133923601340e-09, 2.517978381355590e-09, 2.517810848701174e-09, 2.517629882348160e-09, 2.517434039006616e-09, 2.517221764364308e-09, 2.516991060019791e-09, 2.516739705527016e-09, 2.516465702484538e-09, 2.516165609200982e-09, 2.515836872163391e-09, 2.515475161501968e-09, 2.515076480413825e-09, 2.514635832895351e-09, 2.514147445786818e-09, 2.513604324683172e-09, 2.512998475978634e-09, 2.512320573799798e-09, 2.511559182849510e-09, 2.510700980451475e-09, 2.509729757349533e-09, 2.508625973618450e-09, 2.507366425597013e-09, 2.505921525841615e-09, 2.504256357838130e-09, 2.502326790221332e-09, 2.500077533884593e-09, 2.497439421933478e-09, 2.494324247148683e-09, 2.490618655759391e-09, 2.486175321170236e-09, 2.480800564974572e-09, 2.474236482363779e-09, 2.466134130241215e-09, 2.456014613905211e-09, 2.443208080293857e-09, 2.426758793916406e-09, 2.405272869765440e-09, 2.377086694149710e-09, 2.341755483969976e-09, 2.297702500486665e-09, 2.242774105321033e-09, 2.174284835509965e-09, 2.088886258411193e-09, 1.982402894618041e-09, 1.849628367134315e-09, 1.684070757845824e-09, 1.477634958835239e-09, 1.220227058285062e-09, 8.992606936875092e-10, 4.990415580774510e-10, -4.208324063460023e-23, -6.222658915921997e-10, -1.398183520351370e-09, -2.365693620165477e-09, -3.572105541915782e-09, -5.076410555804323e-09, -6.952166481388744e-09, -9.291094477115180e-09, -1.220756418174318e-08, -1.584418615752092e-08, -2.037878504834723e-08, -2.603309548487864e-08, -3.308360396747645e-08, -4.187506874586688e-08, -5.283737797290300e-08, -6.650657008444583e-08, -8.355104497148602e-08, -1.048042475026989e-07, -1.313054202034536e-07, -1.643504193848955e-07, -2.055550786805860e-07, -2.569342167357824e-07, -3.210001533471285e-07, -4.008855497006358e-07, -5.004965768495850e-07, -6.247039000539800e-07, -7.795808144028804e-07, -9.727001679671332e-07, -1.213504582209257e-06, -1.513768057126441e-06, -1.888171507258285e-06, -2.355020333966173e-06, -2.937139061076621e-06, -3.662986687191783e-06, -4.568047149322574e-06, -5.696562662471649e-06, -7.103694343424394e-06, -8.858215224893939e-06, -1.104586649613992e-05, -1.377353976839135e-05, -1.717448782878606e-05, -2.141481549700064e-05, -2.670156304629412e-05, -3.329276978536466e-05, -4.150999799246158e-05, -5.175391113643180e-05, -6.452363948283857e-05, -8.044083562419591e-05, -1.002795274224200e-04, -1.250031216609715e-04, -1.558102032684916e-04, -1.941911156088105e-04, -2.419976971548277e-04, -3.015289829039203e-04, -3.756361388815854e-04, -4.678503519079946e-04, -5.825377853404534e-04, -7.250859365310891e-04, -9.021256392514054e-04, -1.121792314356274e-03, -1.394028558000970e-03, -1.730927332259435e-03, -2.147110349238757e-03, -2.660129130047428e-03, -3.290866177790397e-03, -4.063900589218683e-03, -5.007786885759424e-03, -6.155179858715831e-03, -7.542725667060601e-03, -9.210636215301937e-03, -1.120187715360643e-02, -1.356093587232876e-02, -1.633219609264214e-02, -1.955802291413677e-02, -2.327673904312388e-02, -2.752072521737903e-02, -3.231488373640667e-02, -3.767565498752745e-02, -4.361068391378264e-02, -5.011912351695047e-02, -5.719246701131020e-02, -6.481574162201031e-02, -7.296888192813844e-02, -8.162812138207687e-02, -9.076728201979800e-02, -1.003588891563969e-01, -1.103750792457605e-01, -1.207882998568939e-01, -1.315718201008491e-01, -1.427000796200868e-01, -1.541489071231517e-01, -1.658956380366401e-01, -1.779191572005734e-01, -1.901998880621483e-01, -2.027197453741645e-01, -2.154620644191900e-01, -2.284115164341036e-01, -2.415540172223232e-01, -2.548766338536659e-01, -2.683674927728656e-01, -2.820156914701786e-01 }
            };

            // Run test
            AnalyzeDC(dc, ckt, exports, references);
            DestroyExports(exports);
        }
Beispiel #8
0
        public void When_DiodeSmallSignal_Expect_Spice3f5Reference()
        {
            /*
             * DC voltage source shunted by a diode
             * Current is expected to behave like the reference
             */
            // Build circuit
            var ckt = new Circuit();

            ckt.Add(
                CreateDiode("D1", "0", "OUT", "1N914"),
                CreateDiodeModel("1N914", "Is=2.52e-9 Rs=0.568 N=1.752 Cjo=4e-12 M=0.4 tt=20e-9"),
                new VoltageSource("V1", "OUT", "0", 1.0)
                .SetParameter("acmag", 1.0)
                );

            // Create simulation
            var ac = new AC("ac", new DecadeSweep(1e3, 10e6, 5));

            // Create exports
            Export <Complex>[] exports = { new ComplexPropertyExport(ac, "V1", "i") };

            // Create references
            double[]    riRef      = { -1.945791742986885e-12, -1.904705637099517e-08, -1.946103289747125e-12, -3.018754997881332e-08, -1.946885859826953e-12, -4.784404245850086e-08, -1.948851586992178e-12, -7.582769719229839e-08, -1.953789270386556e-12, -1.201788010800761e-07, -1.966192170307985e-12, -1.904705637099495e-07, -1.997346846331992e-12, -3.018754997881245e-07, -2.075603854314768e-12, -4.784404245849736e-07, -2.272176570837208e-12, -7.582769719228451e-07, -2.765944910274710e-12, -1.201788010800207e-06, -4.006234902415568e-12, -1.904705637097290e-06, -7.121702504803603e-12, -3.018754997872460e-06, -1.494740330300116e-11, -4.784404245814758e-06, -3.460467495474045e-11, -7.582769719089195e-06, -8.398150889530617e-11, -1.201788010744768e-05, -2.080105080892987e-10, -1.904705636876583e-05, -5.195572682013223e-10, -3.018754996993812e-05, -1.302127347221150e-09, -4.784404242316795e-05, -3.267854507347871e-09, -7.582769705163549e-05, -8.205537869558709e-09, -1.201788005200868e-04, -2.060843758802494e-08, -1.904705614805916e-04 };
            Complex[][] references = new Complex[1][];
            references[0] = new Complex[riRef.Length / 2];
            for (int i = 0; i < riRef.Length; i += 2)
            {
                references[0][i / 2] = new Complex(riRef[i], riRef[i + 1]);
            }

            // Run test
            AnalyzeAC(ac, ckt, exports, references);
            DestroyExports(exports);
        }
Beispiel #9
0
        static void TestIC2()
        {
            Circuit c = new Circuit();

            Gates.IOGates.UserInput  ui_r = new Gates.IOGates.UserInput();
            Gates.IOGates.UserInput  ui_s = new Gates.IOGates.UserInput();
            Gates.IOGates.UserOutput uo   = new Gates.IOGates.UserOutput();
            IC latch = SRLatch();

            c.Add(ui_r);
            c.Add(ui_s);
            c.Add(uo);
            c.Add(latch);

            c[new Terminal(0, latch)] = new Terminal(0, ui_r);
            c[new Terminal(1, latch)] = new Terminal(0, ui_s);
            c[new Terminal(0, uo)]    = new Terminal(0, latch);

            uo.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(uo_PropertyChanged);

            while (true)
            {
                Console.WriteLine("Ready for input (S = set, R = reset, X = hold, I = illegal)");
                string val = Console.In.ReadLine();
                if (val == "S")
                {
                    ui_r.Value = false;
                    ui_s.Value = true;
                }
                if (val == "R")
                {
                    ui_s.Value = false;
                    ui_r.Value = true;
                }
                if (val == "X")
                {
                    ui_s.Value = false;
                    ui_r.Value = false;
                }
                if (val == "I")
                {
                    ui_s.Value = true;
                    ui_r.Value = true;
                }
            }
        }
Beispiel #10
0
        static void TestClock()
        {
            Circuit c = new Circuit();

            Gates.IOGates.Clock  clk1 = new Gates.IOGates.Clock(1000);
            Gates.IOGates.Clock  clk2 = new Gates.IOGates.Clock(250);
            Gates.BasicGates.And mand = new Gates.BasicGates.And();

            c.Add(clk1);
            c.Add(clk2);
            c.Add(mand);

            mand.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mand2_PropertyChanged);

            //mand[1] = true;

            c[new Terminal(0, mand)] = new Terminal(0, clk1);
            c[new Terminal(1, mand)] = new Terminal(0, clk2);
        }
Beispiel #11
0
        static IC Nor()
        {
            Circuit nor = new Circuit();

            Gates.BasicGates.And mand  = new Gates.BasicGates.And();
            Gates.BasicGates.Not mnot1 = new Gates.BasicGates.Not();
            Gates.BasicGates.Not mnot2 = new Gates.BasicGates.Not();

            Gates.IOGates.UserInput  in1  = new Gates.IOGates.UserInput();
            Gates.IOGates.UserInput  in2  = new Gates.IOGates.UserInput();
            Gates.IOGates.UserOutput out1 = new Gates.IOGates.UserOutput();

            nor.Add(mand);
            nor.Add(mnot1);
            nor.Add(mnot2);
            nor.Add(in1);
            nor.Add(in2);
            nor.Add(out1);

            nor[new Terminal(0, mnot1)] = new Terminal(0, in1);
            nor[new Terminal(0, mnot2)] = new Terminal(0, in2);
            nor[new Terminal(0, mand)]  = new Terminal(0, mnot1);
            nor[new Terminal(1, mand)]  = new Terminal(0, mnot2);
            nor[new Terminal(0, out1)]  = new Terminal(0, mand);

            return(new IC(nor, new Gates.IOGates.UserInput[] { in1, in2 },
                          new Gates.IOGates.UserOutput[] { out1 }, "Nor"));
        }
Beispiel #12
0
        static IC SRLatch()
        {
            Circuit sr   = new Circuit();
            IC      nor1 = Nor();
            IC      nor2 = Nor();

            Gates.IOGates.UserInput  in1  = new Gates.IOGates.UserInput();
            Gates.IOGates.UserInput  in2  = new Gates.IOGates.UserInput();
            Gates.IOGates.UserOutput out1 = new Gates.IOGates.UserOutput();
            Gates.IOGates.UserOutput out2 = new Gates.IOGates.UserOutput();

            sr.Add(nor1);
            sr.Add(nor2);

            sr.Add(in1);
            sr.Add(in2);
            sr.Add(out1);
            sr.Add(out2);

            nor1.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(nor_PropertyChanged);

            sr[new Terminal(0, nor1)] = new Terminal(0, in1);
            sr[new Terminal(1, nor2)] = new Terminal(0, in2);

            sr[new Terminal(1, nor1)] = new Terminal(0, nor2);
            sr[new Terminal(0, nor2)] = new Terminal(0, nor1);

            sr[new Terminal(0, out1)] = new Terminal(0, nor1);
            sr[new Terminal(0, out2)] = new Terminal(0, nor2);

            return(new IC(sr, new Gates.IOGates.UserInput[] { in1, in2 },
                          new Gates.IOGates.UserOutput[] { out1, out2 }, "SRLatch"));
        }
Beispiel #13
0
        static void TestAndNotLoop(bool oscillation)
        {
            Circuit c = new Circuit();

            Gates.BasicGates.And mand  = new Gates.BasicGates.And();
            Gates.BasicGates.And mand2 = new Gates.BasicGates.And();
            Gates.BasicGates.Not mnot  = new Gates.BasicGates.Not();
            mand2.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mand2_PropertyChanged);
            mnot.PropertyChanged  += new System.ComponentModel.PropertyChangedEventHandler(mnot3_PropertyChanged);
            c.Add(mand);
            c.Add(mand2);
            c.Add(mnot);
            mand2[0] = true;


            c[new Terminal(1, mand2)] = new Terminal(0, mand);
            c[new Terminal(0, mnot)]  = new Terminal(0, mand2);
            if (oscillation)
            {
                c[new Terminal(0, mand)] = new Terminal(0, mnot);
            }

            Console.Out.WriteLine(mand2.Output[0].ToString());

            mand[1] = true;
            Console.Out.WriteLine(mand2.Output[0].ToString());

            mand[0] = true;
            Console.Out.WriteLine(mand2.Output[0].ToString());

            Console.Out.Write(mnot.Output[0].ToString());
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Console.Out.Write(mnot.Output[0].ToString());

            Console.Out.Write(mnot[0].ToString());

            Console.Out.Write("X");
        }
Beispiel #14
0
    private void AddElectricalElement(Slot slot, Circuit ckt)
    {
        string componentName  = "S" + slot.slotID;
        string componentStart = "C" + GetSlotColumn(slot.slotID);
        string componentEnd   = "C" + GetSlotColumn(slot.slotPair.GetComponent <Slot>().slotID);

        if (slot.slotType == Globals.SlotType.BananaPlugSlot)
        {
            componentStart = "B" + slot.slotID;
        }

        float value = GetComponentValue(slot);

        switch (slot.itemPlaced.itemName)
        {
        case (Globals.AvailableItems.Wire):
            Resistor wire = new Resistor(componentName, componentStart, componentEnd, wireResistance);
            Debug.Log("Adding Wire: " + componentName + " " + componentStart + " " + componentEnd + " " + wireResistance);
            ckt.Add(wire);
            break;

        case (Globals.AvailableItems.Resistor):
            Resistor resistor = new Resistor(componentName, componentStart, componentEnd, value);
            Debug.Log("Adding Resistor: " + componentName + " " + componentStart + " " + componentEnd + " " + value);
            ckt.Add(resistor);
            break;

        case (Globals.AvailableItems.Capacitor):
            Capacitor capacitor = new Capacitor(componentName, componentStart, componentEnd, value);
            Debug.Log("Adding Capacitor: " + componentName + " " + componentStart + " " + componentEnd + " " + value);
            ckt.Add(capacitor);
            break;

        default:
            Debug.Log("Nothing found");
            break;
        }
    }
        /// <summary>
        /// Create a circuit with a current source and N resistors in series to ground
        /// </summary>
        /// <param name="count">Number of resistors to ground</param>
        /// <param name="current">Current (A)</param>
        /// <param name="resistance">Resistance (Ohm)</param>
        /// <returns></returns>
        static Circuit CreateResistorsInSeriesCircuit(int count, double current, double resistance)
        {
            Assert.IsTrue(count > 1);
            var ckt = new Circuit(
                new CurrentSource("I1", "IN", "0", current),
                new Resistor("R1", "IN", "B1", resistance),
                new Resistor($"R{count}", $"B{count - 1}", "0", resistance)
                );

            for (var i = 2; i <= count - 1; i++)
            {
                ckt.Add(new Resistor($"R{i}", $"B{i - 1}", $"B{i}", resistance));
            }
            return(ckt);
        }
        public void When_VoltageSourceSeries_Expect_Reference()
        {
            double[] voltages = { 1.0, 1.5, 2.8, 3.9, 0.5, -0.1, -0.5 };

            // Build the circuit
            var ckt = new Circuit();
            var sum = 0.0;

            for (var i = 0; i < voltages.Length; i++)
            {
                ckt.Add(new VoltageSource($"V{i + 1}", $"{i + 1}", $"{i}", voltages[i]));
                sum += voltages[i];
            }

            // Build the simulation, exports and references
            var op = new OP("OP");

            Export <double>[] exports    = { new RealVoltageExport(op, $"{voltages.Length}") };
            double[]          references = { sum };
            AnalyzeOp(op, ckt, exports, references);
        }
Beispiel #17
0
        public void When_DiodeDC_Expect_NoException()
        {
            /*
             * Bug found by Marcin Golebiowski
             * Running simulations twice will give rise to errors. We are using a diode model here
             * in order to make sure we're use states, extra equations, etc.
             */

            var ckt = new Circuit();

            ckt.Add(
                CreateDiode("D1", "OUT", "0", "1N914"),
                CreateDiodeModel("1N914", "Is=2.52e-9 Rs=0.568 N=1.752 Cjo=4e-12 M=0.4 tt=20e-9"),
                new VoltageSource("V1", "OUT", "0", 0.0)
                );

            // Create simulations
            var dc = new DC("DC 1", "V1", -1, 1, 10e-3);
            var op = new OP("OP 1");

            // Create exports
            var dcExportV1  = new RealPropertyExport(dc, "V1", "i");
            var dcExportV12 = new RealPropertyExport(dc, "V1", "i");

            dc.ExportSimulationData += (sender, args) =>
            {
                var v1  = dcExportV1.Value;
                var v12 = dcExportV12.Value;
            };
            var opExportV1 = new RealPropertyExport(op, "V1", "i");

            op.ExportSimulationData += (sender, args) =>
            {
                var v1 = opExportV1.Value;
            };

            // Run DC and op
            dc.Run(ckt);
            dc.Run(ckt);
        }
Beispiel #18
0
        public void CircuitValidatesNotConnectedToOutput()
        {
            // Later change to importing the circuit from file
            var c  = new Circuit();
            var i1 = new InputNode();
            var i2 = new InputNode();
            var n1 = new AndNode();

            c.AddInput(i1, NodeCurrent.None);
            c.AddInput(i2, NodeCurrent.None);

            i1.AddOutput(n1);
            i2.AddOutput(n1);

            c.Add(n1);


            Assert.ThrowsException <CircuitInvalidException>(() =>
            {
                c.Accept(new CircuitConnectionValidatorVisitor());
            });
        }
Beispiel #19
0
        public void CircuitValidatesConnected()
        {
            // Later change to importing the circuit from file
            var c  = new Circuit();
            var i1 = new InputNode();
            var i2 = new InputNode();
            var n1 = new AndNode();
            var o1 = new OutputNode();

            c.AddInput(i1, NodeCurrent.None);
            c.AddInput(i2, NodeCurrent.None);

            i1.AddOutput(n1);
            i2.AddOutput(n1);
            n1.AddOutput(o1);

            c.Add(n1);


            c.Accept(new CircuitConnectionValidatorVisitor());

            Assert.IsTrue(true);
        }
Beispiel #20
0
        static void TestUserIO()
        {
            Circuit c = new Circuit();

            Gates.BasicGates.Not mnot1 = new Gates.BasicGates.Not();
            Gates.BasicGates.And mand  = new Gates.BasicGates.And();
            Gates.BasicGates.Not mnot3 = new Gates.BasicGates.Not();
            Gates.BasicGates.Not mnot2 = new Gates.BasicGates.Not();

            Gates.IOGates.UserInput  ui = new Gates.IOGates.UserInput();
            Gates.IOGates.UserOutput uo = new Gates.IOGates.UserOutput();

            c.Add(mnot1);
            c.Add(mnot2);
            c.Add(mand);
            c.Add(mnot3);

            c.Add(ui);
            c.Add(uo);


            uo.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(uo_PropertyChanged);

            c[new Terminal(0, mand)]  = new Terminal(0, mnot2);
            c[new Terminal(0, mnot3)] = new Terminal(0, mand);
            c[new Terminal(0, mnot1)] = new Terminal(0, mnot3);
            c[new Terminal(0, mnot2)] = new Terminal(0, mnot1);

            c[new Terminal(1, mand)] = new Terminal(0, ui);
            c[new Terminal(0, uo)]   = new Terminal(0, mnot3);



            do
            {
                Console.Out.WriteLine("Ready for X");
                Console.In.ReadLine();
                ui.Value = !ui.Value;
            } while (true);
        }
        private static void Entry(Circuit ckt, string name, string m_in, string m_out, string v_vel, double a0, double b0, double ro, double v, double rout, double rin, double r1, double r2)
        {
            double D0                 = 2 * a0 * b0 / (a0 + b0);
            double F1                 = (rout - rin) * a0 * (rout + rin) / (r2 + r1);
            double F0                 = (r2 - r1) * a0;
            double fraction           = F0 / F1;
            Func <string, string> Q   = (m) => $"({m}/{ro})";
            Func <string, string> vel = (m) => $"{Q(m)}/{a0 * b0}";
            Func <string, string> R   = (x) => $"{x}*{D0 / v}";
            Func <string, string> xi  = (x) => $"40*pow({R(x)},-0.9) + 90*pow({fraction},-0.003)-80";

            string msx = $"{name}.msx", msy = $"{name}.msy", mss = $"{name}.mss";

            ckt.Add(new VoltageSource($"{name}.Vmas", m_in, msx, 0.0));
            ckt.Add(new Resistor($"{name}.Rmas", msx, msy, 1e-6));

            // The pressure drop
            ckt.Add(new BehavioralVoltageSource($"{name}.Exm", msy, m_out, $"{xi($"V({v_vel})")}*{ro}*V({v_vel})*V({v_vel})/2"));
            ckt.Add(new CurrentControlledVoltageSource($"{name}.Hmms", mss, "0", $"{name}.Vmas", 1.0));

            // Velocity
            ckt.Add(new BehavioralCurrentSource($"{name}.Guv", "0", v_vel, vel($"V({mss})")));
            ckt.Add(new Resistor($"{name}.Ruv", "0", v_vel, 1.0));
        }
Beispiel #22
0
    public void AddPhysicalGate(CircuitDevice circuitDevice)
    {
        Primitive newSimGate;

        switch (circuitDevice.logicType)
        {
        case Utils.LogicType.NOT:
            newSimGate = new Not("replaceMe");
            break;

        case Utils.LogicType.NAND:
            newSimGate = new Nand("replaceMe");
            break;

        case Utils.LogicType.AND:
            newSimGate = new And("replaceMe");
            break;

        case Utils.LogicType.OR:
            newSimGate = new Or("replaceMe");
            break;

        case Utils.LogicType.NOR:
            newSimGate = new Nor("replaceMe");
            break;

        case Utils.LogicType.XOR:
            newSimGate = new Xor("replaceMe");
            break;

        case Utils.LogicType.XNOR:
            newSimGate = new Xnor("replaceMe");
            break;

        case Utils.LogicType.Lever:
            newSimGate = new Lever("replaceMe");
            (circuitDevice as LeverDevice).leverGate = newSimGate as Lever;
            break;

        case Utils.LogicType.Indicator:
            newSimGate = new Indicator("replaceMe");
            break;

        case Utils.LogicType.Clock:
            newSimGate = new Clock("replaceMe");
            break;

        case Utils.LogicType.ShiftRegister4:
            newSimGate = new ShiftRegister("replaceMe", 4);
            break;

        case Utils.LogicType.ShiftRegister8:
            newSimGate = new ShiftRegister("replaceMe", 8);
            break;

        case Utils.LogicType.DFF:
            newSimGate = new DFF("replaceMe");
            break;

        case Utils.LogicType.FA1:
            newSimGate = new Adder("replaceMe", 1);
            break;

        default:
            Debug.LogError("Unknown circuit device type, object name: " + circuitDevice.gameObject.name);
            return;
        }

        newSimGate.position          = circuitDevice.transform.localPosition;
        circuitDevice.associatedGuid = newSimGate.guid;

        circuitSimObject.Add(newSimGate);

        physicalDevices.Add(newSimGate.guid, circuitDevice);
    }
Beispiel #23
0
    public void SpiceSharpCalculation()
    {
        Debug.Log("*********************");
        Debug.Log("Starting calculation ");
        ClearAllSlots();

        voltageSource = new VoltageSource("a");
        currentSource = new CurrentSource("b");
        var ckt = new Circuit();


        foreach (KeyValuePair <Globals.AgilentInput, Globals.BananaPlugs> entry in Globals.AgilentConnections)
        {
            if (entry.Value != Globals.BananaPlugs.noConnection)
            {
                ActivateBananaSlot(entry.Value.ToString());
            }
        }

        foreach (KeyValuePair <Globals.FlukeInput, Globals.BananaPlugs> entry in Globals.FlukeConnections)
        {
            if (entry.Value != Globals.BananaPlugs.noConnection)
            {
                ActivateBananaSlot(entry.Value.ToString());
            }
        }

        foreach (KeyValuePair <Globals.PowerSupplyInput, Globals.BananaPlugs> entry in Globals.PSConnections)
        {
            if (entry.Value != Globals.BananaPlugs.noConnection)
            {
                AddPowerSupplyConnection(entry.Key.ToString(), entry.Value.ToString(), ckt);
                ActivateBananaSlot(entry.Value.ToString());
            }
        }

        //Handle Self BananaPlug Connections
        for (int i = 0; i < 5; i++)
        {
            if (bananaPlugActive[i])
            {
                HandleSelfBananaConnections(i, ckt);
            }
        }

        //INSERT POTENTIOMETER VAL HERE


        if (bananaPlugActive[4])
        {
            if (_potentioMeterTextBox.text.Length == 0)
            {
                ckt.Add(new Resistor("PotentioMeterBanana", "B4", "PotentioMeter", 0));
                Debug.Log("Adding Wire: PotentioMeterBanana B4 PotentioMeter 0");
            }
            else
            {
                ckt.Add(new Resistor("PotentioMeterBanana", "B4", "PotentioMeter", int.Parse(_potentioMeterTextBox.text)));
                Debug.Log("Adding Wire: PotentioMeterBanana B4 PotentioMeter " + _potentioMeterTextBox.text);
            }

            ckt.Add(new Resistor("PotentioMeterBlack", "PotentioMeter", "C67", 0));
            ckt.Add(new Resistor("PotentioMeterBlue", "PotentioMeter", "C70", 0));
            ckt.Add(new Resistor("PotentioMeterRed", "PotentioMeter", "C72", 0));
            Debug.Log("Adding Wire: PotentioMeterBlack PotentioMeter C67 0");
            Debug.Log("Adding Wire: PotentioMeterBlue PotentioMeter C70 0");
            Debug.Log("Adding Wire: PotentioMeterRed PotentioMeter C82 0");
        }
        if (bananaPlugActive[2] || bananaPlugActive[4])
        {
            ckt.Add(new Inductor("B2B4Inductor", "B2", "B4", 0.020f));
            Debug.Log("Adding Inductor: PotentioMeterInductor B2 B4 0.020H");
        }
        // ckt.Add(new Resistor("PotentioMeterBanana", "B4", "PotentioMeter", 500));
        // ckt.Add(new Resistor("PotentioMeterBlack", "PotentioMeter", "C17", 0));
        // ckt.Add(new Resistor("PotentioMeterBlue", "PotentioMeter", "C18", 0));
        // ckt.Add(new Resistor("PotentioMeterRed", "PotentioMeter", "C19", 0));
        // ckt.Add(new Resistor("ShortCircuitInductor", "B2", "B4", 0));
        // ckt.Add(new Inductor("ShortCircuitInductor","B2", "B4",0.020f));

        //handle permanent connections
        for (int i = 0; i < _bananaPlugs.transform.childCount; i++)
        {
            if (bananaPlugActive[i])
            {
                var child = _bananaPlugs.transform.GetChild(i);
                if (child.CompareTag("BBSlot"))
                {
                    AddElectricalElement(child.GetComponent <Slot>(), ckt);
                    child.GetComponent <Slot>().slotChecked = true;
                    child.GetComponent <Slot>().slotPair.GetComponent <Slot>().slotChecked = true;
                }
            }
        }
        for (int i = 0; i < _breadboardUI.transform.childCount; i++)
        {
            var child = _breadboardUI.transform.GetChild(i);
            if (child.CompareTag("BBSlot"))
            {
                if (child.GetComponent <Slot>().itemPlaced != null && !child.GetComponent <Slot>().slotChecked&& !child.GetComponent <Slot>().ignoreThisSlot)
                {
                    if (child.GetComponent <Slot>().slotPair.GetComponent <Slot>().slotType == Globals.SlotType.defaultSlot)
                    {
                        AddElectricalElement(child.GetComponent <Slot>(), ckt);
                        child.GetComponent <Slot>().slotChecked = true;
                        child.GetComponent <Slot>().slotPair.GetComponent <Slot>().slotChecked = true;
                    }
                }
            }
            // if (breadboardSlots[i].GetComponent<Slot>().itemPlaced != null && !breadboardSlots[i].GetComponent<Slot>().slotChecked)
            // {
            //     if (breadboardSlots[i].GetComponent<Slot>().slotPair.GetComponent<Slot>().slotType == Globals.SlotType.defaultSlot)
            //     {

            //         AddElectricalElement(breadboardSlots[i].GetComponent<Slot>(), ckt);
            //         breadboardSlots[i].GetComponent<Slot>().slotChecked = true;
            //         breadboardSlots[i].GetComponent<Slot>().slotPair.GetComponent<Slot>().slotChecked = true;
            //     }
            // }
        }

        // VoltageSource powerSupply = AddVoltageSource(ckt);
        // ckt.Add(powerSupply);


        // Create a DC sweep and register to the event for exporting simulation data
        //var dc = new DC("dc", "voltageSourcePS", _powerSupply.GetComponent<PowerSupply>().powerReading, _powerSupply.GetComponent<PowerSupply>().powerReading, 1);
        //var dc = new DC("dc", "voltageSourcePS", _powerSupplyMachine.GetComponent<PSSelect>().voltage, _powerSupplyMachine.GetComponent<PSSelect>().voltage, 1);

        var dc = new DC("dc");

        if (voltageSource.Name != "a" && currentSource.Name != "b")
        {
            dc = new DC("dc", new[] {
                new ParameterSweep(voltageSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().voltage, _powerSupplyMachine.GetComponent <PSSelect>().voltage, 1)),
                new ParameterSweep(currentSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().current, _powerSupplyMachine.GetComponent <PSSelect>().current, 1))
            });
        }
        else if (voltageSource.Name != "a")
        {
            dc = new DC("dc", new[] {
                new ParameterSweep(voltageSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().voltage, _powerSupplyMachine.GetComponent <PSSelect>().voltage, 1))
            });
        }
        else if (currentSource.Name != "b")
        {
            dc = new DC("dc", new[] {
                new ParameterSweep(currentSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().current, _powerSupplyMachine.GetComponent <PSSelect>().current, 1))
            });
        }


        //GetCircuitToolsReading(dc);
        ManageAgilentReadingsConnections(dc);
        ManageFlukeReadingsConnections(dc);
        // Run the simulation
        try
        {
            dc.Run(ckt);
        }
        catch (ValidationFailedException e)
        {
            Debug.Log("Error is: " + e.ToString());
        }
    }
Beispiel #24
0
 private void checkPlayers()
 {
     if (playerList.Count != playerSquares.Count)
     {
         playerList = new Circuit<Player>();
         for (int i = 0; i < playerSquares.Count; i++)
         {
             Player player = new Player(playerList);
             player.PlayerId = playerList.Count + 1;
             player.PlayerPawns = new List<Pawn>();
             player.PlayerSquare = playerSquares[i];
             switch (i+1)
             {
                 case 1:
                     player.Color = PlayerColor.Blue;
                     break;
                 case 2:
                     player.Color = PlayerColor.Green;
                     break;
                 case 3:
                     player.Color = PlayerColor.Red;
                     break;
                 case 4:
                     player.Color = PlayerColor.Yellow;
                     break;
             }
             for (int j = 0; j < NumberOfPawns; j++)
             {
                 player.PlayerPawns.Add(new Pawn(player.PlayerSquare, player));
             }
             playerList.Add(player);
         }
     }
 }
Beispiel #25
0
        internal Game Load(string s, int humanPlayers)
        {
            int humans=0;
            Game game = Load(s);
            Circuit<Player> players = new Circuit<Player>();
            for (int i = 0; i < baricades.Count; i++)
            {
                baricades[i].Square.Piece = null;
                baricades[i].Square = null;
            }

            baricades = new List<BaricadePiece>();
            while (baricades.Count < baricadeSquares.Count)
            {
                BaricadePiece b = new BaricadePiece();
                baricadeSquares[baricades.Count].Piece = b;
                b.Square = baricadeSquares[baricades.Count];
                baricades.Add(b);
            }

            for(int i=0;i<game.Players.Count;i++)
            {
                Player p = game.Players.pop();
                for(int j=0;j<p.PlayerPawns.Count;j++)
                {
                    p.PlayerPawns[j].Square.removePawn(p.PlayerPawns[j]);
                }
                PlayerColor color = p.Color;
                int id = p.PlayerId;
                PlayerSquare square = p.PlayerSquare;

                if (humans < humanPlayers)
                {
                    p = new Player(players);
                    humans++;
                }
                else
                {
                    p = new AIPlayer(players);
                }
                p.PlayerSquare = square;
                p.PlayerId = id;
                p.Color = color;

                for (int j = 0; j < game.Board.NumberOfPawns; j++)
                {
                    p.addPawn(new Pawn(null,p));
                }
                players.Add(p);
            }
            game.Players = players;
            return game;
        }
Beispiel #26
0
        public Game Load(String uri)
        {
            XmlReader r = XmlReader.Create(uri);

            board = null;
            FinishSquare f = null;
            ForestSquare forest = null;
            Square previous = null;

            playerSquares = new List<PlayerSquare>();
            linkList = new List<Square>();
            playerList = new Circuit<Player>();
            baricades = new List<BaricadePiece>();
            pawns = new List<Pawn>();
            baricadeSquares = new List<Square>();
            connectors = new List<Connector>();

            while (r.Read())
            {
                if (r.Name.ToLower() == "square")
                {
                    Square s = new Square(board);
                    if (s.readElement(r))
                    {
                        s.View = new VSquare(s);
                        insertInto(s.Id, s);
                        previous = s;
                    }
                }

                else if (r.Name.ToLower() == "villagesquare")
                {
                    VillageSquare s = new VillageSquare(board);
                    if (s.readElement(r))
                    {
                        insertInto(s.Id, s);
                        previous = s;
                    }
                }

                else if (r.Name.ToLower() == "baricadesquare")
                {
                    BaricadeSquare s = new BaricadeSquare(board);
                    if (s.readElement(r))
                    {
                        insertInto(s.Id, s);
                        baricadeSquares.Add(s);
                        previous = s;
                    }
                }

                else if (r.Name.ToLower() == "baricadevillagesquare")
                {
                    BaricadeVillageSquare s = new BaricadeVillageSquare(board);
                    if(s.readElement(r))
                    {
                        insertInto(s.Id, s);
                        baricadeSquares.Add(s);
                        previous = s;
                    }
                }

                else if (r.Name.ToLower() == "restsquare")
                {
                    RestSquare s = new RestSquare(board);
                    if(s.readElement(r))
                    {
                        insertInto(s.Id,s);
                    }
                }

                else if (r.Name.ToLower() == "playersquare")
                {
                    PlayerSquare s = new PlayerSquare(board);
                    if (s.readElement(r))
                    {
                        playerSquares.Add(s);
                        insertInto(s.Id, s);
                        previous = s;
                    }
                }

                else if (r.Name.ToLower() == "pawn")
                {
                    Pawn p = new Pawn();
                    if (p.readElement(r))
                    {
                        pawns.Add(p);
                    }

                }

                else if (r.Name.ToLower() == "baricadepiece")
                {
                    BaricadePiece b = new BaricadePiece();
                    if (b.readElement(r))
                    {
                        baricades.Add(b);
                    }
                }

                else if (r.Name.ToLower() == "lowrowsquare")
                {
                    LowRowSquare s = new LowRowSquare(board);
                    if (s.readElement(r))
                    {
                        insertInto(s.Id, s);
                    }
                }

                else if (r.Name.ToLower() == "connector")
                {
                    Connector s = new Connector(board);
                    if (s.readElement(r))
                    {
                        insertInto(s.Id, s);
                        connectors.Add(s);
                    }
                }

                else if (r.Name.ToLower() == "player")
                {
                    Player p = new Player(playerList);
                    if (p.readElement(r))
                    {
                        _numberofPlayers++;
                        _numberOfHumanPlayers++;

                        p.PlayerSquare = (PlayerSquare)linkList[find(p.PlayerSquareId)];
                        p.PlayerId = playerList.Count + 1;
                        playerList.Add(p);
                    }
                }

                else if (r.Name.ToLower() == "aiplayer")
                {
                    AIPlayer p = new AIPlayer(playerList);
                    if (p.readElement(r))
                    {
                        _numberOfAIPlayers++;
                        _numberOfHumanPlayers++;
                        p.PlayerSquare = (PlayerSquare)linkList[find(p.PlayerSquareId)];
                        p.PlayerId = playerList.Count + 1;
                        playerList.Add(p);
                    }
                }

                else if (r.Name.ToLower() == "finishsquare")
                {
                    FinishSquare s = new FinishSquare(board);
                    if (s.readElement(r))
                    {
                        f = s;
                        insertInto(s.Id, s);
                        previous = s;
                    }
                }
                else if (r.Name.ToLower() == "forestsquare")
                {
                    ForestSquare s = new ForestSquare(board);
                    if (s.readElement(r))
                    {
                        forest = s;
                        insertInto(s.Id, s);
                        previous = s;
                    }
                }
                else if (r.Name.ToLower() == "board")
                {
                    Board b = new Board();
                    if (b.readElement(r))
                    {
                        board = b;
                        _numberOfPawns = board.NumberOfPawns;
                    }
                }
            }
            r.Close();
            link();
            setPiecesToSquares();
            setPawnsToPlayer();
            checkPlayers();
            checkPieces();
            playerList.setCurrent(_currentPlayer);
            setPosition(f, f.X, f.Y);
            conectors();
            board.Squares = linkList;
            board.Baricades = baricades;
            if (forest != null)
            {
                board.ForestSquare = forest;
            }
            Game game = new Game(board, playerList, f);
            board.Game = game;
            return game;
        }