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

    char *appname = argv[0];
    while (argc > 1) {
        if (!strcmp(argv[1], "-hlt")) {
            hltend = true;
        } else if (!strcmp(argv[1], "-strict")) {
            strict8086 = true;
        } else break;
        --argc;
        ++argv;
    }
    if (argc < 2 || 3 < argc) {
        fprintf(stderr, "usage: %s [-hlt] [-strict] fd1image [fd2image]\n", appname);
        fprintf(stderr, "  -hlt: stop on HLT (for demo)\n");
        fprintf(stderr, "  -strict: deny to execute instructions which are not in 8086 microprocessor\n");
        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, pitc = 0, trial = 4;
    counter = interval;
    nextClock = mclock();
    while (IP || *CS) {
        if (!--counter) {
            int c = mclock();
            if (c <= nextClock && !--trial) {
                int old = interval;
                if ((interval <<= 1) < old) interval = old;
                trial = 4;
            }
            while (c > nextClock) {
                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);
                nextClock += t2 - t1;
                trial = 4;
            }
            counter = interval;
        }
        oldip = IP;
        step(0, NULL);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl %edi, %r15d
callq 0x533c
movq (%rbx), %rax
movq %rax, (%rsp)
leaq 0x1960(%rip), %r12      # 0x68b0
leaq 0x124599(%rip), %r14    # 0x1294f0
leaq 0x124593(%rip), %r13    # 0x1294f1
cmpl $0x2, %r15d
jl 0x520a
movq 0x8(%rbx), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1190
movq %r14, %rcx
testl %eax, %eax
je 0x4f94
movq %rbp, %rdi
leaq 0x192d(%rip), %rsi      # 0x68b5
callq 0x1190
movq %r13, %rcx
testl %eax, %eax
jne 0x4fa0
addq $0x8, %rbx
movb $0x1, (%rcx)
decl %r15d
jmp 0x4f5e
leal -0x4(%r15), %eax
cmpl $-0x2, %eax
jb 0x520a
movl %r15d, %r12d
pushq $0x1
popq %r15
leaq 0x4185(%rip), %r14      # 0x9140
cmpq %r12, %r15
jae 0x4ff9
movq (%rbx,%r15,8), %rsi
movq %r14, %rdi
callq 0x132c
incq %r15
addq $0x18, %r14
testb %al, %al
jne 0x4fbb
movq 0x4002(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq -0x8(%rbx,%r15,8), %rdx
leaq 0x196e(%rip), %rsi      # 0x695b
xorl %eax, %eax
callq 0x11a0
jmp 0x5253
xorl %eax, %eax
pushq $0x1
popq %rcx
leaq 0x1243eb(%rip), %rdx    # 0x1293f0
cmpq $0x100, %rax            # imm = 0x100
je 0x503b
xorl %esi, %esi
movl %ecx, %edi
cmpl $0x100, %edi            # imm = 0x100
jge 0x502b
movl %edi, %r8d
andl %eax, %r8d
cmpl $0x1, %r8d
sbbb $-0x1, %sil
addl %edi, %edi
jmp 0x5011
notb %sil
andb $0x1, %sil
movb %sil, (%rdx,%rax)
incq %rax
jmp 0x5005
pushq $-0x20
popq %rax
leaq 0x12434b(%rip), %rcx    # 0x129390
leaq 0x124354(%rip), %rdx    # 0x1293a0
testq %rax, %rax
je 0x5069
movq %rcx, 0x20(%rdx,%rax)
leaq 0x1(%rcx), %rsi
movq %rsi, 0x40(%rdx,%rax)
addq $0x8, %rax
addq $0x2, %rcx
jmp 0x504c
xorl %eax, %eax
leaq 0x430e(%rip), %rbx      # 0x9380
cmpq $0x20, %rax
je 0x5085
movb %al, (%rbx,%rax,4)
movb $0x0, 0x1(%rbx,%rax,4)
incq %rax
jmp 0x5072
leaq 0x124474(%rip), %rax    # 0x129500
movq %rbx, 0x30(%rax)
andw $0x0, 0x38(%rax)
movq %rbx, 0x20(%rax)
andw $0x0, 0x28(%rax)
movq %rbx, 0x10(%rax)
andw $0x0, 0x18(%rax)
movq %rbx, (%rax)
andw $0x0, 0x8(%rax)
leaq 0x1242ca(%rip), %r14    # 0x129380
movw $0x7c00, (%r14)         # imm = 0x7C00
leaq 0x407d(%rip), %rax      # 0x9140
movq (%rax), %rcx
leaq 0x7c00(%rbx), %rdi
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x11c0
movl $0x1e001e, 0x41a(%rbx)  # imm = 0x1E001E
movl $0x10000, 0x12447a(%rip) # imm = 0x10000
callq 0x5409
movl %eax, 0x124473(%rip)    # 0x12956c
pushq $0x4
popq %r12
movl $0x10000, %eax          # imm = 0x10000
movq %rax, (%rsp)
movl $0x1000, %ebp           # imm = 0x1000
leaq 0x124270(%rip), %r15    # 0x129382
xorl %ebx, %ebx
movl %r12d, %r13d
movzwl (%r14), %eax
testw %ax, %ax
jne 0x5132
leaq 0x1243d9(%rip), %rcx    # 0x129500
cmpw $0x0, 0x18(%rcx)
je 0x51f9
decl 0x124430(%rip)          # 0x129568
jne 0x51e7
callq 0x5409
movl %eax, %r14d
movl 0x12441f(%rip), %r15d   # 0x12956c
cmpl %r15d, %eax
jg 0x5169
decl %r12d
jne 0x5169
movq (%rsp), %rcx
leal (%rcx,%rcx), %eax
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq %rcx, (%rsp)
jmp 0x51c6
cmpl %r15d, %r14d
jle 0x51cb
pushq $0x8
popq %rdi
callq 0x1f98
imull $0x36ee8, %ebx, %ecx    # imm = 0x36EE8
movl %ecx, %eax
cltd
movl $0xfffff000, %esi       # imm = 0xFFFFF000
idivl %esi
movl %eax, %r12d
incl %ebx
movl $0x36ee8, %eax          # imm = 0x36EE8
addl %eax, %ecx
movl %ecx, %eax
cltd
idivl %ebp
movl %eax, %r15d
cmpl %ebp, %ebx
movl $0x0, %eax
cmovel %eax, %ebx
callq 0x529a
testl %eax, %eax
je 0x51b5
pushq $0x9
popq %rdi
callq 0x1f98
addl %r12d, %r15d
addl 0x1243ad(%rip), %r15d   # 0x12956c
movl %r15d, 0x1243a6(%rip)   # 0x12956c
movl %r13d, %r12d
jmp 0x5169
movq (%rsp), %rax
movl %eax, 0x124393(%rip)    # 0x129568
leaq 0x1241a4(%rip), %r14    # 0x129380
movzwl (%r14), %eax
leaq 0x12419b(%rip), %r15    # 0x129382
movw %ax, (%r15)
xorl %edi, %edi
xorl %esi, %esi
callq 0x20b5
jmp 0x5117
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3dcf(%rip), %r14      # 0x8fe0
movq (%r14), %rdi
leaq 0x16a2(%rip), %rsi      # 0x68bd
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x11a0
movq (%r14), %rcx
leaq 0x16bd(%rip), %rdi      # 0x68ed
pushq $0x1f
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1220
movq (%r14), %rcx
leaq 0x16c5(%rip), %rdi      # 0x690d
pushq $0x4d
popq %rsi
movq %rbx, %rdx
callq 0x1220
pushq $0x1
popq %rax
jmp 0x51fb