decompiled_code:
__int64 tape_read()
{
FILE *v0; // rdi
unsigned int v1; // ecx
double v2; // xmm0_8
unsigned __int8 v3; // dl
int v4; // ebx
int v5; // eax
char *v6; // rax
__int64 result; // rax

if ( fake_tape )
{
tape_read_val = (int)(ticks / 1001.0) & 1;
result = (int)(ticks / 1001.0) & 1;
}
else if ( tape_status || (v0 = tape_read_file) == 0LL )
{
tape_read_val = tape_read_val == 0;
result = (unsigned __int8)tape_read_val;
}
else if ( ticks <= *(double *)&tape_read_ticks )
{
result = (unsigned __int8)tape_read_val;
}
else
{
do
{
v4 = fgetc(v0);
v5 = fgetc(tape_read_file);
if ( v5 == -1 )
{
v6 = dcgettext(0LL, "End of tape\n", 5);
fprintf(stderr, v6);
pclose(tape_read_file);
v3 = -1;
tape_read_file = 0LL;
tape_read_val = -1;
*(double *)&tape_read_ticks = *(double *)&tape_read_ticks + 32767.0;
return v3;
}
v0 = tape_read_file;
v1 = (v4 << 8) | v5;
v2 = (double)(v1 & 0x7FFF) + *(double *)&tape_read_ticks;
tape_read_val = v1 >> 15;
v3 = v1 >> 15;
*(double *)&tape_read_ticks = v2;
if ( !tape_read_file )
return v3;
}
while ( ticks > v2 );
result = (unsigned __int8)tape_read_val;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
FILE *v0; // rdi
unsigned int v1; // ecx
double v2; // xmm0_8
unsigned __int8 v3; // dl
int v4; // ebx
int v5; // eax
char *v6; // rax
__int64 result; // rax

if ( fake_tape )
{
tape_read_val = (int)(ticks / 1001.0) & 1;
result = (int)(ticks / 1001.0) & 1;
}
else if ( tape_status || (v0 = tape_read_file) == 0LL )
{
tape_read_val = tape_read_val == 0;
result = (unsigned __int8)tape_read_val;
}
else if ( ticks <= *(double *)&tape_read_ticks )
{
result = (unsigned __int8)tape_read_val;
}
else
{
do
{
v4 = fgetc(v0);
v5 = fgetc(tape_read_file);
if ( v5 == -1 )
{
v6 = dcgettext(0LL, "End of tape\n", 5);
fprintf(stderr, v6);
pclose(tape_read_file);
v3 = -1;
tape_read_file = 0LL;
tape_read_val = -1;
*(double *)&tape_read_ticks = *(double *)&tape_read_ticks + 32767.0;
return v3;
}
v0 = tape_read_file;
v1 = (v4 << 8) | v5;
v2 = (double)(v1 & 0x7FFF) + *(double *)&tape_read_ticks;
tape_read_val = v1 >> 15;
v3 = v1 >> 15;
*(double *)&tape_read_ticks = v2;
if ( !tape_read_file )
return v3;
}
while ( ticks > v2 );
result = (unsigned __int8)tape_read_val;
}
return result;
}

original_code:
int
tape_read() {
    unsigned delta;
    if (fake_tape) {
 tape_read_val = ((unsigned) (ticks / 1001.0)) & 1;
 return tape_read_val;
    }
    if (tape_status || tape_read_file == ((void *)0)) {
 return tape_read_val = !tape_read_val;
    }
    while (tape_read_file && ticks > tape_read_ticks) {
 int c1 = fgetc(tape_read_file);
 int c2 = fgetc(tape_read_file);
 if (c2 == (-1)) {
  fprintf(stderr, gettext ("End of tape\n"));
  pclose(tape_read_file);
  tape_read_file = 0;
 }
 delta = c1 << 8;
 delta |= c2;
 tape_read_val = delta >> 15;
 delta &= 0x7FFF;
 tape_read_ticks += delta;
    }
    return tape_read_val;
}

canonical_original_code:
int tape_read() {
    unsigned int delta;
    if (fake_tape) {
 tape_read_val = ((unsigned int) (ticks / 1001.0)) & 1;
 return tape_read_val;
    }
    if (tape_status || tape_read_file == ((void *)0)) {
 return tape_read_val = !tape_read_val;
    }
    while (tape_read_file && ticks > tape_read_ticks) {
 int c1 = fgetc(tape_read_file);
 int c2 = fgetc(tape_read_file);
 if (c2 == (-1)) {
  fprintf(stderr, gettext ("End of tape\n"));
  pclose(tape_read_file);
  tape_read_file = 0;
 }
 delta = c1 << 8;
 delta |= c2;
 tape_read_val = delta >> 15;
 delta &= 0x7FFF;
 tape_read_ticks += delta;
    }
    return tape_read_val;
}

variable_types:
delta
unsigned int


repo: emestee/bk-emulator

decompiled_code:
__int64 get_emt36_filename()
{
__int64 v0; // rbx
__int64 v1; // rdi
char v2; // ah
__int64 result; // rax
__int64 v5; // rdx
char *v6; // rcx
char *v7; // rsi
__int64 v8; // rdx
unsigned __int64 v9; // rdi
char *v10; // rcx
char *v11; // rsi
unsigned int v12; // edx
__int64 v13; // rcx
__int64 v14; // rdx
unsigned __int16 v15; // [rsp+Ch] [rbp-Ch] BYREF
__int16 v16; // [rsp+Eh] [rbp-Ah] BYREF

v0 = 0LL;
lc_word(198LL, &v15);
do
{
v1 = v15 + (unsigned int)v0 + 6;
v0 += 2LL;
lc_word(v1, &v16);
v2 = HIBYTE(v16);
byte_D8E[v0] = v16;
byte_D8F[v0] = v2;
}
while ( v0 != 16 );
byte_DA0 = 0;
result = 15LL;
while ( *((_BYTE *)&bk_filename + result) == 32 )
{
unix_filename[result] = 0;
if ( result-- == 0 )
return result;
}
v5 = (int)result - result;
++result;
v6 = &unix_filename[v5];
v7 = (char *)&bk_filename + v5;
if ( (unsigned int)result >= 8 )
{
*(_QWORD *)&unix_filename[v5] = *(__int64 *)((char *)&bk_filename + v5);
*(_QWORD *)&v6[(unsigned int)result - 8] = *(_QWORD *)&v7[(unsigned int)result - 8];
v9 = (unsigned __int64)(v6 + 8) & 0xFFFFFFFFFFFFFFF8LL;
v10 = &v6[-v9];
v11 = (char *)(v7 - v10);
result = ((_DWORD)v10 + (_DWORD)result) & 0xFFFFFFF8;
if ( (unsigned int)result >= 8 )
{
result = (unsigned int)result & 0xFFFFFFF8;
v12 = 0;
do
{
v13 = v12;
v12 += 8;
*(_QWORD *)(v9 + v13) = *(_QWORD *)&v11[v13];
}
while ( v12 < (unsigned int)result );
}
}
else if ( (result & 4) != 0 )
{
*(_DWORD *)&unix_filename[v5] = *(_DWORD *)((char *)&bk_filename + v5);
v14 = (unsigned int)result;
result = *(unsigned int *)&v7[(unsigned int)result - 4];
*(_DWORD *)&v6[v14 - 4] = result;
}
else if ( (_DWORD)result )
{
unix_filename[v5] = *((_BYTE *)&bk_filename + v5);
if ( (result & 2) != 0 )
{
v8 = (unsigned int)result;
result = *(unsigned __int16 *)&v7[(unsigned int)result - 2];
*(_WORD *)&v6[v8 - 2] = result;
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rbx
__int64 v1; // rdi
char v2; // ah
__int64 result; // rax
__int64 v5; // rdx
char *v6; // rcx
char *v7; // rsi
__int64 v8; // rdx
unsigned __int64 v9; // rdi
char *v10; // rcx
char *v11; // rsi
unsigned int v12; // edx
__int64 v13; // rcx
__int64 v14; // rdx
unsigned __int16 v15; // [rsp+Ch] [rbp-Ch] BYREF
__int16 v16; // [rsp+Eh] [rbp-Ah] BYREF

v0 = 0LL;
lc_word(198LL, &v15);
do
{
v1 = v15 + (unsigned int)v0 + 6;
v0 += 2LL;
lc_word(v1, &v16);
v2 = HIBYTE(v16);
byte_D8E[v0] = v16;
byte_D8F[v0] = v2;
}
while ( v0 != 16 );
byte_DA0 = 0;
result = 15LL;
while ( *((_BYTE *)&bk_filename + result) == 32 )
{
unix_filename[result] = 0;
if ( result-- == 0 )
return result;
}
v5 = (int)result - result;
++result;
v6 = &unix_filename[v5];
v7 = (char *)&bk_filename + v5;
if ( (unsigned int)result >= 8 )
{
*(_QWORD *)&unix_filename[v5] = *(__int64 *)((char *)&bk_filename + v5);
*(_QWORD *)&v6[(unsigned int)result - 8] = *(_QWORD *)&v7[(unsigned int)result - 8];
v9 = (unsigned __int64)(v6 + 8) & 0xFFFFFFFFFFFFFFF8LL;
v10 = &v6[-v9];
v11 = (char *)(v7 - v10);
result = ((_DWORD)v10 + (_DWORD)result) & 0xFFFFFFF8;
if ( (unsigned int)result >= 8 )
{
result = (unsigned int)result & 0xFFFFFFF8;
v12 = 0;
do
{
v13 = v12;
v12 += 8;
*(_QWORD *)(v9 + v13) = *(_QWORD *)&v11[v13];
}
while ( v12 < (unsigned int)result );
}
}
else if ( (result & 4) != 0 )
{
*(_DWORD *)&unix_filename[v5] = *(_DWORD *)((char *)&bk_filename + v5);
v14 = (unsigned int)result;
result = *(unsigned int *)&v7[(unsigned int)result - 4];
*(_DWORD *)&v6[v14 - 4] = result;
}
else if ( (_DWORD)result )
{
unix_filename[v5] = *((_BYTE *)&bk_filename + v5);
if ( (result & 2) != 0 )
{
v8 = (unsigned int)result;
result = *(unsigned __int16 *)&v7[(unsigned int)result - 2];
*(_WORD *)&v6[v8 - 2] = result;
}
}
return result;
}

original_code:
void
get_emt36_filename() {
 int i;
 d_word base;
 lc_word(0306, &base);
 for (i = 0; i < 8; i++) {
  d_word d;
  lc_word(base + 6 + 2*i, &d);
  bk_filename[2*i] = d & 0xff;
  bk_filename[2*i+1] = d >> 8;
 }
 bk_filename[16] = '\0';
 for (i = 15; i >= 0 && bk_filename[i] == ' '; unix_filename[i--]='\0');
 for (; i >= 0; i--) unix_filename[i] = bk_filename[i];
}

canonical_original_code:
void get_emt36_filename() {
 int i;
 unsigned short base;
 lc_word(0306, &base);
 for (i = 0; i < 8; i++) {
  unsigned short d;
  lc_word(base + 6 + 2*i, &d);
  bk_filename[2*i] = d & 0xff;
  bk_filename[2*i+1] = d >> 8;
 }
 bk_filename[16] = '\0';
 for (i = 15; i >= 0 && bk_filename[i] == ' '; unix_filename[i--]='\0');
 for (; i >= 0; i--) unix_filename[i] = bk_filename[i];
}

variable_types: i int

variable_types:
base
unsigned short

variable_types:
d
unsigned short

return_type: void


decompiled_code:
void tape_read_start()
{
if ( !tapeflag )
{
get_emt36_filename();
sprintf(buf_5, "maketape '%s' '%s'", (const char *)&bk_filename, unix_filename);
tape_read_file = popen(buf_5, "r");
if ( tape_read_file )
tape_read_ticks = ticks;
else
perror(unix_filename);
}
}

canonical_decompiled_code:
void func3()
{
if ( !tapeflag )
{
func2();
sprintf(buf_5, "maketape '%s' '%s'", (const char *)&bk_filename, unix_filename);
tape_read_file = popen(buf_5, "r");
if ( tape_read_file )
tape_read_ticks = ticks;
else
perror(unix_filename);
}
}

original_code:
void
tape_read_start() {
 static char buf[80];
 if (tapeflag) {
  return;
 }
 get_emt36_filename();
 sprintf(buf, "maketape '%s' '%s'", bk_filename, unix_filename);
 tape_read_file = popen(buf, "r");
 if (tape_read_file) {
  tape_read_ticks = ticks;
 } else perror(unix_filename);
}

variable_types:
buf
char [80]


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

if ( tape_read_file )
{
result = pclose(tape_read_file);
tape_read_file = 0LL;
}
return result;
}

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

if ( tape_read_file )
{
result = pclose(tape_read_file);
tape_read_file = 0LL;
}
return result;
}

original_code:
void
tape_read_finish() {
 if (!tape_read_file) return;
 pclose(tape_read_file);
 tape_read_file = 0;
}


decompiled_code:
void __fastcall tape_write(int a1, int a2)
{
char v2; // al
char *v3; // r13
char *v4; // rax
int v5; // edi
__int64 v6; // xmm0_8
unsigned int v7; // ebx
unsigned int v8; // ebp

if ( fake_tape )
{
if ( a1 != (unsigned __int8)tape_status )
{
tape_status = a1;
ticks = ticks + 10000.0;
}
if ( tape_read_file && tape_status )
{
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
}
else
{
v2 = tape_status;
if ( a1 == (unsigned __int8)tape_status )
goto LABEL_7;
tape_status = a1;
ticks = ticks + 10000.0;
if ( (_BYTE)a1 )
v3 = dcgettext(0LL, "OFF", 5);
else
v3 = dcgettext(0LL, "ON", 5);
v4 = dcgettext(0LL, "Tape %s\n", 5);
fprintf(stderr, v4, v3);
if ( !tape_status )
{
tape_write_ticks = ticks;
tape_read_ticks = ticks;
tape_read_start();
v2 = tape_status;
LABEL_7:
if ( v2 )
return;
goto LABEL_8;
}
fflush(tape_write_file);
if ( tape_read_file )
tape_read_finish();
if ( !tape_status )
{
LABEL_8:
v5 = (unsigned __int8)tape_write_val;
if ( (unsigned __int8)tape_write_val != a2 )
{
v6 = ticks;
v7 = (int)(ticks - *(double *)&tape_write_ticks);
if ( v7 )
{
while ( 1 )
{
v8 = 0x7FFF;
if ( v7 <= 0x7FFF )
v8 = v7;
fputc((v8 >> 8) | (v5 << 7), tape_write_file);
fputc((unsigned __int8)v8, tape_write_file);
v7 -= v8;
if ( !v7 )
break;
v5 = (unsigned __int8)tape_write_val;
}
v6 = ticks;
}
tape_write_val = a2;
tape_write_ticks = v6;
}
return;
}
}
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2)
{
char v2; // al
char *v3; // r13
char *v4; // rax
int v5; // edi
__int64 v6; // xmm0_8
unsigned int v7; // ebx
unsigned int v8; // ebp

if ( fake_tape )
{
if ( a1 != (unsigned __int8)tape_status )
{
tape_status = a1;
ticks = ticks + 10000.0;
}
if ( tape_read_file && tape_status )
{
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
}
else
{
v2 = tape_status;
if ( a1 == (unsigned __int8)tape_status )
goto LABEL_7;
tape_status = a1;
ticks = ticks + 10000.0;
if ( (_BYTE)a1 )
v3 = dcgettext(0LL, "OFF", 5);
else
v3 = dcgettext(0LL, "ON", 5);
v4 = dcgettext(0LL, "Tape %s\n", 5);
fprintf(stderr, v4, v3);
if ( !tape_status )
{
tape_write_ticks = ticks;
tape_read_ticks = ticks;
func3();
v2 = tape_status;
LABEL_7:
if ( v2 )
return;
goto LABEL_8;
}
fflush(tape_write_file);
if ( tape_read_file )
func4();
if ( !tape_status )
{
LABEL_8:
v5 = (unsigned __int8)tape_write_val;
if ( (unsigned __int8)tape_write_val != a2 )
{
v6 = ticks;
v7 = (int)(ticks - *(double *)&tape_write_ticks);
if ( v7 )
{
while ( 1 )
{
v8 = 0x7FFF;
if ( v7 <= 0x7FFF )
v8 = v7;
fputc((v8 >> 8) | (v5 << 7), tape_write_file);
fputc((unsigned __int8)v8, tape_write_file);
v7 -= v8;
if ( !v7 )
break;
v5 = (unsigned __int8)tape_write_val;
}
v6 = ticks;
}
tape_write_val = a2;
tape_write_ticks = v6;
}
return;
}
}
}

original_code:
void tape_write(unsigned status, unsigned val) {
    if (fake_tape) {
 if (status != tape_status) {
  ticks += 10000;
  tape_status = status;
 }
 if (tape_read_file && tape_status) {
  fclose(tape_read_file);
  tape_read_file = 0;
  fake_state = Idle;
 }
 return;
    }
    if (status != tape_status) {
 ticks += 10000;
 tape_status = status;
 fprintf(stderr, gettext ("Tape %s\n"), tape_status ? gettext ("OFF") : gettext ("ON"));
 if (!tape_status) {
     tape_read_ticks = tape_write_ticks = ticks;
     tape_read_start();
 } else {
     fflush(tape_write_file);
     if(tape_read_file) tape_read_finish();
 }
    }
    if (!tape_status && val != tape_write_val) {
     unsigned delta = ticks - tape_write_ticks;
     while (delta) {
  unsigned delta2 = (delta <= 32767) ? delta : 32767;
  fputc((delta2 >> 8) | (tape_write_val << 7), tape_write_file);
  fputc(delta2 & 0xFF, tape_write_file);
  delta -= delta2;
     }
     tape_write_ticks = ticks;
 tape_write_val = val;
    }
}

canonical_original_code:
void tape_write(unsigned int status, unsigned int val) {
    if (fake_tape) {
 if (status != tape_status) {
  ticks += 10000;
  tape_status = status;
 }
 if (tape_read_file && tape_status) {
  fclose(tape_read_file);
  tape_read_file = 0;
  fake_state = Idle;
 }
 return;
    }
    if (status != tape_status) {
 ticks += 10000;
 tape_status = status;
 fprintf(stderr, gettext ("Tape %s\n"), tape_status ? gettext ("OFF") : gettext ("ON"));
 if (!tape_status) {
     tape_read_ticks = tape_write_ticks = ticks;
     tape_read_start();
 } else {
     fflush(tape_write_file);
     if(tape_read_file) tape_read_finish();
 }
    }
    if (!tape_status && val != tape_write_val) {
     unsigned int delta = ticks - tape_write_ticks;
     while (delta) {
  unsigned int delta2 = (delta <= 32767) ? delta : 32767;
  fputc((delta2 >> 8) | (tape_write_val << 7), tape_write_file);
  fputc(delta2 & 0xFF, tape_write_file);
  delta -= delta2;
     }
     tape_write_ticks = ticks;
 tape_write_val = val;
    }
}

variable_types:
status
unsigned int

variable_types:
val
unsigned int


decompiled_code:
__int64 fake_tuneup_sequence()
{
sc_word(204LL, 10LL);
return pop(&pdp, (unsigned int)((_DWORD)&pdp + 14));
}

canonical_decompiled_code:
__int64 func6()
{
sc_word(204LL, 10LL);
return pop(&pdp, (unsigned int)((_DWORD)&pdp + 14));
}

original_code:
void fake_tuneup_sequence() {
 sc_word(0314, 10);
        pop( &pdp, &pdp.regs[7] );
}


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

result = 4294942430LL;
pdp[7] = -24866;
return result;
}

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

result = 4294942430LL;
pdp[7] = -24866;
return result;
}

original_code:
void fake_array_with_tuneup() {
 pdp.regs[7] = 0117336;
}


decompiled_code:
__int64 __fastcall fake_read_strobe()
{
int v0; // ebx
char *v1; // rax
__int16 v2; // ax
int v3; // ecx
char v4; // al
int v5; // ebx
unsigned int v7; // ebx
int v8; // eax
char v9; // cl
int v10; // eax
char v11; // cl
int v12; // ebx
unsigned int v13; // er12
char *v14; // rax
int v15; // eax
unsigned int v16; // er12
char *v17; // rax
char *v18; // rax
unsigned __int8 v19; // al
int v20; // ebx
char v21; // si
int v22; // edx
char v23; // al
int v24; // edx
unsigned int v25; // er12
char *v26; // rax
__int16 v27[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( fake_state )
{
switch ( fake_state )
{
case 1:
v15 = bitnum_4;
if ( (bitnum_4 & 7) == 0 )
goto LABEL_28;
v0 = ((int)(unsigned __int8)curbyte_0 >> (bitnum_4 & 7)) & 1;
goto LABEL_25;
case 2:
v3 = bitnum_4;
v4 = bitnum_4 & 7;
if ( (bitnum_4 & 7) != 0 )
{
v5 = (unsigned __int8)curbyte_0;
}
else
{
if ( tape_read_file )
{
v23 = fgetc(tape_read_file);
v3 = bitnum_4;
LOBYTE(v5) = v23;
v4 = bitnum_4 & 7;
}
else
{
LOBYTE(v5) = bitnum_4 == 0;
}
curbyte_0 = v5;
v5 = (unsigned __int8)v5;
curlen_1 |= (unsigned __int8)v5 << v3;
}
bitnum_4 = v3 + 1;
v0 = (v5 >> v4) & 1;
if ( v3 == 15 )
{
v25 = curlen_1;
v26 = dcgettext(0LL, "File length will be %o\n", 5);
fprintf(stderr, v26, v25);
fake_state = 3;
bitnum_4 = 0;
}
break;
case 3:
v8 = bitnum_4 >> 3;
v9 = bitnum_4++ & 7;
v0 = (*((char *)&bk_filename + v8) >> v9) & 1;
if ( bitnum_4 == 128 )
{
fake_state = 4;
bitnum_4 = 0;
}
break;
case 4:
v10 = bitnum_4;
v11 = bitnum_4 & 7;
if ( (bitnum_4 & 7) != 0 )
{
v12 = (unsigned __int8)curbyte_0;
}
else
{
if ( tape_read_file )
{
curbyte_0 = fgetc(tape_read_file);
v12 = (unsigned __int8)curbyte_0;
}
else
{
lc_word(0LL, v27);
curbyte_0 = v27[0];
v12 = LOBYTE(v27[0]);
}
v10 = bitnum_4;
v11 = bitnum_4 & 7;
v24 = (unsigned __int16)(v12 + checksum_3) + 1;
if ( ((v12 + checksum_3) & 0x10000) == 0 )
v24 = v12 + checksum_3;
checksum_3 = v24;
}
bitnum_4 = v10 + 1;
v0 = (v12 >> v11) & 1;
if ( 8 * curlen_1 == v10 + 1 )
{
v13 = checksum_3;
if ( !tape_read_file )
v13 = ++checksum_3;
v14 = dcgettext(0LL, "Checksum will be %06o\n", 5);
fprintf(stderr, v14, v13);
fake_state = 5;
bitnum_4 = 0;
}
break;
case 5:
v7 = (unsigned int)checksum_3 >> bitnum_4++;
v0 = v7 & 1;
if ( bitnum_4 == 16 )
{
if ( tape_read_file )
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
break;
default:
goto LABEL_9;
}
goto LABEL_9;
}
if ( !tape_read_file )
{
get_emt36_filename();
tape_read_file = fopen(unix_filename, "r");
v18 = dcgettext(0LL, "Will read unix file <%s> under BK name <%s>\n", 5);
fprintf(stderr, v18, unix_filename, &bk_filename);
v15 = 0;
fake_state = 1;
bitnum_4 = 0;
checksum_3 = 0;
curaddr_2 = 0;
curlen_1 = 0;
LABEL_28:
if ( tape_read_file )
{
v19 = fgetc(tape_read_file);
v20 = v19;
v21 = v19;
v15 = bitnum_4;
v22 = v20 << bitnum_4;
v0 = (v20 >> (bitnum_4 & 7)) & 1;
}
else
{
v0 = 0;
v22 = 0;
v21 = 0;
}
curaddr_2 |= v22;
curbyte_0 = v21;
LABEL_25:
bitnum_4 = v15 + 1;
if ( v15 == 15 )
{
v16 = curaddr_2;
v17 = dcgettext(0LL, "File address will be %o\n", 5);
fprintf(stderr, v17, v16);
fake_state = 2;
bitnum_4 = 0;
}
LABEL_9:
v2 = v0 == 0 ? 5 : 15;
goto LABEL_10;
}
v1 = dcgettext(0LL, "Asked for strobe in Idle state?\n", 5);
fprintf(stderr, v1);
fclose(tape_read_file);
v2 = 5;
LABEL_10:
pdp[4] = v2;
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

canonical_decompiled_code:
__int64 __fastcall func8()
{
int v0; // ebx
char *v1; // rax
__int16 v2; // ax
int v3; // ecx
char v4; // al
int v5; // ebx
unsigned int v7; // ebx
int v8; // eax
char v9; // cl
int v10; // eax
char v11; // cl
int v12; // ebx
unsigned int v13; // er12
char *v14; // rax
int v15; // eax
unsigned int v16; // er12
char *v17; // rax
char *v18; // rax
unsigned __int8 v19; // al
int v20; // ebx
char v21; // si
int v22; // edx
char v23; // al
int v24; // edx
unsigned int v25; // er12
char *v26; // rax
__int16 v27[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( fake_state )
{
switch ( fake_state )
{
case 1:
v15 = bitnum_4;
if ( (bitnum_4 & 7) == 0 )
goto LABEL_28;
v0 = ((int)(unsigned __int8)curbyte_0 >> (bitnum_4 & 7)) & 1;
goto LABEL_25;
case 2:
v3 = bitnum_4;
v4 = bitnum_4 & 7;
if ( (bitnum_4 & 7) != 0 )
{
v5 = (unsigned __int8)curbyte_0;
}
else
{
if ( tape_read_file )
{
v23 = fgetc(tape_read_file);
v3 = bitnum_4;
LOBYTE(v5) = v23;
v4 = bitnum_4 & 7;
}
else
{
LOBYTE(v5) = bitnum_4 == 0;
}
curbyte_0 = v5;
v5 = (unsigned __int8)v5;
curlen_1 |= (unsigned __int8)v5 << v3;
}
bitnum_4 = v3 + 1;
v0 = (v5 >> v4) & 1;
if ( v3 == 15 )
{
v25 = curlen_1;
v26 = dcgettext(0LL, "File length will be %o\n", 5);
fprintf(stderr, v26, v25);
fake_state = 3;
bitnum_4 = 0;
}
break;
case 3:
v8 = bitnum_4 >> 3;
v9 = bitnum_4++ & 7;
v0 = (*((char *)&bk_filename + v8) >> v9) & 1;
if ( bitnum_4 == 128 )
{
fake_state = 4;
bitnum_4 = 0;
}
break;
case 4:
v10 = bitnum_4;
v11 = bitnum_4 & 7;
if ( (bitnum_4 & 7) != 0 )
{
v12 = (unsigned __int8)curbyte_0;
}
else
{
if ( tape_read_file )
{
curbyte_0 = fgetc(tape_read_file);
v12 = (unsigned __int8)curbyte_0;
}
else
{
lc_word(0LL, v27);
curbyte_0 = v27[0];
v12 = LOBYTE(v27[0]);
}
v10 = bitnum_4;
v11 = bitnum_4 & 7;
v24 = (unsigned __int16)(v12 + checksum_3) + 1;
if ( ((v12 + checksum_3) & 0x10000) == 0 )
v24 = v12 + checksum_3;
checksum_3 = v24;
}
bitnum_4 = v10 + 1;
v0 = (v12 >> v11) & 1;
if ( 8 * curlen_1 == v10 + 1 )
{
v13 = checksum_3;
if ( !tape_read_file )
v13 = ++checksum_3;
v14 = dcgettext(0LL, "Checksum will be %06o\n", 5);
fprintf(stderr, v14, v13);
fake_state = 5;
bitnum_4 = 0;
}
break;
case 5:
v7 = (unsigned int)checksum_3 >> bitnum_4++;
v0 = v7 & 1;
if ( bitnum_4 == 16 )
{
if ( tape_read_file )
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
break;
default:
goto LABEL_9;
}
goto LABEL_9;
}
if ( !tape_read_file )
{
func2();
tape_read_file = fopen(unix_filename, "r");
v18 = dcgettext(0LL, "Will read unix file <%s> under BK name <%s>\n", 5);
fprintf(stderr, v18, unix_filename, &bk_filename);
v15 = 0;
fake_state = 1;
bitnum_4 = 0;
checksum_3 = 0;
curaddr_2 = 0;
curlen_1 = 0;
LABEL_28:
if ( tape_read_file )
{
v19 = fgetc(tape_read_file);
v20 = v19;
v21 = v19;
v15 = bitnum_4;
v22 = v20 << bitnum_4;
v0 = (v20 >> (bitnum_4 & 7)) & 1;
}
else
{
v0 = 0;
v22 = 0;
v21 = 0;
}
curaddr_2 |= v22;
curbyte_0 = v21;
LABEL_25:
bitnum_4 = v15 + 1;
if ( v15 == 15 )
{
v16 = curaddr_2;
v17 = dcgettext(0LL, "File address will be %o\n", 5);
fprintf(stderr, v17, v16);
fake_state = 2;
bitnum_4 = 0;
}
LABEL_9:
v2 = v0 == 0 ? 5 : 15;
goto LABEL_10;
}
v1 = dcgettext(0LL, "Asked for strobe in Idle state?\n", 5);
fprintf(stderr, v1);
fclose(tape_read_file);
v2 = 5;
LABEL_10:
pdp[4] = v2;
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

original_code:
void fake_read_strobe() {
 static int bitnum;
 static unsigned checksum;
 static unsigned char curbyte;
 static unsigned curlen;
 static unsigned curaddr;
 int bit;
 if (fake_state == Idle && !tape_read_file) {
  get_emt36_filename();
  tape_read_file = fopen(unix_filename, "r");
  fprintf(stderr, gettext ("Will read unix file <%s> under BK name <%s>\n"),
   unix_filename, bk_filename);
  fake_state = Addr;
  bitnum = 0;
  checksum = 0;
  curlen = curaddr = 0;
 }
 switch (fake_state) {
 case Idle:
  fprintf(stderr, gettext ("Asked for strobe in Idle state?\n"));
  fclose(tape_read_file);
  bit = 0;
  break;
 case Addr:
  if (!(bitnum & 7)) {
   curbyte = tape_read_file ? fgetc(tape_read_file) : 0;
   curaddr |= curbyte << bitnum;
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File address will be %o\n"), curaddr);
   fake_state = Len;
   bitnum = 0;
  }
                break;
 case Len:
  if (!(bitnum & 7)) {
                        curbyte = tape_read_file ? fgetc(tape_read_file) :
    bitnum ? 0 : 1;
   curlen |= curbyte << bitnum;
                }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File length will be %o\n"), curlen);
                        fake_state = Name;
                        bitnum = 0;
                }
                break;
 case Name:
  bit = (bk_filename[bitnum >> 3] >> (bitnum & 7)) & 1;
  bitnum++;
  if (bitnum == 16 * 8) {
   fake_state = Data;
   bitnum = 0;
  }
  break;
 case Data:
  if (!(bitnum & 7)) {
   if (tape_read_file) {
    curbyte = fgetc(tape_read_file);
   } else {
    d_word w;
    lc_word(0, &w);
    curbyte = w & 0xff;
   }
   checksum += curbyte;
   if (checksum & 0200000) {
    checksum = (checksum & 0xFFFF) + 1;
   }
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == curlen * 8) {
   if (!tape_read_file) checksum++;
   fprintf(stderr, gettext ("Checksum will be %06o\n"), checksum);
   fake_state = Checksum;
   bitnum = 0;
  }
  break;
 case Checksum:
  bit = (checksum >> bitnum++) & 1;
  if (bitnum == 16) {
   if (tape_read_file) fclose(tape_read_file);
   tape_read_file = 0;
   fake_state = Idle;
  }
  break;
 }
        pdp.regs[4] = bit ? 15 : 5;
        pop( &pdp, &pdp.regs[7] );
}

canonical_original_code:
void fake_read_strobe() {
 static int bitnum;
 static unsigned int checksum;
 static unsigned char curbyte;
 static unsigned int curlen;
 static unsigned int curaddr;
 int bit;
 if (fake_state == Idle && !tape_read_file) {
  get_emt36_filename();
  tape_read_file = fopen(unix_filename, "r");
  fprintf(stderr, gettext ("Will read unix file <%s> under BK name <%s>\n"),
   unix_filename, bk_filename);
  fake_state = Addr;
  bitnum = 0;
  checksum = 0;
  curlen = curaddr = 0;
 }
 switch (fake_state) {
 case Idle:
  fprintf(stderr, gettext ("Asked for strobe in Idle state?\n"));
  fclose(tape_read_file);
  bit = 0;
  break;
 case Addr:
  if (!(bitnum & 7)) {
   curbyte = tape_read_file ? fgetc(tape_read_file) : 0;
   curaddr |= curbyte << bitnum;
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File address will be %o\n"), curaddr);
   fake_state = Len;
   bitnum = 0;
  }
                break;
 case Len:
  if (!(bitnum & 7)) {
                        curbyte = tape_read_file ? fgetc(tape_read_file) :
    bitnum ? 0 : 1;
   curlen |= curbyte << bitnum;
                }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File length will be %o\n"), curlen);
                        fake_state = Name;
                        bitnum = 0;
                }
                break;
 case Name:
  bit = (bk_filename[bitnum >> 3] >> (bitnum & 7)) & 1;
  bitnum++;
  if (bitnum == 16 * 8) {
   fake_state = Data;
   bitnum = 0;
  }
  break;
 case Data:
  if (!(bitnum & 7)) {
   if (tape_read_file) {
    curbyte = fgetc(tape_read_file);
   } else {
    unsigned short w;
    lc_word(0, &w);
    curbyte = w & 0xff;
   }
   checksum += curbyte;
   if (checksum & 0200000) {
    checksum = (checksum & 0xFFFF) + 1;
   }
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == curlen * 8) {
   if (!tape_read_file) checksum++;
   fprintf(stderr, gettext ("Checksum will be %06o\n"), checksum);
   fake_state = Checksum;
   bitnum = 0;
  }
  break;
 case Checksum:
  bit = (checksum >> bitnum++) & 1;
  if (bitnum == 16) {
   if (tape_read_file) fclose(tape_read_file);
   tape_read_file = 0;
   fake_state = Idle;
  }
  break;
 }
        pdp.regs[4] = bit ? 15 : 5;
        pop( &pdp, &pdp.regs[7] );
}

variable_types: bitnum int

variable_types:
checksum
unsigned int

variable_types:
curbyte
unsigned char

variable_types:
curlen
unsigned int

variable_types:
curaddr
unsigned int

variable_types: bit int

variable_types:
w
unsigned short


decompiled_code:
__int64 fake_write_file()
{
FILE *v0; // rax
const char *v1; // rdx
unsigned __int16 v2; // di
int v3; // edi
bool v4; // zf
unsigned __int16 v6; // [rsp+2h] [rbp-16h] BYREF
__int16 v7; // [rsp+4h] [rbp-14h] BYREF
__int16 v8; // [rsp+6h] [rbp-12h] BYREF
__int64 v9; // [rsp+8h] [rbp-10h] BYREF

lc_word(198LL, &v9);
get_emt36_filename();
v0 = fopen(unix_filename, "w");
v1 = " ";
tape_write_file = v0;
if ( !v0 )
v1 = " NOT ";
fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n", v1, &bk_filename, unix_filename);
if ( tape_write_file )
{
lc_word(v9 + 2, &v6);
fputc((unsigned __int8)v6, tape_write_file);
fputc(HIBYTE(v6), tape_write_file);
lc_word(v9 + 4, &v7);
fputc((unsigned __int8)v7, tape_write_file);
fputc(HIBYTE(v7), tape_write_file);
v2 = v6;
if ( v7 )
{
do
{
lc_word(v2, &v8);
if ( (v6 & 1) != 0 )
{
v3 = HIBYTE(v8);
v8 = HIBYTE(v8);
}
else
{
v3 = (unsigned __int8)v8;
}
fputc(v3, tape_write_file);
v4 = v7-- == 1;
v2 = ++v6;
}
while ( !v4 );
}
fclose(tape_write_file);
tape_write_file = 0LL;
sl_byte(pdp, v9 + 1, 0LL);
}
else
{
perror(unix_filename);
sl_byte(pdp, v9 + 1, 1LL);
}
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

canonical_decompiled_code:
__int64 func9()
{
FILE *v0; // rax
const char *v1; // rdx
unsigned __int16 v2; // di
int v3; // edi
bool v4; // zf
unsigned __int16 v6; // [rsp+2h] [rbp-16h] BYREF
__int16 v7; // [rsp+4h] [rbp-14h] BYREF
__int16 v8; // [rsp+6h] [rbp-12h] BYREF
__int64 v9; // [rsp+8h] [rbp-10h] BYREF

lc_word(198LL, &v9);
func2();
v0 = fopen(unix_filename, "w");
v1 = " ";
tape_write_file = v0;
if ( !v0 )
v1 = " NOT ";
fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n", v1, &bk_filename, unix_filename);
if ( tape_write_file )
{
lc_word(v9 + 2, &v6);
fputc((unsigned __int8)v6, tape_write_file);
fputc(HIBYTE(v6), tape_write_file);
lc_word(v9 + 4, &v7);
fputc((unsigned __int8)v7, tape_write_file);
fputc(HIBYTE(v7), tape_write_file);
v2 = v6;
if ( v7 )
{
do
{
lc_word(v2, &v8);
if ( (v6 & 1) != 0 )
{
v3 = HIBYTE(v8);
v8 = HIBYTE(v8);
}
else
{
v3 = (unsigned __int8)v8;
}
fputc(v3, tape_write_file);
v4 = v7-- == 1;
v2 = ++v6;
}
while ( !v4 );
}
fclose(tape_write_file);
tape_write_file = 0LL;
sl_byte(pdp, v9 + 1, 0LL);
}
else
{
perror(unix_filename);
sl_byte(pdp, v9 + 1, 1LL);
}
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

original_code:
void
fake_write_file() {
 c_addr base;
 lc_word(0306, &base);
 get_emt36_filename();
 tape_write_file = fopen(unix_filename, "w");
 fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n",
  tape_write_file ? " " : " NOT ", bk_filename, unix_filename);
 if (tape_write_file) {
  d_word addr;
         d_word length;
  lc_word(base + 2, &addr);
  fputc(addr & 0xff, tape_write_file);
  fputc(addr >> 8, tape_write_file);
  lc_word(base + 4, &length);
  fputc(length & 0xff, tape_write_file);
  fputc(length >> 8, tape_write_file);
  for (; length; addr++, length--) {
   d_word w;
   lc_word(addr, &w);
   if (addr & 1) w >>= 8;
   fputc(w & 0xff, tape_write_file);
  }
  fclose(tape_write_file);
  tape_write_file = 0;
  sl_byte(&pdp, base+1, 0);
 } else {
  perror(unix_filename);
  sl_byte(&pdp, base+1, 1);
 }
 pop( &pdp, &pdp.regs[7] );
}

canonical_original_code:
void fake_write_file() {
 unsigned long base;
 lc_word(0306, &base);
 get_emt36_filename();
 tape_write_file = fopen(unix_filename, "w");
 fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n",
  tape_write_file ? " " : " NOT ", bk_filename, unix_filename);
 if (tape_write_file) {
  unsigned short addr;
         unsigned short length;
  lc_word(base + 2, &addr);
  fputc(addr & 0xff, tape_write_file);
  fputc(addr >> 8, tape_write_file);
  lc_word(base + 4, &length);
  fputc(length & 0xff, tape_write_file);
  fputc(length >> 8, tape_write_file);
  for (; length; addr++, length--) {
   unsigned short w;
   lc_word(addr, &w);
   if (addr & 1) w >>= 8;
   fputc(w & 0xff, tape_write_file);
  }
  fclose(tape_write_file);
  tape_write_file = 0;
  sl_byte(&pdp, base+1, 0);
 } else {
  perror(unix_filename);
  sl_byte(&pdp, base+1, 1);
 }
 pop( &pdp, &pdp.regs[7] );
}

variable_types:
base
unsigned long

variable_types:
addr
unsigned short

variable_types:
length
unsigned short


unit

decompiled_code:
__int64 __fastcall unit(unsigned __int16 a1)
{
return HIBYTE(a1) & 3;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1)
{
return HIBYTE(a1) & 3;
}

original_code:
static inline unsigned unit(d_word word) {
 return (word >> 8) & 3;
}

canonical_original_code:
static inline unsigned int unit(unsigned short word) {
 return (word >> 8) & 3;
}

variable_types:
word
unsigned short

return_type: unsigned int


cmd

decompiled_code:
__int64 __fastcall cmd(unsigned __int16 a1)
{
return (a1 >> 1) & 7;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
return (a1 >> 1) & 7;
}

original_code:
static inline disk_cmd cmd(d_word word) {
 return (word >> 1) & 7;
}

canonical_original_code:
static inline enum disk_cmd cmd(unsigned short word) {
 return (word >> 1) & 7;
}

return_type: enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 }


decompiled_code:
void __fastcall tdisk_open(__int64 a1, const char *a2)
{
int v2; // ebp
unsigned int v3; // eax
void *v4; // rax
char *v5; // rax
char *v6; // rax

v2 = open(a2, 2);
if ( v2 == -1 && (*(_BYTE *)(a1 + 32) = 1, v2 = open(a2, 0), v2 == -1) )
{
perror(a2);
}
else
{
v3 = lseek(v2, 0LL, 2);
*(_DWORD *)a1 = v3;
if ( (v3 & 0x3F) != 0 )
{
v6 = dcgettext(0LL, "%s is not an integer number of blocks\n", 5);
fprintf(stderr, v6, a2);
close(v2);
}
else
{
v4 = mmap(0LL, v3, *(_BYTE *)(a1 + 32) == 0 ? 3 : 1, 1, v2, 0LL);
*(_QWORD *)(a1 + 8) = v4;
if ( v4 == (void *)-1LL )
{
*(_QWORD *)(a1 + 8) = 0LL;
perror(a2);
}
if ( *(_BYTE *)(a1 + 32) )
{
v5 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v5, a2);
}
}
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, const char *a2)
{
int v2; // ebp
unsigned int v3; // eax
void *v4; // rax
char *v5; // rax
char *v6; // rax

v2 = open(a2, 2);
if ( v2 == -1 && (*(_BYTE *)(a1 + 32) = 1, v2 = open(a2, 0), v2 == -1) )
{
perror(a2);
}
else
{
v3 = lseek(v2, 0LL, 2);
*(_DWORD *)a1 = v3;
if ( (v3 & 0x3F) != 0 )
{
v6 = dcgettext(0LL, "%s is not an integer number of blocks\n", 5);
fprintf(stderr, v6, a2);
close(v2);
}
else
{
v4 = mmap(0LL, v3, *(_BYTE *)(a1 + 32) == 0 ? 3 : 1, 1, v2, 0LL);
*(_QWORD *)(a1 + 8) = v4;
if ( v4 == (void *)-1LL )
{
*(_QWORD *)(a1 + 8) = 0LL;
perror(a2);
}
if ( *(_BYTE *)(a1 + 32) )
{
v5 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v5, a2);
}
}
}
}

original_code:
void tdisk_open(tdisk_t * pdt, char * name) {
 int fd = open(name, 02);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length % 64) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks\n"), name);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

canonical_original_code:
void tdisk_open(struct tdisk_t *pdt, char *name) {
 int fd = open(name, 02);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length % 64) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks\n"), name);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

variable_types: struct tdisk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 } cmd; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char cursec; } *

variable_types:
name
char *

variable_types: fd int

user_defined_types: struct tdisk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; enum disk_cmd cmd; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char cursec; }


