Skip to content
Snippets Groups Projects
Select Git revision
  • 2bcc55ec96b1eab077097bc2abcf5dd75d731d61
  • master default protected
2 results

Machine.java

Blame
  • Forked from Tim Miller / SWEN90006-A1-2018
    Source project has a limited visibility.
    Machine.java 7.88 KiB
    package swen90006.machine;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class Machine 
    {
      /** arithmetic instructions each take three registers as arguments with the
       *  destination register appearing first
       */
    
      /** add rd rs1 rs2   =~   rd = rs1 + rs2 */
      public static final String INSTRUCTION_ADD = "add";
    
      /** sub rd rs1 rs2   =~   rd = rs1 - rs2 */    
      public static final String INSTRUCTION_SUBTRACT = "sub";
    
      /** mul rd rs1 rs2   =~   rd = rs1 * rs2 */        
      public static final String INSTRUCTION_MULT = "mul";
    
      /** div rd rs1 rs2   =~   rd = rs1 / rs2 */            
      public static final String INSTRUCTION_DIVIDE = "div";
    
      /** ret rs           =~   return rs */
      public static final String INSTRUCTION_RETURN = "ret";
    
      /** ldr rd rs offs  =~    rd = rs[offs] */
      public static final String INSTRUCTION_LOAD = "ldr";
    
      /** str ra offs rb   =~    ra[offs] = rb */
      public static final String INSTRUCTION_STORE = "str";
    
      /** mov rd val       =~    rd = val */
      public static final String INSTRUCTION_MOVE = "mov";
    
      /** jmp offs         =~    pc = pc + offs */
      public static final String INSTRUCTION_JUMP = "jmp";
    
      /** jz ra offs       =~   if (ra == 0) pc = pc + offs else pc = pc + 1 */
      public static final String INSTRUCTION_JZ = "jz";
        
      public static final int NUM_REGS = 32;
      public static final int MAX_REG = (NUM_REGS - 1);
      public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */
      public static final int MAX_ADDR = MEMORY_SIZE-1;
        
      private int[] memory;
      private int[] regs;
    
      private int count = 0; /* counts number of instructions executed so far */
        
      public Machine()
      {
        memory = new int[MEMORY_SIZE];
        regs = new int[NUM_REGS];
        count = 0;
      }
    
      private void do_add(int dest, int src1, int src2)
      {
        regs[dest] = regs[src1] + regs[src2];
      }
        
      private void do_sub(int dest, int src1, int src2)
      {
        regs[dest] = regs[src1] - regs[src2];
      }
        
      private void do_mult(int dest, int src1, int src2)
      {