// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movl %edi, %r15d
callq 0x71d3
movq (%r14), %rdi
cmpl $0x2, %r15d
jl 0x6df7
movq %rdi, 0x8(%rsp)
leaq 0x1b12(%rip), %r12      # 0x88b0
leaq 0x12474b(%rip), %rbx    # 0x12b4f0
leaq 0x124745(%rip), %r13    # 0x12b4f1
movq 0x8(%r14), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1190
movq %rbx, %rcx
testl %eax, %eax
je 0x6dd8
movq %rbp, %rdi
leaq 0x1ae9(%rip), %rsi      # 0x88b5
callq 0x1190
movq %r13, %rcx
testl %eax, %eax
jne 0x6df2
addq $0x8, %r14
movb $0x1, (%rcx)
leal -0x1(%r15), %eax
cmpl $0x2, %r15d
movl %eax, %r15d
jg 0x6dac
movl $0x1, %r15d
movq 0x8(%rsp), %rdi
leal -0x4(%r15), %eax
cmpl $-0x2, %eax
jb 0x709f
xorl %ebp, %ebp
cmpl $0x2, %r15d
setge %bl
jl 0x6e64
movq 0x8(%r14), %rsi
leaq 0x4326(%rip), %r12      # 0xb140
movq %r12, %rdi
callq 0x140c
testb %al, %al
je 0x70ab
movl %r15d, %ebx
addq $0x18, %r12
movl $0x2, %r15d
cmpq %r15, %rbx
je 0x6e61
movq (%r14,%r15,8), %rsi
movq %r12, %rdi
callq 0x140c
addq $0x18, %r12
incq %r15
testb %al, %al
jne 0x6e37
decq %r15
cmpq %rbx, %r15
setb %bl
jmp 0x70b1
seta %bl
testb %bl, %bl
jne 0x708e
xorl %eax, %eax
leaq 0x12457b(%rip), %rcx    # 0x12b3f0
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
andl %eax, %edi
cmpl $0x1, %edi
sbbl $-0x1, %edx
leal (%rsi,%rsi), %edi
cmpl $0x80, %esi
movl %edi, %esi
jl 0x6e7c
notb %dl
andb $0x1, %dl
movb %dl, (%rcx,%rax)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x6e75
leaq 0x1244e3(%rip), %rax    # 0x12b390
movq $-0x4, %rcx
leaq 0x1244e5(%rip), %rdx    # 0x12b3a0
movq %rax, 0x20(%rdx,%rcx,8)
leaq 0x1(%rax), %rsi
movq %rsi, 0x40(%rdx,%rcx,8)
addq $0x2, %rax
incq %rcx
jne 0x6ebb
movl %ebp, 0x14(%rsp)
xorl %eax, %eax
leaq 0x44a1(%rip), %rbx      # 0xb380
movb %al, (%rbx,%rax,4)
movb $0x0, 0x1(%rbx,%rax,4)
incq %rax
cmpq $0x20, %rax
jne 0x6edf
leaq 0x124609(%rip), %r12    # 0x12b500
movq %rbx, 0x30(%r12)
xorl %r15d, %r15d
movw %r15w, 0x38(%r12)
movq %rbx, 0x20(%r12)
movw %r15w, 0x28(%r12)
movq %rbx, 0x10(%r12)
movw %r15w, 0x18(%r12)
movq %rbx, (%r12)
movw %r15w, 0x8(%r12)
leaq 0x124454(%rip), %rbp    # 0x12b380
movw $0x7c00, (%rbp)         # imm = 0x7C00
leaq 0x4207(%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
movl $0x10000, 0x124602(%rip) # imm = 0x10000
callq 0x72aa
movl %eax, 0x1245fb(%rip)    # 0x12b56c
movl $0x4, %r13d
movl $0x10000, %eax          # imm = 0x10000
movq %rax, 0x8(%rsp)
movl $0x36ee8, %ebx          # imm = 0x36EE8
xorl %r14d, %r14d
cmpw $0x0, (%rbp)
jne 0x6f9d
cmpw $0x0, 0x18(%r12)
je 0x708a
decl 0x1245c5(%rip)          # 0x12b568
jne 0x706e
callq 0x72aa
movl %eax, %ebp
cmpl 0x1245b6(%rip), %eax    # 0x12b56c
jg 0x6fd5
decl %r13d
jne 0x6fd5
movq 0x8(%rsp), %rcx
leal (%rcx,%rcx), %eax
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq %rcx, 0x8(%rsp)
movl $0x4, %r13d
cmpl 0x124591(%rip), %ebp    # 0x12b56c
jle 0x705c
movl $0x8, %edi
callq 0x21c8
imull $0x36ee8, %r14d, %r13d  # imm = 0x36EE8
leal 0xfff(%r13), %r12d
testl %r13d, %r13d
cmovnsl %r13d, %r12d
incl %r14d
leal (%rbx,%r13), %eax
addl $0x37ee7, %r13d         # imm = 0x37EE7
testl %eax, %eax
cmovnsl %eax, %r13d
cmpl $0x1000, %r14d          # imm = 0x1000
cmovel %r15d, %r14d
callq 0x7129
testl %eax, %eax
je 0x702e
movl $0x9, %edi
callq 0x21c8
sarl $0xc, %r12d
negl %r12d
sarl $0xc, %r13d
addl %r12d, %r13d
addl 0x124529(%rip), %r13d   # 0x12b56c
movl %r13d, 0x124522(%rip)   # 0x12b56c
cmpl %r13d, %ebp
jg 0x6fdd
movl $0x4, %r13d
leaq 0x1244a4(%rip), %r12    # 0x12b500
movq 0x8(%rsp), %rax
movl %eax, 0x124501(%rip)    # 0x12b568
leaq 0x124312(%rip), %rbp    # 0x12b380
movzwl (%rbp), %eax
leaq 0x124309(%rip), %rcx    # 0x12b382
movw %ax, (%rcx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x236d
jmp 0x6f89
movl 0x14(%rsp), %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1297
movl $0x1, %ebp
jmp 0x708e
movl $0x1, %r15d
movq 0x3f28(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movq (%r14,%r15,8), %rdx
leaq 0x1895(%rip), %rsi      # 0x895b
xorl %eax, %eax
callq 0x11a0
movl $0x1, %ebp
jmp 0x6e64
nop