4-Bit Computer Simulator John T. Rine 11/10/2013 ======Objectives====== To create a software simulator for the 4-Bit Computer project described at the www.waitingforfriday.com Website (see references section at the bottom of the page). ======Prerequisites====== *C/C++ Programming Experience *Knowledge of Digital Logic *Knowledge of basic CPU Architecture ======Background====== The 4-bit computer project at www.waitingforfriday.com was the inspiration for this project.\\ \\ The waitingforfridays 4-bit computer is built from switches, transistors and components to bias them for digital operation rather than from logic gates. It isn't really a 4-bit CPU but rather a 4-bit adder with carry out. An adder is a component of a CPU. Furthermore, the 4-bit CPU doesn't have the capability to perform subtraction.\\ This project contains not one but two simulators. The first, fourBitComputer, simulates the 4-computer described at www.waitingforfriday.com. The second, fourBitComputer2, adds subtraction capability (addition of the 2s complement of the second data parameter). The high-level logic used in the simulators is built up from inverter, or, and and logic. From these primitive logic functions higher-level logic functions are built using the object-oriented principle of composition because higher-level logic functions are composed of lower-level logic functions.\\ To use either simulator, in Windows open the command or console window. Using the command line, navigate to the location of the simulators. On the command line, enter either either of the aforementioned simulators with the -h switch. Help information regarding how to enter data into the simulator is returned:\\ **Execution of fourBitComputer.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer -h Usage: fourBitComputer [1st 4-bit binary field] [2nd 4-bit binary field] 1st binary field-adden 1 2nd binary field-adden 2 C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of fourBitComputer2.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 -h Usage: fourBitComputer [1-bit binary field] [4-bit binary field] [4-bit binary f ield] 1st binary field-operation 2nd binary field-adden 1/minuend 3rd binary field-adden 2/subtrahend Commands: 0 = add 1 = subtract Returns: carry/not borrow sum/difference C:\Users\John\Desktop\4-Bit Computer and Gates> Both simulators have an extra result bit. During operation of the fourBitComputer simulator, the extra result bit is set only if the result of an addition operation is greater than 111 binary or 15 decimal. During the operation of the fourBitComputer2 simulator, however, the extra result bit is set depending on the result of either an addition or a subtraction operation, and therefore functions as a carry/not borrow bit. Note that this is how a carry/not borrow status register flag operates. Therefore, not only can these simulators add and subtract, but they also have a status bit whose state is dependent upon operation and the values used in the operation. ======Code====== **makeLogicandCPU.bat**\\ REM makeLogicandCPU.bat REM John T. Rine REM 11-10-2013 del *.exe del *.o del *.a g++ -c notGate.cc -o notGate.o g++ -c orGate.cc -o orGate.o g++ -c andGate.cc -o andGate.o g++ -c norGate.cc -o norGate.o g++ -c norFF.cc -o norFF.o g++ -c nandGate.cc -o nandGate.o g++ -c xorGate.cc -o xorGate.o g++ -c nandFF.cc -o nandFF.o g++ -c xnorGate.cc -o xnorGate.o g++ -c halfAdderGate.cc -o halfAdderGate.o g++ -c fullAdderGate.cc -o fullAdderGate.o ar src BoolLib.a *.o g++ -I. notTest.cc -o notTest BoolLib.a g++ -I. orTest.cc -o orTest BoolLib.a g++ -I. andTest.cc -o andTest BoolLib.a g++ -I. norTest.cc -o norTest BoolLib.a g++ -I. norFFtest.cc -o norFFtest BoolLib.a g++ -I. norFFtest2.cc -o norFFtest2 g++ -I. nandTest.cc -o nandTest BoolLib.a g++ -I. xorTest.cc -o xorTest BoolLib.a g++ -I. nandFFtest.cc -o nandFFtest BoolLib.a g++ -I. nandFFtest2.cc -o nandFFtest2 g++ -I. xnorTest.cc -o xnorTest BoolLib.a g++ -I. halfAdderTest.cc -o halfAddertest BoolLib.a g++ -I. fullAdderTest.cc -o fullAddertest BoolLib.a g++ -I. fourBitComputer.cc -o fourBitComputer BoolLib.a g++ -I. fourBitComputer2.cc -o fourBitComputer2 BoolLib.a pause \\ **notGate.h**\\ //notGate.h //John T. Rine //11-10-2013 #ifndef _NOTGATE_H #define _NOTGATE_H //not gate class notGate { public: notGate(); ~notGate(); void set(); void set(bool); void reset(); bool get(); private: bool input; //bool output; }; #endif \\ **notGate.cc**\\ //notGate.cc //John T. Rine //11-10-2013 //#include "basicGates.h" #include "notGate.h" notGate::notGate() { input = false; } notGate::~notGate() { } void notGate::set() { input = true; } void notGate::set(bool in) { input = in; } void notGate::reset() { input = false; } bool notGate::get() { //bool result; //if (input) result = false; //else result = true; //return(result); return(!input); } \\ **notTest.cc**\\ //notTest.cc //John T. Rine //11-10-2013 //#include "basicGates.h" #include "notGate.h" #include //#include // or #include using namespace std; int main(void) { notGate notGate1; bool a = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Not Gate Truth Table" << endl; cout << " ------- "<< endl; cout << "| A | O |" << endl; cout << " ------- "<< endl; for (int i = 0; i <= 1; i++) { a = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // a = true; // break; //} notGate1.set(a); cout << "| "<< a << " | " << notGate1.get() << " |" << endl; cout << " -------- "<< endl; } //system("pause"); return(0); } \\ **orGate.h**\\ //orGate.h //John T. Rine //11-10-2013 #ifndef _ORGATE_H #define _ORGATE_H //or gate class orGate { public: orGate(); ~orGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool get(); private: bool input1; bool input2; //bool output; }; #endif \\ **orGate.cc**\\ //orGate.cc //John T. Rine //11-10-2013 //#include "basicGates.h" #include "orGate.h" orGate::orGate() { input1 = input2 = false; } orGate::~orGate() { } void orGate::set() { input1 = input2 = true; } void orGate::set(bool in) { input1 = input2 = in; } void orGate::set(bool in1, bool in2) { input1 = in1; input2 = in2; } void orGate::reset() { input1 = input2 = false; } bool orGate::get() { //bool result; //if (input1 || input2) result = true; //else result = false; //return(result); return(input1 || input2); } \\ **orTest.cc**\\ //orTest.cc //John T. Rine //11-10-2013 //#include "basicGates.h" #include "orGate.h" #include //#include // or #include using namespace std; int main(void) { orGate orGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Or Gate Truth Table" << endl; cout << " ----------- "<< endl; cout << "| A B | O |" << endl; cout << " ----------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} orGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << orGate1.get() << " |" << endl; cout << " ----------- " << endl; } //system("pause"); return(0); } \\ **andGate.h**\\ //andGate.h //John T. Rine //11-10-2013 #ifndef _ANDGATE_H #define _ANDGATE_H //and gate class andGate { public: andGate(); ~andGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool get(); private: bool input1; bool input2; }; #endif \\ **andGate.cc**\\ //andGate.cc //John T. Rine //11-10-2013 //#include "basicGates.h" #include "andGate.h" andGate::andGate() { input1 = input2 = false; } andGate::~andGate() { } void andGate::set() { input1 = input2 = true; } void andGate::set(bool in) { input1 = input2 = in; } void andGate::set(bool in1, bool in2) { input1 = in1; input2 = in2; } void andGate::reset() { input1 = input2 = false; } bool andGate::get() { //bool result; //if (input1 && input2) result = true; //else result = false; //return(result); return(input1 && input2); } \\ **andTest.cc**\\ //andTest.cc //John T. Rine //11-10-2013 //#include "basicGates.h" #include "andGate.h" #include //#include // or #include using namespace std; int main(void) { andGate andGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "And Gate Truth Table" << endl; cout << " ----------- "<< endl; cout << "| A B | O |" << endl; cout << " ----------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} andGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << andGate1.get() << " |" << endl; cout << " ----------- " << endl; } //system("pause"); return(0); } \\ **norGate.h**\\ //norGate.h //John T. Rine //11-10-2013 #ifndef _NORGATE_H #define _NORGATE_H #include "orGate.h" #include "notGate.h" class norGate { public: norGate(); ~norGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool get(); private: orGate orGate1; notGate notGate1; }; #endif \\ **norGate.cc**\\ //norGate.cc //John T. Rine //11-10-2013 #include "norGate.h" norGate::norGate() { orGate1.set(false, false); notGate1.set(orGate1.get()); } norGate::~norGate() { } void norGate::set() { orGate1.set(true, true); notGate1.set(orGate1.get()); } void norGate::set(bool in) { orGate1.set(in, in); notGate1.set(orGate1.get()); } void norGate::set(bool in1, bool in2) { orGate1.set(in1, in2); notGate1.set(orGate1.get()); } void norGate::reset() { orGate1.set(false, false); notGate1.set(orGate1.get()); } bool norGate::get() { return(notGate1.get()); } \\ **norTest.cc**\\ //norTest.cc //John T. Rine //11-10-2013 #include "norGate.h" #include //#include // or #include using namespace std; int main(void) { norGate norGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Nor Gate Truth Table" << endl; cout << " ----------- "<< endl; cout << "| A B | O |" << endl; cout << " ----------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} norGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << norGate1.get() << " |" << endl; cout << " ----------- " << endl; } //system("pause"); return(0); } \\ **norFF.h**\\ //norFF.h //John T. Rine //11-10-2013 #ifndef _NORFF_H #define _NORFF_H #include "norGate.h" class norFF { public: norFF(); ~norFF(); void setFF(bool, bool); bool getQ(); bool getNotQ(); private: //bool set; //bool reset; //bool Q; //bool notQ; norGate setGate; norGate resetGate; }; #endif \\ **norFF.cc**\\ //norFF.cc //John T. Rine //11-10-2013 #ifdef DEBUG #include #endif #include "norFF.h" using namespace std; norFF::norFF() { for (int i = 1; i < 3; i++) { setGate.set(false, resetGate.get()); resetGate.set(setGate.get(), true); } } norFF::~norFF() { } void norFF::setFF(bool Set, bool Reset) { for (int i = 1; i < 3; i++) { setGate.set(Set, resetGate.get()); resetGate.set(setGate.get(), Reset); } } bool norFF::getQ() { return(resetGate.get()); } bool norFF::getNotQ() { return(setGate.get()); } \\ **norFFtest.cc**\\ //norFFtest.cc //John T. Rine //11-10-2013 #include //#include // or #include #include "norFF.h" using namespace std; int main() { norFF norRSFF; bool set = false; bool reset = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Nor Flip-Flop Gate Truth Table" << endl; cout << "--------------" << endl; cout << "| s r | Q !Q |" << endl; cout << "--------------" << endl; for (int i = 1; i <= 8; i++) { switch(i) { case 1: //no change-checks initialization in constructor set = false; reset = false; break; case 2: //set ff set = true; reset = false; break; case 3: //no change set = false; reset = false; break; case 4: //set ff set = true; reset = false; break; case 5: //no change set = false; reset = false; break; case 6: //reset ff set = false; reset = true; break; case 7: //no change set = false; reset = false; break; case 8: //reset ff set = false; reset = true; break; } norRSFF.setFF(set, reset); cout << "| " << set << " " << reset << " | " << norRSFF.getQ() << " " << norRSFF.getNotQ() << " |" << endl; } cout << "--------------" << endl; //system("pause"); return(0); } \\ **norFFtest2.cc**\\ //norFFtest2.cc //John T. Rine //11-10-2013 #include //#include // or #include using namespace std; void norFF (bool, bool, bool &, bool &); int main() { bool set = false; bool reset = false; bool Q = false; bool notQ = true; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Nor Flip-Flop Gate Truth Table" << endl; cout << "--------------" << endl; cout << "| s r | Q !Q |" << endl; cout << "--------------" << endl; for (int i = 0; i != 6; i++) { switch(i) { case 0: break; case 1: set = true; reset = false; break; case 2: set = false; reset = false; break; case 3: set = false; reset = true; break; case 4: set = false; reset = false; break; case 5: set = false; reset = true; break; } norFF(set, reset, Q, notQ); cout << "| " << set << " " << reset << " | " << Q << " " << notQ << " |" << endl; } cout << "--------------" << endl; //system("pause"); return(0); } void norFF (bool set, bool reset, bool &Q, bool ¬Q) { for(int i = 1; i != 3; i++) { if(!(set | Q)) notQ = true; else notQ = false; if(!(reset | notQ)) Q = true; else Q = false; } } \\ **nandGate.h**\\ //nandGate.h //John T. Rine //11-10-2013 #ifndef _NANDGATE_H #define _NANDGATE_H #include "andGate.h" #include "notGate.h" class nandGate { public: nandGate(); ~nandGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool get(); private: andGate andGate1; notGate notGate1; }; #endif \\ **nandGate.cc**\\ //nandGate.cc //John T. Rine //11-10-2013 #include "nandGate.h" nandGate::nandGate() { andGate1.set(false, false); notGate1.set(andGate1.get()); } nandGate::~nandGate() { } void nandGate::set() { andGate1.set(true, true); notGate1.set(andGate1.get()); } void nandGate::set(bool in) { andGate1.set(in, in); notGate1.set(andGate1.get()); } void nandGate::set(bool in1, bool in2) { andGate1.set(in1, in2); notGate1.set(andGate1.get()); } void nandGate::reset() { andGate1.set(false, false); notGate1.set(andGate1.get()); } bool nandGate::get() { return(notGate1.get()); } \\ **nandTest.cc**\\ //nandTest.cc //John T. Rine //11-10-2013 #include "nandGate.h" #include //#include // or #include using namespace std; int main(void) { nandGate nandGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Nand Gate Truth Table" << endl; cout << " ----------- "<< endl; cout << "| A B | O |" << endl; cout << " ----------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} nandGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << nandGate1.get() << " |" << endl; cout << " ----------- " << endl; } //system("pause"); return(0); } \\ **xorGate.h**\\ //xorGate.h //John T. Rine //11-10-2013 #ifndef _XORGATE_H #define _XORGATE_H #include "notGate.h" #include "andGate.h" #include "orGate.h" class xorGate { public: xorGate(); ~xorGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool get(); private: notGate notGate1, notGate2; andGate andGate1, andGate2; orGate orGate1; }; #endif \\ **xorGate.cc**\\ //xorGate.cc //John T. Rine //11-10-2013 #include "xorGate.h" xorGate::xorGate() { notGate1.set(false); andGate1.set(notGate1.get(), false); notGate2.set(false); andGate2.set(false, notGate2.get()); orGate1.set(andGate1.get(), andGate2.get()); } xorGate::~xorGate() { } void xorGate::set() { notGate1.set(true); andGate1.set(notGate1.get(), true); notGate2.set(true); andGate2.set(true, notGate2.get()); orGate1.set(andGate1.get(), andGate2.get()); } void xorGate::set(bool in) { notGate1.set(in); andGate1.set(notGate1.get(), in); notGate2.set(in); andGate2.set(in, notGate2.get()); orGate1.set(andGate1.get(), andGate2.get()); } void xorGate::set(bool in1, bool in2) { notGate1.set(in1); andGate1.set(notGate1.get(), in2); notGate2.set(in2); andGate2.set(in1, notGate2.get()); orGate1.set(andGate1.get(), andGate2.get()); } void xorGate::reset() { notGate1.set(false); andGate1.set(notGate1.get(), false); notGate2.set(false); andGate2.set(false, notGate2.get()); orGate1.set(andGate1.get(), andGate2.get()); } bool xorGate::get() { return(orGate1.get()); } \\ **xorTest.cc**\\ //xorTest.cc //John T. Rine //11-10-2013 #include "xorGate.h" #include //#include // or #include using namespace std; int main(void) { xorGate xorGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Xor Gate Truth Table" << endl; cout << " ----------- "<< endl; cout << "| A B | O |" << endl; cout << " ----------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} xorGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << xorGate1.get() << " |" << endl; cout << " ----------- " << endl; } //system("pause"); return(0); } \\ **nandFF.h**\\ //nandFF.h //John T. Rine ///11-10-2013 #ifndef _NANDFF_H #define _NANDFF_H #include "nandGate.h" class nandFF { public: nandFF(); ~nandFF(); void setFF(bool, bool); bool getQ(); bool getNotQ(); private: //bool set; //bool reset; //bool Q; //bool notQ; nandGate setGate; nandGate resetGate; }; #endif \\ **nandFF.cc**\\ //nandFF.cc //John T. Rine //11-10-2013 #ifdef DEBUG #include #endif #include "nandFF.h" using namespace std; nandFF::nandFF() { for (int i = 1; i < 3; i++) { setGate.set(true, resetGate.get()); resetGate.set(setGate.get(), false); } } nandFF::~nandFF() { } void nandFF::setFF(bool Set, bool Reset) { for (int i = 1; i < 3; i++) { setGate.set(Set, resetGate.get()); resetGate.set(setGate.get(), Reset); } } bool nandFF::getQ() { return(setGate.get()); } bool nandFF::getNotQ() { return(resetGate.get()); } \\ **nandFFtest.cc**\\ //nandFFTest.cc //John T. Rine //11-10-2013 #include //#include // or #include #include "nandFF.h" using namespace std; int main() { nandFF nandRSFF; bool set = false; bool reset = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Nand Flip-Flop Gate Truth Table" << endl; cout << "--------------" << endl; cout << "| s r | Q !Q |" << endl; cout << "--------------" << endl; for (int i = 1; i <= 8; i++) { switch(i) { case 1: //no change state-checks that FF is initialized properly by constructor set = true; reset = true; break; case 2: //set FF set = false; reset = true; break; case 3: //no change set = true; reset = true; break; case 4: //set ff set = false; reset = true; break; case 5: //no change set = true; reset = true; break; case 6: //reset ff set = true; reset = false; break; case 7: //no change set = true; reset = true; break; case 8: //reset ff set = true; reset = false; break; } nandRSFF.setFF(set, reset); cout << "| " << set << " " << reset << " | " << nandRSFF.getQ() << " " << nandRSFF.getNotQ() << " |" << endl; } cout << "--------------" << endl; //system("pause"); return(0); } \\ **nandFFtest2.cc**\\ //nandFFtest2.cc //John T. Rine //11-10-2013 #include //#include // or #include using namespace std; void nandFF (bool, bool, bool &, bool &); int main() { bool set = true; bool reset = false; bool Q = false; bool notQ = true; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Nand Flip-Flop Gate Truth Table" << endl; cout << "--------------" << endl; cout << "| s r | Q !Q |" << endl; cout << "--------------" << endl; for (int i = 0; i != 6; i++) { switch(i) { case 0: break; case 1: set = true; reset = false; break; case 2: set = true; reset = true; break; case 3: set = false; reset = true; break; case 4: set = true; reset = true; break; case 5: set = false; reset = true; break; } nandFF(set, reset, Q, notQ); cout << "| " << set << " " << reset << " | " << Q << " " << notQ << " |" << endl; } cout << "--------------" << endl; //system("pause"); return(0); } void nandFF (bool set, bool reset, bool &Q, bool ¬Q) { for(int i = 1; i < 3; i++) { if(!(set & notQ)) Q = true; else Q = false; if(!(reset & Q)) notQ = true; else notQ = false; } } \\ **xnorGate.h**\\ //xnorGate.h //John T. Rine //11-10-2013 #ifndef _XNORGATE_H #define _XNORGATE_H #include "xorGate.h" #include "notGate.h" class xnorGate { public: xnorGate(); ~xnorGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool get(); private: xorGate xorGate1; notGate notGate1; }; #endif \\ **xnorGate.cc**\\ //xnorGate.cc //John T. Rine //11-10-2013 #include "xnorGate.h" xnorGate::xnorGate() { xorGate1.set(false, false); notGate1.set(xorGate1.get()); } xnorGate::~xnorGate() { } void xnorGate::set() { xorGate1.set(true, true); notGate1.set(xorGate1.get()); } void xnorGate::set(bool in) { xorGate1.set(in, in); notGate1.set(xorGate1.get()); } void xnorGate::set(bool in1, bool in2) { xorGate1.set(in1, in2); notGate1.set(xorGate1.get()); } void xnorGate::reset() { xorGate1.set(false, false); notGate1.set(xorGate1.get()); } bool xnorGate::get() { return(notGate1.get()); } \\ **xnorTest.cc**\\ //xnorTest.cc //John T. Rine //11-10-2013 #include "xnorGate.h" #include //#include // or #include using namespace std; int main(void) { xnorGate xnorGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Xnor Gate Truth Table" << endl; cout << " ----------- "<< endl; cout << "| A B | O |" << endl; cout << " ----------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} xnorGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << xnorGate1.get() << " |" << endl; cout << " ----------- " << endl; } //system("pause"); return(0); } \\ **halfAdderGate.h**\\ //halfAdderGate.h //John T. Rine //11-10-2013 #ifndef _HALFADDERGATE_H #define _HALFADDERGATE_H #include "xorGate.h" #include "andGate.h" class halfAdderGate { public: halfAdderGate(); ~halfAdderGate(); void set(); void set(bool); void set(bool, bool); void reset(); bool getSum(); bool getCarry(); private: xorGate xorGate1; andGate andGate1; }; #endif \\ **halfAdderGate.cc**\\ //halfAdderGate.cc //John T. Rine //11-10-2013 #include "halfAdderGate.h" halfAdderGate::halfAdderGate() { xorGate1.set(false, false); andGate1.set(false, false); } halfAdderGate::~halfAdderGate() { } void halfAdderGate::set() { xorGate1.set(true, true); andGate1.set(true, true); } void halfAdderGate::set(bool in) { xorGate1.set(in, in); andGate1.set(in, in); } void halfAdderGate::set(bool in1, bool in2) { xorGate1.set(in1, in2); andGate1.set(in1, in2); } void halfAdderGate::reset() { xorGate1.set(false, false); andGate1.set(false, false); } bool halfAdderGate::getSum() { return(xorGate1.get()); } bool halfAdderGate::getCarry() { return(andGate1.get()); } \\ **halfAdderTest.cc**\\ //halfAdderTest.cc //John T. Rine //11-10-2013 #include "halfAdderGate.h" #include //#include // or #include using namespace std; int main(void) { halfAdderGate halfAdderGate1; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Half-Adder Gate Truth Table" << endl; cout << " -------------- "<< endl; cout << "| A B | S C |" << endl; cout << " -------------- "<< endl; for (int i = 0; i != 4; i++) { a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; //} halfAdderGate1.set(a, b); cout << "| "<< a << " " << b <<" | " << halfAdderGate1.getSum() << " " << halfAdderGate1.getCarry() << " |" << endl; cout << " -------------- " << endl; } //system("pause"); return(0); } \\ **fullAdderGate.h**\\ //fullAdderGate.h //John T. Rine //11-10-2013 #ifndef _FULLADDERGATE_H #define _FULLADDERGATE_H #include "halfAdderGate.h" #include "orGate.h" class fullAdderGate { public: fullAdderGate(); ~fullAdderGate(); void set(); void set(bool); void set(bool, bool, bool); void reset(); bool getSum(); bool getCarry(); private: halfAdderGate halfAdderGate1; halfAdderGate halfAdderGate2; orGate orGate1; }; #endif \\ **fullAdderGate.cc**\\ //fullAdderGate.cc //John T. Rine //11-10-2013 #include "fullAdderGate.h" fullAdderGate::fullAdderGate() { halfAdderGate1.set(false, false); //a and b inputs halfAdderGate2.set(false, halfAdderGate1.getSum()); //Carry in and sum from half adder 1 orGate1.set(halfAdderGate1.getCarry(), halfAdderGate2.getCarry()); } fullAdderGate::~fullAdderGate() { } void fullAdderGate::set() { halfAdderGate1.set(true, true); //a and b inputs halfAdderGate2.set(true, halfAdderGate1.getSum()); //Carry in and sum from half adder 1 orGate1.set(halfAdderGate1.getCarry(), halfAdderGate2.getCarry()); } void fullAdderGate::set(bool in) { halfAdderGate1.set(in, in); //a and b inputs halfAdderGate2.set(in, halfAdderGate1.getSum()); //Carry in and sum from half adder 1 orGate1.set(halfAdderGate1.getCarry(), halfAdderGate2.getCarry()); } void fullAdderGate::set(bool cin, bool in1, bool in2) { halfAdderGate1.set(in1, in2); //a and b inputs halfAdderGate2.set(cin, halfAdderGate1.getSum()); //Carry in and sum from half adder 1 orGate1.set(halfAdderGate1.getCarry(), halfAdderGate2.getCarry()); } void fullAdderGate::reset() { halfAdderGate1.set(false, false); //a and b inputs halfAdderGate2.set(false, halfAdderGate1.getSum()); //Carry in and sum from half adder 1 orGate1.set(halfAdderGate1.getCarry(), halfAdderGate2.getCarry()); } bool fullAdderGate::getSum() { return(halfAdderGate2.getSum()); } bool fullAdderGate::getCarry() { return(orGate1.get()); } \\ **fullAdderTest.cc**\\ //fullAdderTest.cc //John T. Rine //11-10-2013 #include "fullAdderGate.h" #include //#include // or #include using namespace std; int main(void) { fullAdderGate fullAdderGate1; bool cin = false; bool a = false; bool b = false; cout << endl << "TRUE is: " << true << "; "; cout << "FALSE is: " << false << endl << endl; cout << "Full-Adder Gate Truth Table" << endl; cout << " ------------------ "<< endl; cout << "| Cin A B | S C |" << endl; cout << " ------------------ "<< endl; for (int i = 0; i <= 7; i++) { cin = i & 0x04; a = i & 0x02; b = i & 0x01; //switch(i) //{ // case 0: // break; // case 1: // b = true; // break; // case 2: // a = true; // b = false; // break; // case 3: // b = true; // break; // case 4: // cin = true; // a = false; // b = false; // break; // case 5: // b = true; // break; // case 6: // a = true; // b = false; // break; // case 7: // b = true; // break; //} fullAdderGate1.set(cin, a, b); cout << "| "<< cin << " " << a << " " << b <<" | " << fullAdderGate1.getSum() << " " << fullAdderGate1.getCarry() << " |" << endl; cout << " ------------------ " << endl; } //system("pause"); return(0); } \\ **fourBitComputer.cc**\\ //fourBitComputer.cc //John T. Rine //11-10-2013 #include #include #include "fullAdderGate.h" using namespace std; int main(int argc, char ** argv) { int bits = 4; fullAdderGate bit[bits]; if (*(argv + 1) == string("-h")) { cout << "Usage: fourBitComputer [1st 4-bit binary field] [2nd 4-bit binary field]" << endl; cout << "1st binary field-adden 1" << endl; cout << "2nd binary field-adden 2" << endl; exit(0); } else if (*(argv + 1) == string("-v")) { cout << "Version 1.0" << endl; exit(0); } else if (argc != 3) { if (argc < 3) cout << "missing a command line argument"; else if (argc > 3) cout << "too many command line arguments"; exit(1); } int ii = bits - 1; bool cin = false; for(int i = 0; i <= bits - 1; i++) { if (i > 0) cin = bit[i - 1].getCarry(); bit[i].set(cin, *(*(argv + 1) + ii) & 0x01, *(*(argv + 2) + ii) & 0x01); ii--; } cout << bit[bits - 1].getCarry() << " "; for (int i = bits - 1; i >= 0; i--) { cout << bit[i].getSum(); } cout << endl; } \\ **fourBitComputer2.cc**\\ //fourBitComputer2.cc //John T. Rine //11-10-2013 #include #include #include "fullAdderGate.h" #include "xorGate.h" using namespace std; int main(int argc, char ** argv) { int bits = 4; int fields = 4; fullAdderGate bit[bits]; xorGate compliment[bits]; if (*(argv + 1) == string("-h")) { cout << "Usage: fourBitComputer [1-bit binary field] [4-bit binary field] [4-bit binary field]" << endl; cout << "1st binary field-operation" << endl; cout << "2nd binary field-adden 1/minuend" << endl; cout << "3rd binary field-adden 2/subtrahend" << endl; cout << endl; cout << "Commands:" << endl; cout << "0 = add" << endl; cout << "1 = subtract" << endl; cout << endl; cout << "Returns:" << endl; cout << "carry/not borrow \t sum/difference" << endl; exit(0); } else if (*(argv + 1) == string("-v")) { cout << "Version 2.0" << endl; exit(0); } else if (argc != fields) { if (argc < fields) cout << "missing a command line argument"; else if (argc > fields) cout << "too many command line arguments"; exit(1); } int ii = bits - 1; bool cin = false; for(int i = 0; i <= bits - 1; i++) { compliment[i].set(*(*(argv + 1) + 0) & 0x01, *(*(argv + 3) + ii) & 0x01); if (i == 0) cin = *(*(argv + 1) + 0) & 0x01; else cin = bit[i - 1].getCarry(); bit[i].set(cin, *(*(argv + 2) + ii) & 0x01, compliment[i].get()); ii--; } cout << bit[bits - 1].getCarry() << " "; for (int i = bits - 1; i >= 0; i--) { cout << bit[i].getSum(); } cout << endl; } ======Compilation====== **Compilation (makeLogicandCPU.bat)**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>REM makeLogicandCPU.bat C:\Users\John\Desktop\4-Bit Computer and Gates>REM John T. Rine C:\Users\John\Desktop\4-Bit Computer and Gates>REM 11-10-2013 C:\Users\John\Desktop\4-Bit Computer and Gates>del *.exe C:\Users\John\Desktop\4-Bit Computer and Gates>del *.o C:\Users\John\Desktop\4-Bit Computer and Gates>del *.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c notGate.cc -o notGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c orGate.cc -o orGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c andGate.cc -o andGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c norGate.cc -o norGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c norFF.cc -o norFF.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c nandGate.cc -o nandGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c xorGate.cc -o xorGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c nandFF.cc -o nandFF.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c xnorGate.cc -o xnorGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c halfAdderGate.cc -o halfAd derGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -c fullAdderGate.cc -o fullAd derGate.o C:\Users\John\Desktop\4-Bit Computer and Gates>ar src BoolLib.a *.o C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. notTest.cc -o notTest Boo lLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. orTest.cc -o orTest BoolL ib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. andTest.cc -o andTest Boo lLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. norTest.cc -o norTest Boo lLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. norFFtest.cc -o norFFtest BoolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. norFFtest2.cc -o norFFtes t2 C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. nandTest.cc -o nandTest B oolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. xorTest.cc -o xorTest Boo lLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. nandFFtest.cc -o nandFFte st BoolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. nandFFtest2.cc -o nandFFt est2 C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. xnorTest.cc -o xnorTest B oolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. halfAdderTest.cc -o halfA ddertest BoolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. fullAdderTest.cc -o fullA ddertest BoolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. fourBitComputer.cc -o fou rBitComputer BoolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>g++ -I. fourBitComputer2.cc -o fo urBitComputer2 BoolLib.a C:\Users\John\Desktop\4-Bit Computer and Gates>pause Press any key to continue . . . ======Execution====== **Execution of notTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>notTest TRUE is: 1; FALSE is: 0 Not Gate Truth Table ------- | A | O | ------- | 0 | 1 | -------- | 1 | 0 | -------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of orTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>orTest TRUE is: 1; FALSE is: 0 Or Gate Truth Table ----------- | A B | O | ----------- | 0 0 | 0 | ----------- | 0 1 | 1 | ----------- | 1 0 | 1 | ----------- | 1 1 | 1 | ----------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of andTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>andTest TRUE is: 1; FALSE is: 0 And Gate Truth Table ----------- | A B | O | ----------- | 0 0 | 0 | ----------- | 0 1 | 0 | ----------- | 1 0 | 0 | ----------- | 1 1 | 1 | ----------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of norTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>norTest TRUE is: 1; FALSE is: 0 Nor Gate Truth Table ----------- | A B | O | ----------- | 0 0 | 1 | ----------- | 0 1 | 0 | ----------- | 1 0 | 0 | ----------- | 1 1 | 0 | ----------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of norFFtest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>norFFtest TRUE is: 1; FALSE is: 0 Nor Flip-Flop Gate Truth Table -------------- | s r | Q !Q | -------------- | 0 0 | 0 1 | | 1 0 | 1 0 | | 0 0 | 1 0 | | 1 0 | 1 0 | | 0 0 | 1 0 | | 0 1 | 0 1 | | 0 0 | 0 1 | | 0 1 | 0 1 | -------------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of norFFtest2.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>norFFtest2 TRUE is: 1; FALSE is: 0 Nor Flip-Flop Gate Truth Table -------------- | s r | Q !Q | -------------- | 0 0 | 0 1 | | 1 0 | 1 0 | | 0 0 | 1 0 | | 0 1 | 0 1 | | 0 0 | 0 1 | | 0 1 | 0 1 | -------------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of nandTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>nandTest TRUE is: 1; FALSE is: 0 Nand Gate Truth Table ----------- | A B | O | ----------- | 0 0 | 1 | ----------- | 0 1 | 1 | ----------- | 1 0 | 1 | ----------- | 1 1 | 0 | ----------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of xorTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>xorTest TRUE is: 1; FALSE is: 0 Xor Gate Truth Table ----------- | A B | O | ----------- | 0 0 | 0 | ----------- | 0 1 | 1 | ----------- | 1 0 | 1 | ----------- | 1 1 | 0 | ----------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of nandFFtest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>nandFFtest TRUE is: 1; FALSE is: 0 Nand Flip-Flop Gate Truth Table -------------- | s r | Q !Q | -------------- | 1 1 | 0 1 | | 0 1 | 1 0 | | 1 1 | 1 0 | | 0 1 | 1 0 | | 1 1 | 1 0 | | 1 0 | 0 1 | | 1 1 | 0 1 | | 1 0 | 0 1 | -------------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of nandFFtest2.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>nandFFtest2 TRUE is: 1; FALSE is: 0 Nand Flip-Flop Gate Truth Table -------------- | s r | Q !Q | -------------- | 1 0 | 0 1 | | 1 0 | 0 1 | | 1 1 | 0 1 | | 0 1 | 1 0 | | 1 1 | 1 0 | | 0 1 | 1 0 | -------------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of xnorTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>xnorTest TRUE is: 1; FALSE is: 0 Xnor Gate Truth Table ----------- | A B | O | ----------- | 0 0 | 1 | ----------- | 0 1 | 0 | ----------- | 1 0 | 0 | ----------- | 1 1 | 1 | ----------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of halfAdderTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>halfAdderTest TRUE is: 1; FALSE is: 0 Half-Adder Gate Truth Table -------------- | A B | S C | -------------- | 0 0 | 0 0 | -------------- | 0 1 | 1 0 | -------------- | 1 0 | 1 0 | -------------- | 1 1 | 0 1 | -------------- C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of fullAdderTest.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>fullAdderTest TRUE is: 1; FALSE is: 0 Full-Adder Gate Truth Table ------------------ | Cin A B | S C | ------------------ | 0 0 0 | 0 0 | ------------------ | 0 0 1 | 1 0 | ------------------ | 0 1 0 | 1 0 | ------------------ | 0 1 1 | 0 1 | ------------------ | 1 0 0 | 1 0 | ------------------ | 1 0 1 | 0 1 | ------------------ | 1 1 0 | 0 1 | ------------------ | 1 1 1 | 1 1 | ------------------ C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of fourBitComputer.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer -h Usage: fourBitComputer [1st 4-bit binary field] [2nd 4-bit binary field] 1st binary field-adden 1 2nd binary field-adden 2 C:\Users\John\Desktop\4-Bit Computer and Gates> \\ C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer 0000 0001 0 0001 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer 1000 0111 0 1111 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer 1001 0111 1 0000 C:\Users\John\Desktop\4-Bit Computer and Gates> \\ **Execution of fourBitComputer2.exe**\\ C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 -h Usage: fourBitComputer [1-bit binary field] [4-bit binary field] [4-bit binary f ield] 1st binary field-operation 2nd binary field-adden 1/minuend 3rd binary field-adden 2/subtrahend Commands: 0 = add 1 = subtract Returns: carry/not borrow sum/difference C:\Users\John\Desktop\4-Bit Computer and Gates> \\ Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\John>cd desktop C:\Users\John\Desktop>cd 4-bit computer and gates C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 0 0001 0001 0 0010 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 0 1111 0001 1 0000 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 0 1111 1111 1 1110 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 1 0001 0001 1 0000 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 1 1111 1111 1 0000 C:\Users\John\Desktop\4-Bit Computer and Gates>fourBitComputer2 1 0000 1111 0 0001 C:\Users\John\Desktop\4-Bit Computer and Gates> ======References====== * http://www.waitingforfriday.com/index.php/4-Bit_Computer * http://www.youtube.com/watch?v=xISG4nGTQYE