decompiled_code:
__int64 tdisk_init()
{
char *v0; // rax

if ( !init_done_0 )
{
disk_open(tdisks, floppyA);
disk_open((unsigned int)((_DWORD)tdisks + 40), floppyB);
disk_open((unsigned int)((_DWORD)tdisks + 80), floppyC);
disk_open((unsigned int)((_DWORD)tdisks + 120), floppyD);
init_done_0 = 1;
}
v0 = tdisks;
do
{
*((_QWORD *)v0 + 2) = 0LL;
v0 += 40;
*(v0 - 6) = 0;
*(v0 - 7) = 0;
*(v0 - 16) = 0;
}
while ( v0 != &tdisks[160] );
selected = -1;
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
char *v0; // rax

if ( !init_done_0 )
{
disk_open(tdisks, floppyA);
disk_open((unsigned int)((_DWORD)tdisks + 40), floppyB);
disk_open((unsigned int)((_DWORD)tdisks + 80), floppyC);
disk_open((unsigned int)((_DWORD)tdisks + 120), floppyD);
init_done_0 = 1;
}
v0 = tdisks;
do
{
*((_QWORD *)v0 + 2) = 0LL;
v0 += 40;
*(v0 - 6) = 0;
*(v0 - 7) = 0;
*(v0 - 16) = 0;
}
while ( v0 != &tdisks[160] );
selected = -1;
return 0LL;
}

