decompiled_code:
__int64 __fastcall HD63484_command_w(__int16 a1)
{
int v1; // eax
int v2; // edx
__int64 result; // rax
__int16 v4; // ax
int v5; // ebx
int v6; // edx
int v7; // edx
int v8; // edx
char v9; // dh
_BYTE *v10; // rcx
__int16 v11; // r10
int v12; // er8
__int16 v13; // r9
int v14; // edx
_BYTE *v15; // rsi
char v16; // di
int v17; // ebx
int v18; // er11
int v19; // er12
int v20; // ecx
int v21; // ebp
_BYTE *v22; // r13
_BYTE *v23; // r13
_BYTE *v24; // r13
_BYTE *v25; // r13
int v26[11]; // [rsp+0h] [rbp-2Ch] BYREF

v1 = fifo_counter;
v2 = ++fifo_counter;
fifo[v1] = a1;
result = instruction_length[((unsigned __int16)fifo[0] >> 10) & 0x3F];
if ( (_DWORD)result == -1 )
{
if ( v2 <= 1 )
return result;
result = (unsigned int)(unsigned __int16)word_F82 + 2;
}
else if ( (_DWORD)result == -2 )
{
if ( v2 <= 1 )
return result;
result = 2 * (unsigned int)(unsigned __int16)word_F82 + 2;
}
if ( v2 < (int)result )
return result;
switch ( fifo[0] )
{
case 1024:
v6 = (unsigned __int16)word_F84 >> 4;
result = v6 | ((unsigned __int16)word_F82 << 12) & 0xFF000u;
org = v6 | ((unsigned __int16)word_F82 << 12) & 0xFF000;
goto LABEL_24;
case 2048:
result = (unsigned __int16)word_F82;
cl0 = word_F82;
goto LABEL_24;
case 2049:
LABEL_24:
fifo_counter = 0;
return result;
case 2050:
result = (unsigned __int16)word_F82;
ccmp = word_F82;
goto LABEL_24;
case 2060:
v7 = rwp & 0xFFF;
result = v7 | ((unsigned __int16)word_F82 << 12) & 0xFF000u;
rwp = v7 | ((unsigned __int16)word_F82 << 12) & 0xFF000;
goto LABEL_24;
case 2061:
v8 = (unsigned __int16)word_F82 >> 4;
result = v8 | rwp & 0xFF000u;
rwp = v8 | rwp & 0xFF000;
goto LABEL_24;
case 18432:
v9 = HIBYTE(word_F82);
v10 = HD63484_ram;
*((_BYTE *)HD63484_ram + 2 * rwp) = word_F82;
v10[2 * rwp + 1] = v9;
result = (rwp + 1) & 0xFFFFF;
rwp = (rwp + 1) & 0xFFFFF;
goto LABEL_24;
case 22528:
rwp *= 2;
doclr(0, word_F82, &rwp, (__int16)(2 * word_F84 + 1), word_F86);
result = (unsigned int)(rwp / 2);
rwp /= 2;
break;
default:
if ( (fifo[0] & 0xFFFC) == 23552 )
{
rwp *= 2;
doclr(fifo[0], word_F82, &rwp, (__int16)(2 * word_F84 + 1), word_F86);
result = (unsigned int)(rwp / 2);
rwp /= 2;
}
else
{
LOBYTE(v4) = fifo[0];
HIBYTE(v4) = HIBYTE(fifo[0]) & 0xF0;
if ( v4 == 24576 || (fifo[0] & 0xF0FC) == 28672 )
{
rwp *= 2;
docpy(
fifo[0],
2 * (((unsigned __int16)word_F84 >> 4) | ((unsigned __int16)word_F82 << 12) & 0xFF000),
&rwp,
2 * word_F86 + 1,
word_F88);
result = (unsigned int)(rwp / 2);
rwp /= 2;
}
else if ( fifo[0] == (__int16)0x8000 )
{
cpx = word_F82;
result = (unsigned __int16)word_F84;
cpy = word_F84;
}
else
{
result = fifo[0] & 0xFFF8;
if ( (fifo[0] & 0xFFF8) == 0xC000 )
{
v11 = word_F84;
v12 = (unsigned __int16)word_F82 - (unsigned __int16)cpx;
v13 = cpy;
v14 = (unsigned __int16)word_F84 - (unsigned __int16)cpy;
LODWORD(result) = (cpx + 2 * org - 384 * cpy) & 0x1FFFFF;
v15 = HD63484_ram;
v16 = cl0;
v17 = (unsigned __int8)cl0;
v18 = (unsigned __int8)ccmp;
v19 = v12;
v20 = fifo[0] & 7;
v21 = (__int16)(word_F82 - cpx);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
switch ( v20 )
{
case 0:
v15[(int)result] = v16;
break;
case 1:
v15[(int)result] |= v16;
break;
case 2:
v15[(int)result] &= v16;
break;
case 3:
v15[(int)result] ^= v16;
break;
case 4:
v22 = &v15[(int)result];
if ( (unsigned __int8)*v22 == v18 )
*v22 = v16;
break;
case 5:
v23 = &v15[(int)result];
if ( (unsigned __int8)*v23 != v18 )
*v23 = v16;
break;
case 6:
v24 = &v15[(int)result];
if ( (unsigned __int8)*v24 < v17 )
*v24 = v16;
break;
case 7:
v25 = &v15[(int)result];
if ( (unsigned __int8)*v25 > v17 )
*v25 = v16;
break;
}
if ( !(_WORD)v19 )
break;
if ( (__int16)v19 <= 0 )
{
LODWORD(result) = (result - 1) & 0x1FFFFF;
++v19;
}
else
{
LODWORD(result) = (result + 1) & 0x1FFFFF;
--v19;
}
}
if ( v13 <= v11 )
break;
result = ((_DWORD)result + 384 - v21) & 0x1FFFFF;
if ( !(_WORD)v14 )
goto LABEL_31;
++v14;
v19 = v12;
}
result = ((_DWORD)result - 384 - v21) & 0x1FFFFF;
if ( !(_WORD)v14 )
break;
--v14;
v19 = v12;
}
}
else if ( (fifo[0] & 0xF0F8) == 0xE000 )
{
v5 = 2 * org;
v26[0] = (-384 * cpy + 2 * org + cpx) & 0x1FFFFF;
docpy(fifo[0], (-384 * word_F84 + 2 * org + word_F82) & 0x1FFFFF, v26, word_F86, word_F88);
result = (unsigned int)((v26[0] - v5) / 384);
cpx = (v26[0] - v5) % 384;
cpy = (v26[0] - v5) / 384;
}
}
}
break;
}
LABEL_31:
fifo_counter = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int16 a1)
{
int v1; // eax
int v2; // edx
__int64 result; // rax
__int16 v4; // ax
int v5; // ebx
int v6; // edx
int v7; // edx
int v8; // edx
char v9; // dh
_BYTE *v10; // rcx
__int16 v11; // r10
int v12; // er8
__int16 v13; // r9
int v14; // edx
_BYTE *v15; // rsi
char v16; // di
int v17; // ebx
int v18; // er11
int v19; // er12
int v20; // ecx
int v21; // ebp
_BYTE *v22; // r13
_BYTE *v23; // r13
_BYTE *v24; // r13
_BYTE *v25; // r13
int v26[11]; // [rsp+0h] [rbp-2Ch] BYREF

v1 = fifo_counter;
v2 = ++fifo_counter;
fifo[v1] = a1;
result = instruction_length[((unsigned __int16)fifo[0] >> 10) & 0x3F];
if ( (_DWORD)result == -1 )
{
if ( v2 <= 1 )
return result;
result = (unsigned int)(unsigned __int16)word_F82 + 2;
}
else if ( (_DWORD)result == -2 )
{
if ( v2 <= 1 )
return result;
result = 2 * (unsigned int)(unsigned __int16)word_F82 + 2;
}
if ( v2 < (int)result )
return result;
switch ( fifo[0] )
{
case 1024:
v6 = (unsigned __int16)word_F84 >> 4;
result = v6 | ((unsigned __int16)word_F82 << 12) & 0xFF000u;
org = v6 | ((unsigned __int16)word_F82 << 12) & 0xFF000;
goto LABEL_24;
case 2048:
result = (unsigned __int16)word_F82;
cl0 = word_F82;
goto LABEL_24;
case 2049:
LABEL_24:
fifo_counter = 0;
return result;
case 2050:
result = (unsigned __int16)word_F82;
ccmp = word_F82;
goto LABEL_24;
case 2060:
v7 = rwp & 0xFFF;
result = v7 | ((unsigned __int16)word_F82 << 12) & 0xFF000u;
rwp = v7 | ((unsigned __int16)word_F82 << 12) & 0xFF000;
goto LABEL_24;
case 2061:
v8 = (unsigned __int16)word_F82 >> 4;
result = v8 | rwp & 0xFF000u;
rwp = v8 | rwp & 0xFF000;
goto LABEL_24;
case 18432:
v9 = HIBYTE(word_F82);
v10 = HD63484_ram;
*((_BYTE *)HD63484_ram + 2 * rwp) = word_F82;
v10[2 * rwp + 1] = v9;
result = (rwp + 1) & 0xFFFFF;
rwp = (rwp + 1) & 0xFFFFF;
goto LABEL_24;
case 22528:
rwp *= 2;
func0(0, word_F82, &rwp, (__int16)(2 * word_F84 + 1), word_F86);
result = (unsigned int)(rwp / 2);
rwp /= 2;
break;
default:
if ( (fifo[0] & 0xFFFC) == 23552 )
{
rwp *= 2;
func0(fifo[0], word_F82, &rwp, (__int16)(2 * word_F84 + 1), word_F86);
result = (unsigned int)(rwp / 2);
rwp /= 2;
}
else
{
LOBYTE(v4) = fifo[0];
HIBYTE(v4) = HIBYTE(fifo[0]) & 0xF0;
if ( v4 == 24576 || (fifo[0] & 0xF0FC) == 28672 )
{
rwp *= 2;
func1(
fifo[0],
2 * (((unsigned __int16)word_F84 >> 4) | ((unsigned __int16)word_F82 << 12) & 0xFF000),
&rwp,
2 * word_F86 + 1,
word_F88);
result = (unsigned int)(rwp / 2);
rwp /= 2;
}
else if ( fifo[0] == (__int16)0x8000 )
{
cpx = word_F82;
result = (unsigned __int16)word_F84;
cpy = word_F84;
}
else
{
result = fifo[0] & 0xFFF8;
if ( (fifo[0] & 0xFFF8) == 0xC000 )
{
v11 = word_F84;
v12 = (unsigned __int16)word_F82 - (unsigned __int16)cpx;
v13 = cpy;
v14 = (unsigned __int16)word_F84 - (unsigned __int16)cpy;
LODWORD(result) = (cpx + 2 * org - 384 * cpy) & 0x1FFFFF;
v15 = HD63484_ram;
v16 = cl0;
v17 = (unsigned __int8)cl0;
v18 = (unsigned __int8)ccmp;
v19 = v12;
v20 = fifo[0] & 7;
v21 = (__int16)(word_F82 - cpx);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
switch ( v20 )
{
case 0:
v15[(int)result] = v16;
break;
case 1:
v15[(int)result] |= v16;
break;
case 2:
v15[(int)result] &= v16;
break;
case 3:
v15[(int)result] ^= v16;
break;
case 4:
v22 = &v15[(int)result];
if ( (unsigned __int8)*v22 == v18 )
*v22 = v16;
break;
case 5:
v23 = &v15[(int)result];
if ( (unsigned __int8)*v23 != v18 )
*v23 = v16;
break;
case 6:
v24 = &v15[(int)result];
if ( (unsigned __int8)*v24 < v17 )
*v24 = v16;
break;
case 7:
v25 = &v15[(int)result];
if ( (unsigned __int8)*v25 > v17 )
*v25 = v16;
break;
}
if ( !(_WORD)v19 )
break;
if ( (__int16)v19 <= 0 )
{
LODWORD(result) = (result - 1) & 0x1FFFFF;
++v19;
}
else
{
LODWORD(result) = (result + 1) & 0x1FFFFF;
--v19;
}
}
if ( v13 <= v11 )
break;
result = ((_DWORD)result + 384 - v21) & 0x1FFFFF;
if ( !(_WORD)v14 )
goto LABEL_31;
++v14;
v19 = v12;
}
result = ((_DWORD)result - 384 - v21) & 0x1FFFFF;
if ( !(_WORD)v14 )
break;
--v14;
v19 = v12;
}
}
else if ( (fifo[0] & 0xF0F8) == 0xE000 )
{
v5 = 2 * org;
v26[0] = (-384 * cpy + 2 * org + cpx) & 0x1FFFFF;
func1(fifo[0], (-384 * word_F84 + 2 * org + word_F82) & 0x1FFFFF, v26, word_F86, word_F88);
result = (unsigned int)((v26[0] - v5) / 384);
cpx = (v26[0] - v5) % 384;
cpy = (v26[0] - v5) / 384;
}
}
}
break;
}
LABEL_31:
fifo_counter = 0;
return result;
}

original_code:
void HD63484_command_w(UINT16 cmd)
{
 int len;
 fifo[fifo_counter++] = cmd;
 len = instruction_length[fifo[0]>>10];
 if (len == -1)
 {
  if (fifo_counter < 2) return;
  else len = fifo[1]+2;
 }
 else if (len == -2)
 {
  if (fifo_counter < 2) return;
  else len = 2*fifo[1]+2;
 }
 if (fifo_counter >= len)
 {
  if (fifo[0] == 0x0400)
   org = ((fifo[1] & 0x00ff) << 12) | ((fifo[2] & 0xfff0) >> 4);
  else if (fifo[0] == 0x0800)
   cl0 = fifo[1];
  else if (fifo[0] == 0x0801)
   cl1 = fifo[1];
  else if (fifo[0] == 0x0802)
   ccmp = fifo[1];
  else if (fifo[0] == 0x080c)
   rwp = (rwp & 0x00fff) | ((fifo[1] & 0x00ff) << 12);
  else if (fifo[0] == 0x080d)
   rwp = (rwp & 0xff000) | ((fifo[1] & 0xfff0) >> 4);
  else if (fifo[0] == 0x4800)
  {
   HD63484_ram[2*rwp] = fifo[1] & 0x00ff ;
   HD63484_ram[2*rwp+1] = (fifo[1] & 0xff00) >> 8;
   rwp = (rwp + 1) & 0xfffff;
  }
  else if (fifo[0] == 0x5800)
  {
rwp *= 2;
   doclr(fifo[0],fifo[1],&rwp,2*fifo[2]+1,fifo[3]);
rwp /= 2;
  }
  else if ((fifo[0] & 0xfffc) == 0x5c00)
  {
rwp *= 2;
   doclr(fifo[0],fifo[1],&rwp,2*fifo[2]+1,fifo[3]);
rwp /= 2;
  }
  else if ((fifo[0] & 0xf0ff) == 0x6000)
  {
   int src;
   src = ((fifo[1] & 0x00ff) << 12) | ((fifo[2] & 0xfff0) >> 4);
rwp *= 2;
   docpy(fifo[0],2*src,&rwp,2*fifo[3]+1,fifo[4]);
rwp /= 2;
  }
  else if ((fifo[0] & 0xf0fc) == 0x7000)
  {
   int src;
   src = ((fifo[1] & 0x00ff) << 12) | ((fifo[2] & 0xfff0) >> 4);
rwp *= 2;
   docpy(fifo[0],2*src,&rwp,2*fifo[3]+1,fifo[4]);
rwp /= 2;
  }
  else if (fifo[0] == 0x8000)
  {
   cpx = fifo[1];
   cpy = fifo[2];
  }
  else if ((fifo[0] & 0xfff8) == 0xc000)
  {
   INT16 pcx,pcy;
   INT16 ax,ay;
   int dst;
   pcx = fifo[1];
   pcy = fifo[2];
   ax = pcx - cpx;
   ay = pcy - cpy;
   dst = (2*org + cpx - cpy * 384) & 0x1fffff;
   for (;;)
   {
    for (;;)
    {
     switch (fifo[0] & 0x0007)
     {
      case 0:
       HD63484_ram[dst] = cl0; break;
      case 1:
       HD63484_ram[dst] |= cl0; break;
      case 2:
       HD63484_ram[dst] &= cl0; break;
      case 3:
       HD63484_ram[dst] ^= cl0; break;
      case 4:
       if (HD63484_ram[dst] == (ccmp & 0xff))
        HD63484_ram[dst] = cl0;
       break;
      case 5:
       if (HD63484_ram[dst] != (ccmp & 0xff))
        HD63484_ram[dst] = cl0;
       break;
      case 6:
       if (HD63484_ram[dst] < (cl0 & 0xff))
        HD63484_ram[dst] = cl0;
       break;
      case 7:
       if (HD63484_ram[dst] > (cl0 & 0xff))
        HD63484_ram[dst] = cl0;
       break;
     }
     if (ax == 0) break;
     else if (ax > 0)
     {
      dst = (dst + 1) & 0x1fffff;
      ax--;
     }
     else
     {
      dst = (dst - 1) & 0x1fffff;
      ax++;
     }
    }
    ax = pcx - cpx;
    if (pcy < cpy)
    {
     dst = (dst + 384 - ax) & 0x1fffff;
     if (ay == 0) break;
     ay++;
    }
    else
    {
     dst = (dst - 384 - ax) & 0x1fffff;
     if (ay == 0) break;
     ay--;
    }
   }
  }
  else if ((fifo[0] & 0xf0f8) == 0xe000)
  {
   INT16 pcx,pcy;
   int src,dst;
   pcx = fifo[1];
   pcy = fifo[2];
   src = (2*org + pcx - pcy * 384) & 0x1fffff;
   dst = (2*org + cpx - cpy * 384) & 0x1fffff;
   docpy(fifo[0],src,&dst,fifo[3],fifo[4]);
   cpx = (dst - 2*org) % 384;
   cpy = (dst - 2*org) / 384;
  }
  fifo_counter = 0;
 }
}

