Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

**This is code**

public class Computer {

private Memory ram;

private PC pc;

private IR ir;

private ACC acc;

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

}

}

//add( change) codes

public class Memory {

//attributes

private int[] contents;

private int size;

//constructor

public Memory(int size) {

this.size = size;

contents = new int[size];

}

//setter and getter

public void setValue(int address, int value) {

if(address >= 0 && address < size)

contents[address] = value;

else

System.out.println("Invalid address");

}

public int getValue(int address) {

if(address >= 0 && address < size)

return contents[address];

else{

System.out.println("Invalid address");

return -1;

}

}

//other methods

public void showContents(){

for (int i = 0; i < size; i++)

System.out.println(getValue(i));

}

public void loadProgram() {

/*

* contents[0] = 799;

* contents[1] = 598;

contents[2] = 198;

contents[3] = 499;

contents[4] = 1008;

contents[5] = 1108;

contents[6] = 899;

contents[7] = 909;

contents[8] = 898;

contents[9] = 0;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

How satisfied are you with this attitude?

Answered: 1 week ago

Question

=+7 How has the COVID-19 pandemic impacted the operations of IHRM?

Answered: 1 week ago