【Nand2tetris】Project01

nand门来实现15个simple chips

a Nand b = Not(a And b)

Nand Truth Table:

a b a Nand b
0 0 1
0 1 1
1 0 1
1 1 0

1.Not

Not x = x Nand x

// 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/01/Not.hdl/*** Not gate:* out = not in*/CHIP Not {IN in;OUT out;PARTS:// Put your code here:Nand(a=in,b=in,out=out);
}

2. And

x Nand x = Not x

x Nand y =Not(x And y)

x and y = Nand(x And y) Nand ( Nand(x And y) );

// 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/01/And.hdl/*** And gate: * out = 1 if (a == 1 and b == 1)*       0 otherwise*/CHIP And {IN a, b;OUT out;PARTS:// Put your code here://solution1:Nand(a=a,b=b,out=tmp);Nand(a=tmp,b=tmp,out=out);//solution2(use Not)://Nand(a=a,b=b,out=tmp);//Not(in=tmp,out=out);
}

3.or

x or y = not(not x and not y)

 /*** Or gate:* out = 1 if (a == 1 or b == 1)*       0 otherwise*/
CHIP Or {IN a, b;OUT out;PARTS:// Put your code here:Not(in = a, out = nota);Not(in = b, out = notb);And(a=nota,b=notb,out=tmp);Not(in=tmp,out=out);
}

4.xor

Xor=(a and not b )or(not a and b)

/*** Exclusive-or gate:* out = not (a == b)*/
CHIP Xor {IN a, b;OUT out;PARTS:// Put your code here:Not(in=a,out=nota);Not(in=b,out=notb);And(a=a,b=notb,out=w1);And(a=nota,b=b,out=w2);Or(a=w1,b=w2,out=out);
}

5.Mux

/** * Multiplexor:* out = a if sel == 0*       b otherwise*/// solution:Mux=Or(And(a,Not(sel),And(b,sel))
CHIP Mux {IN a, b, sel;OUT out;PARTS:// Put your code here:Not(in=sel,out=notsel);And(a=a,b=notsel,out=w1);And(a=b,b=sel,out=w2);Or(a=w1,b=w2,out=out);
}

6.DMux

/*** Demultiplexor:* {a, b} = {in, 0} if sel == 0*          {0, in} if sel == 1*/
//solution: a=And(in,Not(sel)),b=And(in,sel)
CHIP DMux {IN in, sel;OUT a, b;PARTS:// Put your code here:Not(in=sel,out=notsel);And(a=in,b=notsel,out=a);And(a=in,b=sel,out=b);
}

7.Not16

/*** 16-bit Not:* for i=0..15: out[i] = not in[i]*/CHIP Not16 {IN in[16];OUT out[16];PARTS:// Put your code here: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]);
}

8.And16

/*** 16-bit bitwise And:* for i = 0..15: out[i] = (a[i] and b[i])*/CHIP And16 {IN a[16], b[16];OUT out[16];PARTS:// Put your code here: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]);
}

9.Or16

/*** 16-bit bitwise Or:* for i = 0..15 out[i] = (a[i] or b[i])*/CHIP Or16 {IN a[16], b[16];OUT out[16];PARTS:// Put your code here: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]);
}

10.Mux16

/*** 16-bit multiplexor: * for i = 0..15 out[i] = a[i] if sel == 0 *                        b[i] if sel == 1*/CHIP Mux16 {IN a[16], b[16], sel;OUT out[16];PARTS:// Put your code here:Mux(a=a[0],b=b[0],sel=sel,out=out[0]);Mux(a=a[1],b=b[1],sel=sel,out=out[1]);Mux(a=a[2],b=b[2],sel=sel,out=out[2]);Mux(a=a[3],b=b[3],sel=sel,out=out[3]);Mux(a=a[4],b=b[4],sel=sel,out=out[4]);Mux(a=a[5],b=b[5],sel=sel,out=out[5]);Mux(a=a[6],b=b[6],sel=sel,out=out[6]);Mux(a=a[7],b=b[7],sel=sel,out=out[7]);Mux(a=a[8],b=b[8],sel=sel,out=out[8]);Mux(a=a[9],b=b[9],sel=sel,out=out[9]);Mux(a=a[10],b=b[10],sel=sel,out=out[10]);Mux(a=a[11],b=b[11],sel=sel,out=out[11]);Mux(a=a[12],b=b[12],sel=sel,out=out[12]);Mux(a=a[13],b=b[13],sel=sel,out=out[13]);Mux(a=a[14],b=b[14],sel=sel,out=out[14]);Mux(a=a[15],b=b[15],sel=sel,out=out[15]);
}