original_code:
int tdisk_init() {
 static char init_done = 0;
 int i;
 if (!init_done) {
  disk_open(&tdisks[0], floppyA);
  disk_open(&tdisks[1], floppyB);
  disk_open(&tdisks[2], floppyC);
  disk_open(&tdisks[3], floppyD);
  init_done = 1;
 }
 for (i = 0; i < 4; i++) {
  tdisks[i].ptr = ((void *)0);
  tdisks[i].track =
  tdisks[i].motor =
  tdisks[i].inprogress = 0;
 }
 selected = -1;
 return 0;
}

variable_types: i int

return_type: int


decompiled_code:
int tdisk_finish()
{
char *v0; // rbx
void *v1; // rdi
int result; // eax

v0 = tdisks;
do
{
v1 = (void *)*((_QWORD *)v0 + 1);
if ( v1 )
result = munmap(v1, *(unsigned int *)v0);
v0 += 40;
}
while ( v0 != &tdisks[160] );
return result;
}

canonical_decompiled_code:
int func4()
{
char *v0; // rbx
void *v1; // rdi
int result; // eax

v0 = tdisks;
do
{
v1 = (void *)*((_QWORD *)v0 + 1);
if ( v1 )
result = munmap(v1, *(unsigned int *)v0);
v0 += 40;
}
while ( v0 != &tdisks[160] );
return result;
}

