Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. Project - Instruction Cycle
  2. Goal
  3. The goal of this program is to simulate a simple computer processor. The processor will read a program consisting of 32-bit instructions, decode each instruction, and then perform each operation and store the values in registers. Registers are temporary storage locations often referenced by names such as R0, R1, R2, etc. The program will allow users to run a binary program on a set of registers and display the results, mimicking a real computer!
  4.  
  5. When C programs (and many other languages) are compiled, they are converted to instructions — simple operations that processors execute. Most instructions specify an operation to be performed and the data to be used in that operation. Processors often store data in registers — temporary storage locations that are referenced by name or number in the instruction, as shown in the example below. This instruction adds the contents of registers 0 and 1 (the source operands) and stores the result in register 2 (the destination operand):
  6.  
  7.  
  8. ADD R2, R0, R1
  9.  
  10. In practice, each instruction is encoded as a bit sequence; the processor decodes those bits to determine the operation and operands used for each instruction. Each possible operation is assigned a number, or opcode — for example, 1 might represent addition. Registers are usually referred to by a number.
  11.  
  12. ---
  13.  
  14. Please place this header in all of your java source files:
  15.  
  16. /* PROGRAMMER:
  17. * CLASS: AP Computer Science
  18. * ASSIGNMENT:
  19. *
  20. * CERTIFICATION: I certify that this work is my own and that
  21. * none of it is the work of any other person.
  22. */
  23.  
  24.  
  25. Specification
  26. Use the following steps to guide your program development. Pay attention to data types and the necessary arithmetic operations. Test your programs thoroughly! This means trying a variety of input values within the possible range described below.
  27.  
  28. Create a new Netbeans Project called LastNameFirstInitial_Project4, where LastName is your last name.
  29. Create the following classes and implement them using the descriptions provided.
  30. Test your program on multiple inputs and validate that all methods, constructors, and functionalities work as intended.
  31. All classes described below may have any number of methods or instance variables created or used, as long as it follows the Java standards learned in class.
  32.  
  33. Class: Registers
  34. Registers are temporary storage directly on a CPU. This allows for extremely fast computation without the need to ask for data from RAM. This class will represent a set of registers that will be used in a given program and its instructions.
  35.  
  36. Registers should hold a given number of int values in a single instance variable.
  37. A program should always know the next instruction to execute.
  38. A special register called the Program Counter should exist to keep track of the next instruction to be run.
  39. Create a default constructor that should assume 32 registers all containing a value of 0.
  40. Create a constructor that accepts a number of registers and sets all their values to 0.
  41. A method should exist that accepts a register number and returns the value stored within it.
  42. A method should exist that accepts a register number and value, and stores that value at the given register.
  43. A method should exist that prints the contents of all registers in a format like:
  44. R0 = 1
  45. R1 = 1
  46. R2 = 5
  47. R3 = 10
  48.  
  49. Class: Instruction
  50. An instruction is a single line of 32 binary digits that represents an operation to perform with values in registers and where to store the resulting value. While real world CPU instructions are very complex and have a variety of formats, all instructions used in this project follow the same format which is:
  51.  
  52. <Register> = <Register> <Operator> <Register>
  53. Or
  54. <Register> = <Register> <Operator> <Value>
  55. Or
  56. <Operator> <Register> <Value>
  57.  
  58. For example:
  59. R0 = R1 - R3
  60. R2 = R3 << 4
  61. LD R4 15
  62. R2 = R0 + R32
  63.  
  64. All Instructions should maintain their binary representation in an instance variable.
  65. Create a constructor that accepts a binary representation of an instruction to be kept.
  66. Most binary instructions can be broken down into the following sections:
  67.  
  68. example: 00001 000010 000000 100000 000000000
  69. operation destination src1 src2 shift amount
  70. 5 bits | 6 bits | 6 bits | 6 bits | 9 bits
  71.  
  72. An Instruction should be able to print out its binary representation
  73. Depending on the operation, the binary can be broken into its respective parts
  74. Destination
  75. Source 1
  76. Source 2
  77. Shift amount
  78. An instruction should be able to be executed, given a set of registers to manipulate
  79. Create a method that accepts a Registers object. The evaluation of the instruction will affect only these registers.
  80. An Instruction should be able to print out its human-readable representation, which follows the format:
  81. <Register> = <Register> <Operator> <Register>
  82. E.g. R5 = R4 * R4
  83. Note: based on which operation is being executed, show the appropriate symbol
  84.  
  85. The following are all valid operations and their decimal equivalent values that may be used in an instruction:
  86.  
  87. ADD (+) → 1 → adds two values together
  88. E.g. destination = source1 + source2
  89. SUB (-) → 2 → subtracts two values
  90. E.g. destination = source1 - source2
  91. MULT (*) → 3 → multiplies two values together
  92. E.g. destination = source1 * source2
  93. DIV (/) → 4 → divides two values
  94. E.g. destination = source1 / source2
  95. LEFT SHIFT (<<)→ 5 → shifts the source1 value binary left by the shift amount given
  96. E.g. destination = source1 << shftAmt
  97. Example: 1001b << 1 ⇒ 10010b
  98. Example: 10b << 3 ⇒ 10000b
  99. RIGHT SHIFT (>>) → 6 → shifts the source1 value binary right by the shift amount given
  100. E.g. destination = source1 >> shftAmt
  101. Example: 1010b >>1 ⇒ 101b
  102. Example: 101101b >> 3 ⇒ 101b
  103. ROTATE (ROT) ⁠⁠⁠→ 7 → rotates the source1 value binary right by the shift amount. Bits on the right end of the value move to the left of the value.
  104. E.g destination = source1 ROT shftAmt
  105. Example: 11001b ROT 3 ⇒ 00111b
  106. Example: 1001b ROT 1 ⇒ 1100b
  107. LOAD (LD) → 8 → loads the shift amount value into the destination register. Both sources are ignored.
  108. E.g. destination = shftAmt
  109. JUMP (JMP) → 9 → sets the next instruction to run to the shift amount if the value in source 1 is equal to the value in source 2. The destination register is ignored.
  110. E.g. JMP R0 R4 7
  111.  
  112. Class: Program
  113. A Program will represent a set of related and ordered instructions that are to be run sequentially.
  114.  
  115. A program must keep track of the Registers it is using during its execution.
  116. A program should keep track of all instructions that are part of the program in an instance variable.
  117. Create a method to add a single instruction to your program.
  118. An instruction added to the program should be the last instruction in the collection of instructions that exists.
  119. Because you do not know how many instructions will be added, you must be aware of how many instructions currently exist, how many can still be added safely, and if necessary expand how much space is available so new instructions can be added. (Arrays are fixed size, so how do you handle this?)
  120. Programs should have a method called execute that accepts no parameters, but runs every instruction one at a time.
  121. Display what number instruction you are running
  122. Display the binary representation of the instruction
  123. Display the human-readable representation of the instruction
  124. Execute the instruction on the existing registers
  125. Example output:
  126.  
  127. Instruction 1: 00000100010000000000100000000000
  128. R2 = R0 + R1
  129.  
  130. Instruction 2: 00000100011000010001000000000000
  131. R3 = R1 + R2
  132.  
  133. Instruction 3: 00000100100000100001100000000000
  134. R4 = R2 + R3
  135.  
  136.  
  137. Class: ProgramTester
  138.  
  139. Program Tester will read two files to set up our simulation. The first file will be used to create a set of Registers and load them with initial values. The second file will be the binary instructions that can be passed into a Program. Once both are read, the program can be executed.
  140.  
  141. Create a Scanner to read the register init file.
  142. A register init file will have the following format:
  143. The first line is a single number: the number of registers that can be used.
  144. Then each subsequent line will be an integer value that represents the initial value for each register, starting with register 0. There will be exactly as many values/lines as the number given in the first line of the file.
  145. Create a Registers object from the information in the given register init file.
  146. Print the contents of the registers to verify the initialization and file reading went correctly.
  147. Create a Scanner to read the program binary file.
  148. A program binary file will be any number of lines where each line is a single binary string of 32-bits.
  149. Create a Program object and add each line read from the file to it.
  150. Execute the program using your execute method.
  151. Print the contents of the registers to verify their contents after all operations have occurred.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement