예제 #1
0
 private static void TestWireSet_2sComplement(WireSet wireSet, int value)
 {
     wireSet.Set2sComplement(value);
     if (value != wireSet.Get2sComplement())
     {
         Console.WriteLine("bugbug wireset");
     }
 }
예제 #2
0
        public override bool TestGate()
        {
            WireSet ws1 = new WireSet(5);
            WireSet ws2 = new WireSet(5);

            ws1.SetValue(5);
            ws2.Set2sComplement(7);
            this.ConnectInput1(ws1);
            this.ConnectInput2(ws2);
            Console.WriteLine("TESTING Or..Is it true?    :MULTI_BIT_ADDER " + this.ToString());
            return(true);
        }
예제 #3
0
        public override bool TestGate()
        {
            Random  rand     = new Random();
            WireSet randWire = new WireSet(Size);
            WireSet maxWire  = new WireSet(Size);

            maxWire.Set2sComplement((int)Math.Pow(2, Size - 1) - 1);

            for (int i = 0; i < 1000; i++)
            {
                int num = (int)rand.Next(-(int)Math.Pow(2, Size - 1), ((int)Math.Pow(2, Size - 1) - 1));
                randWire.Set2sComplement(num);

                Input.Set2sComplement(randWire.Get2sComplement());
                Load.Value = 1;

                Clock.ClockDown();
                Clock.ClockUp();

                Load.Value = 0;
                Input.Set2sComplement(maxWire.Get2sComplement());
                if (randWire.Get2sComplement() != Output.Get2sComplement())
                {
                    return(false);
                }

                Clock.ClockDown();
                Clock.ClockUp();

                if (randWire.Get2sComplement() != Output.Get2sComplement())
                {
                    return(false);
                }

                Load.Value = 1;

                Clock.ClockDown();
                Clock.ClockUp();

                if (Input.Get2sComplement() != Output.Get2sComplement())
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #4
0
        private bool Test(int wordBitsFilter, int x, int y, int zx, int nx, int zy, int ny, int f, int no)
        {
            if (zx != 0)
            {
                x = 0;
            }
            if (nx != 0)
            {
                x = ~x;
            }
            if (zy != 0)
            {
                y = 0;
            }
            if (ny != 0)
            {
                y = ~y;
            }

            int output;

            if (f == 0)
            {
                output = x & y;
            }
            else
            {
                output = x + y;
            }

            if (no != 0)
            {
                output = ~output;
            }

            output = output & wordBitsFilter;
            var wsExpectedOutput = new WireSet(Size);

            wsExpectedOutput.Set2sComplement(output);

            int expectedZr = output == 0 ? 1 : 0;
            int expectedNg = wsExpectedOutput.Get2sComplement() < 0 ? 1 : 0;

            return(TestOutput(wsExpectedOutput, expectedZr, expectedNg));
        }
예제 #5
0
        public override bool TestGate()
        {
            int bits = Size - 1;

            for (int i = 0; i < 10; i++)
            {
                Random  rand = new Random();
                int     num1 = rand.Next((-(int)Math.Pow(2, bits - 1)), ((int)Math.Pow(2, bits - 1) - 1) + 1);
                int     num2 = rand.Next((-(int)Math.Pow(2, bits - 1)), ((int)Math.Pow(2, bits - 1) - 1) + 1);
                WireSet sum  = new WireSet(Size);
                sum.Set2sComplement(num1 + num2);
                Input1.Set2sComplement(num1);
                Input2.Set2sComplement(num2);
                if (Output.Get2sComplement() != sum.Get2sComplement())
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #6
0
        static void Main(string[] args)
        {
            OrGate               or        = new OrGate();
            XorGate              xor       = new XorGate();
            AndGate              and       = new AndGate();
            MuxGate              mux       = new MuxGate();
            Demux                demux     = new Demux();
            HalfAdder            halfAdder = new HalfAdder();
            FullAdder            fullAdder = new FullAdder();
            WireSet              wireSet   = new WireSet(9);
            BitwiseAndGate       bwag      = new BitwiseAndGate(2);
            BitwiseNotGate       bwng      = new BitwiseNotGate(3);
            BitwiseOrGate        bwog      = new BitwiseOrGate(2);
            BitwiseMux           bwm       = new BitwiseMux(2);
            BitwiseDemux         bwd       = new BitwiseDemux(2);
            MultiBitAndGate      mbag      = new MultiBitAndGate(4);
            MultiBitAdder        mba       = new MultiBitAdder(3);
            BitwiseMultiwayMux   bwmwm     = new BitwiseMultiwayMux(5, 4);
            BitwiseMultiwayDemux bwmwd     = new BitwiseMultiwayDemux(4, 4);
            SingleBitRegister    sbr       = new SingleBitRegister();
            MultiBitRegister     mbr       = new MultiBitRegister(4);

            if (!sbr.TestGate())
            {
                Console.WriteLine("SingleBitRegisterbugbug");
            }
            if (!mbr.TestGate())
            {
                Console.WriteLine("MultiBitRegisterbugbug");
            }
            ALU alu = new ALU(4);

            if (!alu.TestGate())
            {
                Console.WriteLine("ALUbugbug");
            }
            if (!bwmwd.TestGate())
            {
                Console.WriteLine("BitwiseMultiwayDemuxbugbug");
            }
            if (!bwmwm.TestGate())
            {
                Console.WriteLine("BitwiseMultiwayMuxbugbug");
            }
            if (!mba.TestGate())
            {
                Console.WriteLine("MultiBitAdderbugbug");
            }
            if (!mbag.TestGate())
            {
                Console.WriteLine("MultiBitAndGatebugbug");
            }
            if (!bwd.TestGate())
            {
                Console.WriteLine("BitWiseDemuxbugbug");
            }
            if (!bwm.TestGate())
            {
                Console.WriteLine("BitWiseMuxbugbug");
            }
            if (!bwog.TestGate())
            {
                Console.WriteLine("BitWiseOrGatebugbug");
            }
            if (!bwng.TestGate())
            {
                Console.WriteLine("BitWiseNotGatebugbug");
            }
            if (!bwag.TestGate())
            {
                Console.WriteLine("BitWiseAndGatebugbug");
            }
            wireSet.SetValue(137);
            wireSet.Set2sComplement(-32);
            if (!and.TestGate())
            {
                Console.WriteLine("andbugbug");
            }
            if (!or.TestGate())
            {
                Console.WriteLine("orbugbug");
            }
            if (!xor.TestGate())
            {
                Console.WriteLine("xorbugbug");
            }
            if (!mux.TestGate())
            {
                Console.WriteLine("muxbugbug");
            }
            if (!demux.TestGate())
            {
                Console.WriteLine("demuxbugbug");
            }
            if (!halfAdder.TestGate())
            {
                Console.WriteLine("HAbugbug");
            }
            if (!fullAdder.TestGate())
            {
                Console.WriteLine("FAbugbug");
            }
            Memory memory = new Memory(2, 6);

            if (!memory.TestGate())
            {
                Console.WriteLine("Membugbug");
            }
            Console.WriteLine("done");
            Console.ReadLine();
        }
예제 #7
0
파일: Program.cs 프로젝트: alongigi/Memory
        static void Main(string[] args)
        {
            AndGate   and   = new AndGate();
            OrGate    or    = new OrGate();
            XorGate   xor   = new XorGate();
            MuxGate   mux   = new MuxGate();
            Demux     demux = new Demux();
            HalfAdder ha    = new HalfAdder();
            FullAdder fa    = new FullAdder();

            if (!and.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done And");

            if (!or.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done Or");

            if (!xor.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done Xor");

            if (!mux.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done Mux");

            if (!demux.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done Demux");

            if (!ha.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done HalfAdder");

            if (!fa.TestGate())
            {
                Console.WriteLine("bugbug");
            }



            Console.WriteLine("done FullAdder");



            WireSet num = new WireSet(4);

            num.SetValue(6);
            Console.WriteLine(num.ToString());
            Console.WriteLine(num.GetValue());

            Console.WriteLine("trying 2's complement");
            WireSet num2 = new WireSet(4);

            num2.Set2sComplement(6);
            Console.WriteLine(num2.Get2sComplement());
            WireSet num3 = new WireSet(4);

            num3.Set2sComplement(-2);
            Console.WriteLine(num3.Get2sComplement());

            BitwiseAndGate BWAnd = new BitwiseAndGate(3);

            BWAnd.TestGate();

            BitwiseNotGate BWNot = new BitwiseNotGate(3);

            BWNot.TestGate();

            BitwiseOrGate BWOr = new BitwiseOrGate(3);

            BWOr.TestGate();

            BitwiseMux BWMux = new BitwiseMux(3);

            BWMux.TestGate();

            BitwiseDemux BWDemux = new BitwiseDemux(3);

            BWDemux.TestGate();

            MultiBitAndGate multiAnd = new MultiBitAndGate(3);

            multiAnd.TestGate();

            MultiBitAdder multiAdd = new MultiBitAdder(5);

            multiAdd.TestGate();

            BitwiseMultiwayMux multimux = new BitwiseMultiwayMux(8, 2);

            WireSet[] inp = new WireSet[4];
            inp[0] = new WireSet(8);
            inp[0].Set2sComplement(1);
            multimux.ConnectInput(0, inp[0]);
            inp[1] = new WireSet(8);
            inp[1].Set2sComplement(2);
            multimux.ConnectInput(1, inp[1]);
            inp[2] = new WireSet(8);
            inp[2].Set2sComplement(3);
            multimux.ConnectInput(2, inp[2]);
            inp[3] = new WireSet(8);
            inp[3].Set2sComplement(4);
            multimux.ConnectInput(3, inp[3]);



            WireSet control = new WireSet(2);

            control.Set2sComplement(3);
            multimux.ConnectControl(control);

            multimux.TestGate();

            BitwiseMultiwayDemux multidemux = new BitwiseMultiwayDemux(8, 1);


            ALU alu = new ALU(16);

            alu.TestGate();



            SingleBitRegister sbr = new SingleBitRegister();

            if (sbr.TestGate())
            {
                Console.WriteLine("sbr IS OK");
            }

            MultiBitRegister mbr = new MultiBitRegister(8);

            if (mbr.TestGate())
            {
                Console.WriteLine("mbr IS OK");
            }

            Memory mem = new Memory(3, 6);

            if (mem.TestGate())
            {
                Console.WriteLine("mem IS OK");
            }



            Console.ReadLine();
        }
예제 #8
0
        static void Main(string[] args)
        {
            //This is an example of a testing code that you should run for all the gates that you create
//
//            //Create a gate
//            AndGate and = new AndGate();
//            //Test that the unit testing works properly
//            if (!and.TestGate())
//                Console.WriteLine("bugbug");
//
//            //test or gate
//            OrGate or = new OrGate();
//            if (or.TestGate())
//                Console.WriteLine("done or");
//
            //test xor gate
//            XorGate xor = new XorGate();
//            if (xor.TestGate())
//                Console.WriteLine("done xor");
//
//            MultiBitAndGate mbaGate = new MultiBitAndGate(4);
//            if (mbaGate.TestGate())
//                Console.WriteLine("done mba");
//
//            MultiBitOrGate mboGate = new MultiBitOrGate(4);
//            if (mboGate.TestGate())
//                Console.WriteLine("done mbo");
//
//            MuxGate mux = new MuxGate();
//            if (mux.TestGate())
//                Console.WriteLine("done mux");
//
//            Demux demux = new Demux();
//            if (demux.TestGate())
//                Console.WriteLine("done demux");
//
//            BitwiseAndGate bwAg = new BitwiseAndGate(4);
//            if (bwAg.TestGate())
//                Console.WriteLine("done bwAg");
//
//            BitwiseNotGate bwNg = new BitwiseNotGate(4);
//            if (bwNg.TestGate())
//                Console.WriteLine("done bwNg");
////
//            BitwiseOrGate bwOg = new BitwiseOrGate(4);
//            if (bwOg.TestGate())
//                Console.WriteLine("done bwOg");
//
//
//            WireSet ws = new WireSet(4);
//            ws.SetValue(8);
//            Console.WriteLine(ws.ToString());
////
//            BitwiseMux bwMux = new BitwiseMux(4);
//            if (bwMux.TestGate())
//                Console.WriteLine("done bwMux");
//
//            BitwiseDemux bwDemux = new BitwiseDemux(4);
//            if (bwDemux.TestGate())
//                Console.WriteLine("done bwDemux");
//
//            BitwiseMultiwayMux bwMwMux = new BitwiseMultiwayMux(3,3);
//            if (bwMwMux.TestGate())
//                Console.WriteLine("done bwMwMux");


            OrGate               or        = new OrGate();
            XorGate              xor       = new XorGate();
            AndGate              and       = new AndGate();
            MuxGate              mux       = new MuxGate();
            Demux                demux     = new Demux();
            HalfAdder            halfAdder = new HalfAdder();
            FullAdder            fullAdder = new FullAdder();
            WireSet              wireSet   = new WireSet(9);
            BitwiseAndGate       bwag      = new BitwiseAndGate(2);
            BitwiseNotGate       bwng      = new BitwiseNotGate(3);
            BitwiseOrGate        bwog      = new BitwiseOrGate(2);
            BitwiseMux           bwm       = new BitwiseMux(2);
            BitwiseDemux         bwd       = new BitwiseDemux(2);
            MultiBitAndGate      mbag      = new MultiBitAndGate(4);
            MultiBitAdder        mba       = new MultiBitAdder(3);
            BitwiseMultiwayMux   bwmwm     = new BitwiseMultiwayMux(5, 4);
            BitwiseMultiwayDemux bwmwd     = new BitwiseMultiwayDemux(4, 4);
            SingleBitRegister    sbr       = new SingleBitRegister();
            MultiBitRegister     mbr       = new MultiBitRegister(4);

            wireSet.SetValue(137);
            wireSet.Set2sComplement(-32);
            if (halfAdder.TestGate())
            {
                Console.WriteLine("HAbugbug");
            }
            if (fullAdder.TestGate())
            {
                Console.WriteLine("FAbugbug");
            }
            if (mba.TestGate())
            {
                Console.WriteLine("MultiBitAdderbugbug");
            }
            ALU alu = new ALU(16);

            if (alu.TestGate())
            {
                Console.WriteLine("ALU bugbug");
            }

            Console.WriteLine("FINISH HIM");
        }
예제 #9
0
        public ALU(int iSize)
        {
            Size      = iSize;
            InputX    = new WireSet(Size);
            InputY    = new WireSet(Size);
            ZeroX     = new Wire();
            ZeroY     = new Wire();
            NotX      = new Wire();
            NotY      = new Wire();
            F         = new Wire();
            NotOutput = new Wire();
            Negative  = new Wire();
            Zero      = new Wire();
            Output    = new WireSet(iSize);

            muxZx  = new BitwiseMux(iSize);
            muxZy  = new BitwiseMux(iSize);
            muxNx  = new BitwiseMux(iSize);
            muxNy  = new BitwiseMux(iSize);
            muxF   = new BitwiseMux(iSize);
            muxNo  = new BitwiseMux(iSize);
            notNx  = new BitwiseNotGate(iSize);
            notNy  = new BitwiseNotGate(iSize);
            notNo  = new BitwiseNotGate(iSize);
            andF   = new BitwiseAndGate(iSize);
            adderF = new MultiBitAdder(iSize);
            wireX0 = new WireSet(iSize);
            wireX0.Set2sComplement(0);
            wireY0 = new WireSet(iSize);
            wireY0.Set2sComplement(0);
            isZeroNot      = new BitwiseNotGate(iSize);
            isZeroMultiAnd = new MultiBitAndGate(iSize);

            muxZx.ConnectInput1(InputX);
            muxZx.ConnectInput2(wireX0);
            muxZx.ConnectControl(ZeroX);
            muxNx.ConnectInput1(muxZx.Output);
            notNx.ConnectInput(muxZx.Output);
            muxNx.ConnectInput2(notNx.Output);
            muxNx.ConnectControl(NotX);
            andF.ConnectInput1(muxNx.Output);
            adderF.ConnectInput1(muxNx.Output);

            muxZy.ConnectInput1(InputY);
            muxZy.ConnectInput2(wireY0);
            muxZy.ConnectControl(ZeroY);
            muxNy.ConnectInput1(muxZy.Output);
            notNy.ConnectInput(muxZy.Output);
            muxNy.ConnectInput2(notNy.Output);
            muxNy.ConnectControl(NotY);
            andF.ConnectInput2(muxNy.Output);
            adderF.ConnectInput2(muxNy.Output);
            //now from muxF
            muxF.ConnectInput1(andF.Output);
            muxF.ConnectInput2(adderF.Output);
            muxF.ConnectControl(F);
            muxNo.ConnectInput1(muxF.Output);
            notNo.ConnectInput(muxF.Output);
            muxNo.ConnectInput2(notNo.Output);
            muxNo.ConnectControl(NotOutput);
            Output.ConnectInput(muxNo.Output);
            Negative.ConnectInput(muxNo.Output[iSize - 1]);
            isZeroNot.ConnectInput(muxNo.Output);
            isZeroMultiAnd.ConnectInput(isZeroNot.Output);
            Zero.ConnectInput(isZeroMultiAnd.Output);
        }
예제 #10
0
파일: ALU.cs 프로젝트: Serfati/Memory
        public ALU(int iSize)
        {
            Size      = iSize;
            InputX    = new WireSet(Size);
            InputY    = new WireSet(Size);
            ZeroX     = new Wire();
            ZeroY     = new Wire();
            NotX      = new Wire();
            NotY      = new Wire();
            F         = new Wire();
            NotOutput = new Wire();
            Negative  = new Wire();
            Zero      = new Wire();
            Output    = new WireSet(Size);


            var fx     = new BitwiseMux(Size);
            var fAdder = new MultiBitAdder(Size);
            var fAnd   = new BitwiseAndGate(Size);

            var notX = new BitwiseNotGate(Size);
            var notY = new BitwiseNotGate(Size);

            var notCheckzero = new BitwiseNotGate(Size);
            var zeroCheck    = new MultiBitAndGate(Size);
            var nOut         = new BitwiseMux(Size);
            var notOut       = new BitwiseNotGate(Size);

            var nx = new BitwiseMux(Size);
            var ny = new BitwiseMux(Size);
            var zx = new BitwiseMux(Size);
            var zy = new BitwiseMux(Size);

            var wireX0 = new WireSet(iSize);
            var wireY0 = new WireSet(iSize);

            wireX0.Set2sComplement(0);
            wireY0.Set2sComplement(0);

            try
            {
                //zx//
                var zeroXWire = new WireSet(Size);
                zeroXWire.Set2sComplement(0);
                zx.ConnectInput1(InputX);
                zx.ConnectInput2(zeroXWire);
                zx.ConnectControl(ZeroX);
            }
            catch (Exception e)
            {
                Console.WriteLine("zx exp" + e.Message);
            }

            try
            {
                //nx//
                nx.ConnectInput1(zx.Output);
                notX.ConnectInput(zx.Output);
                nx.ConnectInput2(notX.Output);
                nx.ConnectControl(NotX);
            }
            catch (Exception e)
            {
                Console.WriteLine("nx exp" + e.Message);
            }

            try
            {
                //zy//
                var zeroYWire = new WireSet(Size);
                zeroYWire.Set2sComplement(0);
                zy.ConnectInput1(InputY);
                zy.ConnectInput2(zeroYWire);
                zy.ConnectControl(ZeroY);
            }
            catch (Exception e)
            {
                Console.WriteLine("zy exp" + e.Message);
            }

            try
            {
                //ny//
                ny.ConnectInput1(zy.Output);
                notY.ConnectInput(zy.Output);
                ny.ConnectInput2(notY.Output);
                ny.ConnectControl(NotY);
            }
            catch (Exception e)
            {
                Console.WriteLine("ny exp" + e.Message);
            }

            try
            {
                //f//
                fAdder.ConnectInput1(nx.Output);
                fAdder.ConnectInput2(ny.Output);

                fAnd.ConnectInput1(nx.Output);
                fAnd.ConnectInput2(ny.Output);

                fx.ConnectInput1(fAnd.Output);
                fx.ConnectInput2(fAdder.Output);

                fx.ConnectControl(F);
            }
            catch (Exception e)
            {
                Console.WriteLine("f exp" + e.Message);
                throw;
            }

            try
            {
                notOut.ConnectInput(fx.Output);
                nOut.ConnectInput1(fx.Output);
                nOut.ConnectInput2(notOut.Output);
                nOut.ConnectControl(NotOutput);

                /*********************************/
                Output = nOut.Output;
                /*********************************/
            }
            catch (Exception e)
            {
                Console.WriteLine("not Output exp" + e.Message);
            }

            try
            {
                //negative number check
                Negative = Output[Size - 1];

                //Zero number check
                notCheckzero.ConnectInput(nOut.Output);
                zeroCheck.ConnectInput(notCheckzero.Output);
                Zero.ConnectInput(zeroCheck.Output);
            }
            catch (Exception e)
            {
                Console.WriteLine("Output info exp" + e.Message);
                throw;
            }
        }
예제 #11
0
        public override bool TestGate()
        {
            Random  rand       = new Random();
            WireSet randInWire = new WireSet(WordSize);

            for (int i = 0; i < m_rMultiBit.Length; i++)
            {
                randInWire.Set2sComplement((int)rand.Next(-(int)Math.Pow(2, WordSize - 1), ((int)Math.Pow(2, WordSize - 1) - 1))); //generate random valid input
                Input.Set2sComplement(randInWire.Get2sComplement());                                                               // connect input to randomized wire
                Address.Set2sComplement((int)rand.Next(0, (int)Math.Pow(2, AddressSize) + 1));                                     //generate random valid address
                Load.Value = 1;

                Clock.ClockDown();
                Clock.ClockUp();

                Load.Value = 0;
                int tempChange = randInWire.Get2sComplement();
                if (tempChange < 0)
                {
                    tempChange++;
                }
                else
                {
                    tempChange--;
                }
                Input.Set2sComplement(tempChange);
                if (Output.Get2sComplement() != randInWire.Get2sComplement())
                {
                    return(false);
                }

                Clock.ClockDown();
                Clock.ClockUp();
                if (Output.Get2sComplement() != randInWire.Get2sComplement())
                {
                    return(false);
                }

                Load.Value = 1;
                Clock.ClockDown();
                Clock.ClockUp();
                if (Output.Get2sComplement() != Input.Get2sComplement())
                {
                    return(false);
                }

                tempChange = Address.Get2sComplement();
                if (tempChange != 0)
                {
                    tempChange--;
                }
                else
                {
                    tempChange++;
                }
                Address.Set2sComplement(tempChange);
                Clock.ClockDown();
                Clock.ClockUp();
                if (Output.Get2sComplement() != Input.Get2sComplement())
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #12
0
        public override bool TestGate()
        {
            ALU al;

            WireSet[] inputs   = new WireSet[2];
            WireSet[] solution = new WireSet[18];
            WireSet[] all      = new WireSet[6];

            for (int i = 0; i < all.Length; i++)
            {
                all[i] = new WireSet(18);
            }


            WireSet wsTemp  = new WireSet(18);
            WireSet wsTemp2 = new WireSet(Size);

            wsTemp.SetValue(240288);
            all[0].ConnectInput(wsTemp);
            wsTemp = new WireSet(18);
            wsTemp.SetValue(109481);
            all[1].ConnectInput(wsTemp);
            wsTemp = new WireSet(18);
            wsTemp.SetValue(251200);
            all[2].ConnectInput(wsTemp);
            wsTemp = new WireSet(18);
            wsTemp.SetValue(87493);
            all[3].ConnectInput(wsTemp);
            wsTemp = new WireSet(18);
            wsTemp.SetValue(231420);
            all[4].ConnectInput(wsTemp);
            wsTemp = new WireSet(18);
            wsTemp.SetValue(73613);
            all[5].ConnectInput(wsTemp);

            for (int i = 0; i < Size; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    for (int f = 0; f < solution.Length; f++)
                    {
                        solution[f] = new WireSet(Size);
                    }

                    inputs[0] = new WireSet(Size);
                    inputs[1] = new WireSet(Size);
                    inputs[0].SetValue(i);
                    inputs[1].SetValue(j);

                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(0);
                    solution[0].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(1);
                    solution[1].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.Set2sComplement(-1);
                    solution[2].ConnectInput(wsTemp2);
                    solution[3].ConnectInput(inputs[0]);
                    solution[4].ConnectInput(inputs[1]);

                    BitwiseNotGate bwng = new BitwiseNotGate(Size);
                    bwng.ConnectInput(inputs[0]);

                    solution[5].ConnectInput(bwng.Output);
                    bwng = new BitwiseNotGate(Size);
                    bwng.ConnectInput(inputs[1]);
                    solution[6].ConnectInput(bwng.Output);
                    wsTemp2 = new WireSet(Size);

                    wsTemp2.SetValue(inputs[0].GetValue() * -1);
                    solution[7].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[1].GetValue() * -1);
                    solution[8].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[0].GetValue() + 1);
                    solution[9].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[1].GetValue() + 1);
                    solution[10].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[0].GetValue() - 1);
                    solution[11].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[1].GetValue() - 1);
                    solution[12].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[0].GetValue() + inputs[1].GetValue());
                    solution[13].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[0].GetValue() - inputs[1].GetValue());
                    solution[14].ConnectInput(wsTemp2);
                    wsTemp2 = new WireSet(Size);
                    wsTemp2.SetValue(inputs[1].GetValue() - inputs[0].GetValue());
                    solution[15].ConnectInput(wsTemp2);

                    BitwiseOrGate  bwog = new BitwiseOrGate(Size);
                    BitwiseAndGate bwag = new BitwiseAndGate(Size);
                    bwog.ConnectInput1(inputs[0]);
                    bwog.ConnectInput2(inputs[1]);
                    bwag.ConnectInput1(inputs[0]);
                    bwag.ConnectInput2(inputs[1]);
                    solution[16].ConnectInput(bwag.Output);
                    solution[17].ConnectInput(bwog.Output);

                    for (int k = 0; k < 18; k++)
                    {
                        al = new ALU(Size);
                        al.InputX.ConnectInput(inputs[0]);
                        al.InputY.ConnectInput(inputs[1]);
                        al.ZeroX.Value     = all[0][17 - k].Value;
                        al.NotX.Value      = all[1][17 - k].Value;
                        al.ZeroY.Value     = all[2][17 - k].Value;
                        al.NotY.Value      = all[3][17 - k].Value;
                        al.F.Value         = all[4][17 - k].Value;
                        al.NotOutput.Value = all[5][17 - k].Value;

                        if (al.Output.ToString() != solution[k].ToString())
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
예제 #13
0
        //your code here

        public ALU(int iSize)
        {
            Size      = iSize;
            InputX    = new WireSet(Size);
            InputY    = new WireSet(Size);
            ZeroX     = new Wire();
            ZeroY     = new Wire();
            NotX      = new Wire();
            NotY      = new Wire();
            F         = new Wire();
            NotOutput = new Wire();
            Negative  = new Wire();
            Zero      = new Wire();


            //Create and connect all the internal components
            Output = new WireSet(Size);

            BitwiseMux m_gZX     = new BitwiseMux(Size);
            BitwiseMux m_gZY     = new BitwiseMux(Size);
            WireSet    zeroForXY = new WireSet(Size);

            zeroForXY.Set2sComplement(0);

            BitwiseNotGate m_gNotX = new BitwiseNotGate(Size); // reverse X
            BitwiseNotGate m_gNotY = new BitwiseNotGate(Size); // reverse Y
            BitwiseMux     m_gNX   = new BitwiseMux(Size);
            BitwiseMux     m_gNY   = new BitwiseMux(Size);

            BitwiseAndGate m_gBAnd = new BitwiseAndGate(Size); // X & Y
            MultiBitAdder  mbAdder = new MultiBitAdder(Size);  // X + Y

            BitwiseMux m_gF = new BitwiseMux(Size);

            BitwiseNotGate m_gNotFOut = new BitwiseNotGate(Size); // reverse F output
            BitwiseMux     m_gN       = new BitwiseMux(Size);

            MultiBitAndGate m_gZeroAnd = new MultiBitAndGate(Size);
            BitwiseNotGate  m_gZeroNot = new BitwiseNotGate(Size);


            // connect inputX and input0 to ZX, connect ZXControl
            m_gZX.ConnectInput1(InputX);
            m_gZX.ConnectInput2(zeroForXY);
            m_gZX.ConnectControl(ZeroX);

            // connect bitwiseNotXIn to ZX output, connect ZXOut to NXIn1, connect bitwiseNotOut to NXIn2
            m_gNotX.ConnectInput(m_gZX.Output);
            m_gNX.ConnectInput1(m_gZX.Output);
            m_gNX.ConnectInput2(m_gNotX.Output);
            m_gNX.ConnectControl(NotX);

            // connect inputY and input0 to ZY, connect ZYControl
            m_gZY.ConnectInput1(InputY);
            m_gZY.ConnectInput2(zeroForXY);
            m_gZY.ConnectControl(ZeroY);

            // connect bitwiseNotYIn to ZY output, connect ZYOut to NYIn1, connect bitwiseNotYOut to NYIn2
            m_gNotY.ConnectInput(m_gZY.Output);
            m_gNY.ConnectInput1(m_gZY.Output);
            m_gNY.ConnectInput2(m_gNotY.Output);
            m_gNY.ConnectControl(NotY);

            // connect NXOut to bitwiseAndIn1, connect NYOut to bitwiseAndIn2
            m_gBAnd.ConnectInput1(m_gNX.Output);
            m_gBAnd.ConnectInput2(m_gNY.Output);
            // connect NXOut to mbAdderIn1, connect NYOut to mbAdderIn2
            mbAdder.ConnectInput1(m_gNX.Output);
            mbAdder.ConnectInput2(m_gNY.Output);
            // connect bitwiseAndOut to F_In1, connect mbAdderOut to F_In2
            m_gF.ConnectInput1(m_gBAnd.Output);
            m_gF.ConnectInput2(mbAdder.Output);
            m_gF.ConnectControl(F);

            //connect F_Out to bitwiseNotFIn
            m_gNotFOut.ConnectInput(m_gF.Output);
            // connect F_Out to N_In1, connect bitwiseNotFOut to N_In2
            m_gN.ConnectInput1(m_gF.Output);
            m_gN.ConnectInput2(m_gNotFOut.Output);
            m_gN.ConnectControl(NotOutput);

            // connect N_Out to Output
            Output.ConnectInput(m_gN.Output);

            // connect negativeIndicator to last Output wire
            Negative.ConnectInput(Output[Size - 1]);
            // connect ZeroIndicator depending on zeroNot and zeroAnd concatenation
            m_gZeroNot.ConnectInput(Output);
            m_gZeroAnd.ConnectInput(m_gZeroNot.Output);
            Zero.ConnectInput(m_gZeroAnd.Output);
        }
예제 #14
0
        public override bool TestGate()
        {
            int[] arrZX = new int[] { 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
            int[] arrNX = new int[] { 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1 };
            int[] arrZY = new int[] { 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
            int[] arrNY = new int[] { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1 };
            int[] arrF  = new int[] { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 };
            int[] arrNO = new int[] { 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1 };

            InputX = new WireSet(Size);
            InputY = new WireSet(Size);
            Random rand = new Random();

            WireSet[] answer = new WireSet[arrZX.Length];
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] = new WireSet(Size);
            }
            answer[0].Set2sComplement(0);
            answer[1].Set2sComplement(1);
            answer[2].Set2sComplement(-1);

            for (int i = 0; i < arrZX.Length; i++)
            {
                int num1 = rand.Next((-(int)Math.Pow(2, Size - 1)), ((int)Math.Pow(2, Size - 1) - 1) + 1);
                int num2 = rand.Next((-(int)Math.Pow(2, Size - 1)), ((int)Math.Pow(2, Size - 1) - 1) + 1);
                InputX.Set2sComplement(num1);
                InputY.Set2sComplement(num2);

                ZeroX.Value     = arrZX[i];
                NotX.Value      = arrNX[i];
                ZeroY.Value     = arrZY[i];
                NotY.Value      = arrNY[i];
                F.Value         = arrF[i];
                NotOutput.Value = arrNO[i];

                if (Negative.Value != Output[Size - 1].Value)
                {
                    return(false);
                }
                if (Output.Get2sComplement() == 0 && Zero.Value != 1)
                {
                    return(false);
                }
                if (Output.Get2sComplement() != 0 && Zero.Value != 0)
                {
                    return(false);
                }


                answer[3].Set2sComplement(num1);
                answer[4].Set2sComplement(num2);
                BitwiseNotGate tempGX = new BitwiseNotGate(Size);
                tempGX.ConnectInput(InputX);
                answer[5].ConnectInput(tempGX.Output);
                BitwiseNotGate tempGY = new BitwiseNotGate(Size);
                tempGY.ConnectInput(InputY);
                answer[6].ConnectInput(tempGY.Output);
                answer[7].Set2sComplement(-num1);
                answer[8].Set2sComplement(-num2);
                answer[9].Set2sComplement(num1 + 1);
                answer[10].Set2sComplement(num2 + 1);
                answer[11].Set2sComplement(num1 - 1);
                answer[12].Set2sComplement(num2 - 1);
                answer[13].Set2sComplement(num1 + num2);
                answer[14].Set2sComplement(num1 - num2);
                answer[15].Set2sComplement(num2 - num1);
                BitwiseAndGate tempGAnd = new BitwiseAndGate(Size);
                tempGAnd.ConnectInput1(InputX);
                tempGAnd.ConnectInput2(InputY);
                answer[16].ConnectInput(tempGAnd.Output);
                answer[17].ConnectInput(Output);

                for (int j = 0; j < Size; j++)
                {
                    if (answer[i][j].Value != Output[j].Value)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }