没脚的雀

从与非门到俄罗斯方块

从与非门到俄罗斯方块(From Nand to Tetris)

哈佛版本 CS 101:) (http://www.nand2tetris.org/)

(CS 101: Stanford 的计算机公开课 https://lagunita.stanford.edu/courses/Engineering/CS101/Summer2014/about#video-modal)

课程配套的教材中文译本是: 《计算机系统要素》(https://book.douban.com/subject/1998341/)

在作者的视频介绍中提及,CS涵盖的主要内容是:

1
OS, Hardware, Programming Language, Architecture, Data Structures, Compilers, Algorithm, Software Engineering, etc.

现有很多课程主要专注于上述的某一方面的内容,缺少了对整个计算机系统介绍的big picture,这正式这门课程想要解决的问题。

课程从 Nand 逻辑门开始讲起,涉及机器语言、汇编器、OS等内容,全面介绍了计算机系统,但是因为涉及面比较广,介绍的不是很深入,比较适合入门,另外课程配套了一些project, 可以在网站上面找到相应的软件以及text。

这里主要介绍第一个 project

布尔逻辑

两输入的真值表,一共由16个,这些逻辑元器件可以使用 与非门作为基础元件进行构建。这里涉及到一个问题: 为什么与非门可以作为basic building block 用来构建其余的逻辑元器件? 参考 [1, 2, 3] 的内容, 这里不展开说明, 其实我也不是很懂。

Pro 1 中的作业

使用 Nand 构建以下的逻辑门。

非门

非门的真值表:

in out
0 1
1 0

逻辑图:

Not

代码:

1
2
3
4
5
6
7
CHIP Not {
IN in;
OUT out;

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

与门

真值表:

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

逻辑图:

And

1
2
3
4
5
6
7
8
9
10
CHIP And {
IN a, b;
OUT out;

PARTS:
// Put your code here:
Nand(a=a, b=b, out=y);
Not(in=y, out=out);

}

或门

真值表:

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

逻辑图:

Or

1
2
3
4
5
6
7
8
9
10
11
CHIP Or {
IN a, b;
OUT out;

PARTS:
// Put your code here:
Not(in=a, out=na);
Not(in=b, out=nb);
Nand(a=na, b=nb, out=x);

}

异或门

真值表:

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

逻辑图:

Xor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
CHIP Xor {
IN a, b;
OUT out;

PARTS:
// Version 1
// Not(in=a, out=na);
// Not(in=b, out=nb);
// And(a=na, b=b, out=x);
// And(a=nb, b=a, out=y);
// Or(a=x, b=y, out=out);

// Version 2 5 Nand
// Not(in=a, out=na);
// Not(in=b, out=nb);
// Nand(a=na, b=b, out=x);
// Nand(a=nb, b=a, out=y);
// Nand(a=x, b=y, out=out);

// Version 3 4 Nand
Nand(a=a, b=b, out=ab);
Nand(a=a, b=ab, out=x);
Nand(a=b, b=ab, out=y);
Nand(a=x, b=y, out=out);
}

数据选择器

真值表:

a b sel out
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 1

逻辑图:

Mux

1
2
3
4
5
6
7
8
9
10
11
CHIP Mux {
IN a, b, sel;
OUT out;

PARTS:
// Put your code here:
Not(in=sel, out=ns);
Nand(a=a, b=ns, out=x);
Nand(a=b, b=sel, out=y);
Nand(a=x, b=y, out=out);
}

参考引用

[1] https://en.wikipedia.org/wiki/NAND_logic

[2] https://en.wikipedia.org/wiki/Functional_completeness

[3] https://www.quora.com/Why-do-we-use-NAND-and-NOR-Gate-for-implementing-any-logic-design

大佬给口饭吃咧