示例#1
0
        public void CloneTest()
        {
            Gates.IC nor  = CreateNor();
            Gates.IC nor2 = (Gates.IC)nor.Clone();
            nor2.Circuit.Start();

            nor[0]  = false;
            nor2[0] = false;

            Gates.PropagationThread.Instance.WaitOnPropagation();
            //Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(true, nor.Output[0]);
            Assert.AreEqual(true, nor2.Output[0]);

            nor[0] = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            //nor2.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);
            Assert.AreEqual(true, nor2.Output[0]);

            nor2[0] = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            //nor2.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);
            Assert.AreEqual(false, nor2.Output[0]);

            nor[0] = false;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            //nor2.WaitOnPropagation();
            Assert.AreEqual(true, nor.Output[0]);
            Assert.AreEqual(false, nor2.Output[0]);
        }
示例#2
0
        public void Nor()
        {
            Gates.IC nor = CreateNor();


            nor[0] = false;
            nor[1] = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);

            nor[0] = true;
            nor[1] = false;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);

            nor[0] = false;
            nor[1] = false;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(true, nor.Output[0]);

            nor[0] = true;
            nor[1] = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);
        }
示例#3
0
        private Gates.IC CreateAndChain(int len)
        {
            Gates.Circuit andc = new Gates.Circuit();
            andc.Start();



            Gates.IOGates.UserInput in1 = new Gates.IOGates.UserInput();
            andc.Add(in1);
            Gates.IOGates.UserOutput out1 = new Gates.IOGates.UserOutput();
            andc.Add(out1);

            if (len > 1)
            {
                Gates.IC cand1 = CreateAndChain(len / 2);
                Gates.IC cand2 = CreateAndChain(len / 2);
                andc.Add(cand1);
                andc.Add(cand2);
                andc[new Gates.Terminal(0, cand1)] = new Gates.Terminal(0, in1);
                andc[new Gates.Terminal(0, cand2)] = new Gates.Terminal(0, cand1);
                andc[new Gates.Terminal(0, out1)]  = new Gates.Terminal(0, cand2);
            }
            else
            {
                Gates.BasicGates.And mand = new Gates.BasicGates.And();
                andc.Add(mand);
                andc[new Gates.Terminal(0, mand)] = new Gates.Terminal(0, in1);
                andc[new Gates.Terminal(1, mand)] = new Gates.Terminal(0, in1);
                andc[new Gates.Terminal(0, out1)] = new Gates.Terminal(0, mand);
            }

            return(new Gates.IC(andc, new Gates.IOGates.UserInput[] { in1 },
                                new Gates.IOGates.UserOutput[] { out1 }, "AndChain"));
        }
示例#4
0
        Gates.IC CreateSRLatch()
        {
            Gates.Circuit sr = new Gates.Circuit();
            sr.Start();
            Gates.IC nor1 = CreateNor();
            Gates.IC nor2 = CreateNor();

            Gates.IOGates.UserInput  in1  = new Gates.IOGates.UserInput();
            Gates.IOGates.UserInput  in2  = new Gates.IOGates.UserInput();
            Gates.IOGates.UserOutput out1 = new Gates.IOGates.UserOutput();
            Gates.IOGates.UserOutput out2 = new Gates.IOGates.UserOutput();

            sr.Add(nor1);
            sr.Add(nor2);

            sr.Add(in1);
            sr.Add(in2);
            sr.Add(out1);
            sr.Add(out2);


            sr[new Gates.Terminal(0, nor1)] = new Gates.Terminal(0, in1);
            sr[new Gates.Terminal(1, nor2)] = new Gates.Terminal(0, in2);

            sr[new Gates.Terminal(1, nor1)] = new Gates.Terminal(0, nor2);
            sr[new Gates.Terminal(0, nor2)] = new Gates.Terminal(0, nor1);

            sr[new Gates.Terminal(0, out1)] = new Gates.Terminal(0, nor1);
            sr[new Gates.Terminal(0, out2)] = new Gates.Terminal(0, nor2);

            return(new Gates.IC(sr, new Gates.IOGates.UserInput[] { in1, in2 },
                                new Gates.IOGates.UserOutput[] { out1, out2 }, "SRLatch"));
        }
示例#5
0
        public void NorInCiruit()
        {
            Gates.IC      nor = CreateNor();
            Gates.Circuit c   = new Gates.Circuit();
            c.Start();
            c.Add(nor);


            // primarily just tests that c's wait
            // also makes the nor wait

            nor[0] = false;
            nor[1] = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);

            nor[0] = true;
            nor[1] = false;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);

            nor[0] = false;
            nor[1] = false;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(true, nor.Output[0]);

            nor[0] = true;
            nor[1] = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, nor.Output[0]);
        }
