Task

"Simulate" a four-bit adder.

This design can be realized using four 1-bit full adders.
Each of these 1-bit full adders can be built with two half adders and an   or   gate. ;

Finally a half adder can be made using an   xor   gate and an   and   gate.

The   xor   gate can be made using two   nots,   two   ands   and one   or.

Not,   or   and   and,   the only allowed "gates" for the task, can be "imitated" by using the bitwise operators of your language.

If there is not a bit type in your language, to be sure that the   not   does not "invert" all the other bits of the basic type   (e.g. a byte)   we are not interested in,   you can use an extra   nand   (and   then   not)   with the constant   1   on one input.

Instead of optimizing and reducing the number of gates used for the final 4-bit adder,   build it in the most straightforward way,   connecting the other "constructive blocks",   in turn made of "simpler" and "smaller" ones.


Schematics of the "constructive blocks"


(Xor gate with ANDs, ORs and NOTs)        

   (A half adder)        

          (A full adder)            

                (A 4-bit adder)        












Solutions should try to be as descriptive as possible, making it as easy as possible to identify "connections" between higher-order "blocks".

It is not mandatory to replicate the syntax of higher-order blocks in the atomic "gate" blocks, i.e. basic "gate" operations can be performed as usual bitwise operations, or they can be "wrapped" in a block in order to expose the same syntax of higher-order blocks, at implementers' choice.

To test the implementation, show the sum of two four-bit numbers (in binary).




 
program Four_bit_adder;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils;
 
Type
  TBitAdderOutput = record
    S, C: Boolean;
    procedure Assign(_s, _C: Boolean);
    function ToString: string;
  end;
 
  TNibbleBits = array[1..4] of Boolean;
 
  TNibble = record
    Bits: TNibbleBits;
    procedure Assign(_Bits: TNibbleBits); overload;
    procedure Assign(value: byte); overload;
    function ToString: string;
  end;
 
  TFourBitAdderOutput = record
    N: TNibble;
    c: Boolean;
    procedure Assign(_c: Boolean; _N: TNibbleBits);
    function ToString: string;
  end;
 
  TLogic = record
    class function GateNot(const A: Boolean): Boolean; static;
    class function GateAnd(const A, B: Boolean): Boolean; static;
    class function GateOr(const A, B: Boolean): Boolean; static;
    class function GateXor(const A, B: Boolean): Boolean; static;
  end;
 
  TConstructiveBlocks = record
    function HalfAdder(const A, B: Boolean): TBitAdderOutput;
    function FullAdder(const A, B, CI: Boolean): TBitAdderOutput;
    function FourBitAdder(const A, B: TNibble; const CI: Boolean): TFourBitAdderOutput;
  end;
 
 
 
{ TBitAdderOutput }
 
procedure TBitAdderOutput.Assign(_s, _C: Boolean);
begin
  s := _s;
  c := _C;
end;
 
function TBitAdderOutput.ToString: string;
begin
  Result := 'S' + ord(s).ToString + 'C' + ord(c).ToString;
end;
 
{ TNibble }
 
procedure TNibble.Assign(_Bits: TNibbleBits);
var
  i: Integer;
begin
  for i := 1 to 4 do
    Bits[i] := _Bits[i];
end;
 
procedure TNibble.Assign(value: byte);
var
  i: Integer;
begin
  value := value and $0F;
  for i := 1 to 4 do
    Bits[i] := ((value shr (i - 1)) and 1) = 1;
end;
 
function TNibble.ToString: string;
var
  i: Integer;
begin
  Result := '';
  for i := 4 downto 1 do
    Result := Result + ord(Bits[i]).ToString;
end;
 
{ TFourBitAdderOutput }
 
procedure TFourBitAdderOutput.Assign(_c: Boolean; _N: TNibbleBits);
begin
  N.Assign(_N);
  c := _c;
end;
 
function TFourBitAdderOutput.ToString: string;
begin
  Result := N.ToString + ' c=' + ord(c).ToString;
end;
 
{ TLogic }
 
class function TLogic.GateAnd(const A, B: Boolean): Boolean;
begin
  Result := A and B;
end;
 
class function TLogic.GateNot(const A: Boolean): Boolean;
begin
  Result := not A;
end;
 
class function TLogic.GateOr(const A, B: Boolean): Boolean;
begin
  Result := A or B;
end;
 
class function TLogic.GateXor(const A, B: Boolean): Boolean;
begin
  Result := GateOr(GateAnd(A, GateNot(B)), (GateAnd(GateNot(A), B)));
end;
 
{ TConstructiveBlocks }
 
function TConstructiveBlocks.FourBitAdder(const A, B: TNibble; const CI: Boolean):
  TFourBitAdderOutput;
var
  FA: array[1..4] of TBitAdderOutput;
  i: Integer;
begin
  FA[1] := FullAdder(A.Bits[1], B.Bits[1], CI);
  Result.N.Bits[1] := FA[1].S;
 
  for i := 2 to 4 do
  begin
    FA[i] := FullAdder(A.Bits[i], B.Bits[i], FA[i - 1].C);
    Result.N.Bits[i] := FA[i].S;
  end;
  Result.C := FA[4].C;
end;
 
function TConstructiveBlocks.FullAdder(const A, B, CI: Boolean): TBitAdderOutput;
var
  HA1, HA2: TBitAdderOutput;
begin
  HA1 := HalfAdder(CI, A);
  HA2 := HalfAdder(HA1.S, B);
  Result.Assign(HA2.S, TLogic.GateOr(HA1.C, HA2.C));
end;
 
function TConstructiveBlocks.HalfAdder(const A, B: Boolean): TBitAdderOutput;
begin
  Result.Assign(TLogic.GateXor(A, B), TLogic.GateAnd(A, B));
end;
 
var
  j, k: Integer;
  A, B: TNibble;
  Blocks: TConstructiveBlocks;
 
begin
  for k := 0 to 255 do
  begin
    A.Assign(0);
    B.Assign(0);
    for j := 0 to 7 do
    begin
      if j < 4 then
        A.Bits[j + 1] := ((1 shl j) and k) > 0
      else
        B.Bits[j + 1 - 4] := ((1 shl j) and k) > 0;
    end;
 
    write(A.ToString, ' + ', B.ToString, ' = ');
    Writeln(Blocks.FourBitAdder(A, B, false).ToString);
  end;
  Writeln;
  Readln;
end.
 