11.Or8Way

/*** 8-way Or: * out = (in[0] or in[1] or ... or in[7])*/CHIP Or8Way {IN in[8];OUT out;PARTS:// Put your code here:Or(a=in[0],b=in[1],out=w1);Or(a=in[2],b=in[3],out=w2);Or(a=in[4],b=in[5],out=w3);Or(a=in[6],b=in[7],out=w4);Or(a=w1,b=w2,out=tmp1);Or(a=w3,b=w4,out=tmp2);Or(a=tmp1,b=tmp2,out=out);
}

12.Mux4Way16

CHIP Mux4Way16 {IN a[16], b[16], c[16], d[16], sel[2];OUT out[16];PARTS:// Put your code here:Or(a=sel[0],b=sel[1],out=flag1);And(a=sel[0],b=sel[1],out=flag2);Mux16(a=a,b=b,sel=flag1,out=choice1);Mux16(a=c,b=d,sel=flag2,out=choice2);Mux16(a=choice1,b=choice2,sel=sel[1],out=out);
}
sel[1] sel[0] a b c d
0 0 out
0 1 out
1 0 out
1 1 out

思路:

  1. a和b用Mux16选一次,c和d用Mux16选一次
  2. 再将两次的结果选一次
  3. a和b靠flag1=Or(sel[0],sel[1])选
  4. c和d靠flag2=And(sel[0],sel[1])选————==三四简化为靠sel[0]选择即可==之前想复杂了
  5. 两次的结果依靠sel[1]选

13. Mux8Way16

/*** 8-way 16-bit multiplexor:* out = a if sel == 000*       b if sel == 001*       etc.*       h if sel == 111*/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:// Put your code here:Mux16(a=a,b=b,sel=sel[0],out=choice1);Mux16(a=c,b=d,sel=sel[0],out=choice2);Mux16(a=e,b=f,sel=sel[0],out=choice3);Mux16(a=g,b=h,sel=sel[0],out=choice4);Mux16(a=choice1,b=choice2,sel=sel[1],out=w1);Mux16(a=choice3,b=choice4,sel=sel[1],out=w2);Mux16(a=w1,b=w2,sel=sel[2],out=out);
}

按顺序分四组各选一个,把选出的按顺序再分成两组再选,最后两中再选

每次组选择的sel分别是sel[3]的从0开始的每一位

simplified version:(use Mux4Way16)

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:// Put your code here:Mux4Way16(a=a,b=b,c=c,d=d,sel=sel[0..1],out=w1);Mux4Way16(a=e,b=f,c=g,d=h,sel=sel[0..1],out=w2);Mux16(a=w1,b=w2,sel=sel[2],out=out);
}

14.DMux4Way

有二分的想法:先依靠sel[1]判断in在前面2个中还是后面2个中

/*** 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*///用sel[1]把一二和三四分开//再用sel[0]把两组内的分开
CHIP DMux4Way {IN in, sel[2];OUT a, b, c, d;PARTS:// Put your code here:DMux(in=in,sel=sel[1],a=tmp1,b=tmp2);DMux(in=tmp1,sel=sel[0],a=a,b=b);DMux(in=tmp2,sel=sel[0],a=c,b=d);
}

15.DMux8Way

/*** 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*                            etc.*                            {0, 0, 0, 0, 0, 0, 0, in} if sel == 111*///类比DMux4Way的做法,依靠sel[2]把in分出两份,再利用已经完成的DMux4Way
CHIP DMux8Way {IN in, sel[3];OUT a, b, c, d, e, f, g, h;PARTS:// Put your code here:DMux(in=in,sel=sel[2],a=tmp1,b=tmp2);DMux4Way(in=tmp1,sel=sel[0..1],a=a,b=b,c=c,d=d);DMux4Way(in=tmp2,sel=sel[0..1],a=e,b=f,c=g,d=h);
}

