Need this code in C++ in CLion. All the code should be combined into one document. In this task, you will build your own computer! Not a real one, but a simulated one, according to the specification below.
The Xyclotron-001 is a very simple computer, with a limited set of machine code instructions, and a small amount of free memory in which it stores both its machine code program and the data that the program operates on. (The Xyclotron can only keep one program loaded in memory at a time, so multitasking is beyond its reach for now.)
The Xyclotron's memory consists of 100 words. Each word can store a value from 0 to 9999. The value of a word can be conveniently represented as a 4-digit number, e.g. 9876, 0012, 0902, etc. Values can be retrieved from memory by using the memory location as an index, e.g. memory[3] contains the value stored in the 4th location in memory (counting starts from 0).
The Xyclotron has 3 registers with which it performs its computations:
- a general-purpose register known as the accumulator
- a secondary register for storing temporary values, known as T
- a program counter, which stores the memory location of the next command to be executed
Xyclotron programs perform calculations by transferring values around between the accumulator, the temporary register T, and any open locations in memory (i.e. locations that are not in use by the program). Various arithmetic operations (adding, subtracting) can be performed on these values, and the Xyclotron can produce output by printing numbers to the screen.
The Xyclotron follows a fetch-decode-execute cycle for running a program stored in memory:
1. Fetch: The computer retrieves from memory the value of the word that is stored at the location indicated by the program counter.
2. Decode: It attempts to interpret the value as a valid machine code instruction.
3. Execute: If decoding is successful, it attempts to perform the instruction and increments the program counter (unless the instruction has explicitly modified the program counter already).
The Xyclotron has a machine code language with just 13 different commands. These are described in the table below.
When a word has been fetched from memory, the decoding step breaks it up into two equal parts. The part represented by the first two digits of the word is the operation code or opcode. This is a number from 00 to 99 representing the particular instruction. Only the 13 opcodes as shown in the table are valid. The last two digits represent the operand, i.e. the value on which the instruction will be carried out.
Example: if the current location in memory contains the word 1109, this is broken up into opcode = 11 and operand = 09. Because 11 corresponds to the SUB instruction, the purpose of the instruction will be to modify the value that is currently in the accumulator by subtracting from it the value stored in location 9 in memory. So if the accumulator contains the value 7 and memory location 0009 contains the value 12, then the result of the command is that the accumulator will contain the value -5.
Note that T and the accumulator can contain negative values; however, memory locations can store only zero or a positive integer.
Your task is to implement a simulation of the Xyclotron-000. The simulated computer will read a program from cin (terminating with EOF). All Xyclotron programs consist of a series of 4-digit numbers, one number per line, and have the file extension .xyc. A few example programs have been provided (see below).
You will do this by implementing Xyclotron.cpp, following the specification given in Xyclotron.h. To test your program, run the main method in main.cpp.
You will not modify main.cpp for this task. You may need to add methods to Xyclotron.h.
You are given implementations of the two static methods formatted_trace() and opcode_to_mnemonic() in Xyclotron.h, which you can use in your own program.
(NOTE: Because of a small design error, and also in order to facilitate programming by human programmers, the Xyclotron cannot access a program statement located at memory location 0, and cannot store data there either. Hence, all Xyclotron machine code programs start running at the instruction stored in the 2nd location in memory, i.e. at memory location 1.)
The program prog1a.xyc loads the number in location 05 (i.e. line 5 in the program) into the Accumulator. Next, it adds the number in location 06, prints out the accumulator value (which is now 1180) to the screen, and terminates. Note that the program does not continue beyond the HALT statement in line 4.
The program prog1b-1.xyc stores X in location 6 and Y in location 7, and prints out the value of X - 2*Y.
Program prog2a.xyc stores X in location 2, Y in location 3, and Z in location 4, and M1 in location 5, and M2 in location 6. If X+Y=Z, it outputs M1; otherwise, it outputs M2.
Program prog2b.xyc stores X in 2, and Y in 3, and the messages M1 to M3 in locations 4 to 6. If X = Y, it prints M3. If X > Y, it prints M1. If X < Y, it prints M2.
For Levels 1 and 2, submit your Xyclotron.cpp and Xyclotron.h file. Do not submit main.cpp!
Level 1:
Implement the fetch-decode-execute cycle of the Xyclotron. Get the following commands to work: LOAD, ADD, SUB, PRINT, and HALT. These should allow you to run example programs prog1a and prog1b.
Also, implement memory_dump(), which prints out a dump of the current values in memory when the command line argument -dump is passed in.
Level 2:
Implement the remaining opcodes. Implement tracing: when the command line argument -trace is passed in, the computer will print a trace of the current line to the screen. The format is given by formatted_trace(). After the trace is printed, the instruction is performed (including any output). Tracing may help you to debug your program.
Levels 3 and 4:
For these levels, your submission will be two .xyc programs, called loop.xyc and prime.xyc, as well as a PDF document describing your solutions.
Your task here is to write programs in the Xyclotron machine language, which I will test by hand. (We do provide automated tests for these, but these are not auto-graded).
Level 3: write loop.xyc,