cs270 Spring 2014 Programming Assignment 4 - Simulating Digital Logic

Programming due Saturday, Mar. 1 at 11:59pm, no late submissions.


This assignment has three objectives:
  1. To learn how to simulate digital logic with C functions,
  2. to learn how to write unit tests in C using asserts,
  3. and to increase your understanding of circuits that are fundamental to computer hardware.

About the Assignment

For this assignment you will write a C program to simulate gates, latches, decoders, multiplexers, and an adder. The homework questions in the PA4.QandA file should be answered in that file and submitted in the tar ball with the code files: main.c, logic.h, and logic.c. The logic.h and logic.c files already contain declarations, implementations, and the main program contains a unit test for each of the following functions:
    // Gates
    BIT or_gate(BIT A, BIT B);
    BIT not_gate(BIT A);

    // Sequential Circuits
    BIT rs_latch(BIT S, BIT R);
The header file also defines a BIT as an enumerated value. Add the functions shown below to the logic.c file, and corresponding tests to the main.c program. Note that O1 and O2 inputs to the adder function are 4-bits and unsigned, so you only need to worry about input values between 0 and 15.
    // Gates
    BIT and_gate(BIT A, BIT B);
    BIT xor_gate(BIT A, BIT B);

    // Sequential Circuits
    BIT d_latch(BIT D, BIT WE);

    // Combinational Circuits
    int adder(int O1, int O2,  BIT carryIn, BIT *carryOut);
    BIT multiplexer(BIT A, BIT B, BIT C, BIT D, BIT S1, BIT S0);
    void decoder(BIT A, BIT B, BIT *O0, BIT *O1, BIT *O2, BIT *O3);
To compile all files into a program called PA4, type the following command:
    $ make
To run the compiled program, type the following command:
    $ ./PA4
Part of this assignment is to make sure that each test is exhaustive for the gate or circuit being tested, i.e. all combinations of inputs should be tested, except for the multiplexer and the adder circuits, where you only need to test the cases shown. NOTE that you will need to determine what the correct output should be in each case and encode that in your unit test case.

MULTIPLEXER

A B C D S1 S0 Output
0 0 0 0 0 0
1 1 1 1 1 1
1 0 1 0 1 0
0 1 0 1 0 1

ADDER

O1 O2 Carryin Output Sum Output Carryout
0101 0101 0
0101 0101 1
0001 0111 1
0111 0111 0
1101 1101 1

Getting Started

Copy the following five files into the PA4 working directory. It is easiest to right click on the link and do a Save Target As.. for each of the files: Open a terminal and cd to the directory created in step 1.
In the terminal, type make to build the executable. You should see the following output:

Compiling each C source file separately ...
gcc -g -std=c99 -Wall -O0 -c main.c

Compiling each C source file separately ...
gcc -g -std=c99 -Wall -O0 -c logic.c

Linking all object modules ...
gcc -g -std=c99 -Wall -O0 main.o logic.o  -o PA4 
In the terminal type ./PA4 to see that the program currently runs. The output should be as follows:

===== Testing not_gate =====
===== Testing or_gate =====
===== Testing rs_latch =====
    
As long as none of the asserts in main.c do not cause the program to halt, the implementations are correct.

You have a functioning program. There are some example unit tests in main.c and some example implementations in logic.c. You need to write comments in all of the source files, implement the rest of the functions specified in logic.h, write unit tests for all of the functions in main.c. Copy the methodology of asserts from the existing code in main for your test cases. Also, answer the questions in PA4.QandA. Keep in mind that the unit test cases need to be exhaustive for all of the functions except for the multiplexer and the adder, as described above.


Submission instructions

You will be submitting a single file PA4.tar using the Checkin tab./b>. The tar ball should include the following files: logic.c, logic.h, and main.c. Bring the PA4.QandA file to class on March 4 for a group discussion. The file PA4.tar is made by the Makefile, just type make package. Check your source files to make sure you have put comments in all of the source files.


Reminders


Grading Criteria

To grade the assignment, we will verify that your logic functions work correctly (50 points), and that your test cases are comprehensive (25 points). In addition points will be given for coding style and comments (10 points), and following assignment directions (15 points). The grading factors we consider for coding style include having clear and concise comments, consistent indentation, and avoidance of excessive code to solve the problem. NOTE: No preliminary testing is setup for PA4, since much of the assignment is for you to test your own code!

© 2014 CS270 Colorado State University. All Rights Reserved.