Designing a brainf**k computer
Tags: tutorial, hardware, esoteric, brainfuck
Published: 2021-05-22 17:56:50

Even though modern computers are fast, minimized and optimized machines it's important to understand at least the basic principles of their design. Because the best way to learn is by experimenting I present you a series of articles showing how to design, simulate and (hopefully) build a very simple computing machine and, because it's me you're reading, let's mix some esolangs into it.

*Note: This tutorials aim to build a brainfuck operated computer which allows for/requires some unusual solutions. For a more in-depth tutorial on a more standard computer design see Ben Eater's [8-bit breadboard computer](https://youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU) on youtube.*

### Two parts of a computer
###### CPU and the rest
Most of the modern computer's weight consists of parts that are not at all necessary for the computer's operation: a GPU, other extension cards, a lot of sockets (each with it's own circuit to interpret incoming signals), and if you have a laptop then it even has built in keyboard, screen and touchpad. All these devices are important for the user in some way, but a machine doesn't require them to still be called a computer.

The "brain" of each computer is it's Central Processing Unit (CPU). It executes commands stored in memory performing arithmetic and logic operations and exchanges data with other components to run the program it was meant to, which is the definition of what a computer should do. Being the most (and almost only) important part of the computer this tutorials will mostly focus on CPU design. I will call my CPU a Brainfuck Processing Unit (BPU).

Outside of the CPU there isn't much else required for a machine to be called a computer. It requires memory to read its program from and brainfuck includes input and output commands so they will appear in the design but they won't be the main focus.

### Many parts of a CPU
###### And just a simple one

##### 0. Clock
Not actually a part of the CPU but it also doesn't fit with the external devices. A computer clock is a circuit that periodically switches between emitting high and low voltage, letting all computer's components stay in sync. Almost every action in the CPU is triggered by the clock's transition from low to high (rising edge) or from high to low (falling edge).

##### 1. Buses
A bus is a line of parallel wires used to transfer data between parts of the CPU. Usually many components have connections to each bus but only one outputs data to it at any given time. A simple CPU can be built with only one bus or without buses at all, but for efficiency I will use two: one for transmitting data and one for addressing the memory.

##### 2. Registers
Register is a simple memory cell able to store a single value. They are used to carry intermediate values for arithmetic operations and to remember a value between different command executions. The most common example is a Program Register (a.k.a. Program Counter) which stores the address of the next command to execute.

##### 3. Arithmetic and Logic Unit
ALU is the part of the processor responsible for every simple arithmetic and logic operation the CPU is supposed to do. In BPU these include only addition and subtraction, but more complex ALUs are able to also perform multiplication, division and all sorts of bitwise logic functions. It is usually paired with an accumulator register to store the result between commands to allow for more complex mathematical operations.

##### 4. Control Unit
Every part of a CPU is operated using a couple of simple signals, for example telling a register to load contents of the bus or the ALU what operation it should perform. The Control Unit (CU) takes the current command as input and outputs the correct control codes for all other CPU components. Most commands take more than one clock cycle to finish so the CU should also have a counter to know which cycle it is on.

##### 5. More complex stuff
Modern processors have a lot more component types like caches, dedicated Address Generation Units or Memory Management Units. I will not touch these advanced features as their implementation would complicate the design and I don't need them for my BPU anyway. If I aimed for efficiency I wouldn't have started designing my own computer in the first place.

### Tools and prerequisites
###### What to have and what to know
To follow this tutorial it's best to have a way to make all the parts along with it, either physically or in a simulation. I will be using a free piece of software [Logisim](http://www.cburch.com/logisim/) to design and simulate the BPU.

As for the knowledge I will explain the underlying construction of Logisim building blocks (memory, adders, registers) only if you ask and I will expect the reader to know the basic logic gates. Apart from that I will try to explain most stuff as it comes.

### Wrap-up
###### Cya!
I've shown what parts are necessary for the computer to work and why. I already have [a language for the BPU](https://dev.to/olus2000/bal-1-designed-for-hardware-me7) and an overview of my building blocks. Tune in next week when I'll design first components in Logisim!