canonical_original_code:
void HD63484_command_w(unsigned short cmd)
{
 int len;
 fifo[fifo_counter++] = cmd;
 len = instruction_length[fifo[0]>>10];
 if (len == -1)
 {
  if (fifo_counter < 2) return;
  else len = fifo[1]+2;
 }
 else if (len == -2)
 {
  if (fifo_counter < 2) return;
  else len = 2*fifo[1]+2;
 }
 if (fifo_counter >= len)
 {
  if (fifo[0] == 0x0400)
   org = ((fifo[1] & 0x00ff) << 12) | ((fifo[2] & 0xfff0) >> 4);
  else if (fifo[0] == 0x0800)
   cl0 = fifo[1];
  else if (fifo[0] == 0x0801)
   cl1 = fifo[1];
  else if (fifo[0] == 0x0802)
   ccmp = fifo[1];
  else if (fifo[0] == 0x080c)
   rwp = (rwp & 0x00fff) | ((fifo[1] & 0x00ff) << 12);
  else if (fifo[0] == 0x080d)
   rwp = (rwp & 0xff000) | ((fifo[1] & 0xfff0) >> 4);
  else if (fifo[0] == 0x4800)
  {
   HD63484_ram[2*rwp] = fifo[1] & 0x00ff ;
   HD63484_ram[2*rwp+1] = (fifo[1] & 0xff00) >> 8;
   rwp = (rwp + 1) & 0xfffff;
  }
  else if (fifo[0] == 0x5800)
  {
rwp *= 2;
   doclr(fifo[0],fifo[1],&rwp,2*fifo[2]+1,fifo[3]);
rwp /= 2;
  }
  else if ((fifo[0] & 0xfffc) == 0x5c00)
  {
rwp *= 2;
   doclr(fifo[0],fifo[1],&rwp,2*fifo[2]+1,fifo[3]);
rwp /= 2;
  }
  else if ((fifo[0] & 0xf0ff) == 0x6000)
  {
   int src;
   src = ((fifo[1] & 0x00ff) << 12) | ((fifo[2] & 0xfff0) >> 4);
rwp *= 2;
   docpy(fifo[0],2*src,&rwp,2*fifo[3]+1,fifo[4]);
rwp /= 2;
  }
  else if ((fifo[0] & 0xf0fc) == 0x7000)
  {
   int src;
   src = ((fifo[1] & 0x00ff) << 12) | ((fifo[2] & 0xfff0) >> 4);
rwp *= 2;
   docpy(fifo[0],2*src,&rwp,2*fifo[3]+1,fifo[4]);
rwp /= 2;
  }
  else if (fifo[0] == 0x8000)
  {
   cpx = fifo[1];
   cpy = fifo[2];
  }
  else if ((fifo[0] & 0xfff8) == 0xc000)
  {
   short pcx; short pcy;
   short ax; short ay;
   int dst;
   pcx = fifo[1];
   pcy = fifo[2];
   ax = pcx - cpx;
   ay = pcy - cpy;
   dst = (2*org + cpx - cpy * 384) & 0x1fffff;
   for (;;)
   {
    for (;;)
    {
     switch (fifo[0] & 0x0007)
     {
      case 0:
       HD63484_ram[dst] = cl0; break;
      case 1:
       HD63484_ram[dst] |= cl0; break;
      case 2:
       HD63484_ram[dst] &= cl0; break;
      case 3:
       HD63484_ram[dst] ^= cl0; break;
      case 4:
       if (HD63484_ram[dst] == (ccmp & 0xff))
        HD63484_ram[dst] = cl0;
       break;
      case 5:
       if (HD63484_ram[dst] != (ccmp & 0xff))
        HD63484_ram[dst] = cl0;
       break;
      case 6:
       if (HD63484_ram[dst] < (cl0 & 0xff))
        HD63484_ram[dst] = cl0;
       break;
      case 7:
       if (HD63484_ram[dst] > (cl0 & 0xff))
        HD63484_ram[dst] = cl0;
       break;
     }
     if (ax == 0) break;
     else if (ax > 0)
     {
      dst = (dst + 1) & 0x1fffff;
      ax--;
     }
     else
     {
      dst = (dst - 1) & 0x1fffff;
      ax++;
     }
    }
    ax = pcx - cpx;
    if (pcy < cpy)
    {
     dst = (dst + 384 - ax) & 0x1fffff;
     if (ay == 0) break;
     ay++;
    }
    else
    {
     dst = (dst - 384 - ax) & 0x1fffff;
     if (ay == 0) break;
     ay--;
    }
   }
  }
  else if ((fifo[0] & 0xf0f8) == 0xe000)
  {
   short pcx; short pcy;
   int src; int dst;
   pcx = fifo[1];
   pcy = fifo[2];
   src = (2*org + pcx - pcy * 384) & 0x1fffff;
   dst = (2*org + cpx - cpy * 384) & 0x1fffff;
   docpy(fifo[0],src,&dst,fifo[3],fifo[4]);
   cpx = (dst - 2*org) % 384;
   cpy = (dst - 2*org) / 384;
  }
  fifo_counter = 0;
 }
}

variable_types:
cmd
unsigned short

variable_types: len int

variable_types: pcx short

variable_types: pcy short

variable_types: ax short

variable_types: ay short

variable_types: dst int


