Components
Registers
We can combine registers to reduce the amount of wires needed.
Using a data bus (wiring at the top) and a binary decoder, we can select which register to read/write to.
Memory
Assembly
Assembly is a human-friendly representation of code: binary values that a computer can understand.
An assembler converts ASM instructions into machine code, which is given to the CPU as input.
Arithmetic Operations
For example, a simple computer architecture could use 00
to represent arithmetic operations.
To decide which type of operation to execute (subtraction, multiplication, addition, etc), the 3rd and 4th bits could be used.
Using a index, we can build an inefficient, but simple circuit to do this.
This type of circuit is an Arithmetic Logic Unit (ALU).
Memory Operations
Of course, assembly can also provide instructions to store or load values.
Load
LOAD R2 1000 ;Load into register 2 the value in the memory address 1000
Store
STORE R1 0110 ;Store the value in register 1 into the 0110 memory address
Select Which Instruction to Execute (first 2 bits)
To decide which operation to execute, a binary decoder can be used.
For memory operations, the 3rd and 4th bits are used to select which register to use.
The last 4 bits represent the memory address to read/write to.
Instruction Register
For the instruction to be given, it is stored in a special register: An Instruction Register.
Optimization
We can use a single Binary Decoder instead of two, to achieve the same result. (Optimization on the right pane)
Different architectures can have the exact same functionality, while being implemented differently, or even having different instructions. This is why code that is compiled for Intel x64 is not compatible with ARM or RISC-V.
Control Unit
We can finally add the ALU (Arithmetic Logic Unit) we built before into the new circuit, like so:
The gray trapezoids are multiplexers:
The output value is then stored in a temporary register, before replacing the first operand register’s value.
The component we just built to control the ALU
is part of a Control Unit
. The full control unit
is very complex, as it needs to handle every possible instruction. (So far, we have seen how to implement the ALU
and RAM
.)
Each register in the CU
has a specific purpose, unlike RAM
, which can be used to store any values.
To read the first instruction, the CU
will fetch data from the first address in memory.
After fetching, the CU
will decode the instruction: interpret the bit sequence in the instruction register
, to send the necessary signals to the components that will execute the instruction. We can finally load instructions into the instruction register.
Then, the CU
increments 1 byte, to point the address register
to the next instruction. (Modern architectures increment different values, as the instruction set is more complex)
If the instruction is an arithmetic operation, the steps are similar. The ALU stores the output in a temporary register, which overwrites the register 0 with the result. The result can then be stored in RAM
.
Load a Program Into Memory
So far, we can store instructions in memory, but it is also necessary to store values, besides from the instructions themselves.
For example:
This program uses 2 numeric values. The first 2 instructions load these values into the registers, and then, these values are added together and stored in another memory address. The final instruction, HALT
, marks the end of the program, to make sure the CU
does not attempt to read the number 20 as an instruction.
If the program is extended, all memory addresses must be altered. To fix this issue, we can instead store values at the end of the memory stack.
Conditions and Loops
To create a loop, we can simply jump to a smaller address in memory.
Internally, the JMP
command overwrites the Address Register, making it so that the next CPU cycle fetches the chosen memory address, instead of the next one.
Flags
Sometimes, we might want to loop only if a certain condition is met.
For context, imagine subtracting a number from itself. In this case, the ALU will provide some extra information, using 1 bit registers called flags
.
N | Flag | Description |
---|---|---|
0 | Overflow | When a number is too large to fit in the output register. |
1 | Zero | When the result is zero. |
0 | Negative | When a number is negative. |
This additional information can be used to make decisions, and make conditional jumps possible.
ASM | Command | Description |
---|---|---|
JMP_OFW XXXX | Jump Overflow | Overwrites the Address Register with the value XXXX if the O_FLAG is ON. If the flag is OFF, the Address Register ’s value is incremented by 1. |
JMP_ZRO XXXX | Jump Zero | Overwrites the Address Register with the value XXXX if the Z_FLAG is ON. If the flag is OFF, the Address Register ’s value is incremented by 1. |
JMP_NEG XXXX | Jump Negative | Overwrites the Address Register with the value XXXX if the N_FLAG is ON. If the flag is OFF, the Address Register ’s value is incremented by 1. |
JMP_ABV XXXX | Jump Above | Overwrites the Address Register with the value XXXX if neither the Z_FLAG nor N_FLAG are ON. If either is ON, the Address Register ’s value is incremented by 1. |
Comparing two numbers is the same as subtracting them.
$$a - 5 = b$$
b is negative | b is zero | b is positive |
---|---|---|
then | then | then |
a < 5 | a == 5 | a > 5 |
For example:
An IF
statement works in the exact same way, but without the need to loop:
Note: These instructions are not from any real architecture. These are examples for this simple, custom architecture.
Clock
The final piece of the puzzle is the clock. A clock can give us time before a circuit loops.
This is necessary, because energy travels extremely quickly, and so, all memory would be reset before we could even use the stored values. Each clock tick corresponds to an action the CU
performs (fetch, decode and execute).
A Data FLIP-FLOP
, for example, uses the clock to store data, acting as the manual RESET
input.
This circuit can be used to build a single bit register.
To generate a clock pulse, we can use a circuit similar to this one: