From 9542deeb483a00b6fabed7574720926ce97d7511 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Tue, 16 Aug 2022 11:54:23 +0800 Subject: Projects, 01-06 completed --- projects/02/ALU-nostat.cmp | 37 +++++ projects/02/ALU-nostat.tst | 353 ++++++++++++++++++++++++++++++++++++++++++ projects/02/ALU.cmp | 37 +++++ projects/02/ALU.hdl | 61 ++++++++ projects/02/ALU.out | 37 +++++ projects/02/ALU.tst | 377 +++++++++++++++++++++++++++++++++++++++++++++ projects/02/Add16.cmp | 7 + projects/02/Add16.hdl | 32 ++++ projects/02/Add16.out | 7 + projects/02/Add16.tst | 39 +++++ projects/02/FullAdder.cmp | 9 ++ projects/02/FullAdder.hdl | 19 +++ projects/02/FullAdder.out | 9 ++ projects/02/FullAdder.tst | 47 ++++++ projects/02/HalfAdder.cmp | 5 + projects/02/HalfAdder.hdl | 18 +++ projects/02/HalfAdder.out | 5 + projects/02/HalfAdder.tst | 29 ++++ projects/02/Inc16.cmp | 5 + projects/02/Inc16.hdl | 32 ++++ projects/02/Inc16.out | 5 + projects/02/Inc16.tst | 25 +++ 22 files changed, 1195 insertions(+) create mode 100644 projects/02/ALU-nostat.cmp create mode 100644 projects/02/ALU-nostat.tst create mode 100644 projects/02/ALU.cmp create mode 100644 projects/02/ALU.hdl create mode 100644 projects/02/ALU.out create mode 100644 projects/02/ALU.tst create mode 100644 projects/02/Add16.cmp create mode 100644 projects/02/Add16.hdl create mode 100644 projects/02/Add16.out create mode 100644 projects/02/Add16.tst create mode 100644 projects/02/FullAdder.cmp create mode 100644 projects/02/FullAdder.hdl create mode 100644 projects/02/FullAdder.out create mode 100644 projects/02/FullAdder.tst create mode 100644 projects/02/HalfAdder.cmp create mode 100644 projects/02/HalfAdder.hdl create mode 100644 projects/02/HalfAdder.out create mode 100644 projects/02/HalfAdder.tst create mode 100644 projects/02/Inc16.cmp create mode 100644 projects/02/Inc16.hdl create mode 100644 projects/02/Inc16.out create mode 100644 projects/02/Inc16.tst (limited to 'projects/02') diff --git a/projects/02/ALU-nostat.cmp b/projects/02/ALU-nostat.cmp new file mode 100644 index 0000000..976ab78 --- /dev/null +++ b/projects/02/ALU-nostat.cmp @@ -0,0 +1,37 @@ +| x | y |zx |nx |zy |ny | f |no | out | +| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | +| 0101101110100000 | 0001111011010010 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 0 | 0101101110100000 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 0 | 0001111011010010 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 1 | 1010010001011111 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 1 | 1110000100101101 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 1 | 1010010001100000 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 1 | 1110000100101110 | +| 0101101110100000 | 0001111011010010 | 0 | 1 | 1 | 1 | 1 | 1 | 0101101110100001 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 1 | 1 | 1 | 0001111011010011 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 0 | 0101101110011111 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 0 | 0001111011010001 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 1 | 0 | 0111101001110010 | +| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 0 | 1 | 1 | 0011110011001110 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 1 | 1 | 1 | 1100001100110010 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 0 | 0 | 0001101010000000 | +| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 1 | 0 | 1 | 0101111111110010 | diff --git a/projects/02/ALU-nostat.tst b/projects/02/ALU-nostat.tst new file mode 100644 index 0000000..fa3e448 --- /dev/null +++ b/projects/02/ALU-nostat.tst @@ -0,0 +1,353 @@ +// This file is part of the materials accompanying the book +// "The Elements of Computing Systems" by Nisan and Schocken, +// MIT Press. Book site: www.idc.ac.il/tecs +// File name: projects/02/ALU-nostat.tst + +// ALU-nostat.tst provides a partial test of the ALU chip. +// It IS NOT a replacement for ALU.tst. + +// ALU-nostat.tst tests only the computation part of the ALU. +// The 'zr' and 'ng' status outputs are ignored. + +// This test lets you concentrate on getting the ALU computation right without the +// additional task of handling the status outputs. + +// Once your ALU passes ALU-nostat.tst you need to test it with ALU.tst. +// This way, any comparison failures during ALU.tst will be caused by errors in +// the handling of the 'zr' and 'ng' status outputs. + +load ALU.hdl, +output-file ALU-nostat.out, +compare-to ALU-nostat.cmp, +output-list x%B1.16.1 y%B1.16.1 zx%B1.1.1 nx%B1.1.1 zy%B1.1.1 + ny%B1.1.1 f%B1.1.1 no%B1.1.1 out%B1.16.1; + +set x %B0000000000000000, +set y %B1111111111111111, + +set zx 1, +set nx 0, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 1, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 0, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 0, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 0, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +set zx 0, +set nx 1, +set zy 0, +set ny 1, +set f 0, +set no 1, +eval, +output; + +set x %B101101110100000, +set y %B001111011010010, + +set zx 1, +set nx 0, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 1, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 0, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 0, +eval, +output; + +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +set zx 0, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +set zx 0, +set nx 1, +set zy 0, +set ny 1, +set f 0, +set no 1, +eval, +output; diff --git a/projects/02/ALU.cmp b/projects/02/ALU.cmp new file mode 100644 index 0000000..2ec3a55 --- /dev/null +++ b/projects/02/ALU.cmp @@ -0,0 +1,37 @@ +| x | y |zx |nx |zy |ny | f |no | out |zr |ng | +| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000010001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 0 | 0000000000000011 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111101110 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 1 | 1111111111111100 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111101111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111101 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000010010 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000100 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 0 | 0000000000010000 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 0 | 0000000000000010 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0000000000010100 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000001110 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111110010 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 1 | 0 | 1 | 0000000000010011 | 0 | 0 | diff --git a/projects/02/ALU.hdl b/projects/02/ALU.hdl new file mode 100644 index 0000000..c12064c --- /dev/null +++ b/projects/02/ALU.hdl @@ -0,0 +1,61 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/ALU.hdl + +/** + * The ALU (Arithmetic Logic Unit). + * Computes one of the following functions: + * x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y, + * x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs, + * according to 6 input bits denoted zx,nx,zy,ny,f,no. + * In addition, the ALU computes two 1-bit outputs: + * if the ALU output == 0, zr is set to 1; otherwise zr is set to 0; + * if the ALU output < 0, ng is set to 1; otherwise ng is set to 0. + */ + +// Implementation: the ALU logic manipulates the x and y inputs +// and operates on the resulting values, as follows: +// if (zx == 1) set x = 0 // 16-bit constant +// if (nx == 1) set x = !x // bitwise not +// if (zy == 1) set y = 0 // 16-bit constant +// if (ny == 1) set y = !y // bitwise not +// if (f == 1) set out = x + y // integer 2's complement addition +// if (f == 0) set out = x & y // bitwise and +// if (no == 1) set out = !out // bitwise not +// if (out == 0) set zr = 1 +// if (out < 0) set ng = 1 + +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 (if 1) or x & y (if 0) + no; // negate the out output? + + OUT + out[16], // 16-bit output + zr, // 1 if (out == 0), 0 otherwise + ng; // 1 if (out < 0), 0 otherwise + + PARTS: + // zdx = (zx == 0) ? x : 0x0000; same with y + Mux16(a=x, b=false, sel=zx, out=zdx); + Mux16(a=y, b=false, sel=zy, out=zdy); + Not16(in=zdx, out=ndx); + Not16(in=zdy, out=ndy); + Mux16(a=zdx, b=ndx, sel=nx, out=px); + Mux16(a=zdy, b=ndy, sel=ny, out=py); + And16(a=px, b=py, out=xandy); + Add16(a=px, b=py, out=xplusy); + Mux16(a=xandy, b=xplusy, sel=f, out=fxy); + Not16(in=fxy, out=nfxy); + Mux16(a=fxy, b=nfxy, sel=no, out[15]=ng, out[0..7]=low, out[8..15]=high, out=out); + Or8Way(in=low, out=lowneq0); + Or8Way(in=high, out=highneq0); + Or(a=lowneq0, b=highneq0, out=outneq0); + Not(in=outneq0, out=zr); +} diff --git a/projects/02/ALU.out b/projects/02/ALU.out new file mode 100644 index 0000000..2424a73 --- /dev/null +++ b/projects/02/ALU.out @@ -0,0 +1,37 @@ +| x | y |zx |nx |zy |ny | f |no | out |zr |ng | +| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000010001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 0 | 0000000000000011 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111101110 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 1 | 1111111111111100 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111101111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111101 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000010010 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000100 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 0 | 0000000000010000 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 0 | 0000000000000010 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0000000000010100 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000001110 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111110010 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 1 | 0 | 1 | 0000000000010011 | 0 | 0 | diff --git a/projects/02/ALU.tst b/projects/02/ALU.tst new file mode 100644 index 0000000..9e665b0 --- /dev/null +++ b/projects/02/ALU.tst @@ -0,0 +1,377 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/ALU.tst + +load ALU.hdl, +output-file ALU.out, +compare-to ALU.cmp, +output-list x%B1.16.1 y%B1.16.1 zx%B1.1.1 nx%B1.1.1 zy%B1.1.1 + ny%B1.1.1 f%B1.1.1 no%B1.1.1 out%B1.16.1 zr%B1.1.1 + ng%B1.1.1; + +set x %B0000000000000000, // x = 0 +set y %B1111111111111111; // y = -1 + +// Compute 0 +set zx 1, +set nx 0, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute 1 +set zx 1, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute -1 +set zx 1, +set nx 1, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute x +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 0, +eval, +output; + +// Compute y +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +// Compute !x +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 1, +eval, +output; + +// Compute !y +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 1, +eval, +output; + +// Compute -x +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute -y +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +// Compute x + 1 +set zx 0, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute y + 1 +set zx 1, +set nx 1, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute x - 1 +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 0, +eval, +output; + +// Compute y - 1 +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute x + y +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute x - y +set zx 0, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +// Compute y - x +set zx 0, +set nx 0, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute x & y +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +// Compute x | y +set zx 0, +set nx 1, +set zy 0, +set ny 1, +set f 0, +set no 1, +eval, +output; + +set x %B000000000010001, // x = 17 +set y %B000000000000011; // y = 3 + +// Compute 0 +set zx 1, +set nx 0, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute 1 +set zx 1, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute -1 +set zx 1, +set nx 1, +set zy 1, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute x +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 0, +eval, +output; + +// Compute y +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +// Compute !x +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 0, +set no 1, +eval, +output; + +// Compute !y +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 0, +set no 1, +eval, +output; + +// Compute -x +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute -y +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +// Compute x + 1 +set zx 0, +set nx 1, +set zy 1, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute y + 1 +set zx 1, +set nx 1, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute x - 1 +set zx 0, +set nx 0, +set zy 1, +set ny 1, +set f 1, +set no 0, +eval, +output; + +// Compute y - 1 +set zx 1, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute x + y +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 1, +set no 0, +eval, +output; + +// Compute x - y +set zx 0, +set nx 1, +set zy 0, +set ny 0, +set f 1, +set no 1, +eval, +output; + +// Compute y - x +set zx 0, +set nx 0, +set zy 0, +set ny 1, +set f 1, +set no 1, +eval, +output; + +// Compute x & y +set zx 0, +set nx 0, +set zy 0, +set ny 0, +set f 0, +set no 0, +eval, +output; + +// Compute x | y +set zx 0, +set nx 1, +set zy 0, +set ny 1, +set f 0, +set no 1, +eval, +output; diff --git a/projects/02/Add16.cmp b/projects/02/Add16.cmp new file mode 100644 index 0000000..76d069c --- /dev/null +++ b/projects/02/Add16.cmp @@ -0,0 +1,7 @@ +| a | b | out | +| 0000000000000000 | 0000000000000000 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1111111111111111 | +| 1111111111111111 | 1111111111111111 | 1111111111111110 | +| 1010101010101010 | 0101010101010101 | 1111111111111111 | +| 0011110011000011 | 0000111111110000 | 0100110010110011 | +| 0001001000110100 | 1001100001110110 | 1010101010101010 | diff --git a/projects/02/Add16.hdl b/projects/02/Add16.hdl new file mode 100644 index 0000000..abad4f9 --- /dev/null +++ b/projects/02/Add16.hdl @@ -0,0 +1,32 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/Adder16.hdl + +/** + * Adds two 16-bit 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); +} diff --git a/projects/02/Add16.out b/projects/02/Add16.out new file mode 100644 index 0000000..20f9c65 --- /dev/null +++ b/projects/02/Add16.out @@ -0,0 +1,7 @@ +| a | b | out | +| 0000000000000000 | 0000000000000000 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1111111111111111 | +| 1111111111111111 | 1111111111111111 | 1111111111111110 | +| 1010101010101010 | 0101010101010101 | 1111111111111111 | +| 0011110011000011 | 0000111111110000 | 0100110010110011 | +| 0001001000110100 | 1001100001110110 | 1010101010101010 | diff --git a/projects/02/Add16.tst b/projects/02/Add16.tst new file mode 100644 index 0000000..85e1be1 --- /dev/null +++ b/projects/02/Add16.tst @@ -0,0 +1,39 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/Add16.tst + +load Add16.hdl, +output-file Add16.out, +compare-to Add16.cmp, +output-list a%B1.16.1 b%B1.16.1 out%B1.16.1; + +set a %B0000000000000000, +set b %B0000000000000000, +eval, +output; + +set a %B0000000000000000, +set b %B1111111111111111, +eval, +output; + +set a %B1111111111111111, +set b %B1111111111111111, +eval, +output; + +set a %B1010101010101010, +set b %B0101010101010101, +eval, +output; + +set a %B0011110011000011, +set b %B0000111111110000, +eval, +output; + +set a %B0001001000110100, +set b %B1001100001110110, +eval, +output; diff --git a/projects/02/FullAdder.cmp b/projects/02/FullAdder.cmp new file mode 100644 index 0000000..84551aa --- /dev/null +++ b/projects/02/FullAdder.cmp @@ -0,0 +1,9 @@ +| a | b | c | sum | carry | +| 0 | 0 | 0 | 0 | 0 | +| 0 | 0 | 1 | 1 | 0 | +| 0 | 1 | 0 | 1 | 0 | +| 0 | 1 | 1 | 0 | 1 | +| 1 | 0 | 0 | 1 | 0 | +| 1 | 0 | 1 | 0 | 1 | +| 1 | 1 | 0 | 0 | 1 | +| 1 | 1 | 1 | 1 | 1 | diff --git a/projects/02/FullAdder.hdl b/projects/02/FullAdder.hdl new file mode 100644 index 0000000..fc93910 --- /dev/null +++ b/projects/02/FullAdder.hdl @@ -0,0 +1,19 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/FullAdder.hdl + +/** + * Computes the sum of three bits. + */ + +CHIP FullAdder { + IN a, b, c; // 1-bit inputs + OUT sum, // Right bit of a + b + c + carry; // Left bit of a + b + c + + PARTS: + HalfAdder(a=a, b=b, sum=sumab, carry=carryab); + HalfAdder(a=sumab, b=c, sum=sum, carry=carryabc); + Or(a=carryab, b=carryabc, out=carry); +} diff --git a/projects/02/FullAdder.out b/projects/02/FullAdder.out new file mode 100644 index 0000000..610181f --- /dev/null +++ b/projects/02/FullAdder.out @@ -0,0 +1,9 @@ +| a | b | c | sum | carry | +| 0 | 0 | 0 | 0 | 0 | +| 0 | 0 | 1 | 1 | 0 | +| 0 | 1 | 0 | 1 | 0 | +| 0 | 1 | 1 | 0 | 1 | +| 1 | 0 | 0 | 1 | 0 | +| 1 | 0 | 1 | 0 | 1 | +| 1 | 1 | 0 | 0 | 1 | +| 1 | 1 | 1 | 1 | 1 | diff --git a/projects/02/FullAdder.tst b/projects/02/FullAdder.tst new file mode 100644 index 0000000..5125cee --- /dev/null +++ b/projects/02/FullAdder.tst @@ -0,0 +1,47 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/FullAdder.tst + +load FullAdder.hdl, +output-file FullAdder.out, +compare-to FullAdder.cmp, +output-list a%B3.1.3 b%B3.1.3 c%B3.1.3 sum%B3.1.3 carry%B3.1.3; + +set a 0, +set b 0, +set c 0, +eval, +output; + +set c 1, +eval, +output; + +set b 1, +set c 0, +eval, +output; + +set c 1, +eval, +output; + +set a 1, +set b 0, +set c 0, +eval, +output; + +set c 1, +eval, +output; + +set b 1, +set c 0, +eval, +output; + +set c 1, +eval, +output; diff --git a/projects/02/HalfAdder.cmp b/projects/02/HalfAdder.cmp new file mode 100644 index 0000000..911c770 --- /dev/null +++ b/projects/02/HalfAdder.cmp @@ -0,0 +1,5 @@ +| a | b | sum | carry | +| 0 | 0 | 0 | 0 | +| 0 | 1 | 1 | 0 | +| 1 | 0 | 1 | 0 | +| 1 | 1 | 0 | 1 | diff --git a/projects/02/HalfAdder.hdl b/projects/02/HalfAdder.hdl new file mode 100644 index 0000000..72476e0 --- /dev/null +++ b/projects/02/HalfAdder.hdl @@ -0,0 +1,18 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/HalfAdder.hdl + +/** + * 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); +} diff --git a/projects/02/HalfAdder.out b/projects/02/HalfAdder.out new file mode 100644 index 0000000..612c8e3 --- /dev/null +++ b/projects/02/HalfAdder.out @@ -0,0 +1,5 @@ +| a | b | sum | carry | +| 0 | 0 | 0 | 0 | +| 0 | 1 | 1 | 0 | +| 1 | 0 | 1 | 0 | +| 1 | 1 | 0 | 1 | diff --git a/projects/02/HalfAdder.tst b/projects/02/HalfAdder.tst new file mode 100644 index 0000000..069b8ea --- /dev/null +++ b/projects/02/HalfAdder.tst @@ -0,0 +1,29 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/HalfAdder.tst + +load HalfAdder.hdl, +output-file HalfAdder.out, +compare-to HalfAdder.cmp, +output-list a%B3.1.3 b%B3.1.3 sum%B3.1.3 carry%B3.1.3; + +set a 0, +set b 0, +eval, +output; + +set a 0, +set b 1, +eval, +output; + +set a 1, +set b 0, +eval, +output; + +set a 1, +set b 1, +eval, +output; diff --git a/projects/02/Inc16.cmp b/projects/02/Inc16.cmp new file mode 100644 index 0000000..451f563 --- /dev/null +++ b/projects/02/Inc16.cmp @@ -0,0 +1,5 @@ +| in | out | +| 0000000000000000 | 0000000000000001 | +| 1111111111111111 | 0000000000000000 | +| 0000000000000101 | 0000000000000110 | +| 1111111111111011 | 1111111111111100 | diff --git a/projects/02/Inc16.hdl b/projects/02/Inc16.hdl new file mode 100644 index 0000000..6b27473 --- /dev/null +++ b/projects/02/Inc16.hdl @@ -0,0 +1,32 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/Inc16.hdl + +/** + * 16-bit incrementer: + * out = in + 1 (arithmetic addition) + */ + +CHIP Inc16 { + IN in[16]; + OUT out[16]; + + PARTS: + Not(in=in[0], out=out[0]); + HalfAdder(a=in[1], b=in[0], sum=out[1], carry=c1); + HalfAdder(a=in[2], b=c1, sum=out[2], carry=c2); + HalfAdder(a=in[3], b=c2, sum=out[3], carry=c3); + HalfAdder(a=in[4], b=c3, sum=out[4], carry=c4); + HalfAdder(a=in[5], b=c4, sum=out[5], carry=c5); + HalfAdder(a=in[6], b=c5, sum=out[6], carry=c6); + HalfAdder(a=in[7], b=c6, sum=out[7], carry=c7); + HalfAdder(a=in[8], b=c7, sum=out[8], carry=c8); + HalfAdder(a=in[9], b=c8, sum=out[9], carry=c9); + HalfAdder(a=in[10], b=c9, sum=out[10], carry=c10); + HalfAdder(a=in[11], b=c10, sum=out[11], carry=c11); + HalfAdder(a=in[12], b=c11, sum=out[12], carry=c12); + HalfAdder(a=in[13], b=c12, sum=out[13], carry=c13); + HalfAdder(a=in[14], b=c13, sum=out[14], carry=c14); + HalfAdder(a=in[15], b=c14, sum=out[15], carry=c15); +} diff --git a/projects/02/Inc16.out b/projects/02/Inc16.out new file mode 100644 index 0000000..78de1fe --- /dev/null +++ b/projects/02/Inc16.out @@ -0,0 +1,5 @@ +| in | out | +| 0000000000000000 | 0000000000000001 | +| 1111111111111111 | 0000000000000000 | +| 0000000000000101 | 0000000000000110 | +| 1111111111111011 | 1111111111111100 | diff --git a/projects/02/Inc16.tst b/projects/02/Inc16.tst new file mode 100644 index 0000000..8537d86 --- /dev/null +++ b/projects/02/Inc16.tst @@ -0,0 +1,25 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/02/Inc16.tst + +load Inc16.hdl, +output-file Inc16.out, +compare-to Inc16.cmp, +output-list in%B1.16.1 out%B1.16.1; + +set in %B0000000000000000, // in = 0 +eval, +output; + +set in %B1111111111111111, // in = -1 +eval, +output; + +set in %B0000000000000101, // in = 5 +eval, +output; + +set in %B1111111111111011, // in = -5 +eval, +output; -- cgit v1.2.3