Collaboration
For assignments in CS 2400, you will complete each of them on your own or solo. However, you may discuss high-level ideas with other students, but any viewing, sharing, copying, or dictating of code is forbidden. If you are worried about whether something violates academic integrity, please post on Ed or contact the instructor.
Setup
If you haven’t already, you should follow the VMWare setup.
Once you have the VM setup, you should boot it up, and download the appropriate tar file based on your machine. To download them, you should open the terminal (which should be an application on the right hand side of the desktop) and use the provided command:
wget https://www.seas.upenn.edu/~cis2400/22fa/projects/code/hw5.tar
After downloading the tar file, you should be able to type in the command “ls” in the terminal, or use the file explorer to see the download tar file. Once you confirm that the file is downloaded, run the following command to decompress the files:
tar -xvf hw5.tar
Alternatively, you can download the files directly here:
Instructions
For this assignment, we are asking you to complete a function in C that “decodes” the control signals for five diffferent instructions. This is not a typical programming assignment, it is much more like the assignments on canvas in terms of what we are asking you to do, the course staff just decided to use C as a medium for this assignment. If you would like to read more about why we think doing this in C was a good idea, feel free to read more here Why C?.
This class has Java programming as a pre-requisite and if you still recall Java, there should not be much here that you haven’t seen before. Please refer to the C section below for some information on what is going on with the signals
data type.
Implementation
There are four files associated with this homework assignment, you will only need to modify decoder.c
to complete this assignemnt. </code>main.c</code> is provided to give you a way to test/debug your code locally. The other files Makefile
and decoder.h
and should not be modified.
decoder.c
is the main file you will be interacting with this in this assignment. The function decode_signals()
is the only function you need to modify for this assignment. In that function, we provide a signals
variable that will need to have it’s fields appropriately set for the instruction type that is passed in to the function. We provide an example that handles the ILLEGAL_INSN
instruction type where every signal in set to be ANY_SIGNAL
which is the equivalent of saying “It doesn’t matter” for that type.
Your job in this assignment is to add code so that when instructions of type ADDI, BRnz, JSR, CMPU, or STR are passed in to decode_signals()
the correct control signals are set and returned.
Note: there are two types of ADD instruction in LC4, one that uses Rd, Rs and Rt, another that uses Rd, Rs, and an integer immediate. To distinguish the two, the variant using Rt is called ADD and the variant using an integer immediate is calle ADDI.
Note that you should set each signal to its corresponding integer value but if a signal doesn’t matter, it should be set to the constant we provided called ANY_SIGNAL
.
decoder.h
is a file that contains the declarations for the enum and strut types used in this assignment. There are comments in this file to document the type, but the example code provided in decode_signals()
should be enough to give you an idea of what is going on. We also have a section below on structs that you can refer to. Note that the signals
struct type is likely the type you will need to look at the most for this assignment.
Compiling and Testing
We have provided a sample file for you to run & debug locally. There are also public tests on gradescope that can be used for verifying the correctness of your solutions.
To test that your code compiles, you just need to be in the same directory as your HW5 files in the terminal and type the make
command. If you don’t encounter any errors, then you should see something like this:
$ make
gcc -g -Wall -std=c11 -c decoder.c
gcc -g -Wall -std=c11 -o main main.c decoder.o
$
If there were any warnings or errors in the compilation process, it would be printed out there. You can also run modify main.c
to test different inputs and print their values and then run the code by typing in ./main
after compiling your code. main.c
already has some example code to show how you can run your code with the ADDI instruction.
Note that we provide a function for you called print_type()
that you can use to print out the instruciton type. You should not have to modify this function.
C
Java takes a lot of design inspiration from C. As a result, if you recall how to do Java programming, C should feel very similar. This section just includes some details that are different:
printf
Printing in C is done differently than in Java, and since we still haven’t talked about C directly in the course, we are including the same text on printf
as we did in HW01. Note that if you want to print the the instruction type, we provide a function for you called print_type()
that you can use. You should not have to modify this function.
printf
is a C function used for printing output when the program runs. This is comparable to System.out.println
from Java. Since we haven’t covered C yet, here is some basics of using printf
that should cover your printing needs for this assignment.
Assume we have an integer variable int a;
defined somewhere in our code. If you wanted to print out the decimal value of a
on a new line, you would write the line of code: printf("%d\n", a);
.
We can make our print message have a little more context by modifying the string passed in to printf
. An example of this would be: printf("The value of a is %d\n", a);
.
If we want to print out the value of a in hexadecimal, we just need to replace the %d
with %X
. For example printf("%X\n", a);
. Note that we cannot print a
in binary just using a single call to printf
. If you want the exact binary, we recommend you print the value in hexadecimal and convert hexadecimal to binary.
Structs in C
In this assignment, you will also have to work with the data type called signals
. signals
is a struct type which (for now) can be thought of as similar to an object type in Java, but it has no associated methods or constructors, and can only hold data members/fields. Our struct has 11 members in it, each corresponding to a signal that needs to be set.
You can set a value of a data field in a sruct using the similiar .
notaiton used for setting fields in Java objects. An example of this is provided in the decode_signals()
function.
Why C
This assignment was done in canvas as a quiz in past offerings (similar to the check-ins). We decided to shift to C since there were many complaints about the canvas quiz format:
- It did not allow someone to save their progress and return to the quiz without having to re-input each of the 55 signal values.
- If you later changed your mind on an answer or wanted to fix your submission, you had to put in every signal again.
As a result, we looked to other formats. There are many to choose from and we considered many (like having people fill in a spreadsheet and upload that), but we decided to do C programming just to give you some more “gentle” exposure to C so that when we engage with C later in the semester, it won’t be as intimidating.
Submission
Please submit all of your decoder.c
to Gradescope