Beispiel #1
0
        public void TwosComplement()
        {
            Gates.IOGates.NumericInput ni = new Gates.IOGates.NumericInput(4);

            ni.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.D2C;
            ni.Value = "0";
            Assert.AreEqual(ni.IntValue, 0);

            ni.Value = "7";
            Assert.AreEqual(ni.IntValue, 7);

            ni.Value = "-8"; // -8 = 1000 = 8
            Assert.AreEqual(ni.IntValue, 8);

            ni.Value = "-1"; // -1 = 1111 = 15
            Assert.AreEqual(ni.IntValue, 15);

            try
            {
                ni.Value = "-9";
                Assert.Fail("Allows -9");
            }
            catch (Exception)
            {
                // good
            }

            try
            {
                ni.Value = "8";
                Assert.Fail("Allows 8");
            }
            catch (Exception)
            {
                // good
            }

            Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(4);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.D2C;

            no[0] = false;
            no[1] = false;
            no[2] = false;
            no[3] = false;

            Assert.AreEqual("0", no.Value);
            Assert.AreEqual(0, no.IntValue);

            no[0] = true;
            no[1] = false;
            no[2] = true;
            no[3] = false;

            Assert.AreEqual("5", no.Value);
            Assert.AreEqual(5, no.IntValue);

            no[0] = false;
            no[1] = true;
            no[2] = false;
            no[3] = true;

            Assert.AreEqual("-6", no.Value);
            Assert.AreEqual(10, no.IntValue);

            no[0] = true;
            no[1] = true;
            no[2] = true;
            no[3] = true;

            Assert.AreEqual("-1", no.Value);
            Assert.AreEqual(15, no.IntValue);
        }
Beispiel #2
0
        private Gates.AbstractGate CreateGate(XElement gate)
        {
            int numInputs = 2; // default for variable input gates
            if (gate.Attribute("NumInputs") != null)
                numInputs = int.Parse(gate.Attribute("NumInputs").Value);

            switch (gate.Attribute("Type").Value)
            {
                case "And":
                    return new Gates.BasicGates.And(numInputs);
                case "Not":
                    return new Gates.BasicGates.Not();
                case "Or":
                    return new Gates.BasicGates.Or(numInputs);
                case "Nand":
                    return new Gates.BasicGates.Nand(numInputs);
                case "Nor":
                    return new Gates.BasicGates.Nor(numInputs);
                case "Xor":
                    return new Gates.BasicGates.Xor();
                case "Xnor":
                    return new Gates.BasicGates.Xnor();
                case "Buffer":
                    return new Gates.BasicGates.Buffer();
                case "UserInput":
                    Gates.IOGates.UserInput ui = new Gates.IOGates.UserInput();
                    ui.SetName(gate.Attribute("Name").Value);
                    return ui;
                case "UserOutput":
                    Gates.IOGates.UserOutput uo = new Gates.IOGates.UserOutput();
                    uo.SetName(gate.Attribute("Name").Value);
                    return uo;
                case "NumericInput":
                    Gates.IOGates.NumericInput ni = new Gates.IOGates.NumericInput(int.Parse(gate.Attribute("Bits").Value));
                    ni.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation)int.Parse(gate.Attribute("SelRep").Value);
                    ni.Value = gate.Attribute("Value").Value;
                    return ni;
                case "NumericOutput":
                    Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(int.Parse(gate.Attribute("Bits").Value));
                    no.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation)int.Parse(gate.Attribute("SelRep").Value);
                    return no;
                case "Clock":
                    Gates.IOGates.Clock clk = new Gates.IOGates.Clock(int.Parse(gate.Attribute("Milliseconds").Value));
                    return clk;
                case "IC":
                    // check if this ic has been renamed
                    string cname = gate.Attribute("Name").Value;
                    // check first if we need to rename
                    if (UpdateICNames.ContainsKey(cname))
                        cname = UpdateICNames[cname];
                
                    return icl.GetIC(cname).AbGate.Clone();
                case "Comment":
                    Gates.IOGates.Comment cmt = new Gates.IOGates.Comment();
                    cmt.Value = gate.Element("Comment").Value;
                    return cmt;
                
            }
            throw new ArgumentException("unknown gate");
        }
