package de.hwp.tinyvm;

import java.io.IOException;

public class Main {

    public static final int CMD_MASK = 0b0000000000011111;
    public static final int LOAD_MASK = 0b1000000000000000;
    public static final int IDX0_MASK = 0b0111110000000000;
    public static final int IDX1_MASK = 0b0000001111100000;
    public static final int JMP_MASK = 0b1111111111100000;

    public static int[] memory;
    public static int[] reg = new int[32];
    public static int pc = 0;

    public static void main(String[] args) {

        Assembler myAssembler = new Assembler();
        try {
            memory = myAssembler.fileToOpcode("test.asm");
        } catch (IOException e) {
            e.printStackTrace();
        }

        reg[4] = 8;
        reg[3] = 6;

        Main myVM = new Main();
        while (pc < memory.length) {
            myVM.doCommand();
        }

        System.out.println("Done");
    }

    public void doCommand() {
        int opcode = memory[pc];

        if (opcode == 0) {
            pc++;
            return;
        }

        int cmd = opcode & CMD_MASK;
        int idx1 = (opcode & IDX1_MASK) >> 5;
        int idx0 = (opcode & IDX0_MASK) >> 10;
        int load = (opcode & LOAD_MASK) >> 15;

        System.out.println("BEFORE CMD: " + cmd + " | IDX0: " + reg[idx0] + " | IDX1:" + reg[idx1]);

        switch (cmd) {
            case 0: //TODO: Commands.MOVE_FROM_MEM_TO_REG
                pc++;
                break;
            case 1: //TODO: Commands.MOVE_FROM_REG_TO_MEM
                pc++;
                break;
            case 2: //Commands.MOVE_FROM_REG_TO_REG
                reg[idx0] = reg[idx1];
                pc++;
                break;
            case 3: //TODO: Commands.MOVE_FROM_MEM_TO_MEM
                pc++;
                break;
            case 4: //Commands.AND
                reg[idx0] = reg[idx0] & reg[idx1];
                pc++;
                break;
            case 5: //Commands.OR
                reg[idx0] = reg[idx0] | reg[idx1];
                pc++;
                break;
            case 6: //Commands.MUL
                reg[idx0] = reg[idx0] * reg[idx1];
                pc++;
                break;
            case 7: //Commands.DIV
                reg[idx0] = reg[idx0] / reg[idx1];
                pc++;
                break;
            case 8: //Commands.SUB
                reg[idx0] = reg[idx0] - reg[idx1];
                pc++;
                break;
            case 9: //Commands.ADD
                reg[idx0] = reg[idx0] + reg[idx1];
                pc++;
                break;
            case 10: //Commands.HIG
                reg[idx0] = (reg[idx0] > reg[idx1]) ? 1 : 0;
                pc++;
                break;
            case 11: //Commands.EQU
                reg[idx0] = (reg[idx0] == reg[idx1]) ? 1 : 0;
                pc++;
                break;
            case 12: //Commands.NOT
                reg[idx0] = ~reg[idx1]; //TODO: ~ correct? - unary bitwise complement
                pc++;
                break;
            case 13: //TODO: Commands.RET
                pc++;
                break;
            case 14: //TODO: Commands.JIT
                break;
            case 15: //TODO: Commands.JSR
                break;
            case 16: //TODO: Commands.JMP
                break;

            default: //Command not Found
                pc++;
                break;
        }

        System.out.println("AFTER CMD: " + cmd + " | IDX0: " + reg[idx0] + " | IDX1:" + reg[idx1] + "\n");

    }

}
@@ -717,25 +717,24 @@
       %7D%0A
-%0A
         
 reg%5B4%5D =
@@ -729,40 +729,69 @@
     
-reg%5B4%5D = 8;%0A        reg%5B3%5D = 6;%0A
+%0A        System.out.println(%22--- Output START ---%22);%0A        
 %0A   
@@ -888,32 +888,40 @@
 nd();%0A        %7D%0A
+        
 %0A        System.
@@ -937,12 +937,26 @@
 ln(%22
-Done
+--- Output END ---
 %22);%0A
@@ -1280,32 +1280,34 @@
 %3E%3E 15;%0A%0A        
+//
 System.out.print
@@ -1457,32 +1457,71 @@
 FROM_MEM_TO_REG%0A
+            %09reg%5Bidx1%5D = memory%5Bidx0%5D;%0A
                 
@@ -1599,32 +1599,71 @@
 FROM_REG_TO_MEM%0A
+            %09memory%5Bidx0%5D = reg%5Bidx1%5D;%0A
                 
@@ -1877,32 +1877,74 @@
 FROM_MEM_TO_MEM%0A
+            %09memory%5Bidx0%5D = memory%5Bidx1%5D;%0A
                 
@@ -3491,16 +3491,328 @@
  break;%0A
+            case 17: //Commands.PRINTREG%0A            %09System.out.println(%22REGISTER:%22 + idx0 + %22 WERT:%22 + reg%5Bidx0%5D);%0A            %09pc++;%0A            %09break;%0A            case 18: //Commands.PRINTMEM%0A            %09System.out.println(%22MEMORY:%22 + idx0 + %22 WERT:%22 + memory%5Bidx0%5D);%0A            %09pc++;%0A            %09break;
 %0A       
@@ -3901,32 +3901,34 @@
      %7D%0A%0A        
+//
 System.out.print
@@ -4019,8 +4019,9 @@
     %7D%0A%0A%7D
+%0A

Update Main.java