AuditRomSet

original_code:
int AuditRomSet (int game, tAuditRecord **audit)
{
 const struct RomModule *romp;
 const char *name;
 const struct GameDriver *gamedrv;
 int count = 0;
 tAuditRecord *aud;
 int err;
 if (!gAudits)
  gAudits = (tAuditRecord *)malloc(100 * sizeof (tAuditRecord));
 if (gAudits)
  *audit = aud = gAudits;
 else
  return 0;
 gamedrv = drivers[game];
 romp = gamedrv->rom;
 if (!romp) return -1;
 if (!osd_faccess (gamedrv->name, OSD_FILETYPE_ROM))
 {
  if (gamedrv->clone_of == 0 || (gamedrv->clone_of->flags & 0x4000) ||
    !osd_faccess(gamedrv->clone_of->name,OSD_FILETYPE_ROM))
   return 0;
 }
 while (romp->name || romp->offset || romp->length)
 {
  if (romp->name || romp->length) return 0;
  romp++;
  while (romp->length)
  {
   const struct GameDriver *drv;
   if (romp->name == 0)
    return 0;
   else if (romp->name == (char *)-1)
    return 0;
   name = romp->name;
   strcpy (aud->rom, name);
   aud->explength = 0;
   aud->length = 0;
   aud->expchecksum = romp->crc;
   aud->checksum = romp->crc;
   count++;
   drv = gamedrv;
   do
   {
    err = osd_fchecksum (drv->name, name, &aud->length, &aud->checksum);
    drv = drv->clone_of;
   } while (err && drv);
   do
   {
    if (romp->name != (char *)-1)
     aud->explength += romp->length & ~0xfc000000;
    romp++;
   }
   while (romp->length && (romp->name == 0 || romp->name == (char *)-1));
   if (err)
   {
    if (!aud->expchecksum)
     aud->status = 0x00000008;
    else
     aud->status = 0x00000004;
   }
   else if (aud->explength != aud->length)
    aud->status = 0x00000040;
   else if (aud->checksum != aud->expchecksum)
   {
    if (!aud->expchecksum)
     aud->status = 0x00000080;
    else if (aud->checksum == (~(aud->expchecksum)))
     aud->status = 0x00000002;
    else
     aud->status = 0x00000010;
   }
   else
    aud->status = 0x00000001;
   aud++;
  }
 }
 return count;
}

canonical_original_code:
int AuditRomSet(int game, struct tAuditRecord **audit)
{
 struct RomModule *romp;
 char *name;
 struct GameDriver *gamedrv;
 int count = 0;
 struct tAuditRecord *aud;
 int err;
 if (!gAudits)
  gAudits = (struct tAuditRecord *)malloc(100 * sizeof (tAuditRecord));
 if (gAudits)
  *audit = aud = gAudits;
 else
  return 0;
 gamedrv = drivers[game];
 romp = gamedrv->rom;
 if (!romp) return -1;
 if (!osd_faccess (gamedrv->name, OSD_FILETYPE_ROM))
 {
  if (gamedrv->clone_of == 0 || (gamedrv->clone_of->flags & 0x4000) ||
    !osd_faccess(gamedrv->clone_of->name,OSD_FILETYPE_ROM))
   return 0;
 }
 while (romp->name || romp->offset || romp->length)
 {
  if (romp->name || romp->length) return 0;
  romp++;
  while (romp->length)
  {
   struct GameDriver *drv;
   if (romp->name == 0)
    return 0;
   else if (romp->name == (char *)-1)
    return 0;
   name = romp->name;
   strcpy (aud->rom, name);
   aud->explength = 0;
   aud->length = 0;
   aud->expchecksum = romp->crc;
   aud->checksum = romp->crc;
   count++;
   drv = gamedrv;
   do
   {
    err = osd_fchecksum (drv->name, name, &aud->length, &aud->checksum);
    drv = drv->clone_of;
   } while (err && drv);
   do
   {
    if (romp->name != (char *)-1)
     aud->explength += romp->length & ~0xfc000000;
    romp++;
   }
   while (romp->length && (romp->name == 0 || romp->name == (char *)-1));
   if (err)
   {
    if (!aud->expchecksum)
     aud->status = 0x00000008;
    else
     aud->status = 0x00000004;
   }
   else if (aud->explength != aud->length)
    aud->status = 0x00000040;
   else if (aud->checksum != aud->expchecksum)
   {
    if (!aud->expchecksum)
     aud->status = 0x00000080;
    else if (aud->checksum == (~(aud->expchecksum)))
     aud->status = 0x00000002;
    else
     aud->status = 0x00000010;
   }
   else
    aud->status = 0x00000001;
   aud++;
  }
 }
 return count;
}

variable_types: game int

variable_types: struct tAuditRecord { char rom[20]; unsigned int explength; unsigned int length; unsigned int expchecksum; unsigned int checksum; int status; } **

variable_types: struct RomModule { char *name; unsigned int offset; unsigned int length; unsigned int crc; } *

variable_types:
name
char *

variable_types: count int

variable_types: err int

user_defined_types: struct InputPortTiny { unsigned short mask; unsigned short default_value; unsigned int type; char *name; }

user_defined_types: struct rectangle { int min_x; int min_y; }

user_defined_types: struct MachineSound { int sound_type; void *sound_interface; }

user_defined_types: struct MemoryReadAddress { unsigned int start; unsigned int (*handler)(unsigned int offset); }

user_defined_types: struct MemoryWriteAddress { unsigned int start; void (*handler)(unsigned int offset, unsigned int data); unsigned char **base; unsigned long *size; }

user_defined_types: struct IOReadPort { int start; unsigned int (*handler)(unsigned int offset); }

user_defined_types: struct IOWritePort { int start; void (*handler)(unsigned int offset, unsigned int data); }

user_defined_types: struct GfxLayout { unsigned short width; unsigned int total; unsigned short planes; unsigned int planeoffset[8]; unsigned int xoffset[64]; unsigned int yoffset[64]; unsigned short charincrement; }


RomsetMissing

original_code:
int RomsetMissing (int game)
{
 const struct GameDriver *gamedrv = drivers[game];
 if (gamedrv->clone_of)
 {
  tAuditRecord *aud;
  int count;
  int i;
  int cloneRomsFound = 0;
  if ((count = AuditRomSet (game, &aud)) == 0)
   return 1;
  if (count == -1) return 0;
  for (i = 0; i < count; i++)
   if (aud[i].status != 0x00000004)
    if (!RomInSet (gamedrv->clone_of, aud[i].expchecksum))
     cloneRomsFound++;
  return !cloneRomsFound;
 }
 else
  return !osd_faccess (gamedrv->name, OSD_FILETYPE_ROM);
}

canonical_original_code:
int RomsetMissing(int game)
{
 struct GameDriver *gamedrv = drivers[game];
 if (gamedrv->clone_of)
 {
  struct tAuditRecord *aud;
  int count;
  int i;
  int cloneRomsFound = 0;
  if ((count = AuditRomSet (game, &aud)) == 0)
   return 1;
  if (count == -1) return 0;
  for (i = 0; i < count; i++)
   if (aud[i].status != 0x00000004)
    if (!RomInSet (gamedrv->clone_of, aud[i].expchecksum))
     cloneRomsFound++;
  return !cloneRomsFound;
 }
 else
  return !osd_faccess (gamedrv->name, OSD_FILETYPE_ROM);
}

variable_types: struct GameDriver { char *source_file; struct GameDriver *clone_of; char *name; char *description; char *year; char *manufacturer; struct MachineDriver { struct MachineCPU { int cpu_type; int cpu_clock; struct MemoryReadAddress { unsigned int start; unsigned int (*handler)(unsigned int offset); } *memory_read; struct MemoryWriteAddress { unsigned int start; void (*handler)(unsigned int offset, unsigned int data); unsigned char **base; unsigned long *size; } *memory_write; struct IOReadPort { int start; unsigned int (*handler)(unsigned int offset); } *port_read; struct IOWritePort { int start; void (*handler)(unsigned int offset, unsigned int data); } *port_write; int (*vblank_interrupt)(); int vblank_interrupts_per_frame; int (*timed_interrupt)(); int timed_interrupts_per_second; void *reset_param; } cpu[8]; float frames_per_second; int vblank_duration; int cpu_slices_per_frame; void (*init_machine)(); int screen_width; struct rectangle { int min_x; int min_y; } default_visible_area; struct GfxDecodeInfo { int memory_region; int start; struct GfxLayout { unsigned short width; unsigned int total; unsigned short planes; unsigned int planeoffset[8]; unsigned int xoffset[64]; unsigned int yoffset[64]; unsigned short charincrement; } *gfxlayout; int color_codes_start; int total_color_codes; } *gfxdecodeinfo; unsigned int total_colors; unsigned int color_table_len; void (*vh_init_palette)(unsigned char *palette, unsigned short *colortable, unsigned char *color_prom); int video_attributes; void (*vh_eof_callback)(); int (*vh_start)(); void (*vh_stop)(); void (*vh_update)(struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *bitmap, int full_refresh); int sound_attributes; int obsolete1; int obsolete2; int obsolete3; struct MachineSound { int sound_type; void *sound_interface; } sound[5]; void (*nvram_handler)(void *file, int read_or_write); } *drv; struct InputPortTiny { unsigned short mask; unsigned short default_value; unsigned int type; char *name; } *input_ports; void (*driver_init)(); struct RomModule { char *name; unsigned int offset; unsigned int length; unsigned int crc; } *rom; unsigned int flags; } *

variable_types: struct tAuditRecord { char rom[20]; unsigned int explength; unsigned int length; unsigned int expchecksum; unsigned int checksum; int status; } *

variable_types: i int

variable_types: cloneRomsFound int

return_type: int

user_defined_types: struct GameDriver { char *source_file; struct GameDriver *clone_of; char *name; char *description; char *year; char *manufacturer; struct MachineDriver *drv; struct InputPortTiny *input_ports; void (*driver_init)(); struct RomModule *rom; unsigned int flags; }

user_defined_types: struct MachineDriver { struct MachineCPU cpu[8]; float frames_per_second; int vblank_duration; int cpu_slices_per_frame; void (*init_machine)(); int screen_width; struct rectangle default_visible_area; struct GfxDecodeInfo *gfxdecodeinfo; unsigned int total_colors; unsigned int color_table_len; void (*vh_init_palette)(unsigned char *palette, unsigned short *colortable, unsigned char *color_prom); int video_attributes; void (*vh_eof_callback)(); int (*vh_start)(); void (*vh_stop)(); void (*vh_update)(struct osd_bitmap *bitmap, int full_refresh); int sound_attributes; int obsolete1; int obsolete2; int obsolete3; struct MachineSound sound[5]; void (*nvram_handler)(void *file, int read_or_write); }

user_defined_types: struct MachineCPU { int cpu_type; int cpu_clock; struct MemoryReadAddress *memory_read; struct MemoryWriteAddress *memory_write; struct IOReadPort *port_read; struct IOWritePort *port_write; int (*vblank_interrupt)(); int vblank_interrupts_per_frame; int (*timed_interrupt)(); int timed_interrupts_per_second; void *reset_param; }

user_defined_types: struct GfxDecodeInfo { int memory_region; int start; struct GfxLayout *gfxlayout; int color_codes_start; int total_color_codes; }


VerifyRomSet