示例#6
0
        /// <summary>
        /// Clone the IC with terminals and location hinting.  The IC may optionally
        /// be renamed in this cloning process.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IC CreateUserInstance(string name)
        {
            Gates.IC nic = (Gates.IC)((Gates.IC)_gate).Clone(name);

            // duplicate the term ids because they have individual
            // refernece to actual terminal
            IC nuic = new IC(nic, CloneTerminals());

            DuplicateLocationHinting(nuic);

            return(nuic);
        }
示例#7
0
        public IC(Gates.IC gate, TerminalID[] termsid)
            : base(gate, termsid)
        {
            locationHints = new Dictionary <Gates.AbstractGate, GateLocation>();

            // may need to length the gate symbol
            // to accomodate the label


            // load the terminal tooltips
            foreach (TerminalID tid in _termsid)
            {
                Gates.AbstractGate ab;
                if (tid.isInput)
                {
                    ab = gate.Inputs[tid.ID];
                }
                else
                {
                    ab = gate.Outputs[tid.ID];
                }
                tid.t.ToolTip = ab.Name;
            }



            r                 = new Rectangle();
            r.Margin          = new System.Windows.Thickness(12, 17, 12, 17);
            r.Width           = this.Width - 24;
            r.Height          = this.Height - 34;
            r.Stroke          = Brushes.Black;
            r.StrokeThickness = 2;
            r.Fill            = Brushes.White;
            myCanvas.Children.Add(r);


            nm      = new TextBox();
            nm.Text = gate.Name;
            nm.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            nm.VerticalAlignment   = System.Windows.VerticalAlignment.Center;
            nm.TextAlignment       = TextAlignment.Center;
            nm.Margin          = new System.Windows.Thickness(20, this.Height / 2 - 12, 20, this.Height / 2 - 12);
            nm.Width           = this.Width - 40;
            nm.Height          = 24;
            nm.BorderThickness = new Thickness(0);
            IsReadOnly         = true;
            myCanvas.Children.Add(nm);

            nm.LostFocus += new RoutedEventHandler(nm_LostFocus);
            nm.KeyDown   += new System.Windows.Input.KeyEventHandler(nm_KeyDown);
            ResizeDueToName();
        }
示例#8
0
 private void Visit(Gates.IC ic)
 {
     if (!visited.Contains(ic.Name))
     {
         visited.Add(ic.Name);
         foreach (Gates.AbstractGate ag in ic.Circuit)
         {
             if (ag is Gates.IC)
             {
                 Visit((Gates.IC)ag);
             }
         }
         results.Add(ic);
     }
 }
示例#9
0
        /// <summary>
        /// Create an IC, but ignore user input and output.  This is useful if
        /// the IC is just a package for some other operation.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public UIGates.IC CreateNonTerminaledIC(string name)
        {
            Gates.IC nic = new Gates.IC(c, new Gates.IOGates.UserInput[0],
                                        new Gates.IOGates.UserOutput[0], name);
            UIGates.IC nuic = new UIGates.IC(nic, new Gate.TerminalID[0]);

            // finally, hint the IC so that we can remember
            // where things are placed visually in the future
            foreach (Gates.AbstractGate g in c)
            {
                nuic.locationHints.Add(g, pos(g));
            }

            return((UIGates.IC)nuic.CreateUserInstance());
        }
示例#10
0
        public void PropagationTest()
        {
            // this test checks for the accuracy
            // of wait for propagation
            // by determining if it fully waits for all propagation
            // to occur through the contained sub-circuits

            // this came from a problem where all sub-circuits
            // were instructed to wait BUT
            // the loop didn't repeat this based on that
            // so one could pass off an incomplete result
            // to another and the waiting could end before
            // it was all really done
            Gates.Circuit c = new Gates.Circuit();
            c.Start();

            // EXTREMELY SPECIFIC
            // The long chain appears FIRST in the circuit add list
            // But the input connects to the short chain first
            // Then the wait waits on the long chain, which has nothing
            // then briefly on the short chain, which passes a change
            // to the long chain...
            // but it DOESN'T wait for the long chain to finish
            // (because it already waited on the long chain before it had any work!)
            Gates.IC ac1 = CreateAndChain(64);
            Gates.IC ac2 = CreateAndChain(10);

            Gates.IOGates.UserInput ui   = new Gates.IOGates.UserInput();
            Gates.BasicGates.And    mand = new Gates.BasicGates.And();

            c.Add(ac1);
            c.Add(ac2);
            c.Add(ui);
            c.Add(mand);

            c[new Gates.Terminal(0, ac2)]  = new Gates.Terminal(0, ui);
            c[new Gates.Terminal(0, ac1)]  = new Gates.Terminal(0, ac2);
            c[new Gates.Terminal(0, mand)] = new Gates.Terminal(0, ac1);
            c[new Gates.Terminal(1, mand)] = new Gates.Terminal(0, ac2);

            ui.Value = false;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(false, mand.Output[0]);

            ui.Value = true;
            Gates.PropagationThread.Instance.WaitOnPropagation();
            Assert.AreEqual(true, mand.Output[0]);
        }
