Nand to tetris Week 1

·

0 min read

I am currently participating a course on coursera where the target is "from nand to tetris". We start with a nand gate and in the end we will build our own virtual computer, our own programing language and our own applictions.

Week 1: Boolean logic.

we start with something "simple". The "bit". a bit is defined with following two states: 1 or 0 a.k.a true or false a.k.a Voltage or no Voltage.

After that we get the introduction into boolen logic operators.

and: a & b 
not: !a 
or: a | b

which are represented as

and:              not:            or:
a | b | out        in | out       a | b | out
0 | 0 | 0           1 | 0         0 | 0 | 0
1 | 0 | 0           0 | 1         1 | 0 | 1
0 | 1 | 0                         0 | 1 | 1
1 | 1 | 1                         1 | 1 | 1

And: means a and b has to be 1 to return 1
Or: means it either a or b or both have to be 1 to return 1
Not: means it always return the opposite of what comes in

Next thing is the concept that every table (truth table) can be seen as a function. A classic example would be space- vs time-tradeoff. Either I spend time computing stuff or I need space to store it somewhere.

But this is sidetracking. The point was more about that you can create your logic based on a truth table and look at it as a function. I will take a real live example

xor:             
a | b | out      
0 | 0 | 0       a | b
1 | 0 | 0       a | b    
0 | 1 | 0       a | b
1 | 1 | 0      !a & !b

which would spell out in a more programming way

if ((a | b) & (a | b) & (a | b) & (!a & !b)

we can reduce it to the following

if ((a | b) & (!a & !b))

since (a | b) in a chain always brings the same result

a small sidenote: in most programming languages bit operations are written with only 1 operator sign

c = a && b would be highlevel comparison (e.g: string a equals string b)
c = a & b would be a bit operation (e.g: bit a and bit b)

so the difference with small integers of the size of 8 bits (i8 in rust or similar languages)
a = 1;    // 01
b = 2;   // 10
out = a | b;  // out = 3; -> 11
since it would resolve to the following way 

0     1
or   or
1     0
-------
1     1

that's the reason I only use "|" and "&" and "!" not the "&&" and "||" since i don't only compare but operate on the bits in these examples.

Next we thing is that we learn that we don't need 3 operations since

a | b | out
0 | 0 | 0 
1 | 0 | 1
0 | 1 | 1
1 | 1 | 1

can be seen as:
a | b 

or 

!(a&b)

so by just using "not" and "and" we can represent "or".

and by using "not" and "or" we can represent "and".

so we only need one type of gate the "nand" gate a.k.a "Not And"

a gate is a compound of electronic parts that represent our logic operations. so the operation "nand" as an abstraction is equal to the function

f(a,b) -> !(a & b)

which means we can program on chip level by using these gates.

The next step is a own hardware abstraction language: HAL which is running in the hardware-simulator. Which is an introduction on it's own.

After that we're getting the task of creating 16 different chips from the starting "nand"-gate

I will show one of the easist chips since the fun of the whole thing is building them yourself. Well fun ... at some point bus systems get involved and we need to build mux and dmux.

But I don't wanna spoil the fun of building them yourself that's why I won't post my solutions to all the problems. This is a course you do for yourself if you cheat .... don't .... just send them the money. for the course.

CHIP And {
    IN a, b;
    OUT out;

    PARTS:
    Nand(a=a, b=b, out=nandOut);
    Not(in=nandOut, out=out);
}

Currently the course is a lot of fun and after finishing your 16 different chips the next week is about building your own ALU.

I hope this was useful. To me it was some kind of summary of what I did the last couple of days. :D