decompiled_code:
__int64 __fastcall HD63484_data_w(unsigned int a1, char a2)
{
__int64 result; // rax
__int16 v3; // di
int v4; // edx

result = a1;
dat_1[a1] = a2;
if ( a1 == 1 )
{
v3 = (unsigned __int8)dat_1[0] + ((unsigned __int8)byte_E43 << 8);
v4 = regno;
if ( regno )
{
result = regno / 2;
HD63484_reg[result] = v3;
if ( (v4 & 0x80u) != 0 )
regno = v4 + 2;
}
else
{
result = HD63484_command_w(v3);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1, char a2)
{
__int64 result; // rax
__int16 v3; // di
int v4; // edx

result = a1;
dat_1[a1] = a2;
if ( a1 == 1 )
{
v3 = (unsigned __int8)dat_1[0] + ((unsigned __int8)byte_E43 << 8);
v4 = regno;
if ( regno )
{
result = regno / 2;
HD63484_reg[result] = v3;
if ( (v4 & 0x80u) != 0 )
regno = v4 + 2;
}
else
{
result = func13(v3);
}
}
return result;
}

original_code:
static void HD63484_data_w(offs_t offset,data_t data)
{
 static unsigned char dat[2];
 dat[offset] = data;
 if (offset == 1)
 {
  int val = dat[0] + 256 * dat[1];
  if (regno == 0)
   HD63484_command_w(val);
  else
  {
   HD63484_reg[regno/2] = val;
   if (regno & 0x80) regno += 2;
  }
 }
}

canonical_original_code:
static void HD63484_data_w(unsigned int offset, unsigned int data)
{
 static unsigned char dat[2];
 dat[offset] = data;
 if (offset == 1)
 {
  int val = dat[0] + 256 * dat[1];
  if (regno == 0)
   HD63484_command_w(val);
  else
  {
   HD63484_reg[regno/2] = val;
   if (regno & 0x80) regno += 2;
  }
 }
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int

variable_types:
dat
unsigned char [2]

variable_types: val int

return_type: void


decompiled_code:
int *__fastcall qsound_update(__int64 a1, __int64 a2, int a3)
{
int *result; // rax
_WORD *v5; // rbp
void *v6; // r12
size_t v7; // r13
int v8; // er8
int v9; // edi
_WORD *v10; // rsi
_WORD *v11; // rcx
__int64 v12; // r10
int v13; // edx
int v14; // edx
int v15; // edx
int v16; // er13
int v17; // edx

result = (int *)Machine;
if ( *(_DWORD *)(Machine + 836LL) )
{
v5 = *(_WORD **)a2;
v6 = *(void **)(a2 + 8);
v7 = 2LL * a3;
memset(*(void **)a2, 0, v7);
memset(v6, 0, v7);
result = (int *)&qsound_channel;
do
{
if ( result[9] )
{
v8 = (result[11] * result[6]) >> 8;
v9 = (result[10] * result[6]) >> 8;
if ( a3 > 0 )
{
v10 = v6;
v11 = v5;
v12 = *result + qsound_sample_rom;
do
{
v13 = result[13];
result[13] = (unsigned __int16)v13;
v14 = v13 >> 16;
if ( v14 )
{
v15 = result[1] + v14;
result[1] = v15;
v16 = result[5];
if ( v15 >= v16 )
{
v17 = result[4];
if ( !v17 )
{
result[9] = 0;
break;
}
result[1] = (unsigned __int16)(v16 - v17);
}
result[12] = *(char *)(v12 + result[1]);
}
*v11 += (result[12] * v9) >> 6;
*v10 += (result[12] * v8) >> 6;
++v11;
++v10;
result[13] += result[2];
}
while ( &v5[a3 - 1 + 1] != v11 );
}
}
result += 14;
}
while ( result != (int *)((char *)&qsound_channel + 896) );
}
return result;
}

canonical_decompiled_code:
int *__fastcall func0(__int64 a1, __int64 a2, int a3)
{
int *result; // rax
_WORD *v5; // rbp
void *v6; // r12
size_t v7; // r13
int v8; // er8
int v9; // edi
_WORD *v10; // rsi
_WORD *v11; // rcx
__int64 v12; // r10
int v13; // edx
int v14; // edx
int v15; // edx
int v16; // er13
int v17; // edx

result = (int *)Machine;
if ( *(_DWORD *)(Machine + 836LL) )
{
v5 = *(_WORD **)a2;
v6 = *(void **)(a2 + 8);
v7 = 2LL * a3;
memset(*(void **)a2, 0, v7);
memset(v6, 0, v7);
result = (int *)&qsound_channel;
do
{
if ( result[9] )
{
v8 = (result[11] * result[6]) >> 8;
v9 = (result[10] * result[6]) >> 8;
if ( a3 > 0 )
{
v10 = v6;
v11 = v5;
v12 = *result + qsound_sample_rom;
do
{
v13 = result[13];
result[13] = (unsigned __int16)v13;
v14 = v13 >> 16;
if ( v14 )
{
v15 = result[1] + v14;
result[1] = v15;
v16 = result[5];
if ( v15 >= v16 )
{
v17 = result[4];
if ( !v17 )
{
result[9] = 0;
break;
}
result[1] = (unsigned __int16)(v16 - v17);
}
result[12] = *(char *)(v12 + result[1]);
}
*v11 += (result[12] * v9) >> 6;
*v10 += (result[12] * v8) >> 6;
++v11;
++v10;
result[13] += result[2];
}
while ( &v5[a3 - 1 + 1] != v11 );
}
}
result += 14;
}
while ( result != (int *)((char *)&qsound_channel + 896) );
}
return result;
}

original_code:
void qsound_update( int num, INT16 **buffer, int length )
{
 int i,j;
 int rvol, lvol, count;
 struct QSOUND_CHANNEL *pC=&qsound_channel[0];
 QSOUND_SRC_SAMPLE * pST;
 QSOUND_SAMPLE *datap[2];
 if (Machine->sample_rate == 0) return;
 datap[0] = buffer[0];
 datap[1] = buffer[1];
 memset( datap[0], 0x00, length * sizeof(QSOUND_SAMPLE) );
 memset( datap[1], 0x00, length * sizeof(QSOUND_SAMPLE) );
 for (i=0; i<16; i++)
 {
  if (pC->key)
  {
   QSOUND_SAMPLE *pOutL=datap[0];
   QSOUND_SAMPLE *pOutR=datap[1];
   pST=qsound_sample_rom+pC->bank;
   rvol=(pC->rvol*pC->vol)>>(8*1);
   lvol=(pC->lvol*pC->vol)>>(8*1);
   for (j=length-1; j>=0; j--)
   {
    count=(pC->offset)>>16;
    pC->offset &= 0xffff;
    if (count)
    {
     pC->address += count;
     if (pC->address >= pC->end)
     {
      if (!pC->loop)
      {
       pC->key=0;
       break;
      }
      pC->address = (pC->end - pC->loop) & 0xffff;
     }
     pC->lastdt=pST[pC->address];
    }
    (*pOutL) += ((pC->lastdt * lvol) >> 6);
    (*pOutR) += ((pC->lastdt * rvol) >> 6);
    pOutL++;
    pOutR++;
    pC->offset += pC->pitch;
   }
  }
  pC++;
 }
}

canonical_original_code:
void qsound_update(int num, short **buffer, int length)
{
 int i; int j;
 int rvol; int lvol; int count;
 struct QSOUND_CHANNEL *pC = &qsound_channel[0];
 char *pST;
 short *datap[2];
 if (Machine->sample_rate == 0) return;
 datap[0] = buffer[0];
 datap[1] = buffer[1];
 memset( datap[0], 0x00, length * sizeof(QSOUND_SAMPLE) );
 memset( datap[1], 0x00, length * sizeof(QSOUND_SAMPLE) );
 for (i=0; i<16; i++)
 {
  if (pC->key)
  {
   short *pOutL = datap[0];
   short *pOutR = datap[1];
   pST=qsound_sample_rom+pC->bank;
   rvol=(pC->rvol*pC->vol)>>(8*1);
   lvol=(pC->lvol*pC->vol)>>(8*1);
   for (j=length-1; j>=0; j--)
   {
    count=(pC->offset)>>16;
    pC->offset &= 0xffff;
    if (count)
    {
     pC->address += count;
     if (pC->address >= pC->end)
     {
      if (!pC->loop)
      {
       pC->key=0;
       break;
      }
      pC->address = (pC->end - pC->loop) & 0xffff;
     }
     pC->lastdt=pST[pC->address];
    }
    (*pOutL) += ((pC->lastdt * lvol) >> 6);
    (*pOutR) += ((pC->lastdt * rvol) >> 6);
    pOutL++;
    pOutR++;
    pC->offset += pC->pitch;
   }
  }
  pC++;
 }
}

variable_types: num int

variable_types:
buffer
short **

variable_types: length int

variable_types: i int

variable_types: j int

variable_types: rvol int

variable_types: lvol int

variable_types: count int

variable_types: struct QSOUND_CHANNEL { int bank; int address; int pitch; int reg3; int loop; int end; int vol; int pan; int reg9; int key; int lvol; int rvol; int lastdt; int offset; } *

variable_types:
pST
char *

variable_types:
datap
short *[2]

variable_types:
pOutL
short *

variable_types:
pOutR
short *


decompiled_code:
__int64 __fastcall qsound_sh_start(__int64 a1, const char *a2)
{
void *v3; // rdx
__int64 i; // rbx
double v5; // xmm0_8
double v6; // xmm0_8
const char *v7; // rax
const char *v8; // rdx
const char *v9; // rax
int v10; // eax
int v11; // eax
int v12[2]; // [rsp+0h] [rbp-80h] BYREF
__int64 v13[2]; // [rsp+8h] [rbp-78h] BYREF
char s[40]; // [rsp+18h] [rbp-68h] BYREF
char v15[64]; // [rsp+40h] [rbp-40h] BYREF

if ( !*(_DWORD *)(Machine + 836LL) )
return 0LL;
intf = *(_QWORD *)(a1 + 8);
qsound_sample_rom = memory_region(*(unsigned int *)(intf + 4));
v3 = &qsound_channel;
memset(&qsound_channel, 0, 0x380uLL);
*(float *)&qsound_frq_ratio = (float)((float)((float)*(int *)intf / 166.0) / (float)*(int *)(Machine + 836LL)) * 16.0;
for ( i = 0LL; i != 33; ++i )
{
v5 = (double)(int)i;
if ( (double)(int)i < 0.0 )
v6 = sqrt(v5);
else
v6 = sqrt(v5);
qsound_pan_table[i] = (int)(v6 * 45.25483399593904);
}
v13[0] = (__int64)s;
v13[1] = (__int64)v15;
v7 = (const char *)sound_name(a1, a2, v3);
sprintf(s, "%s L", v7);
v9 = (const char *)sound_name(a1, "%s L", v8);
sprintf(v15, "%s R", v9);
v10 = *(unsigned __int8 *)(intf + 8);
BYTE1(v10) = 1;
v12[0] = v10;
v11 = *(unsigned __int8 *)(intf + 12);
BYTE1(v11) = 2;
v12[1] = v11;
stream_init_multi(2LL, v13, v12, *(unsigned int *)(Machine + 836LL), 0LL, qsound_update);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
void *v3; // rdx
__int64 i; // rbx
double v5; // xmm0_8
double v6; // xmm0_8
const char *v7; // rax
const char *v8; // rdx
const char *v9; // rax
int v10; // eax
int v11; // eax
int v12[2]; // [rsp+0h] [rbp-80h] BYREF
__int64 v13[2]; // [rsp+8h] [rbp-78h] BYREF
char s[40]; // [rsp+18h] [rbp-68h] BYREF
char v15[64]; // [rsp+40h] [rbp-40h] BYREF

if ( !*(_DWORD *)(Machine + 836LL) )
return 0LL;
intf = *(_QWORD *)(a1 + 8);
qsound_sample_rom = memory_region(*(unsigned int *)(intf + 4));
v3 = &qsound_channel;
memset(&qsound_channel, 0, 0x380uLL);
*(float *)&qsound_frq_ratio = (float)((float)((float)*(int *)intf / 166.0) / (float)*(int *)(Machine + 836LL)) * 16.0;
for ( i = 0LL; i != 33; ++i )
{
v5 = (double)(int)i;
if ( (double)(int)i < 0.0 )
v6 = sqrt(v5);
else
v6 = sqrt(v5);
qsound_pan_table[i] = (int)(v6 * 45.25483399593904);
}
v13[0] = (__int64)s;
v13[1] = (__int64)v15;
v7 = (const char *)sound_name(a1, a2, v3);
sprintf(s, "%s L", v7);
v9 = (const char *)sound_name(a1, "%s L", v8);
sprintf(v15, "%s R", v9);
v10 = *(unsigned __int8 *)(intf + 8);
BYTE1(v10) = 1;
v12[0] = v10;
v11 = *(unsigned __int8 *)(intf + 12);
BYTE1(v11) = 2;
v12[1] = v11;
stream_init_multi(2LL, v13, v12, *(unsigned int *)(Machine + 836LL), 0LL, qsound_update);
return 0LL;
}

original_code:
int qsound_sh_start(const struct MachineSound *msound)
{
 int i;
 if (Machine->sample_rate == 0) return 0;
 intf = (struct QSound_interface*)msound->sound_interface;
 qsound_sample_rom = (QSOUND_SRC_SAMPLE *)memory_region(intf->region);
 memset(qsound_channel, 0, sizeof(qsound_channel));
 qsound_frq_ratio = ((float)intf->clock / (float)166) /
      (float) Machine->sample_rate;
 qsound_frq_ratio *= 16.0;
 for (i=0; i<33; i++)
 {
  qsound_pan_table[i]=(int)((256/sqrt(32)) * sqrt(i));
 }
 {
  char buf[( 2 )][40];
  const char *name[( 2 )];
  int vol[2];
  name[0] = buf[0];
  name[1] = buf[1];
  sprintf( buf[0], "%s L", sound_name(msound) );
  sprintf( buf[1], "%s R", sound_name(msound) );
  vol[0]=((intf->mixing_level[0] & 0xff) | ((1 & 0x03) << 8));
  vol[1]=((intf->mixing_level[1] & 0xff) | ((2 & 0x03) << 8));
  qsound_stream = stream_init_multi(
   ( 2 ),
   name,
   vol,
   Machine->sample_rate,
   0,
   qsound_update );
 }
 return 0;
}

canonical_original_code:
int qsound_sh_start(struct MachineSound *msound)
{
 int i;
 if (Machine->sample_rate == 0) return 0;
 intf = (struct QSound_interface *)msound->sound_interface;
 qsound_sample_rom = (char *)memory_region(intf->region);
 memset(qsound_channel, 0, sizeof(qsound_channel));
 qsound_frq_ratio = ((float)intf->clock / (float)166) /
      (float) Machine->sample_rate;
 qsound_frq_ratio *= 16.0;
 for (i=0; i<33; i++)
 {
  qsound_pan_table[i]=(int)((256/sqrt(32)) * sqrt(i));
 }
 {
  char buf[-1][40];
  char *name[-1];
  int vol[2];
  name[0] = buf[0];
  name[1] = buf[1];
  sprintf( buf[0], "%s L", sound_name(msound) );
  sprintf( buf[1], "%s R", sound_name(msound) );
  vol[0]=((intf->mixing_level[0] & 0xff) | ((1 & 0x03) << 8));
  vol[1]=((intf->mixing_level[1] & 0xff) | ((2 & 0x03) << 8));
  qsound_stream = stream_init_multi(
   ( 2 ),
   name,
   vol,
   Machine->sample_rate,
   0,
   qsound_update );
 }
 return 0;
}

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

variable_types:
name
char *[-1]

variable_types:
vol
int [2]

return_type: int

user_defined_types: struct QSound_interface { int clock; int region; int mixing_level[2]; }


decompiled_code:
void qsound_sh_stop()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void qsound_sh_stop (void)
{
 if (Machine->sample_rate == 0) return;
}

canonical_original_code:
void qsound_sh_stop()
{
 if (Machine->sample_rate == 0) return;
}


decompiled_code:
__int64 __fastcall qsound_data_h_w(__int64 a1, int a2)
{
int v2; // esi
__int64 result; // rax

v2 = a2 << 8;
result = v2 | (unsigned int)(unsigned __int8)qsound_data;
qsound_data = v2 | (unsigned __int8)qsound_data;
return result;
}

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

v2 = a2 << 8;
result = v2 | (unsigned int)(unsigned __int8)qsound_data;
qsound_data = v2 | (unsigned __int8)qsound_data;
return result;
}

original_code:
void qsound_data_h_w(offs_t offset,data_t data)
{
 qsound_data=(qsound_data&0xff)|(data<<8);
}

canonical_original_code:
void qsound_data_h_w(unsigned int offset, unsigned int data)
{
 qsound_data=(qsound_data&0xff)|(data<<8);
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int


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

result = a2 | qsound_data & 0xFF00u;
qsound_data = a2 | qsound_data & 0xFF00;
return result;
}

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

result = a2 | qsound_data & 0xFF00u;
qsound_data = a2 | qsound_data & 0xFF00;
return result;
}

original_code:
void qsound_data_l_w(offs_t offset,data_t data)
{
 qsound_data=(qsound_data&0xff00)|data;
}

canonical_original_code:
void qsound_data_l_w(unsigned int offset, unsigned int data)
{
 qsound_data=(qsound_data&0xff00)|data;
}


decompiled_code:
__int64 qsound_status_r()
{
return 128LL;
}

canonical_decompiled_code:
__int64 func5()
{
return 128LL;
}

original_code:
data_t qsound_status_r(offs_t offset)
{
 return 0x80;
}

canonical_original_code:
unsigned int qsound_status_r(unsigned int offset)
{
 return 0x80;
}

return_type: unsigned int


decompiled_code:
unsigned __int64 __fastcall qsound_set_command(int a1, int a2)
{
unsigned __int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // r8
unsigned int v11; // edx
__int64 v12; // r10

if ( a1 > 127 )
{
LODWORD(result) = a1 - 128;
if ( a1 <= 143 )
{
v11 = ((_BYTE)a2 - 16) & 0x3F;
if ( v11 > 0x20 )
v11 = 32;
result = (int)result;
qsound_channel[14 * (int)result + 10] = qsound_pan_table[v11];
v12 = 7LL * (int)result;
qsound_channel[2 * v12 + 11] = qsound_pan_table[32 - v11];
qsound_channel[2 * v12 + 7] = a2;
}
else
{
result = (unsigned int)(a1 - 186);
if ( (unsigned int)result <= 0xF )
{
v3 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v3 + 8] = a2;
}
}
}
else
{
result = (unsigned int)(a1 >> 3);
switch ( a1 & 7 )
{
case 0:
v4 = 7LL * (((_BYTE)result + 1) & 0xF);
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v4] = (a2 << 16) & 0x7F0000;
break;
case 1:
v5 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v5 + 1] = a2;
break;
case 2:
qsound_channel[14 * (int)result + 2] = (int)(float)((float)a2 * *(float *)&qsound_frq_ratio);
if ( !a2 )
{
result = (int)result;
qsound_channel[14 * (int)result + 9] = 0;
}
break;
case 3:
v6 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v6 + 3] = a2;
break;
case 4:
v7 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v7 + 4] = a2;
break;
case 5:
v8 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v8 + 5] = a2;
break;
case 6:
if ( a2 )
{
if ( !qsound_channel[14 * (int)result + 9] )
{
qsound_channel[14 * (int)result + 9] = 1;
v10 = 7LL * (int)result;
qsound_channel[2 * v10 + 13] = 0;
qsound_channel[2 * v10 + 12] = 0;
}
}
else
{
qsound_channel[14 * (int)result + 9] = 0;
}
v9 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v9 + 6] = a2;
break;
case 7:
return result;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func6(int a1, int a2)
{
unsigned __int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // r8
unsigned int v11; // edx
__int64 v12; // r10

if ( a1 > 127 )
{
LODWORD(result) = a1 - 128;
if ( a1 <= 143 )
{
v11 = ((_BYTE)a2 - 16) & 0x3F;
if ( v11 > 0x20 )
v11 = 32;
result = (int)result;
qsound_channel[14 * (int)result + 10] = qsound_pan_table[v11];
v12 = 7LL * (int)result;
qsound_channel[2 * v12 + 11] = qsound_pan_table[32 - v11];
qsound_channel[2 * v12 + 7] = a2;
}
else
{
result = (unsigned int)(a1 - 186);
if ( (unsigned int)result <= 0xF )
{
v3 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v3 + 8] = a2;
}
}
}
else
{
result = (unsigned int)(a1 >> 3);
switch ( a1 & 7 )
{
case 0:
v4 = 7LL * (((_BYTE)result + 1) & 0xF);
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v4] = (a2 << 16) & 0x7F0000;
break;
case 1:
v5 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v5 + 1] = a2;
break;
case 2:
qsound_channel[14 * (int)result + 2] = (int)(float)((float)a2 * *(float *)&qsound_frq_ratio);
if ( !a2 )
{
result = (int)result;
qsound_channel[14 * (int)result + 9] = 0;
}
break;
case 3:
v6 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v6 + 3] = a2;
break;
case 4:
v7 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v7 + 4] = a2;
break;
case 5:
v8 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v8 + 5] = a2;
break;
case 6:
if ( a2 )
{
if ( !qsound_channel[14 * (int)result + 9] )
{
qsound_channel[14 * (int)result + 9] = 1;
v10 = 7LL * (int)result;
qsound_channel[2 * v10 + 13] = 0;
qsound_channel[2 * v10 + 12] = 0;
}
}
else
{
qsound_channel[14 * (int)result + 9] = 0;
}
v9 = 7LL * (int)result;
result = (unsigned __int64)qsound_channel;
qsound_channel[2 * v9 + 6] = a2;
break;
case 7:
return result;
}
}
return result;
}

original_code:
void qsound_set_command(int data, int value)
{
 int ch=0,reg=0;
 if (data < 0x80)
 {
  ch=data>>3;
  reg=data & 0x07;
 }
 else
 {
  if (data < 0x90)
  {
   ch=data-0x80;
   reg=8;
  }
  else
  {
   if (data >= 0xba && data < 0xca)
   {
    ch=data-0xba;
    reg=9;
   }
   else
   {
    ch=99;
    reg=99;
   }
  }
 }
 switch (reg)
 {
  case 0:
   ch=(ch+1)&0x0f;
   qsound_channel[ch].bank=(value&0x7f)<<16;
   qsound_channel[ch].bank /= 1;
   break;
  case 1:
   qsound_channel[ch].address=value;
   qsound_channel[ch].address/=1;
   break;
  case 2:
   qsound_channel[ch].pitch=(long)
     ((float)value * qsound_frq_ratio );
   qsound_channel[ch].pitch/=1;
   if (!value)
   {
    qsound_channel[ch].key=0;
   }
   break;
  case 3:
   qsound_channel[ch].reg3=value;
   break;
  case 4:
   qsound_channel[ch].loop=value/1;
   break;
  case 5:
   qsound_channel[ch].end=value/1;
   break;
  case 6:
   if (value==0)
   {
    qsound_channel[ch].key=0;
   }
   else if (qsound_channel[ch].key==0)
   {
    qsound_channel[ch].key=1;
    qsound_channel[ch].offset=0;
    qsound_channel[ch].lastdt=0;
   }
   qsound_channel[ch].vol=value;
   break;
  case 7:
   break;
  case 8:
   {
      int pandata=(value-0x10)&0x3f;
      if (pandata > 32)
      {
     pandata=32;
      }
      qsound_channel[ch].lvol=qsound_pan_table[pandata];
      qsound_channel[ch].rvol=qsound_pan_table[32-pandata];
      qsound_channel[ch].pan = value;
   }
   break;
   case 9:
   qsound_channel[ch].reg9=value;
   break;
 }
}

