Nand2tetris –Project 3

构建一系列寄存器

/**
 * 1-bit register:
 * If load[t] == 1 then out[t+1] = in[t]
 *                 else out does not change (out[t+1] = out[t])
 */

CHIP Bit {
    IN in, load;
    OUT out;

    PARTS:
    Mux(a=out2, b=in, sel=load, out=out1);
    DFF(in=out1, out=out2);
    DFF(in=out1, out=out);
    // Put your code here:
}

/**
 * 16-bit register:
 * If load[t] == 1 then out[t+1] = in[t]
 * else out does not change
 */


CHIP Register {
    IN in[16], load;
    OUT out[16];

    PARTS:
    Bit(in=in[0], load=load, out=out[0]);
    Bit(in=in[1], load=load, out=out[1]);
    Bit(in=in[2], load=load, out=out[2]);
    Bit(in=in[3], load=load, out=out[3]);
    Bit(in=in[4], load=load, out=out[4]);
    Bit(in=in[5], load=load, out=out[5]);
    Bit(in=in[6], load=load, out=out[6]);
    Bit(in=in[7], load=load, out=out[7]);
    Bit(in=in[8], load=load, out=out[8]);
    Bit(in=in[9], load=load, out=out[9]);
    Bit(in=in[10], load=load, out=out[10]);
    Bit(in=in[11], load=load, out=out[11]);
    Bit(in=in[12], load=load, out=out[12]);
    Bit(in=in[13], load=load, out=out[13]);
    Bit(in=in[14], load=load, out=out[14]);
    Bit(in=in[15], load=load, out=out[15]);

    // Put your code here:
}


/**
 * Memory of 8 registers, each 16 bit-wide. Out holds the value
 * stored at the memory location specified by address. If load==1, then 
 * the in value is loaded into the memory location specified by address 
 * (the loaded value will be emitted to out from the next time step onward).
 */

CHIP RAM8 {
    IN in[16], load, address[3];
    OUT out[16];

    PARTS:
    DMux8Way(in=load, sel=address, a=r1, b=r2, c=r3, d=r4, e=r5, f=r6, g=r7, h=r8);
    Register(in=in, load=r1, out=out0);
    Register(in=in, load=r2, out=out1);
    Register(in=in, load=r3, out=out2);
    Register(in=in, load=r4, out=out3);
    Register(in=in, load=r5, out=out4);
    Register(in=in, load=r6, out=out5);
    Register(in=in, load=r7, out=out6);
    Register(in=in, load=r8, out=out7);
    Mux8Way16(a=out0, b=out1, c=out2, d=out3, e=out4, f=out5, g=out6, h=out7, sel=address, out=out);
    
    // Put your code here:
}


/**
 * Memory of 64 registers, each 16 bit-wide. Out holds the value
 * stored at the memory location specified by address. If load==1, then 
 * the in value is loaded into the memory location specified by address 
 * (the loaded value will be emitted to out from the next time step onward).
 */

CHIP RAM64 {
    IN in[16], load, address[6];
    OUT out[16];

    PARTS:
    DMux8Way(in=load, sel=address[3..5], a=a1, b=a2, c=a3, d=a4, e=a5, f=a6, g=a7, h=a8);

    RAM8(in=in, load=a1, address=address[0..2], out=o1);
    RAM8(in=in, load=a2, address=address[0..2], out=o2);
    RAM8(in=in, load=a3, address=address[0..2], out=o3);
    RAM8(in=in, load=a4, address=address[0..2], out=o4);
    RAM8(in=in, load=a5, address=address[0..2], out=o5);
    RAM8(in=in, load=a6, address=address[0..2], out=o6);
    RAM8(in=in, load=a7, address=address[0..2], out=o7);
    RAM8(in=in, load=a8, address=address[0..2], out=o8);
    Mux8Way16(a=o1, b=o2, c=o3, d=o4, e=o5, f=o6, g=o7, h=o8, sel=address[3..5], out=out);
    // Put your code here:
    
}

/**
 * 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: 
    Inc16(in=out1, out=inco);
    Mux16(a=in, b=inco, sel=inc, out=Mout1);
    Mux16(a=Mout1, b=in, sel=load, out=Mout2);
    Mux16(a=Mout2, b=false, sel=reset, out=Mout3);
    
    Register(in=Mout3, load=true, out=out1,out=out);
    

    // Put your code here:
}