Machine Problem 2, due Oct 12

Part of the homework for CS:2820, Fall 2015
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Extending MP1: Consider the posted solution to Machine Problem 1. This solution is only the first step on the road to a working logic simulator.

Class Hierarchy: The posted solution to Machine Problem 1 uses an unweildy kluge to handle initialization of the different types of gates. What we really want (as suggested by a number of bug reporting comments in the code, is a class hierarchy where the class Gate has subclasses for AndGate, OrGate, etc.

Each of these subclasses has its own scan method, the implementation of the Gate.scan abstract method declared in the underlying Gate abstract class. These scan methods initialize such things as the list of permitted inputs before calling a method of the Gate class that finishes the initialization.

In the simulator, each of these subclasses will be augmented with other methods to add the simulation behavior, but we will ignore these for the current round of work.

Efficiency: Eventually, in the simulator, when an input to a gate changes value (from true to false or false to true), the identity of that input will need to be passed to the gate's input-change method, and that method will need to know which input changed in order to determine what to do about it. As currently constructed, inputs are named only by strings. The wire knows the string that names the input, and the gate has a list of strings. Knowing which input changed involves string comparisons, and doing these comparisons over and over each time the input changes would seriously slow a simulation of something as large as a modern microprocessor (and yes, Intel and other chip vendors routinely simulate their entire microprocessors before building them).

A far better way to do things is to translate input names into input numbers in the initializer and use input numbers from there on. So, where the code distributed as a solution to MP1 says:

class Wire {
        float  delay;        // delay of this wire
        Gate   driven;       // what gate does this wire drive
        String input;        // what input of driven does this wire drive
        ...

This ought to be changed to:

class Wire {
        float  delay;        // delay of this wire
        Gate   driven;       // what gate does this wire drive
        int    input;        // what input of driven does this wire drive
        ...

When the string corresponding to an input number i of gate g is needed, call g.inputName(i). The current g.checkInput(s) method can be replaced with g.inputNumber(s) which returns the input number corresponding to the string s, so long as that input has not yet been used. In the event that the string s does not correspond to a legal and not-yet used input of g, g.inputNumber(s) should return -1 (any negative value would be a good way to signal that the input is not the ordinal position of an entry in the input list, and -1 is the obvious negative number to use).

Completeness: Finally, there is a little and well-defined programming job hiding inside the posted solution to MP1. A logic circuit with some inputs to some gates left undefined is not well defined. For example, an and gate input in1 unused has an output of false when input in2 is false, but its output is undefined when input in2 is true.

Therefore, before starting the simulation (and before outputting the defined part of the logic circuit in our test program), we should check to see that all of the inputs to all of the gates in our circuit are connected. If any gates have unused inputs, those should be reported, for example with a message saying "Unused input gate 'x' in2".

Grading criteria: No credit will be given to programs that do not represent a substantial attempt at meeting the requirements of this assignment. Your program must:

Note that the requirements for header comments in the file header are absolute. Your name must appear in the form that it appears on your ID card. The TA will not waste his time doing detective work to attempt to figure out who submitted what code.

Submission: Your solution should consist of a single file, with one public method, main. (Later, we will chop the file into smaller more managable pieces.)

To submit your solution, make sure it is in a file called LogicCircuit.java (this requires that the main method be in a class called LogicCircuit. This must be in the current directory. Then, follow the following dialogue with the Linux shell (what you type is given in boldface):

[HawkID@serv16 ~/project]$ submit LogicCircuit.java
Course (22C:016 would be c_016): CS2820
Possible submit directories for /group/submit/CS2820 are:

section1/mp1
section1/mp2
section2/mp1
section2/mp2

Choice:   section1/mp2
LogicCircuit.java
* File/directory LogicCircuit.java has been 
*  copied to /group/submit/CS2820/section1/mp2/LogicCircuit.java.HawkID.
[HawkID@serv16 ~/project]$ 

The final output ("File ... copied to ...") is your confirmation that the file was successfully submitted. If you make multiple submissions, only the last one will be counted. The date of your final submission must be before midnight on the due date.

Note that the submit program doesn't know what section you are in. Please give your section (either section1 or section2). You must know what section you are enrolled in!