canonical_original_code:
void qsound_set_command(int data, int value)
{
 int ch = 0; int reg = 0;
 if (data < 0x80)
 {
  ch=data>>3;
  reg=data & 0x07;
 }
 else
 {
  if (data < 0x90)
  {
   ch=data-0x80;
   reg=8;
  }
  else
  {
   if (data >= 0xba && data < 0xca)
   {
    ch=data-0xba;
    reg=9;
   }
   else
   {
    ch=99;
    reg=99;
   }
  }
 }
 switch (reg)
 {
  case 0:
   ch=(ch+1)&0x0f;
   qsound_channel[ch].bank=(value&0x7f)<<16;
   qsound_channel[ch].bank /= 1;
   break;
  case 1:
   qsound_channel[ch].address=value;
   qsound_channel[ch].address/=1;
   break;
  case 2:
   qsound_channel[ch].pitch=(long)
     ((float)value * qsound_frq_ratio );
   qsound_channel[ch].pitch/=1;
   if (!value)
   {
    qsound_channel[ch].key=0;
   }
   break;
  case 3:
   qsound_channel[ch].reg3=value;
   break;
  case 4:
   qsound_channel[ch].loop=value/1;
   break;
  case 5:
   qsound_channel[ch].end=value/1;
   break;
  case 6:
   if (value==0)
   {
    qsound_channel[ch].key=0;
   }
   else if (qsound_channel[ch].key==0)
   {
    qsound_channel[ch].key=1;
    qsound_channel[ch].offset=0;
    qsound_channel[ch].lastdt=0;
   }
   qsound_channel[ch].vol=value;
   break;
  case 7:
   break;
  case 8:
   {
      int pandata = (value-0x10)&0x3f;
      if (pandata > 32)
      {
     pandata=32;
      }
      qsound_channel[ch].lvol=qsound_pan_table[pandata];
      qsound_channel[ch].rvol=qsound_pan_table[32-pandata];
      qsound_channel[ch].pan = value;
   }
   break;
   case 9:
   qsound_channel[ch].reg9=value;
   break;
 }
}

variable_types: data int

variable_types: value int

variable_types: ch int

variable_types: reg int

variable_types: pandata int


decompiled_code:
unsigned __int64 __fastcall qsound_cmd_w(__int64 a1, int a2)
{
return qsound_set_command(a2, qsound_data);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(__int64 a1, int a2)
{
return func6(a2, qsound_data);
}

original_code:
void qsound_cmd_w(offs_t offset,data_t data)
{
 qsound_set_command(data, qsound_data);
}

canonical_original_code:
void qsound_cmd_w(unsigned int offset, unsigned int data)
{
 qsound_set_command(data, qsound_data);
}


decompiled_code:
void __fastcall soundlatch_callback(int a1)
{
latch = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
latch = a1;
}

original_code:
static void soundlatch_callback(int param)
{
 latch = param;
}

variable_types: param int


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

result = (unsigned int)latch;
latch = 0;
return result;
}

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

result = (unsigned int)latch;
latch = 0;
return result;
}

original_code:
data_t bombjack_soundlatch_r(offs_t offset)
{
 int res;
 res = latch;
 latch = 0;
 return res;
}

canonical_original_code:
unsigned int bombjack_soundlatch_r(unsigned int offset)
{
 int res;
 res = latch;
 latch = 0;
 return res;
}

variable_types: res int


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

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

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

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

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

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


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

result = (void *)(a2 & 1);
if ( (_DWORD)result != flipscreen )
{
flipscreen = a2 & 1;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

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

result = (void *)(a2 & 1);
if ( (_DWORD)result != flipscreen )
{
flipscreen = a2 & 1;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

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

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


decompiled_code:
void *__fastcall bombjack_vh_screenrefresh(__int64 a1)
{
int v1; // er14
__int64 v2; // r15
int v3; // ebp
void *result; // rax
int v5; // ebp
__int64 v6; // rbx
__int64 v7; // rdx
__int64 v8; // rdi
__int64 v9; // r8
__int64 v10; // r9
char v11; // al
unsigned __int8 v12; // cl
__int64 v13; // rbx
__int64 v14; // rbp
__int64 v15; // r9
int v16; // ebx
int v17; // er12
unsigned int v18; // er13
unsigned int v19; // ecx
int v20; // ebp
int v21; // edx
int v22; // [rsp+4h] [rbp-54h]
int v24; // [rsp+10h] [rbp-48h] BYREF
int v25; // [rsp+14h] [rbp-44h]
int v26; // [rsp+18h] [rbp-40h]
int v27; // [rsp+1Ch] [rbp-3Ch]

if ( palette_recalc() )
memset(dirtybuffer, 1, videoram_size);
v22 = (background_image & 7) << 9;
v1 = videoram_size - 1;
if ( (int)videoram_size - 1 >= 0 )
{
v2 = v1;
do
{
v16 = v1 % 32;
v17 = v1 % 32;
v18 = 255;
v19 = background_image & 0x10;
if ( (background_image & 0x10) != 0 )
{
v3 = 16 * (v1 / 64) + v22 + v16 / 2;
v18 = *(unsigned __int8 *)(memory_region(140LL) + v3);
v19 = *(unsigned __int8 *)(memory_region(140LL) + v3 + 256);
}
if ( !*((_BYTE *)dirtybuffer + v2) )
goto LABEL_21;
v20 = v1 / 32;
v21 = v1 / 32;
if ( flipscreen )
{
v17 = 31 - v16;
v21 = 31 - v20;
if ( v18 != 255 )
{
v13 = (unsigned int)(8 * v17);
v24 = v13;
v25 = v13 + 7;
v14 = (unsigned int)(8 * v21);
v26 = v14;
v27 = v14 + 7;
v15 = (v19 >> 7) ^ 1;
goto LABEL_19;
}
}
else if ( v18 != 255 )
{
v13 = (unsigned int)(8 * v16);
v24 = v13;
v25 = v13 + 7;
v14 = (unsigned int)(8 * v20);
v26 = v14;
v27 = v14 + 7;
v15 = (unsigned __int8)v19 & 0x80;
LABEL_19:
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 520LL),
v18,
v19 & 0xF,
(unsigned int)flipscreen,
v15,
(unsigned int)(16 * (v17 / 2)),
(unsigned int)(16 * (v21 / 2)),
&v24,
0LL,
0LL);
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 512LL),
*(unsigned __int8 *)(videoram + v2) + 16 * (*(_BYTE *)(colorram + v2) & 0x10u),
*(_BYTE *)(colorram + v2) & 0xF,
(unsigned int)flipscreen,
(unsigned int)flipscreen,
v13,
v14,
Machine + 776LL,
2LL,
0LL);
goto LABEL_20;
}
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 512LL),
16 * (*(_BYTE *)(colorram + v2) & 0x10) + (unsigned int)*(unsigned __int8 *)(videoram + v2),
*(_BYTE *)(colorram + v2) & 0xF,
(unsigned int)flipscreen,
(unsigned int)flipscreen,
(unsigned int)(8 * v17),
(unsigned int)(8 * v21),
Machine + 776LL,
0LL,
0LL);
LABEL_20:
*((_BYTE *)dirtybuffer + v2) = 0;
LABEL_21:
--v2;
--v1;
}
while ( v1 >= 0 );
}
copybitmap(a1, tmpbitmap, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 0LL, 0LL);
result = &spriteram_size;
v5 = spriteram_size - 4;
if ( spriteram_size - 4 >= 0 )
{
v6 = v5 + 2LL;
do
{
v8 = *(unsigned __int8 *)(spriteram + v6 + 1);
v11 = *(_BYTE *)(spriteram + v6 - 2);
if ( v11 < 0 )
v7 = 225 - (unsigned int)*(unsigned __int8 *)(spriteram + v6);
else
v7 = 241 - (unsigned int)*(unsigned __int8 *)(spriteram + v6);
v12 = *(_BYTE *)(spriteram + v6 - 1);
v9 = v12 & 0x40;
v10 = v12 & 0x80;
if ( flipscreen )
{
if ( (v12 & 0x20) != 0 )
{
v8 = (unsigned int)(224 - v8);
v7 = (unsigned int)(224 - v7);
}
else
{
v8 = (unsigned int)(240 - v8);
v7 = (unsigned int)(240 - v7);
}
v9 = (_DWORD)v9 == 0;
v10 = (_DWORD)v10 == 0;
}
result = (void *)drawgfx(
a1,
*(_QWORD *)(Machine + 8LL * (int)(((unsigned int)v11 >> 31) + 2) + 512),
v11 & 0x7F,
*(_BYTE *)(spriteram + v6 - 1) & 0xF,
v9,
v10,
v8,
v7,
Machine + 776LL,
2LL,
0LL);
v6 -= 4LL;
v5 -= 4;
}
while ( v5 >= 0 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(__int64 a1)
{
int v1; // er14
__int64 v2; // r15
int v3; // ebp
void *result; // rax
int v5; // ebp
__int64 v6; // rbx
__int64 v7; // rdx
__int64 v8; // rdi
__int64 v9; // r8
__int64 v10; // r9
char v11; // al
unsigned __int8 v12; // cl
__int64 v13; // rbx
__int64 v14; // rbp
__int64 v15; // r9
int v16; // ebx
int v17; // er12
unsigned int v18; // er13
unsigned int v19; // ecx
int v20; // ebp
int v21; // edx
int v22; // [rsp+4h] [rbp-54h]
int v24; // [rsp+10h] [rbp-48h] BYREF
int v25; // [rsp+14h] [rbp-44h]
int v26; // [rsp+18h] [rbp-40h]
int v27; // [rsp+1Ch] [rbp-3Ch]

if ( palette_recalc() )
memset(dirtybuffer, 1, videoram_size);
v22 = (background_image & 7) << 9;
v1 = videoram_size - 1;
if ( (int)videoram_size - 1 >= 0 )
{
v2 = v1;
do
{
v16 = v1 % 32;
v17 = v1 % 32;
v18 = 255;
v19 = background_image & 0x10;
if ( (background_image & 0x10) != 0 )
{
v3 = 16 * (v1 / 64) + v22 + v16 / 2;
v18 = *(unsigned __int8 *)(memory_region(140LL) + v3);
v19 = *(unsigned __int8 *)(memory_region(140LL) + v3 + 256);
}
if ( !*((_BYTE *)dirtybuffer + v2) )
goto LABEL_21;
v20 = v1 / 32;
v21 = v1 / 32;
if ( flipscreen )
{
v17 = 31 - v16;
v21 = 31 - v20;
if ( v18 != 255 )
{
v13 = (unsigned int)(8 * v17);
v24 = v13;
v25 = v13 + 7;
v14 = (unsigned int)(8 * v21);
v26 = v14;
v27 = v14 + 7;
v15 = (v19 >> 7) ^ 1;
goto LABEL_19;
}
}
else if ( v18 != 255 )
{
v13 = (unsigned int)(8 * v16);
v24 = v13;
v25 = v13 + 7;
v14 = (unsigned int)(8 * v20);
v26 = v14;
v27 = v14 + 7;
v15 = (unsigned __int8)v19 & 0x80;
LABEL_19:
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 520LL),
v18,
v19 & 0xF,
(unsigned int)flipscreen,
v15,
(unsigned int)(16 * (v17 / 2)),
(unsigned int)(16 * (v21 / 2)),
&v24,
0LL,
0LL);
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 512LL),
*(unsigned __int8 *)(videoram + v2) + 16 * (*(_BYTE *)(colorram + v2) & 0x10u),
*(_BYTE *)(colorram + v2) & 0xF,
(unsigned int)flipscreen,
(unsigned int)flipscreen,
v13,
v14,
Machine + 776LL,
2LL,
0LL);
goto LABEL_20;
}
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 512LL),
16 * (*(_BYTE *)(colorram + v2) & 0x10) + (unsigned int)*(unsigned __int8 *)(videoram + v2),
*(_BYTE *)(colorram + v2) & 0xF,
(unsigned int)flipscreen,
(unsigned int)flipscreen,
(unsigned int)(8 * v17),
(unsigned int)(8 * v21),
Machine + 776LL,
0LL,
0LL);
LABEL_20:
*((_BYTE *)dirtybuffer + v2) = 0;
LABEL_21:
--v2;
--v1;
}
while ( v1 >= 0 );
}
copybitmap(a1, tmpbitmap, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 0LL, 0LL);
result = &spriteram_size;
v5 = spriteram_size - 4;
if ( spriteram_size - 4 >= 0 )
{
v6 = v5 + 2LL;
do
{
v8 = *(unsigned __int8 *)(spriteram + v6 + 1);
v11 = *(_BYTE *)(spriteram + v6 - 2);
if ( v11 < 0 )
v7 = 225 - (unsigned int)*(unsigned __int8 *)(spriteram + v6);
else
v7 = 241 - (unsigned int)*(unsigned __int8 *)(spriteram + v6);
v12 = *(_BYTE *)(spriteram + v6 - 1);
v9 = v12 & 0x40;
v10 = v12 & 0x80;
if ( flipscreen )
{
if ( (v12 & 0x20) != 0 )
{
v8 = (unsigned int)(224 - v8);
v7 = (unsigned int)(224 - v7);
}
else
{
v8 = (unsigned int)(240 - v8);
v7 = (unsigned int)(240 - v7);
}
v9 = (_DWORD)v9 == 0;
v10 = (_DWORD)v10 == 0;
}
result = (void *)drawgfx(
a1,
*(_QWORD *)(Machine + 8LL * (int)(((unsigned int)v11 >> 31) + 2) + 512),
v11 & 0x7F,
*(_BYTE *)(spriteram + v6 - 1) & 0xF,
v9,
v10,
v8,
v7,
Machine + 776LL,
2LL,
0LL);
v6 -= 4LL;
v5 -= 4;
}
while ( v5 >= 0 );
}
return result;
}

original_code:
void bombjack_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int offs,base;
 if (palette_recalc())
  memset(dirtybuffer,1,videoram_size);
 base = 0x200 * (background_image & 0x07);
 for (offs = videoram_size - 1;offs >= 0;offs--)
 {
  int sx,sy;
  int tilecode,tileattribute;
  sx = offs % 32;
  sy = offs / 32;
  if (background_image & 0x10)
  {
   int bgoffs;
   bgoffs = base+16*(sy/2)+sx/2;
   tilecode = memory_region(REGION_GFX4)[bgoffs],
   tileattribute = memory_region(REGION_GFX4)[bgoffs + 0x100];
  }
  else
  {
   tilecode = 0xff;
   tileattribute = 0;
  }
  if (dirtybuffer[offs])
  {
   if (flipscreen)
   {
    sx = 31 - sx;
    sy = 31 - sy;
   }
   if (tilecode != 0xff)
   {
    struct rectangle clip;
    int flipy;
    clip.min_x = 8*sx;
    clip.max_x = 8*sx+7;
    clip.min_y = 8*sy;
    clip.max_y = 8*sy+7;
    flipy = tileattribute & 0x80;
    if (flipscreen) flipy = !flipy;
    drawgfx(tmpbitmap,Machine->gfx[1],
      tilecode,
      tileattribute & 0x0f,
      flipscreen,flipy,
      16*(sx/2),16*(sy/2),
      &clip,TRANSPARENCY_NONE,0);
    drawgfx(tmpbitmap,Machine->gfx[0],
      videoram[offs] + 16 * (colorram[offs] & 0x10),
      colorram[offs] & 0x0f,
      flipscreen,flipscreen,
      8*sx,8*sy,
      &Machine->visible_area,TRANSPARENCY_PEN,0);
   }
   else
    drawgfx(tmpbitmap,Machine->gfx[0],
      videoram[offs] + 16 * (colorram[offs] & 0x10),
      colorram[offs] & 0x0f,
      flipscreen,flipscreen,
      8*sx,8*sy,
      &Machine->visible_area,TRANSPARENCY_NONE,0);
   dirtybuffer[offs] = 0;
  }
 }
 copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
 for (offs = spriteram_size - 4;offs >= 0;offs -= 4)
 {
  int sx,sy,flipx,flipy;
  sx = spriteram[offs+3];
  if (spriteram[offs] & 0x80)
   sy = 225-spriteram[offs+2];
  else
   sy = 241-spriteram[offs+2];
  flipx = spriteram[offs+1] & 0x40;
  flipy = spriteram[offs+1] & 0x80;
  if (flipscreen)
  {
   if (spriteram[offs+1] & 0x20)
   {
    sx = 224 - sx;
    sy = 224 - sy;
   }
   else
   {
    sx = 240 - sx;
    sy = 240 - sy;
   }
   flipx = !flipx;
   flipy = !flipy;
  }
  drawgfx(bitmap,Machine->gfx[(spriteram[offs] & 0x80) ? 3 : 2],
    spriteram[offs] & 0x7f,
    spriteram[offs+1] & 0x0f,
    flipx,flipy,
    sx,sy,
    &Machine->visible_area,TRANSPARENCY_PEN,0);
 }
}

