42 lines
1.2 KiB
Plaintext
42 lines
1.2 KiB
Plaintext
// 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:
|
|
Mux16(a=s, b=false, sel=reset, out=s1);
|
|
|
|
/* Make sure we only load if we didn't reset. */
|
|
Not(in=reset, out=notreset);
|
|
And(a=notreset, b=load, out=load1);
|
|
|
|
Mux16(a=s1, b=in, sel=load1, out=s2);
|
|
|
|
/* Make sure we only inc if we didn't reset or load. */
|
|
Not(in=load, out=notload);
|
|
And(a=notreset, b=notload, out=notresetorload);
|
|
And(a=notresetorload, b=inc, out=inc1);
|
|
|
|
/* Calculate increment and set depending on inc flag. */
|
|
Inc16(in=s, out=si);
|
|
Mux16(a=s2, b=si, sel=inc1, out=s3);
|
|
|
|
/* s3 will have the original value if none of load/inc/reset was set. */
|
|
Register(in=s3, load=true, out=s);
|
|
|
|
/* Output nextstate */
|
|
Id(in=s, out=out);
|
|
}
|