Proj1

1.1 Boolean Logic

image-20250404141214589

Some useful equations

image-20250404141414517

→Boolean Algebra can be 化简

image-20250404141608949

image-20250404141759670

(Two way : ①equations,②write down the truth table)

1.2 Boolean Function

001:

  • 理论上,任何布尔函数都可以用NotAndOr等表示出来(通过真值表选取f=1的情况)(做芯片有用)

image-20250404142330760

image-20250404142531366

002:

  • (or可以通过And和Not表示)

image-20250404142856026

003:

  • 而Nand可以表示出AndNot芯片(根本的根本)

image-20250404142922081

image-20250404144646895

1.3 Logic Gates

上一节是抽象的逻辑运算,这一节是实际的芯片/逻辑门

image-20250404145506478

image-20250404145701379

image-20250404145744036

image-20250404145815647

一个接口,可以有不同的实现

image-20250404150147322

image-20250404150426261

1.4 Hardware Description Language

一种硬件描述语言(做硬件/芯片用的)

image-20250404150718865

芯片接口 和 芯片实现

实现:用真值表看1,写出布尔函数化简,画出逻辑图

image-20250404151805805

image-20250404152954069

image-20250404153644134

image-20250404154013569

image-20250404154041022

1.5 Hardware Simulation

  • for 模拟芯片测试
    • way:在给定的硬件模拟器中 + 使用编写的测试文件

image-20250404160947915

image-20250404163421803

image-20250404170858647

image-20250404172732071

image-20250404172955446

1.6 Mutil-bit Buses

多位总线(eg16位总线,一条线上有16个位bit)

(最左边第一位是高位。 最右边的最低位一般标记为第 0 位)

image-20250404202521312

image-20250404202530048

⭐ useful

image-20250404202602223

说明中第一句的意思是:可以进行多个output,比如前8位输出,后八位输出,全部输出

1.7 proj 7 preview

image-20250404202849141

image-20250404202926080

tip几个

image-20250404203025658

image-20250404203159712

(既可以用作And门,也可以用作Mux门)

image-20250404203425278

image-20250404204239746

image-20250404204625628

And 16 : 两个输入,都是16位的,一个输出(每位进行And运算),也是十六位的

image-20250404204739365

image-20250404204849942

16-bit,4way multiplexor : 输入都是16位的,4个输入,一个16bit输出(mux逻辑,根据which sel进行选择)。

image-20250404205018344

image-20250404205126035

notes

image-20250404210112492

API

image-20250404211743753

1.8 code

Nand

x y out
1 1 0
1 0 1
0 1 1
0 0 1

Not

1
2
3
4
5
6
7
8
9
10
// Not gate : if (in) out = 0, else out = 1

CHIP Not {
IN in;
OUT out;

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

}

And

1
2
3
4
5
6
7
8
9
10
// And gate : if (a and b) out = 1, else out = 0 

