decompiled_code:
char *sound_init()
{
char *v0; // rax
char *result; // rax

v0 = sound_reg;
do
*v0++ = 0;
while ( v0 != (char *)(unsigned int)((_DWORD)sound_reg + 256) );
result = &sound2_reg;
do
*result++ = 0;
while ( result != (char *)(unsigned int)((_DWORD)&sound2_reg + 256) );
sound_reg[14] = -1;
sound_reg[15] = -1;
intr_sound_enable = 0;
sound_reg[41] = 3;
sound_ENABLE_A = 0;
sound_ENABLE_B = 0;
sound_LOAD_A = 0;
sound_LOAD_B = 0;
sound_FLAG_A = 0;
sound_FLAG_B = 0;
sound_TIMER_A = 0;
sound_TIMER_B = 0;
sound_prescaler = 6;
sound_prescaler_sel = 2;
sound2_MSK_TA = 0;
sound2_MSK_TB = 0;
sound2_EN_TA = 1;
sound2_EN_TB = 2;
sound2_EN_EOS = 0;
sound2_EN_BRDY = 0;
sound2_EN_ZERO = 0;
sound2_FLAG_EOS = 0;
sound2_FLAG_BRDY = 0;
sound2_FLAG_ZERO = 0;
sound2_FLAG_PCMBSY = 0;
sound2_read_dummy = 2;
return result;
}

canonical_decompiled_code:
char *func0()
{
char *v0; // rax
char *result; // rax

v0 = sound_reg;
do
*v0++ = 0;
while ( v0 != (char *)(unsigned int)((_DWORD)sound_reg + 256) );
result = &sound2_reg;
do
*result++ = 0;
while ( result != (char *)(unsigned int)((_DWORD)&sound2_reg + 256) );
sound_reg[14] = -1;
sound_reg[15] = -1;
intr_sound_enable = 0;
sound_reg[41] = 3;
sound_ENABLE_A = 0;
sound_ENABLE_B = 0;
sound_LOAD_A = 0;
sound_LOAD_B = 0;
sound_FLAG_A = 0;
sound_FLAG_B = 0;
sound_TIMER_A = 0;
sound_TIMER_B = 0;
sound_prescaler = 6;
sound_prescaler_sel = 2;
sound2_MSK_TA = 0;
sound2_MSK_TB = 0;
sound2_EN_TA = 1;
sound2_EN_TB = 2;
sound2_EN_EOS = 0;
sound2_EN_BRDY = 0;
sound2_EN_ZERO = 0;
sound2_FLAG_EOS = 0;
sound2_FLAG_BRDY = 0;
sound2_FLAG_ZERO = 0;
sound2_FLAG_PCMBSY = 0;
sound2_read_dummy = 2;
return result;
}

original_code:
void sound_init( void )
{
  int i;
  for( i=0; i<0x100; i++ ) sound_reg [ i ] = 0x00;
  for( i=0; i<0x100; i++ ) sound2_reg[ i ] = 0x00;
  sound_reg[0x0e]=0xff;
  sound_reg[0x0f]=0xff;
  intr_sound_enable = 0x00;
  sound_reg[0x29]=0x03;
  sound_ENABLE_A = (0);
  sound_ENABLE_B = (0);
  sound_LOAD_A = (0);
  sound_LOAD_B = (0);
  sound_FLAG_A = 0;
  sound_FLAG_B = 0;
  sound_TIMER_A = 0;
  sound_TIMER_B = 0;
  sound_prescaler = 6;
  sound_prescaler_sel = 2;
  sound2_MSK_TA = 0x00;
  sound2_MSK_TB = 0x00;
  sound2_EN_TA = 0x01;
  sound2_EN_TB = 0x02;
  sound2_EN_EOS = (0);
  sound2_EN_BRDY = (0);
  sound2_EN_ZERO = (0);
  sound2_FLAG_EOS = 0;
  sound2_FLAG_BRDY = 0;
  sound2_FLAG_ZERO = 0;
  sound2_FLAG_PCMBSY = 0;
  sound2_read_dummy = 2;
}

canonical_original_code:
void sound_init()
{
  int i;
  for( i=0; i<0x100; i++ ) sound_reg [ i ] = 0x00;
  for( i=0; i<0x100; i++ ) sound2_reg[ i ] = 0x00;
  sound_reg[0x0e]=0xff;
  sound_reg[0x0f]=0xff;
  intr_sound_enable = 0x00;
  sound_reg[0x29]=0x03;
  sound_ENABLE_A = (0);
  sound_ENABLE_B = (0);
  sound_LOAD_A = (0);
  sound_LOAD_B = (0);
  sound_FLAG_A = 0;
  sound_FLAG_B = 0;
  sound_TIMER_A = 0;
  sound_TIMER_B = 0;
  sound_prescaler = 6;
  sound_prescaler_sel = 2;
  sound2_MSK_TA = 0x00;
  sound2_MSK_TB = 0x00;
  sound2_EN_TA = 0x01;
  sound2_EN_TB = 0x02;
  sound2_EN_EOS = (0);
  sound2_EN_BRDY = (0);
  sound2_EN_ZERO = (0);
  sound2_FLAG_EOS = 0;
  sound2_FLAG_BRDY = 0;
  sound2_FLAG_ZERO = 0;
  sound2_FLAG_PCMBSY = 0;
  sound2_read_dummy = 2;
}

variable_types: i int


decompiled_code:
__int64 __fastcall sound_out_reg(unsigned __int8 a1)
{
sound_reg_select = a1;
return xmame_sound_out_reg();
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1)
{
sound_reg_select = a1;
return xmame_sound_out_reg();
}

original_code:
void sound_out_reg( byte data )
{
  sound_reg_select = data & 0xff;
  xmame_sound_out_reg( data );
}

canonical_original_code:
void sound_out_reg(unsigned char data)
{
  sound_reg_select = data & 0xff;
  xmame_sound_out_reg( data );
}

variable_types:
data
unsigned char

return_type: void


