コード例 #1
0
        /// <summary>
        /// Generate the mosfet instance
        /// </summary>
        /// <param name="type">Type</param>
        /// <param name="name">Name</param>
        /// <param name="parameters">Parameters</param>
        /// <param name="netlist">Netlist</param>
        /// <returns></returns>
        protected override ICircuitObject Generate(string type, CircuitIdentifier name, List <Token> parameters, Netlist netlist)
        {
            // Errors
            switch (parameters.Count)
            {
            case 0: throw new ParseException($"Node expected for component {name}");

            case 1:
            case 2:
            case 3: throw new ParseException(parameters[parameters.Count - 1], "Node expected", false);

            case 4: throw new ParseException(parameters[3], "Model name expected");
            }

            // Get the model and generate a component for it
            ICircuitObject    model  = netlist.Path.FindModel <ICircuitObject>(netlist.Circuit.Objects, new CircuitIdentifier(parameters[4].image));
            ICircuitComponent mosfet = null;

            if (Mosfets.ContainsKey(model.GetType()))
            {
                mosfet = Mosfets[model.GetType()].Invoke(name, model);
            }
            else
            {
                throw new ParseException(parameters[4], "Invalid model");
            }

            // The rest is all just parameters
            mosfet.ReadNodes(netlist.Path, parameters);
            netlist.ReadParameters((IParameterized)mosfet, parameters, 4);
            return(mosfet);
        }
コード例 #2
0
        /// <summary>
        /// Deal with a component
        /// </summary>
        /// <param name="c">The circuit object</param>
        private void CheckObject(ICircuitObject c)
        {
            // Circuit components
            if (c is ICircuitComponent icc)
            {
                // Check for short-circuited components
                int  n  = -1;
                bool sc = true;
                for (int i = 0; i < icc.PinCount; i++)
                {
                    if (n < 0)
                    {
                        n = icc.GetNodeIndex(i);
                    }
                    else if (n != icc.GetNodeIndex(i))
                    {
                        sc = false;
                        break;
                    }
                }
                if (sc)
                {
                    throw new CircuitException($"{icc.Name}: All pins have been short-circuited");
                }

                // Get the node indices for each pin
                int[] nodes = new int[icc.PinCount];
                for (int i = 0; i < nodes.Length; i++)
                {
                    nodes[i] = icc.GetNodeIndex(i);
                    unconnected.Add(nodes[i]);
                }

                // Use attributes for checking properties
                var  attributes     = c.GetType().GetCustomAttributes(false);
                bool hasconnections = false;
                foreach (var attr in attributes)
                {
                    // Voltage driven nodes are checked for voltage loops
                    if (attr is VoltageDriver vd)
                    {
                        voltagedriven.Add(new Tuple <ICircuitComponent, int, int>(icc, nodes[vd.Positive], nodes[vd.Negative]));
                    }

                    // At least one source needs to be available
                    if (attr is IndependentSource)
                    {
                        HasSource = true;
                    }

                    if (attr is ConnectedPins conn)
                    {
                        int[] tmp = new int[conn.Pins.Length];
                        for (int i = 0; i < conn.Pins.Length; i++)
                        {
                            tmp[i] = nodes[conn.Pins[i]];
                        }
                        AddConnections(tmp);
                        hasconnections = true;
                    }
                }

                // Check connections
                if (!hasconnections)
                {
                    AddConnections(nodes);
                }
            }
        }