Build a Modern Computer from First Principles From Nand to Tetris, I will keep updating this series of blogs, as a record of my learning process, but also to force myself to speak out what I have learned, to further understand.

In fact, all the electronic devices in our life are based on logic gate circuits (explained later). This is an amazing thing. When I was in middle school, I was curious about how computers could perform these complicated operations, and I’m sure many people are also curious about it. The vision of this course is to build a complete computer from a very basic circuit.

Boolean algebra

In mathematics and mathematical logic, logical algebra (sometimes also called switching algebra, Boolean algebra) is a branch of algebra whose variables have only two truth values, true and false (usually denoted as 1 and 0). Logic algebra was introduced by George Boole in his first book, The Mathematical Analysis of Logic (1847), and more fully developed in his Studies of The Laws of Thought (1854).

This passage is from Wikipedia, and it is named after the man who invented Boolean algebra. We’re not going to go into Boolean algebra, we’re just going to cover what we can use.

Or take mathematics as an example. The four most basic operations in mathematics are addition, subtraction, multiplication and division. Similarly, Boolean algebra also has many calculation methods, and the most basic should be: and, or, not.

Before we get into the actual calculations, we need to keep the premise in mind: Boolean variables have only two truth values: true and false (usually denoted as 1 and 0, true being 1 and false being 0).

with

Since Boolean variables have only two truth values, true and false, we can actually list all the cases and form a table, which is what we usually call a truth table.

The truth table of and is as follows:

x y X and y
0 0 0
0 1 0
1 0 0
1 1 1

It can also be seen from the above that there are 2n2^n2n cases for the truth table of NNN variables.

So x and y are essentially 1 when x and y are both 1, which is true when x and y are both true.

You can have your own interpretation, for example, when we first learned addition, the teacher told us that 1+1=2 is actually one apple plus one apple equals two apples. If you want to travel and ask your parents for advice, x represents your father’s opinion, x=1 represents your father’s opinion, y=1 represents your mother’s opinion, y=1 represents your mother’s opinion, y=1 represents your mother’s opinion, and 0 means you do not agree. So with means, father and mother both agree, is passed.

We usually express x plus y as the sign + : x+y; ⋅yx \cdot yx \

or

The truth table of or is as follows:

x y X or y
0 0 0
0 1 1
1 0 1
1 1 1

In the example above, or mom or dad say yes, it’s a yes.

In the same manner, the symbol is “XXX” or “yyy \Rightarrow”

non

The truth table is not the same as, or slightly different from, the above:

x The x
0 1
1 0

Not is just the opposite of x, literally.

“Not XXX \Rightarrow \ x‾\overline{x}x

With the

Nand literally means a combination of operations and non-operations.

The truth table of and and not is as follows:

x y X and y
0 0 1
0 1 1
1 0 1
1 1 0

And non is the result of the calculation of and, and then do the non calculation. ⋅y‾\overline{x \cdot y}x⋅y

I’m going to separate the and not out here because in a circuit, we can use one and not to construct all the other components. So we just need to understand a circuit diagram. The point of this lesson is to figure out how to express Boolean logic in terms of circuits, and how efficient the circuits are, and so on, for hardware engineers. In fact, learning is like this, we need to grasp the main line, clear purpose. If we get bogged down in the details of Boolean logic or circuit diagrams, we are going astray.

Logic circuit

A logic circuit is a circuit that performs logical operations. This kind of circuit generally has several input ends and one or several output ends. When the input signals meet a specific logical relationship, the circuit is open and there is output. Otherwise, the circuit is closed with no output. Therefore, this kind of circuit is also called logic gate circuit, short for gate circuit.

This paragraph from Baidu encyclopedia, may be a mouthful, we directly look at the implementation.

Nand gate

As shown in the figure, I1 and I2 are the input, and NPN is a triode (when I1 or I2 is powered on, the upper and lower ends are connected). We use 1 to mean there is electricity, and 0 to mean there is no electricity, so only when I1 and I2 are 1, Output is 0. We can verify this with an example from the truth table.

To simplify the representation, we use the following notation to represent the nand gate:

Here we have taken the first step by representing a Boolean operation logic using a physically real circuit diagram. So now we use this circuit diagram to construct the rest of the circuit diagram.

gate

The implementation of the not gate is as follows:

In other words, we only need to connect the two inputs of the nand gate on the circuit diagram to get a not gate: at this time, the two inputs of the Nand gate are always equal, when the two inputs are 1, the output is 0, when the two inputs are 0, the output is 1.

The symbol of the not gate is as follows:

With the door

Similarly, an and gate is implemented as follows:

The notation is as follows:

Or gate

An or gate is implemented as follows:

The notation is as follows:

review

In this section we implement a circuit to represent the basic gate circuit. In the next section, we will simply learn a hardware Description Language (HDL: Hardware Description Language) to express the realization of the circuit, step by step to abstract, and achieve more components. To build a complete computer.

Another digression: abstraction. Abstraction is an important skill for software engineers. Here, we use the nor gate to construct the not gate, and the door, or the door, so we can understand the process of construction as an abstract process, the construction of the gate circuit can be directly for our use, rather than each time to draw several nor gates, and then connect them. Of course, the current gate circuit is relatively simple, and our perception is not very strong. If we construct a certain door constructed by dozens of or not doors, then abstraction is particularly important. In fact, the same is true when writing code, we should abstract the common parts out of a module or a utility class.