Transcript for:
Redstone Circuits and Computer Logic in Minecraft

It all starts with a Redstone line Think of it as a cable. It can be turned on and off to produce an output. If the lamp is turned on, then the output is one. But if it's turned off, then the output is zero. This next circuit is a little more complex. If both pistons are off the lamp is off, so we get an output of zero. The same thing happens if only one of them is on. But if both are on, then we get an output of one. This circuit is called an AND gate and is described by this truth table. The output is one or in other words true, only if the blue input and the green input are true. Now let's zoom in on the pistons. They act as transistors. Simply put, transistors control the flow of electricity. When we apply voltage to the transistor, it lets the current flow through. Just like the piston in the AND gate, example. Simple. If we add a block and a Redstone torch in front of our output, the signal will be inverted. This part alone is called an NOT gate. And together with an AND gate they form and NOT AND gate or NAND for short. This is a very significant component because any logic function can be implemented using only NAND gates. We can actually make these Redstone circuits much more compact like this. This will be very useful soon when we start making more complex components like this one for example. It is called a half adder and it uses five NAND gates to add two binary digits together. Computers count different from humans. We use the decimal system, which uses ten digits 0 to 9, and each digit is multiplied by a power of ten. Computers don't do that. They use binary. Binary has only two digits, zero and one, which are multiplied by powers of two. So the number 23 and binary would look like this. 10111. Now, if you want to add two binary numbers together like one plus one, we have to carry the one to the next spot. This gives us the result two, which is one zero in binary. This is exactly what the half adder does. It adds two bits together, but counting up to two is not really that useful. That's where the full adder comes in. It uses nine and NAND gates, and unlike the half adder, it has three inputs. The third input is called the carry in and it can be used to add the carry from the previous operation. So now we can put four adders in a row and connect their inputs and outputs to make a four bit adder. To check if it really works, I've built this binary display. If we try to add, for example, five and four, we should get a nine. And indeed this is the result. Two to the power of three plus two to the power of zero equals nine. But while binary is easy for computers to read, it is the opposite for humans. If you don't want to convert ones and zeros to decimal numbers in your head, you can build a seven segment display instead. It is a very basic component that has many real world applications such as digital clocks, electronic meters and calculators. Such a display is divided into seven parts labeled A through G, which can be lit in different combinations to represent each of the Arabic numerals. After quickly building the decoder. we get this: a display that converts binary input to decimal digits. After entering five plus four, we still get a nine, but this time it is displayed in a more readable way. Skipping through a few pages of negative numbers theory, we eventually end up with this a very simple arithmetic logic unit. This component can process data by performing addition and subtraction. If we try to perform the operation two plus three, we get a five as expected. But after turning on the subtraction mode and waiting for a bit for the machine to process the data, we get minus one. This extra functionality is really useful, but it is still far from a full computer. By definition, a machine can be classified as a computer if it processes, stores, and retrieves data. We can already do a third of that. Storing and retrieving data in a real computer is a bit complicated. To store a single bit, we would have to build a latch with a few gates. It is possible to translate this design to Minecraft, but there is a much simpler way to do this. We can actually block repeaters to store their state. After stacking a couple of these on top of each other, we've just made a register. We can store numbers on it, which we can later fetch and process with the ALU. If we build enough registers, we can eventually start storing programs on them. Programs are essentially rows of instructions and operands. For example, this instruction tells the computer to write the number four into register two. But there is a catch. The entire instruction must be encoded in binary for the computer to understand it. This means that if you want to code a simple program, you would have to manually set the correct bits in the registers, which is a very tedious and time consuming process. Normally, computers avoid this by using an assembler. An assembler takes an assembly code, which is easier for humans to understand and compiles it into machine code. But let's get back to the computer. Now we need to build two separate storages: an instruction memory to store the programs, and a data memory. This approach is called the Harvard architecture, and its alternative is the von Neumann architecture, which stores instructions and data in the same place. After adding the memory, all that is missing is a control unit which will fetch, decode and execute the instructions. What we've got here is a very simple central processing unit or CPU for short. As you can probably tell by now, building a processor from scratch is very time consuming. That's why I took this design from mattbatwings. He has a great video explaining the structure and function of this CPU. I highly recommend checking it out. Now if we connect the display inputs and code some instructions, we can run a simple program like this Fibonacci counter where each next number is the sum of the previous two. After that, all that's left to do is to add pipelining, cache hierarchy, vector parallelism, branch predictions, Isa extensions, clock gating, beep beep. And before you know it, we get Minecraft in Minecraft.