Logic Gates

From Terraria Wiki
(Redirected from Logic Gate (XOR))
Jump to navigation Jump to search
Desktop versionConsole versionMobile version
Desktop/Console/Mobile-Only Content: This information applies only to the Desktop, Console, and Mobile versions of Terraria.
Logic Gates
  • Logic Gates item sprite
Auto-use
Stack digit 9.pngStack digit 9.pngStack digit 9.pngStack digit 9.png
Statistics
Type
Placeable✔️ (1 wide × 1 high)
Use time15 (Very fast)
RarityRarity level: 1
Buy2 GC
Sell40 SC
Research5 required
Example of the logic gate AND.

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 2 GC 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
Logic Gate (AND)Logic Gate (AND)Internal Item ID: 3603 Turns on when all lamps are active.
Logic Gate (NAND)Logic Gate (NAND)Internal Item ID: 3605 Turns on unless all lamps are active.
Logic Gate (OR)Logic Gate (OR)Internal Item ID: 3604 Turns on when at least one lamp is active.
Logic Gate (NOR)Logic Gate (NOR)Internal Item ID: 3606 Turns on when no lamp is active.
Logic Gate (XOR)Logic Gate (XOR)Internal Item ID: 3607 Turns on when exactly one lamp is active.
Logic Gate (XNOR)Logic Gate (XNOR)Internal Item ID: 3608 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[1], 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 the player triggers 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)

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)

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

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).

A more compact restricted storage circuit design.

Safety switch

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 logic gates 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

  • Desktop 1.3.2:
    • Fixed XNOR being named NXOR.
    • Added "Also often called NXOR" to XNOR's tooltip.

Footnotes

  1. A tick is a time unit countable by the software. Most of Terraria's updating logic happens every tick. A tick has the length of 1/60th of a second, hence there are 60 ticks in a second and 3600 ticks in a minute.