Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static constant.Constants.*;
- public class Microprocessor {
- private int[] instructions = new int[MAX_INPUT];
- private int r0 = 0;
- private int r1 = 0;
- private int pos = 0;
- public void createInstruct(int[] store) {
- System.arraycopy(store, 0, instructions, 0, MAX_INPUT);
- scannerVal();
- }
- private void scannerVal(){
- int scanOne = -1;
- int scanTwo = -1;
- while (scanOne != 0){
- System.out.println("PROGRAM ENTER!!!!!");
- scanOne = instructions[pos];
- if (scanOne == 0){
- break;
- }
- System.out.println(" Scan = " + scanOne);
- if (scanOne > 7 && scanOne < 16){
- pos++;
- scanTwo = instructions[pos];
- }
- checkVal(scanOne, scanTwo);
- pos++;
- }
- System.out.println("SOLVED!");
- // System.exit(1);
- }
- private void checkVal(int scanOne, int scanTwo){
- if (scanOne == 1) {
- System.out.println("ADDING");
- add ();
- }else if (scanOne == 2) {
- System.out.println("SUBTRACTING");
- subtract();
- }else if (scanOne == 3) {
- System.out.println("INCREMENT R0");
- incrementR0();
- }else if (scanOne == 4) {
- System.out.println("INCREMENT R1");
- incrementR1();
- }else if (scanOne == 5) {
- System.out.println("DECREMENT R0");
- decrementR0();
- }else if (scanOne == 6) {
- System.out.println("DECREMENT R1");
- decrementR1();
- }else if (scanOne == 7) {
- System.out.println("SWAP");
- swapRegisters ();
- }else if (scanOne == 8) {
- System.out.println("PRINT AND BINARY");
- binaryConversion (scanTwo);
- }else if (scanOne == 9) {
- System.out.println("LOAD R0");
- loadR0 (scanTwo);
- }else if (scanOne == 10) {
- System.out.println("LOAD R1");
- loadR1 (scanTwo);
- }else if (scanOne == 11) {
- System.out.println("STORE R0");
- storeR0 (scanTwo);
- }else if (scanOne == 12) {
- System.out.println("STORE R1");
- storeR1 (scanTwo);
- }else if (scanOne == 13) {
- System.out.println("JUMP");
- jump (scanOne, scanTwo);
- }else if (scanOne == 14) {
- if (r0 == 0){
- jump (scanOne, scanTwo);
- }
- }else if (scanOne == 15) {
- if (r0 != 0){
- jump (scanOne, scanTwo);
- }
- }
- }
- //instruction 1
- private void add () {
- r0 += r1;
- }
- //instruction 2
- private void subtract() {
- r0 = r0 - r1;
- }
- //instruction 3
- private void incrementR0() {
- r0++;
- }
- //instruction 4
- private void incrementR1() {
- r1++;
- }
- //instruction 5
- private void decrementR0 () {
- r0--;
- }
- //instruction 6
- private void decrementR1() {
- r1--;
- }
- //instruction 7
- private void swapRegisters () {
- int temp = r0;
- r0 = r1;
- r1 = temp;
- //ring bell
- }
- //instruction 8
- public void binaryConversion (int scanTwo) {
- double two = 2.0;
- double n = 0;
- int result = scanTwo;
- int max = 0;
- int[] binary = new int[1000];
- for (int i = 0; i < 1000; i++){
- binary[i] = 0;
- }
- while (result != 0){
- for (n = 0; n < 32; n++){
- if ((int)Math.pow(two, n) > result){
- n = n - 1.0;
- break;
- }else if ( (int)Math.pow (two, n) == result){
- break;
- }
- }
- // System.out.println("value of n = " + (int)n);
- result = result - (int)Math.pow(two, n);
- // System.out.println("value of result = " + result);
- binary[(int)n] = 1;
- if ((int)n > max){
- max = (int)n;
- }
- }
- for (int j = max; j > -1; j--){
- // System.out.print(binary[j]);
- }
- //System.out.print("scanTwo = " + scanTwo);
- System.out.println("printing: " + instructions[pos]);
- }
- //instruction 9
- private void loadR0 (int scanTwo) {
- r0 = instructions[scanTwo];
- }
- //instruction 10
- private void loadR1 (int scanTwo) {
- r1 = instructions[scanTwo];
- }
- //instruction 11
- private void storeR0 (int scanTwo) {
- //System.out.println("scanTwo = " + scanTwo);
- instructions[scanTwo] = r0;
- }
- //instruction 12
- private void storeR1 (int scanTwo) {
- instructions[scanTwo] = r1;
- }
- //instruction 13, 14, 15
- private void jump (int current, int scanTwo) {
- pos = scanTwo;
- scannerVal();
- }
- }
Add Comment
Please, Sign In to add comment