【Nand2tetris】Project01相关推荐

  1. 【Nand2tetris】Project02

    [Nand2tetris]Project02 1.Half Adder sum(a,b) -----> Xor(a,b) carry(a,b)----->And(a,b) CHIP Hal ...

  2. 【CentOS】利用Kubeadm部署Kubernetes (K8s)

    [CentOS]利用Kubeadm部署Kubernetes (K8s)[阅读时间:约10分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 三.具体实验流程 1 系统准 ...

  3. 【Spring】框架简介

    [Spring]框架简介 Spring是什么 Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:反转控制)和AOP(Asp ...

  4. 【C#】类——里式转换

    类是由面对对象程序设计中产生的,在面向结构的程序设计例如C语言中是没有类这个概念的!C语言中有传值调用和传址调用的两种方式!在c语言中,主方法调用方法,通过传递参数等完成一些操作,其中比较常用的的数据 ...

  5. 【C#】Out与ref是干什么的?

    关于return: 1.最后没有写 return 语句的话,表示程序正常退出 2.不需要返回值时,存在return的作用 例子 void main() {return; //return退出该程序的作 ...

  6. 【软件工程】RUP与软件开发5大模型

    软件开发的5大模型 1.瀑布模型:按照人的思维一步一步的开发下去,如果需求分析得当,每个阶段顺利,结果还不错! 2.快速原型模型:后来人们发现,自己不可能一下子就把所有的需求搞清楚,总是在开发的过程中 ...

  7. 【VB】学生信息管理系统5——数据库代码

    这次学生信息管理系统在代码的理解过程中遇到了一些问题.总结如下: 1. sql server的安装过程各个步骤的意思.在安装SQL Server的时候按照网上的步骤,我觉得这个需要学完整个数据库再返回 ...

  8. 白化(预处理步骤)【转】

    白化(预处理步骤)[转] 介绍 我们已经了解了如何使用PCA降低数据维度.在一些算法中还需要一个与之相关的预处理步骤,这个预处理过程称为白化.举例来说,假设训练数据是图像,由于图像中相邻像素之间具有很 ...

  9. 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积

    介绍 关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论: 1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional networ ...

最新文章

  1. 为什么双层循环 冒泡排序_冒泡排序的双重循环理解
  2. c 自动生成html报告,Pytest框架之 - Allure生成漂亮的HTML图形测试报告
  3. C++中如何初始化类中const或引用类型的数据成员?
  4. 倒排索引 - C/C++
  5. 飞畅科技-专业交换机厂家解读市场对工业交换机产品的要求有哪些?
  6. jQuery之ajax的跨域获取数据
  7. delphi 生成 超大量xml_用OpenCV4实现图像的超分别率
  8. mac 10.13 配置 php,MacOS10.13.6 升级后 PHP7.3配置
  9. python sklearn: 模型(如 SVM,PCA等)的保存与加载调用
  10. Integer的自动装箱底层缓存原理
  11. Microsoft 补丁下载
  12. Python爬虫:爬取网站视频
  13. Screenie for Mac(Mac截图工具)
  14. 后直播时代的技术弄潮儿——TRTC
  15. Qmail加装自动杀毒(转)
  16. 为谷歌浏览器Chrome创建多个用户
  17. 苹果系列手机往电脑上传照片,有什么方法怎么传
  18. 研究生毕业论文如何选题
  19. Couldn‘t flush user prefs: java.util.prefs.BackingStoreException: Couldn‘t get file lock
  20. 自动点击网页脚本---selenium库使用

热门文章

  1. 职业综合英语 章节答案考试答案 深圳职业技术学院[渝粤教育]
  2. 移动端VUE实现一周课程表
  3. 【iOS】Plist-XML-JSON数据解析
  4. 队爷的讲学计划 (强连通缩点+最短路)
  5. ear的英语怎么念_ear英语怎么读谐音
  6. h5怎么跟mysql进行交互_H5活动有哪些交互形式
  7. 斯皮尔 皮尔森 肯德尔_失焦图像的无参考质量评价
  8. wifi(华硕天选2)找不到怎么办
  9. 基于高光谱成像的苹果虫害检测特征向量的选取
  10. 用HTML+CSS简单做了张简历表格