计算机系统要素:第五章 计算机体系结构

来源:互联网 发布:酷狗软件 编辑:程序博客网 时间:2024/05/16 17:28

这篇文章是该书第五章Computer Architecture的解析。这一章结合了前面所有的内容,最终需要我们构建出完整的Computer单元芯片。


在完成这一章的时候,我在调试过程中常常因为一个小错误耽误很久,费很多精力才能把问题解决,所以贴出代码给大家参考,但希望大家不要依赖答案,重要的是搭建CPU和调试中的思考过程。


注:instruction的标号为0到15,其中instruction[15]表示最前面指代A/C指令的一位。


1:CPU.hdl

CHIP CPU {    IN  inM[16],         // M value input  (M = contents of RAM[A])        instruction[16], // Instruction for execution        reset;           // Signals whether to re-start the current                         // program (reset=1) or continue executing                         // the current program (reset=0).    OUT outM[16],        // M value output        writeM,          // Write into M?         addressM[15],    // Address in data memory (of M)        pc[15];          // address of next instruction    PARTS:    // Put your code here:Mux16(a=instruction,b=outALU,sel=instruction[15],out=outMux1);//Register A(是否存入A取决于ins[15]与ins[5])Not(in=instruction[15],out=notAnd);<span style="font-family: Arial, Helvetica, sans-serif;"></span>Or(a=notAnd,b=instruction[5],out=outOr);ARegister(in=outMux1,load=outOr,out=outA,out[0..14]=addressM);//DAnd(a=instruction[15],b=instruction[4],out=loadD);DRegister(in=outALU,load=loadD,out=outD);//Mux A&MMux16(a=outA,b=inM,sel=instruction[12],out=outAM);//ALUALU(x=outD,y=outAM,zx=instruction[11],nx=instruction[10],zy=instruction[9], ny=instruction[8], f=instruction[7], no=instruction[6],out=outALU,out=outM,zr=zr,ng=ng);//Mux writeMAnd(a=instruction[3],b=instruction[15],out=writeM);//PC(在判断jump指令时,由于ALU只能生成表示结果为负数或零的两个参数zr、ng,所以还先得到一个表示正数的参数ps,然后将其与jump中的参数相比较,只要有一个参数对应,那么便可以跳转)Not(in=zr,out=notzr);Not(in=ng,out=notng);And(a=notzr,b=notng,out=ps);And(a=ng,b=instruction[2],out=and1);And(a=zr,b=instruction[1],out=and2);And(a=ps,b=instruction[0],out=and3);Or(a=and1,b=and2,out=or1);Or(a=or1,b=and3,out=or2);//禁止A指令时PC采取load行为(非常重要!否则会重复读取指令!)And(a=or2,b=instruction[15],out=outAnd3); PC(in=outAM,load=outAnd3,reset=reset,inc=true,out[0..14]=pc);}

2:Memory.hdl

CHIP Memory {    IN in[16], load, address[15];    OUT out[16];    PARTS:    // Put your code here:Mux(a=load, b=false, sel=address[14], out=rload);  //address[14]=0时,RAM的load有效Mux(a=false, b=load, sel=address[14], out=sload); //address[14]=0时,Screen的load无效RAM16K(in=in, load=rload, address=address[0..13], out=rout);Screen(in=in, load=sload, address=address[0..12], out=sout);Keyboard(out=kout);Mux16(a=sout, b=kout, sel=address[13], out=hout);Mux16(a=rout, b=hout, sel=address[14], out=out);}

3:Computer.hdl

CHIP Computer {    IN reset;    PARTS:    // Put your code here:<span style="white-space:pre"></span>CPU(inM=outM0,instruction=outins,reset=reset,writeM=writeM,outM=outM,addressM=addressM,pc=pc);Memory(in=outM,load=writeM,address=addressM,out=outM0);ROM32K(address=pc,out=outins);}


至此,《计算机系统要素》硬件部分全部结束。从连Nand门是什么都不知道,到亲自动手构建出了Computer芯片,看着一行行01码按着自己的芯片完成指令,这个过程真的超有成就感!可以说种学习体验这胜过我大学所有的计算机课程。也希望所有人都能从学习中收获快乐!


参考资料:

http://blog.csdn.net/tianzhhy/article/details/5807498

《计算机系统要素》一到五章代码


0 0