Perfect Toggle (T) Latch, Torch-Only Version:
On my latches page, I showed how an imperfect toggle (T) latch could be constructed from an RS latch and a multiplexer. Before assembling the full counter, we need a perfect toggle latch - one that will work correctly regardless of how long or short the trigger line is ON.
I figured out how to do this by using two imperfect toggle latches put together. The wires are crossed, so instead of the RS latch output re-feeding the same side (and causing a "race" condition), it instead feeds the OPPOSITE side (the inactive side). When the trigger is turned OFF, the opposite side is triggered ON, and that RS latch feeds the original side.
This behavior is illustrated in the following four screenshots:
Here's the simplified schematic for this circuit:
Came here from YouTube and are wondering what the Bs and Ts are? Those are inverters; click here for explanation.
It looks like I accidentally made it so the output redstone lamp toggles on an ON-to-OFF transition of the main trigger (instead of OFF-to-ON), but for the clock project it doesn't matter, and it's also easily changed (either change the output to the other latch OR change the location of the inverter at the bottom of the main trigger's "bridge").
Putting It All Together:
With the perfect toggle latch complete, the next step was to duplicate it 3 times. To keep the counter from sprawling out all over the place, I decided to place each one in a separate vertical layer. Also to simplify things each layer is identical. This screenshot shows the spacing between layers as well as an easy way to duplicate layers one or two "rows" at a time:
Next I added staircases to propagate the output of each toggle latch to the next one above. The latch input is at the front so I pulled a signal with an identical state from the near-left of the layer. A couple of inverters were cleverly incorporated into the staircase to repeat the signal due to the long distance involved.
Finally, the clock (pulser) was added. I first tried a standard 5 clock. Despite the "perfect" nature of the toggle latch, apparently the propagation delays through the circuit are long enough that the clock is already sending the next pulse by the time the signals are returing from the back due to the previous pulse. As a result, Minecraft went berzerk and all my torches kept flashing on and off. I had to tear up parts of the circuit and rebuild it (not to mention exit and re-enter the game twice) before the torch freak-out finally stopped. I'm guessing this is a game bug. Anyway, the latch worked just fine with a 9 clock attached. I then had to up it to a 13 clock because of propagation delays through all the layers; otherwise, the bottom layer would change state for the next pulse before the top layer was finished updating! The 13 clock seemed to be the smallest clock that would work and still have the outputs correct for at least a half second.
Check out the video to see the counter in action.
Here's a sample view from the back showing the four outputs. The counter counts from 0000 to 1111 in binary (0 to 15). The screenshot is showing a value of 6. The video above all 16 states are shown and labeled.
In Minecraft PE 0.14.0, repeater blocks were added. As discussed on my inverter page, these help make much more compact circuits, but, aside from the ability to adjust timing, don't actually add new functionality. However, if you put two inverters together like this, with one pointed at the side of the other, you get a totally new device called a Data Latch:
If the switch on the left is in the "lock" position, the data latch outputs whatever value (ON or OFF) that was previously loaded into it (in this case ON). If the switch on the left is in the "load" position, it loads the value from the data line (and outputs it also, in this case OFF).
An equivalent way to think of it is like this: The image on the right is like a normal repeater (OFF goes in, OFF goes out). If power comes in off of the side from another repeater (and only from another repeater), the first repeater is locked to whatever output it had at the time it was locked (in this case ON).
Using two data latches instead of two RS latches, it's possible to make a totally redesigned toggle latch that is amazingly smaller, faster, and simpler. But it works exactly the same as the original perfect toggle latch. Here are the four states:
The simplified schematic for this circuit is below. Note that the repeater in the lower-left gets its power, interestingly, from the anchor of the inverter, so it receives a "positive" (non-inverted) signal whereas the repeater just above it gets the usual inverted signal from the redstone torch.
Just as we had two RS latches loop back to each other to make the perfect toggle latch, so we are using two data latches looped back to each other here. But there's an equivalent, even easier way to interpret the above circuit. Notice in the last screenshot above the schematic, the area in the light blue box looks just like a compact 5-clock. If you think about it, a toggle latch is similar to a 5-clock in that the output goes ON, OFF, ON, OFF, ON, OFF. We merely need to control this pattern so it goes ON, OFF when WE want it to, not automatically. So the data latches in effect "pause" the clock. Each time the input line changes state, the repeater that is locked alternates. Every OTHER cycle, the new state of the clock leaks through.
With the much smaller latch, it's really easy to string four together, add a clock, and be done:
Check out the following video to see the revised 4-bit counter in action:
There's still some slight propagation delays, but it's still much faster than the previous counter. It's also small enough to be practical in a full Minecraft PE game, though you'll still need a fairly large basement or warehouse for the circuiting.