🔌
nand2tetris
  • Introduction
  • Combinational Chips
    • Nand Gate
    • Not Gate
    • And Gate
    • Or Gate
    • Xor Gate
    • Multiplexor Chip
    • Demultiplexor Chip
    • Not16 Chip
    • And16 Chip
    • Or16 Chip
    • Mux16 Chip
    • Or8Way Chip
    • Mux4Way16 Chip
    • Mux8Way16 Chip
    • DMux4Way Chip
    • DMux8Way Chip
    • Half Adder Chip
    • Full Adder Chip
    • Add16 Chip
    • Inc16 Chip
    • Half Subtractor Chip
    • Full Subtractor Chip
    • Subtract16 Chip
    • Dec16 Chip
    • Arithmetic Chip
    • ALU
  • Misc
    • Int2Bool
    • Bool2Int
    • Arrayto16
Powered by GitBook
On this page
  • Arithmetic Chip
  • Implementation of Arithmetic Chip in HDL
  • Implementation of Arithmetic Chip in Java™
  1. Combinational Chips

Arithmetic Chip

Abstraction and Implementation of (16-bit) Arithmetic Chip in Hardware Design Language and Java™.

PreviousDec16 ChipNextALU

Last updated 3 years ago

Arithmetic Chip

Arithmetic Chip is an 16-bit user-defined chip, is a mixed implementation of 16-bit Adder and 16-bit Subtractor (also known as Parallel Adder and Parallel Subtractor respectively).

Chip name: Arithmetic
Inputs: a[16], b[16]
Outputs: out[16]
Function: if (select == 1) then out = a + b
          else out = a - b

Implementation of Arithmetic Chip in HDL

You can use 16-bit Multiplexor that you've created earlier.

CHIP Arithmetic {
    IN a[16], b[16], sel;
    OUT out[16];

    PARTS:
   Add16(a=a, b=b, out=addab);
   Subtract16(a=a, b=b, out=diffab);
   Mux16(a=diffab, b=addab, sel=sel, out=out);
}

Implementation of Arithmetic Chip in Java™

Similar to the Implementation in HDL

package CombChips;

import Misc.Convert;

class Arithmetic_Gate extends Mux16_Gate {
    
    protected static int[] Arithmetic(int[] a, int[] b, int sel) {
        int[] arr_a = Convert.Arrayto16(a);
        int[] arr_b = Convert.Arrayto16(b);

        int[] addab = Add16_Gate.Add16(arr_a, arr_b);
        int[] subtractab = Subtract16_Gate.Subtract16(arr_a, arr_b);
        int[] out = Mux16(subtractab, addab, sel);

        return out;
    }
}
Abstraction and Implementation of Arithmetic Chip