1

I'm implementing the counter from chap. 3. Here is my code:

// 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];
      //sel=0; a;sel=1;b
      PARTS:
      //reset
      Not16(in=true, out=resetted);
      //inc
      Inc16(in=t, out=incremented);
      //Choose input between reset and out[t]
      Mux16(a=t,b=resetted,sel=reset,out=o1);
      //Choose input between o1 and in[t](load)
      Mux16(a=o1, b=in,sel=load,out=o2);
      //Choose input between o2 and inc
      Mux16(a=o2,b=incremented, sel=inc, out=o3);
      Register(in=o3, load=load, out=t, out=out);
  }

It's seems to fail at this test:

set in -32123,
tick,
output

This is line 5, I can't find my mistake(s). Any help is appreciated

Planet_Earth
  • 325
  • 1
  • 3
  • 11
  • Right, the order of the Mux gates defines the if/else logic. You want the reset function to be closest to the register since that is the first "if". – mjw Sep 04 '22 at 21:54

1 Answers1

1

Hint (because this is a learning exercise, after all):

You are doing a cascade of muxes to generate your new value, but under what circumstances are you updating the Register?

Suggestions:

Go back to Chapter 2 and notice that they had you implement a Mux4Way16 component. Using it will make your life a lot easier.

Also, you can use false as an input and it will automatically become as wide as needed. So you don't need to run true through a Not16 to get false[16].

MadOverlord
  • 1,034
  • 6
  • 11
  • Also in case somebody came here using search engine, I want to note that I had to change the order of the operations to make it work(I have no idea why this worked). – Planet_Earth May 27 '16 at 08:40