A completely incomplete guide to computer basics - PART2 Hardware: process unit

2020/12/10
code, guide, computer
How come silicon brick learnt to think, what magic is this? Or the question may be is it actually thinking?

The process unit, or just the chip, is some silicon getting hot to touch while electric current passing though. In some sense, the unit is basically a space heater when the computing capability is removed, or a fancy Resistor, which is both funny and sometimes useful.

First let's try to get a brief understanding of what chips do: they get "numbers" from "boxes", "adding" the "number" up, then put the result in maybe a different "box".

The "numbers"

The quoted "numbers" is actually binary, that means 2-based, or represented in only 0 and 1, for example to count in binary is like: 0, 1, 10, 11, 100, 101, 110, .... By the way, each digit also called a bit, so 0 is a 1bit binary number, 100100 is 6bit, and 8bit is a called byte.

The unique quality binary number has is it can easily be represented, in this case, by electricity. We can have one wire and use on/off to represent 1/0, or use high/low voltage for 1/0. Now if we prepare 32 wires, we can represent all 32bit, or 4byte binary numbers.

The "boxes"

How to put a 32bit electric number in a box? use a register, or memory, or storage.

But how can electric be stored in a chip? well, use a flip-flop, like an SR latch. Those circuit is designed to "hold" the electric current, what that mean is, the circuit will keep one of the current path open, depend on two switches, and a smart (or twisted) feedback design.

SetReset+VR1R2R4R30VQ1Q2Press thegreen button
Animation of SR latch

The "adding"

The circled Q1/Q2 components in the image are called Bipolar junction transistor (BJT), which is also three-decades-the-device-of-choice in the design of discrete and integrated circuits, with that said, it is logical, but very basic. BJT allows an extra current injected as input (C) to control the main current (A to B), meaning the on/off of C can control the open/close or increase/decrease the main current from A to B. In other words, it's if (C and A) then B, sound like logic, didn't it?

But it's not calculating yet, with BJT we can get the AND gate (&), and a OR gate (|) or NOT gate (!) is also simple to make, with those in hand, let's design a 2bit + 2bit -> 3bit adder:

     Ia1 Ia0  |    1 0  |    1 1 
 +   Ib1 Ib0  | +  0 1  | +  1 1 
 ------------ | ------- | -------
  O2  O1  O0  |  0 1 1  |  1 1 0 

So for each output, we get the following logic:

const getO0 = (Ia0, Ib0) => { // | * 1
  return Ia0 | Ib0
}

const upI0 = Ia0 & Ib0 // increase to higher bit // & * 1
const getO1 = (upI0, Ia1, Ib1) => { // & * 8, | * 3, ! * 6
  return (
    (upI0 & Ia1 & Ib1) |
    (!upI0 & !Ia1 & Ib1) |
    (!upI0 & Ia1 & !Ib1) |
    (upI0 & !Ia1 & !Ib1)
  )
}

const upI1 = ( // increase to higher bit // & * 8, | * 3, ! * 3
  (upI0 & Ia1 & Ib1) |
  (!upI0 & Ia1 & Ib1) |
  (upI0 & !Ia1 & Ib1) |
  (upI0 & Ia1 & !Ib1)
)
const getO2 = (ipI1, Ia2, Ib2) => { // & * 8, | * 3, ! * 6
  return (
    (ipI1 & Ia2 & Ib2) |
    (!ipI1 & !Ia2 & Ib2) |
    (!ipI1 & Ia2 & !Ib2) |
    (ipI1 & !Ia2 & !Ib2)
  )
}

The total gate count for our very basic 2bit adder will be: 50 = 1 + 1 + 8+3+6 + 8+3+3 + 8+3+6, not a small number for mere 2bits, and let's not count the wires! Modern computer CPU provides 64bit adder, and many other binary operations, luckily a bunch of smart people optimized the adder logic to some extreme, and we now use the more compact MOSFET transistor technology to make chips.

The missing piece

One thing is still missing, as you must be thinking: even with the "number", "boxes", and "adding", the state of the current will not change by itself! The adding operation, along with the input and output bits, will be represented by the current in the circuit, forever.

There's next operation to do, it shouldn't lock up like this, so here comes the "clock" chip to solve the problem. The "clock" chip emits high frequency signals, and it's called an Oscillator. If the "clock" output a single on/off or high/low electric signal per second, that'll be 1tick/sec, or 1Hz, and 1kHz would be 1000tick/sec. With some more circuits, our adder result can "move" to next "box", based on the rhythm of the "clock", and then the next operation can finally happen on the next tick of the "clock". There's also MHz and GHz used for frequency, and modern computer CPU often have variable clock speed around 2~5GHz.

Wrap up

I think this much of explanation should be just (and barely) enough to explain how and why a process unit chip work.

To repeat it simply:

"And that's just a calculator, not the same level as a computer or phone!" you may be thinking.

Well, it's correct, but also not quite: with enough number of these circuits working together, then the output will not just be numbers, you can get picture, music, and anything else, if the hardware and software is connected.

But the actual computing still happen in forms of binary numbers, they represent the input, the output, the type of operation to perform, they're both data and code, the boundary is really blurred at this level.

That's all for this part... wait, I seem to miss all the tech terms for modern CPU, like Register, Cache, Opcode, Cores, Threads, and more! Whelp, there's Wikipedia, dig on, please!

Extended thinking: model the electric current

Most people model the electric current as water, which flows from higher to lower places, and the height difference as voltage.

But I find compressed air a better model for electricity, say we have a can of spray paint, which looks like a big "battery", and it's high pressure inside, we can let some "electron" out, but over time, the pressure, or "voltage" will drop, then finally run out. If we think wire as metal tubes, and there's compressed air inside, then we can understand the energy stored inside more easily. But electron rarely move out of the wire, so we add one restriction to our model: the compressed air won't flow out of the metal tubes, maybe the tube is sealed on both ends.

Extended thinking: model the BJT

With the above compressed air model in mind, the BJT can be thought as a rubber cork with a hole to pass through air.

Below image shows 2 version of BJT, of the 3 input, input C is the control, the above one allows air pass from A to B when "activated", and the other denies it.

Image of BJT model