@willow-dls/core
    Preparing search index...

    Class Circuit

    The Circuit is how you execute the simulation. It provides everything you need to evaluate a complete circuit (which may or may not consist of SubCircuits) with or without a clock.

    If a circuit has no clock, then it is simply evaluated like a combinatorial or sequential circuit. However, if it does have a clock, then the engine will tick the clock automatically, evaluating the circuit each clock cycle.

    It is highly unlikely that you will instantiate this class directly; rather, you should normally retrieve circuits from your CircuitProject, provided to you by a CircuitLoader (see loadProject).

    Note

    This simulation engine is not thread-safe. While this shouldn't be a concern for most JavaScript environments, it is important to remember that this class contains a lot of internal state which is not intended to be mutated by different operating system threads at the same time.

    You absolutely cannot call run or resolve from two different threads, or modify the circuit while it is running in another thread. This will result in unintended consequences and undefined behavior.

    Hierarchy (View Summary)

    Index

    Constructors

    • Construct a new circuit.

      Parameters

      • id: string

        The string ID of the circuit. This will typically come from the data file from which the circuit was loaded.

      • name: string

        The name of the circuit. This will typically come from the data file from which the circuit was loaded.

      • elements: CircuitElement[]

        All of the circuit elements which make up this circuit. The order does not matter and does not affect execution of this circuit in any way. The constructor will index elements by their type for efficient access and execution as necessary.

      Returns Circuit

    Methods

    • Attach a logger to this loggable object so that messages logged with log will be send to this logger. Note that this can be called multiple times and multiple loggers can be attached to the same object. Log messages will be sent to all attached loggers.

      Parameters

      Returns void

    • Get all of the clocks in this circuit.

      Returns Clock[]

      An array of all the clocks in this circuit, recursively including the clocks from subcircuits as well (though subcircuits really ought to not have clocks in them, they should take the clock as an input.)

    • Get all of the input elements in this circuit.

      Returns Record<string, Input>

      A record of inputs where the key is the string name of the input and the value is the input object itself.

    • Get the name of this circuit.

      Returns string

      The name of this circuit which was passed into the constructor.

    • Get all of the output elements in this circuit.

      Returns Record<string, Output>

      A record of outputs where the key is the name of the output and the value is the output object itself.

    • Log a message, sending it to all loggers associated with this loggable.

      Parameters

      • level: LogLevel

        The log level to log the message at.

      • msg: string

        The message to log.

      • Optionaldata: any

        Any additional data to associate with the message.

      Returns void

    • Propagate all registered loggers (present and future) to the given loggable. This function will register the loggable object with the object it is being called on, establishing a parent-child relationship where all changes to the parent loggers will be propagated to the children.

      Parameters

      • loggable: CircuitLoggable

        The loggable object to attach all registered loggers to.

      Returns void

    • Resolve this circuit as though it was a CircuitElement even though it isn't. This method is used to implement SubCircuits and is called internally by run, and should not be used in any other scenarios. Use run instead.

      This method is responsible for executing a single clock cycle of the circuit. It contains the event loop, taking in the inputs, queuing them up, and then running the event loop until the values have propagated entirely through the circuit and there are no more events in the queue.

      Type Parameters

      Parameters

      • Optionalinputs: T

        The optional inputs to the circuit. If inputs are provided, each element in the circuit is reset by calling CircuitElement.reset and the simulation effectively starts over. If no inputs are provided here, then the simulation runs from the previous state it was in when it ended the last time it ran. This functionality is extremely useful for a clocked circuit, where the clock input changes and requires the circuit to be re-evaluated each cycle.

      Returns CircuitRunResult<T>

    • Execute this circuit with the provided inputs. This method runs an event-driven simulation which evaluates the circuit in terms of its elements, propagating the outputs of elements to other elements which are connected.

      If a circuit has no clock, then it is simply evaluated like a combinatorial or sequential circuit. However, if it does have a clock, then the engine will tick the clock automatically, evaluating the circuit each clock cycle.

      Note

      This simulation engine inherits some limitations of CircuitVerse when evaluating circuits with clocks:

      • Real life circuits go into a stable state between clock ticks. Irrespective of any implemented circuit element delays, the circuits go into a stable state between clock ticks. The simulator thus cannot simulate circuits that do not go into a stable state between clock ticks.
      • Similarly, circuits go into a stable state before processing input signals from different input elements.

      These limitations are due to the event-driven architecture of the engine, which this engine originally took inspiration from CircuitVerse. Note, of course, that this engine does not share any code with CircuitVerse, it simply used the same algorithm and general principles.

      Warning

      It is possible for this function to run forever. If you are attempting to run a circuit with clock inputs, but don't provide the optional haltCond, the simulation will simply keep ticking the clock indefinitely, and will thus never return, even if the circuit has stabilized.

      If your circuit contains a clock (which you can check by checking that getClocks returns an array with size > 0), you must provide a haltCond that will terminate the simulation either after a bus contains a certain value, or after the desired number of clock cycles have passed.

      If any CircuitLoggers are attached to this circuit, a warning will be emitted to them if this method detects that the simulation will run forever.

      Type Parameters

      Parameters

      • inputs: T

        A keyed object of input values or an array of input values. For end users, the keyed object will provide the most intuitive interface; the array syntax exists to support CircuitVerse SubCircuits, which don't have labeled inputs, but rather take their inputs by index. See the documentation for CircuitRunType and CircuitRunResult for more information on this parameter.

      • OptionalhaltCond: (
            clockHigh: boolean,
            clockCycles: number,
            output: CircuitRunResult<T>,
        ) => boolean

        A callback function which is executed each clock cycle and accepts a boolean representing whether the clock is currently high or low, the clock cycle number, and the current state of the circuits outputs. This function returns a boolean value indicating whether or not the simulation should be halted: a value of true indicates that the simulation should be halted, and a value of false indicates that it should continue on to the next clock cycle.

      • clockFrequency: number = 500

        An optional number which specifies the clock "frequency". The simulation speed is not changed (the simulation always executes as fast as possible), but if this parameter is provided, the simulator will emit a warning if a circuit takes longer than this amount of time to propagate its value. If you see this warning, the output values of the circuit probably cannot be trusted because of the limitations stated above.

      Returns CircuitRunResult<T>

      An object which contains the results of simulating this circuit, including the total propagation delay, the number of simulation steps required, and the output values.