package io.lp0onfire.smtnes.generators.cpu;

import io.lp0onfire.smtnes.smt2.BinaryConstant;

public enum CPUState {

  Resetting(0), // instead look at ResetSequence
  InstructionFetch(1), // read instruction from DataIn
  ;
  
  private final int index;
  public int getIndex() {
    return this.index;
  }
  
  public BinaryConstant toBinaryConstant() {
    // TODO
     throw new UnsupportedOperationException("not yet implemented");
  }
  
  private static Integer maxIndex = null;
  public static int getMaxIndex() {
    if (maxIndex == null) {
      maxIndex = Integer.MIN_VALUE;
      for (CPUState state : CPUState.values()) {
        if (state.getIndex() > maxIndex) {
          maxIndex = state.getIndex();
        }
      }
    }
    return maxIndex;
  }
  
  public static int getStateWidth() {
    // Get the number of bits needed to represent any CPUState as a bitvector.
    return Integer.toBinaryString(getMaxIndex()).length();
  }
  
  CPUState(int index) {
    this.index = index;
  }
  
}

@@ -38,16 +38,62 @@
 s.cpu;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 import i
@@ -404,82 +404,210 @@
     
-// TODO%0A     throw new UnsupportedOperationException(%22not yet implemented%22
+String bits = Integer.toBinaryString(getIndex());%0A    // zero-pad on the left%0A    int zeroCount = getStateWidth() - bits.length();%0A    return new BinaryConstant(StringUtils.repeat('0', zeroCount) + bits
 );%0A 

add implementation of toBinaryConstant()