《计算机系统要素学习》笔记之第5章 计算机体系结构
- 冯诺依曼体系结构:由一个中央处理单元(CPU)与记忆设备(memory device)即内存进行交互,负责从输入设备(input device)接收数据,向输出设备(output device)发送数据。
- 内存(Memory):数据内存(Data Memory) 指令内存(Instruction Memory) 中央处理器(CPU):由算术逻辑单元(ALU),寄存器(Registers),控制单元(Control Unit)组成
- 寄存器(Registers):数据寄存器(Data Registers) 寻址寄存器(Addressing Registers)
程序计数寄存器(Program Counter Registers) - 输入和输出(Input and output):通过I/O映像到内存空间实现
- Hack硬件平台实现
- Memory
DMux(in=load,sel=address[14],a=l1,b=l2);
DMux(in=l2,sel=address[13],a=l21,b=l22);
DMux(in=l22,sel=address[0],a=l3,b=l4);
RAM16K(in=in,load=l1,address=address[0…13],out=r1);
Screen(in=in,load=l21,address=address[0…12],out=r2);
Keyboard(out=r3);
Mux16(a=r2,b=r3,sel=address[13],out=out1);
Mux16(a=r1,b=out1,sel=address[14],out=out); - CPU
//********** Decoder实现**********//
CHIP Decoder {
IN in[16];
OUT m1,m2,a,d,m,jump[3],
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 out = x & y (if 0)
no; // negate the out output?
PARTS:
//Mux1 Part
Mux(a=true,b=false,sel=in[15],out=m1);
//A Part
Mux(a=false,b=true,sel=in[5],out=ar2);
Mux(a=true,b=ar2,sel=in[15],out=a);
//D Part
Mux(a=false,b=true,sel=in[4],out=dr2);
Mux(a=false,b=dr2,sel=in[15],out=d);
//M Part
Mux(a=false,b=true,sel=in[3],out=mr);
Mux(a=false,b=mr,sel=in[15],out=m);
//Mux2 Part
Mux(a=false,b=true,sel=in[12],out=mr2);
Mux(a=false,b=mr2,sel=in[15],out=m2);
//ALU Part
Mux(a=in[6],b=in[6],sel=true,out=no);
Mux(a=in[7],b=in[7],sel=true,out=f);
Mux(a=in[8],b=in[8],sel=true,out=ny);
Mux(a=in[9],b=in[9],sel=true,out=zy);
Mux(a=in[10],b=in[10],sel=true,out=nx);
Mux(a=in[11],b=in[11],sel=true,out=zx);
//Jump Part
Mux(a=in[0],b=in[0],sel=true,out=jump[0]);
Mux(a=in[1],b=in[1],sel=true,out=jump[1]);
Mux(a=in[2],b=in[2],sel=true,out=jump[2]);
}
//********** Select15实现**********//
CHIP Select15 {
IN in[16];
OUT out[15];
PARTS:
Mux(a=in[0],b=in[0],sel=true,out=out[0]);
Mux(a=in[1],b=in[1],sel=true,out=out[1]);
Mux(a=in[2],b=in[2],sel=true,out=out[2]);
Mux(a=in[3],b=in[3],sel=true,out=out[3]);
Mux(a=in[4],b=in[4],sel=true,out=out[4]);
Mux(a=in[5],b=in[5],sel=true,out=out[5]);
Mux(a=in[6],b=in[6],sel=true,out=out[6]);
Mux(a=in[7],b=in[7],sel=true,out=out[7]);
Mux(a=in[8],b=in[8],sel=true,out=out[8]);
Mux(a=in[9],b=in[9],sel=true,out=out[9]);
Mux(a=in[10],b=in[10],sel=true,out=out[10]);
Mux(a=in[11],b=in[11],sel=true,out=out[11]);
Mux(a=in[12],b=in[12],sel=true,out=out[12]);
Mux(a=in[13],b=in[13],sel=true,out=out[13]);
Mux(a=in[14],b=in[14],sel=true,out=out[14]);
}
//********** Nextfetch实现**********//
CHIP Nextfetch {
IN zr,ng,jump[3];
OUT load;
PARTS:
//out<0
And(a=ng,b=jump[2],out=jump1);
Mux(a=load1,b=true,sel=jump1,out=load);
//out=0
And(a=zr,b=jump[1],out=jump2);
Mux(a=load2,b=true,sel=jump2,out=load1);
//out>0
Xor(a=true,b=ng,out=xor1);
Xor(a=true,b=zr,out=xor2);
And(a=xor1,b=xor2,out=and3);
And(a=and3,b=jump[0],out=jump3);
Mux(a=false,b=true,sel=jump3,out=load2);
}
//CPU-PARTS//
//Decoder
Decoder(in=instruction,m1=mux1,m2=mux2,a=ar,d=dr,m=mr,jump=jump1,
zx=zx1, // zero the x input?
nx=nx1, // negate the x input?
zy=zy1, // zero the y input?
ny=ny1, // negate the y input?
f=f1, // compute out = x + y (if 1) or out = x & y (if 0)
no=no1); // negate the out output?
DRegister(in=aluout,load=dr,out=dout);
ALU(x=dout,y=out2,zx=zx1,nx=nx1,zy=zy1,ny=ny1,f=f1,no=no1,out=aluout,zr=zr1,ng=ng1);
ARegister(in=out1,load=ar,out=aout);
Mux16(a=aout,b=inM,sel=mux2,out=out2);
// next instruction fetching
Nextfetch(zr=zr1,ng=ng1,jump=jump1,load=pcload);
Mux(a=false,b=pcload,sel=instruction[15],out=pcload1);
//PC Part
PC(in=aout,load=pcload1,inc=true,reset=reset,out=pcout);
Select15(in=aout,out=addressM);
Select15(in=pcout,out=pc);
Mux16(a=aluout,b=false,sel=false,out=outM);
Mux(a=mr,b=false,sel=false,out=writeM);
Mux16(a=aluout,b=instruction,sel=mux1,out=out1); - Computer
ROM32K(address=PC,out=rom32k);
CPU(inM=mmr,instruction=rom32k,reset=reset,outM=otm,writeM=wrt,addressM=addrm,pc=PC);
Memory(in=otm,load=wrt,address=addrm,out=mmr);