original_code:
void tdisk_finish() {
 int i;
 for (i = 0; i < 4; i++) {
  if (!tdisks[i].image)
   continue;
  munmap(tdisks[i].image, tdisks[i].length);
 }
}


original_code:
int
tdisk_read(c_addr addr, d_word *word) {
 d_word offset = addr - 0177000;
 tdisk_t * pdt = &tdisks[selected];
 int index;
 switch(offset) {
 case 0:
  if (selected == -1) {
  *word = errF | doneF;
  break;
  }
  *word = (pdt->track ? 0 : track0F) | (pdt->ro ? wrprF : 0) | headF | doneF;
  if (!pdt->inprogress) {
   return 0;
  } else switch (pdt->cmd) {
   case nopD:
   case rtcD:
   case writeD:
   case delD:
    break;
   case stepinD:
    if (pdt->track == 76)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track++;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case stepoutD:
    if (pdt->track == 0)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track--;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case readtsD:
    if (pdt->inprogress) {
     pdt->cursec %= 26;
     pdt->cursec++;
    }
    break;
   case readD:
    fprintf(stderr, "Reading track %d, sector %d\n",
     pdt->track, pdt->cursec);
    pdt->ptr = pdt->image +
     pdt->track*26*64 +
     (pdt->cursec-1)*64;
    break;
  }
  pdt->inprogress = 0;
  break;
 case 2:
  switch (pdt->cmd) {
  case readtsD:
   *word = pdt->cursec << 8 | pdt->track;
   fprintf(stderr, "Trk/sec = %d/%d\n", pdt->track, pdt->cursec);
   break;
  case readD:
   *word = *pdt->ptr++;
   break;
  default:
   *word = 0;
  }
 break;
 }
 return 0;
}

canonical_original_code:
int tdisk_read(unsigned long addr, unsigned short *word) {
 unsigned short offset = addr - 0177000;
 struct tdisk_t *pdt = &tdisks[selected];
 int index;
 switch(offset) {
 case 0:
  if (selected == -1) {
  *word = errF | doneF;
  break;
  }
  *word = (pdt->track ? 0 : track0F) | (pdt->ro ? wrprF : 0) | headF | doneF;
  if (!pdt->inprogress) {
   return 0;
  } else switch (pdt->cmd) {
   case nopD:
   case rtcD:
   case writeD:
   case delD:
    break;
   case stepinD:
    if (pdt->track == 76)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track++;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case stepoutD:
    if (pdt->track == 0)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track--;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case readtsD:
    if (pdt->inprogress) {
     pdt->cursec %= 26;
     pdt->cursec++;
    }
    break;
   case readD:
    fprintf(stderr, "Reading track %d, sector %d\n",
     pdt->track, pdt->cursec);
    pdt->ptr = pdt->image +
     pdt->track*26*64 +
     (pdt->cursec-1)*64;
    break;
  }
  pdt->inprogress = 0;
  break;
 case 2:
  switch (pdt->cmd) {
  case readtsD:
   *word = pdt->cursec << 8 | pdt->track;
   fprintf(stderr, "Trk/sec = %d/%d\n", pdt->track, pdt->cursec);
   break;
  case readD:
   *word = *pdt->ptr++;
   break;
  default:
   *word = 0;
  }
 break;
 }
 return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short *

variable_types:
offset
unsigned short

variable_types: index int

user_defined_types: enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 }


