예제 #1
0
        public void Test_AddAndRemoveComponentsToCircuit()
        {
            Circuit   circuit       = new Circuit();
            TrueConst true_constant = new TrueConst();

            Assert.That(() => circuit.AddComponent(true_constant), Throws.Nothing);
            // Check we can't add the same component twice:
            Assert.That(() => circuit.AddComponent(true_constant), Throws.ArgumentException);
            // Try removing:
            Assert.That(() => circuit.RemoveComponent(true_constant), Throws.Nothing);
            Assert.That(() => circuit.RemoveComponent(true_constant), Throws.TypeOf <KeyNotFoundException>());
            // Try re-adding again:
            Assert.That(() => circuit.AddComponent(true_constant), Throws.Nothing);
        }
예제 #2
0
        public static Circuit ACVSourceTestCircuit()
        {
            var testCkt = new Circuit("ACVSourceTestCircuit");
            var gnd     = testCkt.Ground;
            var node0   = testCkt.GenNode();

            testCkt.AddComponent(new Resistor("R1", 1, node0, gnd));

            var vs = new VSource("vSin", 1, node0, gnd);

            vs.SetSine(2, 4, 50E-3, -Math.PI / 4);
            testCkt.AddComponent(vs);
            return(testCkt);
        }
예제 #3
0
        public void Test_RemoveComponent()
        {
            Circuit circuit    = new Circuit();
            var     and_gate   = new AndGate();
            var     true_const = new TrueConst();

            circuit.AddComponent(and_gate);
            circuit.AddComponent(true_const);
            circuit.Connect(true_const, 0, and_gate, 0);
            circuit.Connect(and_gate, 0, and_gate, 1);

            circuit.RemoveComponent(and_gate);
            circuit.RemoveComponent(true_const);
        }
예제 #4
0
        public static Circuit DCTestCircuit()
        {
            var testCkt = new Circuit("DCTestCircuit");
            var gnd     = testCkt.Ground;
            var node1   = testCkt.GenNode();
            var node2   = testCkt.GenNode();
            var node3   = testCkt.GenNode();

            testCkt.AddComponent(new Resistor("R1", 2, node1, gnd));
            testCkt.AddComponent(new Resistor("R2", 4, node2, node3));
            testCkt.AddComponent(new Resistor("R3", 8, node2, gnd));
            testCkt.AddComponent(new VSource("Vs1", 32, node2, node1));
            testCkt.AddComponent(new VSource("Vs2", 20, node3, gnd));
            return(testCkt);
        }
예제 #5
0
파일: TrueOrTile.cs 프로젝트: ITC-Tec21/MVP
    public override void RefreshTile(Vector3Int location, ITilemap tilemap)
    {
        if (tilemap.GetTile(location))
        {
            OrGate gate = new OrGate(location, tilemap);
            Circuit.AddComponent(location, gate);
            gate.previousTile = replacedTile;
        }
        else
        {
            Circuit.RemoveComponent(location);
        }
        tilemap.RefreshTile(location);

        if (Circuit.circuitComponents.ContainsKey(location))
        {
            CircuitComponent component = Circuit.circuitComponents[location];
            foreach (CircuitComponent inComponent in component.ins)
            {
                tilemap.RefreshTile(inComponent.location);
            }
            foreach (CircuitComponent outComponent in component.outs)
            {
                tilemap.RefreshTile(outComponent.location);
            }
        }
    }
예제 #6
0
    public override void RefreshTile(Vector3Int location, ITilemap tilemap)
    {
        if (tilemap.GetTile(location))
        {
            Circuit.AddComponent(location, new Wire(location, tilemap));
        }
        else
        {
            Circuit.RemoveComponent(location);
        }
        tilemap.RefreshTile(location);

        if (Circuit.circuitComponents.ContainsKey(location))
        {
            CircuitComponent component = Circuit.circuitComponents[location];
            foreach (CircuitComponent inComponent in component.ins)
            {
                tilemap.RefreshTile(inComponent.location);
            }
            foreach (CircuitComponent outComponent in component.outs)
            {
                tilemap.RefreshTile(outComponent.location);
            }
        }
    }
예제 #7
0
        public static Circuit RLTestCircuit()
        {
            var testCkt = new Circuit("RLTestCircuit");
            var gnd     = testCkt.Ground;
            var node0   = testCkt.GenNode();
            var node1   = testCkt.GenNode();

            testCkt.AddComponent(new Resistor("R1", 1E3, node0, node1));
            testCkt.AddComponent(new Inductor("L1", 1, node1, gnd));

            var vs = new VSource("vStep", 1, node0, gnd);

            vs.SetStep(0, 0, 1E-9);
            testCkt.AddComponent(vs);
            return(testCkt);
        }
