
public class Simpletron {

   private int ac;   // accumulator
   private int ic;   // instruction counter (2 digits)
   private int ir;   // instruction register
   private int opcode; // operation code (2 digits)
   private int operand; // operation data (2 digits)
   private Memory mem; 
   
   
   public Simpletron() {
      mem = new Memory();
   }
   
   
   public Simpletron(Memory mem) {
      this.mem = mem;
   }
   
   
   /* execute one cycle of the machine. returns true if the machine 
      has not halted or crashed */
   public boolean step() {
      return false;
   }
   
   
   /* execute the machine continuously until it halts or crashes */
   public void run() {
      while ( step() )
         ;
   }
   
   
   // update value in the accumulator, checking for overflow
   // returns false if overflow occurs
   private boolean setAc( int val ) {
       if ( val > Memory.MAXWVAL || val < Memory.MINWVAL ) {
           ac = val % Memory.MAXWVAL;
           return false;               /* overflow occured */
       } else {
           ac = val;
       }
       return true;
   }


   public String toString() {
      String str = ( "Simpletron Register/Memory Dump\n" );
      str += String.format( "REGISTERS: \n" +
                             "%-30s %+05d\n" +
                             "%-30s    %02d\n" +
                             "%-30s %+05d\n" +
                             "%-30s    %02d\n" +
                             "%-30s    %02d\n",
                             "accumulator", ac,
                             "instructionCounter", ic,
                             "instructionRegister", ir,
                             "operationCode", opcode,
                             "operand", operand );
      str += mem.toString();
      return str;
   }
   
}