CHIP And {
IN a, b;
OUT out;

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

Or

b75694b72e0957867d154be60f9a207
1
2
3
4
5
6
7
8
9
10
11
12
// Or gate : if (a or b) out = 1, else out = 0 
// only a=0,b=0,out=0,else out=1

CHIP Or {
IN a, b;
OUT out;

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

Xor

14c146587d40e107bec5f52c50fc019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Exclusive-or gate: if ((a and Not(b)) or (Not(a) and b)) out = 1, else out = 0

CHIP Xor {
IN a, b;
OUT out;

PARTS:
////真值表推出来的(定义也写明了哈哈哈)
Not(in=a, out=nota);
Not(in=b, out=notb);
And(a=a, b=notb, out=aAndnotb);
And(a=b, b=nota, out=bAndnota);
Or(a=aAndnotb, b=bAndnota, out=out);
}

Mux(二1bit并一)

bb2b12af45a30fae8396224ade119a7
1
2
3
4
5
6
7
8
9
10
11
12
// Multiplexor: if (sel = 0) out = a, else out = b  (good!)

CHIP Mux {
IN a, b, sel;
OUT out;

PARTS:
Not(in=sel, out=notsel);
And(a=a , b=notsel, out=outa);//if sel==0,返回a (depend on a)
And(a=sel, b=b, out=outb ); //if sel==1,返回b (depend on b)
Or(a=outa, b=outb, out=out );
}

Dmux(一解二)

1c6ff7a10f0431156d30ca2e457635b
1
2
3
4
5
6
7
8
9
10
11
// Demultiplexor: [a, b] = 		[in, 0] if sel = 0 		[0, in] if sel = 1

CHIP DMux {
IN in, sel;
OUT a, b;

PARTS:
Not(in=sel , out=notsel );
And(a=in, b=notsel, out=a );
And(a=in, b=sel , out=b );
}

Not16

2888a1bdd8c56821dc4950f143b6396
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
// 16-bit Not gate:		for i = 0, ..., 15: 	out[i] = Not(a[i])

// 把一个16位的数,每一位都取反
CHIP Not16 {
IN in[16];
OUT out[16];

PARTS:
Not(in=in[0] , out=out[0] );
Not(in=in[1] , out=out[1] );
Not(in=in[2] , out=out[2] );
Not(in=in[3] , out=out[3] );
Not(in=in[4] , out=out[4] );
Not(in=in[5] , out=out[5] );
Not(in=in[6] , out=out[6] );
Not(in=in[7] , out=out[7] );
Not(in=in[8] , out=out[8] );
Not(in=in[9] , out=out[9] );
Not(in=in[10] , out=out[10] );
Not(in=in[11] , out=out[11] );
Not(in=in[12] , out=out[12] );
Not(in=in[13] , out=out[13] );
Not(in=in[14] , out=out[14] );
Not(in=in[15] , out=out[15] );
}

And16

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
26
// 16-bit And gate: 	for i = 0, ..., 15: 	out[i] = a[i] And b[i] 

// 把两个16位的数,每一位都求And
CHIP And16 {
IN a[16], b[16];
OUT out[16];

PARTS:
//// Replace this comment with your code.
And(a=a[0] , b=b[0] , out=out[0] );
And(a=a[1] , b=b[1] , out=out[1] );
And(a=a[2] , b=b[2] , out=out[2] );
And(a=a[3] , b=b[3] , out=out[3] );
And(a=a[4] , b=b[4] , out=out[4] );
And(a=a[5] , b=b[5] , out=out[5] );
And(a=a[6] , b=b[6] , out=out[6] );
And(a=a[7] , b=b[7] , out=out[7] );
And(a=a[8] , b=b[8] , out=out[8] );
And(a=a[9] , b=b[9] , out=out[9] );
And(a=a[10] , b=b[10] , out=out[10] );
And(a=a[11] , b=b[11] , out=out[11] );
And(a=a[12] , b=b[12] , out=out[12] );
And(a=a[13] , b=b[13] , out=out[13] );
And(a=a[14] , b=b[14] , out=out[14] );
And(a=a[15] , b=b[15] , out=out[15] );
}

Or16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 16-bit Or gate: 		for i = 0, ..., 15: 	out[i] = a[i] Or b[i] 

CHIP Or16 {
IN a[16], b[16];
OUT out[16];

PARTS:
Or(a=a[0] , b=b[0] , out=out[0] );
Or(a=a[1] , b=b[1] , out=out[1] );
Or(a=a[2] , b=b[2] , out=out[2] );
Or(a=a[3] , b=b[3] , out=out[3] );
Or(a=a[4] , b=b[4] , out=out[4] );
Or(a=a[5] , b=b[5] , out=out[5] );
Or(a=a[6] , b=b[6] , out=out[6] );
Or(a=a[7] , b=b[7] , out=out[7] );
Or(a=a[8] , b=b[8] , out=out[8] );
Or(a=a[9] , b=b[9] , out=out[9] );
Or(a=a[10] , b=b[10] , out=out[10] );
Or(a=a[11] , b=b[11] , out=out[11] );
Or(a=a[12] , b=b[12] , out=out[12] );
Or(a=a[13] , b=b[13] , out=out[13] );
Or(a=a[14] , b=b[14] , out=out[14] );
Or(a=a[15] , b=b[15] , out=out[15] );
}

Mux16(二16bit并一)

  • 实现方式:16个Mux并排作用与每一个bit
e37fe587999c0d2944e788df5cbfd59
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 16-bit multiplexor: 		for i = 0, ..., 15:	   if (sel = 0) out[i] = a[i], else out[i] = b[i]

CHIP Mux16 {
IN a[16], b[16], sel;
OUT out[16];

PARTS:
Not(in=sel , out=notsel );

And(a=a[0] , b=notsel , out=out01 );
And(a=sel , b=b[0] , out=out02 );
Or(a=out01 , b=out02 , out=out[0] );

And(a=a[1] , b=notsel , out=out11 );
And(a=sel , b=b[1] , out=out12 );
Or(a=out11 , b=out12 , out=out[1] );

And(a=a[2] , b=notsel , out=out21 );
And(a=sel , b=b[2] , out=out22 );
Or(a=out21 , b=out22 , out=out[2] );

And(a=a[3] , b=notsel , out=out31 );
And(a=sel , b=b[3] , out=out32 );
Or(a=out31 , b=out32 , out=out[3] );

And(a=a[4] , b=notsel , out=out41 );
And(a=sel , b=b[4] , out=out42 );
Or(a=out41 , b=out42 , out=out[4] );

And(a=a[5] , b=notsel , out=out51 );
And(a=sel , b=b[5] , out=out52 );
Or(a=out51 , b=out52 , out=out[5] );

And(a=a[6] , b=notsel , out=out61 );
And(a=sel , b=b[6] , out=out62 );
Or(a=out61 , b=out62 , out=out[6] );

And(a=a[7] , b=notsel , out=out71 );
And(a=sel , b=b[7] , out=out72 );
Or(a=out71 , b=out72 , out=out[7] );

And(a=a[8] , b=notsel , out=out81 );
And(a=sel , b=b[8] , out=out82 );
Or(a=out81 , b=out82 , out=out[8] );

And(a=a[9] , b=notsel , out=out91 );
And(a=sel , b=b[9] , out=out92 );
Or(a=out91 , b=out92 , out=out[9] );

And(a=a[10] , b=notsel , out=out101 );
And(a=sel , b=b[10] , out=out102 );
Or(a=out101 , b=out102 , out=out[10] );

And(a=a[11] , b=notsel , out=out111 );
And(a=sel , b=b[11] , out=out112 );
Or(a=out111 , b=out112 , out=out[11] );

And(a=a[12] , b=notsel , out=out121 );
And(a=sel , b=b[12] , out=out122 );
Or(a=out121 , b=out122 , out=out[12] );

And(a=a[13] , b=notsel , out=out131 );
And(a=sel , b=b[13] , out=out132 );
Or(a=out131 , b=out132 , out=out[13] );

And(a=a[14] , b=notsel , out=out141 );
And(a=sel , b=b[14] , out=out142 );
Or(a=out141 , b=out142 , out=out[14] );

And(a=a[15] , b=notsel , out=out151 );
And(a=sel , b=b[15] , out=out152 );
Or(a=out151 , b=out152 , out=out[15] );
}

Or8Way

1113aa3384d980cf7a3cf7c22f999ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 8-way Or gate!  		out = in[0] Or in[1] Or ... Or in[7]
// 输入 一个 8-bit数!而不是 两个 !输出一个1-bit数。

CHIP Or8Way {
IN in[8];
OUT out;

PARTS:
// 输入一个八位(bit)数,依次每两位取Or,最后输出结果(全0才会输出0,有一个1就输出1)
Or(a=in[0] , b=in[1] , out=s1 );
Or(a=in[2] , b=in[3] , out=s2 );
Or(a=in[4] , b=in[5] , out=s3 );
Or(a=in[6] , b=in[7] , out=s4 );
Or(a=s1 , b=s2 , out=w1 );
Or(a=s3 , b=s4 , out=w2 );
Or(a=w1 , b=w2 , out=out );
}

Mux4Way16(四16bit并一)

  • 解析名字含义:

    • Mux1616-bit 的意思。

    • Mux4Way1616-bit,4-way 的意思

    • 那为啥 Or8Way 是 8-bit,1-way?的意思?

412c8358a3261cc20cca4fb7d9437aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 4-way 16-bit multiplexor:
* out = a if sel = 00
* b if sel = 01
* c if sel = 10
* d if sel = 11
*/

// 四条路/输入,每个都是16bit,根据sel的不同(4个sel,两位的二进制数字),输出其中 一 个数。
CHIP Mux4Way16 { // 看成 Mux 16 ,把 4Way 插在名字中间
IN a[16], b[16], c[16], d[16], sel[2];
OUT out[16];

PARTS:
// we just need one output !
Mux16(a=a , b=b , sel=sel[0] , out=w1 ); // [0]是最右
Mux16(a=c , b=d , sel=sel[0] , out=w2 );

Mux16(a=w1 , b=w2 , sel=sel[1] , out=out );
}

Mux8Way16(八16bit并一)

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
/**
* 8-way 16-bit multiplexor:
* out = a if sel = 000
* b if sel = 001
* c if sel = 010
* d if sel = 011
* e if sel = 100
* f if sel = 101
* g if sel = 110
* h if sel = 111
*/

// 8条路/输入,每个都是16bit,根据sel的不同(8个sel,三位的二进制数字),输出其中 一 个数。
CHIP Mux8Way16 {
IN a[16], b[16], c[16], d[16],
e[16], f[16], g[16], h[16],
sel[3];
OUT out[16];

PARTS:
// 用两个Mux4Way16再加上一个Mux16就行了。
Mux4Way16(a=a , b=b , c=c , d=d , sel[0]=sel[0] ,sel[1]=sel[1],out=w1 );
Mux4Way16(a=e , b=f , c=g , d=h , sel[0]=sel[0] ,sel[1]=sel[1],out=w2 );
Mux16(a=w1 , b=w2 , sel=sel[2] , out=out );
}

DMux4Way(一解四)

042760ad0a8f3a0d172d509e6a2462e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 4-way demultiplexor:
* [a, b, c, d] = [in, 0, 0, 0] if sel = 00
* [0, in, 0, 0] if sel = 01
* [0, 0, in, 0] if sel = 10
* [0, 0, 0, in] if sel = 11
*/

// 一个输入(几bit应该没要求),根据sel的不同选择赋予abcd哪个的值(4个值4Way,需要4个sel),其他值输出0(4个sel,两位二进制数字)
CHIP DMux4Way {
IN in, sel[2];
OUT a, b, c, d;

PARTS:
DMux(in=in, sel=sel[1], a=top, b=bottom);

DMux(in=top, sel=sel[0], a=a, b=b);
DMux(in=bottom, sel=sel[0], a=c, b=d);

}

DMux8Way(一解八)

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
26
27
28
29
30
31
32
33
/**
* 8-way demultiplexor:
* [a, b, c, d, e, f, g, h] = [in, 0, 0, 0, 0, 0, 0, 0] if sel = 000
* [0, in, 0, 0, 0, 0, 0, 0] if sel = 001
* [0, 0, in, 0, 0, 0, 0, 0] if sel = 010
* [0, 0, 0, in, 0, 0, 0, 0] if sel = 011
* [0, 0, 0, 0, in, 0, 0, 0] if sel = 100
* [0, 0, 0, 0, 0, in, 0, 0] if sel = 101
* [0, 0, 0, 0, 0, 0, in, 0] if sel = 110
* [0, 0, 0, 0, 0, 0, 0, in] if sel = 111
*/

// 根据三位数的sel把in分成8部分
CHIP DMux8Way {
IN in, sel[3];
// sel有8种可能,但打开时,只有一种状态,所以只有一个有值,其他都是0值。
OUT a, b, c, d, e, f, g, h;

PARTS:
// 根据sel第一位,把in数据分成两部分,top和bottom
DMux(in=in, sel=sel[2], a=top, b=bottom);

// 根据sel第二位,分别把top和bottom再分成两个部分,总计四部分
DMux(in=top, sel=sel[1], a=w1, b=w2);
DMux(in=bottom, sel=sel[1], a=w3, b=w4);

// 根据sel第三位,把in总共分成8部分
DMux(in=w1, sel=sel[0], a=a, b=b);
DMux(in=w2, sel=sel[0], a=c, b=d);

DMux(in=w3, sel=sel[0], a=e, b=f);
DMux(in=w4, sel=sel[0], a=g, b=h);
}