Beispiel #3
0
        public void BCD()
        {
            Gates.IOGates.NumericInput ni = new Gates.IOGates.NumericInput(8);

            ni.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BCD;
            ni.Value = "00";
            Assert.AreEqual(ni.IntValue, 0);

            ni.Value = "59";
            Assert.AreEqual(ni.IntValue, 89); // 59 = 01011001 = 89

            ni.Value = "90";                  // 90 = 10010000 = 144
            Assert.AreEqual(ni.IntValue, 144);

            try
            {
                ni.Value = "111"; // too long
                Assert.Fail("Allowed 1111");
            }
            catch (Exception)
            {
                // good
            }

            Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(8);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BCD;
            no[0] = true;
            no[1] = false;
            no[2] = false;
            no[3] = true;

            no[4] = false;
            no[5] = true;
            no[6] = true;
            no[7] = false;

            Assert.AreEqual(no.Value, "69");

            no[0] = true;
            no[1] = true;
            no[2] = false;
            no[3] = false;

            no[4] = true;
            no[5] = false;
            no[6] = true;
            no[7] = false;

            Assert.AreEqual(no.Value, "53");

            try
            {
                no[0] = true;
                no[1] = false;
                no[2] = true;
                no[3] = true;

                no[4] = true;
                no[5] = true;
                no[6] = true;
                no[7] = false;

                Assert.Fail("Allowed too much digit");
            }
            catch (Exception)
            {
                // good
            }
        }
