Exemple #1
0
        public MultiBitAdder(int iSize)
        {
            Size     = iSize;
            Input1   = new WireSet(Size);
            Input2   = new WireSet(Size);
            Output   = new WireSet(Size);
            Overflow = new Wire();

            full1 = new FullAdder();

            full1.ConnectInput1(Input1[0]);
            full1.ConnectInput2(Input2[0]);
            Output[0].ConnectInput(full1.Output);

            for (int i = 1; i < Input1.Size; i++)
            {
                Wire temp_S = full1.Output;
                Wire temp_C = full1.CarryOutput;

                full1 = new FullAdder();

                full1.CarryInput.ConnectInput(temp_C);
                full1.ConnectInput1(Input1[i]);
                full1.ConnectInput2(Input2[i]);
                Output[i].ConnectInput(full1.Output);
            }


            Overflow.ConnectInput(full1.CarryOutput); // updating the overflow.
        }
        public BitwiseMultiwayDemux(int iSize, int cControlBits)
        {
            Size    = iSize;
            Input   = new WireSet(Size);
            Control = new WireSet(cControlBits);
            Outputs = new WireSet[(int)Math.Pow(2, cControlBits)];

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

            Queue <WireSet> temp1 = new Queue <WireSet>();
            Queue <WireSet> temp2 = new Queue <WireSet>();

            temp1.Enqueue(Input);
            int level = cControlBits - 1;

            while (level != -1)
            {
                if (temp1.Count == 0)
                {
                    while (temp2.Count != 0)
                    {
                        bitwiseDemux = new BitwiseDemux(iSize);
                        bitwiseDemux.ConnectControl(Control[level]);

                        bitwiseDemux.ConnectInput(temp2.Dequeue());
                        temp1.Enqueue(bitwiseDemux.Output1);
                        temp1.Enqueue(bitwiseDemux.Output2);
                    }
                }
                else if (temp2.Count == 0)
                {
                    while (temp1.Count != 0)
                    {
                        bitwiseDemux = new BitwiseDemux(iSize);
                        bitwiseDemux.ConnectControl(Control[level]);

                        bitwiseDemux.ConnectInput(temp1.Dequeue());
                        temp2.Enqueue(bitwiseDemux.Output1);
                        temp2.Enqueue(bitwiseDemux.Output2);
                    }
                }
                level--;
            }

            int j = 0;

            while (temp1.Count != 0)
            {
                Outputs[j].ConnectInput(temp1.Dequeue());
                j++;
            }
            while (temp2.Count != 0)
            {
                Outputs[j].ConnectInput(temp2.Dequeue());
                j++;
            }
        }