예제 #8
0
        public void Test_CircuitSimulate_SelfCycle()
        {
            LogicComponent not_gate   = new NotGate();
            LogicComponent connection = new Connection();
            Circuit        circuit    = new Circuit();

            /* The circuit is just a not gate whose output is connected to its own input by a wire */
            circuit.AddComponent(not_gate);
            circuit.AddComponent(connection);
            circuit.Connect(not_gate, 0, connection, 0);
            circuit.Connect(connection, 0, not_gate, 0);

            /* The circuit's state should repeat every 4 simulate steps.
             * The expected result of each of the 4 simulate steps in order:
             * 1) The output of the not gate switches to True.
             * 2) The output of the connection switches to True.
             * 3) The output of the not gate switches back to False.
             * 4) The output of connection switches back to False.
             */
            for (int i = 0; i < 100; i++)
            {
                // After first step, not gate should have true output:
                circuit.Simulate();
                Assert.AreEqual(not_gate.Outputs, new List <bool>()
                {
                    true
                });
                // After second step, connection should have true output too:
                circuit.Simulate();
                Assert.AreEqual(connection.Outputs, new List <bool>()
                {
                    true
                });
                // After third step, not gate should have false output again:
                circuit.Simulate();
                Assert.AreEqual(not_gate.Outputs, new List <bool>()
                {
                    false
                });
                // After fourth step, connection should have false output again:
                circuit.Simulate();
                Assert.AreEqual(connection.Outputs, new List <bool>()
                {
                    false
                });
            }
        }
예제 #9
0
        public void Test_Clock()
        {
            // Assert that clock with zero period is invalid
            Assert.That(() => new Clock(0), Throws.TypeOf <ArgumentOutOfRangeException>());

            // Check outputs for different periods
            for (uint i = 1; i < 100; i++)
            {
                Circuit        circuit = new Circuit();
                LogicComponent clock   = new Clock(i);
                circuit.AddComponent(clock);
                for (uint j = 0; j < 400; j++)
                {
                    bool expected = ((j / i) % 2 != 0);
                    Assert.AreEqual(clock.Outputs[0], expected);
                    circuit.Simulate();
                }
            }
        }
예제 #10
0
        public void Test_CircuitSimulate_SmallTree()
        {
            LogicComponent true_constant1 = new TrueConst();
            LogicComponent true_constant2 = new TrueConst();
            LogicComponent false_constant = new FalseConst();
            LogicComponent and_gate       = new AndGate();
            LogicComponent or_gate        = new OrGate();

            LogicComponent true_constant1_connection = new Connection();
            LogicComponent true_constant2_connection = new Connection();
            LogicComponent false_constant_connection = new Connection();
            LogicComponent and_gate_connection       = new Connection();

            Circuit circuit = new Circuit();

            /* Test will be 2 TRUEs connected to an AND gate.
             * This AND gate and a FALSE will then be connected to the OR gate.
             */
            circuit.AddComponent(true_constant1);
            circuit.AddComponent(true_constant2);
            circuit.AddComponent(false_constant);
            circuit.AddComponent(and_gate);
            circuit.AddComponent(or_gate);
            circuit.AddComponent(true_constant1_connection);
            circuit.AddComponent(true_constant2_connection);
            circuit.AddComponent(false_constant_connection);
            circuit.AddComponent(and_gate_connection);

            circuit.Connect(true_constant1, 0, true_constant1_connection, 0);
            circuit.Connect(true_constant2, 0, true_constant2_connection, 0);
            circuit.Connect(true_constant1_connection, 0, and_gate, 0);
            circuit.Connect(true_constant2_connection, 0, and_gate, 1);
            circuit.Connect(and_gate, 0, and_gate_connection, 0);
            circuit.Connect(false_constant, 0, false_constant_connection, 0);
            circuit.Connect(and_gate_connection, 0, or_gate, 0);
            circuit.Connect(false_constant_connection, 0, or_gate, 1);

            // Try first step: the output of the connections from the constants should change.
            circuit.Simulate();
            Assert.AreEqual(true_constant1_connection.Outputs, new List <bool>()
            {
                true
            });
            Assert.AreEqual(true_constant2_connection.Outputs, new List <bool>()
            {
                true
            });
            // Try second step: the output from the and gate should change.
            circuit.Simulate();
            Assert.AreEqual(and_gate.Outputs, new List <bool>()
            {
                true
            });
            // Try third step: the output of the connections from the and gate should change.
            circuit.Simulate();
            Assert.AreEqual(and_gate_connection.Outputs, new List <bool>()
            {
                true
            });
            // Try fourth step: the output of the or gate should change.
            circuit.Simulate();
            Assert.AreEqual(or_gate.Outputs, new List <bool>()
            {
                true
            });

            // Ensure the state has stabilized into the expected state:
            for (int i = 0; i < 100; i++)
            {
                circuit.Simulate();
                Assert.AreEqual(true_constant1_connection.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(true_constant2_connection.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(and_gate.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(and_gate_connection.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(or_gate.Outputs, new List <bool>()
                {
                    true
                });
            }
        }
예제 #11
0
 public void AddCompToCurrent(Component c)
 {
     CurrentCircuit.AddComponent(c);
     SelectedComp = c;
 }