decompiled_code:
__int64 __fastcall sound_out_data(unsigned __int8 a1)
{
__int64 result; // rax

sound_reg[sound_reg_select] = a1;
xmame_sound_out_data(a1);
result = (unsigned int)(sound_reg_select - 36);
switch ( sound_reg_select )
{
case '$':
case '%':
sound_TIMER_A = sound_reg[37] & 3 | (4 * (unsigned __int8)sound_reg[36]);
result = interval_work_set_TIMER_A();
break;
case '&':
sound_TIMER_B = a1;
result = interval_work_set_TIMER_B();
break;
case '\'':
result = change_sound_flags(39LL);
break;
case ')':
if ( sound_board == 1 )
result = change_sound_flags(41LL);
break;
case '-':
if ( verbose_snd )
printf("SOUND out %X\n", 45LL);
sound_prescaler_sel |= 2u;
result = change_sound_prescaler((unsigned int)pres_3[sound_prescaler_sel]);
break;
case '.':
if ( verbose_snd )
printf("SOUND out %X\n", 46LL);
sound_prescaler_sel |= 1u;
result = change_sound_prescaler((unsigned int)pres_3[sound_prescaler_sel]);
break;
case '/':
if ( verbose_snd )
printf("SOUND out %X\n", 47LL);
sound_prescaler_sel = 0;
result = change_sound_prescaler(2LL);
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
__int64 result; // rax

sound_reg[sound_reg_select] = a1;
xmame_sound_out_data(a1);
result = (unsigned int)(sound_reg_select - 36);
switch ( sound_reg_select )
{
case '$':
case '%':
sound_TIMER_A = sound_reg[37] & 3 | (4 * (unsigned __int8)sound_reg[36]);
result = interval_work_set_TIMER_A();
break;
case '&':
sound_TIMER_B = a1;
result = interval_work_set_TIMER_B();
break;
case '\'':
result = change_sound_flags(39LL);
break;
case ')':
if ( sound_board == 1 )
result = change_sound_flags(41LL);
break;
case '-':
if ( verbose_snd )
printf("SOUND out %X\n", 45LL);
sound_prescaler_sel |= 2u;
result = change_sound_prescaler((unsigned int)pres_3[sound_prescaler_sel]);
break;
case '.':
if ( verbose_snd )
printf("SOUND out %X\n", 46LL);
sound_prescaler_sel |= 1u;
result = change_sound_prescaler((unsigned int)pres_3[sound_prescaler_sel]);
break;
case '/':
if ( verbose_snd )
printf("SOUND out %X\n", 47LL);
sound_prescaler_sel = 0;
result = change_sound_prescaler(2LL);
break;
default:
return result;
}
return result;
}

original_code:
void sound_out_data( byte data )
{
  static const int pres[4] = { 2, 2, 6, 3 };
  sound_reg[ sound_reg_select ] = data;
  xmame_sound_out_data( data );
  switch( sound_reg_select ){
  case 0x24:
  case 0x25:
    sound_TIMER_A = ((int)sound_reg[ 0x24 ]<<2) | (sound_reg[ 0x25 ] & 0x03);
    interval_work_set_TIMER_A();
    break;
  case 0x26:
    sound_TIMER_B = (int)data;
    interval_work_set_TIMER_B();
    break;
  case 0x27:
    change_sound_flags( 0x27 );
    break;
  case 0x29:
    if( sound_board==SOUND_II ){
      change_sound_flags( 0x29 );
    }
    break;
  case 0x2d:
    if( verbose_snd ) printf("SOUND out %X\n",sound_reg_select);
    sound_prescaler_sel |= 2;
    change_sound_prescaler( pres[sound_prescaler_sel] );
    break;
  case 0x2e:
    if( verbose_snd ) printf("SOUND out %X\n",sound_reg_select);
    sound_prescaler_sel |= 1;
    change_sound_prescaler( pres[sound_prescaler_sel] );
    break;
  case 0x2f:
    if( verbose_snd ) printf("SOUND out %X\n",sound_reg_select);
    sound_prescaler_sel = 0;
    change_sound_prescaler( pres[sound_prescaler_sel] );
    break;
  }
}

canonical_original_code:
void sound_out_data(unsigned char data)
{
  static int pres[4] = { 2, 2, 6, 3 };
  sound_reg[ sound_reg_select ] = data;
  xmame_sound_out_data( data );
  switch( sound_reg_select ){
  case 0x24:
  case 0x25:
    sound_TIMER_A = ((int)sound_reg[ 0x24 ]<<2) | (sound_reg[ 0x25 ] & 0x03);
    interval_work_set_TIMER_A();
    break;
  case 0x26:
    sound_TIMER_B = (int)data;
    interval_work_set_TIMER_B();
    break;
  case 0x27:
    change_sound_flags( 0x27 );
    break;
  case 0x29:
    if( sound_board==SOUND_II ){
      change_sound_flags( 0x29 );
    }
    break;
  case 0x2d:
    if( verbose_snd ) printf("SOUND out %X\n",sound_reg_select);
    sound_prescaler_sel |= 2;
    change_sound_prescaler( pres[sound_prescaler_sel] );
    break;
  case 0x2e:
    if( verbose_snd ) printf("SOUND out %X\n",sound_reg_select);
    sound_prescaler_sel |= 1;
    change_sound_prescaler( pres[sound_prescaler_sel] );
    break;
  case 0x2f:
    if( verbose_snd ) printf("SOUND out %X\n",sound_reg_select);
    sound_prescaler_sel = 0;
    change_sound_prescaler( pres[sound_prescaler_sel] );
    break;
  }
}

variable_types:
pres
int [4]


decompiled_code:
__int64 sound_in_status()
{
__int64 result; // rax

xmame_sound_in_status();
result = (unsigned int)(2 * sound_FLAG_B);
LOBYTE(result) = sound_FLAG_A | (2 * sound_FLAG_B);
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

xmame_sound_in_status();
result = (unsigned int)(2 * sound_FLAG_B);
LOBYTE(result) = sound_FLAG_A | (2 * sound_FLAG_B);
return result;
}

original_code:
byte sound_in_status( void )
{
  xmame_sound_in_status();
  return ( sound_FLAG_B << 1 ) | sound_FLAG_A;
}

canonical_original_code:
unsigned char sound_in_status()
{
  xmame_sound_in_status();
  return ( sound_FLAG_B << 1 ) | sound_FLAG_A;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall sound_in_data(int a1)
{
unsigned int v1; // edx

xmame_sound_in_data();
if ( sound_reg_select <= 15 )
return (unsigned __int8)sound_reg[sound_reg_select];
v1 = 0;
if ( sound_reg_select == 255 )
LOBYTE(v1) = (a1 | sound_port & 2) != 0;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
unsigned int v1; // edx

xmame_sound_in_data();
if ( sound_reg_select <= 15 )
return (unsigned __int8)sound_reg[sound_reg_select];
v1 = 0;
if ( sound_reg_select == 255 )
LOBYTE(v1) = (a1 | sound_port & 2) != 0;
return v1;
}

original_code:
byte sound_in_data( int always_sound_II )
{
  xmame_sound_in_data();
  if ( sound_reg_select < 0x10 ){
    return sound_reg[ sound_reg_select ];
  }else if( sound_reg_select == 0xff ){
    if( always_sound_II ) return 0x01;
    else{
      if( sound_port & (0x02) ) return 0x01;
      else return 0x00;
    }
  }else{
    return 0x00;
  }
}

canonical_original_code:
unsigned char sound_in_data(int always_sound_II)
{
  xmame_sound_in_data();
  if ( sound_reg_select < 0x10 ){
    return sound_reg[ sound_reg_select ];
  }else if( sound_reg_select == 0xff ){
    if( always_sound_II ) return 0x01;
    else{
      if( sound_port & (0x02) ) return 0x01;
      else return 0x00;
    }
  }else{
    return 0x00;
  }
}


decompiled_code:
__int64 __fastcall sound2_out_reg(unsigned __int8 a1)
{
sound2_reg_select = a1;
return xmame_sound2_out_reg();
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1)
{
sound2_reg_select = a1;
return xmame_sound2_out_reg();
}

original_code:
void sound2_out_reg( byte data )
{
  sound2_reg_select = data & 0xff;
  xmame_sound2_out_reg( data );
}

canonical_original_code:
void sound2_out_reg(unsigned char data)
{
  sound2_reg_select = data & 0xff;
  xmame_sound2_out_reg( data );
}


decompiled_code:
__int64 __fastcall sound2_out_data(char a1)
{
__int64 result; // rax
int v2; // esi
int v3; // ecx

sound2_reg[sound2_reg_select] = a1;
xmame_sound2_out_data((unsigned __int8)a1);
result = (unsigned int)sound2_reg_select;
switch ( sound2_reg_select )
{
case 0:
sound2_repeat = a1 & 0x10;
result = a1 & 0x20;
sound2_mem = a1 & 0x20;
sound2_rec = a1 & 0x40;
if ( sound2_FLAG_PCMBSY )
{
if ( (a1 & 0x20) == 0 )
{
sound2_FLAG_PCMBSY = 0;
goto LABEL_5;
}
LABEL_12:
if ( (a1 & 1) == 0 )
return result;
goto LABEL_13;
}
if ( (a1 & 0x20) == 0 )
{
LABEL_5:
if ( (a1 & 1) == 0 )
{
LABEL_6:
sound2_stat = 0;
return result;
}
goto LABEL_13;
}
if ( a1 >= 0 )
{
if ( (a1 & 0x40) != 0 )
{
sound2_stat = 3;
sound2_delay |= 8u;
}
else
{
sound2_stat = 4;
sound2_read_dummy = 2;
}
result = (unsigned int)sound2_start_addr;
sound2_data_addr = sound2_start_addr;
goto LABEL_12;
}
if ( (a1 & 0x40) != 0 )
{
sound2_stat = 1;
sound2_intr_base = sound2_record_intr_base;
}
else
{
sound2_stat = 2;
sound2_intr_base = sound2_replay_intr_base;
}
interval_work_set_BDRY();
result = (unsigned int)sound2_stop_addr;
if ( sound2_stop_addr == sound2_limit_addr )
{
result = (unsigned int)(sound2_stop_addr - sound2_start_addr);
if ( sound2_stop_addr <= sound2_start_addr )
result = (unsigned int)(0x3FFFF - sound2_start_addr);
}
else
{
if ( sound2_limit_addr > sound2_start_addr && sound2_stop_addr > sound2_start_addr )
{
if ( sound2_stop_addr >= sound2_limit_addr )
goto LABEL_27;
LODWORD(result) = sound2_stop_addr - sound2_start_addr;
goto LABEL_36;
}
v2 = sound2_start_addr;
if ( sound2_limit_addr <= sound2_start_addr )
v2 = sound2_limit_addr;
if ( sound2_stop_addr < v2 )
{
if ( sound2_limit_addr <= sound2_start_addr )
result = (unsigned int)(0x3FFFF - sound2_start_addr);
else
result = (unsigned int)(sound2_limit_addr - sound2_start_addr + sound2_stop_addr);
}
else
{
result = (unsigned int)(sound2_stop_addr - sound2_start_addr);
if ( sound2_stop_addr <= sound2_start_addr )
result = (unsigned int)(0x3FFFF - sound2_start_addr);
}
}
if ( !(_DWORD)result )
{
LABEL_27:
sound2_notice_EOS = 0;
goto LABEL_28;
}
LABEL_36:
sound2_notice_EOS = 1;
result = interval_work_set_EOS((unsigned int)(result + 1));
LABEL_28:
sound2_FLAG_PCMBSY = 1;
if ( (a1 & 1) != 0 )
{
LABEL_13:
sound2_repeat = 0;
sound2_FLAG_PCMBSY = 0;
}
if ( !sound2_mem )
goto LABEL_6;
return result;
case 1:
result = (a1 & 2) == 0 ? 2 : 5;
sound2_mem_size = (a1 & 2) == 0 ? 2 : 5;
return result;
case 2:
case 3:
result = (*(unsigned __int16 *)&sound2_reg[2] << sound2_mem_size) & 0x3FFFF;
sound2_start_addr = (*(unsigned __int16 *)&sound2_reg[2] << sound2_mem_size) & 0x3FFFF;
return result;
case 4:
case 5:
result = (((*(unsigned __int16 *)&sound2_reg[4] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
sound2_stop_addr = (((*(unsigned __int16 *)&sound2_reg[4] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
return result;
case 6:
case 7:
result = (unsigned __int8)sound2_reg[6] | ((unsigned __int8)sound2_reg[7] << 8) & 0x700u;
if ( !((unsigned __int8)sound2_reg[6] | ((unsigned __int8)sound2_reg[7] << 8) & 0x700) )
result = 2048LL;
sound2_record_intr_base = result;
return result;
case 8:
if ( sound2_stat == 3 )
{
if ( sound2_stop_addr == sound2_data_addr || sound2_data_addr == 0x3FFFF )
sound2_delay |= 4u;
*(_BYTE *)(sound2_adpcm + sound2_data_addr) = a1;
result = (sound2_data_addr + 1) & 0x3FFFF;
sound2_data_addr = (sound2_data_addr + 1) & 0x3FFFF;
sound2_delay |= 8u;
}
return result;
case 9:
case 10:
v3 = *(unsigned __int16 *)&sound2_reg[9];
if ( !*(_WORD *)&sound2_reg[9] )
v3 = 0x10000;
result = (unsigned int)(4718592 / v3);
sound2_replay_intr_base = 4718592 / v3;
return result;
case 12:
case 13:
result = (((*(unsigned __int16 *)&sound2_reg[12] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
sound2_limit_addr = (((*(unsigned __int16 *)&sound2_reg[12] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
return result;
case 16:
return change_sound_flags(16LL);
default:
return result;
}
}

canonical_decompiled_code:
__int64 __fastcall func6(char a1)
{
__int64 result; // rax
int v2; // esi
int v3; // ecx

sound2_reg[sound2_reg_select] = a1;
xmame_sound2_out_data((unsigned __int8)a1);
result = (unsigned int)sound2_reg_select;
switch ( sound2_reg_select )
{
case 0:
sound2_repeat = a1 & 0x10;
result = a1 & 0x20;
sound2_mem = a1 & 0x20;
sound2_rec = a1 & 0x40;
if ( sound2_FLAG_PCMBSY )
{
if ( (a1 & 0x20) == 0 )
{
sound2_FLAG_PCMBSY = 0;
goto LABEL_5;
}
LABEL_12:
if ( (a1 & 1) == 0 )
return result;
goto LABEL_13;
}
if ( (a1 & 0x20) == 0 )
{
LABEL_5:
if ( (a1 & 1) == 0 )
{
LABEL_6:
sound2_stat = 0;
return result;
}
goto LABEL_13;
}
if ( a1 >= 0 )
{
if ( (a1 & 0x40) != 0 )
{
sound2_stat = 3;
sound2_delay |= 8u;
}
else
{
sound2_stat = 4;
sound2_read_dummy = 2;
}
result = (unsigned int)sound2_start_addr;
sound2_data_addr = sound2_start_addr;
goto LABEL_12;
}
if ( (a1 & 0x40) != 0 )
{
sound2_stat = 1;
sound2_intr_base = sound2_record_intr_base;
}
else
{
sound2_stat = 2;
sound2_intr_base = sound2_replay_intr_base;
}
interval_work_set_BDRY();
result = (unsigned int)sound2_stop_addr;
if ( sound2_stop_addr == sound2_limit_addr )
{
result = (unsigned int)(sound2_stop_addr - sound2_start_addr);
if ( sound2_stop_addr <= sound2_start_addr )
result = (unsigned int)(0x3FFFF - sound2_start_addr);
}
else
{
if ( sound2_limit_addr > sound2_start_addr && sound2_stop_addr > sound2_start_addr )
{
if ( sound2_stop_addr >= sound2_limit_addr )
goto LABEL_27;
LODWORD(result) = sound2_stop_addr - sound2_start_addr;
goto LABEL_36;
}
v2 = sound2_start_addr;
if ( sound2_limit_addr <= sound2_start_addr )
v2 = sound2_limit_addr;
if ( sound2_stop_addr < v2 )
{
if ( sound2_limit_addr <= sound2_start_addr )
result = (unsigned int)(0x3FFFF - sound2_start_addr);
else
result = (unsigned int)(sound2_limit_addr - sound2_start_addr + sound2_stop_addr);
}
else
{
result = (unsigned int)(sound2_stop_addr - sound2_start_addr);
if ( sound2_stop_addr <= sound2_start_addr )
result = (unsigned int)(0x3FFFF - sound2_start_addr);
}
}
if ( !(_DWORD)result )
{
LABEL_27:
sound2_notice_EOS = 0;
goto LABEL_28;
}
LABEL_36:
sound2_notice_EOS = 1;
result = interval_work_set_EOS((unsigned int)(result + 1));
LABEL_28:
sound2_FLAG_PCMBSY = 1;
if ( (a1 & 1) != 0 )
{
LABEL_13:
sound2_repeat = 0;
sound2_FLAG_PCMBSY = 0;
}
if ( !sound2_mem )
goto LABEL_6;
return result;
case 1:
result = (a1 & 2) == 0 ? 2 : 5;
sound2_mem_size = (a1 & 2) == 0 ? 2 : 5;
return result;
case 2:
case 3:
result = (*(unsigned __int16 *)&sound2_reg[2] << sound2_mem_size) & 0x3FFFF;
sound2_start_addr = (*(unsigned __int16 *)&sound2_reg[2] << sound2_mem_size) & 0x3FFFF;
return result;
case 4:
case 5:
result = (((*(unsigned __int16 *)&sound2_reg[4] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
sound2_stop_addr = (((*(unsigned __int16 *)&sound2_reg[4] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
return result;
case 6:
case 7:
result = (unsigned __int8)sound2_reg[6] | ((unsigned __int8)sound2_reg[7] << 8) & 0x700u;
if ( !((unsigned __int8)sound2_reg[6] | ((unsigned __int8)sound2_reg[7] << 8) & 0x700) )
result = 2048LL;
sound2_record_intr_base = result;
return result;
case 8:
if ( sound2_stat == 3 )
{
if ( sound2_stop_addr == sound2_data_addr || sound2_data_addr == 0x3FFFF )
sound2_delay |= 4u;
*(_BYTE *)(sound2_adpcm + sound2_data_addr) = a1;
result = (sound2_data_addr + 1) & 0x3FFFF;
sound2_data_addr = (sound2_data_addr + 1) & 0x3FFFF;
sound2_delay |= 8u;
}
return result;
case 9:
case 10:
v3 = *(unsigned __int16 *)&sound2_reg[9];
if ( !*(_WORD *)&sound2_reg[9] )
v3 = 0x10000;
result = (unsigned int)(4718592 / v3);
sound2_replay_intr_base = 4718592 / v3;
return result;
case 12:
case 13:
result = (((*(unsigned __int16 *)&sound2_reg[12] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
sound2_limit_addr = (((*(unsigned __int16 *)&sound2_reg[12] + 1) << sound2_mem_size) - 1) & 0x3FFFF;
return result;
case 16:
return change_sound_flags(16LL);
default:
return result;
}
}

original_code:
void sound2_out_data( byte data )
{
  int wk, l;
  sound2_reg[ sound2_reg_select ] = data;
  xmame_sound2_out_data( data );
  switch( sound2_reg_select ){
  case 0x00:
    sound2_repeat = data & 0x10;
    sound2_mem = data & 0x20;
    sound2_rec = data & 0x40;
    if( sound2_FLAG_PCMBSY ){
      if( !sound2_mem ) sound2_FLAG_PCMBSY = 0;
    }else{
      if( sound2_mem ){
 if( data & 0x80 ){
   if( sound2_rec ){
     sound2_stat = ADPCM_RECORD;
     sound2_intr_base = sound2_record_intr_base;
   }else{
     sound2_stat = ADPCM_REPLAY;
     sound2_intr_base = sound2_replay_intr_base;
   }
   interval_work_set_BDRY();
   if( sound2_stop_addr == sound2_limit_addr ){
     if( sound2_start_addr < sound2_stop_addr ) l = sound2_stop_addr - sound2_start_addr;
     else l = 0x3ffff - sound2_start_addr;
   }else{
     if ( sound2_start_addr<sound2_stop_addr && sound2_start_addr<sound2_limit_addr ){ if( sound2_stop_addr<sound2_limit_addr ) l = sound2_stop_addr - sound2_start_addr;
            else l = 0;
     }else if( sound2_stop_addr<sound2_start_addr && sound2_stop_addr<sound2_limit_addr ){ if( sound2_start_addr<sound2_limit_addr ) l = sound2_limit_addr - sound2_start_addr + sound2_stop_addr;
            else l = 0x3ffff - sound2_start_addr;
     }else { if( sound2_start_addr<sound2_stop_addr ) l = sound2_stop_addr - sound2_start_addr;
        else l = 0x3ffff - sound2_start_addr;
     }
   }
   if( l ){
     sound2_notice_EOS = (1);
     interval_work_set_EOS( l+1 );
   }else{
     sound2_notice_EOS = (0);
   }
   sound2_FLAG_PCMBSY = 1;
 }else{
   if( sound2_rec ){
     sound2_stat = ADPCM_WRITE;
     sound2_delay |= 0x08;
   }else{
     sound2_stat = ADPCM_READ;
     sound2_read_dummy = 2;
   }
   sound2_data_addr = sound2_start_addr;
 }
      }
    }
    if( data & 0x01 ){
      sound2_repeat = 0;
      sound2_FLAG_PCMBSY = 0;
    }
    if( !sound2_mem ) sound2_stat = ADPCM_HALT;
    break;
  case 0x01:
    sound2_mem_size = (data & 0x02) ? 5 : 2;
    break;
  case 0x02:
  case 0x03:
    sound2_start_addr = ((int)sound2_reg[ 0x03 ] << 8) | sound2_reg[ 0x02 ];
    sound2_start_addr = (sound2_start_addr << sound2_mem_size);
    sound2_start_addr &= 0x3ffff;
    break;
  case 0x04:
  case 0x05:
    sound2_stop_addr = ((int)sound2_reg[ 0x05 ] << 8) | sound2_reg[ 0x04 ];
    sound2_stop_addr = ((sound2_stop_addr+1) << sound2_mem_size ) -1;
    sound2_stop_addr &= 0x3ffff;
    break;
  case 0x08:
    if( sound2_stat==ADPCM_WRITE ){
      if( sound2_data_addr == sound2_stop_addr ||
   sound2_data_addr == 0x3ffff ){
 sound2_delay |= 0x04;
      }
      sound2_adpcm[ sound2_data_addr ] = data;
 sound2_data_addr = (sound2_data_addr+1) & 0x3ffff;
      sound2_delay |= 0x08;
    }
    break;
  case 0x0c:
  case 0x0d:
    sound2_limit_addr = ((int)sound2_reg[ 0x0d ] << 8) | sound2_reg[ 0x0c ];
    sound2_limit_addr = ((sound2_limit_addr+1)<< sound2_mem_size ) -1;
    sound2_limit_addr &= 0x3ffff;
    break;
  case 0x06:
  case 0x07:
    wk = (((int)sound2_reg[ 0x07 ]&0x07) << 8) | sound2_reg[ 0x06 ];
    wk &= 0x7ff;
    if( wk==0 ) wk = 0x800;
    sound2_record_intr_base = wk;
    break;
  case 0x09:
  case 0x0a:
    wk = ((int)sound2_reg[ 0x0a ] << 8) | sound2_reg[ 0x09 ];
    if( wk==0 ) wk = 0x10000;
    sound2_replay_intr_base = 72 * 65536 / wk;
    break;
  case 0x10:
    change_sound_flags( 0x10 );
    break;
  }
}

canonical_original_code:
void sound2_out_data(unsigned char data)
{
  int wk; int l;
  sound2_reg[ sound2_reg_select ] = data;
  xmame_sound2_out_data( data );
  switch( sound2_reg_select ){
  case 0x00:
    sound2_repeat = data & 0x10;
    sound2_mem = data & 0x20;
    sound2_rec = data & 0x40;
    if( sound2_FLAG_PCMBSY ){
      if( !sound2_mem ) sound2_FLAG_PCMBSY = 0;
    }else{
      if( sound2_mem ){
 if( data & 0x80 ){
   if( sound2_rec ){
     sound2_stat = ADPCM_RECORD;
     sound2_intr_base = sound2_record_intr_base;
   }else{
     sound2_stat = ADPCM_REPLAY;
     sound2_intr_base = sound2_replay_intr_base;
   }
   interval_work_set_BDRY();
   if( sound2_stop_addr == sound2_limit_addr ){
     if( sound2_start_addr < sound2_stop_addr ) l = sound2_stop_addr - sound2_start_addr;
     else l = 0x3ffff - sound2_start_addr;
   }else{
     if ( sound2_start_addr<sound2_stop_addr && sound2_start_addr<sound2_limit_addr ){ if( sound2_stop_addr<sound2_limit_addr ) l = sound2_stop_addr - sound2_start_addr;
            else l = 0;
     }else if( sound2_stop_addr<sound2_start_addr && sound2_stop_addr<sound2_limit_addr ){ if( sound2_start_addr<sound2_limit_addr ) l = sound2_limit_addr - sound2_start_addr + sound2_stop_addr;
            else l = 0x3ffff - sound2_start_addr;
     }else { if( sound2_start_addr<sound2_stop_addr ) l = sound2_stop_addr - sound2_start_addr;
        else l = 0x3ffff - sound2_start_addr;
     }
   }
   if( l ){
     sound2_notice_EOS = (1);
     interval_work_set_EOS( l+1 );
   }else{
     sound2_notice_EOS = (0);
   }
   sound2_FLAG_PCMBSY = 1;
 }else{
   if( sound2_rec ){
     sound2_stat = ADPCM_WRITE;
     sound2_delay |= 0x08;
   }else{
     sound2_stat = ADPCM_READ;
     sound2_read_dummy = 2;
   }
   sound2_data_addr = sound2_start_addr;
 }
      }
    }
    if( data & 0x01 ){
      sound2_repeat = 0;
      sound2_FLAG_PCMBSY = 0;
    }
    if( !sound2_mem ) sound2_stat = ADPCM_HALT;
    break;
  case 0x01:
    sound2_mem_size = (data & 0x02) ? 5 : 2;
    break;
  case 0x02:
  case 0x03:
    sound2_start_addr = ((int)sound2_reg[ 0x03 ] << 8) | sound2_reg[ 0x02 ];
    sound2_start_addr = (sound2_start_addr << sound2_mem_size);
    sound2_start_addr &= 0x3ffff;
    break;
  case 0x04:
  case 0x05:
    sound2_stop_addr = ((int)sound2_reg[ 0x05 ] << 8) | sound2_reg[ 0x04 ];
    sound2_stop_addr = ((sound2_stop_addr+1) << sound2_mem_size ) -1;
    sound2_stop_addr &= 0x3ffff;
    break;
  case 0x08:
    if( sound2_stat==ADPCM_WRITE ){
      if( sound2_data_addr == sound2_stop_addr ||
   sound2_data_addr == 0x3ffff ){
 sound2_delay |= 0x04;
      }
      sound2_adpcm[ sound2_data_addr ] = data;
 sound2_data_addr = (sound2_data_addr+1) & 0x3ffff;
      sound2_delay |= 0x08;
    }
    break;
  case 0x0c:
  case 0x0d:
    sound2_limit_addr = ((int)sound2_reg[ 0x0d ] << 8) | sound2_reg[ 0x0c ];
    sound2_limit_addr = ((sound2_limit_addr+1)<< sound2_mem_size ) -1;
    sound2_limit_addr &= 0x3ffff;
    break;
  case 0x06:
  case 0x07:
    wk = (((int)sound2_reg[ 0x07 ]&0x07) << 8) | sound2_reg[ 0x06 ];
    wk &= 0x7ff;
    if( wk==0 ) wk = 0x800;
    sound2_record_intr_base = wk;
    break;
  case 0x09:
  case 0x0a:
    wk = ((int)sound2_reg[ 0x0a ] << 8) | sound2_reg[ 0x09 ];
    if( wk==0 ) wk = 0x10000;
    sound2_replay_intr_base = 72 * 65536 / wk;
    break;
  case 0x10:
    change_sound_flags( 0x10 );
    break;
  }
}

variable_types: wk int

variable_types: l int


decompiled_code:
__int64 sound2_in_status()
{
int v0; // eax

xmame_sound2_in_status();
if ( (sound2_delay & 8) != 0 )
{
sound2_delay &= 0xFFFFFFF7;
if ( !sound2_MSK_BRDY )
sound2_FLAG_BRDY = 1;
}
if ( (sound2_delay & 4) != 0 )
{
sound2_delay &= 0xFFFFFFFB;
if ( !sound2_MSK_EOS )
sound2_FLAG_EOS = 1;
}
v0 = (16 * sound2_FLAG_ZERO) | (32 * sound2_FLAG_PCMBSY);
LOBYTE(v0) = sound_FLAG_A | v0;
return (2 * sound_FLAG_B) | (4 * sound2_FLAG_EOS) | (8 * sound2_FLAG_BRDY) | (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax

xmame_sound2_in_status();
if ( (sound2_delay & 8) != 0 )
{
sound2_delay &= 0xFFFFFFF7;
if ( !sound2_MSK_BRDY )
sound2_FLAG_BRDY = 1;
}
if ( (sound2_delay & 4) != 0 )
{
sound2_delay &= 0xFFFFFFFB;
if ( !sound2_MSK_EOS )
sound2_FLAG_EOS = 1;
}
v0 = (16 * sound2_FLAG_ZERO) | (32 * sound2_FLAG_PCMBSY);
LOBYTE(v0) = sound_FLAG_A | v0;
return (2 * sound_FLAG_B) | (4 * sound2_FLAG_EOS) | (8 * sound2_FLAG_BRDY) | (unsigned int)v0;
}

original_code:
byte sound2_in_status( void )
{
  xmame_sound2_in_status();
  if( sound2_delay & 0x08 ){
    sound2_delay &= ~0x08;
    if( !sound2_MSK_BRDY ) sound2_FLAG_BRDY = 1;
  }
  if( sound2_delay & 0x04 ){
    sound2_delay &= ~0x04;
    if( !sound2_MSK_EOS ) sound2_FLAG_EOS = 1;
  }
  return ( sound2_FLAG_PCMBSY << 5 ) | ( sound2_FLAG_ZERO << 4 ) |
         ( sound2_FLAG_BRDY << 3 ) | ( sound2_FLAG_EOS << 2 ) |
  ( sound_FLAG_B << 1 ) | sound_FLAG_A;
}

canonical_original_code:
unsigned char sound2_in_status()
{
  xmame_sound2_in_status();
  if( sound2_delay & 0x08 ){
    sound2_delay &= ~0x08;
    if( !sound2_MSK_BRDY ) sound2_FLAG_BRDY = 1;
  }
  if( sound2_delay & 0x04 ){
    sound2_delay &= ~0x04;
    if( !sound2_MSK_EOS ) sound2_FLAG_EOS = 1;
  }
  return ( sound2_FLAG_PCMBSY << 5 ) | ( sound2_FLAG_ZERO << 4 ) |
         ( sound2_FLAG_BRDY << 3 ) | ( sound2_FLAG_EOS << 2 ) |
  ( sound_FLAG_B << 1 ) | sound_FLAG_A;
}


decompiled_code:
__int64 sound2_in_data()
{
__int64 result; // rax

xmame_sound2_in_data();
result = 0LL;
if ( sound2_reg_select == 8 && sound2_stat == 4 )
{
if ( sound2_read_dummy )
{
--sound2_read_dummy;
result = 0LL;
}
else
{
result = 0LL;
if ( sound2_data_addr == sound2_stop_addr )
return result;
result = *(unsigned __int8 *)(sound2_adpcm + sound2_data_addr);
sound2_data_addr = (sound2_data_addr + 1) & 0x3FFFF;
}
if ( sound2_data_addr != sound2_stop_addr )
sound2_FLAG_BRDY = sound2_MSK_BRDY == 0;
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax

xmame_sound2_in_data();
result = 0LL;
if ( sound2_reg_select == 8 && sound2_stat == 4 )
{
if ( sound2_read_dummy )
{
--sound2_read_dummy;
result = 0LL;
}
else
{
result = 0LL;
if ( sound2_data_addr == sound2_stop_addr )
return result;
result = *(unsigned __int8 *)(sound2_adpcm + sound2_data_addr);
sound2_data_addr = (sound2_data_addr + 1) & 0x3FFFF;
}
if ( sound2_data_addr != sound2_stop_addr )
sound2_FLAG_BRDY = sound2_MSK_BRDY == 0;
}
return result;
}

original_code:
byte sound2_in_data( void )
{
  byte data = 0x00;
  xmame_sound2_in_data();
  if( sound2_reg_select==0x08 ){
    if( sound2_stat==ADPCM_READ ){
      if( sound2_read_dummy ){
 sound2_read_dummy --;
      }else{
 if( sound2_data_addr != sound2_stop_addr ){
   data = sound2_adpcm[ sound2_data_addr ];
     sound2_data_addr = (sound2_data_addr+1) & 0x3ffff;
 }
      }
      if( sound2_data_addr != sound2_stop_addr ){
 sound2_FLAG_BRDY = sound2_MSK_BRDY ? 0 : 1;
      }
      return data;
    }
  }
  return 0x00;
}

canonical_original_code:
unsigned char sound2_in_data()
{
  unsigned char data = 0x00;
  xmame_sound2_in_data();
  if( sound2_reg_select==0x08 ){
    if( sound2_stat==ADPCM_READ ){
      if( sound2_read_dummy ){
 sound2_read_dummy --;
      }else{
 if( sound2_data_addr != sound2_stop_addr ){
   data = sound2_adpcm[ sound2_data_addr ];
     sound2_data_addr = (sound2_data_addr+1) & 0x3ffff;
 }
      }
      if( sound2_data_addr != sound2_stop_addr ){
 sound2_FLAG_BRDY = sound2_MSK_BRDY ? 0 : 1;
      }
      return data;
    }
  }
  return 0x00;
}


decompiled_code:
_BOOL8 statesave_sound()
{
int v0; // edx
_BOOL8 result; // rax

v0 = statesave_table("SND ", &suspend_sound_work);
result = 0LL;
if ( !v0 )
result = (unsigned int)statesave_table("SND2", &suspend_sound_work2) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 func9()
{
int v0; // edx
_BOOL8 result; // rax

v0 = statesave_table("SND ", &suspend_sound_work);
result = 0LL;
if ( !v0 )
result = (unsigned int)statesave_table("SND2", &suspend_sound_work2) == 0;
return result;
}

original_code:
int statesave_sound( void )
{
  if( statesave_table( "SND ", suspend_sound_work ) != (0) ) return (0);
  if( statesave_table( "SND2", suspend_sound_work2 ) != (0) ) return (0);
  return (1);
}

canonical_original_code:
int statesave_sound()
{
  if( statesave_table( "SND ", suspend_sound_work ) != (0) ) return (0);
  if( statesave_table( "SND2", suspend_sound_work2 ) != (0) ) return (0);
  return (1);
}

return_type: int


decompiled_code:
__int64 stateload_sound()
{
int v0; // edx
__int64 result; // rax
int v2; // edx

v0 = stateload_table("SND ", &suspend_sound_work);
result = 0LL;
if ( !v0 )
{
v2 = stateload_table("SND2", &suspend_sound_work2);
result = 1LL;
if ( v2 )
{
puts("stateload : Statefile is old. (ver 0.6.0 or 1?)");
if ( sound_prescaler == 6 )
{
sound_prescaler_sel = 2;
result = 1LL;
}
else
{
if ( sound_prescaler == 3 )
sound_prescaler_sel = 3;
else
sound_prescaler_sel = 0;
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // edx
__int64 result; // rax
int v2; // edx

v0 = stateload_table("SND ", &suspend_sound_work);
result = 0LL;
if ( !v0 )
{
v2 = stateload_table("SND2", &suspend_sound_work2);
result = 1LL;
if ( v2 )
{
puts("stateload : Statefile is old. (ver 0.6.0 or 1?)");
if ( sound_prescaler == 6 )
{
sound_prescaler_sel = 2;
result = 1LL;
}
else
{
if ( sound_prescaler == 3 )
sound_prescaler_sel = 3;
else
sound_prescaler_sel = 0;
result = 1LL;
}
}
}
return result;
}

original_code:
int stateload_sound( void )
{
  if( stateload_table( "SND ", suspend_sound_work ) != (0) ) return (0);
  if( stateload_table( "SND2", suspend_sound_work2 ) != (0) ){
    printf( "stateload : Statefile is old. (ver 0.6.0 or 1?)\n" );
    if ( sound_prescaler == 6 ) sound_prescaler_sel = 2;
    else if ( sound_prescaler == 3 ) sound_prescaler_sel = 3;
    else sound_prescaler_sel = 0;
    return (1);
  }
  return (1);
}

canonical_original_code:
int stateload_sound()
{
  if( stateload_table( "SND ", suspend_sound_work ) != (0) ) return (0);
  if( stateload_table( "SND2", suspend_sound_work2 ) != (0) ){
    printf( "stateload : Statefile is old. (ver 0.6.0 or 1?)\n" );
    if ( sound_prescaler == 6 ) sound_prescaler_sel = 2;
    else if ( sound_prescaler == 3 ) sound_prescaler_sel = 3;
    else sound_prescaler_sel = 0;
    return (1);
  }
  return (1);
}


decompiled_code:
void sound_output_after_stateload()
{
_DWORD *v0; // r12
__int64 v1; // rbp
__int64 i; // rbx
_DWORD *v3; // rax
_DWORD *v4; // rax
__int64 j; // rbx
char v6; // bl

if ( use_sound )
{
switch ( sound_prescaler_sel )
{
case 1:
xmame_sound_out_reg(47LL);
xmame_sound_out_data(0LL);
xmame_sound_out_reg(46LL);
xmame_sound_out_data(0LL);
break;
case 3:
xmame_sound_out_reg(46LL);
xmame_sound_out_data(0LL);
break;
case 0:
xmame_sound_out_reg(47LL);
xmame_sound_out_data(0LL);
break;
}
v0 = &addr_2203_2;
if ( sound_board )
v0 = &addr_2608_1;
v1 = (__int64)&v0[2 * (sound_board == 0 ? 13 : 16) + 2];
for ( i = 0LL; i != 256; ++i )
{
v3 = v0;
while ( *v3 > (int)i || v3[1] < (int)i )
{
v3 += 2;
if ( v3 == (_DWORD *)v1 )
goto LABEL_15;
}
xmame_sound_out_reg((unsigned __int8)i);
xmame_sound_out_data((unsigned __int8)sound_reg[i]);
LABEL_15:
;
}
if ( sound_board == 1 )
{
for ( j = 0LL; j != 256; ++j )
{
v4 = &addr_2608_2_0;
while ( *v4 > (int)j || v4[1] < (int)j )
{
v4 += 2;
if ( v4 == (_DWORD *)&unk_1130 )
goto LABEL_23;
}
xmame_sound2_out_reg((unsigned __int8)j);
xmame_sound2_out_data((unsigned __int8)sound2_reg[j]);
LABEL_23:
;
}
v6 = sound2_reg[0];
xmame_sound2_out_reg(0LL);
xmame_sound2_out_data(v6 & 0x7F);
}
}
}

canonical_decompiled_code:
void func11()
{
_DWORD *v0; // r12
__int64 v1; // rbp
__int64 i; // rbx
_DWORD *v3; // rax
_DWORD *v4; // rax
__int64 j; // rbx
char v6; // bl

if ( use_sound )
{
switch ( sound_prescaler_sel )
{
case 1:
xmame_sound_out_reg(47LL);
xmame_sound_out_data(0LL);
xmame_sound_out_reg(46LL);
xmame_sound_out_data(0LL);
break;
case 3:
xmame_sound_out_reg(46LL);
xmame_sound_out_data(0LL);
break;
case 0:
xmame_sound_out_reg(47LL);
xmame_sound_out_data(0LL);
break;
}
v0 = &addr_2203_2;
if ( sound_board )
v0 = &addr_2608_1;
v1 = (__int64)&v0[2 * (sound_board == 0 ? 13 : 16) + 2];
for ( i = 0LL; i != 256; ++i )
{
v3 = v0;
while ( *v3 > (int)i || v3[1] < (int)i )
{
v3 += 2;
if ( v3 == (_DWORD *)v1 )
goto LABEL_15;
}
xmame_sound_out_reg((unsigned __int8)i);
xmame_sound_out_data((unsigned __int8)sound_reg[i]);
LABEL_15:
;
}
if ( sound_board == 1 )
{
for ( j = 0LL; j != 256; ++j )
{
v4 = &addr_2608_2_0;
while ( *v4 > (int)j || v4[1] < (int)j )
{
v4 += 2;
if ( v4 == (_DWORD *)&unk_1130 )
goto LABEL_23;
}
xmame_sound2_out_reg((unsigned __int8)j);
xmame_sound2_out_data((unsigned __int8)sound2_reg[j]);
LABEL_23:
;
}
v6 = sound2_reg[0];
xmame_sound2_out_reg(0LL);
xmame_sound2_out_data(v6 & 0x7F);
}
}
}

original_code:
void sound_output_after_stateload( void )
{
  const int (*addr)[2];
  int size;
  static const int addr_2203[][2] = {
    { 0x00, 0x0f },
    { 0x24, 0x28 },
    { 0x30, 0x3e },
    { 0x40, 0x4e },
    { 0x50, 0x5e },
    { 0x60, 0x6e },
    { 0x70, 0x7e },
    { 0x80, 0x8e },
    { 0x90, 0x9e },
    { 0xa0, 0xa2 },
    { 0xa4, 0xa6 },
    { 0xa8, 0xaa },
    { 0xac, 0xae },
    { 0xb0, 0xb2 },
  };
  static const int addr_2608[][2] = {
    { 0x00, 0x0f },
    { 0x11, 0x12 },
    { 0x18, 0x1d },
    { 0x22, 0x22 },
    { 0x24, 0x29 },
    { 0x30, 0x3e },
    { 0x40, 0x4e },
    { 0x50, 0x5e },
    { 0x60, 0x6e },
    { 0x70, 0x7e },
    { 0x80, 0x8e },
    { 0x90, 0x9e },
    { 0xa0, 0xa2 },
    { 0xa4, 0xa6 },
    { 0xa8, 0xaa },
    { 0xac, 0xae },
    { 0xb0, 0xb2 },
  };
  static const int addr_2608_2[][2] = {
    { 0x01, 0x10 },
    { 0x30, 0x3e },
    { 0x40, 0x4e },
    { 0x50, 0x5e },
    { 0x60, 0x6e },
    { 0x70, 0x7e },
    { 0x80, 0x8e },
    { 0x90, 0x9e },
    { 0xa0, 0xa2 },
    { 0xa4, 0xa6 },
    { 0xa8, 0xaa },
    { 0xac, 0xae },
    { 0xb0, 0xb2 },
    { 0xb4, 0xb6 },
  };
  if( use_sound ){
    int i, j;
    switch( sound_prescaler_sel ){
    case 0:
      xmame_sound_out_reg( 0x2f ); xmame_sound_out_data( 0 );
      break;
    case 1:
      xmame_sound_out_reg( 0x2f ); xmame_sound_out_data( 0 );
      xmame_sound_out_reg( 0x2e ); xmame_sound_out_data( 0 );
      break;
    case 3:
      xmame_sound_out_reg( 0x2e ); xmame_sound_out_data( 0 );
      break;
    default:
      break;
    }
    if( sound_board==SOUND_I ){ addr = addr_2203; size = (int)(sizeof(addr_2203)/sizeof((addr_2203)[0])); }
    else { addr = addr_2608; size = (int)(sizeof(addr_2608)/sizeof((addr_2608)[0])); }
    for( i=0; i<256; i++ ){
      for( j=0; j<size; j++ ){
 if( addr[j][0] <= i && i <= addr[j][1] ){
   xmame_sound_out_reg( i );
   xmame_sound_out_data( sound_reg[ i ] );
   break;
 }
      }
    }
    if( sound_board==SOUND_II ){
      addr = addr_2608_2;
      size = (int)(sizeof(addr_2608_2)/sizeof((addr_2608_2)[0]));
      for( i=0; i<256; i++ ){
 for( j=0; j<size; j++ ){
   if( addr[j][0] <= i && i <= addr[j][1] ){
     xmame_sound2_out_reg( i );
     xmame_sound2_out_data( sound2_reg[ i ] );
     break;
   }
 }
      }
      i = sound2_reg[ 0 ];
      i &= ~0x80;
      xmame_sound2_out_reg( 0 );
      xmame_sound2_out_data( i );
    }
  }
}

variable_types:
addr
int (*)[2]

variable_types: size int

variable_types: j int


decompiled_code:
void __fastcall set_romaji_que(char *a1)
{
char *v1; // rcx
char v2; // dl
int v3; // eax

v1 = a1 + 1;
v2 = *a1;
if ( *a1 )
{
v3 = romaji_set;
do
{
romaji_que[v3] = v2;
v3 = ((_BYTE)v3 + 1) & 0x3F;
v2 = *v1++;
}
while ( v2 );
romaji_set = v3;
}
}

canonical_decompiled_code:
void __fastcall func0(char *a1)
{
char *v1; // rcx
char v2; // dl
int v3; // eax

v1 = a1 + 1;
v2 = *a1;
if ( *a1 )
{
v3 = romaji_set;
do
{
romaji_que[v3] = v2;
v3 = ((_BYTE)v3 + 1) & 0x3F;
v2 = *v1++;
}
while ( v2 );
romaji_set = v3;
}
}

original_code:
static void set_romaji_que( const Uchar *p )
{
  Uchar c;
  while( (c = *p++ ) ){
    romaji_que[ romaji_set++ ] = c;
    romaji_set &= ((64)-1);
  }
}

canonical_original_code:
static void set_romaji_que(unsigned char *p)
{
  unsigned char c;
  while( (c = *p++ ) ){
    romaji_que[ romaji_set++ ] = c;
    romaji_set &= ((64)-1);
  }
}

variable_types:
p
unsigned char *

variable_types:
c
unsigned char


romajicmp

decompiled_code:
int __fastcall romajicmp(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

canonical_decompiled_code:
int __fastcall func1(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

original_code:
static int romajicmp( const void *p1, const void *p2 )
{
  return strcmp( ((const romaji_list *)p1)->s, ((const romaji_list *)p2)->s );
}

canonical_original_code:
static int romajicmp(void *p1, void *p2)
{
  return strcmp( ((struct romaji_list *)p1)->s, ((struct romaji_list *)p2)->s );
}

user_defined_types: struct romaji_list { char *s; unsigned char list[4]; }


decompiled_code:
__int64 romaji_output()
{
__int64 result; // rax
unsigned __int8 v1; // cl
unsigned __int8 v2; // cl

result = (unsigned int)press_timer;
if ( press_timer == 4 )
{
v2 = romaji_que[romaji_ptr];
romaji_ptr = ((_BYTE)romaji_ptr + 1) & 0x3F;
if ( (v2 & 8) != 0 )
key_scan[8] |= 0x40u;
result = v2 >> 4;
key_scan[result] |= 1 << (v2 & 7);
press_timer = 5;
}
else if ( press_timer == 8 )
{
press_timer = 0;
}
else if ( press_timer )
{
result = (unsigned int)++press_timer;
}
else
{
result = (unsigned int)romaji_ptr;
if ( romaji_ptr != romaji_set )
{
v1 = romaji_que[romaji_ptr];
if ( (v1 & 8) != 0 )
key_scan[8] &= 0xBFu;
result = v1 >> 4;
key_scan[result] &= ~(1 << (v1 & 7));
press_timer = 1;
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
unsigned __int8 v1; // cl
unsigned __int8 v2; // cl

result = (unsigned int)press_timer;
if ( press_timer == 4 )
{
v2 = romaji_que[romaji_ptr];
romaji_ptr = ((_BYTE)romaji_ptr + 1) & 0x3F;
if ( (v2 & 8) != 0 )
key_scan[8] |= 0x40u;
result = v2 >> 4;
key_scan[result] |= 1 << (v2 & 7);
press_timer = 5;
}
else if ( press_timer == 8 )
{
press_timer = 0;
}
else if ( press_timer )
{
result = (unsigned int)++press_timer;
}
else
{
result = (unsigned int)romaji_ptr;
if ( romaji_ptr != romaji_set )
{
v1 = romaji_que[romaji_ptr];
if ( (v1 & 8) != 0 )
key_scan[8] &= 0xBFu;
result = v1 >> 4;
key_scan[result] &= ~(1 << (v1 & 7));
press_timer = 1;
}
}
return result;
}

original_code:
void romaji_output( void )
{
  Uint c;
  switch( press_timer ){
  case 0:
    if( romaji_ptr != romaji_set ){
      c = romaji_que[ romaji_ptr ];
      if( c & 0x08 ) key_scan[8] &= ~0x40;
      key_scan[ c>>4 ] &= ~(1<<(c&7));
      press_timer ++;
    }
    break;
  case (4):
    c = romaji_que[ romaji_ptr++ ];
    romaji_ptr &= ((64)-1);
    if( c & 0x08 ) key_scan[8] |= 0x40;
    key_scan[ c>>4 ] |= (1<<(c&7));
    press_timer ++;
    break;
  case (4)*2:
    press_timer = 0;
    break;
  default:
    press_timer ++;
  }
}

canonical_original_code:
void romaji_output()
{
  unsigned int c;
  switch( press_timer ){
  case 0:
    if( romaji_ptr != romaji_set ){
      c = romaji_que[ romaji_ptr ];
      if( c & 0x08 ) key_scan[8] &= ~0x40;
      key_scan[ c>>4 ] &= ~(1<<(c&7));
      press_timer ++;
    }
    break;
  case (4):
    c = romaji_que[ romaji_ptr++ ];
    romaji_ptr &= ((64)-1);
    if( c & 0x08 ) key_scan[8] |= 0x40;
    key_scan[ c>>4 ] |= (1<<(c&7));
    press_timer ++;
    break;
  case (4)*2:
    press_timer = 0;
    break;
  default:
    press_timer ++;
  }
}

variable_types:
c
unsigned int


decompiled_code:
__int64 romaji_clear()
{
__int64 result; // rax

result = (unsigned int)(press_timer - 1);
if ( (unsigned int)result <= 3 )
{
press_timer = 4;
result = romaji_output();
}
romaji_set = 0;
romaji_ptr = 0;
press_timer = 0;
input_size = 0;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

result = (unsigned int)(press_timer - 1);
if ( (unsigned int)result <= 3 )
{
press_timer = 4;
result = func2();
}
romaji_set = 0;
romaji_ptr = 0;
press_timer = 0;
input_size = 0;
return result;
}

original_code:
void romaji_clear( void )
{
  if( 0< press_timer && press_timer <= (4) ){
    press_timer = (4);
    romaji_output();
  }
  romaji_set = 0;
  romaji_ptr = 0;
  press_timer = 0;
  input_size = 0;
}

canonical_original_code:
void romaji_clear()
{
  if( 0< press_timer && press_timer <= (4) ){
    press_timer = (4);
    romaji_output();
  }
  romaji_set = 0;
  romaji_ptr = 0;
  press_timer = 0;
  input_size = 0;
}


decompiled_code:
__int64 romaji_init()
{
void **v0; // rax
unsigned int v1; // esi
void **v2; // rdx
void **v3; // rsi
int v4; // eax
int v5; // ecx
void **v6; // rdx
int v7; // eax
int v8; // ecx
__int64 result; // rax

romaji_clear();
if ( romaji_type == 1 )
{
v0 = &list_msime;
v1 = 243;
}
else
{
v0 = &list_egg;
if ( romaji_type == 2 )
v0 = &list_atok;
v1 = 214;
if ( romaji_type != 2 )
v1 = 180;
}
v2 = v0 + 2;
nr_list = 1;
list[0] = _mm_load_si128((const __m128i *)v0);
v3 = &v0[2 * v1];
do
{
if ( v2 == v3 )
break;
v2 += 2;
v4 = nr_list++;
v5 = nr_list;
list[v4] = _mm_load_si128((const __m128i *)v2 - 1);
}
while ( v5 <= 279 );
qsort(list, nr_list, 0x10uLL, romajicmp);
v6 = (void **)&list_mark;
do
{
v6 += 2;
v7 = nr_list++;
v8 = nr_list;
result = v7;
list[result] = _mm_load_si128((const __m128i *)v6 - 1);
}
while ( v8 <= 279 && v6 != &list_tu );
return result * 16;
}

canonical_decompiled_code:
__int64 func4()
{
void **v0; // rax
unsigned int v1; // esi
void **v2; // rdx
void **v3; // rsi
int v4; // eax
int v5; // ecx
void **v6; // rdx
int v7; // eax
int v8; // ecx
__int64 result; // rax

func3();
if ( romaji_type == 1 )
{
v0 = &list_msime;
v1 = 243;
}
else
{
v0 = &list_egg;
if ( romaji_type == 2 )
v0 = &list_atok;
v1 = 214;
if ( romaji_type != 2 )
v1 = 180;
}
v2 = v0 + 2;
nr_list = 1;
list[0] = _mm_load_si128((const __m128i *)v0);
v3 = &v0[2 * v1];
do
{
if ( v2 == v3 )
break;
v2 += 2;
v4 = nr_list++;
v5 = nr_list;
list[v4] = _mm_load_si128((const __m128i *)v2 - 1);
}
while ( v5 <= 279 );
qsort(list, nr_list, 0x10uLL, romajicmp);
v6 = (void **)&list_mark;
do
{
v6 += 2;
v7 = nr_list++;
v8 = nr_list;
result = v7;
list[result] = _mm_load_si128((const __m128i *)v6 - 1);
}
while ( v8 <= 279 && v6 != &list_tu );
return result * 16;
}

original_code:
void romaji_init( void )
{
  int i, nr_p;
  const romaji_list *p;
  romaji_clear();
  nr_list = 0;
  if ( romaji_type==1 ){ p = list_msime; nr_p = (int)(sizeof(list_msime)/sizeof((list_msime)[0])); }
  else if( romaji_type==2 ){ p = list_atok; nr_p = (int)(sizeof(list_atok)/sizeof((list_atok)[0])); }
  else { p = list_egg; nr_p = (int)(sizeof(list_egg)/sizeof((list_egg)[0])); }
  for( i=0; i<nr_p; i++ ){
    list[ nr_list ++ ] = *p ++;
    if( nr_list >= (int)(sizeof(list)/sizeof((list)[0])) ) break;
  }
  qsort( &list, nr_list, sizeof(romaji_list), romajicmp );
  p = list_mark;
  nr_p = (int)(sizeof(list_mark)/sizeof((list_mark)[0]));
  for( i=0; i<nr_p; i++ ){
    list[ nr_list ++ ] = *p ++;
    if( nr_list >= (int)(sizeof(list)/sizeof((list)[0])) ) break;
  }
}

canonical_original_code:
void romaji_init()
{
  int i; int nr_p;
  struct romaji_list *p;
  romaji_clear();
  nr_list = 0;
  if ( romaji_type==1 ){ p = list_msime; nr_p = (int)(sizeof(list_msime)/sizeof((list_msime)[0])); }
  else if( romaji_type==2 ){ p = list_atok; nr_p = (int)(sizeof(list_atok)/sizeof((list_atok)[0])); }
  else { p = list_egg; nr_p = (int)(sizeof(list_egg)/sizeof((list_egg)[0])); }
  for( i=0; i<nr_p; i++ ){
    list[ nr_list ++ ] = *p ++;
    if( nr_list >= (int)(sizeof(list)/sizeof((list)[0])) ) break;
  }
  qsort( &list, nr_list, sizeof(romaji_list), romajicmp );
  p = list_mark;
  nr_p = (int)(sizeof(list_mark)/sizeof((list_mark)[0]));
  for( i=0; i<nr_p; i++ ){
    list[ nr_list ++ ] = *p ++;
    if( nr_list >= (int)(sizeof(list)/sizeof((list)[0])) ) break;
  }
}

variable_types: struct romaji_list { char *s; unsigned char list[4]; } *


decompiled_code:
__int64 __fastcall romaji_input(int c)
{
char v1; // bp
unsigned __int64 v2; // rax
__int64 v3; // rdx
int v4; // ebx
bool v5; // zf
int v6; // ebx
__int64 v7; // r12
char v8; // bl
__int64 result; // rax
__m128i *v10; // rdi
int v11; // er9
_BYTE *v12; // rdx
__int64 v13; // rax
int v14; // ecx
__m128i *v15; // r8
unsigned __int16 v16; // ax

v1 = c;
v2 = (unsigned int)(c - 32);
if ( (unsigned int)v2 > 0x20 )
{
if ( (c & 0xFFFFFFDF) == 91 || c == 125 )
goto LABEL_3;
}
else
{
v3 = 0x10000F081LL;
if ( _bittest64(&v3, v2) )
goto LABEL_3;
}
v16 = (*_ctype_b_loc())[c];
if ( (v16 & 0x200) != 0 )
{
v1 = toupper(c);
goto LABEL_3;
}
if ( (v16 & 0x100) == 0 )
{
if ( c == 156 )
{
v8 = 6;
if ( input_size )
{
romaji_clear();
return 0LL;
}
}
else
{
if ( c == 152 || c == 194 )
return (unsigned int)c;
v8 = c + 106;
if ( (unsigned int)(c - 150) > 0x2D )
goto LABEL_15;
}
result = (unsigned int)c;
if ( ((0x200000001003uLL >> v8) & 1) != 0 )
return result;
LABEL_15:
romaji_clear();
return (unsigned int)c;
}
LABEL_3:
v4 = input_size;
input_buf[input_size] = v1;
v5 = v4 == -1;
v6 = v4 + 1;
input_size = v6;
if ( v5 )
return 0LL;
v7 = v6 - 1LL;
while ( nr_list <= 0 )
{
LABEL_27:
if ( input_buf[0] == 78 )
{
set_romaji_que("Q");
goto LABEL_33;
}
if ( v6 <= 1 )
{
LABEL_33:
input_size = --v6;
memmove(input_buf, &byte_16AD, v7--);
if ( !v6 )
return 0LL;
}
else
{
if ( input_buf[0] == byte_16AD )
set_romaji_que("Z");
input_size = --v6;
memmove(input_buf, &byte_16AD, v7--);
}
}
v15 = &list[nr_list];
v11 = 0;
v10 = list;
while ( 1 )
{
LABEL_20:
v12 = (_BYTE *)v10->m128i_i64[0];
if ( v6 > 0 )
{
v13 = 0LL;
while ( input_buf[v13] == *v12 )
{
v14 = v13;
++v12;
if ( v6 == ++v13 )
{
if ( v14 == -1 )
break;
if ( !*v12 )
{
set_romaji_que(&v10->m128i_i8[8]);
input_size = 0;
return 0LL;
}
if ( v15 == ++v10 )
goto LABEL_36;
v11 = 1;
goto LABEL_20;
}
}
}
if ( v11 )
break;
if ( v15 == ++v10 )
goto LABEL_27;
}
LABEL_36:
if ( input_size > 3 )
input_size = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int c)
{
char v1; // bp
unsigned __int64 v2; // rax
__int64 v3; // rdx
int v4; // ebx
bool v5; // zf
int v6; // ebx
__int64 v7; // r12
char v8; // bl
__int64 result; // rax
__m128i *v10; // rdi
int v11; // er9
_BYTE *v12; // rdx
__int64 v13; // rax
int v14; // ecx
__m128i *v15; // r8
unsigned __int16 v16; // ax

v1 = c;
v2 = (unsigned int)(c - 32);
if ( (unsigned int)v2 > 0x20 )
{
if ( (c & 0xFFFFFFDF) == 91 || c == 125 )
goto LABEL_3;
}
else
{
v3 = 0x10000F081LL;
if ( _bittest64(&v3, v2) )
goto LABEL_3;
}
v16 = (*_ctype_b_loc())[c];
if ( (v16 & 0x200) != 0 )
{
v1 = toupper(c);
goto LABEL_3;
}
if ( (v16 & 0x100) == 0 )
{
if ( c == 156 )
{
v8 = 6;
if ( input_size )
{
func3();
return 0LL;
}
}
else
{
if ( c == 152 || c == 194 )
return (unsigned int)c;
v8 = c + 106;
if ( (unsigned int)(c - 150) > 0x2D )
goto LABEL_15;
}
result = (unsigned int)c;
if ( ((0x200000001003uLL >> v8) & 1) != 0 )
return result;
LABEL_15:
func3();
return (unsigned int)c;
}
LABEL_3:
v4 = input_size;
input_buf[input_size] = v1;
v5 = v4 == -1;
v6 = v4 + 1;
input_size = v6;
if ( v5 )
return 0LL;
v7 = v6 - 1LL;
while ( nr_list <= 0 )
{
LABEL_27:
if ( input_buf[0] == 78 )
{
func0("Q");
goto LABEL_33;
}
if ( v6 <= 1 )
{
LABEL_33:
input_size = --v6;
memmove(input_buf, &byte_16AD, v7--);
if ( !v6 )
return 0LL;
}
else
{
if ( input_buf[0] == byte_16AD )
func0("Z");
input_size = --v6;
memmove(input_buf, &byte_16AD, v7--);
}
}
v15 = &list[nr_list];
v11 = 0;
v10 = list;
while ( 1 )
{
LABEL_20:
v12 = (_BYTE *)v10->m128i_i64[0];
if ( v6 > 0 )
{
v13 = 0LL;
while ( input_buf[v13] == *v12 )
{
v14 = v13;
++v12;
if ( v6 == ++v13 )
{
if ( v14 == -1 )
break;
if ( !*v12 )
{
func0(&v10->m128i_i8[8]);
input_size = 0;
return 0LL;
}
if ( v15 == ++v10 )
goto LABEL_36;
v11 = 1;
goto LABEL_20;
}
}
}
if ( v11 )
break;
if ( v15 == ++v10 )
goto LABEL_27;
}
LABEL_36:
if ( input_size > 3 )
input_size = 0;
return 0LL;
}

original_code:
int romaji_input( int key )
{
  int i, j;
  if( key == ' ' ||
      key == '@' ||
      key == '[' ||
      key == '/' ||
      key == '-' ||
      key == '{' ||
      key == '}' ||
      key == '.' ||
      key == ',' ||
      key == '\'' ){
    ;
  }
  else if( ((*__ctype_b_loc ())[(int) ((key))] & (unsigned short int) _ISlower) ){
    key = toupper( key );
  }
  else if( ((*__ctype_b_loc ())[(int) ((key))] & (unsigned short int) _ISupper) ){
    ;
  }
  else {
    if( key == KEY88_ESC && input_size != 0 ){
      romaji_clear();
      return 0;
    }
    if( key == KEY88_SHIFT ||
 key == KEY88_SHIFTL ||
 key == KEY88_SHIFTR ||
 key == KEY88_CAPS ||
 key == KEY88_KANA ||
 key == KEY88_GRAPH ){
      ;
    }else{
      romaji_clear();
    }
    return key;
  }
  input_buf[ input_size ] = key;
  input_size ++;
  while( input_size ){
    int list_size = nr_list;
    romaji_list *list_p = list;
    int same = (0);
    int nearly = (0);
    for( i=0; i<list_size; i++, list_p++ ){
      const char *s1 = input_buf;
      const char *s2 = list_p->s;
      for( j=0; j<input_size; j++, s1++, s2++ ){
 if( *s1 != *s2 ){ j=0; break; }
      }
      if( j==0 ){
 if( nearly ) break;
      }else{
 if( *s2 == '\0' ){
   same = (1); break;
 }else{
   nearly = (1);
 }
      }
    }
    if( same ){
      set_romaji_que( list_p->list );
      input_size = 0;
      break;
    }else if( nearly ){
      break;
    }else{
      if( input_buf[0] == 'N' ){
 set_romaji_que( list_NN.list );
      }else if( input_size >= 2 &&
  input_buf[0]==input_buf[1] ){
 set_romaji_que( list_tu.list );
      }
      input_size --;
      memmove( &input_buf[0], &input_buf[1],input_size );
    }
  }
  if( input_size >= (int)sizeof(input_buf) )
    input_size = 0;
  return 0;
}

canonical_original_code:
int romaji_input(int key)
{
  int i; int j;
  if( key == ' ' ||
      key == '@' ||
      key == '[' ||
      key == '/' ||
      key == '-' ||
      key == '{' ||
      key == '}' ||
      key == '.' ||
      key == ',' ||
      key == '\'' ){
    ;
  }
  else if( ((*__ctype_b_loc ())[(int) ((key))] & (unsigned short) _ISlower) ){
    key = toupper( key );
  }
  else if( ((*__ctype_b_loc ())[(int) ((key))] & (unsigned short) _ISupper) ){
    ;
  }
  else {
    if( key == KEY88_ESC && input_size != 0 ){
      romaji_clear();
      return 0;
    }
    if( key == KEY88_SHIFT ||
 key == KEY88_SHIFTL ||
 key == KEY88_SHIFTR ||
 key == KEY88_CAPS ||
 key == KEY88_KANA ||
 key == KEY88_GRAPH ){
      ;
    }else{
      romaji_clear();
    }
    return key;
  }
  input_buf[ input_size ] = key;
  input_size ++;
  while( input_size ){
    int list_size = nr_list;
    struct romaji_list *list_p = list;
    int same = (0);
    int nearly = (0);
    for( i=0; i<list_size; i++, list_p++ ){
      char *s1 = input_buf;
      char *s2 = list_p->s;
      for( j=0; j<input_size; j++, s1++, s2++ ){
 if( *s1 != *s2 ){ j=0; break; }
      }
      if( j==0 ){
 if( nearly ) break;
      }else{
 if( *s2 == '\0' ){
   same = (1); break;
 }else{
   nearly = (1);
 }
      }
    }
    if( same ){
      set_romaji_que( list_p->list );
      input_size = 0;
      break;
    }else if( nearly ){
      break;
    }else{
      if( input_buf[0] == 'N' ){
 set_romaji_que( list_NN.list );
      }else if( input_size >= 2 &&
  input_buf[0]==input_buf[1] ){
 set_romaji_que( list_tu.list );
      }
      input_size --;
      memmove( &input_buf[0], &input_buf[1],input_size );
    }
  }
  if( input_size >= (int)sizeof(input_buf) )
    input_size = 0;
  return 0;
}

variable_types: key int

variable_types: same int

variable_types: nearly int


original_code:
void BEEP88UpdateOne(int num, INT16 *buffer, int length)
{
  int i, data;
  unsigned short *buf16 = (unsigned short *)buffer;
  {
    for( i=0; i < length ; i++ ){
      if( BEEP[num].port40 & 0x20 ){
 BEEP[num].cnt ++;
 if ( BEEP[num].cnt < BEEP[num].cnt_of_clock/2 ) data = 0x2000;
 else if( BEEP[num].cnt < BEEP[num].cnt_of_clock ) data = 0x0000;
 else{ BEEP[num].cnt -= BEEP[num].cnt_of_clock; data = 0x0000; }
      }else{
 data = 0x0000;
      }
      if( BEEP[num].port40 & 0x80 ) data = 0x2000;
      *buf16 ++ = data;
    }
  }
}

canonical_original_code:
void BEEP88UpdateOne(int num, short *buffer, int length)
{
  int i; int data;
  unsigned short *buf16 = (unsigned short *)buffer;
  {
    for( i=0; i < length ; i++ ){
      if( BEEP[num].port40 & 0x20 ){
 BEEP[num].cnt ++;
 if ( BEEP[num].cnt < BEEP[num].cnt_of_clock/2 ) data = 0x2000;
 else if( BEEP[num].cnt < BEEP[num].cnt_of_clock ) data = 0x0000;
 else{ BEEP[num].cnt -= BEEP[num].cnt_of_clock; data = 0x0000; }
      }else{
 data = 0x0000;
      }
      if( BEEP[num].port40 & 0x80 ) data = 0x2000;
      *buf16 ++ = data;
    }
  }
}

variable_types: num int

variable_types:
buffer
short *

variable_types: length int

variable_types: data int


decompiled_code:
__int64 __fastcall BEEP88Init(int a1, int a2, int a3)
{
__int64 result; // rax

if ( a1 > 1 )
return 0xFFFFFFFFLL;
memset(BEEP, 0, 28LL * a1);
result = 0LL;
if ( a1 == 1 )
{
BEEP[0] = 0;
dword_184 = a2;
dword_188 = a3;
dword_18C = 0;
dword_194 = a3 / a2;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, int a3)
{
__int64 result; // rax

if ( a1 > 1 )
return 0xFFFFFFFFLL;
memset(BEEP, 0, 28LL * a1);
result = 0LL;
if ( a1 == 1 )
{
BEEP[0] = 0;
dword_184 = a2;
dword_188 = a3;
dword_18C = 0;
dword_194 = a3 / a2;
result = 0LL;
}
return result;
}

original_code:
int BEEP88Init(int num, int clock, int sample_rate )
{
  int i;
  if (num>1) return (-1);
  memset(BEEP,0,sizeof(Beep) * num);
  for ( i = 0 ; i < num; i++ ) {
    BEEP[i].status = i;
    BEEP[i].clock = clock;
    BEEP[i].sample_rate = sample_rate;
    BEEP[i].sample_bit_8 = (0);
    BEEP[i].cnt_of_clock = sample_rate / clock;
  }
  return(0);
}

canonical_original_code:
int BEEP88Init(int num, int clock, int sample_rate)
{
  int i;
  if (num>1) return (-1);
  memset(BEEP,0,sizeof(Beep) * num);
  for ( i = 0 ; i < num; i++ ) {
    BEEP[i].status = i;
    BEEP[i].clock = clock;
    BEEP[i].sample_rate = sample_rate;
    BEEP[i].sample_bit_8 = (0);
    BEEP[i].cnt_of_clock = sample_rate / clock;
  }
  return(0);
}

variable_types: clock int

variable_types: i int


decompiled_code:
void BEEP88Shutdown()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void BEEP88Shutdown(void)
{
}

canonical_original_code:
void BEEP88Shutdown()
{
}


decompiled_code:
__int64 __fastcall BEEP88UpdateRequest(int a1)
{
return stream_update((unsigned int)stream[a1], 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return stream_update((unsigned int)stream[a1], 0LL);
}

original_code:
void BEEP88UpdateRequest( int chip )
{
  stream_update( stream[chip], 0 );
}

canonical_original_code:
void BEEP88UpdateRequest(int chip)
{
  stream_update( stream[chip], 0 );
}

variable_types: chip int


decompiled_code:
_BOOL8 __fastcall BEEP88_sh_start(__int64 a1)
{
unsigned int *v2; // rax
__int64 v3; // rdi
__int64 v4; // rbx
const char *v5; // rax
char v7[56]; // [rsp+0h] [rbp-38h] BYREF

v2 = *(unsigned int **)(a1 + 8);
intf = (__int64)v2;
v3 = *v2;
if ( (int)v3 > 0 )
{
v4 = 0LL;
do
{
v5 = (const char *)sound_name(a1);
sprintf(v7, "%s #%d", v5, (unsigned int)v4);
stream[v4] = stream_init(
v7,
*(unsigned __int16 *)(intf + 4LL * (int)v4 + 8),
sample_rate,
(unsigned int)v4,
&BEEP88UpdateOne);
v2 = (unsigned int *)intf;
v3 = *(unsigned int *)intf;
++v4;
}
while ( (int)v3 > (int)v4 );
}
return (unsigned int)BEEP88Init(v3, v2[1], sample_rate) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1)
{
unsigned int *v2; // rax
__int64 v3; // rdi
__int64 v4; // rbx
const char *v5; // rax
char v7[56]; // [rsp+0h] [rbp-38h] BYREF

v2 = *(unsigned int **)(a1 + 8);
intf = (__int64)v2;
v3 = *v2;
if ( (int)v3 > 0 )
{
v4 = 0LL;
do
{
v5 = (const char *)sound_name(a1);
sprintf(v7, "%s #%d", v5, (unsigned int)v4);
stream[v4] = stream_init(
v7,
*(unsigned __int16 *)(intf + 4LL * (int)v4 + 8),
sample_rate,
(unsigned int)v4,
&BEEP88UpdateOne);
v2 = (unsigned int *)intf;
v3 = *(unsigned int *)intf;
++v4;
}
while ( (int)v3 > (int)v4 );
}
return (unsigned int)BEEP88Init(v3, v2[1], sample_rate) != 0;
}

original_code:
int BEEP88_sh_start(const struct MachineSound *msound)
{
  int i;
  intf = msound->sound_interface;
  for (i = 0;i < intf->num;i++)
  {
    int volume;
    char name[20];
    sprintf(name,"%s #%d",sound_name(msound),i);
    volume = intf->mixing_level[i]&0xffff;
    stream[i] = stream_init(name,volume,sample_rate,i,BEEP88UpdateOne);
  }
  if (BEEP88Init(intf->num,intf->baseclock,sample_rate) == 0)
  {
    return 0;
  }
  return 1;
}

canonical_original_code:
int BEEP88_sh_start(struct MachineSound *msound)
{
  int i;
  intf = msound->sound_interface;
  for (i = 0;i < intf->num;i++)
  {
    int volume;
    char name[20];
    sprintf(name,"%s #%d",sound_name(msound),i);
    volume = intf->mixing_level[i]&0xffff;
    stream[i] = stream_init(name,volume,sample_rate,i,BEEP88UpdateOne);
  }
  if (BEEP88Init(intf->num,intf->baseclock,sample_rate) == 0)
  {
    return 0;
  }
  return 1;
}

variable_types: struct MachineSound { int sound_type; void *sound_interface; char *tag; } *

variable_types: volume int

variable_types:
name
char [20]


decompiled_code:
__int64 BEEP88_sh_stop()
{
return BEEP88Shutdown();
}

canonical_decompiled_code:
__int64 func2()
{
return BEEP88Shutdown();
}

original_code:
void BEEP88_sh_stop(void)
{
  BEEP88Shutdown();
}

canonical_original_code:
void BEEP88_sh_stop()
{
  BEEP88Shutdown();
}


decompiled_code:
__int64 BEEP88_sh_reset()
{
__int64 result; // rax
unsigned int v1; // ebx

result = intf;
if ( *(int *)intf > 0 )
{
v1 = 0;
do
{
BEEP88ResetChip(v1++);
result = intf;
}
while ( *(_DWORD *)intf > (signed int)v1 );
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
unsigned int v1; // ebx

result = intf;
if ( *(int *)intf > 0 )
{
v1 = 0;
do
{
BEEP88ResetChip(v1++);
result = intf;
}
while ( *(_DWORD *)intf > (signed int)v1 );
}
return result;
}

original_code:
void BEEP88_sh_reset(void)
{
  int i;
  for (i = 0;i < intf->num;i++)
    BEEP88ResetChip(i);
}

canonical_original_code:
void BEEP88_sh_reset()
{
  int i;
  for (i = 0;i < intf->num;i++)
    BEEP88ResetChip(i);
}


decompiled_code:
__int64 __fastcall BEEP88_write_port_0_w(__int64 a1, unsigned __int8 a2)
{
return BEEP88Write(0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int8 a2)
{
return BEEP88Write(0LL, a2);
}

original_code:
void BEEP88_write_port_0_w(__attribute__((__unused__)) offs_t offset, __attribute__((__unused__)) data8_t data)
{
  BEEP88Write(0,data);
}

canonical_original_code:
void BEEP88_write_port_0_w(unsigned int offset, unsigned char data)
{
  BEEP88Write(0,data);
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned char


decompiled_code:
__int64 __fastcall make_snapshot(__int64 a1, __int64 a2, __int64 a3)
{
void (**v3)(void); // rcx
int v4; // edx
int v5; // eax
__int64 result; // rax

v3 = (void (**)(void))&snapshot_list_normal;
if ( use_interlace )
{
v3 = (void (**)(void))&snapshot_list_itlace;
if ( use_interlace <= 0 )
{
snapshot_clear(a1, a2, a3, &snapshot_list_itlace);
v3 = (void (**)(void))&snapshot_list_skipln;
}
}
if ( (sys_ctrl & 1) != 0 )
v4 = CRTC_SZ_LINES != 25;
else
v4 = (CRTC_SZ_LINES != 25) + 2;
v5 = 2;
if ( (grph_ctrl & 8) != 0 )
{
v5 = 0;
if ( (grph_ctrl & 0x10) == 0 )
v5 = (grph_ctrl & 1) == 0 ? 3 : 1;
}
(*(&v3[8 * (__int64)v5 + 1] + 2 * v4))();
result = setup_palette(&pal);
byte_3F800 = 0;
byte_3F801 = 0;
byte_3F802 = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
void (**v3)(void); // rcx
int v4; // edx
int v5; // eax
__int64 result; // rax

v3 = (void (**)(void))&snapshot_list_normal;
if ( use_interlace )
{
v3 = (void (**)(void))&snapshot_list_itlace;
if ( use_interlace <= 0 )
{
snapshot_clear(a1, a2, a3, &snapshot_list_itlace);
v3 = (void (**)(void))&snapshot_list_skipln;
}
}
if ( (sys_ctrl & 1) != 0 )
v4 = CRTC_SZ_LINES != 25;
else
v4 = (CRTC_SZ_LINES != 25) + 2;
v5 = 2;
if ( (grph_ctrl & 8) != 0 )
{
v5 = 0;
if ( (grph_ctrl & 0x10) == 0 )
v5 = (grph_ctrl & 1) == 0 ? 3 : 1;
}
(*(&v3[8 * (__int64)v5 + 1] + 2 * v4))();
result = setup_palette(&pal);
byte_3F800 = 0;
byte_3F801 = 0;
byte_3F802 = 0;
return result;
}

original_code:
static void make_snapshot( void )
{
  int vram_mode, text_mode;
  SNAPSHOT_FUNC (*list)[4][2];
  if ( use_interlace == 0 ){ list = snapshot_list_normal; }
  else if( use_interlace > 0 ){ list = snapshot_list_itlace; }
  else { snapshot_clear();
     list = snapshot_list_skipln; }
  if( sys_ctrl & (0x01) ){
    if( CRTC_SZ_LINES == 25 ){ text_mode = V_80x25; }
    else { text_mode = V_80x20; }
  }else{
    if( CRTC_SZ_LINES == 25 ){ text_mode = V_40x25; }
    else { text_mode = V_40x20; }
  }
  if( grph_ctrl & (0x08) ){
    if( grph_ctrl & (0x10) ){
        vram_mode = V_COLOR;
    }else{
      if( grph_ctrl & (0x01) ){
 vram_mode = V_MONO;
      }else{
 vram_mode = V_HIRESO;
      }
    }
  }else{
        vram_mode = V_UNDISP;
  }
  (list[ vram_mode ][ text_mode ][ V_ALL ])();
  setup_palette( pal );
  pal[16].red = 0;
  pal[16].green = 0;
  pal[16].blue = 0;
}

canonical_original_code:
static void make_snapshot()
{
  int vram_mode; int text_mode;
  SNAPSHOT_FUNC (*list)[4][2];
  if ( use_interlace == 0 ){ list = snapshot_list_normal; }
  else if( use_interlace > 0 ){ list = snapshot_list_itlace; }
  else { snapshot_clear();
     list = snapshot_list_skipln; }
  if( sys_ctrl & (0x01) ){
    if( CRTC_SZ_LINES == 25 ){ text_mode = V_80x25; }
    else { text_mode = V_80x20; }
  }else{
    if( CRTC_SZ_LINES == 25 ){ text_mode = V_40x25; }
    else { text_mode = V_40x20; }
  }
  if( grph_ctrl & (0x08) ){
    if( grph_ctrl & (0x10) ){
        vram_mode = V_COLOR;
    }else{
      if( grph_ctrl & (0x01) ){
 vram_mode = V_MONO;
      }else{
 vram_mode = V_HIRESO;
      }
    }
  }else{
        vram_mode = V_UNDISP;
  }
  (list[ vram_mode ][ text_mode ][ V_ALL ])();
  setup_palette( pal );
  pal[16].red = 0;
  pal[16].green = 0;
  pal[16].blue = 0;
}


decompiled_code:
const char *screen_snapshot_init()
{
const char *result; // rax
const char *v1; // rbx

if ( !file_snap )
set_snapshot_filename(1LL);
memset(&snapshot_cmd, 0, 0x400uLL);
result = getenv("QUASI88_SSS_CMD");
v1 = result;
if ( result )
{
result = (const char *)strlen(result);
if ( (unsigned __int64)result <= 0x3FF )
result = strcpy(&snapshot_cmd, v1);
}
snapshot_cmd_do = 0;
return result;
}

canonical_decompiled_code:
const char *func4()
{
const char *result; // rax
const char *v1; // rbx

if ( !file_snap )
set_snapshot_filename(1LL);
memset(&snapshot_cmd, 0, 0x400uLL);
result = getenv("QUASI88_SSS_CMD");
v1 = result;
if ( result )
{
result = (const char *)strlen(result);
if ( (unsigned __int64)result <= 0x3FF )
result = strcpy(&snapshot_cmd, v1);
}
snapshot_cmd_do = 0;
return result;
}

original_code:
void screen_snapshot_init( void )
{
  const char *s;
  if( file_snap[0] == '\0' ){
    set_snapshot_filename( (1) );
  }
  memset( snapshot_cmd, 0, (1024) );
  s = getenv( "QUASI88_SSS_CMD" );
  if( s && (strlen(s) < (1024)) ){
    strcpy( snapshot_cmd, s );
  }
  snapshot_cmd_do = (0);
}

canonical_original_code:
void screen_snapshot_init()
{
  char *s;
  if( file_snap[0] == '\0' ){
    set_snapshot_filename( (1) );
  }
  memset( snapshot_cmd, 0, (1024) );
  s = getenv( "QUASI88_SSS_CMD" );
  if( s && (strlen(s) < (1024)) ){
    strcpy( snapshot_cmd, s );
  }
  snapshot_cmd_do = (0);
}

variable_types:
s
char *


decompiled_code:
int __fastcall oss_mixer_get(__int64 a1, int a2, _DWORD *a3, _DWORD *a4)
{
int result; // eax
unsigned __int8 v7; // dh
int v8; // [rsp+Ch] [rbp-1Ch] BYREF

result = ioctl(**(_DWORD **)(a1 + 184), oss_mixer_sysdep_to_oss[a2] | 0x80044D00, &v8);
if ( result )
{
perror("error: SOUND_MIXER_READ");
result = -1;
}
else
{
v7 = BYTE1(v8);
*a3 = (unsigned __int8)v8;
*a4 = v7;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2, _DWORD *a3, _DWORD *a4)
{
int result; // eax
unsigned __int8 v7; // dh
int v8; // [rsp+Ch] [rbp-1Ch] BYREF

result = ioctl(**(_DWORD **)(a1 + 184), oss_mixer_sysdep_to_oss[a2] | 0x80044D00, &v8);
if ( result )
{
perror("error: SOUND_MIXER_READ");
result = -1;
}
else
{
v7 = BYTE1(v8);
*a3 = (unsigned __int8)v8;
*a4 = v7;
}
return result;
}

original_code:
static int oss_mixer_get(struct sysdep_mixer_struct *mixer,
   int channel, int *left, int *right)
{
   int value;
   struct oss_mixer_priv_data *priv = mixer->_priv;
   if(ioctl(priv->fd, (((2U) << (((0 +8)+8)+14)) | ((('M')) << (0 +8)) | (((oss_mixer_sysdep_to_oss[channel])) << 0) | ((((sizeof(int)))) << ((0 +8)+8))),
      &value))
   {
      perror("error: SOUND_MIXER_READ");
      return -1;
   }
   *left = value & 0xFF;
   *right = (value >> 8) & 0xFF;
   return 0;
}

variable_types: struct sysdep_mixer_struct { int flags; char channel_available[10]; char restore_channel[10]; int cache_left[10]; int cache_right[10]; int orig_left[10]; int orig_right[10]; void *_priv; int (*set)(struct sysdep_mixer_struct *mixer, int channel, int left, int right); int (*get)(struct sysdep_mixer_struct *mixer, int channel, int *left, int *right); void (*destroy)(struct sysdep_mixer_struct *mixer); } *

variable_types: channel int

variable_types:
left
int *

variable_types:
right
int *

variable_types: value int

variable_types: struct oss_mixer_priv_data { int fd; } *


decompiled_code:
int __fastcall oss_mixer_set(__int64 a1, int a2, int a3, int a4)
{
int result; // eax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3 | (a4 << 8);
result = ioctl(**(_DWORD **)(a1 + 184), oss_mixer_sysdep_to_oss[a2] | 0xC0044D00, v5);
if ( result )
{
perror("error: SOUND_MIXER_WRITE");
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, int a2, int a3, int a4)
{
int result; // eax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3 | (a4 << 8);
result = ioctl(**(_DWORD **)(a1 + 184), oss_mixer_sysdep_to_oss[a2] | 0xC0044D00, v5);
if ( result )
{
perror("error: SOUND_MIXER_WRITE");
result = -1;
}
return result;
}

original_code:
static int oss_mixer_set(struct sysdep_mixer_struct *mixer,
   int channel, int left, int right)
{
   int i = left | right << 8;
   struct oss_mixer_priv_data *priv = mixer->_priv;
   if(ioctl(priv->fd, (((2U|1U) << (((0 +8)+8)+14)) | ((('M')) << (0 +8)) | (((oss_mixer_sysdep_to_oss[channel])) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &i))
   {
      perror("error: SOUND_MIXER_WRITE");
      return -1;
   }
   return 0;
}

variable_types: left int

variable_types: right int

user_defined_types: struct sysdep_mixer_struct { int flags; char channel_available[10]; char restore_channel[10]; int cache_left[10]; int cache_right[10]; int orig_left[10]; int orig_right[10]; void *_priv; int (*set)(struct sysdep_mixer_struct *mixer, int channel, int left, int right); int (*get)(struct sysdep_mixer_struct *mixer, int channel, int *left, int *right); void (*destroy)(struct sysdep_mixer_struct *mixer); }


decompiled_code:
void __fastcall oss_mixer_destroy(void *ptr)
{
int *v1; // rbp

v1 = (int *)*((_QWORD *)ptr + 23);
if ( v1 )
{
if ( *v1 >= 0 )
close(*v1);
free(v1);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
int *v1; // rbp

v1 = (int *)*((_QWORD *)ptr + 23);
if ( v1 )
{
if ( *v1 >= 0 )
close(*v1);
free(v1);
}
free(ptr);
}

original_code:
static void oss_mixer_destroy(struct sysdep_mixer_struct *mixer)
{
   struct oss_mixer_priv_data *priv = mixer->_priv;
   if(priv)
   {
      if(priv->fd >= 0)
         close(priv->fd);
      free(priv);
   }
   free(mixer);
}


decompiled_code:
__int64 __fastcall crtc_out_command(unsigned __int8 a1)
{
__int64 result; // rax
int v2; // edi

crtc_command = a1 >> 5;
crtc_param_num = 0;
result = a1 >> 5;
switch ( (char)result )
{
case 0:
crtc_status &= 0xE9u;
crtc_active = 0;
text_display = 0;
screen_update_force = 1;
break;
case 1:
crtc_reverse_display = a1 & 1;
result = crtc_status & 0xF7 | 0x10u;
crtc_status = crtc_status & 0xF7 | 0x10;
crtc_active = 1;
if ( (dmac_mode & 4) != 0 && crtc_intr_mask == 3 )
{
if ( (grph_pile & 1) != 0 )
{
result = (grph_ctrl & 0x10) == 0;
text_display = (grph_ctrl & 0x10) == 0;
}
else
{
text_display = 2;
}
}
else
{
text_display = 0;
}
screen_update_palette = 1;
screen_update_force = 1;
break;
case 2:
v2 = a1 & 3;
crtc_intr_mask = v2;
if ( (dmac_mode & 4) != 0 && crtc_active && v2 == 3 )
{
if ( (grph_pile & 1) != 0 )
{
result = (grph_ctrl & 0x10) == 0;
text_display = (grph_ctrl & 0x10) == 0;
}
else
{
text_display = 2;
}
}
else
{
text_display = 0;
}
screen_update_force = 1;
break;
case 3:
crtc_status &= 0xFEu;
break;
case 4:
crtc_load_cursor_position = a1 & 1;
crtc_cursor = -1;
*(&crtc_cursor + 1) = -1;
break;
case 5:
case 6:
crtc_status &= 0xF9u;
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
__int64 result; // rax
int v2; // edi

crtc_command = a1 >> 5;
crtc_param_num = 0;
result = a1 >> 5;
switch ( (char)result )
{
case 0:
crtc_status &= 0xE9u;
crtc_active = 0;
text_display = 0;
screen_update_force = 1;
break;
case 1:
crtc_reverse_display = a1 & 1;
result = crtc_status & 0xF7 | 0x10u;
crtc_status = crtc_status & 0xF7 | 0x10;
crtc_active = 1;
if ( (dmac_mode & 4) != 0 && crtc_intr_mask == 3 )
{
if ( (grph_pile & 1) != 0 )
{
result = (grph_ctrl & 0x10) == 0;
text_display = (grph_ctrl & 0x10) == 0;
}
else
{
text_display = 2;
}
}
else
{
text_display = 0;
}
screen_update_palette = 1;
screen_update_force = 1;
break;
case 2:
v2 = a1 & 3;
crtc_intr_mask = v2;
if ( (dmac_mode & 4) != 0 && crtc_active && v2 == 3 )
{
if ( (grph_pile & 1) != 0 )
{
result = (grph_ctrl & 0x10) == 0;
text_display = (grph_ctrl & 0x10) == 0;
}
else
{
text_display = 2;
}
}
else
{
text_display = 0;
}
screen_update_force = 1;
break;
case 3:
crtc_status &= 0xFEu;
break;
case 4:
crtc_load_cursor_position = a1 & 1;
crtc_cursor = -1;
*(&crtc_cursor + 1) = -1;
break;
case 5:
case 6:
crtc_status &= 0xF9u;
break;
default:
return result;
}
return result;
}

original_code:
void crtc_out_command( byte data )
{
  crtc_command = data >> 5;
  crtc_param_num = 0;
  switch( crtc_command ){
  case CRTC_RESET:
    crtc_status &= ~( (0x10) | (0x04) | (0x02) );
    crtc_active = (0);
    do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
    screen_update_force = (1);
    break;
  case CRTC_START_DISPLAY:
    crtc_reverse_display = data & 0x01;
    crtc_status |= (0x10);
    crtc_status &= ~( (0x08) );
    crtc_active = (1);
    do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
    do{ screen_update_palette = (1); screen_update_force = (1); }while(0);
    break;
  case CRTC_SET_INTERRUPT_MASK:
    crtc_intr_mask = data & 0x03;
    do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
    screen_update_force = (1);
    break;
  case CRTC_READ_LIGHT_PEN:
    crtc_status &= ~( (0x01) );
    break;
  case CRTC_LOAD_CURSOR_POSITION:
    crtc_load_cursor_position = data & 0x01;
    crtc_cursor[ 0 ] = -1;
    crtc_cursor[ 1 ] = -1;
    break;
  case CRTC_RESET_INTERRUPT:
  case CRTC_RESET_COUNTERS:
    crtc_status &= ~( (0x04) | (0x02) );
    break;
  }
}

canonical_original_code:
void crtc_out_command(unsigned char data)
{
  crtc_command = data >> 5;
  crtc_param_num = 0;
  switch( crtc_command ){
  case CRTC_RESET:
    crtc_status &= ~( (0x10) | (0x04) | (0x02) );
    crtc_active = (0);
    do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
    screen_update_force = (1);
    break;
  case CRTC_START_DISPLAY:
    crtc_reverse_display = data & 0x01;
    crtc_status |= (0x10);
    crtc_status &= ~( (0x08) );
    crtc_active = (1);
    do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
    do{ screen_update_palette = (1); screen_update_force = (1); }while(0);
    break;
  case CRTC_SET_INTERRUPT_MASK:
    crtc_intr_mask = data & 0x03;
    do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
    screen_update_force = (1);
    break;
  case CRTC_READ_LIGHT_PEN:
    crtc_status &= ~( (0x01) );
    break;
  case CRTC_LOAD_CURSOR_POSITION:
    crtc_load_cursor_position = data & 0x01;
    crtc_cursor[ 0 ] = -1;
    crtc_cursor[ 1 ] = -1;
    break;
  case CRTC_RESET_INTERRUPT:
  case CRTC_RESET_COUNTERS:
    crtc_status &= ~( (0x04) | (0x02) );
    break;
  }
}


decompiled_code:
__int64 __fastcall crtc_out_parameter(unsigned __int8 a1)
{
__int64 result; // rax
int v2; // eax
char v3; // al
char v4; // cl
unsigned __int8 v5; // dl
int v6; // eax
unsigned int v7; // eax
int v8; // eax
int v9; // eax

result = (unsigned int)crtc_command;
if ( crtc_command )
{
if ( crtc_command == 4 )
{
result = (unsigned int)crtc_param_num;
if ( crtc_param_num <= 1 )
{
++crtc_param_num;
result = (int)result;
if ( crtc_load_cursor_position )
crtc_cursor[(int)result] = a1;
else
crtc_cursor[(int)result] = -1;
}
}
}
else
{
v2 = crtc_param_num;
if ( crtc_param_num <= 4 )
{
++crtc_param_num;
crtc_format[v2] = a1;
}
v3 = crtc_format[2];
crtc_skip_line = v3 & 0x80;
v4 = crtc_format[4];
crtc_attr_non_separate = v4 & 0x80;
crtc_attr_color = v4 & 0x40;
crtc_attr_non_special = v4 & 0x20;
crtc_cursor_style = (v3 & 0x40) == 0 ? 8 : 1;
crtc_cursor_blink = v3 & 0x20;
v5 = crtc_format[1];
blink_cycle = 8 * (unsigned __int8)((v5 >> 6) + 1);
v6 = (v5 & 0x3F) + 1;
if ( v6 > 20 )
crtc_sz_lines = (v6 > 24) + 24;
else
crtc_sz_lines = 20;
v7 = (unsigned __int8)((crtc_format[0] & 0x7F) + 2);
if ( v7 > 0x50 )
v7 = 80;
crtc_sz_columns = v7;
if ( (v4 & 0x20) != 0 )
{
crtc_sz_attrs = 0;
}
else
{
v9 = (v4 & 0x1F) + 1;
if ( v9 > 20 )
v9 = 20;
crtc_sz_attrs = v9;
}
crtc_byte_per_line = crtc_sz_columns + 2 * crtc_sz_attrs;
if ( crtc_sz_lines > 20 )
{
crtc_font_height = 8;
v8 = 25;
}
else
{
crtc_font_height = 10;
v8 = 20;
}
CRTC_SZ_LINES = v8;
result = blink_ctrl_update();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1)
{
__int64 result; // rax
int v2; // eax
char v3; // al
char v4; // cl
unsigned __int8 v5; // dl
int v6; // eax
unsigned int v7; // eax
int v8; // eax
int v9; // eax

result = (unsigned int)crtc_command;
if ( crtc_command )
{
if ( crtc_command == 4 )
{
result = (unsigned int)crtc_param_num;
if ( crtc_param_num <= 1 )
{
++crtc_param_num;
result = (int)result;
if ( crtc_load_cursor_position )
crtc_cursor[(int)result] = a1;
else
crtc_cursor[(int)result] = -1;
}
}
}
else
{
v2 = crtc_param_num;
if ( crtc_param_num <= 4 )
{
++crtc_param_num;
crtc_format[v2] = a1;
}
v3 = crtc_format[2];
crtc_skip_line = v3 & 0x80;
v4 = crtc_format[4];
crtc_attr_non_separate = v4 & 0x80;
crtc_attr_color = v4 & 0x40;
crtc_attr_non_special = v4 & 0x20;
crtc_cursor_style = (v3 & 0x40) == 0 ? 8 : 1;
crtc_cursor_blink = v3 & 0x20;
v5 = crtc_format[1];
blink_cycle = 8 * (unsigned __int8)((v5 >> 6) + 1);
v6 = (v5 & 0x3F) + 1;
if ( v6 > 20 )
crtc_sz_lines = (v6 > 24) + 24;
else
crtc_sz_lines = 20;
v7 = (unsigned __int8)((crtc_format[0] & 0x7F) + 2);
if ( v7 > 0x50 )
v7 = 80;
crtc_sz_columns = v7;
if ( (v4 & 0x20) != 0 )
{
crtc_sz_attrs = 0;
}
else
{
v9 = (v4 & 0x1F) + 1;
if ( v9 > 20 )
v9 = 20;
crtc_sz_attrs = v9;
}
crtc_byte_per_line = crtc_sz_columns + 2 * crtc_sz_attrs;
if ( crtc_sz_lines > 20 )
{
crtc_font_height = 8;
v8 = 25;
}
else
{
crtc_font_height = 10;
v8 = 20;
}
CRTC_SZ_LINES = v8;
result = blink_ctrl_update();
}
return result;
}

original_code:
void crtc_out_parameter( byte data )
{
  switch( crtc_command ){
  case CRTC_RESET:
    if( crtc_param_num < 5 ){
      crtc_format[ crtc_param_num++ ] = data;
    }
    crtc_skip_line = crtc_format[2] & 0x80;
    crtc_attr_non_separate = crtc_format[4] & 0x80;
    crtc_attr_color = crtc_format[4] & 0x40;
    crtc_attr_non_special = crtc_format[4] & 0x20;
    crtc_cursor_style =(crtc_format[2] & 0x40) ?0x01 :0x08;
    crtc_cursor_blink = crtc_format[2] & 0x20;
    blink_cycle =(crtc_format[1]>>6) * 8 +8;
    crtc_sz_lines =(crtc_format[1] & 0x3f) +1;
    if ( crtc_sz_lines <= 20 ) crtc_sz_lines = 20;
    else if( crtc_sz_lines >= 25 ) crtc_sz_lines = 25;
    else crtc_sz_lines = 24;
    crtc_sz_columns =(crtc_format[0] & 0x7f) +2;
    if( crtc_sz_columns > 80 ) crtc_sz_columns = 80;
    crtc_sz_attrs =(crtc_format[4] & 0x1f) +1;
    if ( crtc_attr_non_special ) crtc_sz_attrs = 0;
    else if( crtc_sz_attrs > 20 ) crtc_sz_attrs = 20;
    crtc_byte_per_line = crtc_sz_columns + crtc_sz_attrs * 2;
    crtc_font_height = (crtc_sz_lines>20) ? 8 : 10;
    CRTC_SZ_LINES = (crtc_sz_lines>20) ? 25 : 20;
    blink_ctrl_update();
    break;
  case CRTC_LOAD_CURSOR_POSITION:
    if( crtc_param_num < 2 ){
      if( crtc_load_cursor_position ){
 crtc_cursor[ crtc_param_num++ ] = data;
      }else{
 crtc_cursor[ crtc_param_num++ ] = -1;
      }
    }
    break;
  }
}

canonical_original_code:
void crtc_out_parameter(unsigned char data)
{
  switch( crtc_command ){
  case CRTC_RESET:
    if( crtc_param_num < 5 ){
      crtc_format[ crtc_param_num++ ] = data;
    }
    crtc_skip_line = crtc_format[2] & 0x80;
    crtc_attr_non_separate = crtc_format[4] & 0x80;
    crtc_attr_color = crtc_format[4] & 0x40;
    crtc_attr_non_special = crtc_format[4] & 0x20;
    crtc_cursor_style =(crtc_format[2] & 0x40) ?0x01 :0x08;
    crtc_cursor_blink = crtc_format[2] & 0x20;
    blink_cycle =(crtc_format[1]>>6) * 8 +8;
    crtc_sz_lines =(crtc_format[1] & 0x3f) +1;
    if ( crtc_sz_lines <= 20 ) crtc_sz_lines = 20;
    else if( crtc_sz_lines >= 25 ) crtc_sz_lines = 25;
    else crtc_sz_lines = 24;
    crtc_sz_columns =(crtc_format[0] & 0x7f) +2;
    if( crtc_sz_columns > 80 ) crtc_sz_columns = 80;
    crtc_sz_attrs =(crtc_format[4] & 0x1f) +1;
    if ( crtc_attr_non_special ) crtc_sz_attrs = 0;
    else if( crtc_sz_attrs > 20 ) crtc_sz_attrs = 20;
    crtc_byte_per_line = crtc_sz_columns + crtc_sz_attrs * 2;
    crtc_font_height = (crtc_sz_lines>20) ? 8 : 10;
    CRTC_SZ_LINES = (crtc_sz_lines>20) ? 25 : 20;
    blink_ctrl_update();
    break;
  case CRTC_LOAD_CURSOR_POSITION:
    if( crtc_param_num < 2 ){
      if( crtc_load_cursor_position ){
 crtc_cursor[ crtc_param_num++ ] = data;
      }else{
 crtc_cursor[ crtc_param_num++ ] = -1;
      }
    }
    break;
  }
}


decompiled_code:
__int64 crtc_init()
{
crtc_out_command(0);
crtc_out_parameter(0xCEu);
crtc_out_parameter(0x98u);
crtc_out_parameter(0x6Fu);
crtc_out_parameter(0x58u);
crtc_out_parameter(0x53u);
crtc_out_command(0x80u);
crtc_out_parameter(0);
return crtc_out_parameter(0);
}

canonical_decompiled_code:
__int64 func2()
{
func0(0);
func1(0xCEu);
func1(0x98u);
func1(0x6Fu);
func1(0x58u);
func1(0x53u);
func0(0x80u);
func1(0);
return func1(0);
}

original_code:
void crtc_init( void )
{
  crtc_out_command( CRTC_RESET << 5 );
  crtc_out_parameter( 0xce );
  crtc_out_parameter( 0x98 );
  crtc_out_parameter( 0x6f );
  crtc_out_parameter( 0x58 );
  crtc_out_parameter( 0x53 );
  crtc_out_command( CRTC_LOAD_CURSOR_POSITION << 5 );
  crtc_out_parameter( 0 );
  crtc_out_parameter( 0 );
}

canonical_original_code:
void crtc_init()
{
  crtc_out_command( CRTC_RESET << 5 );
  crtc_out_parameter( 0xce );
  crtc_out_parameter( 0x98 );
  crtc_out_parameter( 0x6f );
  crtc_out_parameter( 0x58 );
  crtc_out_parameter( 0x53 );
  crtc_out_command( CRTC_LOAD_CURSOR_POSITION << 5 );
  crtc_out_parameter( 0 );
  crtc_out_parameter( 0 );
}


decompiled_code:
__int64 crtc_in_status()
{
return (unsigned __int8)crtc_status;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned __int8)crtc_status;
}

original_code:
byte crtc_in_status( void )
{
  return crtc_status;
}

canonical_original_code:
unsigned char crtc_in_status()
{
  return crtc_status;
}


decompiled_code:
__int64 crtc_in_parameter()
{
__int64 result; // rax
int v1; // edx

result = 0xFFFFFFFFLL;
if ( crtc_command == 3 )
{
v1 = crtc_param_num;
if ( crtc_param_num <= 1 )
{
++crtc_param_num;
result = (unsigned __int8)crtc_light_pen[v1];
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
int v1; // edx

result = 0xFFFFFFFFLL;
if ( crtc_command == 3 )
{
v1 = crtc_param_num;
if ( crtc_param_num <= 1 )
{
++crtc_param_num;
result = (unsigned __int8)crtc_light_pen[v1];
}
}
return result;
}

original_code:
byte crtc_in_parameter( void )
{
  byte data = 0xff;
  switch( crtc_command ){
  case CRTC_READ_LIGHT_PEN:
    if( crtc_param_num < 2 ){
      data = crtc_light_pen[ crtc_param_num++ ];
    }
    return data;
  }
  return 0xff;
}

canonical_original_code:
unsigned char crtc_in_parameter()
{
  unsigned char data = 0xff;
  switch( crtc_command ){
  case CRTC_READ_LIGHT_PEN:
    if( crtc_param_num < 2 ){
      data = crtc_light_pen[ crtc_param_num++ ];
    }
    return data;
  }
  return 0xff;
}


decompiled_code:
void dmac_init()
{
dmac_flipflop = 0;
dmac_address = 0;
*(&dmac_address + 1) = 0;
*(&dmac_address + 2) = -3128;
*(&dmac_address + 3) = 0;
dmac_counter = 0;
*(&dmac_counter + 1) = 0;
*(&dmac_counter + 2) = 0;
*(&dmac_counter + 3) = 0;
}

canonical_decompiled_code:
void func5()
{
dmac_flipflop = 0;
dmac_address = 0;
*(&dmac_address + 1) = 0;
*(&dmac_address + 2) = -3128;
*(&dmac_address + 3) = 0;
dmac_counter = 0;
*(&dmac_counter + 1) = 0;
*(&dmac_counter + 2) = 0;
*(&dmac_counter + 3) = 0;
}

original_code:
void dmac_init( void )
{
  dmac_flipflop = 0;
  dmac_address[0].W = 0;
  dmac_address[1].W = 0;
  dmac_address[2].W = 0xf3c8;
  dmac_address[3].W = 0;
  dmac_counter[0].W = 0;
  dmac_counter[1].W = 0;
  dmac_counter[2].W = 0;
  dmac_counter[3].W = 0;
}

canonical_original_code:
void dmac_init()
{
  dmac_flipflop = 0;
  dmac_address[0].W = 0;
  dmac_address[1].W = 0;
  dmac_address[2].W = 0xf3c8;
  dmac_address[3].W = 0;
  dmac_counter[0].W = 0;
  dmac_counter[1].W = 0;
  dmac_counter[2].W = 0;
  dmac_counter[3].W = 0;
}


decompiled_code:
__int64 __fastcall dmac_out_mode(unsigned __int8 a1)
{
__int64 result; // rax

dmac_flipflop = 0;
result = a1;
dmac_mode = a1;
if ( (a1 & 4) != 0 && crtc_active && crtc_intr_mask == 3 )
{
if ( (grph_pile & 1) != 0 )
{
result = (grph_ctrl & 0x10) == 0;
text_display = (grph_ctrl & 0x10) == 0;
}
else
{
text_display = 2;
}
}
else
{
text_display = 0;
}
screen_update_force = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1)
{
__int64 result; // rax

dmac_flipflop = 0;
result = a1;
dmac_mode = a1;
if ( (a1 & 4) != 0 && crtc_active && crtc_intr_mask == 3 )
{
if ( (grph_pile & 1) != 0 )
{
result = (grph_ctrl & 0x10) == 0;
text_display = (grph_ctrl & 0x10) == 0;
}
else
{
text_display = 2;
}
}
else
{
text_display = 0;
}
screen_update_force = 1;
return result;
}

original_code:
void dmac_out_mode( byte data )
{
  dmac_flipflop = 0;
  dmac_mode = data;
  do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
  screen_update_force = (1);
}

canonical_original_code:
void dmac_out_mode(unsigned char data)
{
  dmac_flipflop = 0;
  dmac_mode = data;
  do{ if( (dmac_mode & 0x4) && (crtc_active) && crtc_intr_mask==3){ if( !(grph_pile & (0x01)) ){ text_display = TEXT_ENABLE; }else{ if( grph_ctrl & (0x10) ) text_display = TEXT_DISABLE; else text_display = TEXT_ATTR_ONLY; } }else{ text_display = TEXT_DISABLE; } }while(0);
  screen_update_force = (1);
}


decompiled_code:
__int64 dmac_in_status()
{
return 31LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 31LL;
}

original_code:
byte dmac_in_status( void )
{
  return 0x1f;
}

canonical_original_code:
unsigned char dmac_in_status()
{
  return 0x1f;
}


decompiled_code:
__int64 __fastcall dmac_out_address(unsigned __int8 a1, char a2)
{
int v2; // eax
__int64 result; // rax

v2 = dmac_flipflop;
if ( dmac_flipflop )
HIBYTE(dmac_address[a1]) = a2;
else
LOBYTE(dmac_address[a1]) = a2;
result = v2 ^ 1u;
dmac_flipflop = result;
screen_update_force = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 a1, char a2)
{
int v2; // eax
__int64 result; // rax

v2 = dmac_flipflop;
if ( dmac_flipflop )
HIBYTE(dmac_address[a1]) = a2;
else
LOBYTE(dmac_address[a1]) = a2;
result = v2 ^ 1u;
dmac_flipflop = result;
screen_update_force = 1;
return result;
}

original_code:
void dmac_out_address( byte addr, byte data )
{
  if( dmac_flipflop==0 ) dmac_address[ addr ].B.l=data;
  else dmac_address[ addr ].B.h=data;
  dmac_flipflop ^= 0x1;
  screen_update_force = (1);
}

canonical_original_code:
void dmac_out_address(unsigned char addr, unsigned char data)
{
  if( dmac_flipflop==0 ) dmac_address[ addr ].B.l=data;
  else dmac_address[ addr ].B.h=data;
  dmac_flipflop ^= 0x1;
  screen_update_force = (1);
}

variable_types:
addr
unsigned char


decompiled_code:
__int64 __fastcall dmac_out_counter(unsigned __int8 a1, char a2)
{
int v2; // eax
__int64 result; // rax

v2 = dmac_flipflop;
if ( dmac_flipflop )
HIBYTE(dmac_counter[a1]) = a2;
else
LOBYTE(dmac_counter[a1]) = a2;
result = v2 ^ 1u;
dmac_flipflop = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1, char a2)
{
int v2; // eax
__int64 result; // rax

v2 = dmac_flipflop;
if ( dmac_flipflop )
HIBYTE(dmac_counter[a1]) = a2;
else
LOBYTE(dmac_counter[a1]) = a2;
result = v2 ^ 1u;
dmac_flipflop = result;
return result;
}

original_code:
void dmac_out_counter( byte addr, byte data )
{
  if( dmac_flipflop==0 ) dmac_counter[ addr ].B.l=data;
  else dmac_counter[ addr ].B.h=data;
  dmac_flipflop ^= 0x1;
}

canonical_original_code:
void dmac_out_counter(unsigned char addr, unsigned char data)
{
  if( dmac_flipflop==0 ) dmac_counter[ addr ].B.l=data;
  else dmac_counter[ addr ].B.h=data;
  dmac_flipflop ^= 0x1;
}


decompiled_code:
__int64 __fastcall dmac_in_address(unsigned __int8 a1)
{
unsigned int v1; // edx

if ( dmac_flipflop )
v1 = HIBYTE(dmac_address[a1]);
else
v1 = LOBYTE(dmac_address[a1]);
dmac_flipflop ^= 1u;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
unsigned int v1; // edx

if ( dmac_flipflop )
v1 = HIBYTE(dmac_address[a1]);
else
v1 = LOBYTE(dmac_address[a1]);
dmac_flipflop ^= 1u;
return v1;
}

original_code:
byte dmac_in_address( byte addr )
{
  byte data;
  if( dmac_flipflop==0 ) data = dmac_address[ addr ].B.l;
  else data = dmac_address[ addr ].B.h;
  dmac_flipflop ^= 0x1;
  return data;
}

canonical_original_code:
unsigned char dmac_in_address(unsigned char addr)
{
  unsigned char data;
  if( dmac_flipflop==0 ) data = dmac_address[ addr ].B.l;
  else data = dmac_address[ addr ].B.h;
  dmac_flipflop ^= 0x1;
  return data;
}


decompiled_code:
__int64 __fastcall dmac_in_counter(unsigned __int8 a1)
{
unsigned int v1; // edx

if ( dmac_flipflop )
v1 = HIBYTE(dmac_counter[a1]);
else
v1 = LOBYTE(dmac_counter[a1]);
dmac_flipflop ^= 1u;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int8 a1)
{
unsigned int v1; // edx

if ( dmac_flipflop )
v1 = HIBYTE(dmac_counter[a1]);
else
v1 = LOBYTE(dmac_counter[a1]);
dmac_flipflop ^= 1u;
return v1;
}

original_code:
byte dmac_in_counter( byte addr )
{
  byte data;
  if( dmac_flipflop==0 ) data = dmac_counter[ addr ].B.l;
  else data = dmac_counter[ addr ].B.h;
  dmac_flipflop ^= 0x1;
  return data;
}

canonical_original_code:
unsigned char dmac_in_counter(unsigned char addr)
{
  unsigned char data;
  if( dmac_flipflop==0 ) data = dmac_counter[ addr ].B.l;
  else data = dmac_counter[ addr ].B.h;
  dmac_flipflop ^= 0x1;
  return data;
}


decompiled_code:
_BOOL8 statesave_crtcdmac()
{
return (unsigned int)statesave_table("CRTC", &suspend_crtcdmac_work) == 0;
}

canonical_decompiled_code:
_BOOL8 func12()
{
return (unsigned int)statesave_table("CRTC", &suspend_crtcdmac_work) == 0;
}

original_code:
int statesave_crtcdmac( void )
{
  if( statesave_table( "CRTC", suspend_crtcdmac_work ) == (0) ) return (1);
  else return (0);
}

canonical_original_code:
int statesave_crtcdmac()
{
  if( statesave_table( "CRTC", suspend_crtcdmac_work ) == (0) ) return (1);
  else return (0);
}


decompiled_code:
_BOOL8 stateload_crtcdmac()
{
return (unsigned int)stateload_table("CRTC", &suspend_crtcdmac_work) == 0;
}

canonical_decompiled_code:
_BOOL8 func13()
{
return (unsigned int)stateload_table("CRTC", &suspend_crtcdmac_work) == 0;
}

original_code:
int stateload_crtcdmac( void )
{
  if( stateload_table( "CRTC", suspend_crtcdmac_work ) == (0) ) return (1);
  else return (0);
}

canonical_original_code:
int stateload_crtcdmac()
{
  if( stateload_table( "CRTC", suspend_crtcdmac_work ) == (0) ) return (1);
  else return (0);
}


decompiled_code:
void sigterm_handler()
{
if ( (unsigned int)get_emu_mode() == 6 )
q8tk_event_quit();
set_emu_mode(8LL);
z80_state_intchk = 0;
z80_state_goal = 1;
z80main_cpu[9] = 0;
z80sub_cpu[9] = 0;
}

canonical_decompiled_code:
void func0()
{
if ( (unsigned int)get_emu_mode() == 6 )
q8tk_event_quit();
set_emu_mode(8LL);
z80_state_intchk = 0;
z80_state_goal = 1;
z80main_cpu[9] = 0;
z80sub_cpu[9] = 0;
}

original_code:
static void sigterm_handler( int dummy )
{
  if( get_emu_mode() == MENU ){
    q8tk_event_quit();
  }
  set_emu_mode( QUIT );
  do{ z80_state_intchk = 0; z80_state_goal = 1; }while(0);
  z80main_cpu.icount = 0;
  z80sub_cpu.icount = 0;
}

canonical_original_code:
static void sigterm_handler(int dummy)
{
  if( get_emu_mode() == MENU ){
    q8tk_event_quit();
  }
  set_emu_mode( QUIT );
  do{ z80_state_intchk = 0; z80_state_goal = 1; }while(0);
  z80main_cpu.icount = 0;
  z80sub_cpu.icount = 0;
}

variable_types: dummy int


decompiled_code:
void sigint_handler()
{
if ( (unsigned int)get_emu_mode() == 6 )
q8tk_event_quit();
if ( (unsigned int)get_emu_mode() != 5 )
{
set_emu_mode(5LL);
z80_state_intchk = 0;
z80_state_goal = 1;
}
signal(2, sigint_handler);
}

canonical_decompiled_code:
void func1()
{
if ( (unsigned int)get_emu_mode() == 6 )
q8tk_event_quit();
if ( (unsigned int)get_emu_mode() != 5 )
{
set_emu_mode(5LL);
z80_state_intchk = 0;
z80_state_goal = 1;
}
signal(2, sigint_handler);
}

original_code:
static void sigint_handler( int dummy )
{
  if( get_emu_mode() == MENU ){
    q8tk_event_quit();
  }
  if( get_emu_mode() != MONITOR ){
    set_emu_mode( MONITOR );
    do{ z80_state_intchk = 0; z80_state_goal = 1; }while(0);
  }
  signal( 2, sigint_handler );
}

canonical_original_code:
static void sigint_handler(int dummy)
{
  if( get_emu_mode() == MENU ){
    q8tk_event_quit();
  }
  if( get_emu_mode() != MONITOR ){
    set_emu_mode( MONITOR );
    do{ z80_state_intchk = 0; z80_state_goal = 1; }while(0);
  }
  signal( 2, sigint_handler );
}


decompiled_code:
__sighandler_t set_signal()
{
if ( debug_mode )
signal(2, (__sighandler_t)sigint_handler);
else
signal(2, (__sighandler_t)sigterm_handler);
return signal(15, (__sighandler_t)sigterm_handler);
}

canonical_decompiled_code:
__sighandler_t func2()
{
if ( debug_mode )
signal(2, (__sighandler_t)sigint_handler);
else
signal(2, (__sighandler_t)sigterm_handler);
return signal(15, (__sighandler_t)sigterm_handler);
}

original_code:
void set_signal( void )
{
  if( debug_mode ){
    signal( 2, sigint_handler );
    signal( 15, sigterm_handler );
  }else{
    signal( 2, sigterm_handler );
    signal( 15, sigterm_handler );
  }
}

canonical_original_code:
void set_signal()
{
  if( debug_mode ){
    signal( 2, sigint_handler );
    signal( 15, sigterm_handler );
  }else{
    signal( 2, sigterm_handler );
    signal( 15, sigterm_handler );
  }
}


decompiled_code:
int set_tick()
{
int result; // eax

result = gettimeofday(&start_tv, 0LL);
if ( result )
{
if ( verbose_wait )
result = puts("Clock Error");
tick_error = 1;
start_tv.tv_sec = 0LL;
start_tv.tv_usec = 0LL;
}
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

result = gettimeofday(&start_tv, 0LL);
if ( result )
{
if ( verbose_wait )
result = puts("Clock Error");
tick_error = 1;
start_tv.tv_sec = 0LL;
start_tv.tv_usec = 0LL;
}
return result;
}

original_code:
static __inline__ void set_tick( void )
{
  if( gettimeofday( &start_tv, 0 ) ){
    if( verbose_wait ) printf( "Clock Error\n" );
    tick_error = (1);
    start_tv.tv_sec = 0;
    start_tv.tv_usec = 0;
  }
}

canonical_original_code:
static __inline__ void set_tick()
{
  if( gettimeofday( &start_tv, 0 ) ){
    if( verbose_wait ) printf( "Clock Error\n" );
    tick_error = (1);
    start_tv.tv_sec = 0;
    start_tv.tv_usec = 0;
  }
}


decompiled_code:
__suseconds_t get_tick()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
{
if ( verbose_wait && !tick_error )
puts("Clock Error");
tick_error = 1;
v1.tv_sec = 1LL;
v1.tv_usec = 1LL;
}
return v1.tv_usec + 1000000 * (v1.tv_sec - start_tv.tv_sec) - start_tv.tv_usec;
}

canonical_decompiled_code:
__suseconds_t func1()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
{
if ( verbose_wait && !tick_error )
puts("Clock Error");
tick_error = 1;
v1.tv_sec = 1LL;
v1.tv_usec = 1LL;
}
return v1.tv_usec + 1000000 * (v1.tv_sec - start_tv.tv_sec) - start_tv.tv_usec;
}

original_code:
static __inline__ T_WAIT_TICK get_tick( void )
{
  struct timeval tv;
  if( gettimeofday( &tv, 0 ) ){
    if( verbose_wait ){ if( tick_error==(0) ) printf( "Clock Error\n" ); }
    tick_error = (1);
    tv.tv_sec = 1;
    tv.tv_usec = 1;
  }
  return ( (T_WAIT_TICK)(tv.tv_sec - start_tv.tv_sec) * 1000000 +
    (T_WAIT_TICK)(tv.tv_usec - start_tv.tv_usec) );
}

canonical_original_code:
static __inline__ long get_tick()
{
  struct timeval tv;
  if( gettimeofday( &tv, 0 ) ){
    if( verbose_wait ){ if( tick_error==(0) ) printf( "Clock Error\n" ); }
    tick_error = (1);
    tv.tv_sec = 1;
    tv.tv_usec = 1;
  }
  return ( (T_WAIT_TICK)(tv.tv_sec - start_tv.tv_sec) * 1000000 +
    (T_WAIT_TICK)(tv.tv_usec - start_tv.tv_usec) );
}

variable_types: struct timeval { long tv_sec; long tv_usec; }

return_type: long


decompiled_code:
int __fastcall delay_usec(unsigned int a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = a1;
return select(0, 0LL, 0LL, 0LL, &v2);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = a1;
return select(0, 0LL, 0LL, 0LL, &v2);
}

original_code:
static __inline__ void delay_usec( unsigned int usec )
{
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = usec;
  select( 0, ((void *)0), ((void *)0), ((void *)0), &tv );
}

canonical_original_code:
static __inline__ void delay_usec(unsigned int usec)
{
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = usec;
  select( 0, ((void *)0), ((void *)0), ((void *)0), &tv );
}

variable_types:
usec
unsigned int

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
void wait_vsync_term()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void wait_vsync_term( void )
{
}

canonical_original_code:
void wait_vsync_term()
{
}


decompiled_code:
__int64 wait_vsync_reset()
{
__int64 result; // rax

set_tick();
sleep_min_time = wait_sleep_min_us;
wait_counter = 0;
delta_time = (unsigned int)(int)(1000000.0 / ((double)wait_rate * 55.4 / 100.0));
result = delta_time + get_tick();
next_time = result;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax

func0();
sleep_min_time = wait_sleep_min_us;
wait_counter = 0;
delta_time = (unsigned int)(int)(1000000.0 / ((double)wait_rate * 55.4 / 100.0));
result = delta_time + func1();
next_time = result;
return result;
}

original_code:
void wait_vsync_reset( void )
{
  set_tick();
  sleep_min_time = wait_sleep_min_us;
  wait_counter = 0;
  delta_time = (T_WAIT_TICK)(1000000.0/( (55.4) * wait_rate / 100 ));
  next_time = get_tick() + delta_time;
}

canonical_original_code:
void wait_vsync_reset()
{
  set_tick();
  sleep_min_time = wait_sleep_min_us;
  wait_counter = 0;
  delta_time = (T_WAIT_TICK)(1000000.0/( (55.4) * wait_rate / 100 ));
  next_time = get_tick() + delta_time;
}


decompiled_code:
__int64 wait_vsync_init()
{
if ( verbose_proc )
puts("\nTimer start (gettimeofday(2) - based)");
wait_vsync_reset();
return 1LL;
}

canonical_decompiled_code:
__int64 func5()
{
if ( verbose_proc )
puts("\nTimer start (gettimeofday(2) - based)");
func4();
return 1LL;
}

original_code:
int wait_vsync_init( void )
{
  if( verbose_proc ){
    printf("\nTimer start (gettimeofday(2) - based)\n" );
  }
  wait_vsync_reset();
  return (1);
}

canonical_original_code:
int wait_vsync_init()
{
  if( verbose_proc ){
    printf("\nTimer start (gettimeofday(2) - based)\n" );
  }
  wait_vsync_reset();
  return (1);
}