emulator/cpu/convert.c
int64_t from_machine_code(const char *s, size_t length) {

  int64_t w = 0;
  int state = 0;
  int address = 0;
  int shift = 64;
  bool negative = false;
  bool decimal = false;

  for (size_t i = 0; i < length; ++i) {
    int c = s[i];
    if (' ' == c || '\t' == c) {
      continue;
    }
    switch (state) {
    case 0: // op 1
      if ('-' == c) {
        state = 6;
        decimal = true;
        negative = true;
        break;
      }
      if ('+' == c) {
        state = 6;
        decimal = true;
        break;
      }
    case 1: // op 1
    case 3: // op 2
    case 4: // op 2
      if (c >= '0' && c <= '7') {
        ++state;
        shift -= 3;
        w |= (int64_t)(c - '0') << shift;
      } else {
        return -1LL;
      }
      break;
    case 2: // address 1
    case 5: // address 2
      if (2 == state && ('/' == c || ':' == c)) {
        shift -= 13;
        w |= (int64_t)(address & 8191) << shift;
        address = 0;
        shift -= 1;
        if ('/' == c) {
          w |= 1LL << shift;
        }
        ++state;
      } else if (c >= '0' && c <= '9') {
        address = 10 * address + c - '0';
      } else {
        return -1LL;
      }
      break;
    case 6: // ±decimal
      if (c >= '0' && c <= '9') {
        w = 10 * w + c - '0';
      } else {
        return -1LL;
      }
      break;
    }
  }
  if (decimal) {
    if (negative) {
      w = -w;
    }
    w <<= second_address_shift;
  } else {
    // final address
    shift -= 13;
    w |= (int64_t)(address & 8191) << shift;
  }
  return w;
}
// convert machine code string to a word
// returns:
//   a word fully left shifted to be correct for store to core
//   on error -1LL
convert machine code string to a word
returns:
a word fully left shifted to be correct for store to core
on error -1LL
convert machine code string to a word returns : a word fully left shifted to be correct for store to core on error - 1LL
int64_t from_machine_code(const char *s, size_t length) {
  int64_t w = 0;
  int state = 0;
  int address = 0;
  int shift = 64;
  bool negative = false;
  bool decimal = false;
  for (size_t i = 0; i < length; ++i) {
    int c = s[i];
    if (' ' == c || '\t' == c) {
      continue;
    }
    switch (state) {
    case 0: 
      if ('-' == c) {
        state = 6;
        decimal = true;
        negative = true;
        break;
      }
      if ('+' == c) {
        state = 6;
        decimal = true;
        break;
      }
    case 1: 
    case 3: 
    case 4: 
      if (c >= '0' && c <= '7') {
        ++state;
        shift -= 3;
        w |= (int64_t)(c - '0') << shift;
      } else {
        return -1LL;
      }
      break;
    case 2: 
    case 5: 
      if (2 == state && ('/' == c || ':' == c)) {
        shift -= 13;
        w |= (int64_t)(address & 8191) << shift;
        address = 0;
        shift -= 1;
        if ('/' == c) {
          w |= 1LL << shift;
        }
        ++state;
      } else if (c >= '0' && c <= '9') {
        address = 10 * address + c - '0';
      } else {
        return -1LL;
      }
      break;
    case 6: 
      if (c >= '0' && c <= '9') {
        w = 10 * w + c - '0';
      } else {
        return -1LL;
      }
      break;
    }
  }
  if (decimal) {
    if (negative) {
      w = -w;
    }
    w <<= second_address_shift;
  } else {
    shift -= 13;
    w |= (int64_t)(address & 8191) << shift;
  }
  return w;
}
int64_t from_machine_code ( const char * s , size_t length ) { int64_t w = 0 ; int state = 0 ; int address = 0 ; int shift = 64 ; bool negative = false ; bool decimal = false ; for ( size_t i = 0 ; i < length ; ++ i ) { int c = s [ i ] ; if ( ' ' == c || ' \t ' == c ) { continue ; } switch ( state ) { case 0 : if ( ' ' == c ) { state = 6 ; decimal = true ; negative = true ; break ; } if ( ' ' == c ) { state = 6 ; decimal = true ; break ; } case 1 : case 3 : case 4 : if ( c >= ' ' && c <= ' ' ) { ++ state ; shift -= 3 ; w |= ( int64_t ) ( c - ' ' ) << shift ; } else { return -1LL ; } break ; case 2 : case 5 : if ( 2 == state && ( ' ' == c || ' ' == c ) ) { shift -= 13 ; w |= ( int64_t ) ( address & 8191 ) << shift ; address = 0 ; shift -= 1 ; if ( ' ' == c ) { w |= 1LL << shift ; } ++ state ; } else if ( c >= ' ' && c <= ' ' ) { address = 10 * address + c - ' ' ; } else { return -1LL ; } break ; case 6 : if ( c >= ' ' && c <= ' ' ) { w = 10 * w + c - ' ' ; } else { return -1LL ; } break ; } } if ( decimal ) { if ( negative ) { w = - w ; } w <<= second_address_shift ; } else { shift -= 13 ; w |= ( int64_t ) ( address & 8191 ) << shift ; } return w ; }
convert machine code string to a word
returns:
a word fully left shifted to be correct for store to core
on error -1LL
convert machine code string to a word returns : a word fully left shifted to be correct for store to core on error - 1LL
// op 1 // op 1 // op 2 // op 2 // address 1 // address 2 // ±decimal // final address