decompiled_code:
__int64 __fastcall tdisk_write(__int16 a1, unsigned __int16 a2)
{
__int16 v2; // di
char *v3; // rax
__int64 result; // rax
int v5; // eax
char *v6; // rdx
int v7; // eax
__int64 v8; // rdx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rdx

v2 = a1 + 512;
if ( v2 )
{
if ( v2 == 2 )
{
v3 = dcgettext(0LL, "Writing disk data reg, data %06o\n", 5);
fprintf(stderr, v3, a2);
}
return 0LL;
}
v5 = unit(a2);
selected = v5;
if ( v5 < 0 )
return 0LL;
v6 = &tdisks[40 * v5];
result = 2LL;
if ( v6[34] )
return result;
v6[34] = a2 & 1;
v7 = cmd(a2);
*(_DWORD *)(v8 + 28) = v7;
if ( (a2 & 0x41) != 65 )
return 0LL;
if ( v7 )
{
if ( v7 == 1 )
{
v9 = TICK_RATE;
v10 = (1374389535LL * TICK_RATE) >> 36;
}
else
{
fwrite("Interrupt requested\n", 1uLL, 0x14uLL, stderr);
v9 = TICK_RATE;
v10 = (274877907LL * TICK_RATE) >> 38;
}
v11 = (unsigned int)(v10 - (v9 >> 31));
}
else
{
v11 = (unsigned int)(4 * TICK_RATE);
}
ev_register(1LL, &service, v11, 168LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1, unsigned __int16 a2)
{
__int16 v2; // di
char *v3; // rax
__int64 result; // rax
int v5; // eax
char *v6; // rdx
int v7; // eax
__int64 v8; // rdx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rdx

v2 = a1 + 512;
if ( v2 )
{
if ( v2 == 2 )
{
v3 = dcgettext(0LL, "Writing disk data reg, data %06o\n", 5);
fprintf(stderr, v3, a2);
}
return 0LL;
}
v5 = func0(a2);
selected = v5;
if ( v5 < 0 )
return 0LL;
v6 = &tdisks[40 * v5];
result = 2LL;
if ( v6[34] )
return result;
v6[34] = a2 & 1;
v7 = func1(a2);
*(_DWORD *)(v8 + 28) = v7;
if ( (a2 & 0x41) != 65 )
return 0LL;
if ( v7 )
{
if ( v7 == 1 )
{
v9 = TICK_RATE;
v10 = (1374389535LL * TICK_RATE) >> 36;
}
else
{
fwrite("Interrupt requested\n", 1uLL, 0x14uLL, stderr);
v9 = TICK_RATE;
v10 = (274877907LL * TICK_RATE) >> 38;
}
v11 = (unsigned int)(v10 - (v9 >> 31));
}
else
{
v11 = (unsigned int)(4 * TICK_RATE);
}
ev_register(1LL, &service, v11, 168LL);
return 0LL;
}

original_code:
int
tdisk_write(c_addr addr, d_word word) {
 d_word offset = addr - 0177000;
 tdisk_t * pdt;
 switch (offset) {
 case 0:
  if (word) {
  }
  selected = unit(word);
  if (selected >= 0) {
   pdt = &tdisks[selected];
   if (pdt->inprogress)
    return 2;
   pdt->inprogress = word & enF;
   pdt->cmd = cmd(word);
   if (pdt->inprogress && word & intrF) switch (pdt->cmd) {
    case nopD:
     ev_register(1, service, TICK_RATE*100/25, 0250);
     break;
    case rtcD:
     ev_register(1, service, TICK_RATE/50, 0250);
     break;
    default:
     fprintf(stderr, "Interrupt requested\n");
     ev_register(1, service, TICK_RATE/1000, 0250);
   }
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing disk data reg, data %06o\n"), word);
  break;
 }
 return 0;
}

canonical_original_code:
int tdisk_write(unsigned long addr, unsigned short word) {
 unsigned short offset = addr - 0177000;
 struct tdisk_t *pdt;
 switch (offset) {
 case 0:
  if (word) {
  }
  selected = unit(word);
  if (selected >= 0) {
   pdt = &tdisks[selected];
   if (pdt->inprogress)
    return 2;
   pdt->inprogress = word & enF;
   pdt->cmd = cmd(word);
   if (pdt->inprogress && word & intrF) switch (pdt->cmd) {
    case nopD:
     ev_register(1, service, TICK_RATE*100/25, 0250);
     break;
    case rtcD:
     ev_register(1, service, TICK_RATE/50, 0250);
     break;
    default:
     fprintf(stderr, "Interrupt requested\n");
     ev_register(1, service, TICK_RATE/1000, 0250);
   }
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing disk data reg, data %06o\n"), word);
  break;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall tdisk_bwrite(__int64 a1, unsigned __int8 a2)
{
return disk_write(a1 & 0xFFFFFFFFFFFFFFFELL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 a2)
{
return disk_write(a1 & 0xFFFFFFFFFFFFFFFELL, a2);
}

original_code:
int
tdisk_bwrite(c_addr addr, d_byte byte) {
 return disk_write(addr & ~1, byte);
}

canonical_original_code:
int tdisk_bwrite(unsigned long addr, unsigned char byte) {
 return disk_write(addr & ~1, byte);
}

variable_types:
byte
unsigned char


printop

decompiled_code:
size_t __fastcall printop(unsigned int a1, _QWORD *a2, char **a3)
{
__int64 v3; // rbp
char *v5; // rax
char *v6; // rbp
size_t result; // rax
char *v8; // rax
__int64 v9; // rdx
char *v10; // rax
__int64 v11; // rdx
char *v12; // rdi
__int16 v13[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v3 = a1 & 7;
if ( a1 <= 0x3F )
{
switch ( a1 >> 3 )
{
case 0u:
sprintf(*a3, regnam[v3]);
break;
case 1u:
sprintf(*a3, "(%s)", regnam[v3]);
break;
case 2u:
goto LABEL_10;
case 3u:
v10 = (*a3)++;
*v10 = 64;
LABEL_10:
if ( (_DWORD)v3 == 7 )
{
lc_word(*a2, v13);
v11 = (unsigned __int16)v13[0];
v12 = *a3;
*a2 = (unsigned __int16)(*a2 + 2);
sprintf(v12, "#%o", v11);
}
else
{
sprintf(*a3, "(%s)+", regnam[v3]);
}
break;
case 4u:
goto LABEL_4;
case 5u:
v5 = (*a3)++;
*v5 = 64;
LABEL_4:
sprintf(*a3, "-(%s)", regnam[v3]);
break;
case 6u:
goto LABEL_7;
case 7u:
v8 = (*a3)++;
*v8 = 64;
LABEL_7:
lc_word(*a2, v13);
v9 = (unsigned __int16)(*a2 + 2);
*a2 = v9;
if ( (_DWORD)v3 == 7 )
sprintf(*a3, "%o", (unsigned __int16)(v13[0] + v9));
else
sprintf(*a3, "%o(%s)", (unsigned __int16)v13[0], regnam[v3]);
break;
}
}
v6 = *a3;
result = strlen(*a3);
*a3 = &v6[result];
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(unsigned int a1, _QWORD *a2, char **a3)
{
__int64 v3; // rbp
char *v5; // rax
char *v6; // rbp
size_t result; // rax
char *v8; // rax
__int64 v9; // rdx
char *v10; // rax
__int64 v11; // rdx
char *v12; // rdi
__int16 v13[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v3 = a1 & 7;
if ( a1 <= 0x3F )
{
switch ( a1 >> 3 )
{
case 0u:
sprintf(*a3, regnam[v3]);
break;
case 1u:
sprintf(*a3, "(%s)", regnam[v3]);
break;
case 2u:
goto LABEL_10;
case 3u:
v10 = (*a3)++;
*v10 = 64;
LABEL_10:
if ( (_DWORD)v3 == 7 )
{
lc_word(*a2, v13);
v11 = (unsigned __int16)v13[0];
v12 = *a3;
*a2 = (unsigned __int16)(*a2 + 2);
sprintf(v12, "#%o", v11);
}
else
{
sprintf(*a3, "(%s)+", regnam[v3]);
}
break;
case 4u:
goto LABEL_4;
case 5u:
v5 = (*a3)++;
*v5 = 64;
LABEL_4:
sprintf(*a3, "-(%s)", regnam[v3]);
break;
case 6u:
goto LABEL_7;
case 7u:
v8 = (*a3)++;
*v8 = 64;
LABEL_7:
lc_word(*a2, v13);
v9 = (unsigned __int16)(*a2 + 2);
*a2 = v9;
if ( (_DWORD)v3 == 7 )
sprintf(*a3, "%o", (unsigned __int16)(v13[0] + v9));
else
sprintf(*a3, "%o(%s)", (unsigned __int16)v13[0], regnam[v3]);
break;
}
}
v6 = *a3;
result = strlen(*a3);
*a3 = &v6[result];
return result;
}

original_code:
void printop(unsigned rm, c_addr * a, char **destp) {
 d_word word;
 unsigned r = rm & 7;
 unsigned mode = rm >> 3;
 switch (mode) {
 case 0:
  sprintf(*destp, regnam[r]);
  break;
 case 1:
  sprintf(*destp, "(%s)", regnam[r]);
  break;
 case 3:
  *(*destp)++ = '@';
 case 2:
  if (r == 7) {
   lc_word(*a, &word);
   *a += 2;
   *a &= 0177777;
   sprintf(*destp, "#%o", word);
  } else {
   sprintf(*destp, "(%s)+", regnam[r]);
  }
  break;
 case 5:
  *(*destp)++ = '@';
 case 4:
  sprintf(*destp, "-(%s)", regnam[r]);
  break;
 case 7:
  *(*destp)++ = '@';
 case 6:
  lc_word(*a, &word);
  *a += 2;
  *a &= 0177777;
  if (r == 7) {
   sprintf(*destp, "%o", (*a + word) & 0177777);
  } else {
   sprintf(*destp, "%o(%s)", word, regnam[r]);
  }
 }
 *destp += strlen(*destp);
}

canonical_original_code:
void printop(unsigned int rm, unsigned long *a, char **destp) {
 unsigned short word;
 unsigned int r = rm & 7;
 unsigned int mode = rm >> 3;
 switch (mode) {
 case 0:
  sprintf(*destp, regnam[r]);
  break;
 case 1:
  sprintf(*destp, "(%s)", regnam[r]);
  break;
 case 3:
  *(*destp)++ = '@';
 case 2:
  if (r == 7) {
   lc_word(*a, &word);
   *a += 2;
   *a &= 0177777;
   sprintf(*destp, "#%o", word);
  } else {
   sprintf(*destp, "(%s)+", regnam[r]);
  }
  break;
 case 5:
  *(*destp)++ = '@';
 case 4:
  sprintf(*destp, "-(%s)", regnam[r]);
  break;
 case 7:
  *(*destp)++ = '@';
 case 6:
  lc_word(*a, &word);
  *a += 2;
  *a &= 0177777;
  if (r == 7) {
   sprintf(*destp, "%o", (*a + word) & 0177777);
  } else {
   sprintf(*destp, "%o(%s)", word, regnam[r]);
  }
 }
 *destp += strlen(*destp);
}

variable_types:
rm
unsigned int

variable_types:
a
unsigned long *

variable_types:
destp
char **

variable_types:
r
unsigned int

variable_types:
mode
unsigned int


disas

original_code:
c_addr disas (c_addr a, char * dest) {
 d_word inst = 0;
 lc_word(a, &inst);
 a += 2;
 a &= 0177777;
 char * code = base[inst >> 12];
 char soffset;
 unsigned char uoffset;
 switch (*code) {
  case '0': code = decode0[(inst >> 6) & 077]; break;
  case '7': code = decode7[(inst >> 9) & 7]; break;
  case '8': code = decode8[(inst >> 6) & 077]; break;
 }
 switch (*code) {
  case '0': code = decode000[inst & 077]; break;
  case '2': code = decode002[inst & 077]; break;
  case '5':
   if (inst & 0000740)
    code = "N";
   else
    code = decode750[(inst >> 3) & 3];
   break;
 }
 sprintf(dest, "%06o: %s ", a - 2, code + !!((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short int) _ISupper));
 if (((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short int) _ISupper)) {
  dest += strlen(dest);
  switch (*code) {
  case 'B':
   soffset = inst;
   sprintf(dest, "%06o", a + 2 * soffset);
   break;
  case 'F':
   sprintf(dest, "(%s)", regnam[inst & 7]);
   break;
  case 'S':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   uoffset = inst & 077;
   dest += strlen(dest);
   sprintf(dest, "%06o", a - 2 * uoffset);
   break;
  case 'G':
   printop((inst >> 6) & 077, &a, &dest);
   sprintf(dest, ", ");
   dest += 2;
   printop(inst & 077, &a, &dest);
   break;
  case 'R':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   dest += strlen(dest);
  case 'O':
   printop(inst & 077, &a, &dest);
   break;
  case 'T':
   sprintf(dest, "%o", inst & 0377);
   break;
  case 'U':
   sprintf(dest, "%o", inst & 077);
   break;
  case 'N':
   sprintf(dest, "%06o", inst);
   break;
  default: sprintf(dest, gettext ("ERROR code %c"), *code);
 }}
 return a;
}

canonical_original_code:
unsigned long disas(unsigned long a, char *dest) {
 unsigned short inst = 0;
 lc_word(a, &inst);
 a += 2;
 a &= 0177777;
 char *code = base[inst >> 12];
 char soffset;
 unsigned char uoffset;
 switch (*code) {
  case '0': code = decode0[(inst >> 6) & 077]; break;
  case '7': code = decode7[(inst >> 9) & 7]; break;
  case '8': code = decode8[(inst >> 6) & 077]; break;
 }
 switch (*code) {
  case '0': code = decode000[inst & 077]; break;
  case '2': code = decode002[inst & 077]; break;
  case '5':
   if (inst & 0000740)
    code = "N";
   else
    code = decode750[(inst >> 3) & 3];
   break;
 }
 sprintf(dest, "%06o: %s ", a - 2, code + !!((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short) _ISupper));
 if (((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short) _ISupper)) {
  dest += strlen(dest);
  switch (*code) {
  case 'B':
   soffset = inst;
   sprintf(dest, "%06o", a + 2 * soffset);
   break;
  case 'F':
   sprintf(dest, "(%s)", regnam[inst & 7]);
   break;
  case 'S':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   uoffset = inst & 077;
   dest += strlen(dest);
   sprintf(dest, "%06o", a - 2 * uoffset);
   break;
  case 'G':
   printop((inst >> 6) & 077, &a, &dest);
   sprintf(dest, ", ");
   dest += 2;
   printop(inst & 077, &a, &dest);
   break;
  case 'R':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   dest += strlen(dest);
  case 'O':
   printop(inst & 077, &a, &dest);
   break;
  case 'T':
   sprintf(dest, "%o", inst & 0377);
   break;
  case 'U':
   sprintf(dest, "%o", inst & 077);
   break;
  case 'N':
   sprintf(dest, "%06o", inst);
   break;
  default: sprintf(dest, gettext ("ERROR code %c"), *code);
 }}
 return a;
}

variable_types:
a
unsigned long

variable_types:
dest
char *

variable_types:
inst
unsigned short

variable_types:
code
char *

variable_types: soffset char

variable_types:
uoffset
unsigned char

return_type: unsigned long


state

original_code:
char * state(pdp_regs * p) {
 static char buf[80];
 sprintf(buf, "%04x %04x %04x %04x %04x %04x %04x %04x",
  p->regs[0], p->regs[1], p->regs[2], p->regs[3],
  p->regs[4], p->regs[5], p->regs[6], p->psw);
 return buf;
}

canonical_original_code:
char *state(struct _pdp_regs *p) {
 static char buf[80];
 sprintf(buf, "%04x %04x %04x %04x %04x %04x %04x %04x",
  p->regs[0], p->regs[1], p->regs[2], p->regs[3],
  p->regs[4], p->regs[5], p->regs[6], p->psw);
 return buf;
}

variable_types: struct _pdp_regs { unsigned short regs[8]; unsigned char psw; unsigned short ir; unsigned short ea_addr; unsigned long total; unsigned int look_time; } *

variable_types:
buf
char [80]

return_type: char *


halt

decompiled_code:
__int64 halt()
{
io_stop_happened = 4;
return 5LL;
}

canonical_decompiled_code:
__int64 func2()
{
io_stop_happened = 4;
return 5LL;
}

original_code:
int halt(pdp_regs *p) {
 io_stop_happened = 4;
 return 5;
}

canonical_original_code:
int halt(struct _pdp_regs *p) {
 io_stop_happened = 4;
 return 5;
}

user_defined_types: struct _pdp_regs { unsigned short regs[8]; unsigned char psw; unsigned short ir; unsigned short ea_addr; unsigned long total; unsigned int look_time; }


iot

decompiled_code:
__int64 iot()
{
return 11LL;
}

canonical_decompiled_code:
__int64 func3()
{
return 11LL;
}

original_code: int iot() { return 11; }

canonical_original_code: int iot() { return 11; }


emt

decompiled_code:
__int64 emt()
{
return 9LL;
}

canonical_decompiled_code:
__int64 func4()
{
return 9LL;
}

original_code: int emt() { return 9; }

canonical_original_code: int emt() { return 9; }


trap

decompiled_code:
__int64 trap()
{
return 8LL;
}

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

original_code: int trap() { return 8; }

canonical_original_code: int trap() { return 8; }


bpt

decompiled_code:
__int64 bpt()
{
return 10LL;
}

canonical_decompiled_code:
__int64 func6()
{
return 10LL;
}

original_code: int bpt() { return 10; }

canonical_original_code: int bpt() { return 10; }


fis

decompiled_code:
__int64 fis()
{
return 4LL;
}

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

original_code: int fis() { return 4; }

canonical_original_code: int fis() { return 4; }


illegal

decompiled_code:
__int64 illegal()
{
return fis();
}

canonical_decompiled_code:
__int64 func8()
{
return func7();
}

original_code: int illegal() { return 4; }

canonical_original_code: int illegal() { return 4; }


decompiled_code:
__int64 __fastcall force_read(unsigned __int64 a1, _WORD *a2)
{
*a2 = *(_WORD *)((char *)*(&pagemap + (a1 >> 14)) + (a1 & 0x3FFE));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1, _WORD *a2)
{
*a2 = *(_WORD *)((char *)*(&pagemap + (a1 >> 14)) + (a1 & 0x3FFE));
return 0LL;
}

original_code:
int
force_read(c_addr addr, d_word *word )
{
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
}

canonical_original_code:
int force_read(unsigned long addr, unsigned short *word)
{
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short *


decompiled_code:
__int64 q_null()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
int q_null()
{
 return 0;
}

canonical_original_code:
int q_null()
{
 return 0;
}


decompiled_code:
__int64 __fastcall port_read(__int64 a1, _WORD *a2)
{
FILE *v3; // rdi

v3 = stderr;
*a2 = 0;
fprintf(v3, "Reading port %06o\n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _WORD *a2)
{
FILE *v3; // rdi

v3 = stderr;
*a2 = 0;
fprintf(v3, "Reading port %06o\n", a1);
return 0LL;
}

original_code:
int port_read(c_addr a, d_word *d) {
 *d = 0;
 fprintf(stderr, "Reading port %06o\n", a);
 return 0;
}

canonical_original_code:
int port_read(unsigned long a, unsigned short *d) {
 *d = 0;
 fprintf(stderr, "Reading port %06o\n", a);
 return 0;
}

variable_types:
d
unsigned short *


decompiled_code:
__int64 __fastcall port_write(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %06o to port %06o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %06o to port %06o\n", a2, a1);
return 0LL;
}

original_code:
int port_write(c_addr a, d_word d) {
 fprintf(stderr, "Writing %06o to port %06o\n", d, a);
 return 0;
}

canonical_original_code:
int port_write(unsigned long a, unsigned short d) {
 fprintf(stderr, "Writing %06o to port %06o\n", d, a);
 return 0;
}

variable_types:
d
unsigned short


decompiled_code:
__int64 __fastcall port_bwrite(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %03o to port %06o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %03o to port %06o\n", a2, a1);
return 0LL;
}

original_code:
int port_bwrite(c_addr a, d_byte d) {
 fprintf(stderr, "Writing %03o to port %06o\n", d, a);
 return 0;
}

canonical_original_code:
int port_bwrite(unsigned long a, unsigned char d) {
 fprintf(stderr, "Writing %03o to port %06o\n", d, a);
 return 0;
}

variable_types:
d
unsigned char


decompiled_code:
__int64 __fastcall secret_write(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

original_code:
int secret_write(c_addr a, d_word d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}

canonical_original_code:
int secret_write(unsigned long a, unsigned short d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}


decompiled_code:
__int64 __fastcall secret_bwrite(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

original_code:
int secret_bwrite(c_addr a, d_byte d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}

canonical_original_code:
int secret_bwrite(unsigned long a, unsigned char d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}


decompiled_code:
__int64 __fastcall terak_read(__int64 a1, _WORD *a2)
{
__int64 result; // rax

if ( a1 == 63102 )
{
fwrite("Reading serial num\n", 1uLL, 0x13uLL, stderr);
goto LABEL_5;
}
result = 2LL;
if ( (unsigned __int64)(a1 - 62976) <= 0x7F )
{
LABEL_5:
*a2 = *(_WORD *)((char *)*(&pagemap + 3) + (a1 & 0x3FFE));
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, _WORD *a2)
{
__int64 result; // rax

if ( a1 == 63102 )
{
fwrite("Reading serial num\n", 1uLL, 0x13uLL, stderr);
goto LABEL_5;
}
result = 2LL;
if ( (unsigned __int64)(a1 - 62976) <= 0x7F )
{
LABEL_5:
*a2 = *(_WORD *)((char *)*(&pagemap + 3) + (a1 & 0x3FFE));
result = 0LL;
}
return result;
}

original_code:
int
terak_read(c_addr addr, d_word *word )
{
 if (addr == 0173176) fprintf(stderr, "Reading serial num\n");
 if (addr >= 0173000 && addr < 0173200) {
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
 }
 return 2;
}

canonical_original_code:
int terak_read(unsigned long addr, unsigned short *word)
{
 if (addr == 0173176) fprintf(stderr, "Reading serial num\n");
 if (addr >= 0173000 && addr < 0173200) {
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
 }
 return 2;
}


decompiled_code:
__int64 __fastcall q_err(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

original_code:
int q_err(c_addr x, d_word y)
{
 fprintf(stderr, gettext ("Writing to ROM addr %06o:"), x);
 return 2;
}

canonical_original_code:
int q_err(unsigned long x, unsigned short y)
{
 fprintf(stderr, gettext ("Writing to ROM addr %06o:"), x);
 return 2;
}

variable_types:
x
unsigned long

variable_types:
y
unsigned short


decompiled_code:
__int64 __fastcall q_errb(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing byte to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing byte to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

original_code:
int q_errb(c_addr x, d_byte y)
{
 fprintf(stderr, gettext ("Writing byte to ROM addr %06o:"), x);
 return 2;
}

canonical_original_code:
int q_errb(unsigned long x, unsigned char y)
{
 fprintf(stderr, gettext ("Writing byte to ROM addr %06o:"), x);
 return 2;
}

variable_types:
y
unsigned char


decompiled_code:
__m128i *plug_printer()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_printer);
result[1] = _mm_load_si128((const __m128i *)&q_printer + 1);
result[2] = _mm_load_si128((const __m128i *)&q_printer + 2);
return result;
}

canonical_decompiled_code:
__m128i *func13()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_printer);
result[1] = _mm_load_si128((const __m128i *)&q_printer + 1);
result[2] = _mm_load_si128((const __m128i *)&q_printer + 2);
return result;
}

original_code: void plug_printer() { qmap[0] = q_printer; }


decompiled_code:
__m128i *plug_mouse()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_mouse);
result[1] = _mm_load_si128((const __m128i *)&q_mouse + 1);
result[2] = _mm_load_si128((const __m128i *)&q_mouse + 2);
return result;
}

canonical_decompiled_code:
__m128i *func14()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_mouse);
result[1] = _mm_load_si128((const __m128i *)&q_mouse + 1);
result[2] = _mm_load_si128((const __m128i *)&q_mouse + 2);
return result;
}

original_code: void plug_mouse() { qmap[0] = q_mouse; }


decompiled_code:
__m128i *plug_covox()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_covox);
result[1] = _mm_load_si128((const __m128i *)&q_covox + 1);
result[2] = _mm_load_si128((const __m128i *)&q_covox + 2);
return result;
}

canonical_decompiled_code:
__m128i *func15()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_covox);
result[1] = _mm_load_si128((const __m128i *)&q_covox + 1);
result[2] = _mm_load_si128((const __m128i *)&q_covox + 2);
return result;
}

original_code: void plug_covox() { qmap[0] = q_covox; }


decompiled_code:
__m128i *plug_synth()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_synth);
result[1] = _mm_load_si128((const __m128i *)&q_synth + 1);
result[2] = _mm_load_si128((const __m128i *)&q_synth + 2);
return result;
}

canonical_decompiled_code:
__m128i *func16()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_synth);
result[1] = _mm_load_si128((const __m128i *)&q_synth + 1);
result[2] = _mm_load_si128((const __m128i *)&q_synth + 2);
return result;
}

original_code: void plug_synth() { qmap[0] = q_synth; }


decompiled_code:
__m128i *plug_bkplip()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_bkplip);
result[1] = _mm_load_si128((const __m128i *)&q_bkplip + 1);
result[2] = _mm_load_si128((const __m128i *)&q_bkplip + 2);
return result;
}

