Skip to content

Commit 9ba5a98

Browse files
committed
project05 done. built CPU, RAM, and then assemble them with ROM together to build a computer
1 parent 0b707d1 commit 9ba5a98

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

05/CPU.hdl

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/05/CPU.hdl
5+
6+
/**
7+
* The Hack CPU (Central Processing unit), consisting of an ALU,
8+
* two registers named A and D, and a program counter named PC.
9+
* The CPU is designed to fetch and execute instructions written in
10+
* the Hack machine language. In particular, functions as follows:
11+
* Executes the inputted instruction according to the Hack machine
12+
* language specification. The D and A in the language specification
13+
* refer to CPU-resident registers, while M refers to the external
14+
* memory location addressed by A, i.e. to Memory[A]. The inM input
15+
* holds the value of this location. If the current instruction needs
16+
* to write a value to M, the value is placed in outM, the address
17+
* of the target location is placed in the addressM output, and the
18+
* writeM control bit is asserted. (When writeM==0, any value may
19+
* appear in outM). The outM and writeM outputs are combinational:
20+
* they are affected instantaneously by the execution of the current
21+
* instruction. The addressM and pc outputs are clocked: although they
22+
* are affected by the execution of the current instruction, they commit
23+
* to their new values only in the next time step. If reset==1 then the
24+
* CPU jumps to address 0 (i.e. pc is set to 0 in next time step) rather
25+
* than to the address resulting from executing the current instruction.
26+
*/
27+
28+
CHIP CPU {
29+
30+
IN inM[16], // M value input (M = contents of RAM[A])
31+
instruction[16], // Instruction for execution
32+
reset; // Signals whether to re-start the current
33+
// program (reset==1) or continue executing
34+
// the current program (reset==0).
35+
36+
OUT outM[16], // M value output
37+
writeM, // Write to M?
38+
addressM[15], // Address in data memory (of M)
39+
pc[15]; // address of next instruction
40+
41+
PARTS:
42+
// Put your code here:
43+
And(a=instruction[15], b=instruction[15], out=isC);
44+
Not(in=instruction[15], out=isA);
45+
46+
// For Cinstruction, if d1==1, means set Aregister as the destination of cmp which is result of ALU, then load it in ARegister
47+
And(a=isC, b=instruction[5], out=aluout2A);
48+
Mux16(a=instruction, b=aluout, sel=aluout2A, out=inAReg);
49+
50+
// if AReg needs to be load, load it
51+
Or(a=isA, b=aluout2A, out=loadAReg);
52+
ARegister(in=inAReg, load=loadAReg, out=ARegout, out[0..14]=addressM);
53+
54+
// define 1 input of ALU
55+
And(a=isC, b=instruction[12], out=aset);
56+
Mux16(a=ARegout, b=inM, sel=aset, out=aluin1);
57+
58+
// whether writeM is set
59+
And(a=isC, b=instruction[3], out=writeM);
60+
61+
// whether write DReg
62+
And(a=isC, b=instruction[4], out=writeDReg);
63+
DRegister(in=aluout, load=writeDReg, out=DRegout);
64+
65+
And(a = isC, b = instruction[6], out = no);
66+
And(a = isC, b = instruction[7], out = f);
67+
And(a = isC, b = instruction[8], out = ny);
68+
And(a = isC, b = instruction[9], out = zy);
69+
And(a = isC, b = instruction[10], out = nx);
70+
And(a = isC, b = instruction[11], out = zx);
71+
72+
ALU(x = DRegout, y = aluin1, zx = zx, nx = nx, zy = zy, ny = ny, f = f, no = no, out = aluout, out = outM, zr=zr, ng=ng);
73+
74+
And(a=isC, b=instruction[0], out=GT);
75+
And(a=isC, b=instruction[1], out=EQ);
76+
And(a=isC, b=instruction[2], out=LT);
77+
78+
And(a=ng, b=LT, out=LTJump);
79+
And(a=zr, b=EQ, out=EQJump);
80+
81+
// output > 0
82+
Not(in = ng, out = notNg);
83+
Not(in = zr, out = notZr);
84+
And(a = notNg, b = notZr, out = outGT);
85+
86+
And(a=outGT, b=GT, out=GTJump);
87+
88+
Or(a=LTJump, b=EQJump, out=jump0);
89+
Or(a=jump0, b=GTJump, out=jump);
90+
91+
PC(in=ARegout, load=jump, inc=true, reset=reset, out[0..14]=pc);
92+
93+
}

05/Computer.hdl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/05/Computer.hdl
5+
6+
/**
7+
* The HACK computer, including CPU, ROM and RAM.
8+
* When reset is 0, the program stored in the computer's ROM executes.
9+
* When reset is 1, the execution of the program restarts.
10+
* Thus, to start a program's execution, reset must be pushed "up" (1)
11+
* and "down" (0). From this point onward the user is at the mercy of
12+
* the software. In particular, depending on the program's code, the
13+
* screen may show some output and the user may be able to interact
14+
* with the computer via the keyboard.
15+
*/
16+
17+
CHIP Computer {
18+
19+
IN reset;
20+
21+
PARTS:
22+
// Put your code here:
23+
CPU(inM=memoryout, instruction=romout, reset=reset, outM=outM, writeM=writeM, addressM=addressM, pc=pc);
24+
Memory(in=outM, load=writeM, address=addressM, out=memoryout);
25+
ROM32K(address=pc, out=romout);
26+
27+
28+
29+
30+
}

05/Memory.hdl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/05/Memory.hdl
5+
6+
/**
7+
* The complete address space of the Hack computer's memory,
8+
* including RAM and memory-mapped I/O.
9+
* The chip facilitates read and write operations, as follows:
10+
* Read: out(t) = Memory[address(t)](t)
11+
* Write: if load(t-1) then Memory[address(t-1)](t) = in(t-1)
12+
* In words: the chip always outputs the value stored at the memory
13+
* location specified by address. If load==1, the in value is loaded
14+
* into the memory location specified by address. This value becomes
15+
* available through the out output from the next time step onward.
16+
* Address space rules:
17+
* Only the upper 16K+8K+1 words of the Memory chip are used.
18+
* Access to address>0x6000 is invalid. Access to any address in
19+
* the range 0x4000-0x5FFF results in accessing the screen memory
20+
* map. Access to address 0x6000 results in accessing the keyboard
21+
* memory map. The behavior in these addresses is described in the
22+
* Screen and Keyboard chip specifications given in the book.
23+
*/
24+
25+
CHIP Memory {
26+
IN in[16], load, address[15];
27+
OUT out[16];
28+
29+
PARTS:
30+
// Put your code here:
31+
DMux4Way(in=load, sel=address[13..14], a=a0, b=a1, c=loadscreen, d=loadkbd);
32+
Or(a=a0, b=a1, out=loadram);
33+
34+
RAM16K(in=in, load=loadram, address=address[0..13], out=outram);
35+
Screen(in=in, load=loadscreen, address=address[0..12], out=outscreen);
36+
Keyboard(out=outkbd);
37+
38+
Mux4Way16(a=outram, b=outram, c=outscreen, d=outkbd, sel=address[13..14], out=out);
39+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ Hack Machine Language programming.
2222

2323
Project05
2424

25-
25+
Built RAM and CPU, and then assemble them wit ROM together to build a computer.
2626

2727

0 commit comments

Comments
 (0)