Nand2Tetris Chapter5
2022. 9. 22. 20:09ㆍComputer Science/CPU
Nand2Tetris : https://www.coursera.org/learn/build-a-computer
Build a Modern Computer from First Principles: From Nand to Tetris (Project-Centered Course)
히브리대학교에서 제공합니다. What you’ll achieve: In this project-centered course* you will build a modern computer system, from the ground up. We’ll divide ... 무료로 등록하십시오.
www.coursera.org
Chapter 5
Central Processing Unit
Chapter 5에서는 Memory, CPU, Computer를 만든다.
이번 챕터는 개인적으로 너무너무 어려웠다 ㅜㅜㅜ
1. Memory
// 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/05/Memory.hdl
/**
* The complete address space of the Hack computer's memory,
* including RAM and memory-mapped I/O.
* The chip facilitates read and write operations, as follows:
* Read: out(t) = Memory[address(t)](t)
* Write: if load(t-1) then Memory[address(t-1)](t) = in(t-1)
* In words: the chip always outputs the value stored at the memory
* location specified by address. If load==1, the in value is loaded
* into the memory location specified by address. This value becomes
* available through the out output from the next time step onward.
* Address space rules:
* Only the upper 16K+8K+1 words of the Memory chip are used.
* Access to address>0x6000 is invalid. Access to any address in
* the range 0x4000-0x5FFF results in accessing the screen memory
* map. Access to address 0x6000 results in accessing the keyboard
* memory map. The behavior in these addresses is described in the
* Screen and Keyboard chip specifications given in the book.
*/
CHIP Memory {
IN in[16], load, address[15];
OUT out[16];
PARTS:
DMux(in=load, sel=address[14], a=load2, b=load1);
Screen(in=in, load=load1, address=address[0..12], out=Scrout);
Keyboard(out=Keyout);
Mux16(a=Scrout, b=Keyout, sel=address[13], out=specialout);
RAM16K(in=in, load=load2, address=address[0..13], out=commonout);
Mux16(a=commonout, b=specialout, sel=address[14], out=out);
}
2. CPU
// 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/05/CPU.hdl
/**
* The Hack CPU (Central Processing unit), consisting of an ALU,
* two registers named A and D, and a program counter named PC.
* The CPU is designed to fetch and execute instructions written in
* the Hack machine language. In particular, functions as follows:
* Executes the inputted instruction according to the Hack machine
* language specification. The D and A in the language specification
* refer to CPU-resident registers, while M refers to the external
* memory location addressed by A, i.e. to Memory[A]. The inM input
* holds the value of this location. If the current instruction needs
* to write a value to M, the value is placed in outM, the address
* of the target location is placed in the addressM output, and the
* writeM control bit is asserted. (When writeM==0, any value may
* appear in outM). The outM and writeM outputs are combinational:
* they are affected instantaneously by the execution of the current
* instruction. The addressM and pc outputs are clocked: although they
* are affected by the execution of the current instruction, they commit
* to their new values only in the next time step. If reset==1 then the
* CPU jumps to address 0 (i.e. pc is set to 0 in next time step) rather
* than to the address resulting from executing the current instruction.
*/
// A code 0~~~~~~ : ~만큼 A에 들어감
// C code 111accccccdddjjj :
// a : 계산시 A대신 M
// c : ALU에 넣을 계산의 방법
// d : dest 각각 ADM
// j : jump 각각 < = >
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 to M?
addressM[15], // Address in data memory (of M)
pc[15]; // address of next instruction
PARTS:
//controlA : A 코드일때만 instruction 선택
Not(in=instruction[15], out=Acode);
Mux16(a=ALUoutput, b=instruction, sel=Acode, out=goA);
Or(a=Acode, b=instruction[5], out=ALoad);
ARegister(in=goA, load=ALoad, out=goPC, out[0..14]=addressM);
And(a=instruction[15], b=instruction[4], out=DLoad);
DRegister(in=ALUoutput, load=DLoad, out=goALUX, out=DRegister);
Mux16(a=goPC, b=inM, sel=instruction[12], out=goALUY);
ALU(x=goALUX, y=goALUY,
zx=instruction[11],
nx=instruction[10],
zy=instruction[9],
ny=instruction[8],
f=instruction[7],
no=instruction[6],
out=ALUoutput,
out=outM,
zr=zr,ng=ng);
And(a=instruction[3], b=instruction[15], out=writeM);
And(a=instruction[2], b=ng, out=PCLoadA);
And(a=instruction[1], b=zr, out=PCLoadB);
Or(a=ng, b=zr, out=ngOrzr);
Not(in=ngOrzr, out=Nornrzr);
And(a=instruction[0], b=Nornrzr, out=PCLoadC);
Or(a=PCLoadA, b=PCLoadB, out=PCLoadAB);
Or(a=PCLoadAB, b=PCLoadC, out=PCLoadABC);
And (a=instruction[15], b=PCLoadABC, out=PCLoad);
PC(in=goPC, load=PCLoad, inc=true, reset=reset, out[0..14]=pc);
}
3. Computer
// 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/05/Computer.hdl
/**
* The HACK computer, including CPU, ROM and RAM.
* When reset is 0, the program stored in the computer's ROM executes.
* When reset is 1, the execution of the program restarts.
* Thus, to start a program's execution, reset must be pushed "up" (1)
* and "down" (0). From this point onward the user is at the mercy of
* the software. In particular, depending on the program's code, the
* screen may show some output and the user may be able to interact
* with the computer via the keyboard.
*/
CHIP Computer {
IN reset;
PARTS:
CPU(inM=inM, instruction=instruction, reset=reset,
outM=outM, writeM=writeM, addressM=addressM, pc=pc);
ROM32K(address=pc, out=instruction);
Memory(in=outM, load=writeM, address=addressM, out=inM);
}
'Computer Science > CPU' 카테고리의 다른 글
Nand2Tetris Chapter7 (0) | 2022.10.21 |
---|---|
Nand2Tetris Chapter6 (0) | 2022.10.10 |
Nand2Tetris Chapter4 (0) | 2022.09.20 |
Nand2Tetris Chapter3 (0) | 2022.09.18 |
Nand2Tetris Chapter 2 (0) | 2022.09.17 |