C++ programming

profileshresthadeepu75.
instruction1.doc

For this assignment, to get all of the functions of the simulator working, you need to perform the following tasks in this order. I give an outline of what should be done here to write each member function of the simulator. There are additional hints in the template files given as comments that you should look at as well for additional tasks you will need to perform that are not described here.

1. Implement the initializeMemory() function. You can pass these unit tests by simply initializing the member variables with the parameters given to this function. However, you also need to dynamically allocate an array of integers in this function that will serve as the memory storage for the simulation. You should also initialize the allocated memory so that all locations initially contain a value of 0. If you are a bit rusty on dynamic memory allocation, basically you need to do the following. There is already a member variable named memory in this class. Memory is a type int* (a pointer to an integer) defined for our HypotheticalMachineSimulator class. If you know how much memory you need to allocate, you can simply use the new keyword to allocate a block / array of memory, doing something like the following

memory = new int[memorySize]; There are some additional tasks as well for this first function. You should check that the memory to be initialized makes sense in terms of it size for this simulation.

2. Implement the translateAddress() function and get the unit tests to work for this test case. The translateAddress() function takes a virtual address in the simulation memory address space and translates it to a real address. So for example, if the address space defined for the simulation has a base address of 300 and a bounding (last) address of 1000, then if you ask to translate address 355, this should be translated to the real address 55. The address / index of 55 can then be used to index into the memory[] array to read or write values to the simulated memory. There is one additional thing that should be done in this function. If the requested address is beyond the bounds of our simulation address space, you should throw an exception. For example, if the base address of memory is 300, and the bounds address is 1000, then any address of 299 or lower should be rejected and an exception thrown. Also for our simulation, any address exactly equal to the upper bound of 1000 or bigger is an illegal reference, and should also generate an exception.

3. Implement the peekAddress() and pokeAddress() functions and pass the unit tests for those functions. These functions are tested by using poke to write a value somewhere in memory, then we peek the same address and see if we get the value we wrote to read back out again. Both of these functions should reuse the translateAddress() function form the previous step. In both cases, you first start by translating the given address to a real address. Then for poke you need to save the indicated value into the correct location of your memory[] array. And likewise for peek, you need to read out a value from your memory[] array and return it.

4. Implement the fetch() method for the fetch phase of a fetch/execute cycle. If you are following along in the unit test file, you will see there are unit tests before the fetch() unit tests to test the loadProgram() function. You have already been given all of loadProgram(), but you should read over this function and see if you understand how it works. Your implementation of fetch should be a simple single line of code if you reuse your peekAddress() funciton. Basically, given the current value of the PC, you want to use peekAddress() to read the value pointed to by your PC and store this into the IR instruction register.

5. Implement the execute() method for the execute phase of a fetch/execute cycle. The execute phase has a lot more it needs to do than the fetch. You need to do the following tasks in the execute phase:

· Test that the value in the instruction register is valid

· Translate the opcode and address from the current value in the instruction register.

· Increment the PC by 1 in preparation for the next fetch phase.

· Finally actually execute the indicated instruction. You will do this by calling one of the functions

executeLoad(), executeStore(), executeJump(), executeSub() or executeAdd()

To translate the opcode and address you need to perform integer division and use the modulus operator %. Basically the instruction register should have a 4 digit decimal value such as 1940 in the format XYYY. The first decimal digit, the 1000’s digit, is the opcode or instruction, a 1 in this case for a LOAD instruction. The last 3 decimal digits represent a reference address, memory address 940 in this case. The translation phase should end up with a 1 opcode in the irOpcode member variable, and 940 in the irAddress member variable. You should use something like a switch statement as the final part of your execute() function to simply call one of the 5 member functions that will handle performing the actual instruction execution.

6. Implement the executeLoad(), executeStore(), executeJump(), executeSub() and executeAdd() func- tions. Each of these has individual unit tests for them, so you should implement each one individually. All of these should be relatively simple 1 or 2 lines of code function if you reuse some of the previously implemented function. For example for the executeLoad() function, you should simply be able to use peekAddress() to get the value referenced by the irAddress member variable, then store this value into the accumulator.

7. Finally put it all together and test a full simulation using the runSimulation() method. The final unit tests load programs and call the runSimulation() method to see if they halt when expected and end up with the expected final calculations in memory and in the AC. Your runSimulation() For this assignment you have been given the code for the runSimulation() method, but the code is commented out because it relies on you correctly implementing the above functions first to work correctly. Uncomment the code in the runSimulation() method and the final unit tests should now be passing for you.