From e389939297714dce68ce1d3e662b2f1fd8045c3d Mon Sep 17 00:00:00 2001 From: Sean O'Connor Date: Mon, 1 Sep 2025 15:05:16 -0400 Subject: [PATCH] Fix ALU: Properly split output for flag computation - Use out=out, out[0..7]=outLow, out[8..15]=outHigh, out[15]=sign syntax - Compute flags using internal signals outLow, outHigh, sign - Avoids HDL restriction on using output pins as inputs --- 02/ALU.hdl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/02/ALU.hdl b/02/ALU.hdl index e30c730..51c71a2 100644 --- a/02/ALU.hdl +++ b/02/ALU.hdl @@ -62,14 +62,14 @@ CHIP ALU { // handle no (negate out) Not16(in=fout, out=notfout); - Mux16(a=fout, b=notfout, sel=no, out=out); + Mux16(a=fout, b=notfout, sel=no, out=out, out[0..7]=outLow, out[8..15]=outHigh, out[15]=sign); // compute zr (zero flag) - Or8Way(in=out[0..7], out=tmp1); - Or8Way(in=out[8..15], out=tmp2); + Or8Way(in=outLow, out=tmp1); + Or8Way(in=outHigh, out=tmp2); Or(a=tmp1, b=tmp2, out=notzr); Not(in=notzr, out=zr); // compute ng (negative flag) - And(a=out[15], b=true, out=ng); + And(a=sign, b=true, out=ng); }