Question

In: Computer Science

Write the following methods in java class ARM that represent state information as well as functional...

Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform.

[Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please don't copy the answer from another question here on chegg. Thank you so much in advance.

--As an example the following store signature is provided:

public void str(int Rd, int Rn, int o, boolean pre, boolean mod_pre)

STR r0,[r1,#10] is the pre address store which would look like:

str(0,1,10,true)

STR r0,[r1,#10]! is a pre address store with an update which would look like:

str(0,1,10,true,true)

------------------------------------------------------------------------------------------------------------------------------------------

public class ARM {

    private RegisterBank rb;

    private RAM mb;

   

    public ARM(int size_reg, int bits_reg, int size_ram, int bytes_ram){

        rb = new RegisterBank(size_reg,bits_reg);

        mb = new RAM(size_ram,bytes_ram);

    }

    public void mov(int Rd, int Rn, int bitshift){

        //TO DO

    }

   

    public void str(int Rd, int Rn, int o, boolean pre, boolean mod_pre){

        //TO DO

    }

   

    public void ldr(int Rd, int Rn, int o, boolean pre, boolean mod_pre){

        //TO DO

    }

   

    public void add(int Rd, int Rn, int Rc){

        //TO DO

    }

   

    public void print(){

        //TO DO

    }

   

}

public class Main {

    public static void main(String[] args) {

        /* (1) write a short program using the java ARM class that is like this C++ code:

            int x = 13;

            int y = 14;

            int z = 16;

            z = x+y+z;

        Note: assume 32bit ARM and RAM should have 4 byte alignments (4*bytes)

            (2) call the print method periodically to check your work or use the debugger

        */

    }

   

}

public class RAM {

    private int[] rs;

   

    public RAM(int size, int bytes){

        rs = new int[size*8*bytes];

    }

   

    public Word get(int bytes, int a){

        int bits = 8*bytes;

        int[] r = new int[bits];

        for(int i = 0; i < bits; i++){

            r[i] = rs[a+i];

        }

        Word w = new Word(bits);

        w.set(r);

        return w;      

    }

   

    public void set(Word w, int a){

        int[] r = w.get();

        for(int i = 0; i < r.length; i++){

            rs[a+i] = r[i];

        }

    }

}

import java.util.ArrayList;

public class RegisterBank {

    private ArrayList rs;

   

    public RegisterBank(int size, int bits){

        rs = new ArrayList();

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

            rs.add(new Word(bits));

        }

    }

    public Word get(int index){

        int[] i = rs.get(index).get();

        Word r = new Word(i.length);

        r.set(i);

        return r;

    }

    public void set(int index, Word r){

        rs.get(index).set(r.get());

    }

}

public class Word {

    private int[] bs;

    public int bits;

   

    public Word(int bits){

        bs = new int[bits];

        this.bits = bits;

    }

    public int[] get(){

        int[] r = new int[this.bs.length];

        for(int i = 0; i < this.bs.length; i++){

            r[i] = this.bs[i];

        }

        return r;

    }

    public void set(int[] r){

        for(int i = 0; i < r.length; i++){

            this.bs[i] = r[i];

        }

    }

}

Solutions

Expert Solution

RegisterBank.java

import java.util.ArrayList;

public class RegisterBank

{

private ArrayList<Word> rs;

  

public RegisterBank(int size, int bits)

{

rs = new ArrayList<Word>();

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

{

rs.add(new Word(bits));

}

}

public Word get(int index)

{

int[] i = rs.get(index).get();

Word r = new Word(i.length);

r.set(i);

return r;

}

public void set(int index, Word r)

{

rs.get(index).set(r.get());

}

}

=============================================================================================

Word.java

public class Word

{

private int[] bs; //SET BACK TO PRIVATE AFTER...

public int bits;

  

public Word(int bits)

{ //sets a new array the size of the bits

bs = new int[bits];

this.bits = bits;

}

  

//Creates an array and populates, then returns array values

public int[] get()

{

int[] r = new int[this.bs.length];

for(int i = 0; i < this.bs.length; i++)

{

r[i] = this.bs[i];

}

return r;

}

  

//setting the value of the word array

public void set(int[] r)

{

for(int i = 0; i < r.length; i++)

{

this.bs[i] = r[i];

}

}

}

======================================================================

RAM.java

public class RAM

{

private int[] rs;

  

/*

Keeps track of pointer....

*/

  

public RAM(int size, int bytes)

{

rs = new int[size*8*bytes]; //size * bits

}

  

//converts to bits and then creates a new word, which shifts by amount

public Word get(int bytes, int a)

{

int bits = 8*bytes; //convert to bits

//creates new array, with bit size

int[] r = new int[bits];

  

//Loop to set a shift in array for values

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

{

r[i] = rs[a+i];

}

Word w = new Word(bits);

w.set(r);

return w;

}

  

//shifts by a, in bits

public void set(Word w, int a)

{

int[] r = w.get();

for(int i = 0; i < r.length; i++)

{

rs[a+i] = r[i];

}

}

}

==========================================================================

ARM.java

public class ARM

{

public RegisterBank rb;

public RAM mb;

public int sp = 8*32;

public int pc = 0;

private final int bits;

private final int registers;

private final int size_ram;

  

public ARM(int size_reg, int bits_reg, int size_ram, int bytes_ram)

{

rb = new RegisterBank(size_reg,bits_reg);

mb = new RAM(size_ram,bytes_ram);

registers=size_reg;

bits = bits_reg;

this.size_ram = size_ram;

}

/*

Where Rd is register and Rn is a value in bits, and a shift in bits,

*/

public void mov(int Rd, int Rn, int bitshift)

{

System.out.println("MOV...");

System.out.println("Value of Rd: r" + Rd);

System.out.println("Value of Rn: #" + Rn);

System.out.println("Value of bitshift: " + bitshift);

//Both variables used to store into register

Word Rn1 = new Word(bits);

int[] RntoBits = numtoBits(Rn1,Rn);

//Sets value of word to Rn in bit form

Rn1.set(RntoBits);

//Set the register value into Rd for now....

rb.set(Rd,Rn1);

//new Word set up

Word shiftBit = new Word(bits);

//Calls method to shiftBits

RntoBits = shiftBits(Rn1,bitshift);

//Finally sets shiftBit value to array and sets registry

shiftBit.set(RntoBits);

rb.set(Rd, shiftBit);

pc++; //Increment Program Counter

}

  

//Store Rd into Rn with an offset ammount, and pre, !

public void str(int Rd, int Rn, int o, boolean pre, boolean mod_pre)

{

//Setting up Rn1 to have Rn value in bits

Word Rn1 = new Word(bits);

Rn1.set(rb.get(Rd).get());

  

  

//

if(pre)

{//if a pre-Offset

if(mod_pre == false)

{//If no exclamation point

sp += (o*8);//sp + offset in bits

mb.set(Rn1, sp + (Rd * bits)); //set RAM to have value in it at position

sp -= (o*8);

}

else

{

sp += (o*8);//sp + offset in bits

mb.set(Rn1, sp + (Rd * bits));

}

}

else

{//If a post-Offset

if(mod_pre == false)

{

mb.set(Rn1, sp + (Rd * bits));

sp += (o*8);//sp + offset in bits

}

else

{

throw new NullPointerException();

}

}

pc++;//program counter++

}

//reg, sp, offset, pre, !

public void ldr(int Rd, int Rn, int o, boolean pre, boolean mod_pre)

{

//For Loop that will group each amount of bits to each register.

if(pre)

{//If it is a pre-offset

if(mod_pre)

{

sp+= (o*8);

rb.set(Rd, mb.get(bits / 8, sp + (Rd*bits)));

}

else

{

sp+= (o*8);

rb.set(Rd, mb.get(bits / 8, sp + (Rd*bits)));

sp-= (o*8);

}

}

else

{ //If it is a post offset

sp+= (o*8);

rb.set(Rd, mb.get(bits / 8, sp + (Rd*bits)));

}

pc++;//Program counter increment

  

}

  

public void add(int Rd, int Rn, int Rc)

{

int[] RnBits = rb.get(Rn).get();

int[] RcBits = rb.get(Rc).get();

int[] RnDest = new int[RnBits.length];

Word w = new Word(bits);

//Loop that doesn't carry...

for(int i = 0; i < RnBits.length; i++)

{

RnDest[i] = RnBits[i] + RcBits[i];

}

  

//Loop that carries over numbers

for(int i = RnDest.length-1; i >=0; i--)

{

if(RnDest[i] > 1)

{

RnDest[i]-=2;

try

{

RnDest[i-1]+=1;

}

catch(Exception e)

{

  

}

}

w.set(RnDest);

rb.set(Rd, w);

  

}

pc++;

}

  

public void print()

{

int track = 0;

int[] val;

try{

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

{

val = rb.get(track).get();

System.out.print("Register " + track + ": ");

for(int j = 0; j < bits;j++)

{

System.out.print(val[j]);

}

System.out.println();

  

track++;

}

}

catch(IndexOutOfBoundsException e)

{

}

  

System.out.println("SP: " + sp);

System.out.println("PC: " + pc);

  

}

  

  

/*

Converts number to Bits...

Does NOT shift...

*/

public int[] numtoBits(Word w, int Rn)

{

int[] RntoBits = new int[w.bits];

//Used to convert to binary value

String s = Long.toBinaryString(Rn);

  

//Loop that allows for binary representation

for(int i = 0; i< s.length(); i++)

{

//Sets a reverse value to end of array

RntoBits[RntoBits.length-1-i]= Integer.valueOf(s.substring(s.length()-1-i, s.length()-1-(i-1)));

}

  

  

  

return RntoBits;

}

  

/*

Shifts the bits of a Word....

*/

  

public int[] shiftBits(Word w, int bitshift)

{

int[] RntoBits = w.get();

  

/*

Shifting left!

*/

if(Integer.toString(bitshift).contains("-"))

{ //SHIFTING LEFT...

//Count through each shift

for(int i = 0; i < Math.abs(bitshift); i++)

{

//Loop to rotate each time until it reaches [1]

int temp = RntoBits[0];//Value stored

for(int j = 0; j < RntoBits.length-1; j++)

{

try

{

RntoBits[j]=

RntoBits[j+1]; //take whatever is left of j

}

catch(Exception e)

{

System.out.println("ERROR");

temp = RntoBits[0];

RntoBits[RntoBits.length-1] = temp;

RntoBits[j]=RntoBits[j+1];

  

}

}

RntoBits[RntoBits.length-1]= temp;

}

}

/*

Shifting Right!

*/

else

{ //SHIFTING RIGHT...

//Count through each shift

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

{

//Loop to rotate each time until it reaches [1]

int temp = RntoBits[RntoBits.length-1];//Value stored

for(int j = RntoBits.length-2; j >= 0; j--)

{

try

{

RntoBits[j+1]=

RntoBits[j]; //take whatever is left of j

}

catch(Exception e)

{ //CATCHES OUT OF BOUNDS

temp = RntoBits[RntoBits.length-1];

RntoBits[0] = temp;

RntoBits[j]=RntoBits[j-1];

}

}

RntoBits[0]= temp;

}

}

return RntoBits;

}

  

}


Related Solutions

Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Write a Java application, and an additional class to represent some real-world entity such as a...
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods). The class will: Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT