// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x8f50
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
cmpl $0x1, -0x8(%rbp)
jle 0x8971
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x1f05(%rip), %rsi      # 0xa820
callq 0x21e0
cmpl $0x0, %eax
jne 0x8931
leaq 0x124c84(%rip), %rax    # 0x12d5b0
movb $0x1, (%rax)
jmp 0x895a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x1ee5(%rip), %rsi      # 0xa825
callq 0x21e0
cmpl $0x0, %eax
jne 0x8956
leaq 0x124c60(%rip), %rax    # 0x12d5b1
movb $0x1, (%rax)
jmp 0x8958
jmp 0x8971
jmp 0x895a
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x8906
cmpl $0x2, -0x8(%rbp)
jl 0x8981
movl $0x3, %eax
cmpl -0x8(%rbp), %eax
jge 0x89d9
movq 0x4658(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1e97(%rip), %rsi      # 0xa82d
movb $0x0, %al
callq 0x21f0
movq 0x463c(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0x1eaf(%rip), %rsi      # 0xa85d
movb $0x0, %al
callq 0x21f0
movq 0x4624(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0x1eb7(%rip), %rsi      # 0xa87d
movb $0x0, %al
callq 0x21f0
movl $0x1, -0x4(%rbp)
jmp 0x8df0
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x8a50
movl -0x1c(%rbp), %eax
subl $0x1, %eax
cltq
leaq 0x4809(%rip), %rdi      # 0xd200
imulq $0x18, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x23a0
testb $0x1, %al
jne 0x8a43
movq 0x45c6(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x1e9b(%rip), %rsi      # 0xa8cb
movb $0x0, %al
callq 0x21f0
movl $0x1, -0x4(%rbp)
jmp 0x8df0
jmp 0x8a45
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x89e0
movl $0x0, -0x20(%rbp)
cmpl $0x100, -0x20(%rbp)     # imm = 0x100
jge 0x8ac0
movl $0x0, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
jge 0x8a98
movl -0x20(%rbp), %eax
andl -0x28(%rbp), %eax
cmpl $0x0, %eax
je 0x8a8b
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8a8d
movl -0x28(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x8a6e
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
sete %dl
movslq -0x20(%rbp), %rcx
andb $0x1, %dl
leaq 0x1249fe(%rip), %rax    # 0x12d4b0
movb %dl, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x8a57
movw $0x1234, -0x2a(%rbp)    # imm = 0x1234
leaq 0x124983(%rip), %rax    # 0x12d450
movq %rax, -0x38(%rbp)
movzbl -0x2a(%rbp), %eax
cmpl $0x34, %eax
jne 0x8b38
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jge 0x8b36
movq -0x38(%rbp), %rdx
movl -0x3c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdx
movslq -0x3c(%rbp), %rcx
leaq 0x124960(%rip), %rax    # 0x12d460
movq %rdx, (%rax,%rcx,8)
movslq -0x3c(%rbp), %rcx
leaq 0x124951(%rip), %rax    # 0x12d460
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rcx
leaq 0x124939(%rip), %rax    # 0x12d460
movq %rdx, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x8ae1
jmp 0x8b9a
movl $0x0, -0x40(%rbp)
cmpl $0x4, -0x40(%rbp)
jge 0x8b98
movq -0x38(%rbp), %rdx
movl -0x40(%rbp), %eax
shll %eax
cltq
addq %rax, %rdx
addq $0x1, %rdx
movslq -0x40(%rbp), %rcx
leaq 0x1248fe(%rip), %rax    # 0x12d460
movq %rdx, (%rax,%rcx,8)
movslq -0x40(%rbp), %rcx
leaq 0x1248ef(%rip), %rax    # 0x12d460
movq (%rax,%rcx,8), %rdx
addq $-0x1, %rdx
movl -0x40(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rcx
leaq 0x1248d7(%rip), %rax    # 0x12d460
movq %rdx, (%rax,%rcx,8)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x8b3f
jmp 0x8b9a
movl $0x0, -0x44(%rbp)
cmpl $0x20, -0x44(%rbp)
jge 0x8bcf
movl -0x44(%rbp), %eax
shll $0x2, %eax
cltq
leaq 0x488a(%rip), %rdi      # 0xd440
addq %rax, %rdi
movl -0x44(%rbp), %eax
movzwl %ax, %esi
callq 0x31e0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x8ba1
leaq 0x1249ea(%rip), %rdi    # 0x12d5c0
addq $0x30, %rdi
xorl %esi, %esi
callq 0x3230
leaq 0x1249d8(%rip), %rdi    # 0x12d5c0
addq $0x20, %rdi
movzwl %ax, %esi
callq 0x3230
leaq 0x1249c5(%rip), %rdi    # 0x12d5c0
addq $0x10, %rdi
movzwl %ax, %esi
callq 0x3230
leaq 0x1249b2(%rip), %rdi    # 0x12d5c0
movzwl %ax, %esi
callq 0x3230
leaq 0x124823(%rip), %rax    # 0x12d440
movw $0x7c00, (%rax)         # imm = 0x7C00
leaq 0x124817(%rip), %rax    # 0x12d440
movzwl (%rax), %eax
leaq 0x480d(%rip), %rdi      # 0xd440
addq %rax, %rdi
leaq 0x45c3(%rip), %rax      # 0xd200
movq (%rax), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2210
cmpq $0x0, %rax
je 0x8c57
jmp 0x8c57
leaq 0x47e2(%rip), %rdi      # 0xd440
addq $0x41a, %rdi            # imm = 0x41A
movl $0x1e, %esi
callq 0x31e0
leaq 0x47ca(%rip), %rdi      # 0xd440
addq $0x41c, %rdi            # imm = 0x41C
movl $0x1e, %esi
callq 0x31e0
movl $0x10000, -0x48(%rbp)   # imm = 0x10000
movl $0x0, -0x4c(%rbp)
movl $0x4, -0x50(%rbp)
movl -0x48(%rbp), %eax
movl %eax, 0x124983(%rip)    # 0x12d628
callq 0x9030
movl %eax, 0x12497c(%rip)    # 0x12d62c
movb $0x1, %al
leaq 0x124787(%rip), %rcx    # 0x12d440
cmpw $0x0, (%rcx)
movb %al, -0x61(%rbp)
jne 0x8cdc
leaq 0x1248f7(%rip), %rdi    # 0x12d5c0
addq $0x10, %rdi
callq 0x37a0
cmpw $0x0, %ax
setne %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x8ce8
jmp 0x8df0
movl 0x12493a(%rip), %eax    # 0x12d628
addl $-0x1, %eax
movl %eax, 0x124931(%rip)    # 0x12d628
cmpl $0x0, %eax
jne 0x8dcc
callq 0x9030
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl 0x12491b(%rip), %eax    # 0x12d62c
jg 0x8d41
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, %eax
jne 0x8d41
movl -0x48(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x48(%rbp), %eax
shll %eax
movl %eax, -0x48(%rbp)
cmpl -0x58(%rbp), %eax
jge 0x8d3a
movl -0x58(%rbp), %eax
movl %eax, -0x48(%rbp)
movl $0x4, -0x50(%rbp)
jmp 0x8d43
movl -0x54(%rbp), %eax
cmpl 0x1248e0(%rip), %eax    # 0x12d62c
jle 0x8dc3
movl $0x8, %edi
callq 0x37c0
imull $0x36ee8, -0x4c(%rbp), %eax # imm = 0x36EE8
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, -0x5c(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
imull $0x36ee8, %eax, %eax    # imm = 0x36EE8
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, -0x60(%rbp)
cmpl $0x1000, -0x4c(%rbp)    # imm = 0x1000
jne 0x8d94
movl $0x0, -0x4c(%rbp)
callq 0x8e70
cmpl $0x0, %eax
je 0x8da8
movl $0x9, %edi
callq 0x37c0
movl -0x60(%rbp), %eax
subl -0x5c(%rbp), %eax
addl 0x124878(%rip), %eax    # 0x12d62c
movl %eax, 0x124872(%rip)    # 0x12d62c
movl $0x4, -0x50(%rbp)
jmp 0x8d43
movl -0x48(%rbp), %eax
movl %eax, 0x12485c(%rip)    # 0x12d628
leaq 0x12466d(%rip), %rax    # 0x12d440
movw (%rax), %cx
leaq 0x124665(%rip), %rax    # 0x12d442
movw %cx, (%rax)
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x3970
jmp 0x8cb0
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)