Search code examples
debugginghdlhardwarenand2tetris

DMux.hdl failure when in=1, sel=0


I'm writing the hdl code for a DMux based on the Nand2Tetris course.

CHIP DMux {
IN in, sel;
OUT a, b;

PARTS:
And(a = sel, b = in, out = b);
Not(in = sel, out = selNot);
And(a = in, b = selNot, out = a);   
}

For some reason, this code fails on the test script values of in = 1 and sel = 0. It evaluates a and b both to 0 in this case.

I have written out the gates multiple times, and I can't figure out why the result is not a = 1 and b = 0

Can someone explain to me what is happening?


Solution

  • I have a feeling your Not implementation may have a problem.

    Try replacing the Not with a Nand:

    Nand(a=sel,b=sel,out=notSel);   // notSel = ! sel
    

    If this works, then your Not.hdl is incorrect.

    Also, on a style point, it's clearer if you define your intermediates before your final outputs (ie: put the Nand first), and be consistent in your input ordering (ie: a=in, b=sel or notSel, out = a or b). Helps reduce the chance you will misread something.