Exemple #3
0
        public override bool TestGate()
        {
            for (int k = 1; k >= 0; k--)
            {
                Control.Value = k;
                for (int i = 0; i < Math.Pow(2, Size); i++)
                {
                    BitwiseDemux mgDemux = new BitwiseDemux(Size);
                    WireSet      ws      = new WireSet(Size);
                    ws.SetValue(i);
                    mgDemux.ConnectInput(ws);
                    mgDemux.ConnectControl(Control);

                    for (int j = Size - 1; j >= 0; j--)
                    {
                        if (k == 0)
                        {
                            if (mgDemux.Output1[j].Value != ws[j].Value || mgDemux.Output2[j].Value != 0)
                            {
                                return(false);
                            }
                        }
                        else if (mgDemux.Output1[j].Value != ws[j].Value ||
                                 mgDemux.Output2[j].Value != 0)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Exemple #4
0
        public MultiBitAdder(int iSize)
        {
            Size   = iSize;
            Input1 = new WireSet(Size);
            Input2 = new WireSet(Size);
            Output = new WireSet(Size);
            //your code here

            Overflow = new Wire();

            var firstAdder = new HalfAdder();

            m_firstAdder = firstAdder;
            firstAdder.ConnectInput1(Input1[0]);
            firstAdder.ConnectInput2(Input2[0]);
            Output[0].ConnectInput(firstAdder.Output);

            var prevCout = firstAdder.CarryOutput;

            m_fullAdders = new FullAdder[iSize - 1];
            for (int i = 1; i < iSize; i++)
            {
                var adder = new FullAdder();
                m_fullAdders[i - 1] = adder;
                adder.ConnectInput1(Input1[i]);
                adder.ConnectInput2(Input2[i]);
                adder.CarryInput.ConnectInput(prevCout);
                Output[i].ConnectInput(adder.Output);
                prevCout = adder.CarryOutput;
            }

            Overflow.ConnectInput(prevCout);
        }
 public BitwiseTwoInputGate(int iSize)
 {
     Size   = iSize;
     Input1 = new WireSet(iSize);
     Input2 = new WireSet(iSize);
     Output = new WireSet(iSize);
 }
Exemple #6
0
 private void SetInputValues(int[] input, WireSet inputWs)
 {
     for (int i = 0; i < Size; i++)
     {
         inputWs[i].Value = input[i];
     }
 }
Exemple #7
0
        // adding 2 binary numbers by using full adder for each bit
        public MultiBitAdder(int iSize)
        {
            //initilaize
            Size     = iSize;
            Input1   = new WireSet(Size);
            Input2   = new WireSet(Size);
            Output   = new WireSet(Size);
            Overflow = new Wire();
            // one extra full addr for the overflow bit
            full_addr_op_array = new FullAdder[Size + 1];

            for (int i = 0; i < Size + 1; i++)
            {
                full_addr_op_array[i] = new FullAdder();
            }

            //carry in the first bit is 0
            full_addr_op_array[0].CarryInput.Value = 0;

            for (int bit = 0; bit < Size; bit++)
            {
                full_addr_op_array[bit].ConnectInput1(Input1[bit]);
                full_addr_op_array[bit].ConnectInput2(Input2[bit]);

                Output[bit].ConnectInput(full_addr_op_array[bit].Output);
                full_addr_op_array[bit + 1].CarryInput.ConnectInput(full_addr_op_array[bit].CarryOutput);
            }
            Overflow.ConnectInput(full_addr_op_array[Size].CarryInput);
        }
Exemple #8
0
        public MultiBitAdder(int iSize)
        {
            Size   = iSize;
            Input1 = new WireSet(Size);
            Input2 = new WireSet(Size);
            Output = new WireSet(Size);
            //your code here
            Overflow = new Wire();
            Wire tempCarry = new Wire();

            FullAdder[] tempFullAdder = new FullAdder[Size];
            for (int i = 0; i < Size; i++)
            {
                tempFullAdder[i] = new FullAdder();
            }

            tempCarry.Value = 0;
            tempFullAdder[0].CarryInput.ConnectInput(tempCarry);
            tempFullAdder[0].ConnectInput1(Input1[0]);
            tempFullAdder[0].ConnectInput2(Input2[0]);
            Output[0].ConnectInput(tempFullAdder[0].Output);
            for (int i = 1; i < Size; i++)
            {
                tempCarry = tempFullAdder[i - 1].CarryOutput;
                tempFullAdder[i].CarryInput.ConnectInput(tempCarry);
                tempFullAdder[i].ConnectInput1(Input1[i]);
                tempFullAdder[i].ConnectInput2(Input2[i]);
                Output[i].ConnectInput(tempFullAdder[i].Output);
            }
            Overflow.ConnectInput(tempFullAdder[Size - 1].CarryOutput);
        }
Exemple #9
0
        public BitwiseMultiwayMux(int iSize, int cControlBits)
        {
            Size    = iSize;
            Output  = new WireSet(Size);
            Control = new WireSet(cControlBits);
            Inputs  = new WireSet[(int)Math.Pow(2, cControlBits)];

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

            //your code here
            ControlBits        = cControlBits;
            m_multiwayMuxGates = new MultiwayMuxGate[Size];
            for (int i = 0; i < Size; i++)
            {
                var multiwayMuxGate = new MultiwayMuxGate(ControlBits);
                m_multiwayMuxGates[i] = multiwayMuxGate;

                multiwayMuxGate.ConnectControl(Control);
                for (int j = 0; j < Inputs.Length; j++)
                {
                    multiwayMuxGate.ConnectInput(j, Inputs[j][i]);
                }
                Output[i].ConnectInput(multiwayMuxGate.Output);
            }
        }
Exemple #10
0
        //your code here

        public BitwiseNotGate(int iSize)
        {
            Size   = iSize;
            Input  = new WireSet(Size);
            Output = new WireSet(Size);
            //your code here
        }
        public BitwiseDemux(int iSize)
        {
            Size    = iSize;
            Control = new Wire();
            Input   = new WireSet(Size);
            Output1 = new WireSet(Size);
            Output2 = new WireSet(Size);

            //your code here

            m_gDemux = new Demux[iSize];

            WireSet ws_resault1 = new WireSet(iSize);
            WireSet ws_resault2 = new WireSet(iSize);

            //your code here
            for (int i = 0; i < iSize; i++)
            {
                m_gDemux[i] = new Demux();
                m_gDemux[i].ConnectInput(Input[i]);
                m_gDemux[i].ConnectControl(Control);

                ws_resault1[i].ConnectInput(m_gDemux[i].Output1);
                ws_resault2[i].ConnectInput(m_gDemux[i].Output2);
            }
            Output1.ConnectInput(ws_resault1);
            Output2.ConnectInput(ws_resault2);
        }
Exemple #12
0
        public override bool TestGate()
        {
            var ws1 = new WireSet(Size);

            for (int i = 0; i < Math.Pow(2, Size); i++)
            {
                ws1.SetValue(i);
                for (int j = 0; j < Math.Pow(2, Size); j++)
                {
                    var mgAnd = new BitwiseAndGate(Size);
                    var ws2   = new WireSet(Size);
                    ws2.SetValue(j);
                    mgAnd.ConnectInput1(ws1);
                    mgAnd.ConnectInput2(ws2);
                    for (int k = 0; k < ws2.Size; k++)
                    {
                        if (mgAnd.Output[k].Value != (ws1[k].Value & ws2[k].Value))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Exemple #13
0
        public Memory(int iAddressSize, int iWordSize)
        {
            AddressSize = iAddressSize;
            WordSize    = iWordSize;
            int registerNum = (int)Math.Pow(2.0, AddressSize);

            Input   = new WireSet(WordSize);
            Output  = new WireSet(WordSize);
            Address = new WireSet(AddressSize);
            Load    = new Wire();
            WireSet newLoad = new WireSet(1);

            newLoad[0].ConnectInput(Load);
            demux     = new BitwiseMultiwayDemux(1, AddressSize);
            mux       = new BitwiseMultiwayMux(WordSize, AddressSize);
            registers = new MultiBitRegister[registerNum];
            WireSet AddressMirror = new WireSet(AddressSize);

            for (int j = 0; j < AddressSize; j++)
            {
                AddressMirror[j].ConnectInput(Address[AddressSize - 1 - j]);
            }
            demux.ConnectControl(AddressMirror);
            demux.ConnectInput(newLoad);
            for (int i = 0; i < registerNum; i++)
            {
                registers[i] = new MultiBitRegister(WordSize);
                registers[i].ConnectInput(Input);
                registers[i].Load.ConnectInput(demux.Outputs[i][0]);
                mux.ConnectInput(i, registers[i].Output);
            }
            mux.ConnectControl(Address);
            Output.ConnectInput(mux.Output);
        }
Exemple #14
0
        //your code here

        public Memory(int iAddressSize, int iWordSize)
        {
            AddressSize = iAddressSize;
            WordSize    = iWordSize;

            Input   = new WireSet(WordSize);
            Output  = new WireSet(WordSize);
            Address = new WireSet(AddressSize);
            Load    = new Wire();

            //your code here
            demux  = new BitwiseMultiwayDemux(1, iAddressSize);
            mux    = new BitwiseMultiwayMux(iWordSize, iAddressSize);
            loadWs = new WireSet(1);
            loadWs[0].ConnectInput(Load);
            demux.ConnectControl(Address);
            demux.ConnectInput(loadWs);
            MultiBitRegister[] mbr = new MultiBitRegister[(int)Math.Pow(2, iAddressSize)];
            for (int i = 0; i < mbr.Length; i++)
            {
                mbr[i] = new MultiBitRegister(iWordSize);
                mbr[i].ConnectInput(Input);
                mbr[i].Load.ConnectInput(demux.Outputs[i][0]);
                mux.Inputs[i].ConnectInput(mbr[i].Output);
            }
            mux.ConnectControl(Address);
            Output.ConnectInput(mux.Output);
        }
Exemple #15
0
 public MultiBitAdder(int iSize)
 {
     Size     = iSize;
     Input1   = new WireSet(Size);
     Input2   = new WireSet(Size);
     Output   = new WireSet(Size);
     Overflow = new Wire();
     FullAdder[] fa = new FullAdder[Size];
     for (int i = 0; i < Size; i++)
     {
         if (fa[i] == null)
         {
             fa[i] = new FullAdder();
         }
         fa[i].ConnectInput1(Input1[i]);
         fa[i].ConnectInput2(Input2[i]);
         Output[i].ConnectInput(fa[i].Output);
         if (i == Size - 1)
         {
             Overflow.ConnectInput(fa[i].CarryOutput);
         }
         else
         {
             fa[i + 1] = new FullAdder();
             fa[i + 1].CarryInput.ConnectInput(fa[i].CarryOutput);
         }
     }
 }
Exemple #16
0
        public override bool TestGate()
        {
            WireSet ws1 = new WireSet(3);
            Wire    w1  = new Wire();

            w1.Value = 0;
            Wire w2 = new Wire();

            w2.Value = 0;
            Wire w3 = new Wire();

            w3.Value = 0;

            WireSet ws2 = new WireSet(3);
            Wire    w4  = new Wire();

            w4.Value = 1;
            Wire w5 = new Wire();

            w5.Value = 0;
            Wire w6 = new Wire();

            w6.Value = 1;

            Input1[0].ConnectInput(w1);
            Input1[1].ConnectInput(w2);
            Input1[2].ConnectInput(w3);
            Input2[0].ConnectInput(w4);
            Input2[1].ConnectInput(w5);
            Input2[2].ConnectInput(w6);
            Console.WriteLine("TESTING Or..Is it true?    : " + this.ToString());
            return(true);

            ;
        }
Exemple #17
0
        // creating counter by using one multibit register and 1 full addr to increment by 1
        // using mux to decide if to increment or to get new input
        public Counter(int iSize)
        {
            Size = iSize;
            Input = new WireSet(Size);
            Output = new WireSet(Size);
            Load = new Wire();
            val = new WireSet(Size);
            val.SetValue(1);

            mux_op = new BitwiseMux(Size);
            adder = new MultiBitAdder(Size);
            register = new MultiBitRegister(Size);

            register.Load.Value = 1;

            mux_op.ConnectControl(Load);
            mux_op.ConnectInput1(adder.Output);
            mux_op.ConnectInput2(Input);
            register.ConnectInput(mux_op.Output);
            Output.ConnectInput(register.Output);

            adder.ConnectInput1(register.Output);
            adder.ConnectInput2(val);


        }
Exemple #18
0
        public MuxGate()
        {
            ControlInput = new Wire();

            //Init the variables
            m_gAnd1 = new AndGate();
            m_gAnd2 = new AndGate();
            m_gAnd3 = new AndGate();
            m_gNot  = new NotGate();
            m_gmOr  = new MultiBitOrGate(3);
            m_ws    = new WireSet(3);

            //Connect the wires
            m_gNot.ConnectInput(ControlInput);
            m_gAnd1.ConnectInput1(Input1);
            m_gAnd1.ConnectInput2(Input2);
            m_gAnd2.ConnectInput1(Input2);
            m_gAnd2.ConnectInput2(ControlInput);
            m_gAnd3.ConnectInput1(Input1);
            m_gAnd3.ConnectInput2(m_gNot.Output);
            m_ws[0].ConnectInput(m_gAnd1.Output);
            m_ws[1].ConnectInput(m_gAnd2.Output);
            m_ws[2].ConnectInput(m_gAnd3.Output);
            m_gmOr.ConnectInput(m_ws);
            Output = m_gmOr.Output;
        }
Exemple #19
0
        public override bool TestGate()
        {
            try
            {
                var ws1 = new WireSet(Size);
                for (int i = 0; i < Math.Pow(2, Size) - 1; i++)
                {
                    ws1.SetValue(i);
                    for (int j = 0; j < Math.Pow(2, Size) - 1; j++)
                    {
                        var mgOr = new BitwiseOrGate(Size);
                        var ws2  = new WireSet(Size);
                        ws2.SetValue(j);
                        mgOr.ConnectInput1(ws1);
                        mgOr.ConnectInput2(ws2);

                        for (int k = 0; k < ws1.Size; k++)
                        {
                            if (mgOr.Output[j].Value != (ws1[j].Value | ws2[j].Value))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.HelpLink);
            }
            return(true);
        }
        //your code here

        public BitwiseDemux(int iSize)
        {
            Size    = iSize;
            Control = new Wire();
            Input   = new WireSet(Size);

            //your code here
        }
Exemple #21
0
 private static void TestWireSet_2sComplement(WireSet wireSet, int value)
 {
     wireSet.Set2sComplement(value);
     if (value != wireSet.Get2sComplement())
     {
         Console.WriteLine("bugbug wireset");
     }
 }
Exemple #22
0
 private static void TestWireSet_Value(WireSet wireSet, int value)
 {
     wireSet.SetValue(value);
     if (value != wireSet.GetValue())
     {
         Console.WriteLine("bugbug wireset");
     }
 }
 public MultiBitRegister(int iSize)
 {
     Size   = iSize;
     Input  = new WireSet(Size);
     Output = new WireSet(Size);
     Load   = new Wire();
     //your code here
 }
Exemple #24
0
 public MultiBitAdder(int iSize)
 {
     Size   = iSize;
     Input1 = new WireSet(Size);
     Input2 = new WireSet(Size);
     Output = new WireSet(Size);
     //your code here
 }
Exemple #25
0
        public MultiBitZero(int iSize)
        {
            Size   = iSize;
            Input  = new WireSet(Size);
            Output = new WireSet(Size);

            Output.SetValue(0);
        }
Exemple #26
0
 private void SetFlags(WireSet wsFlags)
 {
     ZeroX.Value     = wsFlags[0].Value;
     NotX.Value      = wsFlags[1].Value;
     ZeroY.Value     = wsFlags[2].Value;
     NotY.Value      = wsFlags[3].Value;
     F.Value         = wsFlags[4].Value;
     NotOutput.Value = wsFlags[5].Value;
 }
Exemple #27
0
        //your code here

        public BitwiseMultiwayDemux(int iSize, int cControlBits)
        {
            Size    = iSize;
            Input   = new WireSet(Size);
            Control = new WireSet(cControlBits);
            Outputs = new WireSet[(int)Math.Pow(2, cControlBits)];
            for (int i = 0; i < Outputs.Length; i++)
            {
                Outputs[i] = new WireSet(Size);
            }

            BitwiseDemux[] demux = new BitwiseDemux[(int)Math.Pow(2, cControlBits) - 1];


            int currentDemux   = 0;
            int maxDemux       = 0;
            int currentControl = cControlBits - 1;

            demux[0] = new BitwiseDemux(Size);
            demux[0].ConnectInput(Input);
            demux[0].ConnectControl(Control[currentControl]);
            currentControl--;

            for (int i = 1; i < cControlBits; i++)
            {
                while (currentDemux <= maxDemux)
                {
                    demux[currentDemux * 2 + 1] = new BitwiseDemux(Size);
                    demux[currentDemux * 2 + 1].ConnectInput(demux[currentDemux].Output1);
                    demux[currentDemux * 2 + 1].ConnectControl(Control[currentControl]);
                    demux[currentDemux * 2 + 2] = new BitwiseDemux(Size);
                    demux[currentDemux * 2 + 2].ConnectInput(demux[currentDemux].Output2);
                    demux[currentDemux * 2 + 2].ConnectControl(Control[currentControl]);

                    currentDemux++;
                }


                maxDemux += (int)Math.Pow(2, i);
                currentControl--;
            }



            int c = 0;

            for (int j = ((demux.Length + 1) / 2) - 1; j < demux.Length; j++)
            {
                Outputs[c].ConnectInput(demux[j].Output1);
                Outputs[c + 1].ConnectInput(demux[j].Output2);



                c = c + 2;
            }
        }
Exemple #28
0
        public BitwiseMultiwayDemux(int iSize, int cControlBits)
        {
            Size    = iSize;
            Input   = new WireSet(Size);
            Control = new WireSet(cControlBits);
            Outputs = new WireSet[(int)Math.Pow(2, cControlBits)];
            for (int i = 0; i < Outputs.Length; i++)
            {
                Outputs[i] = new WireSet(Size);
            }

            BitwiseDemux[] demuxes = new BitwiseDemux[Outputs.Length - 1];

            demuxes[0] = new BitwiseDemux(Size);
            demuxes[0].ConnectControl(Control[cControlBits - 1]);

            for (int i = 1, index2 = 1, nextStep = 1, currControl = cControlBits - 1; i < demuxes.Length; i++)
            {
                demuxes[i] = new BitwiseDemux(Size);
                if (i == index2)
                {
                    currControl--;
                    demuxes[index2].ConnectControl(Control[currControl]);
                    index2   += nextStep * 2;
                    nextStep *= 2;
                }
                else
                {
                    demuxes[i].ConnectControl(Control[currControl]);
                }
            }

            try
            {
                for (int position = demuxes.Length / 2, j = 0, i = demuxes.Length / 2; i >= 0; i--)
                {
                    Outputs[j].ConnectInput(demuxes[position].Output1);
                    Outputs[j + 1].ConnectInput(demuxes[position].Output2);
                    j = j + 2;
                    position++;
                }

                for (int currDemux = demuxes.Length - 1, index0 = demuxes.Length / 2 - 1, i = index0; i >= 0; i--)
                {
                    demuxes[currDemux].ConnectInput(demuxes[index0].Output2);
                    demuxes[currDemux - 1].ConnectInput(demuxes[index0].Output1);
                    index0--;
                    currDemux -= 2;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally{ demuxes[0].ConnectInput(Input); }
        }
        private String WStoString(WireSet ws)
        {
            String str = "";

            for (int i = 0; i < ws.Size; i++)
            {
                str += ws[i].Value.ToString();
            }
            return(str);
        }
        private WireSet InitRandTestVar(int testSize)
        {
            WireSet wsTest = new WireSet(testSize);
            Random  rnd    = new Random();

            for (int i = 0; i < testSize; i++)
            {
                wsTest[i].Value = rnd.Next(0, 2);
            }
            return(wsTest);
        }