【Nand2tetris】Project02

1.Half Adder

sum(a,b) -----> Xor(a,b)

carry(a,b)----->And(a,b)

CHIP HalfAdder {IN a, b;    // 1-bit inputsOUT sum,    // Right bit of a + b carry;  // Left bit of a + bPARTS:// Put you code here:Xor(a=a,b=b,out=sum);And(a=a,b=b,out=carry);
}

2.Full Adder

CHIP FullAdder {IN a, b, c;  // 1-bit inputsOUT sum,     // Right bit of a + b + ccarry;   // Left bit of a + b + cPARTS:// Put you code here:HalfAdder(a=a,b=b,sum=tmp,carry=c1);HalfAdder(a=tmp,b=c,sum=sum,carry=c2);Or(a=c1,b=c2,out=carry);
}

3. 16-bit adder

CHIP Add16 {IN a[16], b[16];OUT out[16];PARTS:// Put you code here: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);
}

4. 16-bit incrementor(16位递增器)

/*** 16-bit incrementer:* out = in + 1 (arithmetic addition)*/
//in 16bit true:1111111111111111
CHIP Inc16 {IN in[16];OUT out[16];PARTS:// Put you code here:Add16(a=in,b[1..15]=false,b[0]=true,out=out);
}

5.ALU(The Arithmetic Logic Unit)(算术逻辑单元)

主要思考如何去实现IF,通过之前构建的Mux来选择

/*** 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 = 1CHIP 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 outputzr, // 1 if (out == 0), 0 otherwiseng; // 1 if (out < 0),  0 otherwisePARTS:// Put you code here://1.根据zx处理xMux16(a=x,b=false,sel=zx,out=x1);//2.根据nx处理xNot16(in=x1,out=nx1);Mux16(a=x1,b=nx1,sel=nx,out=x2);//3.根据zy处理yMux16(a=y,b=false,sel=zy,out=y1);//4.根据ny处理yNot16(in=y1,out=ny1);Mux16(a=y1,b=ny1,sel=ny,out=y2);//5.根据f来选择And or AddAdd16(a=x2,b=y2,out=xpy);And16(a=x2,b=y2,out=xay);Mux16(a=xay,b=xpy,sel=f,out=out1);//6.根据no处理outNot16(in=out1,out=nout1);Mux16(a=out1,b=nout1,sel=no,out=out,out=copy,out[15]=flag);//flag记录最左边符号位//7.由输出out来确定zr,ng//trick: 16位copy不能通过copy[0..7]来获取八位,用line63的技巧把copy分流成tmp1,tmp2And16(a=copy,b=true,out[0..7]=tmp1,out[8..15]=tmp2);Or8Way(in=tmp1,out=o1);Or8Way(in=tmp2,out=o2);Or(a=o1,b=o2,out=flagzr);Mux(a=true,b=false,sel=flagzr,out=zr);Mux(a=false,b=true,sel=flag,out=ng);/* error:上面out=out,out不可再利用了,解决:out出来时复制一个备份Or8Way(in=out[0..7],out=o1);Or8Way(in=out[8..15],out=o2);Or(a=o1,b=o2,out=flag);Mux(a=false,b=true,sel=flag,out=zr);Mux(a=false,b=true,sel=out[15],out=ng);
*/
}

【Nand2tetris】Project02相关推荐

  1. 【Nand2tetris】Project01

    [Nand2tetris]Project01 用nand门来实现15个simple chips a Nand b = Not(a And b) Nand Truth Table: a b a Nand ...

  2. 【笔记】Go语言学习笔记

    一.概述­ ­●什么是程序: 程序:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合 ­●Go语言 是区块链最主流的编程语言 同时也是当前最具发展潜力的语言 ­●Go语言是Googl ...

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

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

  4. 【Spring】框架简介

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. 2020年高等数学方法与提高(上海理工大学)学习笔记:多元函数微分学
  2. 观察者模式(Observer) 简介
  3. 《About Multi-Touch(多点触摸是个什么东西?)》:基于光学原理的多点触摸技术全解析...
  4. 【教女朋友学网络系列5】之VLAN与Trunk、单臂路由
  5. 创建数据库连接字符串的快截方法
  6. 阿里巴巴Maven仓库配置
  7. 卸载 windows_Windows 10可能很快会自动卸载有问题的Windows更新
  8. 计算机报名成功后还可以取消吗,【造价工程师报名信息确认后可以取消吗?如何取消?】- 环球网校...
  9. 计算机硬件常识知识,计算机硬件基础知识大全
  10. JAVA小乌龟游泳_描写乌龟游泳的作文
  11. SystemVerilog之interface
  12. Windows NT
  13. 《千与千寻》与《天空之城》配色分享
  14. MAC上Chrome浏览器没有声音
  15. Work20230405
  16. TextRNN+attention
  17. 【Effective C++ 条款03 笔记】尽可能使用const
  18. [2004年旧文]我是如何搞定Linux上的郑码输入法[原创]
  19. 使用\begin{aligned} 出现 Environment aligned undefined.解决办法
  20. 三菱MR-JE-C伺服电机FB功能块 流水线项目,16个MR-JE-C电机

热门文章

  1. 软考报名时间 流程 条件
  2. ThinkPHP开发手册
  3. 工作中遇到的发送报警短信的流程图设计
  4. 计网 应用题、计算题 答案详解 总结(已更完)
  5. 计算机学院迎条幅,会计学院迎新标语条幅
  6. 好好说话之ret2csu
  7. html表格上只有竖边框,html中画表格让表格只有横边框没有竖边框.doc
  8. 单端通用ISM频段接收器 Si4313
  9. 高通平台提高核电电压
  10. Linux文件锁(Filelock)是什么,怎么用?