示例#11
0
        public void SRLatch()
        {
            Gates.Circuit c = new Gates.Circuit();
            c.Start();

            Gates.IOGates.UserInput  ui_r = new Gates.IOGates.UserInput();
            Gates.IOGates.UserInput  ui_s = new Gates.IOGates.UserInput();
            Gates.IOGates.UserOutput uo   = new Gates.IOGates.UserOutput();
            Gates.IC latch = CreateSRLatch();

            c.Add(ui_r);
            c.Add(ui_s);
            c.Add(uo);
            c.Add(latch);

            c[new Gates.Terminal(0, latch)] = new Gates.Terminal(0, ui_r);
            c[new Gates.Terminal(1, latch)] = new Gates.Terminal(0, ui_s);
            c[new Gates.Terminal(0, uo)]    = new Gates.Terminal(0, latch);

            for (int i = 0; i < 4; i++)
            {
                // SET
                ui_r.Value = false;
                ui_s.Value = true;
                Gates.PropagationThread.Instance.WaitOnPropagation();
                Assert.AreEqual(true, uo.Value);

                // HOLD
                ui_r.Value = false;
                ui_s.Value = false;
                Gates.PropagationThread.Instance.WaitOnPropagation();
                Assert.AreEqual(true, uo.Value);

                // RESET
                ui_r.Value = true;
                ui_s.Value = false;
                Gates.PropagationThread.Instance.WaitOnPropagation();
                Assert.AreEqual(false, uo.Value);

                // HOLD
                ui_r.Value = false;
                ui_s.Value = false;
                Gates.PropagationThread.Instance.WaitOnPropagation();
                Assert.AreEqual(false, uo.Value);
            }
        }
示例#12
0
        public UIGates.IC CreateIC(string name)
        {
            posGates = new List<GTerm>();

            List<Gates.IOGates.UserInput> uis = new List<Gates.IOGates.UserInput>();
            List<Gates.IOGates.UserOutput> uos = new List<Gates.IOGates.UserOutput>();

            // Determine the center-point of the X
            bool fst = true;
            double maxX = 0, maxY = 0, minX = 0, minY = 0;
            foreach (Gates.AbstractGate g in c)
            {
                Point p = pos(g).GetPoint();
                if (fst)
                {
                    maxX = p.X;
                    minX = p.X;
                    maxY = p.Y;
                    minY = p.Y;
                    fst = false;
                }
                maxX = Math.Max(maxX, p.X);
                maxY = Math.Max(maxY, p.Y);
                minX = Math.Min(minX, p.X);
                minY = Math.Min(minY, p.Y);
                //avgX += p.X;
                //avgY += p.Y;
            }

            //avgX /= c.Count;
            //avgY /= c.Count;
            avgX = (maxX + minX) / 2.0;
            avgY = (maxY + minY) / 2.0;

            // For all user i/o gates, determine where they fall in the X
            foreach (Gates.AbstractGate g in c)
            {

                if (g is Gates.IOGates.UserInput)
                {
                    uis.Add((Gates.IOGates.UserInput)g);
                    PositionAndHold(g);
                }
                if (g is Gates.IOGates.UserOutput)
                {
                    uos.Add((Gates.IOGates.UserOutput)g);
                    PositionAndHold(g);
                }
            }

            // Apply a sort to order user i/o gates with respect
            // to the standard ordering that the IC uses to display its terminals
            // see Gate class
            posGates.Sort((firstVal, nextVal) =>
            {
                if (firstVal.pos != nextVal.pos)
                    return firstVal.pos.CompareTo(nextVal.pos);
                else
                    return firstVal.offset.CompareTo(nextVal.offset);
            });

            Gates.IC nic = new Gates.IC(c, uis.ToArray(), uos.ToArray(), name);
            List<Gate.TerminalID> tids = new List<Gate.TerminalID>();

            // constuct the terminal id list based on the sorted sequence
            foreach (GTerm gt in posGates)
            {
                tids.Add(new Gate.TerminalID(gt.gate is Gates.IOGates.UserInput,
                    gt.gate is Gates.IOGates.UserInput ?
                    uis.IndexOf((Gates.IOGates.UserInput)gt.gate) : uos.IndexOf((Gates.IOGates.UserOutput)gt.gate),
                    gt.pos));
            }

            UIGates.IC nuic = new UIGates.IC(nic, tids.ToArray());

            // finally, hint the IC so that we can remember
            // where things are placed visually in the future
            foreach (Gates.AbstractGate g in c)
            {
                nuic.locationHints.Add(g, pos(g));
            }

            // in some cases, the caller may be using these gates
            // so we create a clone so there is no interference
            return (UIGates.IC)nuic.CreateUserInstance();
        }
