Beispiel #1
0
 public NAND()
     : base()
 {
     logic_element = new LogicCircuitSimulator.NAND();
     InputPins.Add(new Visual.Pin(this, PinSide.INPUT, 0));
     InputPins.Add(new Visual.Pin(this, PinSide.INPUT, 1));
     OutputPins.Add(new Visual.Pin(this, PinSide.OUTPUT, 0));
     g_circuit.AddElement(logic_element);
     image_data  = new ConnectableImages.NAND2();
     picture_box = new System.Windows.Forms.PictureBox
     {
         Name     = elements.Count.ToString(),
         Location = new Point(100, 100),
         Size     = new Size(40, 30),
         Image    = image_data.Image
     };
 }
        public static void TestCase_8NandGateSimulation()
        {
            void VerifyStates(Pin x1_out_pin, string x1_out_val,
                              Pin x2_out_pin, string x2_out_val,
                              Pin x3_out_pin, string x3_out_val,
                              Pin x4_out_pin, string x4_out_val,
                              Pin x5_out_pin, string x5_out_val,
                              Pin x6_out_pin, string x6_out_val,
                              Pin x7_out_pin, string x7_out_val,
                              Pin x8_out_pin, string x8_out_val,

                              Pin nand_in0_pin, string nand_in0_val,
                              Pin nand_in1_pin, string nand_in1_val,
                              Pin nand_in2_pin, string nand_in2_val,
                              Pin nand_in3_pin, string nand_in3_val,
                              Pin nand_in4_pin, string nand_in4_val,
                              Pin nand_in5_pin, string nand_in5_val,
                              Pin nand_in6_pin, string nand_in6_val,
                              Pin nand_in7_pin, string nand_in7_val,
                              Pin nand_out_pin, string nand_out_val,

                              Pin y1_in_pin, string y1_in_val,
                              Logic y1_res_real, string y1_res_exp)
            {
                Require.That(x1_out_pin.State.Value == Logic.StringToLogicValue(x1_out_val));
                Require.That(x2_out_pin.State.Value == Logic.StringToLogicValue(x2_out_val));
                Require.That(x3_out_pin.State.Value == Logic.StringToLogicValue(x3_out_val));
                Require.That(x4_out_pin.State.Value == Logic.StringToLogicValue(x4_out_val));
                Require.That(x5_out_pin.State.Value == Logic.StringToLogicValue(x5_out_val));
                Require.That(x6_out_pin.State.Value == Logic.StringToLogicValue(x6_out_val));
                Require.That(x7_out_pin.State.Value == Logic.StringToLogicValue(x7_out_val));
                Require.That(x8_out_pin.State.Value == Logic.StringToLogicValue(x8_out_val));

                Require.That(nand_in0_pin.State.Value == Logic.StringToLogicValue(nand_in0_val));
                Require.That(nand_in1_pin.State.Value == Logic.StringToLogicValue(nand_in1_val));
                Require.That(nand_in2_pin.State.Value == Logic.StringToLogicValue(nand_in2_val));
                Require.That(nand_in3_pin.State.Value == Logic.StringToLogicValue(nand_in3_val));
                Require.That(nand_in4_pin.State.Value == Logic.StringToLogicValue(nand_in4_val));
                Require.That(nand_in5_pin.State.Value == Logic.StringToLogicValue(nand_in5_val));
                Require.That(nand_in6_pin.State.Value == Logic.StringToLogicValue(nand_in6_val));
                Require.That(nand_in7_pin.State.Value == Logic.StringToLogicValue(nand_in7_val));
                Require.That(nand_out_pin.State.Value == Logic.StringToLogicValue(nand_out_val));

                Require.That(y1_in_pin.State.Value == Logic.StringToLogicValue(y1_in_val));
                Require.That(y1_res_real.Value == Logic.StringToLogicValue(y1_res_exp));
            }

            Circuit cir = new Circuit();

            InTerminal x1 = new InTerminal();

            cir.AddElement(x1);
            InTerminal x2 = new InTerminal();

            cir.AddElement(x2);
            InTerminal x3 = new InTerminal();

            cir.AddElement(x3);
            InTerminal x4 = new InTerminal();

            cir.AddElement(x4);
            InTerminal x5 = new InTerminal();

            cir.AddElement(x5);
            InTerminal x6 = new InTerminal();

            cir.AddElement(x6);
            InTerminal x7 = new InTerminal();

            cir.AddElement(x7);
            InTerminal x8 = new InTerminal();

            cir.AddElement(x8);

            NAND nand = new NAND();

            cir.AddElement(nand);
            cir.SetNumberOfInputPins(nand, 8);
            for (byte i = 1; i < 8; i++)
            {
                Require.That(nand.GetPin(PinSide.INPUT, i).ConnectedPin == null);
            }

            OutTerminal y1 = new OutTerminal();

            cir.AddElement(y1);

            Pin x1_out = x1.GetPin(PinSide.OUTPUT, 0);
            Pin x2_out = x2.GetPin(PinSide.OUTPUT, 0);
            Pin x3_out = x3.GetPin(PinSide.OUTPUT, 0);
            Pin x4_out = x4.GetPin(PinSide.OUTPUT, 0);
            Pin x5_out = x5.GetPin(PinSide.OUTPUT, 0);
            Pin x6_out = x6.GetPin(PinSide.OUTPUT, 0);
            Pin x7_out = x7.GetPin(PinSide.OUTPUT, 0);
            Pin x8_out = x8.GetPin(PinSide.OUTPUT, 0);

            Pin nand_in0 = nand.GetPin(PinSide.INPUT, 0);
            Pin nand_in1 = nand.GetPin(PinSide.INPUT, 1);
            Pin nand_in2 = nand.GetPin(PinSide.INPUT, 2);
            Pin nand_in3 = nand.GetPin(PinSide.INPUT, 3);
            Pin nand_in4 = nand.GetPin(PinSide.INPUT, 4);
            Pin nand_in5 = nand.GetPin(PinSide.INPUT, 5);
            Pin nand_in6 = nand.GetPin(PinSide.INPUT, 6);
            Pin nand_in7 = nand.GetPin(PinSide.INPUT, 7);
            Pin nand_out = nand.GetPin(PinSide.OUTPUT, 0);
            Pin y1_in    = y1.GetPin(PinSide.INPUT, 0);

            cir.Connect(x1_out, nand_in0);
            Require.That(x1_out.ConnectedPin == nand_in0);
            Require.That(nand_in0.ConnectedPin == x1_out);
            cir.Connect(x2_out, nand_in1);
            Require.That(x2_out.ConnectedPin == nand_in1);
            Require.That(nand_in1.ConnectedPin == x2_out);
            cir.Connect(x3_out, nand_in2);
            Require.That(x3_out.ConnectedPin == nand_in2);
            Require.That(nand_in2.ConnectedPin == x3_out);
            cir.Connect(x4_out, nand_in3);
            Require.That(x4_out.ConnectedPin == nand_in3);
            Require.That(nand_in3.ConnectedPin == x4_out);
            cir.Connect(x5_out, nand_in4);
            Require.That(x5_out.ConnectedPin == nand_in4);
            Require.That(nand_in4.ConnectedPin == x5_out);
            cir.Connect(x6_out, nand_in5);
            Require.That(x6_out.ConnectedPin == nand_in5);
            Require.That(nand_in5.ConnectedPin == x6_out);
            cir.Connect(x7_out, nand_in6);
            Require.That(x7_out.ConnectedPin == nand_in6);
            Require.That(nand_in6.ConnectedPin == x7_out);
            cir.Connect(x8_out, nand_in7);
            Require.That(x8_out.ConnectedPin == nand_in7);
            Require.That(nand_in7.ConnectedPin == x8_out);
            cir.Connect(nand_out, y1_in);

            x1.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x2.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x3.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x4.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x5.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x6.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x6.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x7.SimulationInputValue = new Logic(LogicValue.LOGIC_0);
            x8.SimulationInputValue = new Logic(LogicValue.LOGIC_0);

            cir.RestartSimulation();
            VerifyStates(x1_out, "U", x2_out, "U", x3_out, "U", x4_out, "U", x5_out, "U", x6_out, "U", x7_out, "U", x8_out, "U",
                         nand_in0, "U", nand_in1, "U", nand_in2, "U", nand_in3, "U", nand_in4, "U", nand_in5, "U", nand_in6, "U", nand_in7, "U", nand_out, "U",
                         y1_in, "U", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(x1_out, "0", x2_out, "0", x3_out, "0", x4_out, "0", x5_out, "0", x6_out, "0", x7_out, "0", x8_out, "0",
                         nand_in0, "0", nand_in1, "0", nand_in2, "0", nand_in3, "0", nand_in4, "0", nand_in5, "0", nand_in6, "0", nand_in7, "0", nand_out, "U",
                         y1_in, "U", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(x1_out, "0", x2_out, "0", x3_out, "0", x4_out, "0", x5_out, "0", x6_out, "0", x7_out, "0", x8_out, "0",
                         nand_in0, "0", nand_in1, "0", nand_in2, "0", nand_in3, "0", nand_in4, "0", nand_in5, "0", nand_in6, "0", nand_in7, "0", nand_out, "1",
                         y1_in, "1", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(x1_out, "0", x2_out, "0", x3_out, "0", x4_out, "0", x5_out, "0", x6_out, "0", x7_out, "0", x8_out, "0",
                         nand_in0, "0", nand_in1, "0", nand_in2, "0", nand_in3, "0", nand_in4, "0", nand_in5, "0", nand_in6, "0", nand_in7, "0", nand_out, "1",
                         y1_in, "1", y1.SimulationResult, "1");
            Console.WriteLine("TestCase_8NandGateSimulation passed.");
        }
        public static void TestCase_SetNumberOfInputPinsNand()
        {
            Circuit c    = new Circuit();
            NAND    nand = new NAND();

            c.AddElement(nand);
            Pin nand_in0 = nand.GetPin(PinSide.INPUT, 0);
            Pin nand_in1 = nand.GetPin(PinSide.INPUT, 1);

            Require.That(nand.GetNumberOfPins(PinSide.INPUT) == 2);
            Require.That(nand.GetNumberOfPins(PinSide.OUTPUT) == 1);

            InTerminal x1 = new InTerminal();
            InTerminal x2 = new InTerminal();

            c.AddElement(x1);
            c.AddElement(x2);
            Pin x1_out = x1.GetPin(PinSide.OUTPUT, 0);
            Pin x2_out = x2.GetPin(PinSide.OUTPUT, 0);

            c.Connect(x1_out, nand_in0);
            Require.That(nand_in0.ConnectedPin == x1_out);
            Require.That(x1_out.ConnectedPin == nand_in0);
            Require.That(nand_in1.ConnectedPin == null);

            c.SetNumberOfInputPins(nand, 8);
            Require.That(nand.GetNumberOfPins(PinSide.INPUT) == 8);
            Require.That(nand.GetNumberOfPins(PinSide.OUTPUT) == 1);
            Require.That(nand_in0.ConnectedPin == x1_out);
            Require.That(x1_out.ConnectedPin == nand_in0);
            for (byte i = 1; i < 8; i++)
            {
                Require.That(nand.GetPin(PinSide.INPUT, i).ConnectedPin == null);
            }

            Pin nand_in6 = nand.GetPin(PinSide.INPUT, 6);

            c.Connect(x2_out, nand_in6);
            Require.That(nand_in6.ConnectedPin == x2_out);
            Require.That(x2_out.ConnectedPin == nand_in6);
            for (byte i = 1; i < 6; i++)
            {
                Require.That(nand.GetPin(PinSide.INPUT, i).ConnectedPin == null);
            }
            Require.That(nand.GetPin(PinSide.INPUT, 7).ConnectedPin == null);

            c.SetNumberOfInputPins(nand, 5);
            Require.That(nand.GetNumberOfPins(PinSide.INPUT) == 5);
            Require.That(nand_in0.ConnectedPin != null);
            Require.That(x2_out.ConnectedPin == null);
            for (byte i = 1; i < 5; i++)
            {
                Require.That(nand.GetPin(PinSide.INPUT, i).ConnectedPin == null);
            }
            c.Disconnect(nand_in0);
            Require.That(x1_out.ConnectedPin == null);
            Require.That(nand_in0.ConnectedPin == null);

            c.RemoveElement(nand);
            Console.WriteLine("TestCase_SetNumberOfInputPinsNand passed.");
        }
        public static void TestCase_Nand_Nor_GatesSimulation()
        {
            void VerifyStates(Pin x1_out_pin, string x1_out_val,
                              Pin x2_out_pin, string x2_out_val,
                              Pin x3_out_pin, string x3_out_val,
                              Pin nand_in0_pin, string nand_in0_val,
                              Pin nand_in1_pin, string nand_in1_val,
                              Pin nand_out_pin, string nand_out_val,
                              Pin nor_in0_pin, string nor_in0_val,
                              Pin nor_in1_pin, string nor_in1_val,
                              Pin nor_out_pin, string nor_out_val,
                              Pin y1_in_pin, string y1_in_val,
                              Logic y1_res_real, string y1_res_exp)
            {
                Require.That(x1_out_pin.State.Value == Logic.StringToLogicValue(x1_out_val));
                Require.That(x2_out_pin.State.Value == Logic.StringToLogicValue(x2_out_val));
                Require.That(x3_out_pin.State.Value == Logic.StringToLogicValue(x3_out_val));

                Require.That(nand_in0_pin.State.Value == Logic.StringToLogicValue(nand_in0_val));
                Require.That(nand_in1_pin.State.Value == Logic.StringToLogicValue(nand_in1_val));
                Require.That(nand_out_pin.State.Value == Logic.StringToLogicValue(nand_out_val));

                Require.That(nor_in0_pin.State.Value == Logic.StringToLogicValue(nor_in0_val));
                Require.That(nor_in1_pin.State.Value == Logic.StringToLogicValue(nor_in1_val));
                Require.That(nor_out_pin.State.Value == Logic.StringToLogicValue(nor_out_val));

                Require.That(y1_in_pin.State.Value == Logic.StringToLogicValue(y1_in_val));
                Require.That(y1_res_real.Value == Logic.StringToLogicValue(y1_res_exp));
            }

            Circuit    cir = new Circuit();
            InTerminal x1  = new InTerminal();

            cir.AddElement(x1);
            InTerminal x2 = new InTerminal();

            cir.AddElement(x2);
            InTerminal x3 = new InTerminal();

            cir.AddElement(x3);

            NAND nand = new NAND();

            cir.AddElement(nand);

            NOR nor = new NOR();

            cir.AddElement(nor);

            OutTerminal y1 = new OutTerminal();

            cir.AddElement(y1);

            Pin x1_out   = x1.GetPin(PinSide.OUTPUT, 0);
            Pin x2_out   = x2.GetPin(PinSide.OUTPUT, 0);
            Pin x3_out   = x3.GetPin(PinSide.OUTPUT, 0);
            Pin nand_in0 = nand.GetPin(PinSide.INPUT, 0);
            Pin nand_in1 = nand.GetPin(PinSide.INPUT, 1);
            Pin nand_out = nand.GetPin(PinSide.OUTPUT, 0);
            Pin nor_in0  = nor.GetPin(PinSide.INPUT, 0);
            Pin nor_in1  = nor.GetPin(PinSide.INPUT, 1);
            Pin nor_out  = nor.GetPin(PinSide.OUTPUT, 0);
            Pin y1_in    = y1.GetPin(PinSide.INPUT, 0);

            cir.Connect(x1_out, nand_in0);
            cir.Connect(x2_out, nand_in1);
            cir.Connect(nand_out, nor_in0);
            cir.Connect(x3_out, nor_in1);
            cir.Connect(nor_out, y1_in);

            x1.SimulationInputValue = new Logic(LogicValue.LOGIC_1);
            x2.SimulationInputValue = new Logic(LogicValue.LOGIC_1);
            x3.SimulationInputValue = new Logic(LogicValue.LOGIC_0);

            cir.RestartSimulation();
            VerifyStates(
                x1_out, "U", x2_out, "U", x3_out, "U",
                nand_in0, "U", nand_in1, "U", nand_out, "U",
                nor_in0, "U", nor_in1, "U", nor_out, "U",
                y1_in, "U", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(
                x1_out, "1", x2_out, "1", x3_out, "0",
                nand_in0, "1", nand_in1, "1", nand_out, "U",
                nor_in0, "U", nor_in1, "0", nor_out, "U",
                y1_in, "U", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(
                x1_out, "1", x2_out, "1", x3_out, "0",
                nand_in0, "1", nand_in1, "1", nand_out, "0",
                nor_in0, "0", nor_in1, "0", nor_out, "U",
                y1_in, "U", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(
                x1_out, "1", x2_out, "1", x3_out, "0",
                nand_in0, "1", nand_in1, "1", nand_out, "0",
                nor_in0, "0", nor_in1, "0", nor_out, "1",
                y1_in, "1", y1.SimulationResult, "U");

            cir.NextMoment();
            VerifyStates(
                x1_out, "1", x2_out, "1", x3_out, "0",
                nand_in0, "1", nand_in1, "1", nand_out, "0",
                nor_in0, "0", nor_in1, "0", nor_out, "1",
                y1_in, "1", y1.SimulationResult, "1");

            Console.WriteLine("TestCase_Nand_Nor_GatesSimulation passed.");
        }