2.1 Binary Numbers

image-20250405164423371

image-20250405163338004

image-20250405163415418

image-20250405163504925

image-20250405163628278

image-20250405163712469

image-20250405163835567

image-20250405164037453

image-20250405164111279

image-20250405164323993

image-20250405164440667

2.2 Binary Addtion

image-20250405164738887

image-20250405164824580

image-20250405165448779

image-20250405165500123

image-20250405165606105

image-20250405165723071

image-20250405165825413

image-20250405165845990

image-20250405170123908

image-20250405170129544

image-20250405170208503

image-20250405170216151

2.3 Negative Numbers

image-20250405170607720

image-20250405170630309

前一半的二进制数字用来表示正数,后一半的二进制数字(16-3=13)留作表示负数

image-20250405170752518

image-20250405170802386

image-20250405170839174

image-20250405171005000

image-20250405171035760

image-20250405171049696

image-20250405171128208

image-20250405171844586

image-20250405171855034

???

image-20250405172020903

image-20250405172052564

image-20250405172109700

下面是二进制转化为对应负数,上面是十进制转化为对应负数。

image-20250405172145471

image-20250405172153998

image-20250405172208946

image-20250405172347010

image-20250405172357526

2.4 ALU

image-20250406110555945

image-20250406110852706

image-20250406111036333

image-20250406111128678

image-20250406111445256

image-20250406111457121

image-20250406111650538

image-20250406111659071

image-20250406111937396

image-20250406112032655

image-20250406112151147

image-20250406112211405

image-20250406112814587

image-20250406113216863

image-20250406113339328

image-20250406113424794

2.5 Project 2 Overview

image-20250406115407406

image-20250406115505314

image-20250406115619723

image-20250406115740949

image-20250406115813870

image-20250406115848432

image-20250406115943371

image-20250406120143252

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
/**
* out = (in[0] or in[1] or ... or in[15])
*/

// 我觉得叫Or1Way16更好,叫Or16Way类似于已有的Or8Way
CHIP Or16Way {
IN in[16];
OUT out;

PARTS:
Or(a = in[0], b = in[1], out = o1);
Or(a = o1, b = in[2], out = o2);
Or(a = o2, b = in[3], out = o3);
Or(a = o3, b = in[4], out = o4);
Or(a = o4, b = in[5], out = o5);
Or(a = o5, b = in[6], out = o6);
Or(a = o6, b = in[7], out = o7);
Or(a = o7, b = in[8], out = o8);
Or(a = o8, b = in[9], out = o9);
Or(a = o9, b = in[10], out = o10);
Or(a = o10, b = in[11], out = o11);
Or(a = o11, b = in[12], out = o12);
Or(a = o12, b = in[13], out = o13);
Or(a = o13, b = in[14], out = o14);
Or(a = o14, b = in[15], out = out);
}

Or 16 :16个并行的Or芯片

image-20250406224427936

Or 16 Way :16个Or芯片串行进行操作, 可以理解为16个bit串行进行or操作。

2.6 code

HalfAdder

08c53c9844af7bccdb643f527c67bff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Computes the sum of two bits.
*/

CHIP HalfAdder {
IN a, b; // 1-bit inputs
OUT sum, // Right bit of a + b
carry; // Left bit of a + b

PARTS:
Xor(a=a , b=b , out =sum );
And(a=a , b=b , out=carry );

}

FullAdder

d0e82880f0fd2ae54e547126bf44e45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Computes the sum of three bits.
*/

CHIP FullAdder {
IN a, b, c;
OUT sum,
carry;

PARTS:
//sum
Xor(a=a , b=b , out=xorab );
Xor(a=xorab , b=c , out=sum );

// carry
And(a=a , b=b , out=w1 );
And(a=b , b=c , out=w2 );
And(a=c , b=a , out=w3 );
Or(a=w1 , b=w2 , out=w4 );
Or(a=w3 , b=w4 , out=carry );
}

Add16 (16-bit adder)

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
/**
* 16-bit adder: Adds two 16-bit two's complement values.
* The most significant carry bit is ignored.
*/

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

PARTS:
HalfAdder(a=a[0] , b=b[0] , sum=out[0] , carry=c0 );
FullAdder(a=a[1] , b=b[1] , c=c0 , sum=out[1] , carry=c1 );
FullAdder(a=a[2] , b=b[2] , c=c1, sum=out[2] , carry=c2 );
FullAdder(a=a[3] , b=b[3] , c=c2 , sum=out[3] , carry=c3 );
FullAdder(a=a[4] , b=b[4] , c=c3 , sum=out[4] , carry=c4 );
FullAdder(a=a[5] , b=b[5] , c=c4 , sum=out[5] , carry=c5 );
FullAdder(a=a[6] , b=b[6] , c=c5 , sum=out[6] , carry=c6 );
FullAdder(a=a[7] , b=b[7] , c=c6 , sum=out[7] , carry=c7 );
FullAdder(a=a[8] , b=b[8] , c=c7 , sum=out[8] , carry=c8 );
FullAdder(a=a[9] , b=b[9] , c=c8 , sum=out[9] , carry=c9 );
FullAdder(a=a[10] , b=b[10] , c=c9 , sum=out[10] , carry=c10 );
FullAdder(a=a[11] , b=b[11] , c=c10 , sum=out[11] , carry=c11 );
FullAdder(a=a[12] , b=b[12] , c=c11 , sum=out[12] , carry=c12 );
FullAdder(a=a[13] , b=b[13] , c=c12 , sum=out[13] , carry=c13 );
FullAdder(a=a[14] , b=b[14] , c=c13 , sum=out[14] , carry=c14 );
FullAdder(a=a[15] , b=b[15] , c=c14 , sum=out[15] , carry=c15 );
}