decompiled_code:
__int64 __fastcall VerifyRomSet(int a1, void (*a2)(const char *, ...))
{
__int64 v2; // r15
int v3; // ecx
__int64 result; // rax
__int64 v5; // rbx
int v6; // er14
unsigned int v7; // ebx
unsigned __int8 v8; // r13
int v9; // eax
unsigned int v10; // [rsp+Ch] [rbp-4Ch]
const char *v11; // [rsp+18h] [rbp-40h] BYREF

v2 = drivers[a1];
v3 = AuditRomSet(a1, &v11);
v10 = v3;
result = 1LL;
if ( v3 )
{
result = 0LL;
if ( v3 != -1 )
{
if ( *(_QWORD *)(v2 + 8) )
{
result = 3LL;
if ( v3 <= 0 )
return result;
v5 = 0LL;
v6 = 0;
do
{
if ( *(_DWORD *)&v11[v5 + 36] != 4 )
v6 += RomInSet(*(_QWORD *)(v2 + 8), *(_DWORD *)&v11[v5 + 28]) == 0;
v5 += 40LL;
}
while ( 40LL * v10 != v5 );
if ( !v6 )
return 3LL;
}
v7 = v10;
v8 = 0;
do
{
v9 = *((_DWORD *)v11 + 9);
v8 |= v9;
if ( v9 > 32 )
{
if ( v9 == 64 )
{
a2(
"%-8s: %-12s %7d bytes %08x INCORRECT LENGTH: %8d\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5),
*((unsigned int *)v11 + 7),
*((unsigned int *)v11 + 6));
}
else if ( v9 == 128 )
{
a2(
"%-8s: %-12s %7d bytes NO GOOD DUMP KNOWN\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5));
}
}
else if ( v9 > 1 )
{
switch ( v9 )
{
case 2:
a2(
"%-8s: %-12s %7d bytes ROM NEEDS REDUMP\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5));
break;
case 4:
a2(
"%-8s: %-12s %7d bytes %08x NOT FOUND\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5),
*((unsigned int *)v11 + 7));
break;
case 8:
a2(
"%-8s: %-12s %7d bytes NOT FOUND - NO GOOD DUMP KNOWN\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5));
break;
case 16:
a2(
"%-8s: %-12s %7d bytes %08x INCORRECT CHECKSUM: %08x\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5),
*((unsigned int *)v11 + 7),
*((unsigned int *)v11 + 8));
break;
case 32:
a2("Out of memory reading ROM %s\n", v11);
break;
default:
break;
}
}
v11 += 40;
--v7;
}
while ( v7 );
result = 2LL;
if ( (v8 & 0x74) == 0 )
{
result = v8 & 0x8A;
if ( (v8 & 0x8A) != 0 )
result = 4LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, void (*a2)(const char *, ...))
{
__int64 v2; // r15
int v3; // ecx
__int64 result; // rax
__int64 v5; // rbx
int v6; // er14
unsigned int v7; // ebx
unsigned __int8 v8; // r13
int v9; // eax
unsigned int v10; // [rsp+Ch] [rbp-4Ch]
const char *v11; // [rsp+18h] [rbp-40h] BYREF

v2 = drivers[a1];
v3 = func1(a1, &v11);
v10 = v3;
result = 1LL;
if ( v3 )
{
result = 0LL;
if ( v3 != -1 )
{
if ( *(_QWORD *)(v2 + 8) )
{
result = 3LL;
if ( v3 <= 0 )
return result;
v5 = 0LL;
v6 = 0;
do
{
if ( *(_DWORD *)&v11[v5 + 36] != 4 )
v6 += func0(*(_QWORD *)(v2 + 8), *(_DWORD *)&v11[v5 + 28]) == 0;
v5 += 40LL;
}
while ( 40LL * v10 != v5 );
if ( !v6 )
return 3LL;
}
v7 = v10;
v8 = 0;
do
{
v9 = *((_DWORD *)v11 + 9);
v8 |= v9;
if ( v9 > 32 )
{
if ( v9 == 64 )
{
a2(
"%-8s: %-12s %7d bytes %08x INCORRECT LENGTH: %8d\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5),
*((unsigned int *)v11 + 7),
*((unsigned int *)v11 + 6));
}
else if ( v9 == 128 )
{
a2(
"%-8s: %-12s %7d bytes NO GOOD DUMP KNOWN\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5));
}
}
else if ( v9 > 1 )
{
switch ( v9 )
{
case 2:
a2(
"%-8s: %-12s %7d bytes ROM NEEDS REDUMP\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5));
break;
case 4:
a2(
"%-8s: %-12s %7d bytes %08x NOT FOUND\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5),
*((unsigned int *)v11 + 7));
break;
case 8:
a2(
"%-8s: %-12s %7d bytes NOT FOUND - NO GOOD DUMP KNOWN\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5));
break;
case 16:
a2(
"%-8s: %-12s %7d bytes %08x INCORRECT CHECKSUM: %08x\n",
*(const char **)(drivers[a1] + 16LL),
v11,
*((unsigned int *)v11 + 5),
*((unsigned int *)v11 + 7),
*((unsigned int *)v11 + 8));
break;
case 32:
a2("Out of memory reading ROM %s\n", v11);
break;
default:
break;
}
}
v11 += 40;
--v7;
}
while ( v7 );
result = 2LL;
if ( (v8 & 0x74) == 0 )
{
result = v8 & 0x8A;
if ( (v8 & 0x8A) != 0 )
result = 4LL;
}
}
}
return result;
}

original_code:
int VerifyRomSet (int game, verify_printf_proc verify_printf)
{
 tAuditRecord *aud;
 int count;
 int archive_status = 0;
 const struct GameDriver *gamedrv = drivers[game];
 if ((count = AuditRomSet (game, &aud)) == 0)
  return 1;
 if (count == -1) return 0;
        if (gamedrv->clone_of)
 {
  int i;
  int cloneRomsFound = 0;
  for (i = 0; i < count; i++)
   if (aud[i].status != 0x00000004)
    if (!RomInSet (gamedrv->clone_of, aud[i].expchecksum))
     cloneRomsFound++;
  if (cloneRomsFound == 0)
   return 3;
 }
 while (count--)
 {
  archive_status |= aud->status;
  switch (aud->status)
  {
   case 0x00000004:
    verify_printf ("%-8s: %-12s %7d bytes %08x NOT FOUND\n",
     drivers[game]->name, aud->rom, aud->explength, aud->expchecksum);
    break;
   case 0x00000008:
    verify_printf ("%-8s: %-12s %7d bytes NOT FOUND - NO GOOD DUMP KNOWN\n",
     drivers[game]->name, aud->rom, aud->explength);
    break;
   case 0x00000080:
    verify_printf ("%-8s: %-12s %7d bytes NO GOOD DUMP KNOWN\n",
     drivers[game]->name, aud->rom, aud->explength);
    break;
   case 0x00000010:
    verify_printf ("%-8s: %-12s %7d bytes %08x INCORRECT CHECKSUM: %08x\n",
     drivers[game]->name, aud->rom, aud->explength, aud->expchecksum,aud->checksum);
    break;
   case 0x00000002:
    verify_printf ("%-8s: %-12s %7d bytes ROM NEEDS REDUMP\n",
     drivers[game]->name, aud->rom, aud->explength);
    break;
   case 0x00000020:
    verify_printf ("Out of memory reading ROM %s\n", aud->rom);
    break;
   case 0x00000040:
    verify_printf ("%-8s: %-12s %7d bytes %08x INCORRECT LENGTH: %8d\n",
     drivers[game]->name, aud->rom, aud->explength, aud->expchecksum,aud->length);
    break;
   case 0x00000001:
    break;
  }
  aud++;
 }
 if (archive_status & (0x00000004|0x00000010|0x00000020|0x00000040))
  return 2;
 if (archive_status & (0x00000080|0x00000002|0x00000008))
  return 4;
 return 0;
}

canonical_original_code:
int VerifyRomSet(int game, void (*verify_printf)(char *fmt, ...))
{
 struct tAuditRecord *aud;
 int count;
 int archive_status = 0;
 struct GameDriver *gamedrv = drivers[game];
 if ((count = AuditRomSet (game, &aud)) == 0)
  return 1;
 if (count == -1) return 0;
        if (gamedrv->clone_of)
 {
  int i;
  int cloneRomsFound = 0;
  for (i = 0; i < count; i++)
   if (aud[i].status != 0x00000004)
    if (!RomInSet (gamedrv->clone_of, aud[i].expchecksum))
     cloneRomsFound++;
  if (cloneRomsFound == 0)
   return 3;
 }
 while (count--)
 {
  archive_status |= aud->status;
  switch (aud->status)
  {
   case 0x00000004:
    verify_printf ("%-8s: %-12s %7d bytes %08x NOT FOUND\n",
     drivers[game]->name, aud->rom, aud->explength, aud->expchecksum);
    break;
   case 0x00000008:
    verify_printf ("%-8s: %-12s %7d bytes NOT FOUND - NO GOOD DUMP KNOWN\n",
     drivers[game]->name, aud->rom, aud->explength);
    break;
   case 0x00000080:
    verify_printf ("%-8s: %-12s %7d bytes NO GOOD DUMP KNOWN\n",
     drivers[game]->name, aud->rom, aud->explength);
    break;
   case 0x00000010:
    verify_printf ("%-8s: %-12s %7d bytes %08x INCORRECT CHECKSUM: %08x\n",
     drivers[game]->name, aud->rom, aud->explength, aud->expchecksum,aud->checksum);
    break;
   case 0x00000002:
    verify_printf ("%-8s: %-12s %7d bytes ROM NEEDS REDUMP\n",
     drivers[game]->name, aud->rom, aud->explength);
    break;
   case 0x00000020:
    verify_printf ("Out of memory reading ROM %s\n", aud->rom);
    break;
   case 0x00000040:
    verify_printf ("%-8s: %-12s %7d bytes %08x INCORRECT LENGTH: %8d\n",
     drivers[game]->name, aud->rom, aud->explength, aud->expchecksum,aud->length);
    break;
   case 0x00000001:
    break;
  }
  aud++;
 }
 if (archive_status & (0x00000004|0x00000010|0x00000020|0x00000040))
  return 2;
 if (archive_status & (0x00000080|0x00000002|0x00000008))
  return 4;
 return 0;
}

user_defined_types: struct tAuditRecord { char rom[20]; unsigned int explength; unsigned int length; unsigned int expchecksum; unsigned int checksum; int status; }

user_defined_types: struct RomModule { char *name; unsigned int offset; unsigned int length; unsigned int crc; }

user_defined_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; }


AuditSampleSet

original_code:
int AuditSampleSet (int game, tMissingSample **audit)
{
 int skipfirst;
 void *f;
 const char **samplenames, *sharedname;
 int exist;
 static const struct GameDriver *gamedrv;
 int j;
 int count = 0;
 tMissingSample *aud;
 gamedrv = drivers[game];
 samplenames = ((void *)0);
 for( j = 0; gamedrv->drv->sound[j].sound_type && j < 5; j++ )
 {
  if( gamedrv->drv->sound[j].sound_type == SOUND_SAMPLES )
   samplenames = ((struct Samplesinterface *)gamedrv->drv->sound[j].sound_interface)->samplenames;
 }
 if (samplenames == 0 || samplenames[0] == 0)
  return 0;
 if (samplenames[0][0] == '*')
 {
  sharedname=samplenames[0]+1;
  skipfirst = 1;
 }
 else
 {
  sharedname = ((void *)0);
  skipfirst = 0;
 }
 exist = osd_faccess (gamedrv->name, OSD_FILETYPE_SAMPLE);
 if (!exist && skipfirst)
  exist = osd_faccess (sharedname, OSD_FILETYPE_SAMPLE);
 if (!exist)
  return -1;
 if (!gMissingSamples)
  gMissingSamples = (tMissingSample *)malloc(200 * sizeof (tMissingSample));
 if (gMissingSamples)
  *audit = aud = gMissingSamples;
 else
  return 0;
 for (j = skipfirst; samplenames[j] != 0; j++)
 {
  if (strlen (samplenames[j]) == 0)
   continue;
  f = osd_fopen (gamedrv->name, samplenames[j], OSD_FILETYPE_SAMPLE, 0);
  if (f == ((void *)0) && skipfirst)
   f = osd_fopen (sharedname, samplenames[j], OSD_FILETYPE_SAMPLE, 0);
  if (f)
   osd_fclose(f);
  else
  {
   strcpy (aud->name, samplenames[j]);
   count++;
   aud++;
  }
 }
 return count;
}

canonical_original_code:
int AuditSampleSet(int game, struct tMissingSample **audit)
{
 int skipfirst;
 void *f;
 char **samplenames; char *sharedname;
 int exist;
 static struct GameDriver *gamedrv;
 int j;
 int count = 0;
 struct tMissingSample *aud;
 gamedrv = drivers[game];
 samplenames = ((void *)0);
 for( j = 0; gamedrv->drv->sound[j].sound_type && j < 5; j++ )
 {
  if( gamedrv->drv->sound[j].sound_type == SOUND_SAMPLES )
   samplenames = ((struct Samplesinterface *)gamedrv->drv->sound[j].sound_interface)->samplenames;
 }
 if (samplenames == 0 || samplenames[0] == 0)
  return 0;
 if (samplenames[0][0] == '*')
 {
  sharedname=samplenames[0]+1;
  skipfirst = 1;
 }
 else
 {
  sharedname = ((void *)0);
  skipfirst = 0;
 }
 exist = osd_faccess (gamedrv->name, OSD_FILETYPE_SAMPLE);
 if (!exist && skipfirst)
  exist = osd_faccess (sharedname, OSD_FILETYPE_SAMPLE);
 if (!exist)
  return -1;
 if (!gMissingSamples)
  gMissingSamples = (struct tMissingSample *)malloc(200 * sizeof (tMissingSample));
 if (gMissingSamples)
  *audit = aud = gMissingSamples;
 else
  return 0;
 for (j = skipfirst; samplenames[j] != 0; j++)
 {
  if (strlen (samplenames[j]) == 0)
   continue;
  f = osd_fopen (gamedrv->name, samplenames[j], OSD_FILETYPE_SAMPLE, 0);
  if (f == ((void *)0) && skipfirst)
   f = osd_fopen (sharedname, samplenames[j], OSD_FILETYPE_SAMPLE, 0);
  if (f)
   osd_fclose(f);
  else
  {
   strcpy (aud->name, samplenames[j]);
   count++;
   aud++;
  }
 }
 return count;
}

variable_types: struct tMissingSample { char name[20]; } **

variable_types: skipfirst int

variable_types:
f
void *

variable_types:
samplenames
char **

variable_types:
sharedname
char *

variable_types: exist int

variable_types: j int

user_defined_types: struct Samplesinterface { int channels; int volume; char **samplenames; }


VerifySampleSet

decompiled_code:
__int64 __fastcall VerifySampleSet(int a1, void (*a2)(const char *, ...))
{
__int64 result; // rax
int v3; // ebx
char *v4; // [rsp+8h] [rbp-30h] BYREF

result = AuditSampleSet(a1, &v4);
if ( (_DWORD)result == -1 )
return 1LL;
if ( (_DWORD)result )
{
v3 = result;
do
{
a2("%-8s: %s NOT FOUND\n", *(const char **)(drivers[a1] + 16LL), v4);
v4 += 20;
--v3;
}
while ( v3 );
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, void (*a2)(const char *, ...))
{
__int64 result; // rax
int v3; // ebx
char *v4; // [rsp+8h] [rbp-30h] BYREF

result = func4(a1, &v4);
if ( (_DWORD)result == -1 )
return 1LL;
if ( (_DWORD)result )
{
v3 = result;
do
{
a2("%-8s: %s NOT FOUND\n", *(const char **)(drivers[a1] + 16LL), v4);
v4 += 20;
--v3;
}
while ( v3 );
result = 2LL;
}
return result;
}

original_code:
int VerifySampleSet (int game, verify_printf_proc verify_printf)
{
 tMissingSample *aud;
 int count;
 count = AuditSampleSet (game, &aud);
 if (count==-1)
  return 1;
 else if (count==0)
  return 0;
 while (count--)
 {
  verify_printf ("%-8s: %s NOT FOUND\n", drivers[game]->name, aud->name);
  aud++;
 }
 return 2;
}

canonical_original_code:
int VerifySampleSet(int game, void (*verify_printf)(char *fmt, ...))
{
 struct tMissingSample *aud;
 int count;
 count = AuditSampleSet (game, &aud);
 if (count==-1)
  return 1;
 else if (count==0)
  return 0;
 while (count--)
 {
  verify_printf ("%-8s: %s NOT FOUND\n", drivers[game]->name, aud->name);
  aud++;
 }
 return 2;
}

variable_types: game int

variable_types: struct tMissingSample { char name[20]; } *


original_code:
void wiz_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
{
 int i;
 for (i = 0;i < Machine->drv->total_colors;i++)
 {
  int bit0,bit1,bit2,bit3;
  bit0 = (color_prom[0] >> 0) & 0x01;
  bit1 = (color_prom[0] >> 1) & 0x01;
  bit2 = (color_prom[0] >> 2) & 0x01;
  bit3 = (color_prom[0] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
  bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
  bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
  color_prom++;
 }
}

canonical_original_code:
void wiz_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable, unsigned char *color_prom)
{
 int i;
 for (i = 0;i < Machine->drv->total_colors;i++)
 {
  int bit0; int bit1; int bit2; int bit3;
  bit0 = (color_prom[0] >> 0) & 0x01;
  bit1 = (color_prom[0] >> 1) & 0x01;
  bit2 = (color_prom[0] >> 2) & 0x01;
  bit3 = (color_prom[0] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
  bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
  bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
  color_prom++;
 }
}

variable_types:
palette
unsigned char *

variable_types:
colortable
unsigned short *

return_type: void


decompiled_code:
void *__fastcall wiz_palettebank_w(int a1, char a2)
{
void *result; // rax

result = (void *)(a2 & 1);
if ( (unsigned __int8)palbank[a1] != (_DWORD)result )
{
palbank[a1] = a2 & 1;
palette_bank = (unsigned __int8)palbank[0] + 2 * (unsigned __int8)byte_FBD;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(int a1, char a2)
{
void *result; // rax

result = (void *)(a2 & 1);
if ( (unsigned __int8)palbank[a1] != (_DWORD)result )
{
palbank[a1] = a2 & 1;
palette_bank = (unsigned __int8)palbank[0] + 2 * (unsigned __int8)byte_FBD;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

original_code:
void wiz_palettebank_w(offs_t offset,data_t data)
{
 if (palbank[offset] != (data & 1))
 {
  palbank[offset] = data & 1;
  palette_bank = palbank[0] + 2 * palbank[1];
  memset(dirtybuffer,1,videoram_size);
 }
}

canonical_original_code:
void wiz_palettebank_w(unsigned int offset, unsigned int data)
{
 if (palbank[offset] != (data & 1))
 {
  palbank[offset] = data & 1;
  palette_bank = palbank[0] + 2 * palbank[1];
  memset(dirtybuffer,1,videoram_size);
 }
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int


decompiled_code:
void *__fastcall wiz_char_bank_select_w(int a1, char a2)
{
void *result; // rax

result = (void *)(a2 & 1);
if ( (unsigned __int8)char_bank[a1] != (_DWORD)result )
{
char_bank[a1] = a2 & 1;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(int a1, char a2)
{
void *result; // rax

result = (void *)(a2 & 1);
if ( (unsigned __int8)char_bank[a1] != (_DWORD)result )
{
char_bank[a1] = a2 & 1;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

original_code:
void wiz_char_bank_select_w(offs_t offset,data_t data)
{
 if (char_bank[offset] != (data & 1))
 {
  char_bank[offset] = data & 1;
  memset(dirtybuffer,1,videoram_size);
 }
}

canonical_original_code:
void wiz_char_bank_select_w(unsigned int offset, unsigned int data)
{
 if (char_bank[offset] != (data & 1))
 {
  char_bank[offset] = data & 1;
  memset(dirtybuffer,1,videoram_size);
 }
}


decompiled_code:
void *__fastcall wiz_flipx_w(__int64 a1, int a2)
{
void *result; // rax

if ( flipx != a2 )
{
flipx = a2;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(__int64 a1, int a2)
{
void *result; // rax

if ( flipx != a2 )
{
flipx = a2;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

original_code:
void wiz_flipx_w(offs_t offset,data_t data)
{
    if (flipx != data)
    {
  flipx = data;
  memset(dirtybuffer, 1, videoram_size);
    }
}

canonical_original_code:
void wiz_flipx_w(unsigned int offset, unsigned int data)
{
    if (flipx != data)
    {
  flipx = data;
  memset(dirtybuffer, 1, videoram_size);
    }
}


decompiled_code:
void *__fastcall wiz_flipy_w(__int64 a1, int a2)
{
void *result; // rax

if ( flipy != a2 )
{
flipy = a2;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, int a2)
{
void *result; // rax

if ( flipy != a2 )
{
flipy = a2;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

original_code:
void wiz_flipy_w(offs_t offset,data_t data)
{
    if (flipy != data)
    {
  flipy = data;
  memset(dirtybuffer, 1, videoram_size);
    }
}

canonical_original_code:
void wiz_flipy_w(unsigned int offset, unsigned int data)
{
    if (flipy != data)
    {
  flipy = data;
  memset(dirtybuffer, 1, videoram_size);
    }
}


decompiled_code:
__int64 __fastcall wiz_vh_screenrefresh(__int64 a1)
{
int v1; // er12

draw_background(a1, ((unsigned __int8)byte_FBF | (2 * (unsigned __int8)char_bank[0])) + 2);
draw_foreground(a1, 0);
if ( *(char *)(spriteram + 1) < 0 || !*(_BYTE *)(spriteram + 3) || (v1 = 8, !*(_BYTE *)spriteram) )
v1 = (unsigned __int8)*wiz_sprite_bank + 7;
draw_sprites(a1, spriteram_2, 6);
return draw_sprites(a1, spriteram, v1);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
int v1; // er12

func8(a1, ((unsigned __int8)byte_FBF | (2 * (unsigned __int8)char_bank[0])) + 2);
func6(a1, 0);
if ( *(char *)(spriteram + 1) < 0 || !*(_BYTE *)(spriteram + 3) || (v1 = 8, !*(_BYTE *)spriteram) )
v1 = (unsigned __int8)*wiz_sprite_bank + 7;
func7(a1, spriteram_2, 6);
return func7(a1, spriteram, v1);
}

original_code:
void wiz_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int bank;
 const struct rectangle* visible_area;
 draw_background(bitmap, 2 + ((char_bank[0] << 1) | char_bank[1]), 0);
 draw_foreground(bitmap, 0);
 visible_area = flipx ? &spritevisibleareaflipx : &spritevisiblearea;
 if ((spriteram[1] & 0x80) || !spriteram[3] || !spriteram[0])
 {
     bank = 7 + *wiz_sprite_bank;
 }
 else
 {
  bank = 8;
 }
 draw_sprites(bitmap, spriteram_2, 6, visible_area);
 draw_sprites(bitmap, spriteram , bank, visible_area);
}

canonical_original_code:
void wiz_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int bank;
 struct rectangle *visible_area;
 draw_background(bitmap, 2 + ((char_bank[0] << 1) | char_bank[1]), 0);
 draw_foreground(bitmap, 0);
 visible_area = flipx ? &spritevisibleareaflipx : &spritevisiblearea;
 if ((spriteram[1] & 0x80) || !spriteram[3] || !spriteram[0])
 {
     bank = 7 + *wiz_sprite_bank;
 }
 else
 {
  bank = 8;
 }
 draw_sprites(bitmap, spriteram_2, 6, visible_area);
 draw_sprites(bitmap, spriteram , bank, visible_area);
}

variable_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *

variable_types: bank int

variable_types: struct rectangle { int min_x; int min_y; } *


decompiled_code:
__int64 __fastcall stinger_vh_screenrefresh(__int64 a1)
{
draw_background(a1, (unsigned __int8)char_bank[0] + 2);
draw_foreground(a1, 1);
draw_sprites(a1, spriteram_2, 4);
return draw_sprites(a1, spriteram, 5);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
func8(a1, (unsigned __int8)char_bank[0] + 2);
func6(a1, 1);
func7(a1, spriteram_2, 4);
return func7(a1, spriteram, 5);
}

original_code:
void stinger_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 draw_background(bitmap, 2 + char_bank[0], 1);
 draw_foreground(bitmap, 1);
 draw_sprites(bitmap, spriteram_2, 4, &Machine->visible_area);
 draw_sprites(bitmap, spriteram , 5, &Machine->visible_area);
}

canonical_original_code:
void stinger_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 draw_background(bitmap, 2 + char_bank[0], 1);
 draw_foreground(bitmap, 1);
 draw_sprites(bitmap, spriteram_2, 4, &Machine->visible_area);
 draw_sprites(bitmap, spriteram , 5, &Machine->visible_area);
}


decompiled_code:
__int64 init_stinger()
{
__int64 v0; // rbx
int v1; // ebp
int v2; // edi
__int64 i; // rdx
char v4; // si
__int64 result; // rax

v0 = memory_region(129LL);
v1 = (int)memory_region_length(129LL) / 2;
memory_set_opcode_base(0LL, v0 + v1);
v2 = v1;
for ( i = 0LL; i != 0x10000; ++i )
{
if ( (i & 0x2040) != 0 )
{
result = v2;
*(_BYTE *)(v0 + v2) = *(_BYTE *)(v0 + i);
}
else
{
v4 = *(_BYTE *)(v0 + i);
LODWORD(result) = ((v4 & 8) != 0) | (2 * ((unsigned __int8)v4 >> 5)) & 2;
if ( v4 < 0 )
LODWORD(result) = 3 - result;
result = (int)result;
*(_BYTE *)(v0 + v2) = xortable_0[4 * (((int)i >> 3) & 1 | ((unsigned int)i >> 4) & 2) + (int)result] ^ v4;
}
++v2;
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 v0; // rbx
int v1; // ebp
int v2; // edi
__int64 i; // rdx
char v4; // si
__int64 result; // rax

v0 = memory_region(129LL);
v1 = (int)memory_region_length(129LL) / 2;
memory_set_opcode_base(0LL, v0 + v1);
v2 = v1;
for ( i = 0LL; i != 0x10000; ++i )
{
if ( (i & 0x2040) != 0 )
{
result = v2;
*(_BYTE *)(v0 + v2) = *(_BYTE *)(v0 + i);
}
else
{
v4 = *(_BYTE *)(v0 + i);
LODWORD(result) = ((v4 & 8) != 0) | (2 * ((unsigned __int8)v4 >> 5)) & 2;
if ( v4 < 0 )
LODWORD(result) = 3 - result;
result = (int)result;
*(_BYTE *)(v0 + v2) = xortable_0[4 * (((int)i >> 3) & 1 | ((unsigned int)i >> 4) & 2) + (int)result] ^ v4;
}
++v2;
}
return result;
}

original_code:
static void init_stinger(void)
{
 static const unsigned char xortable[4][4] =
 {
  { 0xa0,0x88,0x88,0xa0 },
  { 0x88,0x00,0xa0,0x28 },
  { 0x80,0xa8,0x20,0x08 },
  { 0x28,0x28,0x88,0x88 }
 };
 unsigned char *rom = memory_region(REGION_CPU1);
 int diff = memory_region_length(REGION_CPU1) / 2;
 int A;
 memory_set_opcode_base(0,rom+diff);
 for (A = 0x0000;A < 0x10000;A++)
 {
  int row,col;
  unsigned char src;
  if (A & 0x2040)
  {
   rom[A+diff] = rom[A];
  }
  else
  {
   src = rom[A];
   row = ((A >> 3) & 1) + (((A >> 5) & 1) << 1);
   col = ((src >> 3) & 1) + (((src >> 5) & 1) << 1);
   if (src & 0x80) col = 3 - col;
   rom[A+diff] = src ^ xortable[row][col];
  }
 }
}

canonical_original_code:
static void init_stinger()
{
 static unsigned char xortable[4][4] = {
  { 0xa0,0x88,0x88,0xa0 },
  { 0x88,0x00,0xa0,0x28 },
  { 0x80,0xa8,0x20,0x08 },
  { 0x28,0x28,0x88,0x88 }
 };
 unsigned char *rom = memory_region(REGION_CPU1);
 int diff = memory_region_length(REGION_CPU1) / 2;
 int A;
 memory_set_opcode_base(0,rom+diff);
 for (A = 0x0000;A < 0x10000;A++)
 {
  int row; int col;
  unsigned char src;
  if (A & 0x2040)
  {
   rom[A+diff] = rom[A];
  }
  else
  {
   src = rom[A];
   row = ((A >> 3) & 1) + (((A >> 5) & 1) << 1);
   col = ((src >> 3) & 1) + (((src >> 5) & 1) << 1);
   if (src & 0x80) col = 3 - col;
   rom[A+diff] = src ^ xortable[row][col];
  }
 }
}

variable_types:
xortable
unsigned char [4][4]

variable_types:
rom
unsigned char *

variable_types: diff int

variable_types: A int

variable_types: row int

variable_types: col int

variable_types:
src
unsigned char


decompiled_code:
void *__fastcall draw_foreground(__int64 a1, int a2)
{
void *result; // rax
int v3; // ebx
__int64 v5; // rbp
int v6; // edi

result = &videoram_size;
v3 = videoram_size - 1;
if ( videoram_size - 1 >= 0 )
{
v5 = v3;
do
{
if ( a2 )
v6 = *(_BYTE *)(wiz_attributesram2 + 2 * (v3 % 32) + 1) & 7;
else
v6 = *(_BYTE *)(wiz_colorram2 + v5) & 7;
result = (void *)drawgfx(
a1,
*(_QWORD *)(Machine + 8LL * (unsigned __int8)char_bank[1] + 512),
*(unsigned __int8 *)(wiz_videoram2 + v5--),
(unsigned int)(v6 + 8 * palette_bank));
--v3;
}
while ( v3 >= 0 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(__int64 a1, int a2)
{
void *result; // rax
int v3; // ebx
__int64 v5; // rbp
int v6; // edi

result = &videoram_size;
v3 = videoram_size - 1;
if ( videoram_size - 1 >= 0 )
{
v5 = v3;
do
{
if ( a2 )
v6 = *(_BYTE *)(wiz_attributesram2 + 2 * (v3 % 32) + 1) & 7;
else
v6 = *(_BYTE *)(wiz_colorram2 + v5) & 7;
result = (void *)drawgfx(
a1,
*(_QWORD *)(Machine + 8LL * (unsigned __int8)char_bank[1] + 512),
*(unsigned __int8 *)(wiz_videoram2 + v5--),
(unsigned int)(v6 + 8 * palette_bank));
--v3;
}
while ( v3 >= 0 );
}
return result;
}

original_code:
static void draw_foreground(struct osd_bitmap *bitmap, int colortype)
{
 int offs;
 for (offs = videoram_size - 1;offs >= 0;offs--)
 {
  int scroll,sx,sy,col;
  sx = offs % 32;
  sy = offs / 32;
  if (colortype)
  {
   col = (wiz_attributesram2[2 * sx + 1] & 0x07);
  }
  else
  {
   col = (wiz_colorram2[offs] & 0x07);
  }
  scroll = (8*sy + 256 - wiz_attributesram2[2 * sx]) % 256;
  if (flipy)
  {
     scroll = (248 - scroll) % 256;
  }
  if (flipx) sx = 31 - sx;
  drawgfx(bitmap,Machine->gfx[char_bank[1]],
   wiz_videoram2[offs],
   col + 8 * palette_bank,
   flipx,flipy,
   8*sx,scroll,
   &Machine->visible_area,TRANSPARENCY_PEN,0);
 }
}

canonical_original_code:
static void draw_foreground(struct osd_bitmap *bitmap, int colortype)
{
 int offs;
 for (offs = videoram_size - 1;offs >= 0;offs--)
 {
  int scroll; int sx; int sy; int col;
  sx = offs % 32;
  sy = offs / 32;
  if (colortype)
  {
   col = (wiz_attributesram2[2 * sx + 1] & 0x07);
  }
  else
  {
   col = (wiz_colorram2[offs] & 0x07);
  }
  scroll = (8*sy + 256 - wiz_attributesram2[2 * sx]) % 256;
  if (flipy)
  {
     scroll = (248 - scroll) % 256;
  }
  if (flipx) sx = 31 - sx;
  drawgfx(bitmap,Machine->gfx[char_bank[1]],
   wiz_videoram2[offs],
   col + 8 * palette_bank,
   flipx,flipy,
   8*sx,scroll,
   &Machine->visible_area,TRANSPARENCY_PEN,0);
 }
}

variable_types: colortype int

variable_types: offs int

variable_types: scroll int

variable_types: sx int

variable_types: sy int


decompiled_code:
__int64 __fastcall draw_sprites(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // r12

result = (__int64)&spriteram_size;
LODWORD(v4) = spriteram_size - 4;
if ( spriteram_size - 4 >= 0 )
{
v4 = (int)v4;
v5 = a3 + 64LL;
do
{
result = *(unsigned __int8 *)(a2 + v4 + 3);
if ( *(_BYTE *)(a2 + v4 + 3) )
{
if ( *(_BYTE *)(a2 + v4) )
result = drawgfx(
a1,
*(_QWORD *)(Machine + 8 * v5),
*(unsigned __int8 *)(a2 + v4 + 1),
(*(_BYTE *)(a2 + v4 + 2) & 7u) + 8 * palette_bank);
}
v4 -= 4LL;
}
while ( (int)v4 >= 0 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // r12

result = (__int64)&spriteram_size;
LODWORD(v4) = spriteram_size - 4;
if ( spriteram_size - 4 >= 0 )
{
v4 = (int)v4;
v5 = a3 + 64LL;
do
{
result = *(unsigned __int8 *)(a2 + v4 + 3);
if ( *(_BYTE *)(a2 + v4 + 3) )
{
if ( *(_BYTE *)(a2 + v4) )
result = drawgfx(
a1,
*(_QWORD *)(Machine + 8 * v5),
*(unsigned __int8 *)(a2 + v4 + 1),
(*(_BYTE *)(a2 + v4 + 2) & 7u) + 8 * palette_bank);
}
v4 -= 4LL;
}
while ( (int)v4 >= 0 );
}
return result;
}

original_code:
static void draw_sprites(struct osd_bitmap *bitmap, unsigned char* sprite_ram,
                         int bank, const struct rectangle* visible_area)
{
 int offs;
 for (offs = spriteram_size - 4;offs >= 0;offs -= 4)
 {
  int sx,sy;
  sx = sprite_ram[offs + 3];
  sy = sprite_ram[offs];
  if (!sx || !sy) continue;
  if ( flipx) sx = 240 - sx;
  if (!flipy) sy = 240 - sy;
  drawgfx(bitmap,Machine->gfx[bank],
    sprite_ram[offs + 1],
    (sprite_ram[offs + 2] & 0x07) + 8 * palette_bank,
    flipx,flipy,
    sx,sy,
    visible_area,TRANSPARENCY_PEN,0);
 }
}

canonical_original_code:
static void draw_sprites(struct osd_bitmap *bitmap, unsigned char *sprite_ram, int bank, struct rectangle *visible_area)
{
 int offs;
 for (offs = spriteram_size - 4;offs >= 0;offs -= 4)
 {
  int sx; int sy;
  sx = sprite_ram[offs + 3];
  sy = sprite_ram[offs];
  if (!sx || !sy) continue;
  if ( flipx) sx = 240 - sx;
  if (!flipy) sy = 240 - sy;
  drawgfx(bitmap,Machine->gfx[bank],
    sprite_ram[offs + 1],
    (sprite_ram[offs + 2] & 0x07) + 8 * palette_bank,
    flipx,flipy,
    sx,sy,
    visible_area,TRANSPARENCY_PEN,0);
 }
}


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

if ( a2 != 144 )
result = soundlatch_w(0LL);
return result;
}

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

if ( a2 != 144 )
result = soundlatch_w(0LL);
return result;
}

original_code:
static void sound_command_w(offs_t offset,data_t data)
{
 if (data == 0x90)
 {
 }
 else
  soundlatch_w(0,data);
}

canonical_original_code:
static void sound_command_w(unsigned int offset, unsigned int data)
{
 if (data == 0x90)
 {
 }
 else
  soundlatch_w(0,data);
}

variable_types:
offset
unsigned int


original_code:
static void SN76496Update(int chip,INT16 *buffer,int length)
{
 int i;
 struct SN76496 *R = &sn[chip];
 for (i = 0;i < 4;i++)
 {
  if (R->Volume[i] == 0)
  {
   if (R->Count[i] <= length*0x10000) R->Count[i] += length*0x10000;
  }
 }
 while (length > 0)
 {
  int vol[4];
  unsigned int out;
  int left;
  vol[0] = vol[1] = vol[2] = vol[3] = 0;
  for (i = 0;i < 3;i++)
  {
   if (R->Output[i]) vol[i] += R->Count[i];
   R->Count[i] -= 0x10000;
   while (R->Count[i] <= 0)
   {
    R->Count[i] += R->Period[i];
    if (R->Count[i] > 0)
    {
     R->Output[i] ^= 1;
     if (R->Output[i]) vol[i] += R->Period[i];
     break;
    }
    R->Count[i] += R->Period[i];
    vol[i] += R->Period[i];
   }
   if (R->Output[i]) vol[i] -= R->Count[i];
  }
  left = 0x10000;
  do
  {
   int nextevent;
   if (R->Count[3] < left) nextevent = R->Count[3];
   else nextevent = left;
   if (R->Output[3]) vol[3] += R->Count[3];
   R->Count[3] -= nextevent;
   if (R->Count[3] <= 0)
   {
    if (R->RNG & 1) R->RNG ^= R->NoiseFB;
    R->RNG >>= 1;
    R->Output[3] = R->RNG & 1;
    R->Count[3] += R->Period[3];
    if (R->Output[3]) vol[3] += R->Period[3];
   }
   if (R->Output[3]) vol[3] -= R->Count[3];
   left -= nextevent;
  } while (left > 0);
  out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] +
    vol[2] * R->Volume[2] + vol[3] * R->Volume[3];
  if (out > 0x7fff * 0x10000) out = 0x7fff * 0x10000;
  *(buffer++) = out / 0x10000;
  length--;
 }
}

canonical_original_code:
static void SN76496Update(int chip, short *buffer, int length)
{
 int i;
 struct SN76496 *R = &sn[chip];
 for (i = 0;i < 4;i++)
 {
  if (R->Volume[i] == 0)
  {
   if (R->Count[i] <= length*0x10000) R->Count[i] += length*0x10000;
  }
 }
 while (length > 0)
 {
  int vol[4];
  unsigned int out;
  int left;
  vol[0] = vol[1] = vol[2] = vol[3] = 0;
  for (i = 0;i < 3;i++)
  {
   if (R->Output[i]) vol[i] += R->Count[i];
   R->Count[i] -= 0x10000;
   while (R->Count[i] <= 0)
   {
    R->Count[i] += R->Period[i];
    if (R->Count[i] > 0)
    {
     R->Output[i] ^= 1;
     if (R->Output[i]) vol[i] += R->Period[i];
     break;
    }
    R->Count[i] += R->Period[i];
    vol[i] += R->Period[i];
   }
   if (R->Output[i]) vol[i] -= R->Count[i];
  }
  left = 0x10000;
  do
  {
   int nextevent;
   if (R->Count[3] < left) nextevent = R->Count[3];
   else nextevent = left;
   if (R->Output[3]) vol[3] += R->Count[3];
   R->Count[3] -= nextevent;
   if (R->Count[3] <= 0)
   {
    if (R->RNG & 1) R->RNG ^= R->NoiseFB;
    R->RNG >>= 1;
    R->Output[3] = R->RNG & 1;
    R->Count[3] += R->Period[3];
    if (R->Output[3]) vol[3] += R->Period[3];
   }
   if (R->Output[3]) vol[3] -= R->Count[3];
   left -= nextevent;
  } while (left > 0);
  out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] +
    vol[2] * R->Volume[2] + vol[3] * R->Volume[3];
  if (out > 0x7fff * 0x10000) out = 0x7fff * 0x10000;
  *(buffer++) = out / 0x10000;
  length--;
 }
}

variable_types: chip int

variable_types:
buffer
short *

variable_types: length int

variable_types: i int

variable_types: struct SN76496 { int Channel; int SampleRate; unsigned int UpdateStep; int VolTable[16]; int Register[8]; int LastRegister; int Volume[4]; unsigned int RNG; int NoiseFB; int Period[4]; int Count[4]; int Output[4]; } *

variable_types:
vol
int [4]

variable_types:
out
unsigned int

variable_types: left int

variable_types: nextevent int


decompiled_code:
__int64 __fastcall SN76496_set_clock(int a1, int a2)
{
int *v2; // rdi
__int64 result; // rax

v2 = (int *)((char *)&sn + 184 * a1);
result = (unsigned int)(int)(float)((float)((float)((float)v2[1] * 65536.0) * 16.0) / (float)a2);
v2[2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
int *v2; // rdi
__int64 result; // rax

v2 = (int *)((char *)&sn + 184 * a1);
result = (unsigned int)(int)(float)((float)((float)((float)v2[1] * 65536.0) * 16.0) / (float)a2);
v2[2] = result;
return result;
}

original_code:
static void SN76496_set_clock(int chip,int clock)
{
 struct SN76496 *R = &sn[chip];
 R->UpdateStep = ((float)0x10000 * R->SampleRate * 16) / clock;
}

canonical_original_code:
static void SN76496_set_clock(int chip, int clock)
{
 struct SN76496 *R = &sn[chip];
 R->UpdateStep = ((float)0x10000 * R->SampleRate * 16) / clock;
}

variable_types: clock int

user_defined_types: struct SN76496 { int Channel; int SampleRate; unsigned int UpdateStep; int VolTable[16]; int Register[8]; int LastRegister; int Volume[4]; unsigned int RNG; int NoiseFB; int Period[4]; int Count[4]; int Output[4]; }


original_code:
static void SN76496_set_gain(int chip,int gain)
{
 struct SN76496 *R = &sn[chip];
 int i;
 float out;
 gain &= 0xff;
 out = 0x7fff / 3;
 while (gain-- > 0)
  out *= 1.023292992;
 for (i = 0;i < 15;i++)
 {
  if (out > 0x7fff / 3) R->VolTable[i] = 0x7fff / 3;
  else R->VolTable[i] = out;
  out /= 1.258925412;
 }
 R->VolTable[15] = 0;
}

canonical_original_code:
static void SN76496_set_gain(int chip, int gain)
{
 struct SN76496 *R = &sn[chip];
 int i;
 float out;
 gain &= 0xff;
 out = 0x7fff / 3;
 while (gain-- > 0)
  out *= 1.023292992;
 for (i = 0;i < 15;i++)
 {
  if (out > 0x7fff / 3) R->VolTable[i] = 0x7fff / 3;
  else R->VolTable[i] = out;
  out /= 1.258925412;
 }
 R->VolTable[15] = 0;
}

variable_types: gain int

variable_types: out float


original_code:
static void SN76496Write(int chip,int data)
{
 struct SN76496 *R = &sn[chip];
 stream_update(R->Channel,0);
 if (data & 0x80)
 {
  int r = (data & 0x70) >> 4;
  int c = r/2;
  R->LastRegister = r;
  R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);
  switch (r)
  {
   case 0:
   case 2:
   case 4:
    R->Period[c] = R->UpdateStep * R->Register[r];
    if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;
    if (r == 4)
    {
     if ((R->Register[6] & 0x03) == 0x03)
      R->Period[3] = 2 * R->Period[2];
    }
    break;
   case 1:
   case 3:
   case 5:
   case 7:
    R->Volume[c] = R->VolTable[data & 0x0f];
    break;
   case 6:
    {
     int n = R->Register[6];
     R->NoiseFB = (n & 4) ? 0x12000 : 0x08000;
     n &= 3;
     R->Period[3] = (n == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5+n));
     R->RNG = 0x0f35;
     R->Output[3] = R->RNG & 1;
    }
    break;
  }
 }
 else
 {
  int r = R->LastRegister;
  int c = r/2;
  switch (r)
  {
   case 0:
   case 2:
   case 4:
    R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);
    R->Period[c] = R->UpdateStep * R->Register[r];
    if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;
    if (r == 4)
    {
     if ((R->Register[6] & 0x03) == 0x03)
      R->Period[3] = 2 * R->Period[2];
    }
    break;
  }
 }
}

canonical_original_code:
static void SN76496Write(int chip, int data)
{
 struct SN76496 *R = &sn[chip];
 stream_update(R->Channel,0);
 if (data & 0x80)
 {
  int r = (data & 0x70) >> 4;
  int c = r/2;
  R->LastRegister = r;
  R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);
  switch (r)
  {
   case 0:
   case 2:
   case 4:
    R->Period[c] = R->UpdateStep * R->Register[r];
    if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;
    if (r == 4)
    {
     if ((R->Register[6] & 0x03) == 0x03)
      R->Period[3] = 2 * R->Period[2];
    }
    break;
   case 1:
   case 3:
   case 5:
   case 7:
    R->Volume[c] = R->VolTable[data & 0x0f];
    break;
   case 6:
    {
     int n = R->Register[6];
     R->NoiseFB = (n & 4) ? 0x12000 : 0x08000;
     n &= 3;
     R->Period[3] = (n == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5+n));
     R->RNG = 0x0f35;
     R->Output[3] = R->RNG & 1;
    }
    break;
  }
 }
 else
 {
  int r = R->LastRegister;
  int c = r/2;
  switch (r)
  {
   case 0:
   case 2:
   case 4:
    R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);
    R->Period[c] = R->UpdateStep * R->Register[r];
    if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;
    if (r == 4)
    {
     if ((R->Register[6] & 0x03) == 0x03)
      R->Period[3] = 2 * R->Period[2];
    }
    break;
  }
 }
}

variable_types: data int

variable_types: r int

variable_types: c int

variable_types: n int


decompiled_code:
__int64 __fastcall SN76496_0_w(__int64 a1, int a2)
{
return SN76496Write(0, a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
return func3(0, a2);
}

original_code: void SN76496_0_w(offs_t offset,data_t data) { SN76496Write(0,data); }

canonical_original_code: void SN76496_0_w(unsigned int offset, unsigned int data) { SN76496Write(0,data); }

variable_types:
offset
unsigned int

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall SN76496_1_w(__int64 a1, int a2)
{
return SN76496Write(1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
return func3(1, a2);
}

original_code: void SN76496_1_w(offs_t offset,data_t data) { SN76496Write(1,data); }

canonical_original_code: void SN76496_1_w(unsigned int offset, unsigned int data) { SN76496Write(1,data); }


decompiled_code:
__int64 __fastcall SN76496_2_w(__int64 a1, int a2)
{
return SN76496Write(2, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
return func3(2, a2);
}

original_code: void SN76496_2_w(offs_t offset,data_t data) { SN76496Write(2,data); }

canonical_original_code: void SN76496_2_w(unsigned int offset, unsigned int data) { SN76496Write(2,data); }


decompiled_code:
__int64 __fastcall SN76496_3_w(__int64 a1, int a2)
{
return SN76496Write(3, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
return func3(3, a2);
}

original_code: void SN76496_3_w(offs_t offset,data_t data) { SN76496Write(3,data); }

canonical_original_code: void SN76496_3_w(unsigned int offset, unsigned int data) { SN76496Write(3,data); }


canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
int *v1; // r12
__int64 v2; // rbx
unsigned int v3; // ebp

v1 = *(int **)(a1 + 8);
if ( *v1 <= 0 )
return 0;
v2 = 0LL;
while ( 1 )
{
v3 = func4(a1, v2, v1[v2 + 1], LOBYTE(v1[v2 + 5]), *(_DWORD *)(Machine + 836LL));
if ( v3 )
break;
func2(v2, BYTE1(v1[v2 + 5]));
if ( *v1 <= (int)++v2 )
return v3;
}
return 1;
}

original_code:
int SN76496_sh_start(const struct MachineSound *msound)
{
 int chip;
 const struct SN76496interface *intf = (const struct SN76496interface *)msound->sound_interface;
 for (chip = 0;chip < intf->num;chip++)
 {
  if (SN76496_init(msound,chip,intf->baseclock[chip],intf->volume[chip] & 0xff,Machine->sample_rate) != 0)
   return 1;
  SN76496_set_gain(chip,(intf->volume[chip] >> 8) & 0xff);
 }
 return 0;
}

canonical_original_code:
int SN76496_sh_start(struct MachineSound *msound)
{
 int chip;
 struct SN76496interface *intf = (const struct SN76496interface *)msound->sound_interface;
 for (chip = 0;chip < intf->num;chip++)
 {
  if (SN76496_init(msound,chip,intf->baseclock[chip],intf->volume[chip] & 0xff,Machine->sample_rate) != 0)
   return 1;
  SN76496_set_gain(chip,(intf->volume[chip] >> 8) & 0xff);
 }
 return 0;
}

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

variable_types: chip int

variable_types: struct SN76496interface { int num; int baseclock[4]; int volume[4]; } *


decompiled_code:
__int64 __fastcall C140_r(__int16 a1)
{
return REG[a1 & 0x1FF];
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
return REG[a1 & 0x1FF];
}

original_code:
data_t C140_r(offs_t offset)
{
 offset&=0x1ff;
 return REG[offset];
}

canonical_original_code:
unsigned int C140_r(unsigned int offset)
{
 offset&=0x1ff;
 return REG[offset];
}

return_type: unsigned int


decompiled_code:
unsigned __int64 __fastcall C140_w(__int16 a1, unsigned int a2)
{
unsigned __int64 result; // rax
unsigned int v3; // eax
unsigned __int8 *v4; // rbx
__int64 v5; // rdi

if ( !fast_sound )
stream_update((unsigned int)stream, 0LL);
result = a1 & 0x1FF;
REG[result] = a2;
if ( (unsigned int)result <= 0x17F && (a1 & 0xF) == 5 )
{
v3 = (unsigned int)result >> 4;
if ( (a2 & 0x80u) == 0 )
{
result = (unsigned __int64)&voi[14 * v3];
*(_QWORD *)(result + 16) = 0LL;
}
else
{
v4 = &REG[a1 & 0x1F0];
voi[14 * v3 + 2] = 1LL;
voi[14 * v3] = 0LL;
voi[14 * v3 + 1] = 0LL;
voi[14 * v3 + 3] = 0LL;
voi[14 * v3 + 4] = 0LL;
voi[14 * v3 + 5] = 0LL;
voi[14 * v3 + 9] = v4[4];
voi[14 * v3 + 10] = a2;
voi[14 * v3 + 13] = v4[11] + (v4[10] << 8);
v5 = 14LL * v3;
voi[v5 + 11] = v4[7] + (v4[6] << 8);
result = v4[9] + (v4[8] << 8);
voi[v5 + 12] = result;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int16 a1, unsigned int a2)
{
unsigned __int64 result; // rax
unsigned int v3; // eax
unsigned __int8 *v4; // rbx
__int64 v5; // rdi

if ( !fast_sound )
stream_update((unsigned int)stream, 0LL);
result = a1 & 0x1FF;
REG[result] = a2;
if ( (unsigned int)result <= 0x17F && (a1 & 0xF) == 5 )
{
v3 = (unsigned int)result >> 4;
if ( (a2 & 0x80u) == 0 )
{
result = (unsigned __int64)&voi[14 * v3];
*(_QWORD *)(result + 16) = 0LL;
}
else
{
v4 = &REG[a1 & 0x1F0];
voi[14 * v3 + 2] = 1LL;
voi[14 * v3] = 0LL;
voi[14 * v3 + 1] = 0LL;
voi[14 * v3 + 3] = 0LL;
voi[14 * v3 + 4] = 0LL;
voi[14 * v3 + 5] = 0LL;
voi[14 * v3 + 9] = v4[4];
voi[14 * v3 + 10] = a2;
voi[14 * v3 + 13] = v4[11] + (v4[10] << 8);
v5 = 14LL * v3;
voi[v5 + 11] = v4[7] + (v4[6] << 8);
result = v4[9] + (v4[8] << 8);
voi[v5 + 12] = result;
}
}
return result;
}

original_code:
void C140_w(offs_t offset,data_t data)
{
    {
        extern int fast_sound;
        if (!fast_sound) stream_update(stream, 0);
    }
 offset&=0x1ff;
 REG[offset]=data;
 if( offset<0x180 )
 {
  VOICE *v = &voi[offset>>4];
  if( (offset&0xf)==0x5 )
  {
   if( data&0x80 )
   {
    const struct voice_registers *vreg = (struct voice_registers *) &REG[offset&0x1f0];
    v->key=1;
    v->ptoffset=0;
    v->pos=0;
    v->lastdt=0;
    v->prevdt=0;
    v->dltdt=0;
    v->bank = vreg->bank;
    v->mode = data;
    v->sample_loop = vreg->loop_msb*256 + vreg->loop_lsb;
    v->sample_start = vreg->start_msb*256 + vreg->start_lsb;
    v->sample_end = vreg->end_msb*256 + vreg->end_lsb;
   }
   else
   {
    v->key=0;
   }
  }
 }
}

canonical_original_code:
void C140_w(unsigned int offset, unsigned int data)
{
    {
        extern int fast_sound;
        if (!fast_sound) stream_update(stream, 0);
    }
 offset&=0x1ff;
 REG[offset]=data;
 if( offset<0x180 )
 {
  struct VOICE *v = &voi[offset>>4];
  if( (offset&0xf)==0x5 )
  {
   if( data&0x80 )
   {
    struct voice_registers *vreg = (struct voice_registers *) &REG[offset&0x1f0];
    v->key=1;
    v->ptoffset=0;
    v->pos=0;
    v->lastdt=0;
    v->prevdt=0;
    v->dltdt=0;
    v->bank = vreg->bank;
    v->mode = data;
    v->sample_loop = vreg->loop_msb*256 + vreg->loop_lsb;
    v->sample_start = vreg->start_msb*256 + vreg->start_lsb;
    v->sample_end = vreg->end_msb*256 + vreg->end_lsb;
   }
   else
   {
    v->key=0;
   }
  }
 }
}

variable_types: struct VOICE { long ptoffset; long pos; long key; long lastdt; long prevdt; long dltdt; long rvol; long lvol; long frequency; long bank; long mode; long sample_start; long sample_end; long sample_loop; } *

variable_types: struct voice_registers { unsigned char volume_right; unsigned char volume_left; unsigned char frequency_msb; unsigned char frequency_lsb; unsigned char bank; unsigned char mode; unsigned char start_msb; unsigned char start_lsb; unsigned char end_msb; unsigned char end_lsb; unsigned char loop_msb; unsigned char loop_lsb; unsigned char reserved[4]; } *


decompiled_code:
__int64 __fastcall C140_sh_start(__int64 a1)
{
unsigned int *v1; // rbx
int v2; // eax
int v3; // edx
__int64 v4; // rcx
__int64 v5; // rcx
int v6; // eax
_QWORD *v7; // rbx
__int64 v8; // rbx
char *v9; // rax
__int64 v11[3]; // [rsp+0h] [rbp-38h] BYREF
int v12[8]; // [rsp+18h] [rbp-20h] BYREF

v1 = *(unsigned int **)(a1 + 8);
v11[0] = (__int64)"C140 Left";
v11[1] = (__int64)"C140 Right";
v2 = *((unsigned __int8 *)v1 + 8);
v3 = v2;
BYTE1(v3) = 1;
v12[0] = v3;
BYTE1(v2) |= 2u;
v12[1] = v2;
v4 = *v1;
baserate = v4;
sample_rate = v4;
stream = ((__int64 (__fastcall *)(__int64, __int64 *, int *, __int64, _QWORD, _QWORD *(__fastcall *)(__int64, _QWORD *, int)))stream_init_multi)(
2LL,
v11,
v12,
v4,
0LL,
update_stereo);
pRom = memory_region(v1[1]);
v5 = 0LL;
v6 = 0;
do
{
pcmtbl[v5] = v6;
v6 += 16 << v5++;
}
while ( v5 != 8 );
memset(REG, 0, sizeof(REG));
v7 = voi;
do
{
init_voice(v7);
v7 += 14;
}
while ( v7 != &voi[336] );
v8 = sample_rate;
v9 = (char *)malloc(4LL * sample_rate);
mixer_buffer_left = v9;
if ( !v9 )
return 1LL;
mixer_buffer_right = &v9[2 * v8];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int *v1; // rbx
int v2; // eax
int v3; // edx
__int64 v4; // rcx
__int64 v5; // rcx
int v6; // eax
_QWORD *v7; // rbx
__int64 v8; // rbx
char *v9; // rax
__int64 v11[3]; // [rsp+0h] [rbp-38h] BYREF
int v12[8]; // [rsp+18h] [rbp-20h] BYREF

v1 = *(unsigned int **)(a1 + 8);
v11[0] = (__int64)"C140 Left";
v11[1] = (__int64)"C140 Right";
v2 = *((unsigned __int8 *)v1 + 8);
v3 = v2;
BYTE1(v3) = 1;
v12[0] = v3;
BYTE1(v2) |= 2u;
v12[1] = v2;
v4 = *v1;
baserate = v4;
sample_rate = v4;
stream = ((__int64 (__fastcall *)(__int64, __int64 *, int *, __int64, _QWORD, _QWORD *(__fastcall *)(__int64, _QWORD *, int)))stream_init_multi)(
2LL,
v11,
v12,
v4,
0LL,
update_stereo);
pRom = memory_region(v1[1]);
v5 = 0LL;
v6 = 0;
do
{
pcmtbl[v5] = v6;
v6 += 16 << v5++;
}
while ( v5 != 8 );
memset(REG, 0, sizeof(REG));
v7 = voi;
do
{
func1(v7);
v7 += 14;
}
while ( v7 != &voi[336] );
v8 = sample_rate;
v9 = (char *)malloc(4LL * sample_rate);
mixer_buffer_left = v9;
if ( !v9 )
return 1LL;
mixer_buffer_right = &v9[2 * v8];
return 0LL;
}

original_code:
int C140_sh_start( const struct MachineSound *msound )
{
 const struct C140interface *intf = (const struct C140interface *)msound->sound_interface;
 int vol[2];
 const char *stereo_names[2] = { "C140 Left", "C140 Right" };
 vol[0] = ((intf->mixing_level & 0xff) | ((1 & 0x03) << 8));
 vol[1] = ((intf->mixing_level & 0xff) | ((2 & 0x03) << 8));
 sample_rate=baserate=intf->frequency;
 stream = stream_init_multi(2,stereo_names,vol,sample_rate,0,update_stereo);
 pRom=memory_region(intf->region);
 {
  int i;
  INT32 segbase=0;
  for(i=0;i<8;i++)
  {
   pcmtbl[i]=segbase;
   segbase += 16<<i;
  }
 }
 memset(REG,0,0x200 );
 {
  int i;
  for(i=0;i<24;i++) init_voice( &voi[i] );
 }
 mixer_buffer_left = (INT16*)malloc(2 * sizeof(INT16)*sample_rate );
 if( mixer_buffer_left )
 {
  mixer_buffer_right = mixer_buffer_left + sample_rate;
  return 0;
 }
 return 1;
}

canonical_original_code:
int C140_sh_start(struct MachineSound *msound)
{
 struct C140interface *intf = (const struct C140interface *)msound->sound_interface;
 int vol[2];
 char *stereo_names[2] = { "C140 Left", "C140 Right" };
 vol[0] = ((intf->mixing_level & 0xff) | ((1 & 0x03) << 8));
 vol[1] = ((intf->mixing_level & 0xff) | ((2 & 0x03) << 8));
 sample_rate=baserate=intf->frequency;
 stream = stream_init_multi(2,stereo_names,vol,sample_rate,0,update_stereo);
 pRom=memory_region(intf->region);
 {
  int i;
  int segbase = 0;
  for(i=0;i<8;i++)
  {
   pcmtbl[i]=segbase;
   segbase += 16<<i;
  }
 }
 memset(REG,0,0x200 );
 {
  int i;
  for(i=0;i<24;i++) init_voice( &voi[i] );
 }
 mixer_buffer_left = (short *)malloc(2 * sizeof(INT16)*sample_rate );
 if( mixer_buffer_left )
 {
  mixer_buffer_right = mixer_buffer_left + sample_rate;
  return 0;
 }
 return 1;
}

variable_types: struct C140interface { int frequency; int region; int mixing_level; } *

variable_types:
vol
int [2]

variable_types: i int

variable_types: segbase int


decompiled_code:
void C140_sh_stop()
{
free(mixer_buffer_left);
}

canonical_decompiled_code:
void func6()
{
free(mixer_buffer_left);
}

original_code:
void C140_sh_stop( void )
{
 free( mixer_buffer_left );
}

canonical_original_code:
void C140_sh_stop()
{
 free( mixer_buffer_left );
}


original_code:
static long find_sample( long adrs, long bank)
{
 adrs=(bank<<16)+adrs;
 return ((adrs&0x200000)>>2)|(adrs&0x7ffff);
}

canonical_original_code:
static long find_sample(long adrs, long bank)
{
 adrs=(bank<<16)+adrs;
 return ((adrs&0x200000)>>2)|(adrs&0x7ffff);
}

variable_types: adrs long

variable_types: bank long

return_type: long


decompiled_code:
void arkanoid_init_machine()
{
m68705write = 0;
z80write = 0;
portA_out = 0;
portA_in = 0;
}

canonical_decompiled_code:
void func0()
{
m68705write = 0;
z80write = 0;
portA_out = 0;
portA_in = 0;
}

original_code:
void arkanoid_init_machine(void)
{
 portA_in = portA_out = z80write = m68705write = 0;
}

canonical_original_code:
void arkanoid_init_machine()
{
 portA_in = portA_out = z80write = m68705write = 0;
}


decompiled_code:
__int64 arkanoid_Z80_mcu_r()
{
m68705write = 0;
return (unsigned int)toz80;
}

canonical_decompiled_code:
__int64 func1()
{
m68705write = 0;
return (unsigned int)toz80;
}

original_code:
data_t arkanoid_Z80_mcu_r(offs_t offset)
{
 m68705write = 0;
 return toz80;
}

canonical_original_code:
unsigned int arkanoid_Z80_mcu_r(unsigned int offset)
{
 m68705write = 0;
 return toz80;
}


decompiled_code:
__int64 arkanoid_68705_portA_r()
{
return ~(unsigned __int8)ddrA & (unsigned __int8)portA_in | (unsigned int)(unsigned __int8)(portA_out & ddrA);
}

canonical_decompiled_code:
__int64 func2()
{
return ~(unsigned __int8)ddrA & (unsigned __int8)portA_in | (unsigned int)(unsigned __int8)(portA_out & ddrA);
}

original_code:
data_t arkanoid_68705_portA_r(offs_t offset)
{
 return (portA_out & ddrA) | (portA_in & ~ddrA);
}

canonical_original_code:
unsigned int arkanoid_68705_portA_r(unsigned int offset)
{
 return (portA_out & ddrA) | (portA_in & ~ddrA);
}


decompiled_code:
void __fastcall arkanoid_68705_portA_w(__int64 a1, char a2)
{
portA_out = a2;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, char a2)
{
portA_out = a2;
}

original_code:
void arkanoid_68705_portA_w(offs_t offset,data_t data)
{
 portA_out = data;
}

canonical_original_code:
void arkanoid_68705_portA_w(unsigned int offset, unsigned int data)
{
 portA_out = data;
}


decompiled_code:
void __fastcall arkanoid_68705_ddrA_w(__int64 a1, char a2)
{
ddrA = a2;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, char a2)
{
ddrA = a2;
}

original_code:
void arkanoid_68705_ddrA_w(offs_t offset,data_t data)
{
 ddrA = data;
}

canonical_original_code:
void arkanoid_68705_ddrA_w(unsigned int offset, unsigned int data)
{
 ddrA = data;
}


decompiled_code:
__int64 arkanoid_68705_portC_r()
{
int v0; // ecx

v0 = z80write != 0;
if ( !m68705write )
v0 |= 2u;
return (unsigned __int8)(portC_out & ddrC) | v0 & ~(unsigned __int8)ddrC;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // ecx

v0 = z80write != 0;
if ( !m68705write )
v0 |= 2u;
return (unsigned __int8)(portC_out & ddrC) | v0 & ~(unsigned __int8)ddrC;
}

original_code:
data_t arkanoid_68705_portC_r(offs_t offset)
{
 int res=0;
 if (z80write) res |= 0x01;
 if (!m68705write) res |= 0x02;
 return (portC_out & ddrC) | (res & ~ddrC);
}

canonical_original_code:
unsigned int arkanoid_68705_portC_r(unsigned int offset)
{
 int res = 0;
 if (z80write) res |= 0x01;
 if (!m68705write) res |= 0x02;
 return (portC_out & ddrC) | (res & ~ddrC);
}

variable_types: res int


decompiled_code:
void __fastcall arkanoid_68705_ddrC_w(__int64 a1, char a2)
{
ddrC = a2;
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, char a2)
{
ddrC = a2;
}

original_code:
void arkanoid_68705_ddrC_w(offs_t offset,data_t data)
{
 ddrC = data;
}

canonical_original_code:
void arkanoid_68705_ddrC_w(unsigned int offset, unsigned int data)
{
 ddrC = data;
}


original_code:
void arkanoid_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
{
 int i;
 for (i = 0;i < Machine->drv->total_colors;i++)
 {
  int bit0,bit1,bit2,bit3;
  bit0 = (color_prom[0] >> 0) & 0x01;
  bit1 = (color_prom[0] >> 1) & 0x01;
  bit2 = (color_prom[0] >> 2) & 0x01;
  bit3 = (color_prom[0] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  color_prom++;
 }
}

canonical_original_code:
void arkanoid_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable, unsigned char *color_prom)
{
 int i;
 for (i = 0;i < Machine->drv->total_colors;i++)
 {
  int bit0; int bit1; int bit2; int bit3;
  bit0 = (color_prom[0] >> 0) & 0x01;
  bit1 = (color_prom[0] >> 1) & 0x01;
  bit2 = (color_prom[0] >> 2) & 0x01;
  bit3 = (color_prom[0] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  color_prom++;
 }
}

variable_types:
palette
unsigned char *

variable_types:
colortable
unsigned short *


decompiled_code:
__int64 __fastcall arkanoid_Z80_mcu_w(__int64 a1, int a2)
{
z80write = 1;
fromz80 = a2;
return cpu_spinuntil_trigger(700LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
z80write = 1;
fromz80 = a2;
return cpu_spinuntil_trigger(700LL);
}

original_code:
void arkanoid_Z80_mcu_w(offs_t offset,data_t data)
{
 z80write = 1;
 fromz80 = data;
 cpu_spinuntil_trigger(700);
}

canonical_original_code:
void arkanoid_Z80_mcu_w(unsigned int offset, unsigned int data)
{
 z80write = 1;
 fromz80 = data;
 cpu_spinuntil_trigger(700);
}


decompiled_code:
__int64 __fastcall arkanoid_68705_portC_w(__int64 a1, char a2)
{
__int64 result; // rax

if ( (ddrC & 4) != 0 && (a2 & 4) == 0 && (portC_out & 4) != 0 )
{
cpu_trigger(700LL);
z80write = 0;
result = (unsigned int)fromz80;
portA_in = fromz80;
}
if ( (ddrC & 8) != 0 && (a2 & 8) == 0 && (portC_out & 8) != 0 )
{
m68705write = 1;
result = (unsigned __int8)portA_out;
toz80 = (unsigned __int8)portA_out;
}
portC_out = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2)
{
__int64 result; // rax

if ( (ddrC & 4) != 0 && (a2 & 4) == 0 && (portC_out & 4) != 0 )
{
cpu_trigger(700LL);
z80write = 0;
result = (unsigned int)fromz80;
portA_in = fromz80;
}
if ( (ddrC & 8) != 0 && (a2 & 8) == 0 && (portC_out & 8) != 0 )
{
m68705write = 1;
result = (unsigned __int8)portA_out;
toz80 = (unsigned __int8)portA_out;
}
portC_out = a2;
return result;
}

original_code:
void arkanoid_68705_portC_w(offs_t offset,data_t data)
{
 if ((ddrC & 0x04) && (~data & 0x04) && (portC_out & 0x04))
 {
  cpu_trigger(700);
  z80write = 0;
  portA_in = fromz80;
 }
 if ((ddrC & 0x08) && (~data & 0x08) && (portC_out & 0x08))
 {
  m68705write = 1;
  toz80 = portA_out;
 }
 portC_out = data;
}

canonical_original_code:
void arkanoid_68705_portC_w(unsigned int offset, unsigned int data)
{
 if ((ddrC & 0x04) && (~data & 0x04) && (portC_out & 0x04))
 {
  cpu_trigger(700);
  z80write = 0;
  portA_in = fromz80;
 }
 if ((ddrC & 0x08) && (~data & 0x08) && (portC_out & 0x08))
 {
  m68705write = 1;
  toz80 = portA_out;
 }
 portC_out = data;
}


decompiled_code:
char arkanoid_68705_input_0_r()
{
__int64 v0; // rax

v0 = input_port_0_r() & 0x3F;
if ( !z80write )
LOBYTE(v0) = v0 | 0x40;
if ( !m68705write )
LOBYTE(v0) = v0 | 0x80;
return v0;
}

canonical_decompiled_code:
char func10()
{
__int64 v0; // rax

v0 = input_port_0_r() & 0x3F;
if ( !z80write )
LOBYTE(v0) = v0 | 0x40;
if ( !m68705write )
LOBYTE(v0) = v0 | 0x80;
return v0;
}

original_code:
data_t arkanoid_68705_input_0_r(offs_t offset)
{
 int res = input_port_0_r(offset) & 0x3f;
 if (!z80write) res |= 0x40;
 if (!m68705write) res |= 0x80;
 return res;
}

canonical_original_code:
unsigned int arkanoid_68705_input_0_r(unsigned int offset)
{
 int res = input_port_0_r(offset) & 0x3f;
 if (!z80write) res |= 0x40;
 if (!m68705write) res |= 0x80;
 return res;
}


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

if ( arkanoid_paddle_select )
result = input_port_3_r();
else
result = input_port_2_r();
return result;
}

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

if ( arkanoid_paddle_select )
result = input_port_3_r();
else
result = input_port_2_r();
return result;
}

original_code:
data_t arkanoid_input_2_r(offs_t offset)
{
 if (arkanoid_paddle_select)
 {
  return input_port_3_r(offset);
 }
 else
 {
  return input_port_2_r(offset);
 }
}

canonical_original_code:
unsigned int arkanoid_input_2_r(unsigned int offset)
{
 if (arkanoid_paddle_select)
 {
  return input_port_3_r(offset);
 }
 else
 {
  return input_port_2_r(offset);
 }
}


decompiled_code:
__int64 __fastcall arkanoid_d008_w(__int64 a1, unsigned int a2)
{
flip_screen_x_w(a1, a2 & 1);
flip_screen_y_w((unsigned int)a1, a2 & 2);
arkanoid_paddle_select = a2 & 4;
coin_lockout_w(0LL, ((unsigned __int8)(a2 >> 3) ^ 1) & 1);
coin_lockout_w(1LL, ((unsigned __int8)(a2 >> 3) ^ 1) & 1);
set_vh_global_attribute(&gfxbank, (a2 >> 5) & 1);
return set_vh_global_attribute(&palettebank, (a2 & 0x40) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, unsigned int a2)
{
flip_screen_x_w(a1, a2 & 1);
flip_screen_y_w((unsigned int)a1, a2 & 2);
arkanoid_paddle_select = a2 & 4;
coin_lockout_w(0LL, ((unsigned __int8)(a2 >> 3) ^ 1) & 1);
coin_lockout_w(1LL, ((unsigned __int8)(a2 >> 3) ^ 1) & 1);
set_vh_global_attribute(&gfxbank, (a2 >> 5) & 1);
return set_vh_global_attribute(&palettebank, (a2 & 0x40) != 0);
}

original_code:
void arkanoid_d008_w(offs_t offset,data_t data)
{
 flip_screen_x_w(offset, data & 0x01);
 flip_screen_y_w(offset, data & 0x02);
    arkanoid_paddle_select = data & 0x04;
 coin_lockout_w(0, !(data & 0x08));
 coin_lockout_w(1, !(data & 0x08));
 set_vh_global_attribute(&gfxbank, (data & 0x20) >> 5);
 set_vh_global_attribute(&palettebank, (data & 0x40) >> 6);
}

canonical_original_code:
void arkanoid_d008_w(unsigned int offset, unsigned int data)
{
 flip_screen_x_w(offset, data & 0x01);
 flip_screen_y_w(offset, data & 0x02);
    arkanoid_paddle_select = data & 0x04;
 coin_lockout_w(0, !(data & 0x08));
 coin_lockout_w(1, !(data & 0x08));
 set_vh_global_attribute(&gfxbank, (data & 0x20) >> 5);
 set_vh_global_attribute(&palettebank, (data & 0x40) >> 6);
}


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

if ( a1 )
result = layer_colorbase[a1] + ((*a4 >> 2) & 0x1Fu);
else
result = layer_colorbase[0] + ((*a4 >> 4) & 0xFu);
*a4 = result;
return result;
}

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

if ( a1 )
result = layer_colorbase[a1] + ((*a4 >> 2) & 0x1Fu);
else
result = layer_colorbase[0] + ((*a4 >> 4) & 0xFu);
*a4 = result;
return result;
}

original_code:
static void xmen_tile_callback(int layer,int bank,int *code,int *color)
{
 if (layer == 0)
  *color = layer_colorbase[layer] + ((*color & 0xf0) >> 4);
 else
  *color = layer_colorbase[layer] + ((*color & 0x7c) >> 2);
}

canonical_original_code:
static void xmen_tile_callback(int layer, int bank, int *code, int *color)
{
 if (layer == 0)
  *color = layer_colorbase[layer] + ((*color & 0xf0) >> 4);
 else
  *color = layer_colorbase[layer] + ((*color & 0x7c) >> 2);
}

variable_types: layer int

variable_types: bank int

variable_types:
code
int *

variable_types:
color
int *


decompiled_code:
__int64 __fastcall xmen_sprite_callback(__int64 a1, int *a2, _DWORD *a3)
{
int v3; // eax
__int64 result; // rax

v3 = (*a2 >> 4) & 0xE;
if ( dword_5D8 < v3 )
{
if ( dword_5D4 < v3 )
*a3 = 2 * (layerpri < v3) + 252;
else
*a3 = 240;
}
else
{
*a3 = 0;
}
result = sprite_colorbase + (*a2 & 0x1Fu);
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int *a2, _DWORD *a3)
{
int v3; // eax
__int64 result; // rax

v3 = (*a2 >> 4) & 0xE;
if ( dword_5D8 < v3 )
{
if ( dword_5D4 < v3 )
*a3 = 2 * (layerpri < v3) + 252;
else
*a3 = 240;
}
else
{
*a3 = 0;
}
result = sprite_colorbase + (*a2 & 0x1Fu);
*a2 = result;
return result;
}

original_code:
static void xmen_sprite_callback(int *code,int *color,int *priority_mask)
{
 int pri = (*color & 0x00e0) >> 4;
 if (pri <= layerpri[2]) *priority_mask = 0;
 else if (pri > layerpri[2] && pri <= layerpri[1]) *priority_mask = 0xf0;
 else if (pri > layerpri[1] && pri <= layerpri[0]) *priority_mask = 0xf0|0xcc;
 else *priority_mask = 0xf0|0xcc|0xaa;
 *color = sprite_colorbase + (*color & 0x001f);
}

canonical_original_code:
static void xmen_sprite_callback(int *code, int *color, int *priority_mask)
{
 int pri = (*color & 0x00e0) >> 4;
 if (pri <= layerpri[2]) *priority_mask = 0;
 else if (pri > layerpri[2] && pri <= layerpri[1]) *priority_mask = 0xf0;
 else if (pri > layerpri[1] && pri <= layerpri[0]) *priority_mask = 0xf0|0xcc;
 else *priority_mask = 0xf0|0xcc|0xaa;
 *color = sprite_colorbase + (*color & 0x001f);
}

variable_types: pri int


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

v0 = K052109_vh_start(137LL, 0LL, 1LL, 2LL, 3LL, xmen_tile_callback);
result = 1LL;
if ( !v0 )
{
result = K053247_vh_start(138LL, 0LL, 1LL, 2LL, 3LL, xmen_sprite_callback);
if ( (_DWORD)result )
{
K052109_vh_stop();
result = 1LL;
}
}
return result;
}

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

v0 = K052109_vh_start(137LL, 0LL, 1LL, 2LL, 3LL, xmen_tile_callback);
result = 1LL;
if ( !v0 )
{
result = K053247_vh_start(138LL, 0LL, 1LL, 2LL, 3LL, xmen_sprite_callback);
if ( (_DWORD)result )
{
K052109_vh_stop();
result = 1LL;
}
}
return result;
}

original_code:
int xmen_vh_start(void)
{
 if (K052109_vh_start(REGION_GFX1,0,1,2,3,xmen_tile_callback))
  return 1;
 if (K053247_vh_start(REGION_GFX2,0,1,2,3,xmen_sprite_callback))
 {
  K052109_vh_stop();
  return 1;
 }
 return 0;
}

canonical_original_code:
int xmen_vh_start()
{
 if (K052109_vh_start(REGION_GFX1,0,1,2,3,xmen_tile_callback))
  return 1;
 if (K053247_vh_start(REGION_GFX2,0,1,2,3,xmen_sprite_callback))
 {
  K052109_vh_stop();
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 xmen_vh_stop()
{
K052109_vh_stop();
return K053247_vh_stop();
}

original_code:
void xmen_vh_stop(void)
{
 K052109_vh_stop();
 K053247_vh_stop();
}

canonical_original_code:
void xmen_vh_stop()
{
 K052109_vh_stop();
 K053247_vh_stop();
}


decompiled_code:
__int64 __fastcall xmen_vh_screenrefresh(__int64 a1)
{
unsigned int v2; // [rsp+4h] [rbp-14h] BYREF
unsigned int v3; // [rsp+8h] [rbp-10h]
unsigned int v4; // [rsp+Ch] [rbp-Ch]

bg_colorbase = K053251_get_palette_index(4LL);
sprite_colorbase = K053251_get_palette_index(1LL);
layer_colorbase[0] = K053251_get_palette_index(3LL);
dword_5EC = K053251_get_palette_index(0LL);
dword_5F0 = K053251_get_palette_index(2LL);
K052109_tilemap_update();
palette_init_used_colors();
K053247_mark_sprites_colors();
*(_BYTE *)(palette_used_colors + 16 * bg_colorbase + 1LL) |= 1u;
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
v2 = 0;
layerpri = K053251_get_priority(3LL);
v3 = 1;
dword_5D4 = K053251_get_priority(0LL);
v4 = 2;
dword_5D8 = K053251_get_priority(2LL);
sortlayers((int *)&v2, &layerpri);
fillbitmap(priority_bitmap, 0LL, 0LL);
fillbitmap(a1, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 32LL * bg_colorbase + 2), Machine + 776LL);
K052109_tilemap_draw(a1, v2, 0x10000LL);
K052109_tilemap_draw(a1, v3, 0x20000LL);
K052109_tilemap_draw(a1, v4, 0x40000LL);
return K053247_sprites_draw(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int v2; // [rsp+4h] [rbp-14h] BYREF
unsigned int v3; // [rsp+8h] [rbp-10h]
unsigned int v4; // [rsp+Ch] [rbp-Ch]

bg_colorbase = K053251_get_palette_index(4LL);
sprite_colorbase = K053251_get_palette_index(1LL);
layer_colorbase[0] = K053251_get_palette_index(3LL);
dword_5EC = K053251_get_palette_index(0LL);
dword_5F0 = K053251_get_palette_index(2LL);
K052109_tilemap_update();
palette_init_used_colors();
K053247_mark_sprites_colors();
*(_BYTE *)(palette_used_colors + 16 * bg_colorbase + 1LL) |= 1u;
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
v2 = 0;
layerpri = K053251_get_priority(3LL);
v3 = 1;
dword_5D4 = K053251_get_priority(0LL);
v4 = 2;
dword_5D8 = K053251_get_priority(2LL);
func2((int *)&v2, &layerpri);
fillbitmap(priority_bitmap, 0LL, 0LL);
fillbitmap(a1, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 32LL * bg_colorbase + 2), Machine + 776LL);
K052109_tilemap_draw(a1, v2, 0x10000LL);
K052109_tilemap_draw(a1, v3, 0x20000LL);
K052109_tilemap_draw(a1, v4, 0x40000LL);
return K053247_sprites_draw(a1);
}

original_code:
void xmen_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int layer[3];
 bg_colorbase = K053251_get_palette_index(K053251_CI4);
 sprite_colorbase = K053251_get_palette_index(K053251_CI1);
 layer_colorbase[0] = K053251_get_palette_index(K053251_CI3);
 layer_colorbase[1] = K053251_get_palette_index(K053251_CI0);
 layer_colorbase[2] = K053251_get_palette_index(K053251_CI2);
 K052109_tilemap_update();
 palette_init_used_colors();
 K053247_mark_sprites_colors();
 palette_used_colors[16 * bg_colorbase+1] |= 1;
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 layer[0] = 0;
 layerpri[0] = K053251_get_priority(K053251_CI3);
 layer[1] = 1;
 layerpri[1] = K053251_get_priority(K053251_CI0);
 layer[2] = 2;
 layerpri[2] = K053251_get_priority(K053251_CI2);
 sortlayers(layer,layerpri);
 fillbitmap(priority_bitmap,0,((void *)0));
 fillbitmap(bitmap,Machine->pens[16 * bg_colorbase+1],&Machine->visible_area);
 K052109_tilemap_draw(bitmap,layer[0],1<<16);
 K052109_tilemap_draw(bitmap,layer[1],2<<16);
 K052109_tilemap_draw(bitmap,layer[2],4<<16);
 K053247_sprites_draw(bitmap);
}

canonical_original_code:
void xmen_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int layer[3];
 bg_colorbase = K053251_get_palette_index(K053251_CI4);
 sprite_colorbase = K053251_get_palette_index(K053251_CI1);
 layer_colorbase[0] = K053251_get_palette_index(K053251_CI3);
 layer_colorbase[1] = K053251_get_palette_index(K053251_CI0);
 layer_colorbase[2] = K053251_get_palette_index(K053251_CI2);
 K052109_tilemap_update();
 palette_init_used_colors();
 K053247_mark_sprites_colors();
 palette_used_colors[16 * bg_colorbase+1] |= 1;
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 layer[0] = 0;
 layerpri[0] = K053251_get_priority(K053251_CI3);
 layer[1] = 1;
 layerpri[1] = K053251_get_priority(K053251_CI0);
 layer[2] = 2;
 layerpri[2] = K053251_get_priority(K053251_CI2);
 sortlayers(layer,layerpri);
 fillbitmap(priority_bitmap,0,((void *)0));
 fillbitmap(bitmap,Machine->pens[16 * bg_colorbase+1],&Machine->visible_area);
 K052109_tilemap_draw(bitmap,layer[0],1<<16);
 K052109_tilemap_draw(bitmap,layer[1],2<<16);
 K052109_tilemap_draw(bitmap,layer[2],4<<16);
 K053247_sprites_draw(bitmap);
}

variable_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *

variable_types:
layer
int [3]


decompiled_code:
__int64 init_xmen()
{
konami_rom_deinterleave_2(137LL);
return konami_rom_deinterleave_4(138LL);
}

canonical_decompiled_code:
__int64 func6()
{
konami_rom_deinterleave_2(137LL);
return konami_rom_deinterleave_4(138LL);
}

original_code:
static void init_xmen(void)
{
 konami_rom_deinterleave_2(REGION_GFX1);
 konami_rom_deinterleave_4(REGION_GFX2);
}

canonical_original_code:
static void init_xmen()
{
 konami_rom_deinterleave_2(REGION_GFX1);
 konami_rom_deinterleave_4(REGION_GFX2);
}


original_code:
static void init_xmen6p(void)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 (*(UINT16 *)(&RAM[0x21a6]) = (0x4e71));
 (*(UINT16 *)(&RAM[0x21a8]) = (0x4e71));
 (*(UINT16 *)(&RAM[0x21aa]) = (0x4e71));
 init_xmen();
}

canonical_original_code:
static void init_xmen6p()
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 (*(unsigned short *)(&RAM[0x21a6]) = (0x4e71));
 (*(unsigned short *)(&RAM[0x21a8]) = (0x4e71));
 (*(unsigned short *)(&RAM[0x21aa]) = (0x4e71));
 init_xmen();
}

variable_types:
RAM
unsigned char *


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

if ( (unsigned int)cpu_getiloops() )
result = m68_level3_irq();
else
result = m68_level5_irq();
return result;
}

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

if ( (unsigned int)cpu_getiloops() )
result = m68_level3_irq();
else
result = m68_level5_irq();
return result;
}

original_code:
static int xmen_interrupt(void)
{
 if (cpu_getiloops() == 0) return m68_level5_irq();
 else return m68_level3_irq();
}

canonical_original_code:
static int xmen_interrupt()
{
 if (cpu_getiloops() == 0) return m68_level5_irq();
 else return m68_level3_irq();
}


decompiled_code:
__int64 __fastcall xmen_18fa00_w(__int64 a1, char a2)
{
__int64 result; // rax

if ( !(_DWORD)a1 )
result = interrupt_enable_w(a1, a2 & 4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, char a2)
{
__int64 result; // rax

if ( !(_DWORD)a1 )
result = interrupt_enable_w(a1, a2 & 4);
return result;
}

original_code:
static void xmen_18fa00_w(offs_t offset,data_t data)
{
 if (offset == 0)
 {
  interrupt_enable_w(0,data & 0x04);
 }
}

canonical_original_code:
static void xmen_18fa00_w(unsigned int offset, unsigned int data)
{
 if (offset == 0)
 {
  interrupt_enable_w(0,data & 0x04);
 }
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall K053251_halfword_w(unsigned int a1, int a2)
{
__int64 result; // rax

if ( (a2 & 0xFF0000) == 0 )
result = K053251_w(a1 >> 1, (unsigned __int8)a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int a1, int a2)
{
__int64 result; // rax

if ( (a2 & 0xFF0000) == 0 )
result = K053251_w(a1 >> 1, (unsigned __int8)a2);
return result;
}

original_code:
static void K053251_halfword_w(offs_t offset,data_t data)
{
 if ((data & 0x00ff0000) == 0)
  K053251_w(offset >> 1,data & 0xff);
}

canonical_original_code:
static void K053251_halfword_w(unsigned int offset, unsigned int data)
{
 if ((data & 0x00ff0000) == 0)
  K053251_w(offset >> 1,data & 0xff);
}


decompiled_code:
__int64 __fastcall K052109_halfword_r(unsigned int a1)
{
return K052109_r(a1 >> 1);
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int a1)
{
return K052109_r(a1 >> 1);
}

original_code:
static data_t K052109_halfword_r(offs_t offset)
{
 return K052109_r(offset >> 1);
}

canonical_original_code:
static unsigned int K052109_halfword_r(unsigned int offset)
{
 return K052109_r(offset >> 1);
}


decompiled_code:
void *__fastcall sound_bankswitch_w(__int64 a1, char a2)
{
void *result; // rax
unsigned int v3; // eax

cpu_bankbase[4] = memory_region(130LL) + (((a2 & 7) + 4) << 14);
result = &ophw;
if ( ophw == 4 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v3);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func9(__int64 a1, char a2)
{
void *result; // rax
unsigned int v3; // eax

cpu_bankbase[4] = memory_region(130LL) + (((a2 & 7) + 4) << 14);
result = &ophw;
if ( ophw == 4 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v3);
}
return result;
}

original_code:
static void sound_bankswitch_w(offs_t offset,data_t data)
{
 int bankaddress;
 unsigned char *RAM = memory_region(REGION_CPU2);
 bankaddress = 0x10000 + (data & 0x07) * 0x4000;
 { if (4 >= 1 && 4 <= 16) { cpu_bankbase[4] = (UINT8 *)(&RAM[bankaddress]); if (ophw == 4) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

canonical_original_code:
static void sound_bankswitch_w(unsigned int offset, unsigned int data)
{
 int bankaddress;
 unsigned char *RAM = memory_region(REGION_CPU2);
 bankaddress = 0x10000 + (data & 0x07) * 0x4000;
 { if (4 >= 1 && 4 <= 16) { cpu_bankbase[4] = (unsigned char *)(&RAM[bankaddress]); if (ophw == 4) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

variable_types: bankaddress int


decompiled_code:
__int64 __fastcall sound_cmd_w(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

if ( !(_DWORD)a1 )
{
soundlatch_w(a1, a2);
result = Machine;
if ( !*(_DWORD *)(Machine + 836LL) && (a2 & 0xFD) == 252 )
result = soundlatch2_w(0LL, 127LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

if ( !(_DWORD)a1 )
{
soundlatch_w(a1, a2);
result = Machine;
if ( !*(_DWORD *)(Machine + 836LL) && (a2 & 0xFD) == 252 )
result = soundlatch2_w(0LL, 127LL);
}
return result;
}

original_code:
static void sound_cmd_w(offs_t offset,data_t data)
{
 if (offset == 0)
 {
  data &= 0xff;
  soundlatch_w(0, data);
  if(!Machine->sample_rate)
   if(data == 0xfc || data == 0xfe)
    soundlatch2_w(0, 0x7f);
 }
}

canonical_original_code:
static void sound_cmd_w(unsigned int offset, unsigned int data)
{
 if (offset == 0)
 {
  data &= 0xff;
  soundlatch_w(0, data);
  if(!Machine->sample_rate)
   if(data == 0xfc || data == 0xfe)
    soundlatch2_w(0, 0x7f);
 }
}


decompiled_code:
__int64 __fastcall eeprom_w(__int64 a1, unsigned int a2)
{
__int64 result; // rax

if ( (a2 & 0xFF0000) != 0 )
{
K053246_set_OBJCHA_line((a2 >> 8) & 1);
result = K052109_set_RMRD_line((a2 & 0x200) != 0);
}
else
{
coin_counter_w(0LL, a2 & 1);
EEPROM_write_bit(a2 & 4);
EEPROM_set_cs_line(((unsigned __int8)(a2 >> 4) ^ 1) & 1);
result = EEPROM_set_clock_line((a2 & 8) != 0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, unsigned int a2)
{
__int64 result; // rax

if ( (a2 & 0xFF0000) != 0 )
{
K053246_set_OBJCHA_line((a2 >> 8) & 1);
result = K052109_set_RMRD_line((a2 & 0x200) != 0);
}
else
{
coin_counter_w(0LL, a2 & 1);
EEPROM_write_bit(a2 & 4);
EEPROM_set_cs_line(((unsigned __int8)(a2 >> 4) ^ 1) & 1);
result = EEPROM_set_clock_line((a2 & 8) != 0);
}
return result;
}

original_code:
static void eeprom_w(offs_t offset,data_t data)
{
 if ((data & 0x00ff0000) == 0)
 {
  coin_counter_w(0,data & 0x01);
  EEPROM_write_bit(data & 0x04);
  EEPROM_set_cs_line((data & 0x10) ? 0 : 1);
  EEPROM_set_clock_line((data & 0x08) ? 1 : 0);
 }
 else
 {
  K053246_set_OBJCHA_line((data & 0x0100) ? 1 : 0);
  K052109_set_RMRD_line((data & 0x0200) ? 1 : 0);
 }
}

canonical_original_code:
static void eeprom_w(unsigned int offset, unsigned int data)
{
 if ((data & 0x00ff0000) == 0)
 {
  coin_counter_w(0,data & 0x01);
  EEPROM_write_bit(data & 0x04);
  EEPROM_set_cs_line((data & 0x10) ? 0 : 1);
  EEPROM_set_clock_line((data & 0x08) ? 1 : 0);
 }
 else
 {
  K053246_set_OBJCHA_line((data & 0x0100) ? 1 : 0);
  K052109_set_RMRD_line((data & 0x0200) ? 1 : 0);
 }
}


decompiled_code:
__int64 eeprom_r()
{
int v0; // ebx
__int64 result; // rax

v0 = EEPROM_read_bit();
result = (unsigned int)input_port_2_r(0LL) | (v0 << 6);
if ( init_eeprom_count )
{
--init_eeprom_count;
result = (unsigned __int16)result & 0xBFFF;
}
return result;
}

canonical_decompiled_code:
__int64 func18()
{
int v0; // ebx
__int64 result; // rax

v0 = EEPROM_read_bit();
result = (unsigned int)input_port_2_r(0LL) | (v0 << 6);
if ( init_eeprom_count )
{
--init_eeprom_count;
result = (unsigned __int16)result & 0xBFFF;
}
return result;
}

original_code:
static data_t eeprom_r(offs_t offset)
{
 int res;
 res = (EEPROM_read_bit() << 6) | input_port_2_r(0);
 if (init_eeprom_count)
 {
  init_eeprom_count--;
  res &= 0xbfff;
 }
 return res;
}

canonical_original_code:
static unsigned int eeprom_r(unsigned int offset)
{
 int res;
 res = (EEPROM_read_bit() << 6) | input_port_2_r(0);
 if (init_eeprom_count)
 {
  init_eeprom_count--;
  res &= 0xbfff;
 }
 return res;
}


decompiled_code:
__int64 sound_status_r()
{
return soundlatch2_r(0LL);
}

canonical_decompiled_code:
__int64 func19()
{
return soundlatch2_r(0LL);
}

original_code:
static data_t sound_status_r(offs_t offset)
{
 return soundlatch2_r(0);
}

canonical_original_code:
static unsigned int sound_status_r(unsigned int offset)
{
 return soundlatch2_r(0);
}


decompiled_code:
__int64 atari_vg_earom_r()
{
return (unsigned int)earom_data;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)earom_data;
}

original_code:
data_t atari_vg_earom_r(offs_t offset)
{
 return (earom_data);
}

canonical_original_code:
unsigned int atari_vg_earom_r(unsigned int offset)
{
 return (earom_data);
}


decompiled_code:
void __fastcall atari_vg_earom_w(int a1, int a2)
{
earom_offset = a1;
earom_data = a2;
}

canonical_decompiled_code:
void __fastcall func1(int a1, int a2)
{
earom_offset = a1;
earom_data = a2;
}

original_code:
void atari_vg_earom_w(offs_t offset,data_t data)
{
 earom_offset = offset;
 earom_data = data;
}

canonical_original_code:
void atari_vg_earom_w(unsigned int offset, unsigned int data)
{
 earom_offset = offset;
 earom_data = data;
}


decompiled_code:
__int64 __fastcall atari_vg_earom_ctrl_w(__int64 a1, char a2)
{
__int64 result; // rax

if ( (a2 & 1) != 0 )
{
result = (unsigned int)*((char *)&earom + earom_offset);
earom_data = *((char *)&earom + earom_offset);
}
if ( (a2 & 0xC) == 12 )
{
result = earom_offset;
*((_BYTE *)&earom + earom_offset) = earom_data;
}
return result;
}

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

if ( (a2 & 1) != 0 )
{
result = (unsigned int)*((char *)&earom + earom_offset);
earom_data = *((char *)&earom + earom_offset);
}
if ( (a2 & 0xC) == 12 )
{
result = earom_offset;
*((_BYTE *)&earom + earom_offset) = earom_data;
}
return result;
}

original_code:
void atari_vg_earom_ctrl_w(offs_t offset,data_t data)
{
 if (data & 0x01)
  earom_data = earom[earom_offset];
 if ((data & 0x0c) == 0x0c)
 {
  earom[earom_offset]=earom_data;
 }
}

canonical_original_code:
void atari_vg_earom_ctrl_w(unsigned int offset, unsigned int data)
{
 if (data & 0x01)
  earom_data = earom[earom_offset];
 if ((data & 0x0c) == 0x0c)
 {
  earom[earom_offset]=earom_data;
 }
}


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

if ( a2 )
return osd_fwrite(a1, &earom, 64LL);
if ( a1 )
return osd_fread(a1, &earom, 64LL);
earom = 0LL;
qword_108 = 0LL;
qword_110 = 0LL;
qword_118 = 0LL;
qword_120 = 0LL;
qword_128 = 0LL;
qword_130 = 0LL;
qword_138 = 0LL;
return result;
}

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

if ( a2 )
return osd_fwrite(a1, &earom, 64LL);
if ( a1 )
return osd_fread(a1, &earom, 64LL);
earom = 0LL;
qword_108 = 0LL;
qword_110 = 0LL;
qword_118 = 0LL;
qword_120 = 0LL;
qword_128 = 0LL;
qword_130 = 0LL;
qword_138 = 0LL;
return result;
}

original_code:
void atari_vg_earom_handler(void *file,int read_or_write)
{
 if (read_or_write)
  osd_fwrite(file,earom,0x40);
 else
 {
  if (file)
   osd_fread(file,earom,0x40);
  else
   memset(earom,0,0x40);
 }
}

canonical_original_code:
void atari_vg_earom_handler(void *file, int read_or_write)
{
 if (read_or_write)
  osd_fwrite(file,earom,0x40);
 else
 {
  if (file)
   osd_fread(file,earom,0x40);
  else
   memset(earom,0,0x40);
 }
}

variable_types:
file
void *


original_code:
static void redbaron_sound_update(int param, INT16 *buffer, int length)
{
 while( length-- )
 {
  int sum = 0;
  poly_counter -= 12000;
  while( poly_counter <= 0 )
  {
   poly_counter += Machine->sample_rate;
   if( ((poly_shift & 0x0001) == 0) == ((poly_shift & 0x4000) == 0) )
    poly_shift = (poly_shift << 1) | 1;
   else
    poly_shift <<= 1;
  }
  filter_counter -= 330;
  while( filter_counter <= 0 )
  {
   filter_counter += Machine->sample_rate;
   crash_amp = (poly_shift & 1) ? latch >> 4 : 0;
  }
  sum += vol_crash[crash_amp] * 35 / 100;
  if( (latch & 0x04) == 0 )
            shot_amp = 32767;
        else
  if( (poly_shift & 0x8000) == 0 )
        {
   if( shot_amp > 0 )
   {
    shot_amp_counter -= (int)(32767 / 0.03264);;
    while( shot_amp_counter <= 0 )
    {
     shot_amp_counter += Machine->sample_rate;
     if( --shot_amp == 0 )
      break;
    }
    sum += vol_lookup[shot_amp] * 35 / 100;
            }
        }
  if( (latch & 0x02) == 0 )
   squeal_amp = 0;
  else
  {
   if( squeal_amp < 32767 )
   {
    squeal_amp_counter -= (int)(32767 / 1.01728);;
    while( squeal_amp_counter <= 0 )
    {
     squeal_amp_counter += Machine->sample_rate;
     if( ++squeal_amp == 32767 )
      break;
    }
   }
   if( squeal_out )
   {
    squeal_off_counter -= (1134 + 1134 * squeal_amp / 32767) / 3;
    while( squeal_off_counter <= 0 )
    {
     squeal_off_counter += Machine->sample_rate;
     squeal_out = 0;
    }
   }
   else
   {
    squeal_on_counter -= 1134;
    while( squeal_on_counter <= 0 )
    {
     squeal_on_counter += Machine->sample_rate;
     squeal_out = 1;
                }
            }
  }
        if( squeal_out )
   sum += 32767 * 40 / 100;
  *buffer++ = sum;
 }
}

canonical_original_code:
static void redbaron_sound_update(int param, short *buffer, int length)
{
 while( length-- )
 {
  int sum = 0;
  poly_counter -= 12000;
  while( poly_counter <= 0 )
  {
   poly_counter += Machine->sample_rate;
   if( ((poly_shift & 0x0001) == 0) == ((poly_shift & 0x4000) == 0) )
    poly_shift = (poly_shift << 1) | 1;
   else
    poly_shift <<= 1;
  }
  filter_counter -= 330;
  while( filter_counter <= 0 )
  {
   filter_counter += Machine->sample_rate;
   crash_amp = (poly_shift & 1) ? latch >> 4 : 0;
  }
  sum += vol_crash[crash_amp] * 35 / 100;
  if( (latch & 0x04) == 0 )
            shot_amp = 32767;
        else
  if( (poly_shift & 0x8000) == 0 )
        {
   if( shot_amp > 0 )
   {
    shot_amp_counter -= (int)(32767 / 0.03264);;
    while( shot_amp_counter <= 0 )
    {
     shot_amp_counter += Machine->sample_rate;
     if( --shot_amp == 0 )
      break;
    }
    sum += vol_lookup[shot_amp] * 35 / 100;
            }
        }
  if( (latch & 0x02) == 0 )
   squeal_amp = 0;
  else
  {
   if( squeal_amp < 32767 )
   {
    squeal_amp_counter -= (int)(32767 / 1.01728);;
    while( squeal_amp_counter <= 0 )
    {
     squeal_amp_counter += Machine->sample_rate;
     if( ++squeal_amp == 32767 )
      break;
    }
   }
   if( squeal_out )
   {
    squeal_off_counter -= (1134 + 1134 * squeal_amp / 32767) / 3;
    while( squeal_off_counter <= 0 )
    {
     squeal_off_counter += Machine->sample_rate;
     squeal_out = 0;
    }
   }
   else
   {
    squeal_on_counter -= 1134;
    while( squeal_on_counter <= 0 )
    {
     squeal_on_counter += Machine->sample_rate;
     squeal_out = 1;
                }
            }
  }
        if( squeal_out )
   sum += 32767 * 40 / 100;
  *buffer++ = sum;
 }
}

variable_types: param int

variable_types:
buffer
short *

variable_types: length int

variable_types: sum int


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

if ( (latch & 0x20) != 0 )
result = pokey1_w();
return result;
}

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

if ( (latch & 0x20) != 0 )
result = pokey1_w();
return result;
}

original_code:
void redbaron_pokey_w(offs_t offset,data_t data)
{
    if( latch & 0x20 )
        pokey1_w (offset, data);
}

canonical_original_code:
void redbaron_pokey_w(unsigned int offset, unsigned int data)
{
    if( latch & 0x20 )
        pokey1_w (offset, data);
}


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

variable_types: i int


decompiled_code:
void redbaron_sh_stop()
{
if ( vol_lookup )
free(vol_lookup);
vol_lookup = 0LL;
}

canonical_decompiled_code:
void func3()
{
if ( vol_lookup )
free(vol_lookup);
vol_lookup = 0LL;
}

original_code:
void redbaron_sh_stop(void)
{
 if( vol_lookup )
  free(vol_lookup);
 vol_lookup = ((void *)0);
}

canonical_original_code:
void redbaron_sh_stop()
{
 if( vol_lookup )
  free(vol_lookup);
 vol_lookup = ((void *)0);
}


decompiled_code:
__int64 redbaron_sh_update()
{
return stream_update((unsigned int)channel, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
return stream_update((unsigned int)channel, 0LL);
}

original_code:
void redbaron_sh_update(void)
{
 stream_update(channel, 0);
}

canonical_original_code:
void redbaron_sh_update()
{
 stream_update(channel, 0);
}


decompiled_code:
void __fastcall starcrus_s1_x_w(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s1_x = a2;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s1_x = a2;
}

original_code: void starcrus_s1_x_w(offs_t offset,data_t data) { s1_x = data^0xff; }

canonical_original_code: void starcrus_s1_x_w(unsigned int offset, unsigned int data) { s1_x = data^0xff; }


decompiled_code:
void __fastcall starcrus_s1_y_w(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s1_y = a2;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s1_y = a2;
}

original_code: void starcrus_s1_y_w(offs_t offset,data_t data) { s1_y = data^0xff; }

canonical_original_code: void starcrus_s1_y_w(unsigned int offset, unsigned int data) { s1_y = data^0xff; }


decompiled_code:
void __fastcall starcrus_s2_x_w(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s2_x = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s2_x = a2;
}

original_code: void starcrus_s2_x_w(offs_t offset,data_t data) { s2_x = data^0xff; }

canonical_original_code: void starcrus_s2_x_w(unsigned int offset, unsigned int data) { s2_x = data^0xff; }


decompiled_code:
void __fastcall starcrus_s2_y_w(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s2_y = a2;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, int a2)
{
LOBYTE(a2) = ~(_BYTE)a2;
s2_y = a2;
}

original_code: void starcrus_s2_y_w(offs_t offset,data_t data) { s2_y = data^0xff; }

canonical_original_code: void starcrus_s2_y_w(unsigned int offset, unsigned int data) { s2_y = data^0xff; }