canonical_original_code:
void bombjack_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int offs; int base;
 if (palette_recalc())
  memset(dirtybuffer,1,videoram_size);
 base = 0x200 * (background_image & 0x07);
 for (offs = videoram_size - 1;offs >= 0;offs--)
 {
  int sx; int sy;
  int tilecode; int tileattribute;
  sx = offs % 32;
  sy = offs / 32;
  if (background_image & 0x10)
  {
   int bgoffs;
   bgoffs = base+16*(sy/2)+sx/2;
   tilecode = memory_region(REGION_GFX4)[bgoffs],
   tileattribute = memory_region(REGION_GFX4)[bgoffs + 0x100];
  }
  else
  {
   tilecode = 0xff;
   tileattribute = 0;
  }
  if (dirtybuffer[offs])
  {
   if (flipscreen)
   {
    sx = 31 - sx;
    sy = 31 - sy;
   }
   if (tilecode != 0xff)
   {
    struct rectangle clip;
    int flipy;
    clip.min_x = 8*sx;
    clip.max_x = 8*sx+7;
    clip.min_y = 8*sy;
    clip.max_y = 8*sy+7;
    flipy = tileattribute & 0x80;
    if (flipscreen) flipy = !flipy;
    drawgfx(tmpbitmap,Machine->gfx[1],
      tilecode,
      tileattribute & 0x0f,
      flipscreen,flipy,
      16*(sx/2),16*(sy/2),
      &clip,TRANSPARENCY_NONE,0);
    drawgfx(tmpbitmap,Machine->gfx[0],
      videoram[offs] + 16 * (colorram[offs] & 0x10),
      colorram[offs] & 0x0f,
      flipscreen,flipscreen,
      8*sx,8*sy,
      &Machine->visible_area,TRANSPARENCY_PEN,0);
   }
   else
    drawgfx(tmpbitmap,Machine->gfx[0],
      videoram[offs] + 16 * (colorram[offs] & 0x10),
      colorram[offs] & 0x0f,
      flipscreen,flipscreen,
      8*sx,8*sy,
      &Machine->visible_area,TRANSPARENCY_NONE,0);
   dirtybuffer[offs] = 0;
  }
 }
 copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
 for (offs = spriteram_size - 4;offs >= 0;offs -= 4)
 {
  int sx; int sy; int flipx; int flipy;
  sx = spriteram[offs+3];
  if (spriteram[offs] & 0x80)
   sy = 225-spriteram[offs+2];
  else
   sy = 241-spriteram[offs+2];
  flipx = spriteram[offs+1] & 0x40;
  flipy = spriteram[offs+1] & 0x80;
  if (flipscreen)
  {
   if (spriteram[offs+1] & 0x20)
   {
    sx = 224 - sx;
    sy = 224 - sy;
   }
   else
   {
    sx = 240 - sx;
    sy = 240 - sy;
   }
   flipx = !flipx;
   flipy = !flipy;
  }
  drawgfx(bitmap,Machine->gfx[(spriteram[offs] & 0x80) ? 3 : 2],
    spriteram[offs] & 0x7f,
    spriteram[offs+1] & 0x0f,
    flipx,flipy,
    sx,sy,
    &Machine->visible_area,TRANSPARENCY_PEN,0);
 }
}

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

variable_types: offs int

variable_types: base int

variable_types: sx int

variable_types: sy int

variable_types: tilecode int

variable_types: tileattribute int

variable_types: bgoffs int

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

variable_types: flipy int

variable_types: flipx int


decompiled_code:
__int64 __fastcall bombjack_soundlatch_w(__int64 a1, __int64 a2)
{
return timer_set(0LL, a2, soundlatch_callback);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
return timer_set(0LL, a2, soundlatch_callback);
}

original_code:
void bombjack_soundlatch_w(offs_t offset,data_t data)
{
 timer_set((0),data,soundlatch_callback);
}

canonical_original_code:
void bombjack_soundlatch_w(unsigned int offset, unsigned int data)
{
 timer_set((0),data,soundlatch_callback);
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int


decompiled_code:
void __fastcall select_analog_ctrl_w(__int64 a1, int a2)
{
analog_ctrl = a2;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, int a2)
{
analog_ctrl = a2;
}

original_code:
static void select_analog_ctrl_w(offs_t offset,data_t data)
{
 analog_ctrl = data;
}

canonical_original_code:
static void select_analog_ctrl_w(unsigned int offset, unsigned int data)
{
 analog_ctrl = data;
}


decompiled_code:
__int64 chqflag_vh_start()
{
unsigned int v0; // ebx

sprite_colorbase = 0;
zoom_colorbase[0] = 16;
zoom_colorbase[1] = 4;
v0 = 1;
if ( !(unsigned int)K051960_vh_start(137LL, 0LL, 1LL, 2LL, 3LL, sprite_callback) )
{
if ( (unsigned int)K051316_vh_start_0(138LL, 4LL, zoom_callback_0) )
{
K051960_vh_stop();
}
else
{
v0 = K051316_vh_start_1(139LL, 7LL, zoom_callback_1);
if ( v0 )
{
K051960_vh_stop();
K051316_vh_stop_0();
v0 = 1;
}
else
{
K051316_set_offset(0LL, 8LL, 0LL);
K051316_wraparound_enable(1LL, 1LL);
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // ebx

sprite_colorbase = 0;
zoom_colorbase[0] = 16;
zoom_colorbase[1] = 4;
v0 = 1;
if ( !(unsigned int)K051960_vh_start(137LL, 0LL, 1LL, 2LL, 3LL, sprite_callback) )
{
if ( (unsigned int)K051316_vh_start_0(138LL, 4LL, zoom_callback_0) )
{
K051960_vh_stop();
}
else
{
v0 = K051316_vh_start_1(139LL, 7LL, zoom_callback_1);
if ( v0 )
{
K051960_vh_stop();
K051316_vh_stop_0();
v0 = 1;
}
else
{
K051316_set_offset(0LL, 8LL, 0LL);
K051316_wraparound_enable(1LL, 1LL);
}
}
}
return v0;
}

original_code:
int chqflag_vh_start( void )
{
 sprite_colorbase = 0;
 zoom_colorbase[0] = 0x10;
 zoom_colorbase[1] = 0x04;
 if (K051960_vh_start(REGION_GFX1,0,1,2,3,sprite_callback))
 {
  return 1;
 }
 if (K051316_vh_start_0(REGION_GFX2,4,zoom_callback_0))
 {
  K051960_vh_stop();
  return 1;
 }
 if (K051316_vh_start_1(REGION_GFX3,7,zoom_callback_1))
 {
  K051960_vh_stop();
  K051316_vh_stop_0();
  return 1;
 }
 K051316_set_offset(0, 8, 0);
 K051316_wraparound_enable(1, 1);
 return 0;
}

canonical_original_code:
int chqflag_vh_start()
{
 sprite_colorbase = 0;
 zoom_colorbase[0] = 0x10;
 zoom_colorbase[1] = 0x04;
 if (K051960_vh_start(REGION_GFX1,0,1,2,3,sprite_callback))
 {
  return 1;
 }
 if (K051316_vh_start_0(REGION_GFX2,4,zoom_callback_0))
 {
  K051960_vh_stop();
  return 1;
 }
 if (K051316_vh_start_1(REGION_GFX3,7,zoom_callback_1))
 {
  K051960_vh_stop();
  K051316_vh_stop_0();
  return 1;
 }
 K051316_set_offset(0, 8, 0);
 K051316_wraparound_enable(1, 1);
 return 0;
}


decompiled_code:
__int64 __fastcall chqflag_vh_screenrefresh(__int64 a1)
{
int i; // edx

K051316_tilemap_update_0();
K051316_tilemap_update_1();
palette_init_used_colors();
K051960_mark_sprites_colors();
for ( i = 0; i != 16; ++i )
*(_BYTE *)(palette_used_colors + 16 * (zoom_colorbase[0] + i)) = 7;
if ( palette_recalc(zoom_colorbase, &palette_used_colors) )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
fillbitmap(a1, **(unsigned __int16 **)(Machine + 792LL), Machine + 776LL);
K051316_zoom_draw_1(a1, 0LL);
K051960_sprites_draw(a1, 0LL, 128LL);
return K051316_zoom_draw_0(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int i; // edx

K051316_tilemap_update_0();
K051316_tilemap_update_1();
palette_init_used_colors();
K051960_mark_sprites_colors();
for ( i = 0; i != 16; ++i )
*(_BYTE *)(palette_used_colors + 16 * (zoom_colorbase[0] + i)) = 7;
if ( palette_recalc(zoom_colorbase, &palette_used_colors) )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
fillbitmap(a1, **(unsigned __int16 **)(Machine + 792LL), Machine + 776LL);
K051316_zoom_draw_1(a1, 0LL);
K051960_sprites_draw(a1, 0LL, 128LL);
return K051316_zoom_draw_0(a1, 0LL);
}

original_code:
void chqflag_vh_screenrefresh( struct osd_bitmap *bitmap, int fullrefresh )
{
 int i;
 K051316_tilemap_update_0();
 K051316_tilemap_update_1();
 palette_init_used_colors();
 K051960_mark_sprites_colors();
 for (i = 0; i < 16; i++){
  palette_used_colors[(zoom_colorbase[0] + i) * 16] = (4|(1|2));
 }
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 fillbitmap(bitmap,Machine->pens[0],&Machine->visible_area);
 K051316_zoom_draw_1(bitmap,0);
 K051960_sprites_draw(bitmap,0,128);
 K051316_zoom_draw_0(bitmap,0);
}

canonical_original_code:
void chqflag_vh_screenrefresh(struct osd_bitmap *bitmap, int fullrefresh)
{
 int i;
 K051316_tilemap_update_0();
 K051316_tilemap_update_1();
 palette_init_used_colors();
 K051960_mark_sprites_colors();
 for (i = 0; i < 16; i++){
  palette_used_colors[(zoom_colorbase[0] + i) * 16] = (4|(1|2));
 }
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 fillbitmap(bitmap,Machine->pens[0],&Machine->visible_area);
 K051316_zoom_draw_1(bitmap,0);
 K051960_sprites_draw(bitmap,0,128);
 K051316_zoom_draw_0(bitmap,0);
}

variable_types: fullrefresh int

variable_types: i int

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


decompiled_code:
__int64 chqflag_sh_irqtrigger_w()
{
return cpu_cause_interrupt(1LL, 4294966296LL);
}

canonical_decompiled_code:
__int64 func7()
{
return cpu_cause_interrupt(1LL, 4294966296LL);
}

original_code:
void chqflag_sh_irqtrigger_w(offs_t offset,data_t data)
{
 cpu_cause_interrupt(1,-1000);
}

canonical_original_code:
void chqflag_sh_irqtrigger_w(unsigned int offset, unsigned int data)
{
 cpu_cause_interrupt(1,-1000);
}


decompiled_code:
__int64 chqflag_ym2151_irq_w()
{
return cpu_cause_interrupt(1LL, 4294967294LL);
}

canonical_decompiled_code:
__int64 func8()
{
return cpu_cause_interrupt(1LL, 4294967294LL);
}

original_code:
static void chqflag_ym2151_irq_w(int data)
{
 cpu_cause_interrupt(1,-2);
}

variable_types: data int


decompiled_code:
_QWORD *init_chqflag()
{
__int64 v0; // rbx
_QWORD *result; // rax

v0 = memory_region(129LL);
konami_rom_deinterleave_2(137LL);
result = &paletteram;
paletteram = v0 + 360448;
return result;
}

canonical_decompiled_code:
_QWORD *func9()
{
__int64 v0; // rbx
_QWORD *result; // rax

v0 = memory_region(129LL);
konami_rom_deinterleave_2(137LL);
result = &paletteram;
paletteram = v0 + 360448;
return result;
}

original_code:
static void init_chqflag(void)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 konami_rom_deinterleave_2(REGION_GFX1);
 paletteram = &RAM[0x58000];
}

canonical_original_code:
static void init_chqflag()
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 konami_rom_deinterleave_2(REGION_GFX1);
 paletteram = &RAM[0x58000];
}

variable_types:
RAM
unsigned char *


original_code:
static void k007232_extvolume_w(offs_t offset,data_t data)
{
 K007232_set_volume(0,0,(data >> 4)*0x11/2,(data & 0x0f)*0x11/2);
}

canonical_original_code:
static void k007232_extvolume_w(unsigned int offset, unsigned int data)
{
 K007232_set_volume(0,0,(data >> 4)*0x11/2,(data & 0x0f)*0x11/2);
}


original_code:
static void k007232_bankswitch_w(offs_t offset,data_t data)
{
 unsigned char *RAM;
 int bank_A, bank_B;
 RAM = memory_region(REGION_SOUND1);
 bank_A = 0x20000*((data >> 4) & 0x03);
 bank_B = 0x20000*((data >> 6) & 0x03);
 K007232_bankswitch(0,RAM + bank_A,RAM + bank_B);
 RAM = memory_region(REGION_SOUND2);
 bank_A = 0x20000*((data >> 0) & 0x03);
 bank_B = 0x20000*((data >> 2) & 0x03);
 K007232_bankswitch(1,RAM + bank_A,RAM + bank_B);
}

canonical_original_code:
static void k007232_bankswitch_w(unsigned int offset, unsigned int data)
{
 unsigned char *RAM;
 int bank_A; int bank_B;
 RAM = memory_region(REGION_SOUND1);
 bank_A = 0x20000*((data >> 4) & 0x03);
 bank_B = 0x20000*((data >> 6) & 0x03);
 K007232_bankswitch(0,RAM + bank_A,RAM + bank_B);
 RAM = memory_region(REGION_SOUND2);
 bank_A = 0x20000*((data >> 0) & 0x03);
 bank_B = 0x20000*((data >> 2) & 0x03);
 K007232_bankswitch(1,RAM + bank_A,RAM + bank_B);
}


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

if ( (unsigned int)cpu_getiloops() )
{
if ( (cpu_getiloops() & 1) != 0 && (unsigned int)K051960_is_NMI_enabled() )
return nmi_interrupt();
return ignore_interrupt();
}
v0 = K051960_is_IRQ_enabled();
result = 1LL;
if ( !v0 )
return ignore_interrupt();
return result;
}

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

if ( (unsigned int)cpu_getiloops() )
{
if ( (cpu_getiloops() & 1) != 0 && (unsigned int)K051960_is_NMI_enabled() )
return nmi_interrupt();
return ignore_interrupt();
}
v0 = K051960_is_IRQ_enabled();
result = 1LL;
if ( !v0 )
return ignore_interrupt();
return result;
}

original_code:
static int chqflag_interrupt( void )
{
 if (cpu_getiloops() == 0){
  if (K051960_is_IRQ_enabled()) return 1;
 }
 else if (cpu_getiloops() % 2){
  if (K051960_is_NMI_enabled()) return nmi_interrupt();
 }
 return ignore_interrupt();
}

canonical_original_code:
static int chqflag_interrupt()
{
 if (cpu_getiloops() == 0){
  if (K051960_is_IRQ_enabled()) return 1;
 }
 else if (cpu_getiloops() % 2){
  if (K051960_is_NMI_enabled()) return nmi_interrupt();
 }
 return ignore_interrupt();
}


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

coin_counter_w(1LL, a2 & 1);
coin_counter_w(0LL, a2 & 2);
K051316_readroms = a2 & 0x10;
if ( (a2 & 0x10) != 0 )
result = cpu_setbankhandler_r(3LL, &K051316_rom_1_r);
else
result = cpu_setbankhandler_r(3LL, &K051316_1_r);
return result;
}

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

