ICS 331 Assignment #4

In Java, C++, or C#, extend your program from assignment 2 (or the posted solution to assignment 2) to include BinaryGate subclasses: NorGate, NandGate, XorGate and XnorGate. Also add Gate abstract subclass TernaryGate that has Wire member variables input1, input2, and input3. TernaryGate has concrete subclasses Xor3Gate, Xnor3Gate, And3Gate, Or3Gate, Nand3Gate, Nor3Gate. See a href="http://www.shef.ac.uk/physics/teaching/phy107/othergates.html"> University of Sheffield's Physics 107 Other Gates page for the truth tables for 3-input Xor and Xnor gates. Also add a subclass of CircuitElement named SPDTSwitch (a Single-Pole Double-Throw Switch) that has a String member variable named name and Wire member variables named connect0, connect1 and common. SPDTSwitch also has a boolean member variable, connect that is false if the SPDTSwitch connects the connect0 Wire to the common Wire and true if the SPDTSwitch connects the connect1 Wire to the common Wire. Modify your program with an outer loop that asks the user for the connect value of each SPDTSwitch (0 or 1), then runs the simulation and prints out all Wires as before and finally asks the user whether to repeat (see example transcript below). Modify your simulation loop to exit out of the program after 1000 iterations. Print "Unstable circuit: more than 1000 compute loops" and exit the program with status 2 if your program exceeds 1000 iterations in any single round (reset the count when you ask the user whether to repeat). The following circuit spec creates a full-adder and should be stored in a file named full_adder.cir:

WIRE name preA0
WIRE name preA1
WIRE name preB0
WIRE name preB1
WIRE name preCin0
WIRE name preCin1
WIRE name A
WIRE name B
WIRE name Cin
WIRE name AandB
WIRE name AandCin
WIRE name BandCin
WIRE name Cout
WIRE name Sum
BATTERY high (preA1 preB1 preCin1) low (preA0 preB0 preCin0)
SPDTSWITCH name A connect0 preA0 connect1 preA1 common A
SPDTSWITCH name B connect0 preB0 connect1 preB1 common B
SPDTSWITCH name Cin connect0 preCin0 connect1 preCin1 common Cin
XOR3GATE input1 A input2 B input3 Cin output Sum
ANDGATE input1 A input2 B output AandB
ANDGATE input1 A input2 Cin output AandCin
ANDGATE input1 B input2 Cin output BandCin
OR3GATE input1 AandB input2 AandCin input3 BandCin output Cout

Here is a transcript of the program running with full_adder.cir:

Please enter the switch position for SPDTSwitch A (0 or 1): 1
Please enter the switch position for SPDTSwitch B (0 or 1): 1
Please enter the switch position for SPDTSwitch Cin (0 or 1): 1
preA0=0 preA1=1 preB0=0 preB1=1 preCin0=0 preCin1=1 A=1 B=1 Cin=1 AandB=1 AandCin=1 BandCin=1 Cout=1 Sum=1
Repeat? (y or n): y
Please enter the switch position for SPDTSwitch A (0 or 1): 1
Please enter the switch position for SPDTSwitch B (0 or 1): 1
Please enter the switch position for SPDTSwitch Cin (0 or 1): 0
preA0=0 preA1=1 preB0=0 preB1=1 preCin0=0 preCin1=1 A=1 B=1 Cin=0 AandB=1 AandCin=0 BandCin=0 Cout=1 Sum=0
Repeat? (y or n): y
Please enter the switch position for SPDTSwitch A (0 or 1): 1
Please enter the switch position for SPDTSwitch B (0 or 1): 0
Please enter the switch position for SPDTSwitch Cin (0 or 1): 0
preA0=0 preA1=1 preB0=0 preB1=1 preCin0=0 preCin1=1 A=1 B=0 Cin=0 AandB=0 AandCin=0 BandCin=0 Cout=0 Sum=1
Repeat? (y or n): y
Please enter the switch position for SPDTSwitch A (0 or 1): 0
Please enter the switch position for SPDTSwitch B (0 or 1): 0
Please enter the switch position for SPDTSwitch Cin (0 or 1): 0
preA0=0 preA1=1 preB0=0 preB1=1 preCin0=0 preCin1=1 A=0 B=0 Cin=0 AandB=0 AandCin=0 BandCin=0 Cout=0 Sum=0
Repeat? (y or n): y

In your simulation loop where you compute new Wire values for the output Wires of all Gates, you will have to treat the SPDTSwitch like a Gate and set the value of the common Wire to be the same as the value of either the connect0 or connect1 Wire depending on whether connect is true or false.

Record a sample run with multiple calls to your program with inputs: a test file for each of the new Gate types (all the Ternary gates, Xor, Xnor, Nor, Nand), the above full_adder.cir (be sure to try all eight combinations of possible inputs) and your ecc.cir file (or the posted solution ecc.cir file) with three different values for the data bits, each tested with no errors and with an error in each of the seven stored bits (so you should have 24 different iterations of your program's outer loop). Start your assignment NOW since this is a long assignment. My solution is about lines long (about not including blank lines). Your solution will most likely be longer.

To submit your assignment, zip all of your source code files (your .java or .cpp or .cs and any .h files), all of your .cir circuit spec files and the transcript of a sample run and email the zip archive to our TA, dwkang@hawaii.edu.


David N. Chin
Last modified: Wed Sep 28 08:43:37 HST