canonical_decompiled_code:
__m128i *func17()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_bkplip);
result[1] = _mm_load_si128((const __m128i *)&q_bkplip + 1);
result[2] = _mm_load_si128((const __m128i *)&q_bkplip + 2);
return result;
}

original_code: void plug_bkplip() { qmap[0] = q_bkplip; }


decompiled_code:
void __fastcall pagereg_write(unsigned __int16 a1)
{
__int16 v1; // dx
__int16 v2; // ax
unsigned __int16 v3; // di
char *v4; // rdx
unsigned __int64 v5; // rax

if ( oldmap != a1 )
{
oldmap = a1;
v1 = a1 >> 12;
v2 = HIBYTE(a1);
v3 = a1 & 0x1B;
v4 = (char *)&ram + 0x4000 * (unsigned __int64)(v1 & 7);
v5 = (unsigned __int64)&ram + 0x4000 * (unsigned __int64)(v2 & 7);
*(&pagemap + 1) = v4;
*(&pagemap + 2) = (void *)v5;
switch ( v3 )
{
case 0u:
pdp_ram_map = (void *)((unsigned __int64)pdp_ram_map | 0xFF0000);
pdp_mem_map |= 0xFF0000uLL;
video_map[2] = 0;
if ( v4 != (char *)&ram + 0x4000 )
goto LABEL_6;
goto LABEL_11;
case 1u:
pdp_mem_map |= 0xFF0000uLL;
*(&pagemap + 2) = &rom;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
v5 = (unsigned __int64)&rom;
break;
case 2u:
pdp_mem_map |= 0xFF0000uLL;
*(&pagemap + 2) = (char *)&rom + 0x4000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
v5 = (unsigned int)((_DWORD)&rom + 0x4000);
break;
case 8u:
*(&pagemap + 2) = (char *)&rom + 0x8000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
v5 = (unsigned int)((_DWORD)&rom + 0x8000);
break;
case 0x10u:
*(&pagemap + 2) = (char *)&rom + 49152;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
v5 = (unsigned int)((_DWORD)&rom + 49152);
break;
default:
fprintf(stderr, "Bad ROM map %o\n", v3);
v4 = (char *)*(&pagemap + 1);
v5 = (unsigned __int64)*(&pagemap + 2);
break;
}
video_map[2] = 0;
if ( v4 == (char *)&ram + 0x4000 )
{
LABEL_11:
video_map[1] = 1;
if ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&ram + 0x4000) )
{
LABEL_7:
if ( (_UNKNOWN *)v5 == (_UNKNOWN *)((char *)&ram + 114688) )
video_map[2] = 2;
return;
}
}
else
{
LABEL_6:
video_map[1] = 2 * (v4 == (char *)&ram + 114688);
if ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&ram + 0x4000) )
goto LABEL_7;
}
video_map[2] = 1;
}
}

canonical_decompiled_code:
void __fastcall func19(unsigned __int16 a1)
{
__int16 v1; // dx
__int16 v2; // ax
unsigned __int16 v3; // di
char *v4; // rdx
unsigned __int64 v5; // rax

if ( oldmap != a1 )
{
oldmap = a1;
v1 = a1 >> 12;
v2 = HIBYTE(a1);
v3 = a1 & 0x1B;
v4 = (char *)&ram + 0x4000 * (unsigned __int64)(v1 & 7);
v5 = (unsigned __int64)&ram + 0x4000 * (unsigned __int64)(v2 & 7);
*(&pagemap + 1) = v4;
*(&pagemap + 2) = (void *)v5;
switch ( v3 )
{
case 0u:
pdp_ram_map = (void *)((unsigned __int64)pdp_ram_map | 0xFF0000);
pdp_mem_map |= 0xFF0000uLL;
video_map[2] = 0;
if ( v4 != (char *)&ram + 0x4000 )
goto LABEL_6;
goto LABEL_11;
case 1u:
pdp_mem_map |= 0xFF0000uLL;
*(&pagemap + 2) = &rom;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
v5 = (unsigned __int64)&rom;
break;
case 2u:
pdp_mem_map |= 0xFF0000uLL;
*(&pagemap + 2) = (char *)&rom + 0x4000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
v5 = (unsigned int)((_DWORD)&rom + 0x4000);
break;
case 8u:
*(&pagemap + 2) = (char *)&rom + 0x8000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
v5 = (unsigned int)((_DWORD)&rom + 0x8000);
break;
case 0x10u:
*(&pagemap + 2) = (char *)&rom + 49152;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
v5 = (unsigned int)((_DWORD)&rom + 49152);
break;
default:
fprintf(stderr, "Bad ROM map %o\n", v3);
v4 = (char *)*(&pagemap + 1);
v5 = (unsigned __int64)*(&pagemap + 2);
break;
}
video_map[2] = 0;
if ( v4 == (char *)&ram + 0x4000 )
{
LABEL_11:
video_map[1] = 1;
if ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&ram + 0x4000) )
{
LABEL_7:
if ( (_UNKNOWN *)v5 == (_UNKNOWN *)((char *)&ram + 114688) )
video_map[2] = 2;
return;
}
}
else
{
LABEL_6:
video_map[1] = 2 * (v4 == (char *)&ram + 114688);
if ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&ram + 0x4000) )
goto LABEL_7;
}
video_map[2] = 1;
}
}

