Logic Gates

A Logic Gate is a mechanism used to provide logic to wiring setups. Input is provided using a stack of Logic Gate Lamps placed on top of it. The logic gate turns on and off depending on its inputs, and emits a signal every time its state changes.
The logic gates, along with the Logic Gate Lamps, are purchased from the Steampunker for each.
== Behavior ==
All gates are turned off when they are first placed. Whenever a lamp is added or removed or one of its lamps changes state, the logic gate updates according to the following rules:
Logic Gate	Rule
 	Turns on when all lamps are active.
 	Turns on when any lamp is inactive.
 	Turns on when at least one lamp is active.
 	Turns on when no lamp is active.
 	Turns on when exactly one lamp is active.
 	Turns on when the total active lamps is not one.
Whenever a logic gate is turned on or off it emits a signal. If this signal would cause a logic gate to change its own inputs and emit two signals on the same tick, instead a puff of smoke is shown at the gate and only one signal is emitted. Note that while only one signal is emitted, the state of the logic gate itself still changes.
=== Faulty lamps ===
Placing a faulty logic gate lamp above a logic gate turns it blue, overriding its normal functionality and changing its behavior entirely. The logic gates function as a randomizer instead, and can no longer turn on or off. The gate no longer emits signals when normal logic lamps above it change state. Instead, when the faulty lamp is triggered by a signal, it randomly selects one of the logic lamps below it. If that lamp is on, the logic gate emits a signal. So, if there are three lamps below the faulty lamp, two ON and one OFF, the gate has a 2/3 chance to emit a signal when the faulty lamp is triggered.
A faulty lamp can be triggered multiple times in one tick, effectively giving the gate multiple chances to emit a signal. Multiple signals cannot be emitted on the same tick this way, however, the gate will have a chance to emit a signal for every time a faulty lamp receives a signal. For example, if you trigger a faulty gate with one out of two of its logic gate lamps on twice in the same tick, it will have an 3/4 chance of emitting a signal.
Faulty lamps continue to work if only one logic gate lamp is under it. In this case, the logic gate as a whole acts as an "if" statement, only passing a signal through if the single lamp is on. If no logic gate lamps are attached, the logic gate never sends signals when the faulty lamp is triggered. A very practical use of this is a one-way wire: when a logic gate receives a signal it does nothing, but triggering the faulty lamp above it does send a signal. The logic gate lamp under the faulty lamps needs to be on. Also, this wire can be turned off by sending a signal to the logic gate lamp. Making two of these lets the player control all four possible states of a wire: Off, on (A->B), on (A<-B), on (A<-->B).
=== Inverted gates ===
Since gates emit a signal whenever they either turn on or off, the "inverted" versions of each gate (those with an "N" prefix) behave identically to their normal counterparts in most situations. The only difference is when the first lamp is added to a new gate. (For example, both the NAND and AND gates start off when first placed. However, upon adding an off gate, the AND gate would stay off and emit no signal while the NAND gate would turn on, thus emitting a signal.)
== Useful circuits ==
=== Toggle flip-flop (T flip-flop) ===
A toggle flip-flop is a device with one input and changes state every other input. The device consists of a faulty logic gate lamp, a logic gate lamp, and a gate. It is particularly useful when utilizing sensors to toggle activations without having the sensor's ending pulse turn off the device.
=== Set/Reset flip-flop (SR flip-flop) ===
A set/reset flip-flop is a device that has two inputs, and can be switched between the two. However, multiple activations on the same input will not change the state. It will output a pulse when the state changes. In the illustration, red and green are the inputs, and blue is the output.
=== Restricted storage circuit ===
A circuit which can store an intermediate value. It has three inputs (at the top), write, read, and input, from left to right, and one output. It also has an internal bit-state that is not accessible from outside. While read is off, the internal bit will never change; while read is on, the internal bit will copy input. Independent of that, while write is off, the output will never change; while write is on, the output will copy the internal bit. The bit is the "or" logic gate next to the junction box (hammering).
=== Safety switch ===
Simple switch to turn traps on/off. Uses two logic gates: AND (3 lamps), and XOR. Switch and pressure plate connected to XOR gate to reset AND gate after each use, so the trap would be activated every time the plate is triggered as long as the switch is not active.
== Notes ==
* NOT gates are not in the game as they would simply emit a signal every time an input changes due to how wire signals work in Terraria.
* Multiple faulty lamps can be attached to a single logic gate, all of which will cause the gate to output a signal. However, only logic gate lamps below the lowest faulty lamp in the chain affect the chance of the logic gate emitting a signal. Any above it are ignored.
*In electronics and logic, an XOR gate with more than 2 inputs is commonly regarded as a cascade of 2-input XOR-gates, thus behaving as a parity-checker with the output being on if there is an odd number of inputs turned on. In terraria, however, an XOR-gate with multiple inputs only turns on when exactly one input is turned on.
== History ==
== Footnotes ==