Starting from:

$25

CS211 - Programming Assignment IV - Solved

This assignment will provide more practice programming in C and working with circuits and digital logic. It has two parts. In the first part, you will design several circuits using a simple specification language. In the second part, you will write a program that generates a truth table given such a circuit specification.

Decide your data structures and algorithms first. Section 4 describes one method for implementing the program that performs well, but you are free to design your own. Writing out pseudocode is not required, but it may be a good idea.

Start working early. You will almost certainly encounter problems you did not anticipate while writing this project. It’s much better if you find them in the first week.

1           Overview
You will write a program truthtable that reads a file containing a description of a circuit, and prints that circuit’s truth table. The files specify (1) the number and names of each input to the circuit, (2) the number and names of each output from the circuit, and (3) the logic gates and components that make up the circuit. In order to indicate the connections between gates, each connection is also given a name.

For example, this is a description for a circuit that implements a 3-argument AND gate:

INPUT 3 a b c

OUTPUT 1 d

AND a b x

AND c x d

Note that it contains three inputs (a, b, and c), a single output (d), and two AND gates, each of which has two inputs and one output. The variable x indicates an internal connection between the first AND gate and the second. See section 3 for details on the specification language. When given this file, truthtable should print:

0 0 0 | 0

0 0 1 | 0

0 1 0 | 0

0    1 1 | 0

1    0 0 | 0

1 0 1 | 0

1 1 0 | 0

1 1 1 | 1

The three columns on the left correspond to the three inputs, and the column to the right corresponds to the output.

This assignment has two parts:

Part 1 (100 points)              For this part, the circuit descriptions will be sorted so that each temporary variable appears as an output parameter before any appearances as an input variable.

Part 2 (Extra Credit)            For this part, the circuit descriptions will not be sorted, meaning that a temporary variable may be used as an input parameter before its use as an output parameter.

2           Program
truthtable takes a single argument, which is the name of a file containing a circuit description. The behavior of truthtable is unspecified if it receives no arguments or more than one argument, but you should still check that the number of arguments is correct. (One possibility is to have truthtable read from standard input if no file argument is given.)

Usage

$ ./truthtable my-cool-circuit.txt

0 0 | 0 0

0    1 | 0 1

1    0 | 0 1

1 1 | 1 0

Input      The input to your program will be a single circuit description using the language described in section 3. The first argument to truthtable will identify a file containing this circuit description.

You MAY assume that the input is correctly formatted and that no variable depends on its own output.

Output   The output of truthtable is a truth table showing each combination of inputs and the corresponding output for the specified circuit. Each column in the table corresponds to a specific input or output variable, which are given in the same order as their declaration in the INPUT and OUTPUT directives. Columns are separated by a single space, and a vertical bar (|) occurs between the input and output variables.

Note that no white space follows the final column.

3           Specification Language
In this language, circuits are specified with a series of directives. These directives refer to various named variables, which correspond to wires in a circuit diagram. Many of the directives describe a logic gate or similar building block, indicating which varibles correspond to its input and output.

Each directive has the form of a name followed by one or more parameters, separated by whitespace. The name indicates which directive and determines the number of parameters. Some directives take a variable number of parameters; their first parameter will always be an integer which is used to determine the number of parameters. Depending on the directive, some parameters will be inputs and some will be outputs.

Variables in a circuit can be classified into three non-overlapping sets. Input variables must be declared by the INPUT directive, and may only occur as input parameters. Output variables must be declared by the OUTPUT directive and may occur exactly once in an output parameter. All other variables are temporary variables and must occur exactly once as an output parameter and zero or more times as an input parameter.

A variable name consists of a letter followed by zero or more letters or digits. You may assume that variable names are no longer than 16 characters.

In addition to variables, the constant inputs 0 and 1 may be used as input parameters. These are always false and always true, respectively.

Finally, _ may be used as the output of a gate, indicating that the output of a gate is discarded.

Note that whitespace may include one or more spaces, tabs, or newline characters. It is recommended to use fscanf to read the files, and to use a format code such as " %16s" to skip whitespace before reading the next variable or directive name.

By convention, we will use multiple spaces to separate the inputs and outputs of a gate, but this is not required and has no special meaning. It is purely for readability. Similarly, we will often put a blank line between OUTPUT and the first gate, but this is also done purely for readability. Your program should treat repeated newlines the same as single newlines (or, ideally, the same as any

whitespace).

Use of fgets is not recommended and will only make your life harder. Remember that fscanf is not limited to reading an entire line at once.

3.1           Directives
This section describes each of the directives used to describe a circuit. Each directive is followed by several parameters. A parameter n is always an integer and has a special meaning. Input parameters are indicated as i and output parameters are indicated as o. Ellipses (···) are used to indicate a variable number of parameters.

•    INPUT n i1 ···in

Declares n input variables. This directive must always occur first in a circuit description.

•    OUTPUT n o1 ···on

Declares n output variables. This directive must always occur second in a circuit description.

•    NOT i o

 

Represents a not gate in logic design. Computes o = i.

•    AND i1 i2 o

Represents an and gate in logic design. Computes o = i1i2.

•    OR i1 i2 o

Represents an or gate in logic design. Computes o = i1 + i2.

•    NAND i1 i2 o

 

Represents a nand gate in logic design. Computes o = i1i2

•    NOR i1 i2 o

 

Represents a nor gate in logic design. Computes o = i1 + i2

•    XOR i1 i2 o

Represents an xor gate in logic design. Computes o = i1 ⊕ i2, where ⊕ indicates exclusive or.