coin_counter_w(1LL, a2 & 1);
coin_counter_w(0LL, a2 & 2);
K051316_readroms = a2 & 0x10;
if ( (a2 & 0x10) != 0 )
result = cpu_setbankhandler_r(3LL, &K051316_rom_1_r);
else
result = cpu_setbankhandler_r(3LL, &K051316_1_r);
return result;
}

original_code:
static void chqflag_vreg_w(offs_t offset,data_t data)
{
 coin_counter_w(1,data & 0x01);
 coin_counter_w(0,data & 0x02);
 if ((K051316_readroms = (data & 0x10))){
  cpu_setbankhandler_r (3, K051316_rom_1_r);
 }
 else{
  cpu_setbankhandler_r (3, K051316_1_r);
 }
}

canonical_original_code:
static void chqflag_vreg_w(unsigned int offset, unsigned int data)
{
 coin_counter_w(1,data & 0x01);
 coin_counter_w(0,data & 0x02);
 if ((K051316_readroms = (data & 0x10))){
  cpu_setbankhandler_r (3, K051316_rom_1_r);
 }
 else{
  cpu_setbankhandler_r (3, K051316_1_r);
 }
}


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

cpu_bankbase[4] = memory_region(129LL) + (((a2 & 0x1F) + 4) << 14);
if ( ophw == 4 )
{
ophw = -1;
v3 = cpu_get_pc();
cpu_setOPbase16(v3);
}
if ( (a2 & 0x20) != 0 )
{
cpu_setbankhandler_r(2LL, &paletteram_r);
cpu_setbankhandler_w(2LL, &paletteram_xBBBBBGGGGGRRRRR_swap_w);
if ( K051316_readroms )
cpu_setbankhandler_r(1LL, &K051316_rom_0_r);
else
cpu_setbankhandler_r(1LL, &K051316_0_r);
result = cpu_setbankhandler_w(1LL, &K051316_0_w);
}
else
{
cpu_setbankhandler_r(1LL, -1LL);
cpu_setbankhandler_w(1LL, -1LL);
cpu_setbankhandler_r(2LL, -1LL);
result = cpu_setbankhandler_w(2LL, -1LL);
}
return result;
}

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

cpu_bankbase[4] = memory_region(129LL) + (((a2 & 0x1F) + 4) << 14);
if ( ophw == 4 )
{
ophw = -1;
v3 = cpu_get_pc();
cpu_setOPbase16(v3);
}
if ( (a2 & 0x20) != 0 )
{
cpu_setbankhandler_r(2LL, &paletteram_r);
cpu_setbankhandler_w(2LL, &paletteram_xBBBBBGGGGGRRRRR_swap_w);
if ( K051316_readroms )
cpu_setbankhandler_r(1LL, &K051316_rom_0_r);
else
cpu_setbankhandler_r(1LL, &K051316_0_r);
result = cpu_setbankhandler_w(1LL, &K051316_0_w);
}
else
{
cpu_setbankhandler_r(1LL, -1LL);
cpu_setbankhandler_w(1LL, -1LL);
cpu_setbankhandler_r(2LL, -1LL);
result = cpu_setbankhandler_w(2LL, -1LL);
}
return result;
}

original_code:
static void chqflag_bankswitch_w(offs_t offset,data_t data)
{
 int bankaddress;
 unsigned char *RAM = memory_region(REGION_CPU1);
 bankaddress = 0x10000 + (data & 0x1f)*0x4000;
 { if (4 >= 1 && 4 <= 16) { cpu_bankbase[4] = (UINT8 *)(&RAM[bankaddress]); if (ophw == 4) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
 if (data & 0x20){
  cpu_setbankhandler_r (2, paletteram_r);
  cpu_setbankhandler_w (2, paletteram_xBBBBBGGGGGRRRRR_swap_w);
  if (K051316_readroms){
   cpu_setbankhandler_r (1, K051316_rom_0_r);
   cpu_setbankhandler_w (1, K051316_0_w);
  }
  else{
   cpu_setbankhandler_r (1, K051316_0_r);
   cpu_setbankhandler_w (1, K051316_0_w);
  }
 }
 else{
  cpu_setbankhandler_r (1, ((mem_read_handler)-1));
  cpu_setbankhandler_w (1, ((mem_write_handler)-1));
  cpu_setbankhandler_r (2, ((mem_read_handler)-1));
  cpu_setbankhandler_w (2, ((mem_write_handler)-1));
 }
}

canonical_original_code:
static void chqflag_bankswitch_w(unsigned int offset, unsigned int data)
{
 int bankaddress;
 unsigned char *RAM = memory_region(REGION_CPU1);
 bankaddress = 0x10000 + (data & 0x1f)*0x4000;
 { if (4 >= 1 && 4 <= 16) { cpu_bankbase[4] = (unsigned char *)(&RAM[bankaddress]); if (ophw == 4) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
 if (data & 0x20){
  cpu_setbankhandler_r (2, paletteram_r);
  cpu_setbankhandler_w (2, paletteram_xBBBBBGGGGGRRRRR_swap_w);
  if (K051316_readroms){
   cpu_setbankhandler_r (1, K051316_rom_0_r);
   cpu_setbankhandler_w (1, K051316_0_w);
  }
  else{
   cpu_setbankhandler_r (1, K051316_0_r);
   cpu_setbankhandler_w (1, K051316_0_w);
  }
 }
 else{
  cpu_setbankhandler_r (1, ((mem_read_handler)-1));
  cpu_setbankhandler_w (1, ((mem_write_handler)-1));
  cpu_setbankhandler_r (2, ((mem_read_handler)-1));
  cpu_setbankhandler_w (2, ((mem_write_handler)-1));
 }
}

variable_types: bankaddress int


decompiled_code:
__int64 __fastcall analog_read_r(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
unsigned int v3; // edx

if ( (analog_ctrl & 3) == 2 )
return (unsigned int)accel_1;
v2 = wheel_0;
if ( (analog_ctrl & 3u) > 2 )
return v2;
if ( (analog_ctrl & 3) != 0 )
{
v3 = readinputport(6LL, a2, (unsigned int)wheel_0);
wheel_0 = v3;
}
else
{
v3 = readinputport(5LL, a2, (unsigned int)wheel_0);
accel_1 = v3;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
unsigned int v3; // edx

if ( (analog_ctrl & 3) == 2 )
return (unsigned int)accel_1;
v2 = wheel_0;
if ( (analog_ctrl & 3u) > 2 )
return v2;
if ( (analog_ctrl & 3) != 0 )
{
v3 = readinputport(6LL, a2, (unsigned int)wheel_0);
wheel_0 = v3;
}
else
{
v3 = readinputport(5LL, a2, (unsigned int)wheel_0);
accel_1 = v3;
}
return v3;
}

original_code:
static data_t analog_read_r(offs_t offset)
{
 static int accel, wheel;
 switch (analog_ctrl & 0x03){
  case 0x00: return (accel = readinputport(5));
  case 0x01: return (wheel = readinputport(6));
  case 0x02: return accel;
  case 0x03: return wheel;
 }
 return 0xff;
}

canonical_original_code:
static unsigned int analog_read_r(unsigned int offset)
{
 static int accel; static int wheel;
 switch (analog_ctrl & 0x03){
  case 0x00: return (accel = readinputport(5));
  case 0x01: return (wheel = readinputport(6));
  case 0x02: return accel;
  case 0x03: return wheel;
 }
 return 0xff;
}

variable_types: accel int

variable_types: wheel int


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

*a1 |= ((unsigned __int16)*a2 << 8) & 0x300;
result = zoom_colorbase + ((*a2 >> 2) & 0xFu);
*a2 = result;
return result;
}

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

*a1 |= ((unsigned __int16)*a2 << 8) & 0x300;
result = zoom_colorbase + ((*a2 >> 2) & 0xFu);
*a2 = result;
return result;
}

original_code:
static void zoom_callback_0(int *code,int *color)
{
 *code |= ((*color & 0x03) << 8);
 *color = zoom_colorbase[0] + ((*color & 0x3c) >> 2);
}

canonical_original_code:
static void zoom_callback_0(int *code, int *color)
{
 *code |= ((*color & 0x03) << 8);
 *color = zoom_colorbase[0] + ((*color & 0x3c) >> 2);
}

variable_types:
code
int *

variable_types:
color
int *


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

tile_info[5] = (*a2 >> 6) & 3;
*a1 |= ((unsigned __int16)*a2 << 8) & 0xF00;
result = zoom_colorbase[1] + ((*a2 >> 4) & 3u);
*a2 = result;
return result;
}

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

tile_info[5] = (*a2 >> 6) & 3;
*a1 |= ((unsigned __int16)*a2 << 8) & 0xF00;
result = zoom_colorbase[1] + ((*a2 >> 4) & 3u);
*a2 = result;
return result;
}

original_code:
static void zoom_callback_1(int *code,int *color)
{
 tile_info.flags = ((*color & 0xc0) >> 6);
 *code |= ((*color & 0x0f) << 8);
 *color = zoom_colorbase[1] + ((*color & 0x30) >> 4);
}

canonical_original_code:
static void zoom_callback_1(int *code, int *color)
{
 tile_info.flags = ((*color & 0xc0) >> 6);
 *code |= ((*color & 0x0f) << 8);
 *color = zoom_colorbase[1] + ((*color & 0x30) >> 4);
}


decompiled_code:
__int64 __fastcall volume_callback1(int a1)
{
return K007232_set_volume(
1LL,
1LL,
(unsigned int)((17 * (a1 & 0xF)) >> 1),
(unsigned int)((int)((a1 & 0xFFFFFFF0) + (a1 >> 4)) / 2));
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
return K007232_set_volume(
1LL,
1LL,
(unsigned int)((17 * (a1 & 0xF)) >> 1),
(unsigned int)((int)((a1 & 0xFFFFFFF0) + (a1 >> 4)) / 2));
}

original_code:
static void volume_callback1(int v)
{
 K007232_set_volume(1,1,(v & 0x0f)*0x11/2,(v >> 4)*0x11/2);
}

variable_types: v int


decompiled_code:
__int64 __fastcall volume_callback0(int a1)
{
K007232_set_volume(0LL, 0LL, (a1 >> 4) + (a1 & 0xFFFFFFF0), 0LL);
return K007232_set_volume(0LL, 1LL, 0LL, 17 * (a1 & 0xFu));
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
K007232_set_volume(0LL, 0LL, (a1 >> 4) + (a1 & 0xFFFFFFF0), 0LL);
return K007232_set_volume(0LL, 1LL, 0LL, 17 * (a1 & 0xFu));
}


decompiled_code:
__int64 __fastcall kangaroo_vh_convert_color_prom(_BYTE *a1)
{
__int64 result; // rax

result = *(_QWORD *)(Machine + 824LL);
if ( *(_DWORD *)(result + 696) )
{
LODWORD(result) = 0;
do
{
*a1 = -(((int)result >> 2) & 1);
a1[1] = -(((int)result >> 1) & 1);
a1 += 3;
*(a1 - 1) = -(result & 1);
result = (unsigned int)(result + 1);
}
while ( *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 696LL) > (unsigned int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
__int64 result; // rax

result = *(_QWORD *)(Machine + 824LL);
if ( *(_DWORD *)(result + 696) )
{
LODWORD(result) = 0;
do
{
*a1 = -(((int)result >> 2) & 1);
a1[1] = -(((int)result >> 1) & 1);
a1 += 3;
*(a1 - 1) = -(result & 1);
result = (unsigned int)(result + 1);
}
while ( *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 696LL) > (unsigned int)result );
}
return result;
}

original_code:
void kangaroo_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++)
 {
  *(palette++) = ((i & 4) >> 2) * 0xff;
  *(palette++) = ((i & 2) >> 1) * 0xff;
  *(palette++) = ((i & 1) >> 0) * 0xff;
 }
}

canonical_original_code:
void kangaroo_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++)
 {
  *(palette++) = ((i & 4) >> 2) * 0xff;
  *(palette++) = ((i & 2) >> 1) * 0xff;
  *(palette++) = ((i & 1) >> 0) * 0xff;
 }
}

variable_types:
palette
unsigned char *

variable_types:
colortable
unsigned short *


decompiled_code:
__int64 __fastcall kangaroo_plot_pixel(_DWORD *a1, __int64 a2, __int64 a3, char a4, int a5, int a6)
{
if ( a6 )
{
a2 = (unsigned int)(*a1 + ~(_DWORD)a2);
a3 = (unsigned int)(a1[1] + ~(_DWORD)a3);
}
if ( (a4 & 8) != 0 )
a5 = 0;
return plot_pixel(a1, a2, a3, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 2LL * (a5 + (a4 & 7))));
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, __int64 a2, __int64 a3, char a4, int a5, int a6)
{
if ( a6 )
{
a2 = (unsigned int)(*a1 + ~(_DWORD)a2);
a3 = (unsigned int)(a1[1] + ~(_DWORD)a3);
}
if ( (a4 & 8) != 0 )
a5 = 0;
return plot_pixel(a1, a2, a3, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 2LL * (a5 + (a4 & 7))));
}

original_code:
static inline void kangaroo_plot_pixel(struct osd_bitmap *bitmap, int x, int y, int col, int color_base, int flip)
{
 if (flip)
 {
  x = bitmap->width - 1 - x;
  y = bitmap->height - 1 - y;
 }
 plot_pixel(bitmap, x, y, Machine->pens[((col & 0x08) ? 0 : color_base) + (col & 0x07)]);
}

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

variable_types: x int

variable_types: y int

variable_types: col int

variable_types: flip int


