The XOR class implements a functioning logical XOR gate for use with our CPU simulator.
XOR myXorGate;
Function | Parameter(s) | Return value |
---|---|---|
XOR() | none | pointer/variable instantiation of the newly allocated XOR gate |
XOR() is the parameterless constructor that is responsible for creating a new instance of an XOR.
reset() will reset both inputs coming into the XOR to FALSE values.
myXorGate.reset();
Function | Parameter(s) | Return value |
---|---|---|
void reset() | no parameters | none |
set(bool) will take the parameter and set both inputs to that value.
myXorGate.set(true);
Function | Parameter(s) | Return value |
---|---|---|
void set() | bool | none |
set(bool, bool) will take the parameter and set each input to a unique value.
bool a = true; bool b = false; ... myXorGate.set(a, b);
Function | Parameter(s) | Return value |
---|---|---|
void set() | bool, bool | none |
get() will retrieve the output of the XOR.
bool output = myXorGate.get();
Function | Parameter(s) | Return value |
---|---|---|
bool get(int) | none | boolean value of gate's output |
xor.h
#ifndef _XOR_H #define _XOR_H class XOR { public: XOR(); void set(); void set(bool); void set(bool, bool); bool get(); private: bool input1; bool input2; }; #endif
xor.cc
#include "xor.h" XOR::XOR() { input1 = input2 = false; } void XOR::set() { input1 = input2 = false; } void XOR::set(bool value) { input1 = input2 = value; } void XOR::set(bool val1, bool val2) { input1 = val1; input2 = val2; } bool XOR::get() { bool result = false; if ((input1 == true && input2 == false) || (input1 == false && input2 == true)) { result = true; } return (result); }
xortest.cc
#include <cstdio> #include "xor.h" int main() { bool a, b; a = true, b = false; printf("--------------------\n"); printf("TRUE is: %d\n", true); printf("FALSE is: %d\n", false); printf("--------------------\n\n"); XOR myXorGate; printf(" a b | x \n"); printf("-----+---\n"); for(int temp = 0; temp <=3; temp++) { a = temp & 0x02; b = temp & 0x01; //if (temp & 0x2) a = true; //else a = false; //if (temp & 0x01) b = true; //else b = false; ////switch(temp) ////{ //// case 0: //// a = false; //// b = false; //// break; //// case 1: //// a = false; //// b = true; //// break; //// case 2: //// a = true; //// b = false; //// break; //// case 3: //// a = true; //// b = true; //// break; ////} myXorGate.set(a, b); printf(" %d %d | %d\n", a, b, myXorGate.get()); } printf("---------\n"); return(0); }
Makefile
CXX = g++ $(CXXFLAGS) $(INC) CXXFLAGS = -Wall INC = -I ../../include/ SRC = xor.cc OBJ = $(SRC:.cc=.o) all: $(SRC) $(OBJ) debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(BIN) .cc.o: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -c $< && echo "OK" || echo "FAIL" else $(CXX) -c $< endif clean: rm -f *.o $(OBJ) core