Eli5: what is inside a cpu, and how does it perform thousands of calculations a second?

67 views
0

Eli5: what is inside a cpu, and how does it perform thousands of calculations a second?

In: Technology

Transistors, basically just lots and lots of transistors.
A transistor switches electrical output.

So it can switch from on to off or a 1 to a 0.

These then make up logic gates.
They have multiple in puts and one output.
So if both inputs are 1 the out put 1. You get various types AND, OR, NAND and NOR.

These are put together to give various outputs depending on the input. Like a calculator.
So then you get CPU commands which translates what you want to do say maybe Add 2 numbers, and it then translate this in to 1’s and 0’s for the cpu to work with.

It really basic on the lowest level but as you add more complexity to it can get real confusing petty quick.

Basically it’s magic.

Try millions. If you’re seriously interested you could try watching Ben Eaters series on Youtube. [https://www.youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU](https://www.youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU) Its extremely well thought out and programmed although long.

Inside a CPU there are billions of transistors, generally configured into NAND gates. Transistors are Hyper-Faster-faster-super switches that are a few hundreds atoms wide and can commute billions of times a second.

But the internal clockwork of even a basic 1950 CPU is complicated to understand even to the ones that have experience in computer hardware.

You can try to think of a CPU like a player piano or automatic piano with a crankThe roll of paper contains the code to be executed.When you spin the crank half a revolution, one beat of the score is played, and when you complete a revolution another beat is played (this is the clock of the CPU)Now you start to spin the crank at a fucking 4.2 billions revolutions per second and you are playing a lot of notes all of a sudden.

The piano will get really hot from all that movement and friction so you better be pointing a large fan on it. If you want to spin the crank faster, you should consider buying a large water hoose and drench that piano in water at least while you are spinning the crank.

Two things:

A special kind of math that can be done by a machine. Boolean math.

And

A special hardware that can do this math. This hardware is a very complex series of electrical circuits, and when you send a math problem into this circuit, it finds an answer

I’ll answer the first part.

Although, transistors are the correct answer, it’s probably not the answer to satisfy your curiosity. CPU chip basically consists of glass (~~silicone~~ silicon), that has been pumped with specific impurities in just the right places to give it desirable electrical characteristics. As a result – in some places silicone can conduct electricity, in others not, and in some places only conduct electricity under specific circumstances. Besides that this glass is also etched so that conductive metal like copper can be poured into it which gives the chip an electrical wire structure.

Using these and other methods more complex structures such as transistors and many other necessary electrical components are created in the chip.

Electricity can be tough to ELI5.

Sometimes it’s easier to think of water flowing through pipes. Imagine a water pipe hanging horizontally, like on a wall or from the ceiling. This pipe has dozens of pipes sticking out the bottom of it, which can be opened or closed by valves. These flow down into other pipes, and they can crisscross and flow into little pools or tanks, or larger pipes or smaller pipes, all the way down to the floor. It looks like a mess, right? But you know that water flows from the top pipe into the other pipes, and by turning certain valves, you can get the water to flow to different places. That much should make sense, even if you can’t follow the flow through the entangled mess.

If you open/close certain combinations of values, water can fill certain tanks or pools along the way. Some of these could be attached to a water wheel or some other object that reacts to water flowing. This is how electric circuits work: electrons “flow” through conductive wire, like copper, and certain special materials can sense and control this flow, maybe able to turn on a light.

In a CPU, this is happening billions of times over minutes or seconds with tiny “electrical valves” called transistors. You don’t need to understand how a transistor works, but it’s like a water valve, like a faucet at your sink, for electricity. On a computer chip, you have power from the wall, which comes from a power plant. This is the top pipe in the water pipe picture above. Without the “power,” the CPU has no electricity to calculate with or measure. Computer software performs calculations. It translates instructions from code or from your interactions with the computer into very basic building blocks. Essentially, it turns language into a series of “yes” or “no” questions for the computer. To ask the CPU these “yes” or “no” questions, it turns a series of these valves open or closed, and “water” (electricity) begins to flow. Once it flows, the “water” flowing through those “pipes” will have flowed to a particular pipe or a particular tank, and the computer software reads it, then translates it back to your monitor so you know what happened. Of course to create the level of complexity we see with a full modern computer this machinery would have to be extraordinarily complex, and it is. You need billions of these “water pipes and valves” to represent the information you expect the computer to do, like addition and subtraction, and displaying letters and numbers and colors on the monitor. It has to do all of that by electricity flowing through little circuits and measuring where that electricity goes, and sometimes how much of it goes there.

IMO the most interesting about a cpu is how it actually reads and executed instructions. It’s called the fetch, decode, execute cycle. It’s almost like the cpu is reading a book. Each line tells it an operation to perform (i.e. take this number and add 5 to it), then it goes to the next line and repeats. Some instructions might also say something like “if the thing in memory spot 1 is equal to 0, skip the next instruction line”.

The internal structure of modern CPUs is ridiculously complex and does so many really cool things, but at its core, it has hardware that tells it how to perform this cycle, hardware to do super basic math, hardware to remember things between instructions, and a special place to remember what line it should execute next.

Transistors are a 3 terminal semiconductor device in which the current in the third terminal can be controlled by the voltage across the other 2. CPUs uses this property in a creative way with logic gates. A logic gate is an arrangement of transistors in which (depending on the function of the gate) will output either a high (5V) or a low (0V), this is the 1s and 0s people think with digital computers. Logic gates can be arranged together to perform binary operations like addition or subtraction as some basic ones. These logic gates are what are arranged in the cpu, and they are incredibly small nowadays. Computers followed Moore’s law, which states that every two years the number of transistors in an integrated circuit doubles. This is how computers have gotten so much more advanced since their early days.

As far as the billions of computations per second, digital circuits have a ‘clock’ signal that controls when operations occur inside the logic gate circuits (usually on the rising or falling edge). This can be VERY fast. The main reason that the transistor is one of the biggest inventions in human history, is because before transistors any switching had to be done mechanically by physically breaking the circuit. Since they could now switch things electronically with changing voltages around the transistor, the speed of switching was increased by several orders of magnitude.

Before we had CPUs, we had devices we called “electromechanical”. Ever seen an old jukebox, the kind that has records inside? Those are very complicated machines but don’t have a computer inside of them! Even before we had electricity, we had devices that did computer-like work. For example, in the 1800s Ada Lovelace and Charles Babbage worked on a fancy calculator. Today’s CPUs are just REALLY tiny versions of the work that started there.

So how it works gets sort of complex, but it all comes back to switches. Just like light switches. Turn the switch on, and electricity flows through it. Turn the switch off, and electricity doesn’t flow through it.

Non-electric machines didn’t use electrcity, they used gears and shafts that moved when you flipped a switch. Hundreds of switches were carefully connected so if you flipped one switch, it interacted with all the others in predictable ways. Electromechanic machines (like the jukebox) are just like that. When you push buttons, electricity makes motors move that turn gears. Those gears flip switches, and that makes other motors turn, and so on. It’s very complicated, but fun to watch! CPUs and other kinds of computer circult use transistors, which are ultimately microscopic switches.

Over decades, we played with this concept of switches and found a lot of neat ways to arrange them. We gave them silly names like “latches” or “flip-flops”. We figured out we can use those arrangements to make more useful things, like an “Arithmetic Logic Unit” or ALU. For that to work, we have to say, “flipping the switches this way is how we represent numbers”, and “flipping the switches that way is how we tell it whether to add or subtract”. There are some switches called “outputs” that will turn on or off after we set number switches and whether to add or subtract. But the point of an ALU is you give it two numbers and it will do math and give you the result as a number. Neat!

If we wire up a lot of light switches and light bulbs the same way as those “circuits”, we can operate a very slow “computer” by turning switches on and off in patterns. That’s what’s happening inside your CPU at a very small scale. There are literally billions of tiny switches inside of it, carefully arranged to do exactly the right thing if you flip them in the right order. The CPU has “input” switches that tell it what it should do next, and the result shows up in the “output” switches the rest of the computer is connected to. There are millions of steps to go from “here is a picture file” to “the picture is on my monitor”, so forgive me if I don’t walk you through an example!

How’s a CPU go so fast? Well, that’s a little hard to answer but I’ll try.

One of the switches the CPU listens to is attached to a crystal. Some crystals vibrate when we charge them with electricity. The neat thing is they always vibrate at exactly the same speed. So we figured out how to make a device we call a “clock”. It has a crystal, the crystal gets electricity, and there’s some fancy wiring that turns the switch on and off every time the crystal vibrates. If the switch is off, the CPU isn’t “listening” to its other switches. That’s when all the things inside the computer set up all the switches just how they want. When the clock turns on, The CPU lets electricity in through switches, and the patterns of the ones that turn on make it do things. The end result is a bunch of other switches on the other side turn on or off. That’s the “output”.

Clocks are very fast. Most computers today “tick” more than 2 billion times per second. The only reason they can’t go faster has to do with some weird science. All that electricity makes heat, and it takes more electricity to stop the CPU from melting itself. At some point, the CPU makes so much heat we couldn’t possibly cool it. We used to be able to deal with this by making the little wires connecting all the switches smaller. But they are so small now, even weirder science happens. Think about a wire like a “lane” that electricity goes down. If the lane gets too small, it’s hard for the electricity to stay in its lane. If we make the lanes much smaller than we make them right now, that happens a lot. Since everything depends on the switches being wired together perfectly, having electricty go in the wrong lane breaks the entire system. So we’re currently a little stuck in terms of making them very much faster. We’re working on it, though. We usually find a way.

[https://youtu.be/EKWGGDXe5MA](https://youtu.be/EKWGGDXe5MA)

Richard Feynman can help you out if you got some time.