// File: 7shi[P]8086run/v1/main.cpp
// Function: main
int main(int argc, char *argv[]) {
    inittty();

    if (argc < 2 || 3 < argc) {
        fprintf(stderr, "usage: %s fd1image [fd2image]\n", argv[0]);
        return 1;
    }
    for (int i = 1; i < argc; ++i) {
        if (!disks[i - 1].open(argv[i])) {
            fprintf(stderr, "can not open: %s\n", argv[i]);
            return 1;
        }
    }

    for (int i = 0; i < 256; ++i) {
        int n = 0;
        for (int j = 1; j < 256; j += j) {
            if (i & j) n++;
        }
        ptable[i] = (n & 1) == 0;
    }

    uint16_t tmp = 0x1234;
    uint8_t *p = (uint8_t *) r;
    if (*(uint8_t *) & tmp == 0x34) {
        for (int i = 0; i < 4; ++i) {
            r8[i] = p + i * 2;
            r8[i + 4] = r8[i] + 1;
        }
    } else {
        for (int i = 0; i < 4; ++i) {
            r8[i] = p + i * 2 + 1;
            r8[i + 4] = r8[i] - 1;
        }
    }

    for (int i = 0; i < 0x20; ++i) {
        write16(&mem[i << 2], i);
    }

    ES = CS = SS = DS = 0;
    IP = 0x7c00;
    if (fread(&mem[IP], 1, 512, disks[0].f)); // read MBR
    write16(&mem[0x41a], 0x1e); // keyboard queue head
    write16(&mem[0x41c], 0x1e); // keyboard queue tail
    int interval = 1 << 16, counter = interval, pitc = 0, trial = 4;
    clock_t next = clock();
    while (IP || *CS) {
        if (!--counter) {
            clock_t c = clock();
            if (c <= next && !--trial) {
                int old = interval;
                if ((interval <<= 1) < old) interval = old;
                trial = 4;
            }
            while (c > next) {
                intr(8);
                // 3600/65536 = 225/4096
                int t1 = pitc * 225000 / 4096;
                int t2 = (++pitc) * 225000 / 4096;
                if (pitc == 4096) pitc = 0;
                if (kbhit()) intr(9);
                next += CLOCKS_PER_SEC * (t2 - t1) / 1000;
                trial = 4;
            }
            counter = interval;
        }
        oldip = IP;
        step(0, NULL);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x13e0
cmpl $0x2, -0x8(%rbp)
jl 0x5cab
movl $0x3, %eax
cmpl -0x8(%rbp), %eax
jge 0x5cd6
movq 0x532e(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2a69(%rip), %rsi      # 0x872c
movb $0x0, %al
callq 0x1140
movl $0x1, -0x4(%rbp)
jmp 0x60e2
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x5d4d
movl -0x14(%rbp), %eax
subl $0x1, %eax
cltq
leaq 0x12567c(%rip), %rdi    # 0x12b370
imulq $0x18, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x7390
testb $0x1, %al
jne 0x5d40
movq 0x52c9(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x2a1e(%rip), %rsi      # 0x874b
movb $0x0, %al
callq 0x1140
movl $0x1, -0x4(%rbp)
jmp 0x60e2
jmp 0x5d42
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x5cdd
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x5dbd
movl $0x0, -0x1c(%rbp)
movl $0x1, -0x20(%rbp)
cmpl $0x100, -0x20(%rbp)     # imm = 0x100
jge 0x5d95
movl -0x18(%rbp), %eax
andl -0x20(%rbp), %eax
cmpl $0x0, %eax
je 0x5d88
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5d8a
movl -0x20(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x5d6b
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
sete %dl
movslq -0x18(%rbp), %rcx
andb $0x1, %dl
leaq 0x125481(%rip), %rax    # 0x12b230
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5d54
movw $0x1234, -0x22(%rbp)    # imm = 0x1234
leaq 0x125406(%rip), %rax    # 0x12b1d0
movq %rax, -0x30(%rbp)
movzbl -0x22(%rbp), %eax
cmpl $0x34, %eax
jne 0x5e35
movl $0x0, -0x34(%rbp)
cmpl $0x4, -0x34(%rbp)
jge 0x5e33
movq -0x30(%rbp), %rdx
movl -0x34(%rbp), %eax
shll %eax
cltq
addq %rax, %rdx
movslq -0x34(%rbp), %rcx
leaq 0x1253e3(%rip), %rax    # 0x12b1e0
movq %rdx, (%rax,%rcx,8)
movslq -0x34(%rbp), %rcx
leaq 0x1253d4(%rip), %rax    # 0x12b1e0
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movl -0x34(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rcx
leaq 0x1253bc(%rip), %rax    # 0x12b1e0
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5dde
jmp 0x5e97
movl $0x0, -0x38(%rbp)
cmpl $0x4, -0x38(%rbp)
jge 0x5e95
movq -0x30(%rbp), %rdx
movl -0x38(%rbp), %eax
shll %eax
cltq
addq %rax, %rdx
addq $0x1, %rdx
movslq -0x38(%rbp), %rcx
leaq 0x125381(%rip), %rax    # 0x12b1e0
movq %rdx, (%rax,%rcx,8)
movslq -0x38(%rbp), %rcx
leaq 0x125372(%rip), %rax    # 0x12b1e0
movq (%rax,%rcx,8), %rdx
addq $-0x1, %rdx
movl -0x38(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rcx
leaq 0x12535a(%rip), %rax    # 0x12b1e0
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x5e3c
jmp 0x5e97
movl $0x0, -0x3c(%rbp)
cmpl $0x20, -0x3c(%rbp)
jge 0x5ecc
movl -0x3c(%rbp), %eax
shll $0x2, %eax
cltq
leaq 0x530d(%rip), %rdi      # 0xb1c0
addq %rax, %rdi
movl -0x3c(%rbp), %eax
movzwl %ax, %esi
callq 0x6150
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x5e9e
leaq 0x12545d(%rip), %rdi    # 0x12b330
addq $0x30, %rdi
xorl %esi, %esi
callq 0x6180
leaq 0x12544b(%rip), %rdi    # 0x12b330
addq $0x20, %rdi
movzwl %ax, %esi
callq 0x6180
leaq 0x125438(%rip), %rdi    # 0x12b330
addq $0x10, %rdi
movzwl %ax, %esi
callq 0x6180
leaq 0x125425(%rip), %rdi    # 0x12b330
movzwl %ax, %esi
callq 0x6180
movw $0x7c00, 0x1252a4(%rip) # imm = 0x7C00
movzwl 0x12529d(%rip), %eax    # 0x12b1c0
leaq 0x5296(%rip), %rdi      # 0xb1c0
addq %rax, %rdi
movq 0x12543c(%rip), %rcx    # 0x12b370
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1160
cmpq $0x0, %rax
je 0x5f4b
jmp 0x5f4b
leaq 0x526e(%rip), %rdi      # 0xb1c0
addq $0x41a, %rdi            # imm = 0x41A
movl $0x1e, %esi
callq 0x6150
leaq 0x5256(%rip), %rdi      # 0xb1c0
addq $0x41c, %rdi            # imm = 0x41C
movl $0x1e, %esi
callq 0x6150
movl $0x10000, -0x40(%rbp)   # imm = 0x10000
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x4, -0x4c(%rbp)
callq 0x1060
movq %rax, -0x58(%rbp)
movb $0x1, %al
cmpw $0x0, 0x125217(%rip)    # 0x12b1c0
movb %al, -0x6d(%rbp)
jne 0x5fc8
leaq 0x12537b(%rip), %rdi    # 0x12b330
addq $0x10, %rdi
callq 0x60f0
cmpw $0x0, %ax
setne %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x5fd4
jmp 0x60e2
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jne 0x60c4
callq 0x1060
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x6027
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
jne 0x6027
movl -0x40(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x40(%rbp)
cmpl -0x64(%rbp), %eax
jge 0x6020
movl -0x64(%rbp), %eax
movl %eax, -0x40(%rbp)
movl $0x4, -0x4c(%rbp)
jmp 0x6029
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x60be
movl $0x8, %edi
callq 0x2390
imull $0x36ee8, -0x48(%rbp), %eax # imm = 0x36EE8
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, -0x68(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
imull $0x36ee8, %eax, %eax    # imm = 0x36EE8
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, -0x6c(%rbp)
cmpl $0x1000, -0x48(%rbp)    # imm = 0x1000
jne 0x607d
movl $0x0, -0x48(%rbp)
callq 0x1300
cmpl $0x0, %eax
je 0x6091
movl $0x9, %edi
callq 0x2390
movl -0x6c(%rbp), %eax
subl -0x68(%rbp), %eax
cltq
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x4, -0x4c(%rbp)
jmp 0x6029
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movw 0x1250f5(%rip), %ax     # 0x12b1c0
movw %ax, 0x1250f0(%rip)     # 0x12b1c2
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x2450
jmp 0x5f9f
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)