Little Computer


General

  • All submissions must include a work history to be worth credit.
  • Submit all files to Canvas. Make sure to name file(s) EXACTLY as specified - including capitalization.
  • Make sure your program accepts input in EXACTLY the order and format given. Do not ask for extra input or change the order of inputs.
    If you want to make an expanded version of a program that and get feedback on it, email it to me or show me in class.
  • Slight variations in output wording/formatting are usually fine (as long as formatting isn't part of the assignment).
    If you do not get the right final output, print out some evidence of the work your program did to demonstrate your progress.
  • Readability and maintainability of your code counts. Poor formatting, confusing variable names, unnecessarily complex code, etc… will all result in deductions to your score.

Objective

Upon completion of this assignment the student will be able to create classes to represent complex data types.

Requirements

Submit file: assign1.zip

For each assignment, you will submit a .zip archive of your project folder including all its contents.

If needed you can review instructions on making a .zip archive. Make sure to compress your assign1 folder, not just your code file.

I should be able to build your code with:

g++ -g -std=c++17 LittleComputer.cpp tests.cpp -o tests.exe

Your code MUST compile with an unmodifed version of the provided tests (see below).

Setup

Make a Unit Test project and replace the contents of tests.cpp with the contents of this file tests.cpp.

Since I will use my own version of tests.cpp, do not modify it to match your code! You can modify the tests if you want to test out something not already checked in them, but make sure that your code builds against the provided file.

Then add a two new files. Name one LittleComputer.h and the other LittleComputer.cpp. Add the two files to the HEADERS and TEST_FILES sections of the Makefile.

# list .h files here
HEADERS = LittleComputer.h

# list .cpp files here
TEST_FILES = tests.cpp LittleComputer.cpp

Overview

For this assignment, you are going to implement a version of the Little Computer - a very simple model of how machine level code works. (if you did not take CS160 and the description below is too brief, you can see demonstrations/read more about it in Welcome to CS).

In the files LittleComputer.h and LittleComputer.cpp, you should implement the class LittleComputer shown in the UML diagram below.

LittleComputer Class

The LittleComputer class should not have any input or output code (no cin/cout).

Start by stubbing out all the functions (see list below below). A stub version of a function is one that just returns some default value without even trying to do its job correctly.

  • A stub constructor or void function does nothing in its body
  • A stub function that returns a value should just returns some default value like 0 or false or ""

Once you can compile against all the tests, you can start worrying about trying to actually pass them. If your program does not compile because you are missing functions, I will not be able to verify the rest of your code works!

Implementation Details

Variables

Memory should be an array of 20 integers - we will never try to load a program with more instructions than that. We will also never load a program that tries to access memory locations lower than 0 or greater than 19.

The programCounter or PC represents the instruction that will be executed next. i.e. if the programCounter is 1 and the computer does a step, the instruction at 1 will be executed.

The accumulator will store the value of the most recent computation done by the Little Computer.

You should not need other class level variables. If you add any, make sure they do not duplicate existing information or anything that can be determined from existing information.

Functions

Below are descriptions of what the functions should do. You can add extra functions if you like, but must implement the ones listed in the UML and they must behave as described below

The LittleComputer.h file should have Doxygen comments for each function.

LittleComputer()

Initializes accumulator (ACC), program counter (PC) and all memory slots to 0.

There is no magic way to initialize all elements of an array to 0 with one statement inside the constructor. You need to initialize the array where it is declared or use a loop to initialize the elements in the constructor.

LittleComputer(int instructions[], int numberOfInstructions)

This takes an array that contains a list of instructions and a size of the array. The instructions from the array provided should be copied into the computer's memory starting at address 0.

The accumulator and program counter, as well as any memory after the list of instructions, should be initialized to 0.

int getProgramCounter() / int getAccumulator()

Return the value of program counter or accumulator.

int getMemoryAt(int location)

Return the value in memory at the indicated location.

int getCurrentInstruction()

Return the value in memory at the location indicated by the program counter. (The instruction that will run when we step the computer).

bool isHalted()

Returns true if the next instruction to be run is 0, otherwise false

void restart()

Resets the accumulator and program counter to 0 but leaves memory unchanged.

void step()

Asks the LittleComputer to run the current instruction (the one indicated by the program counter). Instructions for the little computer are numbers - those numbers should be interpreted as shown in this diagram:

LittleComputer instruction guide

For example, 102 means "add the value from location 2 to the accumulator".

You are NOT responsible for implementing the 8XX, 901, or 902 instructions

After running any instruction except a halt or successful branch, the program counter should increase by 1.

Here are some tips:

  • 000 in that chart is the same as 0. Nothing should happen when this is run - the computer will stay at the same instruction and do nothing.
  • The 100's digit always indicates the basic operation.
  • The next two digits specify a memory address (xx) the instruction will use
  • add / subtract take the value at a memory address and add to or subtract from the accumulator. e.g. "104" says add the value at memory address 4 to the value that is already stored in the accumulator. It does NOT mean add the number 4.
  • store copies the accumulator value into the memory address specified by XX, while load copies the value at the memory location XX to the accumulator (replacing the old value)
  • 6XX branch always changes the program counter to hold the value in XX. The program counter does not increase by 1 after that.
  • 7XX branches if 0 must test the value in the accumulator. If it is 0, do the branch (by changing the program counter to XX). If the accumulator has a non-zero value, the LittleComputer does not branch and just moves on to the next instruction (increase program counter by 1).