Beispiel #4
0
        public void NumericOutput2()
        {
            Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(7);

            no[0] = false;
            no[1] = false;
            no[2] = false;
            no[3] = false;
            no[4] = false;
            no[5] = false;
            no[6] = false;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("0", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("0000000", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("0", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("0", no.Value);

            Assert.AreEqual(7, no.Bits);
            Assert.AreEqual(0, no.IntValue);
            Assert.AreEqual(127, no.MaxValue);

            no[1] = true;
            no[3] = true;
            no[5] = true;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("42", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("0101010", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("52", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("2A", no.Value);

            Assert.AreEqual(42, no.IntValue);


            no[1] = false;
            no[2] = true;
            no[3] = false;
            no[4] = true;
            no[6] = true;
            no[5] = false;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("84", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("1010100", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("124", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("54", no.Value);

            Assert.AreEqual(84, no.IntValue);
        }
Beispiel #5
0
        public void NumericOutput1()
        {
            Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(4);

            no[0] = false;
            no[1] = false;
            no[2] = false;
            no[3] = false;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("0", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("0000", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("0", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("0", no.Value);

            Assert.AreEqual(4, no.Bits);
            Assert.AreEqual(0, no.IntValue);
            Assert.AreEqual(15, no.MaxValue);


            no[0] = true;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("1", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("0001", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("1", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("1", no.Value);
            Assert.AreEqual(1, no.IntValue);

            no[2] = true;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("5", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("0101", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("5", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("5", no.Value);
            Assert.AreEqual(5, no.IntValue);

            no[3] = true;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("13", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("1101", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("15", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("D", no.Value);
            Assert.AreEqual(13, no.IntValue);


            no[1] = true;

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.DECIMAL;
            Assert.AreEqual("15", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.BINARY;
            Assert.AreEqual("1111", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.OCTAL;
            Assert.AreEqual("17", no.Value);

            no.SelectedRepresentation = Gates.IOGates.AbstractNumeric.Representation.HEXADECIMAL;
            Assert.AreEqual("F", no.Value);
            Assert.AreEqual(15, no.IntValue);
        }
Beispiel #6
0
        //Modified method from CircuitXML.CreateGate Method
        private Gates.AbstractGate CreateGate(XElement gate)
        {
            int numInputs = 2; // default for variable input gates
            if (gate.Attribute("NumInputs") != null)
                numInputs = int.Parse(gate.Attribute("NumInputs").Value);

            switch (gate.Attribute("Type").Value)
            {
                case "And":
                    return new Gates.BasicGates.And(numInputs);
                case "Not":
                    return new Gates.BasicGates.Not();
                case "Or":
                    return new Gates.BasicGates.Or(numInputs);
                case "Nand":
                    return new Gates.BasicGates.Nand(numInputs);
                case "Nor":
                    return new Gates.BasicGates.Nor(numInputs);
                case "Xor":
                    return new Gates.BasicGates.Xor();
                case "Xnor":
                    return new Gates.BasicGates.Xnor();
                case "Buffer":
                    return new Gates.BasicGates.Buffer();
                case "UserInput":
                    Gates.IOGates.UserInput ui = new Gates.IOGates.UserInput();
                    ui.SetName(gate.Attribute("Name").Value);
                    return ui;
                case "UserOutput":
                    Gates.IOGates.UserOutput uo = new Gates.IOGates.UserOutput();
                    uo.SetName(gate.Attribute("Name").Value);
                    return uo;
                case "NumericInput":
                    Gates.IOGates.NumericInput ni = new Gates.IOGates.NumericInput(int.Parse(gate.Attribute("Bits").Value));
                    ni.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation)int.Parse(gate.Attribute("SelRep").Value);
                    ni.Value = gate.Attribute("Value").Value;
                    return ni;
                case "NumericOutput":
                    Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(int.Parse(gate.Attribute("Bits").Value));
                    no.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation)int.Parse(gate.Attribute("SelRep").Value);
                    return no;
                case "Clock":
                    Gates.IOGates.Clock clk = new Gates.IOGates.Clock(int.Parse(gate.Attribute("Milliseconds").Value));
                    return clk;
                case "Comment":
                    Gates.IOGates.Comment cmt = new Gates.IOGates.Comment();
                    cmt.Value = gate.Element("Comment").Value;
                    return cmt;
            }
            throw new ArgumentException("unknown gate");
        }
Beispiel #7
0
        private Gates.AbstractGate CreateGate(XElement gate)
        {
            int numInputs = 2; // default for variable input gates

            if (gate.Attribute("NumInputs") != null)
            {
                numInputs = int.Parse(gate.Attribute("NumInputs").Value);
            }

            switch (gate.Attribute("Type").Value)
            {
            case "And":
                return(new Gates.BasicGates.And(numInputs));

            case "Not":
                return(new Gates.BasicGates.Not());

            case "Or":
                return(new Gates.BasicGates.Or(numInputs));

            case "Nand":
                return(new Gates.BasicGates.Nand(numInputs));

            case "Nor":
                return(new Gates.BasicGates.Nor(numInputs));

            case "Xor":
                return(new Gates.BasicGates.Xor());

            case "Xnor":
                return(new Gates.BasicGates.Xnor());

            case "Buffer":
                return(new Gates.BasicGates.Buffer());

            case "UserInput":
                Gates.IOGates.UserInput ui = new Gates.IOGates.UserInput();
                ui.SetName(gate.Attribute("Name").Value);
                return(ui);

            case "UserOutput":
                Gates.IOGates.UserOutput uo = new Gates.IOGates.UserOutput();
                uo.SetName(gate.Attribute("Name").Value);
                return(uo);

            case "NumericInput":
                Gates.IOGates.NumericInput ni = new Gates.IOGates.NumericInput(int.Parse(gate.Attribute("Bits").Value));
                ni.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation) int.Parse(gate.Attribute("SelRep").Value);
                ni.Value = gate.Attribute("Value").Value;
                return(ni);

            case "NumericOutput":
                Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(int.Parse(gate.Attribute("Bits").Value));
                no.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation) int.Parse(gate.Attribute("SelRep").Value);
                return(no);

            case "Clock":
                Gates.IOGates.Clock clk = new Gates.IOGates.Clock(int.Parse(gate.Attribute("Milliseconds").Value));
                return(clk);

            case "IC":
                // check if this ic has been renamed
                string cname = gate.Attribute("Name").Value;
                // check first if we need to rename
                if (UpdateICNames.ContainsKey(cname))
                {
                    cname = UpdateICNames[cname];
                }

                return(icl.GetIC(cname).AbGate.Clone());

            case "Comment":
                Gates.IOGates.Comment cmt = new Gates.IOGates.Comment();
                cmt.Value = gate.Element("Comment").Value;
                return(cmt);
            }
            throw new ArgumentException("unknown gate");
        }