Logic Gate Start with NAND. End with a computer. Solved: %1$d / %2$d Basics Routing & buses Arithmetic Memory CPU Every gate, from NAND alone Selectors, decoders and multi-bit buses Adders, all the way up to an ALU Latches, registers, a program counter and RAM A stored-program computer that runs real code Best %1$d · par %2$d Par %1$d Locked %1$d of 3 stars %1$d · %2$s %1$d NAND %1$s, costs %2$d NAND. Tap for details, or drag it up onto the board. %1$d-bit Add to board Drag here to delete Release to delete Wire Delete Checking… ✓ Passing: %1$d/%2$d ✗ %1$d/%2$d · first failure: %3$s A feedback loop never settles; it is outlined in red. Add a RAM16: the test programs are loaded into it. test got Tick %1$d: %2$s Tap a row to show its values on the wires. Live: tap an input on the board to flip it. Back to live Inputs Outputs %1$s (%2$d-bit) none Show hint Solve it to earn: %1$s Drag a chip up out of the tray to place it, and drag a gate back down onto the tray to delete it. Drag from a pin to wire it up. Drag empty space to pan and pinch to zoom. Bus values show in hex. Start building Set %1$s Done Wires run from an output pin to an input pin. %1$d-bit → %2$d-bit: add a %3$s Circuit verified! You built a computer! Cost %1$d NAND · par %2$d New chips: %1$s Next level Keep editing @string/lg_chip_not_label In this game, you build everything out of NAND gates. A NAND outputs 0 only when both of its inputs are 1. For this level, make the output o the opposite of the input a. Connect a to both inputs of a single NAND. Think about what it outputs when a is 0 and when a is 1. @string/lg_chip_and_label Make o equal to 1 only when a and b are both 1. If either input is 0, o should be 0. NAND means \"NOT AND\": it gives the opposite of AND. Flip a NAND\'s output back with a NOT. @string/lg_chip_or_label Make o equal to 1 when a is 1, when b is 1, or when both are 1. It should be 0 only when both are 0. Flip a and b with a NOT each, then feed both flipped values into a NAND. The NAND outputs 0 only when both flipped values are 1, which means a and b were both 0. @string/lg_chip_nor_label Make o equal to 1 only when a and b are both 0. If either input is 1, o should be 0. This is the opposite of OR, so put a NOT after an OR. @string/lg_chip_xor_label Make o equal to 1 when a and b are different, and 0 when they are the same. You can do this with four NANDs. Feed a and b into the first NAND. Feed the first NAND\'s output and a into a second NAND, and feed the first NAND\'s output and b into a third NAND. Finally, feed the outputs of the second and third NANDs into a fourth NAND. @string/lg_chip_xnor_label Make o equal to 1 when a and b are the same, and 0 when they are different. This is the opposite of XOR, so put a NOT after an XOR. @string/lg_chip_mux_label A MUX chooses between two inputs. When the select input s is 0, the output o should copy a. When s is 1, o should copy b. AND a with NOT s, so it only gets through when s is 0. AND b with s, so it only gets through when s is 1. Then OR the two results together. Built straight from NANDs, it takes only four gates. @string/lg_chip_dmux_label A DMUX sends one input to one of two outputs. When s is 0, the value of in should go to a. When s is 1, it should go to b. The output that is not chosen should stay 0. Output a is in AND NOT s. Output b is in AND s. Decoder Read s1 and s0 as a two-bit number from 0 to 3, with s1 as the high bit. Turn on the output with that number (y0, y1, y2 or y3) and keep the other three at 0. For example, s1 = 1 and s0 = 0 is the number 2, so only y2 should be 1. Each output needs its own AND gate. For y0, AND together NOT s1 and NOT s0. For y3, AND together s1 and s0. Work out y1 and y2 the same way. Nibble swap Thick wires are called buses, and they carry 4 or 8 bits at once. The input x is a byte (8 bits). Split it into its low 4 bits and its high 4 bits, then swap them, so the low half of x becomes the high half of y and the high half of x becomes the low half of y. A NIB SPLIT breaks x into its two halves. Connect them to a NIB JOIN the other way round. Bus MUX Build a MUX that chooses between two whole bytes. When s is 0, o should copy all 8 bits of a, and when s is 1, o should copy all 8 bits of b. This level takes some repetitive wiring, but once you solve it, you get bus versions of NOT, AND, OR, XOR and MUX for free. Split a and b into separate bits. Use eight MUXes, one for each bit position, all sharing the same s. Then join their outputs back into one bus. Half adder Add two bits, a and b. The sum bit s should be 1 when exactly one input is 1. The carry c should be 1 when both inputs are 1, because 1 + 1 = 2, which is written 10 in binary. The sum s is a XOR b, and the carry c is a AND b. To save gates, notice that the first NAND inside your XOR already gives NOT c, so c needs only one more NOT. Full adder Add three bits: a, b and a carry in, cin, which comes from the column to the right. The output s is the sum bit, and cout is the carry that passes to the next column on the left. Use two half adders. The first adds a and b, and the second adds that sum to cin. cout should be 1 if either half adder made a carry, so OR their two carries together. The best known solution uses only nine NANDs. 4-bit adder Add the two 4-bit numbers a and b, plus the carry in cin. The output s is the 4-bit sum. If the total is bigger than 15, it does not fit in 4 bits, so cout should be 1 and s should keep only the low 4 bits. Split a and b into bits and use one full adder per bit, starting from the lowest bit. Connect each adder\'s cout to the next adder\'s cin, then join the four sum bits back together into s. 8-bit adder Add the two bytes a and b, plus the carry in cin. The output s is the 8-bit sum. If the total is bigger than 255, it does not fit in 8 bits, so cout should be 1 and s should keep only the low 8 bits. Use two ADD4s: one for the low 4 bits and one for the high 4 bits. Split the inputs with NIB SPLITs, connect the low adder\'s cout to the high adder\'s cin, and join the two sums with a NIB JOIN. Incrementer Make o equal to a + 1. Because o only has 4 bits, 15 + 1 should wrap back around to 0. The lowest bit always flips, so it only needs a NOT. Each higher bit goes into a half adder together with the carry from the bit below it. Subtractor Make d equal to a − b. If the answer would go below zero, it should wrap around from the top, so 0 − 1 gives 255. Computers subtract by adding. Flipping every bit of b and then adding 1 gives the same result as subtracting b (this trick is called two\'s complement). So add a to NOT b, and supply the extra 1 through the adder\'s carry in. Zero test Make z equal to 1 when every bit of a is 0, which means a is the number zero. If any bit of a is 1, z should be 0. OR all eight bits together, then put a NOT after the result. ALU An ALU (arithmetic logic unit) is the part of a CPU that does the math. The inputs op1 and op0 choose what it does with a and b:\n• 00: add a and b.\n• 01: subtract b from a.\n• 10: AND each pair of bits in a and b.\n• 11: pass b through unchanged.\nThe output o is the result, and z should be 1 when o is 0. Subtracting b is the same as adding NOT b with the carry in set to 1. If you XOR each bit of b with op0, b gets flipped only when op0 is 1, and op0 can also be the carry in. Then use MUXes to choose between the adder\'s result, a AND b, and b. SR latch This circuit remembers one bit by feeding its outputs back into itself. Setting s to 1 sets q to 1, and setting r to 1 resets q to 0. When s and r are both 0, q keeps whatever value it had. The second output, nq, is always the opposite of q. Use two NORs, and wire each one\'s output into one of the other\'s inputs. Connect r to the NOR that makes q, and s to the NOR that makes nq. D latch While en (enable) is 1, q should copy d. When en goes to 0, q should keep the last value it had, no matter what d does. Start with an SR latch. Set it when en is 1 and d is 1, and reset it when en is 1 and d is 0. 1-bit register A DFF stores its input on every tick, whether you want it to or not. Build a 1-bit register that stores d only on ticks when load is 1, and keeps its old bit when load is 0. The output q should always show the stored bit. Put a MUX in front of the DFF. When load is 1, the MUX should pass d through. When load is 0, it should pass the DFF\'s own output back in, so the DFF stores the same bit again. 8-bit register Build the same thing as the 1-bit register, but for a whole byte. On each tick, if load is 1, store the byte on d. If load is 0, keep the byte that is already stored. The output q should always show the stored byte. Put a MUX8b in front of a DFF8, the same way you used a MUX and a DFF for the 1-bit register. Program counter A program counter keeps track of which instruction a CPU runs next. On each tick, it should do one of three things:\n• If load is 1, jump to the number on v.\n• Otherwise, if en is 1, count up by one, wrapping from 15 back to 0.\n• Otherwise, keep the same number.\nThe output pc always shows the current count. Store the count in a DFF4, and use an INC4 to work out the count plus one. Then use two MUX4bs to choose what the DFF4 stores next. The first uses en to choose between the current count and the count plus one. The second uses load to choose between that result and v. RAM Build four bytes of memory, numbered 0 to 3. The inputs a1 and a0 form the address of one byte, with a1 as the high bit, and dout should always show the byte at that address. On each tick, if we (write enable) is 1, the byte on din should be saved at that address. Use one REG8 for each byte. A DEC2 turns the address into four lines, one per register, so AND each line with we to decide which register loads. To read, use MUX8bs to pick the register that matches the address. Instruction decoder The instruction decoder looks at an instruction and decides which parts of the CPU should act. The CPU works on one number at a time, kept in a register called the accumulator.\nThe input op is the instruction number from 0 to 7 (op has 4 bits, but the top one is unused):\n• 0 LDA: load a byte from memory into the accumulator.\n• 1 STA: store the accumulator into memory.\n• 2 ADD: add a byte from memory to the accumulator.\n• 3 SUB: subtract a byte from memory from the accumulator.\n• 4 LDI: load the number written inside the instruction into the accumulator.\n• 5 JMP: jump to a different instruction.\n• 6 JZ: jump only if the accumulator is zero, which is when z is 1.\n• 7 HLT: stop the program.\nSet the outputs like this:\n• aluOp1 and aluOp0 choose the ALU operation: 00 (add) for ADD, 01 (subtract) for SUB, and 11 (pass b through) for LDA and LDI. They are only checked when accLoad is 1.\n• accLoad is 1 for LDA, ADD, SUB and LDI, so the ALU\'s result is stored in the accumulator.\n• memWrite is 1 for STA, so the accumulator is written to memory.\n• useImm is 1 for LDI, so the ALU uses the number inside the instruction instead of a byte from memory.\n• jump is 1 for JMP, and for JZ only when z is 1.\n• halt is 1 for HLT. Split op into bits and ignore the top one. Turn the other three bits into eight separate lines, one for each instruction, like a bigger version of a DEC2. Then each output is an OR of the lines for the instructions that need it. @string/lg_chip_cpu_label Build a CPU that runs one instruction on each tick. Each instruction is a byte: the high 4 bits are the instruction number (the decoder\'s op), and the low 4 bits are the operand. The operand is a memory address, or for LDI, the number to load. On each tick, instr is the instruction stored at address pc, and data is the byte stored at address addr. Your CPU should:\n• Put the operand on addr, so memory sends back the right byte on data.\n• Run the instruction, updating the accumulator, which is shown on acc.\n• Move pc on to the next instruction, or to the operand when it jumps.\n• For STA, put acc on dout and set we to 1, so the accumulator is written to memory.\n• For HLT, set halt to 1 and stop moving pc.\nYour CPU is tested by running three real programs. Split instr into its two halves. Send the instruction number to a decoder, and send the operand to addr and to the v input of a PC4. Keep the accumulator in a REG8 that loads when accLoad is 1. Feed an ALU with the accumulator as a, and as b, either data or the operand, chosen by useImm. Use a ZERO8 on the accumulator to make the decoder\'s z. Finally, connect jump to the PC4\'s load, and connect NOT halt to its en. Computer Connect your CPU to a RAM16 to make a working computer. Three programs will be loaded into the RAM and run: one adds two numbers, one counts down from 5 to 0, and one multiplies 3 × 4. The outputs acc and halt should show your CPU\'s accumulator and halt signal. Connect the CPU\'s pc to the RAM\'s iaddr, addr to daddr, dout to din, and we to we. Then connect the RAM\'s instr output to the CPU\'s instr input, and the RAM\'s dout output to the CPU\'s data input. NAND This is the gate everything else starts from. Its output o is 0 only when a and b are both 1; otherwise it is 1. Every other chip in the game can be built from NANDs alone. HIGH This chip has no inputs. Its output o is always 1. LOW This chip has no inputs. Its output o is always 0. NOT The output o is always the opposite of the input a: when a is 1, o is 0, and when a is 0, o is 1. AND The output o is 1 only when a and b are both 1. If either input is 0, o is 0. OR The output o is 1 when a is 1, when b is 1, or when both are 1. It is 0 only when both inputs are 0. NOR This is an OR followed by a NOT, so the output o is 1 only when a and b are both 0. If you wire two NORs into each other, they can remember a bit. That circuit is called an SR latch. XOR The output o is 1 when a and b are different, and 0 when they are the same. The name stands for \"exclusive OR\". XNOR The output o is 1 when a and b are the same, and 0 when they are different. It is the opposite of XOR. MUX A MUX (short for multiplexer) chooses between two inputs. When the select input s is 0, the output o copies a. When s is 1, o copies b. DMUX A DMUX does the reverse of a MUX: it sends one input to one of two outputs. When s is 0, the value of in goes to a, and when s is 1, it goes to b. The output that is not chosen stays 0. DEC2 A decoder turns a number into a single lit-up output. Read s1 and s0 as a two-bit number from 0 to 3, with s1 as the high bit. The output with that number (y0, y1, y2 or y3) is 1, and the other three are 0. SPLIT 8 A bus is a thick wire that carries several bits at once. This chip splits the 8-bit bus x into its eight separate bits, from b0 (the lowest bit) to b7 (the highest bit). JOIN 8 This chip combines eight separate bits into one 8-bit bus x. b0 becomes the lowest bit and b7 becomes the highest bit. SPLIT 4 This chip splits the 4-bit bus x into its four separate bits, from b0 (the lowest bit) to b3 (the highest bit). JOIN 4 This chip combines four separate bits into one 4-bit bus x. b0 becomes the lowest bit and b3 becomes the highest bit. NIB SPLIT This chip splits a byte (8 bits) into two halves of 4 bits each, called nibbles. lo gets the low 4 bits and hi gets the high 4 bits. NIB JOIN This chip does the reverse of NIB SPLIT. It combines a low nibble lo and a high nibble hi into one byte x. NOT8 This chip flips all eight bits of a at once, so every 1 becomes 0 and every 0 becomes 1. AND8 This chip does an AND on each matching pair of bits in a and b. Each bit of o is 1 only where a and b both have a 1 in that position. OR8 This chip does an OR on each matching pair of bits in a and b. Each bit of o is 1 where a, b or both have a 1 in that position. XOR8 This chip does an XOR on each matching pair of bits in a and b. Each bit of o is 1 where a and b have different bits in that position. MUX8b This is a MUX for whole bytes. When s is 0, o copies all 8 bits of a, and when s is 1, o copies all 8 bits of b. MUX4b This is a MUX for 4-bit buses. When s is 0, o copies all 4 bits of a, and when s is 1, o copies all 4 bits of b. HALF A half adder adds two bits. The sum bit s is 1 when exactly one input is 1. The carry c is 1 when both inputs are 1, because 1 + 1 = 2, which is written 10 in binary. FULL A full adder adds three bits: a, b and a carry in, cin, which comes from the column to its right. The output s is the sum bit, and cout is the carry that passes to the next column on the left. ADD4 This chip adds two 4-bit numbers, a and b, plus a carry in, cin. The output s is the 4-bit sum. If the total is bigger than 15, it does not fit in 4 bits, so cout becomes 1 and s keeps only the low 4 bits. ADD8 This chip adds two bytes, a and b, plus a carry in, cin. The output s is the 8-bit sum. If the total is bigger than 255, it does not fit in 8 bits, so cout becomes 1 and s keeps only the low 8 bits. INC4 This chip counts up by one: the output o is a + 1. Because there are only 4 bits, 15 + 1 wraps back around to 0. SUB8 This chip subtracts: the output d is a − b. If the answer would go below zero, it wraps around from the top, so 0 − 1 gives 255. ZERO8 The output z is 1 only when every bit of a is 0, which means a is the number zero. A CPU uses this to decide whether to jump. ALU8 ALU stands for arithmetic logic unit. It does one of four things to a and b, and op1 and op0 choose which: 00 adds them, 01 subtracts b from a, 10 ANDs each pair of bits, and 11 passes b straight through. The output o is the result, and z is 1 when o is 0. DFF The DFF (D flip-flop) is the basic memory chip. Its output q always shows the bit it has stored. On every tick of the clock, it stores whatever its input d is at that moment, so q always shows d from one tick earlier. Because q only changes on a tick, you can safely loop a DFF\'s output back into its own input. DFF4 This is four DFFs in one chip. On every tick it stores the 4-bit number on d, and q shows the number stored on the tick before. DFF8 This is eight DFFs in one chip. On every tick it stores the byte on d, and q shows the byte stored on the tick before. BIT This is a 1-bit register: a DFF that you can tell when to remember. On each tick, if load is 1, it stores the bit on d. If load is 0, it keeps the bit it already has. q always shows the stored bit. REG8 This is an 8-bit register. On each tick, if load is 1, it stores the byte on d. If load is 0, it keeps the byte it already has. q always shows the stored byte. PC4 The program counter keeps track of which instruction a CPU runs next, and pc shows the current count. On each tick, if load is 1, it jumps to the number on v. Otherwise, if en is 1, it counts up by one, wrapping from 15 back to 0. Otherwise, it stays the same. RAM4 This chip is four bytes of memory, numbered 0 to 3. The inputs a1 and a0 form the address of one byte, with a1 as the high bit, and dout always shows the byte stored there. On each tick, if we (write enable) is 1, the byte on din is saved at that address. RAM16 This chip is sixteen bytes of memory that can be read in two places at once. instr always shows the byte at address iaddr, and dout always shows the byte at address daddr. On each tick, if we (write enable) is 1, the byte on din is saved at address daddr. A CPU uses one address to fetch instructions and the other to read and write data. DECODER The instruction decoder reads the instruction number op and the zero flag z, and works out which parts of the CPU should act on this tick. Its outputs are:\n• aluOp1 and aluOp0: which operation the ALU does (00 adds, 01 subtracts, 11 passes b through).\n• accLoad: 1 when the ALU\'s result should be stored in the accumulator.\n• memWrite: 1 when the accumulator should be written to memory.\n• useImm: 1 when the ALU should use the number inside the instruction instead of a byte from memory.\n• jump: 1 when the program counter should jump to a new address.\n• halt: 1 when the program has finished. CPU This is a complete processor. On each tick it runs the instruction on instr, using data, the byte that memory sends back. Its outputs are:\n• pc: the address of the instruction being run.\n• addr: the memory address this instruction reads from or writes to.\n• dout and we: when we is 1, the byte on dout should be written to memory.\n• acc: the accumulator, which holds the number the CPU is working on.\n• halt: 1 when the program has finished. Computer Sandbox Sandbox Build your own chips from anything you have unlocked, then use them anywhere. No components yet. Tap New component to build your first one. New component Component %1$d My components %1$d in · %2$d out · %3$d NAND %1$d in · %2$d out · contains itself Name Group Drag inputs and outputs onto the board from the Pins tab. Their order from top to bottom is their order on the chip. Pins No pins yet. Delete component Delete %1$s? Any copies of it in your other components and levels will be removed too. Delete pin %1$s Sandbox · tap an input on the board to flip it. Pins IN An input of your component. On the finished chip it becomes an input pin. Tap it on the board to set its value while testing. OUT An output of your component. On the finished chip it becomes an output pin. A component you built in the sandbox, filed under %1$s.