5

Here's my code:

CHIP PC {
    IN in[16],load,inc,reset;
    OUT out[16];

    PARTS:
    Inc16(in = regout, out = incout);
    Mux16(a = regout, b = incout, sel = inc, out = incdecision);
    Mux16(a = incdecision, b = false, sel = reset, out = resetdecision);
    Mux16(a = regout, b = resetdecision, sel = load, out = loaddecision);
    Register(in = loaddecision, load = true, out = regout, out = out);
}

Basically, the value coming out of the register is incremented, which is only accepted if inc is 1 (checked via Mux) which then goes through another Mux which may reset it, and then another Mux which may or may not write it depending on the value of load. Then whatever value came out of that (be it the changed value or the value coming from the old register) is put into the register.

What am I doing wrong?

zubergu
  • 3,646
  • 3
  • 25
  • 38
Doug Smith
  • 29,668
  • 57
  • 204
  • 388

3 Answers3

3

Change the order of resetdecision and loaddecesion. First one has higher priority.

Inc16(in=outpc, out=outincreased);
Mux16(a=outpc, b=outincreased, sel=inc, out=outinc);
Mux16(a=outinc, b=in, sel=load, out=outload);
Mux16(a=outload, b=false, sel=reset, out=outreset); 
    //And16(a=outLOAD, b[0..15]=reset, out=outreset);
Register(in=outreset, load=true, out=out, out=outpc);
sevko
  • 1,402
  • 1
  • 18
  • 37
2

You don't seem to have the In signal connected to anything. If the load signal is set you need to get the appropriate Mux16 to load the In value into the register.

Michael
  • 1,136
  • 1
  • 7
  • 15
0

The reset Mux16 needs to happen after the load Mux16. The load Mux16 needs to have "in" as the "b" pin.

My working code from Nand2Tetris:

Inc16(in = outandabout, out = incout);
Mux16( a = outandabout, b = incout, sel = inc, out = incinc);
Mux16( a = incinc, b = in, sel = load, out = loadout);
Mux16( a = loadout, b = false, sel = reset, out= outreset);
Register(in = outreset, load = true, out = out, out = outandabout);
  • Instead of saying what needs to happen you should comment why things need to happen that way. Doing so allows other people in the future to learn and take away much more from your answer. – yanman1234 Aug 01 '17 at 00:16