decompiled_code:
__int64 __fastcall kangaroo_redraw_4pixels(unsigned int a1, unsigned int a2)
{
int v2; // er15
int v3; // er14
__int64 v4; // rbx

v2 = *kangaroo_video_control & 0x20;
v3 = *kangaroo_video_control & 0x10;
v4 = (int)(a1 + (a2 << 8));
kangaroo_plot_pixel(tmpbitmap, a1, a2, *(_BYTE *)(videoram + v4) & 0xF, 8, v2);
kangaroo_plot_pixel(tmpbitmap, a1 + 1, a2, *(_BYTE *)(videoram + v4 + 1) & 0xF, 8, v2);
kangaroo_plot_pixel(tmpbitmap, a1 + 2, a2, *(_BYTE *)(videoram + v4 + 2) & 0xF, 8, v2);
kangaroo_plot_pixel(tmpbitmap, a1 + 3, a2, *(_BYTE *)(videoram + v4 + 3) & 0xF, 8, v2);
kangaroo_plot_pixel((_DWORD *)tmpbitmap2, a1, a2, *(_BYTE *)(videoram + v4) >> 4, 16, v3);
kangaroo_plot_pixel((_DWORD *)tmpbitmap2, a1 + 1, a2, *(_BYTE *)(videoram + v4 + 1) >> 4, 16, v3);
kangaroo_plot_pixel((_DWORD *)tmpbitmap2, a1 + 2, a2, *(_BYTE *)(videoram + v4 + 2) >> 4, 16, v3);
return kangaroo_plot_pixel((_DWORD *)tmpbitmap2, a1 + 3, a2, *(_BYTE *)(videoram + v4 + 3) >> 4, 16, v3);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned int a2)
{
int v2; // er15
int v3; // er14
__int64 v4; // rbx

v2 = *kangaroo_video_control & 0x20;
v3 = *kangaroo_video_control & 0x10;
v4 = (int)(a1 + (a2 << 8));
func2(tmpbitmap, a1, a2, *(_BYTE *)(videoram + v4) & 0xF, 8, v2);
func2(tmpbitmap, a1 + 1, a2, *(_BYTE *)(videoram + v4 + 1) & 0xF, 8, v2);
func2(tmpbitmap, a1 + 2, a2, *(_BYTE *)(videoram + v4 + 2) & 0xF, 8, v2);
func2(tmpbitmap, a1 + 3, a2, *(_BYTE *)(videoram + v4 + 3) & 0xF, 8, v2);
func2((_DWORD *)tmpbitmap2, a1, a2, *(_BYTE *)(videoram + v4) >> 4, 16, v3);
func2((_DWORD *)tmpbitmap2, a1 + 1, a2, *(_BYTE *)(videoram + v4 + 1) >> 4, 16, v3);
func2((_DWORD *)tmpbitmap2, a1 + 2, a2, *(_BYTE *)(videoram + v4 + 2) >> 4, 16, v3);
return func2((_DWORD *)tmpbitmap2, a1 + 3, a2, *(_BYTE *)(videoram + v4 + 3) >> 4, 16, v3);
}

original_code:
static inline void kangaroo_redraw_4pixels(int x, int y)
{
 int offs, flipA, flipB;
 offs = y * 256 + x;
 flipA = *kangaroo_video_control & 0x20;
 flipB = *kangaroo_video_control & 0x10;
 kangaroo_plot_pixel(tmpbitmap , x , y, videoram[offs ] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap , x+1, y, videoram[offs+1] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap , x+2, y, videoram[offs+2] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap , x+3, y, videoram[offs+3] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap2, x , y, videoram[offs ] >> 4, 16, flipB);
 kangaroo_plot_pixel(tmpbitmap2, x+1, y, videoram[offs+1] >> 4, 16, flipB);
 kangaroo_plot_pixel(tmpbitmap2, x+2, y, videoram[offs+2] >> 4, 16, flipB);
 kangaroo_plot_pixel(tmpbitmap2, x+3, y, videoram[offs+3] >> 4, 16, flipB);
}

canonical_original_code:
static inline void kangaroo_redraw_4pixels(int x, int y)
{
 int offs; int flipA; int flipB;
 offs = y * 256 + x;
 flipA = *kangaroo_video_control & 0x20;
 flipB = *kangaroo_video_control & 0x10;
 kangaroo_plot_pixel(tmpbitmap , x , y, videoram[offs ] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap , x+1, y, videoram[offs+1] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap , x+2, y, videoram[offs+2] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap , x+3, y, videoram[offs+3] & 0x0f, 8, flipA);
 kangaroo_plot_pixel(tmpbitmap2, x , y, videoram[offs ] >> 4, 16, flipB);
 kangaroo_plot_pixel(tmpbitmap2, x+1, y, videoram[offs+1] >> 4, 16, flipB);
 kangaroo_plot_pixel(tmpbitmap2, x+2, y, videoram[offs+2] >> 4, 16, flipB);
 kangaroo_plot_pixel(tmpbitmap2, x+3, y, videoram[offs+3] >> 4, 16, flipB);
}

variable_types: offs int

variable_types: flipA int

variable_types: flipB int


original_code:
void kangaroo_videoram_w(offs_t offset,data_t data)
{
 int a_Z_R,a_G_B,b_Z_R,b_G_B;
 int sx, sy, offs;
 a_Z_R = *kangaroo_bank_select & 0x01;
 a_G_B = *kangaroo_bank_select & 0x02;
 b_Z_R = *kangaroo_bank_select & 0x04;
 b_G_B = *kangaroo_bank_select & 0x08;
 sx = (offset / 256) * 4;
 sy = offset % 256;
 offs = sy * 256 + sx;
 if (a_G_B)
 {
  videoram[offs ] = (videoram[offs ] & 0xfc) | ((data & 0x10) >> 3) | ((data & 0x01) >> 0);
  videoram[offs+1] = (videoram[offs+1] & 0xfc) | ((data & 0x20) >> 4) | ((data & 0x02) >> 1);
  videoram[offs+2] = (videoram[offs+2] & 0xfc) | ((data & 0x40) >> 5) | ((data & 0x04) >> 2);
  videoram[offs+3] = (videoram[offs+3] & 0xfc) | ((data & 0x80) >> 6) | ((data & 0x08) >> 3);
 }
 if (a_Z_R)
 {
  videoram[offs ] = (videoram[offs ] & 0xf3) | ((data & 0x10) >> 1) | ((data & 0x01) << 2);
  videoram[offs+1] = (videoram[offs+1] & 0xf3) | ((data & 0x20) >> 2) | ((data & 0x02) << 1);
  videoram[offs+2] = (videoram[offs+2] & 0xf3) | ((data & 0x40) >> 3) | ((data & 0x04) >> 0);
  videoram[offs+3] = (videoram[offs+3] & 0xf3) | ((data & 0x80) >> 4) | ((data & 0x08) >> 1);
 }
 if (b_G_B)
 {
  videoram[offs ] = (videoram[offs ] & 0xcf) | ((data & 0x10) << 1) | ((data & 0x01) << 4);
  videoram[offs+1] = (videoram[offs+1] & 0xcf) | ((data & 0x20) >> 0) | ((data & 0x02) << 3);
  videoram[offs+2] = (videoram[offs+2] & 0xcf) | ((data & 0x40) >> 1) | ((data & 0x04) << 2);
  videoram[offs+3] = (videoram[offs+3] & 0xcf) | ((data & 0x80) >> 2) | ((data & 0x08) << 1);
 }
 if (b_Z_R)
 {
  videoram[offs ] = (videoram[offs ] & 0x3f) | ((data & 0x10) << 3) | ((data & 0x01) << 6);
  videoram[offs+1] = (videoram[offs+1] & 0x3f) | ((data & 0x20) << 2) | ((data & 0x02) << 5);
  videoram[offs+2] = (videoram[offs+2] & 0x3f) | ((data & 0x40) << 1) | ((data & 0x04) << 4);
  videoram[offs+3] = (videoram[offs+3] & 0x3f) | ((data & 0x80) << 0) | ((data & 0x08) << 3);
 }
 kangaroo_redraw_4pixels(sx, sy);
}

