Beispiel #1
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);
        }
Beispiel #2
0
 private static void TestWireSet_2sComplement(WireSet wireSet, int value)
 {
     wireSet.Set2sComplement(value);
     if (value != wireSet.Get2sComplement())
     {
         Console.WriteLine("bugbug wireset");
     }
 }
Beispiel #3
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));
        }
Beispiel #4
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);
        }
Beispiel #5
0
        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();
        }
Beispiel #6
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);
        }