Question

(Sure, take your time. would you like me to post this again?) Thanks in advance !...

(Sure, take your time. would you like me to post this again?) Thanks in advance !

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 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)]

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

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];

        }

    }

}

Homework Answers

Answer #1

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;
}
  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Add a method to RedBlackBST to print a RB-BST indexing the subnodes. Test the tree inserting...
Add a method to RedBlackBST to print a RB-BST indexing the subnodes. Test the tree inserting element by element and printing the results with (1) S E A R C H E X A M P L E (where the value is the index of the letter in the initial word) and (2) K R I S T E N public class RedBlackBST<Key extends Comparable<Key>, Value> { private Node root; private class Node // BST node with color bit (see...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...