Nand2Tetris — Capítulo 2
Aritmética booleana y la ALU
//Medio sumador de 1 bit
//Entrada: a, b (1 bit)
//Salida: suma (LSB de a+b), acarreo (MSB de a+b)
//Acción: suma = a XOR b, acarreo = a AND b
CHIP HalfAdder {
IN a, b;
OUT sum, // Right bit of a + b
carry; // Left bit of a + b
PARTS:
Xor(a=a, b=b, out=sum);
And(a=a, b=b, out=carry);
}
//sumador completo de 1 bit
//Entrada: a, b, c (entrada de 1 bit + acarreo)
//Salida: suma (LSB de a+b+c), acarreo (MSB de a+b+c)
//Acción: Calcule (a+b) primero, luego agregue c al resultado.
CHIP FullAdder {
IN a, b, c;
OUT sum, // Right bit of a + b + c
carry; // Left bit of a + b + c
PARTS:
//Primera mitadSumador: calcular a + b
HalfAdder(a=a, b=b, sum=sum1, carry=carry1);
//Segunda mitadSumador: calcular suma1 + c
HalfAdder(a=sum1, b=c, sum=sum, carry=carry2);
//acarreo final: acarreo1 O acarreo2
Or(a=carry1, b=carry2, out=carry);
}
//Sumador de 16 bits (método de acarreo ondulado)
//Entrada: a[16], b[16] (número de 16 bits, representación en complemento a 2)
//Salida: out[16] (resultado de a + b, ignorando el acarreo superior)
//Operación: Utilice HalfAdder para 0, FullAdder para 1~15.
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
//Bit 0: sin acarreo de entrada
HalfAdder(a=a[0], b=b[0],
sum=out[0], carry=carry1);
//Bits 1 ~ 15: recibir entrada de acarreo del paso anterior
FullAdder(a=a[1], b=b[1], c=carry1,
sum=out[1], carry=carry2);
FullAdder(a=a[2], b=b[2], c=carry2,
sum=out[2], carry=carry3);
FullAdder(a=a[3], b=b[3], c=carry3,
sum=out[3], carry=carry4);
FullAdder(a=a[4], b=b[4], c=carry4,
sum=out[4], carry=carry5);
FullAdder(a=a[5], b=b[5], c=carry5,
sum=out[5], carry=carry6);
FullAdder(a=a[6], b=b[6], c=carry6,
sum=out[6], carry=carry7);
FullAdder(a=a[7], b=b[7], c=carry7,
sum=out[7], carry=carry8);
FullAdder(a=a[8], b=b[8], c=carry8,
sum=out[8], carry=carry9);
FullAdder(a=a[9], b=b[9], c=carry9,
sum=out[9], carry=carry10);
FullAdder(a=a[10], b=b[10], c=carry10,
sum=out[10], carry=carry11);
FullAdder(a=a[11], b=b[11], c=carry11,
sum=out[11], carry=carry12);
FullAdder(a=a[12], b=b[12], c=carry12,
sum=out[12], carry=carry13);
FullAdder(a=a[13], b=b[13], c=carry13,
sum=out[13], carry=carry14);
FullAdder(a=a[14], b=b[14], c=carry14,
sum=out[14], carry=carry15);
FullAdder(a=a[15], b=b[15], c=carry15,
sum=out[15]);
}
//incrementador de 16 bits
//entrada: en [16]
//Salida: resultado de en + 1
//Acción: Agregar 1 usando Add16
CHIP Inc16 {
IN in[16];
OUT out[16];
PARTS:
//cálculo en + 1 (1 a binario: b[0]=1, resto=0)
Add16(a=in,
b[0]=true, b[1]=false, b[2]=false, b[3]=false,
b[4]=false, b[5]=false, b[6]=false, b[7]=false,
b[8]=false, b[9]=false, b[10]=false, b[11]=false,
b[12]=false, b[13]=false, b[14]=false, b[15]=false,
out=out);
}
//ALU (Unidad Aritmética Lógica)
//Entrada: x[16], y[16] (entrada de 16 bits)
//zx, nx, zy, ny, f, no (señales de control)
//Salida: out[16] (resultado de la operación)
//zr (1 si fuera==0, 0 en caso contrario)
//ng (1 si fuera <0, 0 en caso contrario)
//
//Flujo de acción:
//Paso 1: Si zx=1, x=0, si nx=1, x=NOT(x)
//Paso 2: Si zy=1, y=0, si ny=1, y=NOT(y)
//Paso 3: Si f=1, out=x+y (Add16), si f=0, out=x&y (And16)
//Paso 4: Si no=1 entonces out=NOT(out)
//Paso 5: Calcular los indicadores zr y ng
CHIP ALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero x?
nx, // negate x?
zy, // zero y?
ny, // negate y?
f, // (x+y) or (x&y)?
no; // negate output?
OUT
out[16], // 16-bit output
zr, // is out == 0?
ng; // is out < 0?
PARTS:
//Paso 1: x preprocesamiento (cero, negar)
Mux16(a=x, b=false, sel=zx, out=xZero);
Not16(in=xZero, out=xNot);
Mux16(a=xZero, b=xNot, sel=nx, out=xProc);
//Paso 2: Preprocesar y (cero, negar)
Mux16(a=y, b=false, sel=zy, out=yZero);
Not16(in=yZero, out=yNot);
Mux16(a=yZero, b=yNot, sel=ny, out=yProc);
//Paso 3: Seleccione la operación (AÑADIR o Y)
Add16(a=xProc, b=yProc, out=addRes);
And16(a=xProc, b=yProc, out=andRes);
Mux16(a=andRes, b=addRes, sel=f, out=fRes);
//Paso 4: posprocesamiento de salida (negación) + banderas
Not16(in=fRes, out=fNot);
Mux16(a=fRes, b=fNot, sel=no,
out=out, out[15]=ng, out[0..7]=outLow, out[8..15]=outHigh);
//Paso 5: Calcule el indicador zr (O de todos los bits, luego NO)
Or8Way(in=outLow, out=orLow);
Or8Way(in=outHigh, out=orHigh);
Or(a=orLow, b=orHigh, out=orFinal);
Not(in=orFinal, out=zr);
}