// 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: //全部0にする And16(a[0..15]=in[0..15], b[0..15]=false, out=wall0); //インクリメント(winc) Inc16(in[0..15]=ret, out=winc); //resetが1のときは、wall0を使う。loadが1のときは、inを使う。incが1のときはwincを使う。そうでなければw2が0となり更新されない //実際にはクレノー図 //reset, load, inc //1,?,? = 00 (reset) //0,1,? = 01 (load) //0,0,1 = 10 (inc) //0,0,0 = 11 (---) //resetかloadが1の場合はsel[1]は必ず0 Or(a=reset, b=load, out=nsel1); //sel1がsel[1] Not(in=nsel1, out=sel1); //0,1,0 //0,1,1 //0,0,0 //上記3つのパターンの時のみsel[0]は1 nreset and ((load and ninc) or (load and inc) or (nload and ninc)) Not(in=load, out=nload); Not(in=inc, out=ninc); Not(in=reset, out=nreset); And(a=load, b=ninc, out=wand1); And(a=load, b=inc, out=wand2); And(a=nload, b=ninc, out=wand3); Or(a=wand1, b=wand2, out=wor1); Or(a=wand3, b=wor1, out=wor2); And(a=nreset, b=wor2, out=sel0); Mux4Way16(a[0..15]=wall0, b[0..15]=in[0..15], c[0..15]=winc, d[0..15]=ret, sel[0]=sel0, sel[1]=sel1, out=wreg); //loadやincやresetが1の場合は更新 Or(a=load, b=inc, out=w1); Or(a=reset, b=w1, out=w2); Register(in[0..15]=wreg, load=w2, out[0..15]=out[0..15], out=ret); }