示例#13
0
        /// <summary>
        /// Create an IC, but ignore user input and output.  This is useful if
        /// the IC is just a package for some other operation.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public UIGates.IC CreateNonTerminaledIC(string name)
        {
            Gates.IC nic = new Gates.IC(c, new Gates.IOGates.UserInput[0],
                new Gates.IOGates.UserOutput[0], name);
            UIGates.IC nuic = new UIGates.IC(nic, new Gate.TerminalID[0]);

            // finally, hint the IC so that we can remember
            // where things are placed visually in the future
            foreach (Gates.AbstractGate g in c)
            {
                nuic.locationHints.Add(g, pos(g));
            }

            return (UIGates.IC)nuic.CreateUserInstance();
        }
示例#14
0
        /// <summary>
        /// Create an IC based on the circuit.  All user input and outputs
        /// will be turned into terminals on this IC.  The technique is to establish
        /// a center-point of the gates involved, and establish a "X" shape originating
        /// from this center.  The position of the user i/o gates within this X shape
        /// determine how they appear on the IC.  The goal is to have the layout of
        /// terminals match the layout and ordering of user inputs and outputs.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public UIGates.IC CreateIC(string name)
        {
            posGates = new List <GTerm>();

            List <Gates.IOGates.UserInput>  uis = new List <Gates.IOGates.UserInput>();
            List <Gates.IOGates.UserOutput> uos = new List <Gates.IOGates.UserOutput>();

            // Determine the center-point of the X

            double maxX = 0, maxY = 0, minX = 0, minY = 0;


            var pts = createCenter();

            maxX = pts.Right;
            minX = pts.Left;
            maxY = pts.Bottom;
            minY = pts.Top;

            avgX = (maxX + minX) / 2.0;
            avgY = (maxY + minY) / 2.0;

            // For all user i/o gates, determine where they fall in the X
            foreach (Gates.AbstractGate g in c)
            {
                if (g is Gates.IOGates.UserInput)
                {
                    uis.Add((Gates.IOGates.UserInput)g);
                    PositionAndHold(g);
                }
                if (g is Gates.IOGates.UserOutput)
                {
                    uos.Add((Gates.IOGates.UserOutput)g);
                    PositionAndHold(g);
                }
            }

            // Apply a sort to order user i/o gates with respect
            // to the standard ordering that the IC uses to display its terminals
            // see Gate class
            posGates.Sort((firstVal, nextVal) =>
            {
                if (firstVal.pos != nextVal.pos)
                {
                    return(firstVal.pos.CompareTo(nextVal.pos));
                }
                else
                {
                    return(firstVal.offset.CompareTo(nextVal.offset));
                }
            });

            Gates.IC nic = new Gates.IC(c, uis.ToArray(), uos.ToArray(), name);
            List <Gate.TerminalID> tids = new List <Gate.TerminalID>();

            // constuct the terminal id list based on the sorted sequence
            foreach (GTerm gt in posGates)
            {
                tids.Add(new Gate.TerminalID(gt.gate is Gates.IOGates.UserInput,
                                             gt.gate is Gates.IOGates.UserInput ?
                                             uis.IndexOf((Gates.IOGates.UserInput)gt.gate) : uos.IndexOf((Gates.IOGates.UserOutput)gt.gate),
                                             gt.pos));
            }



            UIGates.IC nuic = new UIGates.IC(nic, tids.ToArray());

            // finally, hint the IC so that we can remember
            // where things are placed visually in the future
            foreach (Gates.AbstractGate g in c)
            {
                nuic.locationHints.Add(g, pos(g));
            }


            // in some cases, the caller may be using these gates
            // so we create a clone so there is no interference
            return((UIGates.IC)nuic.CreateUserInstance());
        }
示例#15
0
 /// <summary>
 /// The "template" here is used as a visual template, that is, a descriptor of
 /// how the terminals should be laid out and a provided of location hints.
 /// The IC becomes the actual circuit within the return value.
 /// Accordingly, it is assumed that the template will be of the same
 /// "type" as the IC.
 /// </summary>
 /// <param name="ic"></param>
 /// <param name="template"></param>
 /// <returns></returns>
 public static IC CreateFromTemplate(Gates.IC ic, IC template)
 {
     UIGates.IC nuic = new UIGates.IC(ic, template.CloneTerminals());
     template.DuplicateLocationHinting(nuic);
     return(nuic);
 }