Question
// add codes or change some codes it needed public class Computer { private Memory ram; private PC pc; private IR ir; private ACC acc;
// add codes or change some codes it needed
public class Computer {
private Memory ram;
private PC pc;
private IR ir;
private ACC acc;
// changes code if needed
public Computer() {
ram = new Memory(100);
pc = new PC(0);
ir = new IR(0);
acc = new ACC(0);
}
private void fetch() { // IR = Memory[PC]
ir.setValue(ram.getValue(pc.getValue()));
}
private void incrementPC() {
pc.increment();
}
private boolean execute() { // decode IR and execute based on OpCode and Operand
boolean stop = false;
switch (ir.getOpCode()) {
case 0: // Stop
stop = true;
break;
case 1: // Load - ACC = Memory[Operand]
acc.setValue(ram.getValue(ir.getOperand()));
break;
case 2: // Store - Memory[Operand] = ACC;
ram.setValue(ir.getOperand(), acc.getValue());
break;
case 3: // Add - ACC = ACC + Memory[Operand]
acc.add(ram.getValue(ir.getOperand()));
break;
case 4: // Subtract - ACC = ACC - Memory[Operand]
acc.sub(ram.getValue(ir.getOperand()));
break;
case 5: // Multiply - ACC = ACC * Memory[Operand]
acc.mult(ram.getValue(ir.getOperand()));
break;
case 6: // Divide - ACC = ACC / Memory[Operand]
acc.div(ram.getValue(ir.getOperand()));
break;
case 7: // Input - Memory[Operand] = input
ram.setValue(ir.getOperand(), Terminal.input());
break;
case 8: // Output - output = Memory[Operand]
Terminal.output(ram.getValue(ir.getOperand()));
break;
case 9: // Unconditional Branch - PC = Operand
pc.setValue(ir.getOperand());
break;
case 10: // Branch greater
if (acc.getValue() > 0)
pc.setValue(ir.getOperand());
break;
case 11: // Branch equal
if (acc.getValue() == 0)
pc.setValue(ir.getOperand());
break;
default: // Not an instruction - could throw their own exception
Terminal.output("Error - not an instruction ");
}
return stop;
}
private void cycle() {
boolean stop = false;
while (!stop) {
this.fetch();
this.incrementPC();
stop = this.execute(); // if opcode = stop instruction, will become true
}
}
public void run() {
ram.loadProgram();
pc.setValue(0);
this.cycle();
}
public void run(String s){
//add codes
}
}
Till now, we have our computer simulator running the default program, which means that it always executes the same code that is loaded into memory using the method LoadMemory (this method was created in the Memory class from Lab P1). To be more realistic, we should load into the memory the machine code corresponding to a program file that is saved somewhere on our computer, e.g. on our Hard Disk Drive.
The objective of this lab is to provide the necessary tools to read and execute any executable files and to integrate that into our simulated computer.
Note that the executable files are produced as a result of a compilation process which reads the source code of a program from one file and converts it to corresponding machine language in another file. We assume that the file with the machine language code is given for this assignment. The compilation process itself is left for the next assignment (P5).
A) Computer Simulation
A sample program file program.ex1 is attached to this lab. Overload the method(s) needed in the Computer class and any other class in order to load into the memory the code from ANY given file (passed as a String argument to the run method see (B) below). Assume the instructions in the executable file (e.g. program.ex) are written using writeInt() method.
Note that you should still have a method run() that takes with no parameters in case you want to load the default program from Lab P1 to the memory.
Marking Guide:
+1 marks for overloading the run method.
+3 marks for updating the correct classes (you need to indicated by a comment near the top of each class whether this class has been updated or it still uses the same code from P3).
+4 marks for updating the code to read a given executable file and storing it into the memory.
+4 marks for properly handling the exceptions while reading the executable file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started