// File: /workspace/github_cpp_makefile_star2_O3/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
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %r14d
callq 0x71f5
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
cmpl $0x2, %r14d
jl 0x70c3
movq 0x8(%rsp), %rax
leal -0x4(%rax), %r14d
movl $0x1, %r15d
leaq 0x1afe(%rip), %r12      # 0x88f0
leaq 0x1246f7(%rip), %r13    # 0x12b4f0
movq (%rbx,%r15,8), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1190
movq %r13, %rcx
testl %eax, %eax
je 0x6e29
movq %rbp, %rdi
leaq 0x1adc(%rip), %rsi      # 0x88f5
callq 0x1190
leaq 0x1246cc(%rip), %rcx    # 0x12b4f1
testl %eax, %eax
jne 0x6e45
movb $0x1, (%rcx)
incq %r15
leal -0x1(%r14), %eax
addl $0x4, %r14d
cmpl $0x2, %r14d
movl %eax, %r14d
jg 0x6df9
jmp 0x70c3
cmpl $-0x2, %r14d
jb 0x70c3
movl 0x8(%rsp), %r12d
leaq 0x42e5(%rip), %r14      # 0xb140
movq (%rbx,%r15,8), %rsi
movq %r14, %rdi
callq 0x13dc
testb %al, %al
je 0x70e1
incq %r15
addq $0x18, %r14
cmpq %r15, %r12
jne 0x6e5b
xorl %eax, %eax
leaq 0x12456c(%rip), %rcx    # 0x12b3f0
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
andl %eax, %edi
cmpl $0x1, %edi
sbbb $-0x1, %dl
leal (%rsi,%rsi), %edi
cmpl $0x80, %esi
movl %edi, %esi
jl 0x6e8b
notb %dl
andb $0x1, %dl
movb %dl, (%rcx,%rax)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x6e84
movdqa 0x1a13(%rip), %xmm0     # 0x88d0
movq $-0x4, %rax
movq 0x4114(%rip), %xmm1     # 0xafe0
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
leaq 0x1244c8(%rip), %rcx    # 0x12b3a0
pcmpeqd %xmm2, %xmm2
movdqa 0x19fc(%rip), %xmm3     # 0x88e0
movdqa %xmm0, %xmm4
paddq %xmm0, %xmm4
paddq %xmm1, %xmm4
movdqu %xmm4, 0x20(%rcx,%rax,8)
psubq %xmm2, %xmm4
movdqu %xmm4, 0x40(%rcx,%rax,8)
paddq %xmm3, %xmm0
addq $0x2, %rax
jne 0x6ee4
xorl %eax, %eax
leaq 0x446d(%rip), %rbx      # 0xb380
movb %al, (%rbx,%rax,4)
movb $0x0, 0x1(%rbx,%rax,4)
incq %rax
cmpq $0x20, %rax
jne 0x6f13
leaq 0x1245d5(%rip), %r13    # 0x12b500
movq %rbx, 0x30(%r13)
xorl %r15d, %r15d
movw %r15w, 0x38(%r13)
movq %rbx, 0x20(%r13)
movw %r15w, 0x28(%r13)
movq %rbx, 0x10(%r13)
movw %r15w, 0x18(%r13)
movq %rbx, (%r13)
movw %r15w, 0x8(%r13)
leaq 0x124427(%rip), %r14    # 0x12b380
movw $0x7c00, (%r14)         # imm = 0x7C00
leaq 0x41da(%rip), %rax      # 0xb140
movq (%rax), %rcx
leaq 0x7c00(%rbx), %rdi
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x11c0
movl $0x1e001e, 0x41a(%rbx)  # imm = 0x1E001E
movq %r14, %rbx
movl $0x10000, 0x1245d2(%rip) # imm = 0x10000
callq 0x72ce
movl %eax, 0x1245cb(%rip)    # 0x12b56c
movl $0x4, %r12d
movl $0x10000, %eax          # imm = 0x10000
movq %rax, 0x8(%rsp)
movl $0x36ee8, %ebp          # imm = 0x36EE8
xorl %r14d, %r14d
movzwl (%rbx), %eax
testw %ax, %ax
jne 0x6fcd
cmpw $0x0, 0x18(%r13)
je 0x70bf
decl 0x124595(%rip)          # 0x12b568
jne 0x70a7
callq 0x72ce
cmpl 0x124588(%rip), %eax    # 0x12b56c
jle 0x7075
movl %eax, %ebx
movl $0x8, %edi
callq 0x2194
imull $0x36ee8, %r14d, %r12d  # imm = 0x36EE8
leal 0xfff(%r12), %r13d
testl %r12d, %r12d
cmovnsl %r12d, %r13d
incl %r14d
leal (%r12,%rbp), %eax
addl $0x37ee7, %r12d         # imm = 0x37EE7
testl %eax, %eax
cmovnsl %eax, %r12d
cmpl $0x1000, %r14d          # imm = 0x1000
cmovel %r15d, %r14d
callq 0x714b
testl %eax, %eax
je 0x703e
movl $0x9, %edi
callq 0x2194
sarl $0xc, %r13d
negl %r13d
sarl $0xc, %r12d
addl %r13d, %r12d
addl 0x124519(%rip), %r12d   # 0x12b56c
movl %r12d, 0x124512(%rip)   # 0x12b56c
cmpl %r12d, %ebx
jg 0x6fec
movl $0x4, %r12d
leaq 0x124494(%rip), %r13    # 0x12b500
leaq 0x12430d(%rip), %rbx    # 0x12b380
jmp 0x7099
decl %r12d
leaq 0x124301(%rip), %rbx    # 0x12b380
jne 0x7099
movq 0x8(%rsp), %rcx
leal (%rcx,%rcx), %eax
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq %rcx, 0x8(%rsp)
movl $0x4, %r12d
movq 0x8(%rsp), %rax
movl %eax, 0x1244c4(%rip)    # 0x12b568
movzwl (%rbx), %eax
leaq 0x1242d4(%rip), %rcx    # 0x12b382
movw %ax, (%rcx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2340
jmp 0x6fb9
xorl %eax, %eax
jmp 0x70d2
movq 0x10(%rsp), %rdi
callq 0x1297
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3ef0(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq (%rbx,%r15,8), %rdx
leaq 0x18a5(%rip), %rsi      # 0x899b
xorl %eax, %eax
callq 0x11a0
jmp 0x70cd
nop