Redstone circuits/Memory

Latches and flip-flops are effectively 1-bit memory cells. They allow circuits to store data and deliver it at a later time, rather than acting only on the inputs at the time they are given. As a side-effect of this, they can turn an impulse into a constant signal. Functions using latches can be built to give different outputs each time a circuit is activated even if the inputs don't change, and so circuits using them are referred to as "sequential logic". They allow for the design of counters, long-term clocks, and complex memory systems, which cannot be created with combinatorial logic gates alone.

There are four basic categories of latches, distinguished by how they are set and reset. Most of these can be built as a "latch" controlled by the level of a signal, or as a "flip-flop" triggered by a change in the signal.
 * A RS latch has separate control lines to set (turn on) or reset (turn off) the latch. Many also have "dual outputs" -- that is, they also have an inverse output, which is always opposite to the main output.  The oldest form of RS latch in Minecraft is the RS-NOR latch, which forms the heart of many other latch and flip-flop designs.
 * A D latch has a data input and a clock input. When the clock is triggered, the data input is copied to the output, and held until the clock is triggered again.
 * A JK latch has three inputs: A clock input, and the J and K inputs. When the clock is triggered, the latch's output can be set, reset, toggled, or left as is, depending on the combination of J and K.
 * A T latch has only one input, the toggle. Whenever the toggle is triggered, the latch changes its state from OFF to ON or vice versa.

About RS Latches
An RS latch is a device with 2 inputs R and S, and 1 or 2 outputs, Q and optionally Q̅. When a signal pulse comes into S, Q is set on and stays on until a similar signal comes into R, upon which Q is reset to "off". Q̅ indicates the opposite of Q -- when Q is high, Q̅ is low, and vice versa. Where a Q̅ output is available, you can often save a NOT gate by using it instead of Q.

The RS latch can turn an impulse into a constant redstone signal, or the lack of one, in both cases reversably. Thus it can be used similarly to a lever, but controlled by redstone instead of clicks. Typical uses include an alarm system in which a warning light stays on after a pressure plate is activated until a reset button is pushed, or a rail T-junction being set and reset by different detector rails.

Setting both inputs high simultaneously is a "forbidden" condition, generally something to avoid. In the truth table, S=1, R=1 breaks the inverse relationship between Q and Q̅. If this happens, you will get "undefined behavior" -- various designs can do different things, and especially, Q and Q̅ can be high or low at the same time.

Any RS latch with dual outputs is functionally symmetrical: pulsing each input turns on "its" output, and turns off the other one. Thus R and S are interchangeable, if you also swap the outputs: Which input you pick as S chooses which of the outputs is Q, then the other input will be R and the other output will be Q̅. (If the original circuit only had a Q output, then swapping the inputs will turn it into Q̅.) In several designs (A, B, D, F, I) the functional symmetry is reflected by the circuit's physical symmetry, with each input energizing the torch it leads to, while turning off the other.

RS latches can be built in a number of ways:
 * The RS NOR latch is the "original" RS latch, and still among the smallest memory devices that can be made in vanilla Minecraft. While they can be built with just torches and redstone dust, repeaters can also be used.
 * It is also possible to construct an RS NAND latch, using NAND gates instead of NOR gates. These will be larger and more complex than an RS NOR latch, but may be useful for specialized purposes. And finally, various new components have allowed for yet other possibilities.
 * Other RS latches can be created by fitting an "input sustaining circuit" with a reset switch, say by adding a pair of NOT gates or a piston, placed so as to interrupt the circuit when triggered. Such a construction can be nearly as compact as an RS NOR latch (and often with better I/O isolation and/or timing), but they will usually not have a natural Q̅ output.
 * The introduction of the Block of Redstone allows a design that is tiny (4&times;1&times;1 plus inputs and outputs) and fast, but expensive -- besides the redstone block, it requires two pistons.

An RS latch can easily be expanded into a monostable circuit, which automatically disables itself some time after being activated. To do this, split the output redstone path into 2 parts. The new path should run trhough some repeaters, and in to the reset input. When you turn on the latch, the redstone will run through the delay before turning off the latch. (This works not only for Q and R, but for Q̅ and S as well.) You can also use a more complex delay mechanism instead of repeaters, e.g. a water clock.)

An "Enable/Disable RS latch" can be made by adding a pair of AND gates in front of the inputs, testing each of them against a third input, E. Now if E is true, the memory cell works as normal. If E is false, the memory cell will not change state. That is, E latches the RS latch itself.