canonical_original_code:
void kangaroo_videoram_w(unsigned int offset, unsigned int data)
{
 int a_Z_R; int a_G_B; int b_Z_R; int b_G_B;
 int sx; int sy; int offs;
 a_Z_R = *kangaroo_bank_select & 0x01;
 a_G_B = *kangaroo_bank_select & 0x02;
 b_Z_R = *kangaroo_bank_select & 0x04;
 b_G_B = *kangaroo_bank_select & 0x08;
 sx = (offset / 256) * 4;
 sy = offset % 256;
 offs = sy * 256 + sx;
 if (a_G_B)
 {
  videoram[offs ] = (videoram[offs ] & 0xfc) | ((data & 0x10) >> 3) | ((data & 0x01) >> 0);
  videoram[offs+1] = (videoram[offs+1] & 0xfc) | ((data & 0x20) >> 4) | ((data & 0x02) >> 1);
  videoram[offs+2] = (videoram[offs+2] & 0xfc) | ((data & 0x40) >> 5) | ((data & 0x04) >> 2);
  videoram[offs+3] = (videoram[offs+3] & 0xfc) | ((data & 0x80) >> 6) | ((data & 0x08) >> 3);
 }
 if (a_Z_R)
 {
  videoram[offs ] = (videoram[offs ] & 0xf3) | ((data & 0x10) >> 1) | ((data & 0x01) << 2);
  videoram[offs+1] = (videoram[offs+1] & 0xf3) | ((data & 0x20) >> 2) | ((data & 0x02) << 1);
  videoram[offs+2] = (videoram[offs+2] & 0xf3) | ((data & 0x40) >> 3) | ((data & 0x04) >> 0);
  videoram[offs+3] = (videoram[offs+3] & 0xf3) | ((data & 0x80) >> 4) | ((data & 0x08) >> 1);
 }
 if (b_G_B)
 {
  videoram[offs ] = (videoram[offs ] & 0xcf) | ((data & 0x10) << 1) | ((data & 0x01) << 4);
  videoram[offs+1] = (videoram[offs+1] & 0xcf) | ((data & 0x20) >> 0) | ((data & 0x02) << 3);
  videoram[offs+2] = (videoram[offs+2] & 0xcf) | ((data & 0x40) >> 1) | ((data & 0x04) << 2);
  videoram[offs+3] = (videoram[offs+3] & 0xcf) | ((data & 0x80) >> 2) | ((data & 0x08) << 1);
 }
 if (b_Z_R)
 {
  videoram[offs ] = (videoram[offs ] & 0x3f) | ((data & 0x10) << 3) | ((data & 0x01) << 6);
  videoram[offs+1] = (videoram[offs+1] & 0x3f) | ((data & 0x20) << 2) | ((data & 0x02) << 5);
  videoram[offs+2] = (videoram[offs+2] & 0x3f) | ((data & 0x40) << 1) | ((data & 0x04) << 4);
  videoram[offs+3] = (videoram[offs+3] & 0x3f) | ((data & 0x80) << 0) | ((data & 0x08) << 3);
 }
 kangaroo_redraw_4pixels(sx, sy);
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int

variable_types: sx int

variable_types: sy int


decompiled_code:
__int64 kangaroo_sec_chip_r()
{
return ++kangaroo_clock & 0xF;
}

canonical_decompiled_code:
__int64 func5()
{
return ++kangaroo_clock & 0xF;
}

original_code:
static data_t kangaroo_sec_chip_r(offs_t offset)
{
  kangaroo_clock++;
  return (kangaroo_clock & 0x0f);
}

canonical_original_code:
static unsigned int kangaroo_sec_chip_r(unsigned int offset)
{
  kangaroo_clock++;
  return (kangaroo_clock & 0x0f);
}


decompiled_code:
void kangaroo_sec_chip_w()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
static void kangaroo_sec_chip_w(offs_t offset,data_t data)
{
}

canonical_original_code:
static void kangaroo_sec_chip_w(unsigned int offset, unsigned int data)
{
}


decompiled_code:
__int64 kangaroo_vh_start()
{
unsigned int v0; // edx

tmpbitmap = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
v0 = 1;
if ( tmpbitmap )
{
tmpbitmap2 = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
if ( tmpbitmap2 )
{
videoram = malloc(*(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 668LL) * *(_DWORD *)(*(_QWORD *)(Machine + 824LL)
+ 664LL));
if ( !videoram )
{
bitmap_free(tmpbitmap);
bitmap_free(tmpbitmap2);
}
v0 = 0;
}
else
{
bitmap_free(tmpbitmap);
v0 = 1;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // edx

tmpbitmap = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
v0 = 1;
if ( tmpbitmap )
{
tmpbitmap2 = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
if ( tmpbitmap2 )
{
videoram = malloc(*(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 668LL) * *(_DWORD *)(*(_QWORD *)(Machine + 824LL)
+ 664LL));
if ( !videoram )
{
bitmap_free(tmpbitmap);
bitmap_free(tmpbitmap2);
}
v0 = 0;
}
else
{
bitmap_free(tmpbitmap);
v0 = 1;
}
}
return v0;
}

original_code:
int kangaroo_vh_start(void)
{
 if ((tmpbitmap = bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  return 1;
 if ((tmpbitmap2 = bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
 {
  bitmap_free(tmpbitmap);
  return 1;
 }
 if ((videoram = (unsigned char*)malloc(Machine->drv->screen_width*Machine->drv->screen_height)) == 0)
 {
  bitmap_free(tmpbitmap);
  bitmap_free(tmpbitmap2);
 }
 return 0;
}

canonical_original_code:
int kangaroo_vh_start()
{
 if ((tmpbitmap = bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  return 1;
 if ((tmpbitmap2 = bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
 {
  bitmap_free(tmpbitmap);
  return 1;
 }
 if ((videoram = (unsigned char *)malloc(Machine->drv->screen_width*Machine->drv->screen_height)) == 0)
 {
  bitmap_free(tmpbitmap);
  bitmap_free(tmpbitmap2);
 }
 return 0;
}


decompiled_code:
void kangaroo_vh_stop()
{
bitmap_free(tmpbitmap2);
bitmap_free(tmpbitmap);
free(videoram);
}

canonical_decompiled_code:
void func8()
{
bitmap_free(tmpbitmap2);
bitmap_free(tmpbitmap);
free(videoram);
}

original_code:
void kangaroo_vh_stop(void)
{
 bitmap_free(tmpbitmap2);
 bitmap_free(tmpbitmap);
 free(videoram);
}


canonical_original_code:
void kangaroo_color_mask_w(unsigned int offset, unsigned int data)
{
 int i;
 for (i = 0;i < 8;i++)
 {
  int r; int g; int b;
  r = ((i & 4) >> 2) * ((data & 0x20) ? 0xff : 0x7f);
  g = ((i & 2) >> 1) * ((data & 0x10) ? 0xff : 0x7f);
  b = ((i & 1) >> 0) * ((data & 0x08) ? 0xff : 0x7f);
  palette_change_color(8+i,r,g,b);
 }
 for (i = 0;i < 8;i++)
 {
  int r; int g; int b;
  r = ((i & 4) >> 2) * ((data & 0x04) ? 0xff : 0x7f);
  g = ((i & 2) >> 1) * ((data & 0x02) ? 0xff : 0x7f);
  b = ((i & 1) >> 0) * ((data & 0x01) ? 0xff : 0x7f);
  palette_change_color(16+i,r,g,b);
 }
}

variable_types: i int

variable_types: r int

variable_types: g int

variable_types: b int


decompiled_code:
__int64 __fastcall kangaroo_vh_screenrefresh(__int64 a1)
{
unsigned int v1; // ebp
unsigned int i; // ebx
__int64 result; // rax
int v4; // [rsp+8h] [rbp-20h] BYREF
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = 0;
if ( !palette_recalc() )
{
if ( !screen_flipped )
goto LABEL_6;
v1 = 0;
}
do
{
for ( i = 0; i != 256; ++i )
kangaroo_redraw_4pixels(v1, i);
v1 += 4;
}
while ( v1 != 256 );
screen_flipped = 0;
LABEL_6:
v5[0] = (unsigned __int8)kangaroo_scroll[1];
v4 = (unsigned __int8)*kangaroo_scroll;
if ( (*kangaroo_bank_select & 1) != 0 )
{
copybitmap(a1, tmpbitmap2, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 0LL, 0LL);
result = copyscrollbitmap(a1, tmpbitmap, 1LL, v5, 1LL, &v4, Machine + 776LL, 6LL, 8LL);
}
else
{
copyscrollbitmap(a1, tmpbitmap, 1LL, v5, 1LL, &v4, Machine + 776LL, 0LL, 0LL);
result = copybitmap(a1, tmpbitmap2, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 6LL, 16LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
unsigned int v1; // ebp
unsigned int i; // ebx
__int64 result; // rax
int v4; // [rsp+8h] [rbp-20h] BYREF
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = 0;
if ( !palette_recalc() )
{
if ( !screen_flipped )
goto LABEL_6;
v1 = 0;
}
do
{
for ( i = 0; i != 256; ++i )
func3(v1, i);
v1 += 4;
}
while ( v1 != 256 );
screen_flipped = 0;
LABEL_6:
v5[0] = (unsigned __int8)kangaroo_scroll[1];
v4 = (unsigned __int8)*kangaroo_scroll;
if ( (*kangaroo_bank_select & 1) != 0 )
{
copybitmap(a1, tmpbitmap2, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 0LL, 0LL);
result = copyscrollbitmap(a1, tmpbitmap, 1LL, v5, 1LL, &v4, Machine + 776LL, 6LL, 8LL);
}
else
{
copyscrollbitmap(a1, tmpbitmap, 1LL, v5, 1LL, &v4, Machine + 776LL, 0LL, 0LL);
result = copybitmap(a1, tmpbitmap2, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 6LL, 16LL);
}
return result;
}

original_code:
void kangaroo_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int scrollx, scrolly;
 if (palette_recalc() || screen_flipped)
 {
  int x, y;
  for (x = 0; x < 256; x+=4)
  {
   for (y = 0; y < 256; y++)
   {
    kangaroo_redraw_4pixels(x, y);
   }
  }
  screen_flipped = 0;
 }
 scrollx = kangaroo_scroll[1];
 scrolly = kangaroo_scroll[0];
 if (*kangaroo_bank_select & 0x01)
 {
  copybitmap(bitmap,tmpbitmap2,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
  copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_COLOR,8);
 }
 else
 {
  copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_NONE,0);
  copybitmap(bitmap,tmpbitmap2,0,0,0,0,&Machine->visible_area,TRANSPARENCY_COLOR,16);
 }
}

canonical_original_code:
void kangaroo_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int scrollx; int scrolly;
 if (palette_recalc() || screen_flipped)
 {
  int x; int y;
  for (x = 0; x < 256; x+=4)
  {
   for (y = 0; y < 256; y++)
   {
    kangaroo_redraw_4pixels(x, y);
   }
  }
  screen_flipped = 0;
 }
 scrollx = kangaroo_scroll[1];
 scrolly = kangaroo_scroll[0];
 if (*kangaroo_bank_select & 0x01)
 {
  copybitmap(bitmap,tmpbitmap2,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
  copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_COLOR,8);
 }
 else
 {
  copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_NONE,0);
  copybitmap(bitmap,tmpbitmap2,0,0,0,0,&Machine->visible_area,TRANSPARENCY_COLOR,16);
 }
}

variable_types: scrollx int

variable_types: scrolly int


decompiled_code:
__int64 kangaroo_init_machine()
{
return cpu_cause_interrupt(0LL, 4294967294LL);
}

canonical_decompiled_code:
__int64 func12()
{
return cpu_cause_interrupt(0LL, 4294967294LL);
}

original_code:
static void kangaroo_init_machine(void)
{
 cpu_cause_interrupt(0,-2);
}

canonical_original_code:
static void kangaroo_init_machine()
{
 cpu_cause_interrupt(0,-2);
}


decompiled_code:
__int64 __fastcall kangaroo_coin_counter_w(__int64 a1, char a2)
{
coin_counter_w(0LL, a2 & 1);
return coin_counter_w(1LL, a2 & 2);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, char a2)
{
coin_counter_w(0LL, a2 & 1);
return coin_counter_w(1LL, a2 & 2);
}

original_code:
static void kangaroo_coin_counter_w(offs_t offset,data_t data)
{
 coin_counter_w(0, data & 1);
 coin_counter_w(1, data & 2);
}

canonical_original_code:
static void kangaroo_coin_counter_w(unsigned int offset, unsigned int data)
{
 coin_counter_w(0, data & 1);
 coin_counter_w(1, data & 2);
}


decompiled_code:
__int64 __fastcall safarir_ram_r(unsigned int a1)
{
return *(unsigned __int8 *)(safarir_ram + a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return *(unsigned __int8 *)(safarir_ram + a1);
}

original_code:
data_t safarir_ram_r(offs_t offset)
{
 return safarir_ram[offset];
}

canonical_original_code:
unsigned int safarir_ram_r(unsigned int offset)
{
 return safarir_ram[offset];
}


decompiled_code:
void __fastcall safarir_scroll_w(__int64 a1, int a2)
{
safarir_scroll = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, int a2)
{
safarir_scroll = a2;
}

original_code:
void safarir_scroll_w(offs_t offset,data_t data)
{
 safarir_scroll = data;
}

canonical_original_code:
void safarir_scroll_w(unsigned int offset, unsigned int data)
{
 safarir_scroll = data;
}


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

if ( a2 )
result = safarir_ram1;
else
result = safarir_ram2;
safarir_ram = result;
return result;
}

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

if ( a2 )
result = safarir_ram1;
else
result = safarir_ram2;
safarir_ram = result;
return result;
}

original_code:
void safarir_ram_bank_w(offs_t offset,data_t data)
{
 safarir_ram = data ? safarir_ram1 : safarir_ram2;
}

canonical_original_code:
void safarir_ram_bank_w(unsigned int offset, unsigned int data)
{
 safarir_ram = data ? safarir_ram1 : safarir_ram2;
}


updatepixels

decompiled_code:
__int64 __fastcall updatepixels(signed int a1, unsigned int a2)
{
__int64 result; // rax
int v4; // esi
unsigned __int16 v5; // r12
__int16 v6; // r13
int v7; // edx
unsigned __int16 v8; // r13

result = Machine;
if ( *(_DWORD *)(Machine + 776LL) <= a1
&& *(_DWORD *)(Machine + 780LL) >= a1
&& *(_DWORD *)(Machine + 784LL) <= (signed int)a2
&& *(_DWORD *)(Machine + 788LL) >= (signed int)a2 )
{
v4 = a2 << 9;
v5 = *(_WORD *)(blockout_videoram + v4 + a1);
v6 = *(_WORD *)(blockout_videoram + v4 + a1 + 0x20000);
v7 = HIBYTE(v5);
if ( (v5 & 0xFF00) == 0 )
v7 = HIBYTE(v6) + 256;
plot_pixel(tmpbitmap, (unsigned int)a1, a2, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 2LL * v7));
v8 = (unsigned __int8)v6 + 256;
v5 = (unsigned __int8)v5;
if ( !(_BYTE)v5 )
v5 = v8;
result = plot_pixel(
tmpbitmap,
(unsigned int)(a1 + 1),
a2,
*(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 2LL * v5));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(signed int a1, unsigned int a2)
{
__int64 result; // rax
int v4; // esi
unsigned __int16 v5; // r12
__int16 v6; // r13
int v7; // edx
unsigned __int16 v8; // r13

result = Machine;
if ( *(_DWORD *)(Machine + 776LL) <= a1
&& *(_DWORD *)(Machine + 780LL) >= a1
&& *(_DWORD *)(Machine + 784LL) <= (signed int)a2
&& *(_DWORD *)(Machine + 788LL) >= (signed int)a2 )
{
v4 = a2 << 9;
v5 = *(_WORD *)(blockout_videoram + v4 + a1);
v6 = *(_WORD *)(blockout_videoram + v4 + a1 + 0x20000);
v7 = HIBYTE(v5);
if ( (v5 & 0xFF00) == 0 )
v7 = HIBYTE(v6) + 256;
plot_pixel(tmpbitmap, (unsigned int)a1, a2, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 2LL * v7));
v8 = (unsigned __int8)v6 + 256;
v5 = (unsigned __int8)v5;
if ( !(_BYTE)v5 )
v5 = v8;
result = plot_pixel(
tmpbitmap,
(unsigned int)(a1 + 1),
a2,
*(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 2LL * v5));
}
return result;
}

original_code:
static void updatepixels(int x,int y)
{
 int front,back;
 int color;
 if (x < Machine->visible_area.min_x ||
   x > Machine->visible_area.max_x ||
   y < Machine->visible_area.min_y ||
   y > Machine->visible_area.max_y)
  return;
 front = (*(UINT16 *)(&blockout_videoram[(y<<9)+x]));
 back = (*(UINT16 *)(&blockout_videoram[0x20000 + (y<<9)+x]));
 if (front>>8) color = front>>8;
 else color = (back>>8) + 256;
 plot_pixel(tmpbitmap, x, y, Machine->pens[color]);
 if (front&0xff) color = front&0xff;
 else color = (back&0xff) + 256;
 plot_pixel(tmpbitmap, x+1, y, Machine->pens[color]);
}

canonical_original_code:
static void updatepixels(int x, int y)
{
 int front; int back;
 int color;
 if (x < Machine->visible_area.min_x ||
   x > Machine->visible_area.max_x ||
   y < Machine->visible_area.min_y ||
   y > Machine->visible_area.max_y)
  return;
 front = (*(unsigned short *)(&blockout_videoram[(y<<9)+x]));
 back = (*(unsigned short *)(&blockout_videoram[0x20000 + (y<<9)+x]));
 if (front>>8) color = front>>8;
 else color = (back>>8) + 256;
 plot_pixel(tmpbitmap, x, y, Machine->pens[color]);
 if (front&0xff) color = front&0xff;
 else color = (back&0xff) + 256;
 plot_pixel(tmpbitmap, x+1, y, Machine->pens[color]);
}

variable_types: x int

variable_types: front int

variable_types: back int

variable_types: color int


decompiled_code:
__int64 __fastcall blockout_videoram_w(unsigned int a1, unsigned int a2)
{
_WORD *v2; // rdx
int v3; // ecx
__int64 result; // rax
int v5; // esi

v2 = (_WORD *)(blockout_videoram + a1);
v3 = (unsigned __int16)*v2;
result = v3 & HIWORD(a2);
v5 = result | (unsigned __int16)a2;
if ( v3 != v5 )
{
*v2 = v5;
result = updatepixels(a1 & 0x1FF, (unsigned __int8)(a1 >> 9));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
_WORD *v2; // rdx
int v3; // ecx
__int64 result; // rax
int v5; // esi

v2 = (_WORD *)(blockout_videoram + a1);
v3 = (unsigned __int16)*v2;
result = v3 & HIWORD(a2);
v5 = result | (unsigned __int16)a2;
if ( v3 != v5 )
{
*v2 = v5;
result = func0(a1 & 0x1FF, (unsigned __int8)(a1 >> 9));
}
return result;
}

original_code:
void blockout_videoram_w(offs_t offset,data_t data)
{
 int oldword = (*(UINT16 *)(&blockout_videoram[offset]));
 int newword = (((oldword) & ((data) >> 16)) | ((data) & 0xffff));
 if (oldword != newword)
 {
  (*(UINT16 *)(&blockout_videoram[offset]) = (newword));
  updatepixels(offset % 512,(offset>>9) % 256);
 }
}

canonical_original_code:
void blockout_videoram_w(unsigned int offset, unsigned int data)
{
 int oldword = (*(UINT16 *)(&blockout_videoram[offset]));
 int newword = (((oldword) & ((data) >> 16)) | ((data) & 0xffff));
 if (oldword != newword)
 {
  (*(unsigned short *)(&blockout_videoram[offset]) = (newword));
  updatepixels(offset % 512,(offset>>9) % 256);
 }
}

variable_types: oldword int

variable_types: newword int


decompiled_code:
__int64 __fastcall blockout_videoram_r(unsigned int a1)
{
return *(unsigned __int16 *)(blockout_videoram + a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
return *(unsigned __int16 *)(blockout_videoram + a1);
}

original_code:
data_t blockout_videoram_r(offs_t offset)
{
   return (*(UINT16 *)(&blockout_videoram[offset]));
}

canonical_original_code:
unsigned int blockout_videoram_r(unsigned int offset)
{
   return (*(unsigned short *)(&blockout_videoram[offset]));
}


decompiled_code:
__int64 __fastcall blockout_frontvideoram_r(unsigned int a1)
{
return *(unsigned __int16 *)(blockout_frontvideoram + a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
return *(unsigned __int16 *)(blockout_frontvideoram + a1);
}

original_code:
data_t blockout_frontvideoram_r(offs_t offset)
{
   return (*(UINT16 *)(&blockout_frontvideoram[offset]));
}

canonical_original_code:
unsigned int blockout_frontvideoram_r(unsigned int offset)
{
   return (*(unsigned short *)(&blockout_frontvideoram[offset]));
}


decompiled_code:
__int64 __fastcall blockout_paletteram_w(unsigned int a1, unsigned int a2)
{
_WORD *v2; // rdx
int v3; // esi

v2 = (_WORD *)(paletteram + a1);
v3 = (unsigned __int16)a2 | (unsigned __int16)*v2 & HIWORD(a2);
*v2 = v3;
return setcolor(a1 >> 1, v3);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned int a2)
{
_WORD *v2; // rdx
int v3; // esi

v2 = (_WORD *)(paletteram + a1);
v3 = (unsigned __int16)a2 | (unsigned __int16)*v2 & HIWORD(a2);
*v2 = v3;
return func5(a1 >> 1, v3);
}

original_code:
void blockout_paletteram_w(offs_t offset,data_t data)
{
 int oldword = (*(UINT16 *)(&paletteram[offset]));
 int newword = (((oldword) & ((data) >> 16)) | ((data) & 0xffff));
 (*(UINT16 *)(&paletteram[offset]) = (newword));
 setcolor(offset>>1, newword);
}

canonical_original_code:
void blockout_paletteram_w(unsigned int offset, unsigned int data)
{
 int oldword = (*(UINT16 *)(&paletteram[offset]));
 int newword = (((oldword) & ((data) >> 16)) | ((data) & 0xffff));
 (*(unsigned short *)(&paletteram[offset]) = (newword));
 setcolor(offset>>1, newword);
}


decompiled_code:
__int64 __fastcall blockout_frontcolor_w(__int64 a1, int a2)
{
return setcolor(512LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
return func5(512LL, a2);
}

original_code:
void blockout_frontcolor_w(offs_t offset,data_t data)
{
 setcolor(512,data);
}

canonical_original_code:
void blockout_frontcolor_w(unsigned int offset, unsigned int data)
{
 setcolor(512,data);
}


decompiled_code:
_BOOL8 blockout_vh_start()
{
tmpbitmap = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
return tmpbitmap == 0LL;
}

canonical_decompiled_code:
_BOOL8 func8()
{
tmpbitmap = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
return tmpbitmap == 0LL;
}

original_code:
int blockout_vh_start (void)
{
 if ((tmpbitmap = bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  return 1;
 return 0;
}

canonical_original_code:
int blockout_vh_start()
{
 if ((tmpbitmap = bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  return 1;
 return 0;
}


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

result = bitmap_free(tmpbitmap);
tmpbitmap = 0LL;
return result;
}

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

result = bitmap_free(tmpbitmap);
tmpbitmap = 0LL;
return result;
}

original_code:
void blockout_vh_stop (void)
{
 bitmap_free(tmpbitmap);
 tmpbitmap = 0;
}

canonical_original_code:
void blockout_vh_stop()
{
 bitmap_free(tmpbitmap);
 tmpbitmap = 0;
}