•    DECODER n i1 ···in o0 ···o2n−1

Represents an n : 2n decoder gate in logic design. The first argument gives the number of inputs, n. The next n parameters are the inputs, followed by 2n parameters indicating the outputs.

The inputs are interpreted as an n-bit binary number s in the range 0,··· ,2n − 1, where i1 is the most significant bit and in is the least significant bit. The output os will be 1 and all others will be 0.

•    MULTIPLEXER 

Represents a 2n : 1 multiplexer gate in logic design. The inputs to a multiplexer are either regular inputs or selectors, indicated by i and i0, respectively. The first parameter, n, gives the number of selectors. The next 2n parameters give the regular inputs, followed by n selector inputs, and finally the output.

The selector inputs are interpreted as an n-bit binary number s in the range 0,··· ,2n − 1.

The output is o = is.

•    PASS i o

Represents the absence of a gate. Computes o = i. This may be used to convert a temporary variable into an output variable.

3.2           Examples
This circuit describes a half-adder, where s is the sum and c is the carry.

INPUT 2 A B

OUTPUT 2 C S

AND A B C

XOR A B S

This circuit computes z = ab + ac:

INPUT 3 a b c

OUTPUT 1 z

AND a b x

AND a c y

OR x y z

Note that x and y are temporary variables, since they were not declared in INPUT or OUTPUT.

This circuit description is invalid, becuase it uses an output variable as an input parameter:

INPUT 3 IN1 IN2 IN3 OUTPUT 2 OUT1 OUT2

AND IN1 IN2             OUT1

OR IN3 OUT1            OUT2

This can be rewritten using PASS:

INPUT 3 IN1 IN2 IN3 OUTPUT 2 OUT1 OUT2

AND IN1 IN2                  temp1

PASS temp1                   OUT1

OR         IN3 temp1       OUT2

This circuit demonstrates the user of MULTIPLEXER:

INPUT 3 A B C OUTPUT 1 Z

MULTIPLEXER 3 0 0 0 1 1 0 1 1                           A B C        Z

As shown in class, this can be re-written to use a 4:1 multiplexer:

INPUT 3 A B C OUTPUT 1 Z

NOT                               C                       NC

MULTIPLEXER 2 0 C NC 1 A B                           Z

An equivalent circuit can be made using a 3:8 decoder:

INPUT 3 A B C OUTPUT 1 Z

DECODER 3 A B C                      _ _ _ p q _ r s

OR                         p q            t

OR                         r s             u

OR                         t u             Z

Note the use of _ for discarded outputs from the decoder.

typedef enum { AND, OR, NAND, NOR, XOR, NOT, PASS, DECODER, MULTIPLEXER } kind_t;

struct gate { kind_t kind; int              size;        // indicates size of DECODER and MULTIPLEXER int      *params; // length determined by kind and size;

// includes inputs and outputs, indicated by variable numbers

};

Figure 1: One possible data structure representing a logic gate

4           Implementation suggestions
There are many ways to design truthtable, but the most efficient way is to create a data structure that represents a circuit. Your program will read the circuit description file, create the corresponding data structure, and then use that structure to determine the output values for each combination of inputs.

Students commonly try to build the entire truth table before printing it. This is not the best strategy, because the truth tables grow exponentially with the number of inputs. (For example, test 1.10 will produce a truth table with 220 = 1048576 rows.) A better design is to generate and print the table one row at a time.

A few tips for creating fast implementations of truthtable.

•    When reading the circuit description file, assign a number to each new variable name and use a linked list or other structure to maintain a table that you can use to look up the number assigned to previously seen variables. Internally, refer to variables by number rather than name: integer comparisons are faster than string comparisons, and you can use variable numbers as array indices.

•    Carefully consider what information you need to represent a circuit. This will likely include the circuit’s input and outputs and the gates making up the circuit.

•    Carefully consider what data is needed to represent a gate. Design a structure that is general enough to represent any gate, and then write code that can handle any of the gates. This will likely include a code that indicates the type of gate and the input and output variables. See fig. 1 for one possibility.

•    In order to handle DECODER and MULTIPLEXER, your gates will need to work with a variable number of inputs and outputs. One possibility is to use an array of variable numbers representing the inputs and outputs along with a field indicating the size of the gate. For fixed-sized gates, such as AND, this number can simply be ignored and the array can be assumed to contain the correct number of inputs and outputs. For MULTIPLEXER and DECODER, one number is sufficient to determine how many inputs and outputs there are.

•    To generate a single row of the truth table, first assign values to the circuit inputs. Then, for each gate, determine the values for its outputs based on the values of its inputs. Once every gate has been handled, you will know the values for the outputs and can print the table row.

If you have assigned a unique number to each variable, then you can use an array to hold the values of all the variables.

•    For each row of the truth table, note that every input value will be followed by a space, and every output value will be preceded by a space.

•    When reading the circuit description, you may assume that the file is correctly formatted. Thus, after reading a directive name, you may assume that it will be followed by the correct number of parameters. Thus, it is acceptable to use the whitespace-skipping tokenizer and ignore line breaks.

•    When using a formatting string such as " %16s", the corresponding variable must be the address of an array containing a sufficient number of characters (17, in this case). While it is fine to use local variable to hold the result from fscanf, be sure to make a copy of the string if you intend to hold onto it.

•    There are several methods for dealing with circuit descriptions where the gates are not given in order. One way is easy to write, but slow. Another is more complicated, but fast. (Hint: the circuit can be thought of as a directed, acyclic graph, and you have learned an algorithm for ordering the nodes in a DAG.)


More products