CS with Seongyeol

Where Do You Want to Go, Data?

In the previous post, we learned that a single NAND gate can build every logic circuit. AND, OR, and NOT were all just combinations of NAND.

Every circuit we’ve built so far processes inputs in the same fixed way. But a real computer must behave differently depending on the situation. When adding, it sends data to the addition circuit; when subtracting, to the subtraction circuit. Sometimes it needs to pick one of four storage locations to read. What kind of circuit makes this selection possible?

Multiplexer

Think of the channel buttons on a remote control. They pick one broadcast out of many and show it on screen. A multiplexer (MUX) is exactly that remote. It’s a circuit that selects one of several inputs and passes it to the output.

The simplest MUX picks one of two inputs: the 2-to-1 MUX. When the select signal S is 0 it outputs A; when S is 1 it outputs B.

Out
0000
0010
0101
0111
1000
1011
1100
1111
  1. Look only at the rows where S is 0. Out always matches A.
  2. Look only at the rows where S is 1. Out always matches B.

So when S is 0 it passes A through; when S is 1 it passes B through. In logic notation: (NOT S AND A) OR (S AND B). We can build this from the gates we learned before.

  1. Set S to 0 and toggle A. A’s value appears directly at Out, while changing B has no effect.
  2. Switch S to 1. Now B’s value appears at Out.
  3. S acts as the select signal, deciding which input to choose.

What if there are four inputs? Just widen the select signal to 2 bits (S1, S0). A 4-to-1 MUX picks the first input for 00, the second for 01, the third for 10, and the fourth for 11. It can be built by combining three 2-to-1 MUXes: S1 drives two MUXes that each narrow four candidates down to two, and S0 drives a third MUX that makes the final pick.

When you need to pick one of four data sources to read, a 4-to-1 MUX does the job. The 2-bit select signal corresponds to the source number.

Decoder

Now we go in the opposite direction. A circuit that takes a single input and activates exactly one of many outputs is a decoder.

Think of elevator buttons. Press the button for floor 3 and only the floor 3 indicator lights up. One input, but only the corresponding output is activated.

The simplest 2-to-4 decoder takes a 2-bit input and sets exactly one of four outputs to 1.

O0O1O2O3
001000
010100
100010
110001
  1. In each row, verify that exactly one of the outputs (O0–O3) is 1.
  2. When AB is 00 O0 is on, 01 turns on O1, 10 turns on O2, 11 turns on O3. Each output is a combination of AND and NOT. For example, O0 = (NOT A) AND (NOT B) and O3 = A AND B.
  1. Set both A and B to 0 — only O0 is 1. The rest are all 0.
  2. Switch B to 1. O0 turns off and O1 turns on.
  3. Try all four combinations by toggling A as well. Exactly one output is active at a time.

Decoders will reappear as a key component inside the computer in later posts.

Application: Selecting from Multiple Stores

What happens when we combine a MUX and a decoder?

Suppose four storage locations each hold a different value. You want to pick one to read, or pick one to write a new value to.

  • Read: A MUX takes an address and outputs the value of that store.
  • Write: A decoder takes an address and activates only the target store.
  1. D0–D3 represent four stored values. S0 and S1 are the address bits that feed into both the MUX and the decoder simultaneously.
  2. Toggle S0 and S1. The MUX outputs the selected value to Read, while the decoder activates only the corresponding W output.

Wrap-up

MUX selects one of many inputs to output; a decoder activates one of many outputs from a single input. These two circuits point in opposite directions, yet both solve the same problem: selection.

In the next post, let’s combine gates to build a circuit that actually performs addition.