Guest User

Untitled

a guest
Jul 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import static constant.Constants.*;
  2. public class Microprocessor {
  3.    private int[] instructions = new int[MAX_INPUT];
  4.    private int r0 = 0;
  5.    private int r1 = 0;
  6.    private int pos = 0;
  7.    public void createInstruct(int[] store) {
  8.       System.arraycopy(store, 0, instructions, 0, MAX_INPUT);
  9.       scannerVal();
  10.    }
  11.    
  12.    private void scannerVal(){
  13.       int scanOne = -1;
  14.       int scanTwo = -1;
  15.      
  16.      
  17.      
  18.       while (scanOne != 0){
  19.          System.out.println("PROGRAM ENTER!!!!!");
  20.          scanOne = instructions[pos];
  21.          if (scanOne == 0){
  22.             break;
  23.          }
  24.          System.out.println(" Scan = " + scanOne);
  25.          if (scanOne > 7 && scanOne < 16){
  26.             pos++;
  27.             scanTwo = instructions[pos];
  28.          }
  29.          
  30.             checkVal(scanOne, scanTwo);
  31.    
  32.          pos++;
  33.       }
  34.       System.out.println("SOLVED!");
  35.      // System.exit(1);
  36.    }
  37.    
  38.    private void checkVal(int scanOne, int scanTwo){
  39.       if (scanOne == 1) {
  40.          System.out.println("ADDING");
  41.          add ();
  42.       }else if (scanOne == 2) {
  43.          System.out.println("SUBTRACTING");
  44.          subtract();
  45.       }else if (scanOne == 3) {
  46.          System.out.println("INCREMENT R0");
  47.          incrementR0();
  48.       }else if (scanOne == 4) {
  49.          System.out.println("INCREMENT R1");
  50.          incrementR1();
  51.       }else if (scanOne == 5) {
  52.          System.out.println("DECREMENT R0");
  53.          decrementR0();
  54.       }else if (scanOne == 6) {
  55.          System.out.println("DECREMENT R1");
  56.          decrementR1();
  57.       }else if (scanOne == 7) {
  58.          System.out.println("SWAP");
  59.          swapRegisters ();
  60.       }else if (scanOne == 8) {
  61.          System.out.println("PRINT AND BINARY");
  62.          binaryConversion (scanTwo);
  63.       }else if (scanOne == 9) {
  64.          System.out.println("LOAD R0");
  65.          loadR0 (scanTwo);
  66.       }else if (scanOne == 10) {
  67.          System.out.println("LOAD R1");
  68.          loadR1 (scanTwo);
  69.       }else if (scanOne == 11) {
  70.          System.out.println("STORE R0");
  71.          storeR0 (scanTwo);
  72.       }else if (scanOne == 12) {
  73.          System.out.println("STORE R1");
  74.          storeR1 (scanTwo);
  75.       }else if (scanOne == 13) {
  76.          System.out.println("JUMP");
  77.          jump (scanOne, scanTwo);
  78.       }else if (scanOne == 14) {
  79.          if (r0 == 0){
  80.             jump (scanOne, scanTwo);
  81.          }
  82.       }else if (scanOne == 15) {
  83.          if (r0 != 0){
  84.             jump (scanOne, scanTwo);
  85.          }
  86.       }
  87.    }
  88.    
  89.    //instruction 1
  90.    private void add () {
  91.       r0 += r1;
  92.    }
  93.    
  94.    //instruction 2
  95.    private void subtract() {
  96.       r0 = r0 - r1;
  97.    }
  98.    
  99.    //instruction 3
  100.    private void incrementR0() {
  101.       r0++;
  102.    }
  103.    
  104.    //instruction 4
  105.    private void incrementR1() {
  106.       r1++;
  107.    }
  108.    
  109.    //instruction 5
  110.    private void decrementR0 () {
  111.       r0--;
  112.    }
  113.    
  114.    //instruction 6
  115.    private void decrementR1() {
  116.       r1--;
  117.    }
  118.    
  119.    //instruction 7
  120.    private void swapRegisters () {
  121.       int temp = r0;
  122.       r0 = r1;
  123.       r1 = temp;
  124.       //ring bell
  125.    }
  126.    
  127.    //instruction 8
  128.    public void binaryConversion (int scanTwo) {
  129.       double two = 2.0;
  130.       double n = 0;
  131.       int result = scanTwo;
  132.       int max = 0;
  133.       int[] binary = new int[1000];
  134.       for (int i = 0; i < 1000; i++){
  135.          binary[i] = 0;
  136.       }
  137.      
  138.       while (result != 0){
  139.          for (n = 0; n < 32; n++){
  140.             if ((int)Math.pow(two, n) > result){
  141.                n = n - 1.0;
  142.                break;
  143.             }else if ( (int)Math.pow (two, n) == result){
  144.                break;
  145.             }
  146.          }
  147.         // System.out.println("value of n = " + (int)n);
  148.          result = result - (int)Math.pow(two, n);
  149.         // System.out.println("value of result = " + result);
  150.          binary[(int)n] = 1;
  151.          if ((int)n > max){
  152.             max = (int)n;
  153.          }              
  154.      
  155.      }
  156.      for (int j = max; j > -1; j--){
  157.        // System.out.print(binary[j]);
  158.      }
  159.      //System.out.print("scanTwo =  " + scanTwo);
  160.      System.out.println("printing:  " + instructions[pos]);
  161.    }
  162.    
  163.    //instruction 9
  164.    private void loadR0 (int scanTwo) {
  165.       r0 = instructions[scanTwo];
  166.    }
  167.  
  168.    //instruction 10
  169.    private void loadR1 (int scanTwo) {
  170.       r1 = instructions[scanTwo];
  171.    }
  172.  
  173.    //instruction 11
  174.    private void storeR0 (int scanTwo) {
  175.       //System.out.println("scanTwo =  " + scanTwo);
  176.       instructions[scanTwo] = r0;
  177.    }
  178.  
  179.    //instruction 12
  180.    private void storeR1 (int scanTwo) {
  181.       instructions[scanTwo] = r1;
  182.    }
  183.  
  184.    //instruction 13, 14, 15
  185.    private void jump (int current, int scanTwo) {
  186.       pos = scanTwo;
  187.       scannerVal();
  188.    }
  189.  
  190.    
  191. }
Add Comment
Please, Sign In to add comment