feat: cpu, memory, computer hdl

This commit is contained in:
2026-02-10 22:25:41 +01:00
parent a06f7dd256
commit 10ef9e6d34
3 changed files with 86 additions and 5 deletions

View File

@@ -71,7 +71,12 @@ CHIP CPU {
And(a=isCInstruction, b=d1, out=loadAFromALU);
Mux16(a=instruction, b=aluOut, sel=loadAFromALU,out=aRegIn);
Or(a=isAInstruction, b=loadAFromALU, out=loadA);
Register(in=aRegIn, load=loadA, out=aRegOut);
Register(in=aRegIn,
load=loadA,
// Out
out=aRegOut,
out[0..14]=addressM
);
// D Register
And(a=isCInstruction, b=d2, out=loadD);
@@ -84,12 +89,44 @@ CHIP CPU {
ALU(x=dRegOut,
y=aluYin,
zx=c1, nx=c2, zy=c3, ny=c4 , f=c5 , no=c6 ,
out=aluOut ,
zr=aluZrOut ,
ng=aluNgOut
zr=aluZrOut,
ng=aluNgOut,
// Out
out=aluOut,
out=outM
);
// IN in[16], reset, load, inc;
// OUT out[16];
// inc almost all the time.
// only when not load. load = jump
// JGT
// not negative and not zero
Not(in=aluNgOut, out=notAluNgOut);
Not(in=aluZrOut, out=notAluZrOut);
And(a=notAluZrOut, b=notAluNgOut , out=isPositive);
And(a=isPositive, b=j3, out=shouldJumpJGT);
// JEQ
// Jump if zero can be checked it zr
And(a=aluZrOut, b=j2 , out=shouldJumpJEQ);
// JLT
// Jump if less than 0
And(a=aluNgOut, b=j1, out=shouldJumpJLT);
// Should Jump
Or(a=shouldJumpJGT, b=shouldJumpJEQ , out=shouldJumpJGTJEQ);
Or(a=shouldJumpJGTJEQ , b= shouldJumpJLT, out=hasJumpInstruction );
// And C instruction
And(a=hasJumpInstruction, b=isCInstruction , out=shouldJump);
//Inc
Not(in=shouldJump, out=shouldIncrement);
PC(in=aRegOut, load=shouldJump, inc=shouldIncrement, reset=reset , out[0..14]=pc);
// Out
And(a=isCInstruction, b=d3 , out=writeM);
}

View File

@@ -0,0 +1,44 @@
// 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/5/Computer.hdl
/**
* The Hack computer, consisting of CPU, ROM and RAM.
* When reset = 0, the program stored in the ROM executes.
* When reset = 1, the program's execution restarts.
* Thus, to start running the currently loaded program,
* set reset to 1, and then set it to 0.
* From this point onwards, the user is at the mercy of the software.
* Depending on the program's code, and whether the code is correct,
* the screen may show some output, the user may be expected to enter
* some input using the keyboard, or the program may do some procerssing.
*/
CHIP Computer {
IN reset;
PARTS:
ROM32K(
address=pcOut,
out=romInstructionOut
);
CPU(
inM=memoryOut,
instruction=romInstructionOut,
reset=reset,
outM=cpuOutMOut,
writeM= cpuWriteMOut,
addressM= cpuAddressMOut,
pc=pcOut
);
Memory(
in= cpuOutMOut,
load= cpuWriteMOut,
address= cpuAddressMOut,
out=memoryOut
);
}