Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions projects/05/CPU.hdl
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,16 @@ CHIP CPU {
// 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
// i x x a c c c c c c d d d j j j

Decoder(i=instruction[15], dest=instruction[3..5], loadA=load-a, loadD=load-d, writeM=writeM);

// load instruction or computation into A
Mux16(a=instruction, b=alu-out, sel=instruction[15], out=in-a);

Not(in=instruction[15], out=is-a-instruction);
Or(a=is-a-instruction, b=instruction[5], out=load-a);

ARegister(in=in-a, load=load-a, out=a-register, out[0..14]=addressM);

// load A or M into ALU
Mux16(a=a-register, b=inM, sel=instruction[12], out=a-or-m);

And(a=instruction[15], b=instruction[4], out=load-d);

DRegister(in=alu-out, load=load-d, out=d-register);

ALU(
Expand All @@ -74,19 +71,7 @@ CHIP CPU {
ng=negative
);

Or(a=negative, b=zero, out=not-positive);
Not(in=not-positive, out=positive);

And(a=instruction[2], b=negative, out=jump-less-than);
And(a=instruction[1], b=zero, out=jump-equal-to);
And(a=instruction[0], b=positive, out=jump-greater-than);

Or(a=jump-less-than, b=jump-equal-to, out=jump-first);
Or(a=jump-first, b=jump-greater-than, out=jump-second);
And(a=jump-second, b=instruction[15], out=jump);
Jump(i=instruction[15], j=instruction[0..2], ng=negative, zr=zero, out=jump);

PC(in=a-register, load=jump, inc=true, reset=reset, out[0..14]=pc);

// Only write to memory if this is a computation instruction
And(a=instruction[15], b=instruction[3], out=writeM);
}
16 changes: 16 additions & 0 deletions projects/05/Decoder.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CHIP Decoder {
IN i, // Instruction type
dest[3]; // Instruction destination

OUT loadA, // 1 if loading A, 0 otherwise
loadD, // 1 if loading D, 0 otherwise
writeM; // Write to M?

PARTS:
Not(in=i, out=is-a-instruction);
Or(a=is-a-instruction, b=dest[2], out=loadA);

And(a=i, b=dest[1], out=loadD);

And(a=i, b=dest[0], out=writeM);
}
19 changes: 19 additions & 0 deletions projects/05/Jump.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CHIP Jump {
IN i, // Instruction type
j[3], // Jump instructions
ng, // ng output of the ALU
zr; // zr output of the ALU

OUT out; // 1 if jumping, 0 otherwise

PARTS:
Or(a=ng, b=zr, out=not-positive);
Not(in=not-positive, out=positive);

And(a=j[2], b=ng, out=jlt);
And(a=j[1], b=zr, out=jeq);
And(a=j[0], b=positive, out=jgt);
Or(a=jlt, b=jeq, out=jle);
Or(a=jle, b=jgt, out=jump);
And(a=jump, b=i, out=out);
}