Inc16 (out = in + 1)

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
/**
* 16-bit incrementer:
* out = in + 1
*/

CHIP Inc16 {
IN in[16];
OUT out[16];

PARTS:
HalfAdder(a=in[0] , b=true , sum=out[0] , carry=c0 );
FullAdder(a=in[1] , b=false , c=c0 , sum=out[1] , carry=c1 );
FullAdder(a=in[2] , b=false , c=c1, sum=out[2] , carry=c2 );
FullAdder(a=in[3] , b=false , c=c2 , sum=out[3] , carry=c3 );
FullAdder(a=in[4] , b=false , c=c3 , sum=out[4] , carry=c4 );
FullAdder(a=in[5] , b=false , c=c4 , sum=out[5] , carry=c5 );
FullAdder(a=in[6] , b=false , c=c5 , sum=out[6] , carry=c6 );
FullAdder(a=in[7] , b=false , c=c6 , sum=out[7] , carry=c7 );
FullAdder(a=in[8] , b=false , c=c7 , sum=out[8] , carry=c8 );
FullAdder(a=in[9] , b=false , c=c8 , sum=out[9] , carry=c9 );
FullAdder(a=in[10] , b=false , c=c9 , sum=out[10] , carry=c10 );
FullAdder(a=in[11] , b=false , c=c10 , sum=out[11] , carry=c11 );
FullAdder(a=in[12] , b=false , c=c11 , sum=out[12] , carry=c12 );
FullAdder(a=in[13] , b=false , c=c12 , sum=out[13] , carry=c13 );
FullAdder(a=in[14] , b=false , c=c13 , sum=out[14] , carry=c14 );
FullAdder(a=in[15] , b=false , c=c14 , sum=out[15] , carry=c15 );
}

ALU

(one by one)

image-20250406111650538

image-20250406111659071

image-20250406113216863

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
/**
* ALU (Arithmetic Logic Unit):
* Computes out = one of the following functions:
* 0, 1, -1,
* x, y, !x, !y, -x, -y,
* x + 1, y + 1, x - 1, y - 1,
* x + y, x - y, y - x,
* x & y, x | y
* on the 16-bit inputs x, y,
* according to the input bits zx, nx, zy, ny, f, no.
* In addition, computes the two output bits:
* if (out == 0) zr = 1, else zr = 0
* if (out < 0) ng = 1, else ng = 0
*/

// Implementation: Manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (zx == 1) sets x = 0 // 16-bit constant
// if (nx == 1) sets x = !x // bitwise not
// if (zy == 1) sets y = 0 // 16-bit constant
// if (ny == 1) sets y = !y // bitwise not
// if (f == 1) sets out = x + y // 整数补码加法
// if (f == 0) sets out = x & y // bitwise !and!
// if (no == 1) sets out = !out // bitwise not

ALU

![3f83a46c9a79a5e0cb1a05e2188f621](C:\Users\99512\Documents\WeChat Files\wxid_mt4e0cuygds122\FileStorage\Temp\3f83a46c9a79a5e0cb1a05e2188f621.jpg)

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
CHIP ALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
nx, // negate the x input?
zy, // zero the y input?
ny, // negate the y input?
f, // compute (out = x + y) or (out = x & y)?
no; // negate the out output?
OUT
out[16], // 16-bit output
zr, // if (out == 0) equals 1, else 0
ng; // if (out < 0) equals 1, else 0

PARTS:
// zx
Mux16(a=x , b=false , sel=zx , out=x1 );

// nx
Not16(in=x1 , out=notx );
Mux16(a=x1 , b=notx , sel=nx , out=x2 );

// zy
Mux16(a=y , b=false , sel=zy , out=y1 );

// ny
Not16(in=y1 , out=noty );
Mux16(a=y1 , b=noty , sel=ny , out=y2 );

// f
And16(a=x2 , b=y2 , out=and ); // & 是 按位 与运算 and
Add16(a =x2 , b =y2 , out =add ); // + 是 按位 加运算 add
Mux16(a=and , b=add , sel=f , out=l1 );

// no
Not16(in=l1 , out=l2 );
Mux16(a=l1, b=l2, sel=no, out=out,out[0..7]=kk1, out[8..15]=kk2, out[15]=kk);
// kk是精华 !

// zr
Or8Way(in=kk1, out=zr1);
Or8Way(in=kk2, out=zr2);
Or(a=zr1, b=zr2, out=zrNot);
Not(in=zrNot, out=zr);

// ng
Mux(a=false, b=kk, sel=zrNot, out=ng);
}