Basic Bits

How many gates does it take to store one bit of memory? Two if you want to keep it simple, eleven if you want to do it right. Wait, that’s not funny. I guess it wasn’t a joke.

Faulty memory

You can build a basic memory cell with just two nor-gates. The circuit below is called an sr-latch. The s stands for set and the r stands for reset. If you turn set on and reset off, then you set the memory to 1. If you turn reset on and set off, then you reset the memory to 0. If you turn both set and reset off, then the memory will remain what it was: it will remember its last value. Neat!

If you turn both set and reset on, then you’re asking for trouble. What does it even mean to set and reset the memory at once? Nothing, is what it means, so the memory becomes 0. No wait, that’s not why it becomes 0. It doesn’t really matter what it becomes; you shouldn’t set and reset at the same time.

Trading problems

Just because you shouldn’t, doesn’t mean you’re not gonna. See, I know how that goes. As soon as you tell somebody “don’t”, they just have to try. But I know what to do: I’ll make it impossible to turn on both set and reset. All it takes is an inverter.

There, much better. Except, now you’re always setting or resetting; you’re never just remembering. Memory that doesn’t remember? That’s not better, that’s worse! Let’s roll with it anyway.

Enabler

Here’s my fix. (Yeah, because I’m totally making this up as I go along and didn’t look it up or anything.) I’ll add a switch that allows you to say when the memory should remember and when you want to change it. The memory should only be set when the data is 1 and enable is 1, so this calls for an and-gate. Two and-gates, actually.

That works as it’s supposed to; all problems solved.

Feedback loop

So let’s come up with a new problem. Now that I have a bit of memory, I also want to add an instruction. It doesn’t have to be complicated. In fact, I’m going with the simplest instruction I can think of: just flip the bit. That means I’ll need an inverter.

The idea is that I take whatever is in memory, flip it, and then write it back to memory. So, I don’t need a switch for the data anymore; the data is coming from memory.

Oh my, that’s… well, I guess it works. I mean, it does flip the bit and then write it and then flip it and then write it and then flip it and then write it and the flip it and then write it. And then flip it. But it does it way too fast. Actually, the simulation slows it down enough so that you can see it, but in reality, this goes so fast that you can never measure what’s stored in memory. I need to slow this down.

I’ll use a clock for this. Not a grandfather’s pendulum or anything, but an oscillator that pulses on and off at a steady interval. When you buy an oscillator for your circuit, its speed is typically measured in megahertz. That’s millions of pulses a second. That many pulses are useful if you want to build a fast computer, but if my simulated circuit would run at that speed, you wouldn’t be able to see what’s happening, so I’m not going to use megahertz; I’m going to use hertz instead. Two hertz, in fact: two pulses per second. Also, you can’t turn an oscillator on and off; it always pulses. That too isn’t very convenient for a simulation, so I added a play and pause button. Also, if you click the oscillator symbol, you can pulse the clock manually. Again, not something you can do with a real oscillator.

Alright, that’s how the clock works. Now, let’s add it to the circuit.

Is that an improvement? When the clock is 0, the circuit is stable, but when it pulses 1, we have the exact same problem we had before: it’s flipping way too fast. In other words, we still have the exact same problem. Splendid! Let’s carry on.

Edge triggered

The final piece of the puzzle is creating an edge trigger. Instead of sending a 1 into the circuit when the clock is 1, I only want to send a 1 into the circuit at the moment the clock changes from 0 to 1. So, only on the edge of the pulse. That’s why it’s called an edge trigger. Turns out, you can do that by putting a latch in front of your latch. I’m not even going to pretend I came up with that myself. I’m also not going to explain how it works, because I built a simulation of the circuit so that you can figure it out for yourself! Saves me a lot of typing.

This configuration of edge-triggered serial latches with data input has a name: it’s called a flip-flop. This is what a processor uses when it needs memory. Flip-flops are so common that it’s convenient to have a separate symbol for them. Here it is.

I could’ve just shown you that and said: “This is a bit of memory.” Would’ve saved us both a lot of time. Still, now you know how it works. I hope that makes you happy. It makes me happy.