nand2tetris/projects/03/a/PC.hdl

26 lines
847 B
Plaintext
Raw Normal View History

2021-12-23 14:14:21 +00:00
// 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/03/a/PC.hdl
/**
* A 16-bit counter with load and reset control bits.
* if (reset[t] == 1) out[t+1] = 0
* else if (load[t] == 1) out[t+1] = in[t]
* else if (inc[t] == 1) out[t+1] = out[t] + 1 (integer addition)
* else out[t+1] = out[t]
*/
CHIP PC {
IN in[16], load, inc, reset;
OUT out[16];
PARTS:
// Put your code here:
Inc16(in=registerValue, out=incrementedValue);
Mux16(a=registerValue, b=incrementedValue, sel=inc, out=incBitStep);
Mux16(a=incBitStep, b=in, sel=load, out=loadBitStep);
Mux16(a=loadBitStep, b=false, sel=reset, out=resetBitStep);
Register(in=resetBitStep, load=true, out=out, out=registerValue);
}