1

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?

Laurence
  • 721
  • 7
  • 24
yalpsid eman
  • 3,064
  • 6
  • 45
  • 71

2 Answers2

1

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.

MadOverlord
  • 1,034
  • 6
  • 11
0

Not sure that anything is wrong with your code. It looks to be the same as mine, which works. Have you tested your other And and Not gates?

My code:

Not(in=sel, out=notsel);
And(a=notsel, b=in, out=a);
And(a=in, b=sel, out=b);