Today I am going to explain how the SR Latch block works. This is how it looks like:
SR Latch in the game is an electronic device that can be used as memory, storing one bit of information. By applying different signals to its 3 inputs, you can manipulate the value of the bit in various ways.
In reality, an SR latch is a very simple device, but in the game I made it universal by combining functions of two different “real” latches. This is because I want to save block IDs by not having to add all of these latches as separate devices.
The value of the bit stored by the latch is available on the output, which in the picture is on the top of the latch. Apart from the output, this latch also has 3 inputs. On the left is the S input, which stands for “set”. On the right is the R input, which stands for “reset”. The third input on the bottom (marked with a little half-circle) I will talk about later. For now let’s assume that it is disconnected.
When the SR latch detects a rising edge on S input, it will set its state to 1, and this will immediately show on the output. The latch will now remember that its state is 1. You can walk away, or exit and reload the game, the latch’s state will still be 1. This is how memory works.
To clear the latch you have to send it a signal on the R input. As soon as a rising edge is detected there, the latch will reset its state to 0. And it will remember it until you again send it a signal on the S input.
Here’s the truth table of the SR latch when the bottom input is disconnected. S and R inputs are activated by a rising edge of the signal, so 1 means rising edge, 0 means anything else:
Both rising edges at exactly the same time are impossible, one will always come first. Simple.
Now about the function of the bottom input, the one marked with a half-circle (in reality it should be a little triangle, but I can’t make it look like triangle without shifting it off-center). When anything is connected to this input, the function of the SR latch changes. It becomes what is known as a synchronous latch. The signal on the bottom input is the “clock” signal. Its function is that the latch will only react to S and R inputs during a rising edge of a signal on the clock input. When there is no clock signal, you can put anything on S and R inputs, the latch will ignore it (i.e. remain in its current state, whatever it is). In other words, it will only “read” the S and R inputs in the instant clock input goes from 0 to 1.
Another difference from the simple mode is that having both S and R at 1 during a clock signal will toggle the current state. I.e. when it was 0 it will become 1 and vice versa.
Here’s the truth table of the SR latch when the clock input is connected:
|0||0||Rising edge||No change|
|Any||Any||Anything else||No change|
If you had anything to do with logic gates in real life, you should be quite comfortable with that. Otherwise, you’ll have to learn. There is a lot of resources and tutorials on the web that will teach you all there is to know about it. Incidentally, this knowledge may turn quite useful in the real life if you intend to study anything related to computers or electronics. After all, real computers are made of logic gates.
Here’s a little device made out of 4 synchronous SR latches (clock input is connected). Can anyone guess what it is doing when one presses the button? Btw. the disconnected NOT gates serve as sources of a constant “1” signal.
I am waiting for answers in the comments. Those of you familiar with logic circuits should have no problems guessing :-)
One more thing remains to be said. The SR latch is nothing magical. You can build it out of other logic gates, for example from AND and NOT gates. I included it as a separate block because it’s a common thing to use, and making it out of gates is unwieldy. One SR latch made from gates would be several times bigger than the mystery circuit above.
And there is another crucial reason. The state of the latch made out of gates would not be saved when you leave the area or exit the game. In general, internal state of electric circuits is lost when they go out of visibility range. Only the state of the built-in SR latch is persistent.