Sum of Two Bits

IMG_0295A while back I read a blog that claimed that no one knows how to build a processor because of the complexity and computers do all the work today, so I’ve decided to see if I can make one using only discrete components.

Before working on the actual instruction parsing, I will work on the individual instructions. So the first instruction I’m going to make is ADD. The image to the left adds two bits together.

Out is only set when A or B are set, but never at the same time, so it is A XOR B.
Carry is only set when A and B are both set, so it is A AND B.

 

Full AdderEasy. However, a carry can happen, so this diagram needs to take a third input for the carry for the previous stage. In the first stage, Cin (Carry In) will be zero.

Since it is built on the previous adder, A XOR B and A AND B will still be a part of this circuit.

Out is just an extension of the previous stage, combining the output of the previous adder with the carry, ( A XOR B ) XOR Cin.

Since Out was just another XOR operation, another carry has the possibility of being generated. It is impossible for both carry’s to be true at the same time, so we can just OR them together. Cout is ( A AND B ) OR ( ( A XOR B ) AND Cin ).

A quick console app to test the logic

//
//
#include <stdio.h>
 
//
//
unsigned int AddTwoBits( const unsigned int inA, const unsigned int inB, const int inCin, unsigned int * outCout )
{
	unsigned int out = ( inA ^ inB ) ^ inCin;
 
	// Output new carry if needed
	unsigned int cout = ( inA & inB ) | ( ( inA ^ inB ) & inCin );
	if( outCout )
	{
		(*outCout) = cout;
	}
 
	//
	printf( "%d\t%d\t%d\t%d\t%d\n", inA, inB, inCin, out, cout );
 
	//
	return out;
}
 
//
//
int main()
{
	printf( "A\tB\tCin\tOut\tCout\n" );
 
	AddTwoBits( 0, 0, 0, NULL );
	AddTwoBits( 1, 0, 0, NULL );
	AddTwoBits( 0, 1, 0, NULL );
	AddTwoBits( 1, 1, 0, NULL );
	AddTwoBits( 0, 0, 1, NULL );
	AddTwoBits( 1, 0, 1, NULL );
	AddTwoBits( 0, 1, 1, NULL );
	AddTwoBits( 1, 1, 1, NULL );
}

And the output gives what I expected.

A	B	Cin	Out	Cout
0	0	0	0	0
1	0	0	1	0
0	1	0	1	0
1	1	0	0	1
0	0	1	1	0
1	0	1	0	1
0	1	1	0	1
1	1	1	1	1

Now I just need to create the logic using discreet components.