1

I am trying to implement a MUX (Multiplexor) gate in the nand2tetris course. I first tried myself, and I got an error. But no matter what I changed I always got the error. So I tried checking some code online, and this is what most people use:

CHIP Mux {
    IN a, b, sel;
    OUT out;

    PARTS:
    Not(in=sel, out=nsel);
    And(a=sel, b=b, out=c1);
    And(a=nsel, b=a, out=c2);
    Or(a=c1, b=c2, out=out);
}

But even when I try this code I still get the following error: error msg

What I get as a truth table:

|   a   |   b   |  sel  |  out  |
|   0   |   0   |   0   |   0   |
|   0   |   0   |   1   |   0   |
|   0   |   1   |   0   |   0   |
|   0   |   1   |   1   |   0   |

What I should get:

|   a   |   b   |  sel  |  out  |
|   0   |   0   |   0   |   0   |
|   0   |   0   |   1   |   0   |
|   0   |   1   |   0   |   0   |
|   0   |   1   |   1   |   1   |
|   1   |   0   |   0   |   1   |
|   1   |   0   |   1   |   0   |
|   1   |   1   |   0   |   1   |
|   1   |   1   |   1   |   1   |

I have the newest software suite per 2020-01-13

zubergu
  • 3,646
  • 3
  • 25
  • 38
S1LV3R
  • 146
  • 3
  • 15
  • Confirm that you loaded your latest Mux.hdl implementation by scrolling down in HDL windows of Hardware Emulator. You should see your latest code. Code you posted is fine, works with me. I have older version of course, but I'd be surprised to see the problem on their side. Also, I use the same version of hardware simulator so the problem would have to be in test scripts and mine look the same as yours from screenshot that you posted. – zubergu Jan 13 '20 at 22:29
  • @zubergu I have the correct version of `Mux.hdl`, and the correct (As far as i know) test script. Even when using default implementations i get the error – S1LV3R Jan 14 '20 at 07:03

1 Answers1

1

From what can be seen your input pins are:

a = 0  
b = 1  
sel = 1

Your internal pins are:

nsel = 1 
c1   = 1 
c2   = 0

All as expected so far.

Expected out = 1 in this case and you get out = 0. Test script stops at this point because of failure.

Now there might be two reasons of that:
1) you didn't load correct Mux.hdl and because if you calculated Or(c1,c2) you would get 1 which is correct. If you placed And gate in place of Or it would explain failure
2) your implementation of Or.hdl is incorrect.Mux uses your version of Or gate if such file is present in the same directory.

So first verify your code in Hardware Simulator, then verify your implementation of Or.hdl. The latter you could do by removing temporarily Or.hdl from project directory. Hardware Simulator would load built-in version of Or gate.

zubergu
  • 3,646
  • 3
  • 25
  • 38
  • This is what is so weird. Even when i use the default everything i still get the same error. Exactly. I have made sure i have the right test script – S1LV3R Jan 14 '20 at 07:02
  • This is weird. I just tried today and it worked. Don't know what happened. I'm marking your answer as accepted as it helped me – S1LV3R Jan 14 '20 at 15:24