original_code:
void pagereg_write(d_word word) {
 if (oldmap == word) return;
 oldmap = word;
 pagemap[1] = ram[(word >> 12) & 7];
 pagemap[2] = ram[(word >> 8) & 7];
 switch (word & 033) {
 case 000:
  pdp_ram_map |= 0x00ff0000;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 001:
  pagemap[2] = rom[0];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 002:
  pagemap[2] = rom[1];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 010:
  pagemap[2] = rom[2];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 case 020:
  pagemap[2] = rom[3];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 default: fprintf(stderr, "Bad ROM map %o\n", word & 033);
 }
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

canonical_original_code:
void pagereg_write(unsigned short word) {
 if (oldmap == word) return;
 oldmap = word;
 pagemap[1] = ram[(word >> 12) & 7];
 pagemap[2] = ram[(word >> 8) & 7];
 switch (word & 033) {
 case 000:
  pdp_ram_map |= 0x00ff0000;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 001:
  pagemap[2] = rom[0];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 002:
  pagemap[2] = rom[1];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 010:
  pagemap[2] = rom[2];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 case 020:
  pagemap[2] = rom[3];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 default: fprintf(stderr, "Bad ROM map %o\n", word & 033);
 }
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

variable_types:
word
unsigned short


decompiled_code:
unsigned __int64 __fastcall pagereg_bwrite(__int16 a1)
{
unsigned __int64 result; // rax
unsigned __int8 v2; // al
char *v3; // rdi

result = (unsigned __int16)oldmap;
if ( HIBYTE(oldmap) != (unsigned __int8)a1 )
{
video_map[2] = 0;
oldmap = (a1 << 8) | (unsigned __int8)result;
v2 = a1;
v3 = (char *)&ram + 0x4000 * (unsigned __int64)(a1 & 7);
*(&pagemap + 2) = v3;
result = (unsigned __int64)&ram + 0x4000 * (unsigned __int64)((v2 >> 4) & 7);
*(&pagemap + 1) = (void *)result;
if ( (_UNKNOWN *)result == (_UNKNOWN *)((char *)&ram + 0x4000) )
{
video_map[1] = 1;
}
else
{
LOBYTE(result) = result == (_QWORD)&ram + 114688;
result = (unsigned int)(2 * result);
video_map[1] = result;
}
if ( v3 == (char *)&ram + 0x4000 )
{
video_map[2] = 1;
}
else if ( v3 == (char *)&ram + 114688 )
{
video_map[2] = 2;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func20(__int16 a1)
{
unsigned __int64 result; // rax
unsigned __int8 v2; // al
char *v3; // rdi

result = (unsigned __int16)oldmap;
if ( HIBYTE(oldmap) != (unsigned __int8)a1 )
{
video_map[2] = 0;
oldmap = (a1 << 8) | (unsigned __int8)result;
v2 = a1;
v3 = (char *)&ram + 0x4000 * (unsigned __int64)(a1 & 7);
*(&pagemap + 2) = v3;
result = (unsigned __int64)&ram + 0x4000 * (unsigned __int64)((v2 >> 4) & 7);
*(&pagemap + 1) = (void *)result;
if ( (_UNKNOWN *)result == (_UNKNOWN *)((char *)&ram + 0x4000) )
{
video_map[1] = 1;
}
else
{
LOBYTE(result) = result == (_QWORD)&ram + 114688;
result = (unsigned int)(2 * result);
video_map[1] = result;
}
if ( v3 == (char *)&ram + 0x4000 )
{
video_map[2] = 1;
}
else if ( v3 == (char *)&ram + 114688 )
{
video_map[2] = 2;
}
}
return result;
}

original_code:
void pagereg_bwrite(d_byte byte) {
 if (oldmap >> 8 == byte) return;
 oldmap = oldmap & 0xff | byte << 8;
 pagemap[1] = ram[(byte >> 4) & 7];
 pagemap[2] = ram[byte & 7];
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

canonical_original_code:
void pagereg_bwrite(unsigned char byte) {
 if (oldmap >> 8 == byte) return;
 oldmap = oldmap & 0xff | byte << 8;
 pagemap[1] = ram[(byte >> 4) & 7];
 pagemap[2] = ram[byte & 7];
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

variable_types:
byte
unsigned char


calc

original_code:
__inline__ static e_int16 calc(PSG *psg)
{
  int i,noise ;
  e_uint32 incr ;
  e_int16 mix = 0 ;
  psg->base_count += base_incr ;
  incr = (psg->base_count >> 24) ;
  psg->base_count &= (1 << 24) - 1 ;
 if(psg->env_enable)
  {
    psg->env_count += incr ;
  if(psg->env_count&0x10000)
    {
   psg->env_ptr = (psg->env_ptr + 1)&31 ;
   if(psg->env_ptr == 0)
      {
    psg->env_enable = psg->env_hold ;
    psg->env_reverse = psg->env_reverse^psg->env_alt ;
   }
    psg->env_count -= psg->env_freq ;
  }
 }
  psg->noise_count += incr ;
  if(psg->noise_count&0x40)
  {
  if(psg->noise_seed&1) psg->noise_seed^= 0x24000 ;
    psg->noise_seed >>= 1 ;
  psg->noise_count -= psg->noise_freq ;
 }
 noise = psg->noise_seed & 1 ;
 for(i=0;i<3;i++)
  {
    psg->count[i] += incr ;
  if(psg->count[i]&0x1000)
    {
       if(psg->freq[i]>1)
       {
         psg->edge[i] = !psg->edge[i] ;
         psg->count[i] -= psg->freq[i] ;
       }
       else
       {
         psg->edge[i] = 1 ;
       }
    }
    if(psg->mute[i]) continue ;
    if((psg->tmask[i]||psg->edge[i])&&(psg->nmask[i]||noise))
    {
   if((psg->volume[i]&32)==0)
        mix += psg->voltbl[psg->volume[i]&31] ;
   else if(psg->env_reverse)
        mix += psg->voltbl[31-psg->env_ptr] ;
   else
        mix += psg->voltbl[psg->env_ptr] ;
  }
    else if(psg->tmask[i]&&psg->nmask[i])
    {
      mix += psg->voltbl[psg->volume[i]&31] ;
    }
 }
 return mix ;
}

canonical_original_code:
__inline__ static short calc(struct PSG *psg)
{
  int i; int noise;
  unsigned int incr;
  short mix = 0;
  psg->base_count += base_incr ;
  incr = (psg->base_count >> 24) ;
  psg->base_count &= (1 << 24) - 1 ;
 if(psg->env_enable)
  {
    psg->env_count += incr ;
  if(psg->env_count&0x10000)
    {
   psg->env_ptr = (psg->env_ptr + 1)&31 ;
   if(psg->env_ptr == 0)
      {
    psg->env_enable = psg->env_hold ;
    psg->env_reverse = psg->env_reverse^psg->env_alt ;
   }
    psg->env_count -= psg->env_freq ;
  }
 }
  psg->noise_count += incr ;
  if(psg->noise_count&0x40)
  {
  if(psg->noise_seed&1) psg->noise_seed^= 0x24000 ;
    psg->noise_seed >>= 1 ;
  psg->noise_count -= psg->noise_freq ;
 }
 noise = psg->noise_seed & 1 ;
 for(i=0;i<3;i++)
  {
    psg->count[i] += incr ;
  if(psg->count[i]&0x1000)
    {
       if(psg->freq[i]>1)
       {
         psg->edge[i] = !psg->edge[i] ;
         psg->count[i] -= psg->freq[i] ;
       }
       else
       {
         psg->edge[i] = 1 ;
       }
    }
    if(psg->mute[i]) continue ;
    if((psg->tmask[i]||psg->edge[i])&&(psg->nmask[i]||noise))
    {
   if((psg->volume[i]&32)==0)
        mix += psg->voltbl[psg->volume[i]&31] ;
   else if(psg->env_reverse)
        mix += psg->voltbl[31-psg->env_ptr] ;
   else
        mix += psg->voltbl[psg->env_ptr] ;
  }
    else if(psg->tmask[i]&&psg->nmask[i])
    {
      mix += psg->voltbl[psg->volume[i]&31] ;
    }
 }
 return mix ;
}

variable_types: struct PSG { unsigned int *voltbl; unsigned char reg[32]; int out; unsigned int count[3]; unsigned int volume[3]; unsigned int freq[3]; unsigned int edge[3]; unsigned int tmask[3]; unsigned int nmask[3]; unsigned int mute[3]; unsigned int base_count; unsigned int env_volume; unsigned int env_ptr; unsigned int env_enable; unsigned int env_reverse; unsigned int env_hold; unsigned int env_alt; unsigned int env_freq; unsigned int env_count; unsigned int noise_seed; unsigned int noise_count; unsigned int noise_freq; unsigned int realstep; unsigned int psgtime; unsigned int psgstep; unsigned int adr; } *

variable_types: i int

variable_types: noise int

variable_types:
incr
unsigned int

variable_types: mix short

return_type: short


decompiled_code:
void __fastcall PSG_set_quality(int a1)
{
if ( a1 )
{
base_incr = 0x1000000;
LABEL_3:
HQ = a1;
return;
}
if ( !rate )
goto LABEL_3;
HQ = 0;
base_incr = (int)((double)clk * 16777216.0 / (double)(16 * rate));
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 )
{
base_incr = 0x1000000;
LABEL_3:
HQ = a1;
return;
}
if ( !rate )
goto LABEL_3;
HQ = 0;
base_incr = (int)((double)clk * 16777216.0 / (double)(16 * rate));
}

original_code:
void PSG_set_quality(e_uint32 q)
{
  if(q)
    base_incr = 1<<24 ;
  else if(rate!=0)
    base_incr = (e_uint32)((double)clk * (1<<24) / (16*rate) ) ;
  HQ = q ;
}

canonical_original_code:
void PSG_set_quality(unsigned int q)
{
  if(q)
    base_incr = 1<<24 ;
  else if(rate!=0)
    base_incr = (e_uint32)((double)clk * (1<<24) / (16*rate) ) ;
  HQ = q ;
}

variable_types:
q
unsigned int


decompiled_code:
void __fastcall PSG_init(int a1, int a2)
{
clk = a1;
rate = a2;
PSG_set_quality(0);
}

canonical_decompiled_code:
void __fastcall func2(int a1, int a2)
{
clk = a1;
rate = a2;
func1(0);
}

original_code:
void PSG_init(e_uint32 c, e_uint32 r)
{
 clk = c ;
 rate = r ;
  PSG_set_quality(0) ;
}

canonical_original_code:
void PSG_init(unsigned int c, unsigned int r)
{
 clk = c ;
 rate = r ;
  PSG_set_quality(0) ;
}

variable_types:
c
unsigned int

variable_types:
r
unsigned int


decompiled_code:
_QWORD *PSG_new()
{
_QWORD *v0; // rax
_QWORD *v1; // r8

v0 = malloc(0xC0uLL);
v1 = v0;
if ( v0 )
PSG_setVolumeMode(v0, 1);
return v1;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *v0; // rax
_QWORD *v1; // r8

v0 = malloc(0xC0uLL);
v1 = v0;
if ( v0 )
func3(v0, 1);
return v1;
}

original_code:
PSG *PSG_new()
{
 PSG *psg ;
 psg = malloc(sizeof(PSG)) ;
 if( psg == ((void *)0) ) return ((void *)0) ;
 PSG_setVolumeMode(psg,1) ;
 return psg ;
}

canonical_original_code:
struct PSG *PSG_new()
{
 struct PSG *psg;
 psg = malloc(sizeof(PSG)) ;
 if( psg == ((void *)0) ) return ((void *)0) ;
 PSG_setVolumeMode(psg,1) ;
 return psg ;
}

return_type: struct PSG { unsigned int *voltbl; unsigned char reg[32]; int out; unsigned int count[3]; unsigned int volume[3]; unsigned int freq[3]; unsigned int edge[3]; unsigned int tmask[3]; unsigned int nmask[3]; unsigned int mute[3]; unsigned int base_count; unsigned int env_volume; unsigned int env_ptr; unsigned int env_enable; unsigned int env_reverse; unsigned int env_hold; unsigned int env_alt; unsigned int env_freq; unsigned int env_count; unsigned int noise_seed; unsigned int noise_count; unsigned int noise_freq; unsigned int realstep; unsigned int psgtime; unsigned int psgstep; unsigned int adr; } *


decompiled_code:
// attributes: thunk
void PSG_delete(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func6(void *ptr)
{
free(ptr);
}

original_code:
void PSG_delete(PSG *psg)
{
 free(psg) ;
}

canonical_original_code:
void PSG_delete(struct PSG *psg)
{
 free(psg) ;
}

user_defined_types: struct PSG { unsigned int *voltbl; unsigned char reg[32]; int out; unsigned int count[3]; unsigned int volume[3]; unsigned int freq[3]; unsigned int edge[3]; unsigned int tmask[3]; unsigned int nmask[3]; unsigned int mute[3]; unsigned int base_count; unsigned int env_volume; unsigned int env_ptr; unsigned int env_enable; unsigned int env_reverse; unsigned int env_hold; unsigned int env_alt; unsigned int env_freq; unsigned int env_count; unsigned int noise_seed; unsigned int noise_count; unsigned int noise_freq; unsigned int realstep; unsigned int psgtime; unsigned int psgstep; unsigned int adr; }


decompiled_code:
void PSG_close()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
void PSG_close()
{
}

canonical_original_code:
void PSG_close()
{
}


decompiled_code:
__int64 __fastcall PSG_readIO(__int64 a1)
{
return *(unsigned __int8 *)(*(unsigned int *)(a1 + 188) + a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned __int8 *)(*(unsigned int *)(a1 + 188) + a1 + 8);
}

original_code:
e_uint8 PSG_readIO(PSG *psg)
{
  return (e_uint8)(psg->reg[psg->adr]) ;
}

canonical_original_code:
unsigned char PSG_readIO(struct PSG *psg)
{
  return (e_uint8)(psg->reg[psg->adr]) ;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall PSG_readReg(__int64 a1, char a2)
{
return *(unsigned __int8 *)((a2 & 0x1F) + a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2)
{
return *(unsigned __int8 *)((a2 & 0x1F) + a1 + 8);
}

original_code:
e_uint8 PSG_readReg(PSG *psg, e_uint32 reg)
{
 return (e_uint8)(psg->reg[reg&0x1f]) ;
}

canonical_original_code:
unsigned char PSG_readReg(struct PSG *psg, unsigned int reg)
{
 return (e_uint8)(psg->reg[reg&0x1f]) ;
}

variable_types:
reg
unsigned int


original_code:
void PSG_writeReg(PSG *psg, e_uint32 reg, e_uint32 val)
{
 int c ;
 if (reg>15) return ;
 psg->reg[reg] = (e_uint8)(val & 0xff) ;
 switch(reg)
  {
 case 0: case 2: case 4:
 case 1: case 3: case 5:
  c = reg >> 1 ;
  psg->freq[c] = ( (psg->reg[c*2+1]&15) << 8 ) + psg->reg[c*2] ;
   break ;
 case 6:
    psg->noise_freq = val==0 ? 1 : ((val & 31) << 1) ;
  break ;
 case 7:
  psg->tmask[0] = (val&1) ;
  psg->tmask[1] = (val&2) ;
  psg->tmask[2] = (val&4) ;
  psg->nmask[0] = (val&8) ;
  psg->nmask[1] = (val&16) ;
  psg->nmask[2] = (val&32) ;
  break ;
 case 8:
 case 9:
 case 10:
  psg->volume[reg-8] = val << 1 ;
    if(val&16) psg->env_enable = 1 ;
    break ;
 case 11:
 case 12:
  psg->env_freq = (psg->reg[12] << 8) + psg->reg[11] ;
  break ;
 case 13:
  val = val&15 ;
  if(val<4) val = 9 ;
    else if(val<8) val = 15 ;
  psg->env_count = 0x10000 - psg->env_freq ;
  psg->env_ptr = 0 ;
  psg->env_hold = (val&1)^1 ;
  psg->env_reverse = ((val>>2)&1)^1 ;
  psg->env_alt = ((val>>1)&1)^(val&1) ;
  break ;
 case 14:
 case 15:
 default:
  break;
 }
 return ;
}

canonical_original_code:
void PSG_writeReg(struct PSG *psg, unsigned int reg, unsigned int val)
{
 int c;
 if (reg>15) return ;
 psg->reg[reg] = (e_uint8)(val & 0xff) ;
 switch(reg)
  {
 case 0: case 2: case 4:
 case 1: case 3: case 5:
  c = reg >> 1 ;
  psg->freq[c] = ( (psg->reg[c*2+1]&15) << 8 ) + psg->reg[c*2] ;
   break ;
 case 6:
    psg->noise_freq = val==0 ? 1 : ((val & 31) << 1) ;
  break ;
 case 7:
  psg->tmask[0] = (val&1) ;
  psg->tmask[1] = (val&2) ;
  psg->tmask[2] = (val&4) ;
  psg->nmask[0] = (val&8) ;
  psg->nmask[1] = (val&16) ;
  psg->nmask[2] = (val&32) ;
  break ;
 case 8:
 case 9:
 case 10:
  psg->volume[reg-8] = val << 1 ;
    if(val&16) psg->env_enable = 1 ;
    break ;
 case 11:
 case 12:
  psg->env_freq = (psg->reg[12] << 8) + psg->reg[11] ;
  break ;
 case 13:
  val = val&15 ;
  if(val<4) val = 9 ;
    else if(val<8) val = 15 ;
  psg->env_count = 0x10000 - psg->env_freq ;
  psg->env_ptr = 0 ;
  psg->env_hold = (val&1)^1 ;
  psg->env_reverse = ((val>>2)&1)^1 ;
  psg->env_alt = ((val>>1)&1)^(val&1) ;
  break ;
 case 14:
 case 15:
 default:
  break;
 }
 return ;
}

variable_types:
val
unsigned int

variable_types: c int


decompiled_code:
void __fastcall PSG_writeIO(__int64 a1, char a2, char a3)
{
if ( (a2 & 1) != 0 )
PSG_writeReg(a1, *(_DWORD *)(a1 + 188), a3);
else
*(_DWORD *)(a1 + 188) = a3 & 0x1F;
}

original_code:
void PSG_writeIO(PSG *psg, e_uint32 adr, e_uint32 val)
{
  if(adr & 1) PSG_writeReg(psg, psg->adr, val) ;
  else psg->adr = val & 0x1f ;
}

canonical_original_code:
void PSG_writeIO(struct PSG *psg, unsigned int adr, unsigned int val)
{
  if(adr & 1) PSG_writeReg(psg, psg->adr, val) ;
  else psg->adr = val & 0x1f ;
}

variable_types:
adr
unsigned int


original_code:
e_int16 PSG_calc(PSG *psg)
{
  if(!HQ) return calc(psg) << 4 ;
  while (psg->realstep > psg->psgtime)
  {
    psg->psgtime += psg->psgstep ;
    psg->out += calc(psg) ;
   psg->out >>= 1 ;
  }
  psg->psgtime = psg->psgtime - psg->realstep ;
  return (e_int16)( psg->out << 4 ) ;
}

canonical_original_code:
short PSG_calc(struct PSG *psg)
{
  if(!HQ) return calc(psg) << 4 ;
  while (psg->realstep > psg->psgtime)
  {
    psg->psgtime += psg->psgstep ;
    psg->out += calc(psg) ;
   psg->out >>= 1 ;
  }
  psg->psgtime = psg->psgtime - psg->realstep ;
  return (e_int16)( psg->out << 4 ) ;
}


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

result = mouseflag;
if ( mouseflag == 1 )
{
mouse_up = 1;
mouse_right = 2;
mouse_down = 4;
mouse_left = 8;
mouse_but0 = 16;
mouse_strobe = 8;
}
else if ( mouseflag == 2 )
{
result = 256LL;
mouse_up = 256;
mouse_right = 512;
mouse_down = 1024;
mouse_left = 2048;
mouse_but0 = 4096;
mouse_strobe = 0x8000;
}
return result;
}

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

result = mouseflag;
if ( mouseflag == 1 )
{
mouse_up = 1;
mouse_right = 2;
mouse_down = 4;
mouse_left = 8;
mouse_but0 = 16;
mouse_strobe = 8;
}
else if ( mouseflag == 2 )
{
result = 256LL;
mouse_up = 256;
mouse_right = 512;
mouse_down = 1024;
mouse_left = 2048;
mouse_but0 = 4096;
mouse_strobe = 0x8000;
}
return result;
}

original_code:
int
mouse_init() {
 switch (mouseflag) {
 case 1:
  mouse_up = 1;
  mouse_right = 2;
  mouse_down = 4;
  mouse_left = 8;
  mouse_but0 = 16;
  mouse_strobe = 8;
  break;
 case 2:
  mouse_up = 0x100;
  mouse_right = 0x200;
  mouse_down = 0x400;
  mouse_left = 0x800;
  mouse_but0 = 0x1000;
  mouse_strobe = 0x8000;
  break;
 }
}


decompiled_code:
__int64 __fastcall mouse_read(__int64 a1, _WORD *a2)
{
__int16 v2; // ax
int v3; // edx
bool v4; // cc
__int16 v5; // dx
__int16 v6; // ax

v2 = mouse_button_state;
v3 = relx;
*a2 = mouse_button_state;
v4 = v3 <= 0;
if ( v3 )
{
v5 = mouse_right;
if ( v4 )
v5 = mouse_left;
*a2 = v5 | v2;
}
if ( rely )
{
v6 = mouse_down;
if ( rely <= 0 )
v6 = mouse_up;
*a2 |= v6;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _WORD *a2)
{
__int16 v2; // ax
int v3; // edx
bool v4; // cc
__int16 v5; // dx
__int16 v6; // ax

v2 = mouse_button_state;
v3 = relx;
*a2 = mouse_button_state;
v4 = v3 <= 0;
if ( v3 )
{
v5 = mouse_right;
if ( v4 )
v5 = mouse_left;
*a2 = v5 | v2;
}
if ( rely )
{
v6 = mouse_down;
if ( rely <= 0 )
v6 = mouse_up;
*a2 |= v6;
}
return 0LL;
}

original_code:
int mouse_read(c_addr addr, d_word *word) {
 *word = mouse_button_state;
 if (relx) {
  *word |= relx > 0 ? mouse_right : mouse_left;
 }
 if (rely) {
  *word |= rely > 0 ? mouse_down : mouse_up;
 }
 return 0;
}

canonical_original_code:
int mouse_read(unsigned long addr, unsigned short *word) {
 *word = mouse_button_state;
 if (relx) {
  *word |= relx > 0 ? mouse_right : mouse_left;
 }
 if (rely) {
  *word |= rely > 0 ? mouse_down : mouse_up;
 }
 return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short *


decompiled_code:
__int64 __fastcall mouse_write(__int64 a1, unsigned __int16 a2)
{
if ( (a2 & (unsigned __int16)mouse_strobe) == 0 )
{
if ( relx )
relx /= 2;
if ( rely )
rely /= 2;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int16 a2)
{
if ( (a2 & (unsigned __int16)mouse_strobe) == 0 )
{
if ( relx )
relx /= 2;
if ( rely )
rely /= 2;
}
return 0LL;
}

original_code:
int mouse_write(c_addr addr, d_word word) {
 if (word & mouse_strobe)
  return 0;
 if (relx) {
  relx = relx > 0 ? relx/2 : -(-relx/2);
 }
 if (rely) {
  rely = rely > 0 ? rely/2 : -(-rely/2);
 }
 return 0;
}

canonical_original_code:
int mouse_write(unsigned long addr, unsigned short word) {
 if (word & mouse_strobe)
  return 0;
 if (relx) {
  relx = relx > 0 ? relx/2 : -(-relx/2);
 }
 if (rely) {
  rely = rely > 0 ? rely/2 : -(-rely/2);
 }
 return 0;
}


decompiled_code:
__int64 serial_read()
{
unsigned int v0; // er12
char v1; // cl
int v2; // eax
char v3; // dl
int v4; // eax
__int64 result; // rax
int v6; // eax
FILE *v7; // rax

v0 = 144;
if ( !data_ptr || !ready_out )
return v0;
v1 = bitnum;
if ( bitnum == -1 )
{
bitnum = 0;
return 128;
}
if ( bitnum == 8 )
return v0;
v2 = *(unsigned __int8 *)data_ptr;
v3 = ++bitnum;
v4 = (v2 >> v1) & 1;
v0 = v4 == 0 ? 0xFFFFFFF0 : 0;
LOWORD(v0) = v4 == 0 ? 128 : 144;
if ( !ser_in )
{
v7 = fopen("serial.in", "r");
v3 = bitnum;
ser_in = v7;
}
if ( v3 != 8 )
return v0;
v6 = getc(ser_in);
if ( v6 == -1 )
data_ptr = 0LL;
retbyte = v6;
result = v0;
bitnum = -1;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
char v1; // cl
int v2; // eax
char v3; // dl
int v4; // eax
__int64 result; // rax
int v6; // eax
FILE *v7; // rax

v0 = 144;
if ( !data_ptr || !ready_out )
return v0;
v1 = bitnum;
if ( bitnum == -1 )
{
bitnum = 0;
return 128;
}
if ( bitnum == 8 )
return v0;
v2 = *(unsigned __int8 *)data_ptr;
v3 = ++bitnum;
v4 = (v2 >> v1) & 1;
v0 = v4 == 0 ? 0xFFFFFFF0 : 0;
LOWORD(v0) = v4 == 0 ? 128 : 144;
if ( !ser_in )
{
v7 = fopen("serial.in", "r");
v3 = bitnum;
ser_in = v7;
}
if ( v3 != 8 )
return v0;
v6 = getc(ser_in);
if ( v6 == -1 )
data_ptr = 0LL;
retbyte = v6;
result = v0;
bitnum = -1;
return result;
}

original_code:
d_word
serial_read() {
 unsigned char ret = 0200;
 if (!data_ptr || !ready_out) {
  return 0200 | 020;
 }
 if (bitnum == -1) {
  bitnum = 0;
  return 0200 & ~020;
 }
 if (bitnum == 8) {
  return 0200 | 020;
 }
 ret |= *data_ptr & (1<<bitnum++) ? 020 : 0;
 if (!ser_in) {
  ser_in = fopen("serial.in", "r");
 }
 if (bitnum == 8) {
  int c = getc(ser_in);
  if (c == (-1)) data_ptr = 0;
  retbyte = c;
  bitnum = -1;
 }
 return ret;
}

canonical_original_code:
unsigned short serial_read() {
 unsigned char ret = 0200;
 if (!data_ptr || !ready_out) {
  return 0200 | 020;
 }
 if (bitnum == -1) {
  bitnum = 0;
  return 0200 & ~020;
 }
 if (bitnum == 8) {
  return 0200 | 020;
 }
 ret |= *data_ptr & (1<<bitnum++) ? 020 : 0;
 if (!ser_in) {
  ser_in = fopen("serial.in", "r");
 }
 if (bitnum == 8) {
  int c = getc(ser_in);
  if (c == (-1)) data_ptr = 0;
  retbyte = c;
  bitnum = -1;
 }
 return ret;
}

variable_types:
ret
unsigned char

return_type: unsigned short


decompiled_code:
size_t __fastcall serial_write(char a1)
{
size_t result; // rax
char v2; // cl

result = (unsigned __int8)ready_out;
if ( ready_out != (a1 & 0x20) )
{
ready_out = a1 & 0x20;
result = a1 & 0x20;
}
if ( (_BYTE)result )
{
if ( !data_ptr )
{
retbyte = -68;
data_ptr = (__int64)&retbyte;
}
v2 = curbit;
bitnum = -1;
if ( curbit >= 0 )
goto LABEL_5;
}
else
{
v2 = curbit;
bitnum = 8;
if ( curbit >= 0 )
{
LABEL_5:
result = (unsigned __int8)((a1 & 0x10) != 0) << v2;
LOBYTE(result) = forming | result;
curbit = v2 + 1;
forming = result;
if ( v2 == 7 )
{
curbit = -1;
if ( (_BYTE)result != 0xBC )
{
data_ptr = 0LL;
result = fwrite("Serial line probe failed\n", 1uLL, 0x19uLL, stderr);
}
}
goto LABEL_6;
}
}
if ( !ser_out || (a1 & 0x10) != 0 )
{
LABEL_6:
ser_out = (a1 & 0x10) != 0;
return result;
}
ser_out = (a1 & 0x10) != 0;
curbit = 0;
return result;
}

canonical_decompiled_code:
size_t __fastcall func1(char a1)
{
size_t result; // rax
char v2; // cl

result = (unsigned __int8)ready_out;
if ( ready_out != (a1 & 0x20) )
{
ready_out = a1 & 0x20;
result = a1 & 0x20;
}
if ( (_BYTE)result )
{
if ( !data_ptr )
{
retbyte = -68;
data_ptr = (__int64)&retbyte;
}
v2 = curbit;
bitnum = -1;
if ( curbit >= 0 )
goto LABEL_5;
}
else
{
v2 = curbit;
bitnum = 8;
if ( curbit >= 0 )
{
LABEL_5:
result = (unsigned __int8)((a1 & 0x10) != 0) << v2;
LOBYTE(result) = forming | result;
curbit = v2 + 1;
forming = result;
if ( v2 == 7 )
{
curbit = -1;
if ( (_BYTE)result != 0xBC )
{
data_ptr = 0LL;
result = fwrite("Serial line probe failed\n", 1uLL, 0x19uLL, stderr);
}
}
goto LABEL_6;
}
}
if ( !ser_out || (a1 & 0x10) != 0 )
{
LABEL_6:
ser_out = (a1 & 0x10) != 0;
return result;
}
ser_out = (a1 & 0x10) != 0;
curbit = 0;
return result;
}

original_code:
void
serial_write(d_word w) {
 unsigned char cur = (w & 020) != 0;
 if (ready_out != (w & 040)) {
  ready_out = w & 040;
 }
 if (!ready_out) {
  bitnum = 8;
 } else {
     if (!data_ptr) {
  retbyte = 0xBC;
  data_ptr = &retbyte;
     }
     bitnum = -1;
 }
 if (curbit >= 0) {
  forming |= cur << curbit++;
  if (curbit == 8) {
   curbit = -1;
   if (forming != 0xBC) {
    data_ptr = 0;
    fprintf(stderr, "Serial line probe failed\n");
   }
  }
 } else if (ser_out && !cur) {
  curbit = 0;
 }
 ser_out = cur;
}

canonical_original_code:
void serial_write(unsigned short w) {
 unsigned char cur = (w & 020) != 0;
 if (ready_out != (w & 040)) {
  ready_out = w & 040;
 }
 if (!ready_out) {
  bitnum = 8;
 } else {
     if (!data_ptr) {
  retbyte = 0xBC;
  data_ptr = &retbyte;
     }
     bitnum = -1;
 }
 if (curbit >= 0) {
  forming |= cur << curbit++;
  if (curbit == 8) {
   curbit = -1;
   if (forming != 0xBC) {
    data_ptr = 0;
    fprintf(stderr, "Serial line probe failed\n");
   }
  }
 } else if (ser_out && !cur) {
  curbit = 0;
 }
 ser_out = cur;
}

variable_types:
w
unsigned short

variable_types:
cur
unsigned char


callback

decompiled_code:
__int64 __fastcall callback(__int64 a1, void *a2, int a3)
{
__int64 result; // rax
int v5; // ebx

result = SDL_SemValue(sem);
if ( (_DWORD)result != 2 )
{
v5 = cur_out_buf_1 + 1;
memcpy(a2, (const void *)sound_buf[2 * cur_out_buf_1], a3);
cur_out_buf_1 = v5 % 2;
result = SDL_SemPost(sem);
}
return result;
}

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

result = SDL_SemValue(sem);
if ( (_DWORD)result != 2 )
{
v5 = cur_out_buf_1 + 1;
memcpy(a2, (const void *)sound_buf[2 * cur_out_buf_1], a3);
cur_out_buf_1 = v5 % 2;
result = SDL_SemPost(sem);
}
return result;
}

original_code:
void callback(void * dummy, Uint8 * outbuf, int len)
{
 static int cur_out_buf;
 if (SDL_SemValue(sem) == 2) {
  return;
 }
 memcpy(outbuf, sound_buf[cur_out_buf].buf, len);
 cur_out_buf = (cur_out_buf + 1) % 2;
 SDL_SemPost(sem);
}

canonical_original_code:
void callback(void *dummy, unsigned char *outbuf, int len)
{
 static int cur_out_buf;
 if (SDL_SemValue(sem) == 2) {
  return;
 }
 memcpy(outbuf, sound_buf[cur_out_buf].buf, len);
 cur_out_buf = (cur_out_buf + 1) % 2;
 SDL_SemPost(sem);
}

variable_types:
dummy
void *

variable_types:
outbuf
unsigned char *

variable_types: len int


decompiled_code:
void sound_finish(void)
{
SDL_PauseAudio(1LL);
SDL_DestroySemaphore(sem);
}

canonical_decompiled_code:
void func1(void)
{
SDL_PauseAudio(1LL);
SDL_DestroySemaphore(sem);
}

original_code:
void sound_finish() {
 SDL_PauseAudio(1);
 SDL_DestroySemaphore(sem);
}


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

if ( psg )
result = (__int16)PSG_calc();
else
result = 0;
return result;
}

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

if ( psg )
result = (__int16)PSG_calc();
else
result = 0;
return result;
}

original_code:
int
synth_next() {
 int a = psg ? PSG_calc(psg) : 0;
 return a;
}

variable_types: a int


decompiled_code:
void boot_init()
{
if ( !boot_done_0 )
{
boot_done_0 = 1;
if ( terak )
{
load_rom(0xF600u, "TERAK.ROM", 0x80u, 0x80u);
}
else if ( bkmodel )
{
load_rom11((__int64)&system_rom, 0, bos11rom, 0x2000);
load_rom11((__int64)&system_rom, 0x2000, diskrom, 4096);
load_rom11((__int64)&rom, 0, basic11arom, 0x4000);
load_rom11((unsigned int)((_DWORD)&rom + 0x4000), 0, basic11brom, 0x2000);
load_rom11((unsigned int)((_DWORD)&rom + 0x4000), 0x2000, bos11extrom, 0x2000);
}
else
{
load_rom(0x8000u, rompath10, 0x2000u, 0x2000u);
load_rom(0xA000u, rompath12, 0x5F80u, 0x6000u);
load_rom(0xE000u, rompath16, 0x1000u, 0x1000u);
}
}
}

canonical_decompiled_code:
void func2()
{
if ( !boot_done_0 )
{
boot_done_0 = 1;
if ( terak )
{
func0(0xF600u, "TERAK.ROM", 0x80u, 0x80u);
}
else if ( bkmodel )
{
func1((__int64)&system_rom, 0, bos11rom, 0x2000);
func1((__int64)&system_rom, 0x2000, diskrom, 4096);
func1((__int64)&rom, 0, basic11arom, 0x4000);
func1((unsigned int)((_DWORD)&rom + 0x4000), 0, basic11brom, 0x2000);
func1((unsigned int)((_DWORD)&rom + 0x4000), 0x2000, bos11extrom, 0x2000);
}
else
{
func0(0x8000u, rompath10, 0x2000u, 0x2000u);
func0(0xA000u, rompath12, 0x5F80u, 0x6000u);
func0(0xE000u, rompath16, 0x1000u, 0x1000u);
}
}
}

original_code:
int
boot_init()
{
 static unsigned char boot_done = 0;
 if (boot_done) return;
 boot_done = 1;
 if (terak) {
  load_rom(0173000, "TERAK.ROM", 128, 128);
  return;
 }
 if (bkmodel != 0) {
  load_rom11(system_rom, 0, bos11rom, 8192);
  load_rom11(system_rom, 8192, diskrom, 4096);
  load_rom11(rom[0], 0, basic11arom, 16384);
  load_rom11(rom[1], 0, basic11brom, 8192);
  load_rom11(rom[1], 8192, bos11extrom, 8192);
  return;
 }
 load_rom(0100000, rompath10, 8192, 8192);
        load_rom(0120000, rompath12, 24448, 24576);
        load_rom(0160000, rompath16, 4096, 4096);
}


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

result = (unsigned __int16)tty_scroll;
tty_reg = 0;
tty_data = 0;
tty_pending_int = 0;
tty_scroll = 728;
timer_intr_enabled = 0;
if ( (_WORD)result != 728 )
scr_dirty = 1;
return result;
}

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

result = (unsigned __int16)tty_scroll;
tty_reg = 0;
tty_data = 0;
tty_pending_int = 0;
tty_scroll = 728;
timer_intr_enabled = 0;
if ( (_WORD)result != 728 )
scr_dirty = 1;
return result;
}

original_code:
int
tty_init()
{
 int i;
 unsigned short old_scroll = tty_scroll;
 tty_reg = 0;
 tty_data = 0;
 tty_pending_int = 0;
 tty_scroll = 01330;
 timer_intr_enabled = 0;
 if (old_scroll != tty_scroll) {
  scr_dirty = 1;
 }
}

variable_types: i int


notice

repo: emguy/ezfdtd

decompiled_code:
size_t notice(const char *a1, unsigned int a2, const char *a3, const char *a4, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a4);
if ( a1 )
fprintf(stderr, "\x1B[31m%s ", a1);
if ( a2 )
fprintf(stderr, "\x1B[31m%d ", a2);
if ( a3 )
fprintf(stderr, "\x1B[31min %s() ", a3);
fwrite("\x1B[31m: \x1B[36m", 1uLL, 0xCuLL, stderr);
vfprintf(stderr, a4, arg);
return fwrite("\x1B[0m\n", 1uLL, 5uLL, stderr);
}

canonical_decompiled_code:
size_t func0(const char *a1, unsigned int a2, const char *a3, const char *a4, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a4);
if ( a1 )
fprintf(stderr, "\x1B[31m%s ", a1);
if ( a2 )
fprintf(stderr, "\x1B[31m%d ", a2);
if ( a3 )
fprintf(stderr, "\x1B[31min %s() ", a3);
fwrite("\x1B[31m: \x1B[36m", 1uLL, 0xCuLL, stderr);
vfprintf(stderr, a4, arg);
return fwrite("\x1B[0m\n", 1uLL, 5uLL, stderr);
}

original_code:
void notice(const char *file, int line, const char *func, const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 if (1)
 {
  if (file) fprintf(stderr, "\x1B[31m" "%s ", file);
  if (line) fprintf(stderr, "\x1B[31m" "%d ", line);
  if (func) fprintf(stderr, "\x1B[31m" "in %s() ", func);
  fprintf(stderr, "\x1B[31m" ": " "\x1B[36m");
 }
 vfprintf(stderr, fmt, ap);
 __builtin_va_end(ap);
 fprintf(stderr, "\x1B[0m" "\n");
}

canonical_original_code:
void notice(char *file, int line, char *func, char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 if (1)
 {
  if (file) fprintf(stderr, "\x1B[31m" "%s ", file);
  if (line) fprintf(stderr, "\x1B[31m" "%d ", line);
  if (func) fprintf(stderr, "\x1B[31m" "in %s() ", func);
  fprintf(stderr, "\x1B[31m" ": " "\x1B[36m");
 }
 vfprintf(stderr, fmt, ap);
 __builtin_va_end(ap);
 fprintf(stderr, "\x1B[0m" "\n");
}

variable_types:
file
char *

variable_types: line int

variable_types:
func
char *

variable_types:
fmt
char *