A computer is a machine that carries out a sequence of instructions.
Most people think of computers as machines that only do information processing, such as Laptops and Servers, but there is also a category called "Embedded Computers" or Embedded Systems. In Embedded Systems, the computer is used for a very specific purpose, which is typically to control the device of which it is a part.
Embedded Systems span a wide range, from small medical devices, like hearing aids, blood pressure sensors, and pace makers, to household appliances, such as washers & dryers, refrigerators, and coffee makers, to automotive, industrial and aerospace applications.
Components of a computer
There are 6 types of components in a computer system:
- CPU - The Brain.
- Clock generator. It is used to supply the clock to the CPU, or to "clock" it.
- Program Memory. It contains the instructions to be executed sequentially by the CPU.
- Output Unit. Sends data out. In a simple case, it is binary, so it can switch a light, LED or motor on/off.
- Input Unit. Reads data from the outside. In a simple case, this is just a switch or push button.
- RAM. Read/write memory used to hold data. It is typically volatile, so its content is lost when the computer is powered down.
The first three components - CPU, Clock, and Program memory - are absolutely essential for a computer to function. With these components, the Clock will prompt the Brain to keep fetching instructions from the Memory and to keep executing them sequentially. This is all it takes! Unfortunately, such a computer has no way to communicate. The results of its executed instructions cannot be seen by anyone and thus whatever it does is irrelevant to the outside world. In other words: it is a computer, but it is completely useless.
By adding an Output Unit, we start to see a computer with some limited uses. With just a single bit output (On/Off) that controls a buzzer, it can be used as an egg timer, that beeps after a few minutes. With multiple outputs, we could add a display and it can now be used as a clock that displays the amount of time since powering up.
Adding an Input Unit increases the scope of usability. A simple addition is a button. In the above example the button resets the clock or adjusts time. Any number of inputs can be added. They can also be analog and this way (via an ADC, Analog / Digital Converter) measure things such as temperature. With a simple system like that, 2 digital Inputs (pushbuttons and limit switches) and 2 digital outputs (controlling a motor), a system such as a shutter control can be build.
As the complexity of the task increases, the short term memory in the CPU (the CPU registers) will no longer be sufficient to get the job done. Adding the sixth type of component, RAM, greatly increases the type of work the CPU can do. Without RAM, it runs out of memory after very few additional computations. This completes the build-out of a fully usable computer system.
How does a computer work?
Today's computers function basically the same way as yesterday's and as did the first computers.
It executes instructions sequentially. It executes the first instruction, then the second one, the third one, and so on. The only exception (for now, putting things like interrupts aside) is when the instruction is a jump instruction, which changes the program counter and so forces the CPU to continue program execution in a different spot.
In other words, the CPU does the following:
- CPU sets an internal register (the PC, Program Counter) to the address of the first instruction
- CPU loads (fetches) the instruction pointed to by the PC
- CPU increases the PC to point to the next instruction
- CPU executes the instruction
It then simply repeats steps 2,3,4. And that's it! It really is that simple!
The differences between modern CPUs and older ones are seen in various places:
- New CPUs have more and wider registers.
- (Most) Modern CPUs have more powerful instructions, such as Multiply / Divide, which were not present in old 8-bit CPUs
- Modern CPUs need fewer Clock cycles per instruction. They can execute most instructions in a single cycle, where as old designs such as the original 8051 needed 12! cycles for even a basic single byte instruction.
- Modern CPUs run at much higher clock speeds. The original 6502 could run at just 1MHz, where as modern CPUs run at multiple GHz.
- Modern CPUs often contain multiple Cores, which can execute multiple programs simultaneously.
- Modern (high-end) CPUs typically come with a Cache, as the memory they connect to cannot keep up with the high clock rate. In order to avoid wait state (CPU waits for memory), a cache is used.
- Virtual memory (MMU) has been added to isolate processes and avoid computer crashes due to application program crashes.
But again, if you understand a good old 6502, you understand a modern multi-processor x86, ARM or RISC-V system.
Now there are follow up questions, such as:
What is the address of the first instruction that the PC is set to in the first step?
The answer is simple: It depends on the CPU. For a lot of CPUs, it is simply a fixed address, such as 0. This is so for many CPUs, such as the ARM-7, ARM-9 and ARM-11 CPU cores. They simply start, fetch and execute the first instruction from Address 0, and from there on keep fetching and executing instructions sequentially. For a classic ARM core, the size of the first instruction is 4 bytes, so it fetches the next instruction from address 4. It then fetches and executes the next instruction from address 8, and so on. This keeps going until the CPU executes a jump instruction, one that changes the PC. There are multiple types of jump instructions, such as conditional and unconditional, call instructions (which preserve the orignal PC, so they can easily jump back after completion of the sub routine call). Other processors have a Vector Table, so they load the initial PC, i.e. the address of the first instruction, from a Vector Table, where one entry is the Reset Vector. An example of this type of CPU is the 6502, which (in numerous variations) was used in many popular "home" computers in the early 80s, such as the Apple II and the Commodore VC20 and VC64.
A typical CPU has around 100 different instructions. Old designs tend to have less instructions, newer ones more. There are different types of instructions.
- Arithmetic & Logical instructions
- Jump instructions
- Load from, and Store to, memory instructions
Note that some CPUs (including the 8086 and its successors, usually referred to as x86 Processors) also have complex instructions mixing Loading, Arithmecs/Logical, and Storing, such as in the example below, in which 1 is added to the memory location. And yes, there is even a shorter version of the code, an INC instruction available in the x86 instruction set.
CPU registers are the short term memory of a CPU. This is what the CPU can "remember" without external RAM memory.
Different CPUs have different registers. Again, modern CPUs tend to have more and wider registers than older ones, but the basics are pretty much the same.
- PC (Program counter). Minimum 16 bits, even on 8-bit CPUs. In some cases called IP for instruction pointer.
- SP (Stack Pointer). In some older 8-bit CPUs such as 8051 or 6502 just 8-bits, in modern 32-bit CPUs 32 bits, in 64-bit CPUs typically 64 bit wide.
- Flag register
- Accumulator and/or other registers.
In the case of the 6502, these are just three 8-bit registers called A, X, Y. In the case of 32-bit ARM architectures, these are basically 16 32-bit registers named R0..R15 (where the top ones R13, R14 and R15 are also used for SP, PC and LR (link register).