# Impossible bug Classic List Threaded 3 messages Open this post in threaded view
|

## Impossible bug

 Hello, in the ALU: ``` Not16(in=x, out=xn); And16(a=x, b=xn, out=xz); Mux16(a=x, b=xz, sel=zx, out=x1); Not16(in=x1, out=x1n); Mux16(a=x1, b=x1n, sel=nx, out=x2); Not16(in=y, out=yn); And16(a=y, b=yn, out=yz); Mux16(a=y, b=yz, sel=zy, out=y1); Not16(in=y1, out=y1n); Mux16(a=y1, b=y1n, sel=ny, out=y2); Add16(a=x2, b=y2, out=plus); And16(a=x2, b=y2, out=and); Mux16(a=and, b=plus, sel=f, out=u); Not16(in=u, out=un); Mux16(a=u, b=un, out=out);```gives me `Comparison failure at line 3` when running `ALU-nostat.tst`. I can't understand how the multiplexor gives so strange results. ``` x = 0 xz = 0 zx = 1 x1 = 0 x1n = -1 nx = 1 x2 = 0 y = -1 yz = 0 zy = 1 y1 = 0 y1n = -1 ny = 1 y2 = 0```zx = 1, nx = 1, zy = 1, ny = 1 is supposed to make Mux16 forward the second input. This is demonstrated as y1 gets its value 0 from yz. However, it is inconsistent with the observations that x2 apparently gets its value 0 from x1 and y2 gets its value 0 from y1. My mind is inconsistent.
 I solved the problem somehow... suddenly the bits were different. May I instead ask how my complicated solution might be simplified? Presently these are my parts: ``` // preprocess x Not16(in=x, out=xn); And16(a=x, b=xn, out=xz); Mux16(a=x, b=xz, sel=zx, out=x1); Not16(in=x1, out=x1n); Mux16(a=x1, b=x1n, sel=nx, out=x2); // preprocess y Not16(in=y, out=yn); And16(a=y, b=yn, out=yz); Mux16(a=y, b=yz, sel=zy, out=y1); Not16(in=y1, out=y1n); Mux16(a=y1, b=y1n, sel=ny, out=y2); // do main computation Add16(a=x2, b=y2, out=plus); And16(a=x2, b=y2, out=and); Mux16(a=and, b=plus, sel=f, out=u); // postprocess Not16(in=u, out=un); Mux16(a=u, b=un, sel=no, out=v); // evaluate extra output flags // if (out == 0) set zr = 1 Not16(in=v, out=vn); All16(in=vn, out=zr); // if (out < 0) set ng = 1 Leftmost(in=v, out=ng); // return Copy16(in=v, out=out); ```and I needed the following helpers: ```CHIP Copy16 { IN in; OUT out; PARTS: Not16(in=in, out=z); Not16(in=z, out=out); } CHIP Leftmost { IN in; OUT out; PARTS: Not(in=in, out=g); Not(in=g, out=out); } CHIP All16 { IN in; OUT out; PARTS: And(a=in, b=in, out=c0); And(a=in, b=c0, out=c1); And(a=in, b=c1, out=c2); And(a=in, b=c2, out=c3); And(a=in, b=c3, out=c4); And(a=in, b=c4, out=c5); And(a=in, b=c5, out=c6); And(a=in, b=c6, out=c7); And(a=in, b=c7, out=c8); And(a=in, b=c8, out=c9); And(a=in, b=c9, out=c10); And(a=in, b=c10, out=c11); And(a=in, b=c11, out=c12); And(a=in, b=c12, out=c13); And(a=in, b=c13, out=c14); And(a=in, b=c14, out=out); } ```I am most concerned about Copy16 and Leftmost. Is there an easier way to assign values?
 Administrator Joel Sjögren wrote I solved the problem somehow... suddenly the bits were different. If you were seeing wrong values in the HardwareSimulator, that's a known problem that can occur when animation is turned off.  There are also problems with the eval button if you have not connected signals to all the chip outputs. May I instead ask how my complicated solution might be simplified? You have missed some features of HDL. There are built in constants true and false so you do not need to make your own 0 using AND(x, ~x). ``` Mux16(a=x, b=false, ... ```You can have multiple connections to part outputs and you can connect to sub-buses of outputs, so you do not need extra chips to copy signals or select bits. ``` Mux16(a=u, b=un, sel=no, out=out, out[__]=ng, out[__]=outLow, ...); Or8Way(in=outLow, ...); ... ```I leave you to fill in the blanks and the remaining parts for zr. --Mark