Building a computer from scratch part III: Memory


“The power of the memory is great, O Lord. It is awe-inspiring in its profound and incalculable complexity… The wide plains of my memory and its innumerable caverns and hollows are full beyond compute of countless things of all kinds.”

Confessions of St. Augustine, Book 10

Part I here.
Part II here.

So far, I’ve learned how to make combinational chips – chips designed to combine two or more inputs in different ways. In this section of the Nand2Tetris course, I focused on sequential chips – chips that control state within the computer. State fluctuates, it can be in one state at a moment in time, and in another state at another moment. The ability to recall this state, and change it at will is called the “memory” of a computer.

Human memory isn’t so much a collection of facts so much as impressions. These we can recall with decreasing accuracy as a function of time. Knowing this, if you could build a machine that could calculate anything and store its results, wouldn’t you want it to store things with perfect recall? Doing so would improve on our shortcomings and enable to do useful things with them. This is what a computer is able to do.

The simplest way to create and store finite values is through electromagnetic charge. We can turn the current on a chip to signify “on,” and turn the current off to signify “off”. Flipping in between these two states, we can create digital gates, called flip flops, that can store its previous state. The naming comes from how these chips behave internally – Nand gates flip in between true and false values thereby storing incoming values and outputting the previous value it received. Similar to how the ALU behaves by combining true/false statements and building complex operations from them, a computer’s memory is based on storing previous outputs and combining them into bits, registers, and eventually memory devices such as RAM, ROM, etc.

A flip flop is able to either “remember” its previous state, then output that, or output the current incoming signal. It can do this through a very simple digital gate design that combines a Multiplexer chip to control which signal gets stored in the flip flop:

A Neural Network design for emulating a 1-bit register
Source: Rick Dzekman

This is a 1-bit register, because it stores one value, 0 or 1 -or Bit. This is the fundamental building block of memory devices. We can easily chain multiple registers together to form registers that can hold inputs of arbitrary bus sizes. This creates a RAM, or Random Access Memory unit. So called because an input is able to access any register in constant time (it takes the same amount of time for the input to be stored in any register on the computer, even if there are 10, 1000, or a billion registers). To sum, we can visualize a unit of RAM memory through the following diagram supplied by the Nand2Tetris book:

The RAM unit has two inputs, a 16-bit “word” you want to store, and the address where you want to store the data in. There is also a “load” input, that controls whether the incoming signal will be stored or not. Finally, “out” will output the current signal it received (if load is false) or the previous state of it (if load is true).

Finally, there is a Counter chip that keeps track of the computations done in a computer. Because chips are located in different parts of a computer, and some calculations take more time than others, signals come to the ALU at different rates; if one signal arrives but another is still pending, the ALU will be outputting gibberish. To prevent this from happening a Counter chip is used to keeps all calculations done in a computer in sync with each other. The passage of time in a computer therefore, is measured in “cycles.” For each cycle, every gate in a computer will perform an action, then nothing more. Only after the counter chip increments time, or moves on to a new cycle, do the gates open up again and allow computations to be made. The cycle then repeats.

This sums the chapter on memory devices. Onwards now to machine language -the place where hardware and software meet.

Thank you for reading through this article. I haven’t stated what’s the purpose of these posts yet, so I’ll clarify now. The primary objective is to write out my learnings and thoughts in as clear way as I can about the subjects I’ve learned in the chapter. By writing and recalling what I learned, I hope to gain a deeper, more solid understanding of the material. The secondary object is simply to share with you all these learnings, and perhaps inspire you to explore on your own the questions you might have about computers in a way that is useful for you.

Published by Alejandro

Engineer, investor, philosopher and lover of the arts.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: