Building a computer from scratch

“People who are really serious about software should make their own hardware.”

Alan Kay

I grew up learning about software like what imagine many developers do nowadays: doing tutorials online and building cool apps in my spare time. I learned about algorithms and data structures and got to learn a LOT more in my job by seeing “how the sausage is made.”

While browsing though online forums, I came across a video of Alan Kay, the creator of the GUI interface and object-oriented programming – ubiquitous inventions that in their absence would make the current world unrecognizable. In this video he mentioned something that blew me away. To paraphrase:

Most ideas don’t scale well, they merely provide incremental change. What’s needed is a change in perspective, an opening up of the world, to look at something in totally new ways, that can provide order of magnitude improvements.

In my day-to-day job, I look at incremental improvements in my program by using shorthand notation, eliminating redundant dependencies, and using design patterns to solve common problems. But if I want to really make a change, if I want to become a really good programmer, I had to change my perspective at a fundamental level. More fundamental than software. More fundamental than the operating system. I needed to understand how a computer works.

Isn’t it amazing? As a software engineer, I have no idea how a computer works. I can say that I know how it works, such as “instructions are sent to the CPU which calculates stuff, and those results are in binary which get translated to assembly which get translated into code by a compiler.” But I don’t really know how that works or even what I am saying when I state these things.

That is why I decided to start the course, Nand2Tetris. It’s a freely given course with an accompanying textbook that teaches you, step by step, on how to make a computer from scratch.

The first lesson of this course focused on Boolean algebra. It’s essentially a branch of mathematics that deals with truthy statements. This is what computers boil down to. Statement are either true or false, 1 or 0. Combined together, they can form complex operations which, almost miraculously, give rise to the computer itself.

There are many different kinds of such statements, or Boolean functions. These include:

  • And
  • Or
  • Not
  • Nand
  • Xor
  • Mux
  • DMux

Each of these can be implemented through physical chips, called digital gates. These gates implement a a boolean function, which provide different outputs depending on their inputs. These input/output combinations are described by Truth Table, as shown below:

AND Gate | Digital Logic Gates | Electronics Tutorial
Credit: electronics-tutorial.net

The first week of Nand2Tetris involved creating the digital gates described above through a single, primitive gate, the Nand gate. Based on the truth table representation of the chip, and its API, I had to create the chip based only on Nand gates and other gates which I previously made. Thus, brick by brick, a house is starting to be built. Some of the gates were straightforward to implement. For example, a Nand gate is simply an And gate connected to a Not gate:

logic nand gate
Credit: electronics-tutorial.net

Another gate, the Xor gate was not so trivial. To come up with it, we can look at the desired truth table we want to achieve with the gate:

Credit: electronics-tutorial.net

Then, we can create a boolean function that could represent the inputs and output, as long as the output is true. One such function is the following:

Thus by creating a statement that fulfills the truth table’s conditions, and simplifying it to its canonical representation, we can create a digital gate diagram that corresponds to that.

These digital gates don’t necessarily have to take a single number input, they can take a group of them, called a “bus.” The course specifies creating a “16-bit” computer, meaning that it is able to compute binary numbers up to 16 bits in length at a time.

This was an excellent exercise in flexing my logic muscles and brushing up on my Math skills. The next week will be a big step forward: building an ALU and Memory that will be able to both compute and store the processes that my computer will perform.

One thought on “Building a computer from scratch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s