D Latches and Flip-Flops
A D or "data" flip-flop, or a gated D latch, has two inputs: The data line D, and the "clock" input C.  When triggered by C, the circuits set their output (Q) to D, then retain that output state between triggers. A D flip-flop is edge triggered; it sets the output to D only when its clock input changes from "off" to "on" (positive edge) or vice versa (negative edge), according to the circuit. A D gated latch, is level triggered, changing the output to match D as long as the clock is either high or low (again, according to the circuit). You can often turn a gated D latch into a D flip flop by adding an edge trigger. Some of the designs also have the additional inputs and inverse output of an RS Latch.

Building these devices with torches is fairly unwieldy, but since version 1.4 of Minecraft, repeaters have gained latching functionality, which reduces the basic D gated latch to two repeaters, and the D flipflop to four repeaters:

Design G uses the repeater's new latching feature, added to the game in version 1.4. It holds its state while the clock is high, and is by far the most compact of the D latch designs. Design H combines two G latches, one high and one low triggered, to create a positive edge-triggered flip-flop. The block and redstone torch can be reversed for a negative edge-triggered design.

Torch-based Designs
For historical interest, several older designs, not dependent on latched repeaters, are provided, along with a table of their resource needs and other characteristics.

JK Flip-Flops and Latches
A JK flip-flop is another memory element which, like the D flip-flop, will only change its output state when triggered by a clock signal C. They can be edge-triggered (designs A, D, E or level-triggered (C).

When the flip-flop is triggered the effect on the output Q will depend on the values of the two inputs J and K:
 * If the input J = 1 and the input K = 0, the output Q = 1.
 * When J = 0 and K = 1, the output Q = 0.
 * If both J and K are 0, then the JK flip-flop maintains its previous state.
 * If both are 1, the output will complement itself — i.e., if Q = 1 before the clock trigger, Q = 0 afterwards.

The table below summarizes these states — note that Q(t) is the new state after the trigger, while Q(t-1) represents the state before the trigger.

The JK flip-flop's complement function (when J and K are 1) is only meaningful with edge-triggered JK flip-flops, as it's an instantaneous trigger condition. With level-triggered flip-flops (e.g. design C), maintaining the clock signal at 1 for too long causes a race condition on the output. Although this race condition is not fast enough to cause the torches to burn out, it makes the complement function unreliable for level-triggered flip-flops.

T Flip-Flops and Toggles
T flip-flops are also known as "toggles." Whenever T changes from OFF to ON, the output will toggle its state. A useful way to use T flip-flops in Minecraft could for example be a button connected to the input. When you press the button the output toggles (a door opens or closes), and does not toggle back when the button pops out. These are also the core of all binary counters and clocks, as they function as a "period doubler", releasing one pulse for every two received.

Rail T Flip-Flops
The Rail T flip-flop is a T flip-flop which uses rails and redstone. The general design uses a length of track that is stopped by a block at both ends. When the T flip-flop is in a stable state, the minecart is at either end of the track (depending on the state). An input pulse turns on powered rails at both ends of the track, causing the minecart to move to the other end.

Along the track, there are two separate detector elements (e.g. detector rails). These two detectors are each connected to an input of an RS NOR latch, and hence serve to translate minecart motion into a state transition. When the minecart moves, depending on its direction of motion, one detector will turn on (and off) before the other; the second detector to be hit is what determines which input of the RS NOR latch stays on last and hence what the new state of the RS NOR latch is.

The design to the left makes use of pressure plates on the inside of a turn to detect minecart motion in redstone. (Note that the other side of the latch isn't a true Q̅, as the passage of the cart turns on Q before actually switching the latch.) The design to the right uses detector rails.

This type of T flip-flop is slower than traditional redstone-only circuits, but this may be desirable in certain situations. With T flip-flop designs that are level-triggered (as opposed to clocked or edge-triggered), a long input pulse will cause the flip-flop to continuously switch state (oscillate) while the pulse is present. In pure redstone circuits, this is only limited by the redstone circuit delays, and hence a relatively short input pulse can cause several state transitions. Pure redstone T flip-flops usually include an edge-trigger or pulse-limiting circuit to the design, since the input pulse usually can't be guaranteed to be short enough without the use of that kind of circuit.

With rail-based designs, the speed at which the output can flip is limited by the time needed for the cart to move from one end of its rail to the other, which allows for a much longer pulse to be applied to a level-triggered input without needing an edge-trigger or pulse limiter circuit. However, the delay between the input pulse and the output transition is also longer.

红石电路/锁存器