// File: /workspace/github_cpp_makefile_star2_O2/7shi[P]8086run/v1/main.cpp
// Function: bios(int)
void bios(int n) {
    void intr(int);
    static uint8_t buf[512];
    switch (n) {
        case 0x00: // #DE
            error("#DE: division exception");
            break;
        case 0x08: // timer
        {
            uint16_t low = read16(&mem[0x46c]);
            if (!++low) {
                uint16_t high = read16(&mem[0x46e]);
                if (++high >= 24) {
                    high %= 24;
                    mem[0x470] = 1;
                }
                write16(&mem[0x46e], high);
            }
            write16(&mem[0x46c], low);
            intr(0x1c);
            return;
        }
        case 0x09: // keyboard
            while (kbhit()) {
                int head = read16(&mem[0x41a]) - 0x1e;
                int tail = read16(&mem[0x41c]) - 0x1e;
                int next = (tail + 2) & 0x1f;
                if (next == head) break;
                int ch = getch();
                if (ch != EOF && kbscan[ch]) {
                    mem[0x41e + tail] = ch;
                    mem[0x41f + tail] = kbscan[ch];
                    write16(&mem[0x41c], 0x1e + next);
                }
            }
            return;
        case 0x10: // video
            switch (AH) {
                case 0x0e:
                    if (write(STDOUT_FILENO, &AL, 1));
                    return;
            }
            return; // ignore
        case 0x11: // get equipment list
            AX = disks[1].f ? 0x41 : 1;
            return;
        case 0x12: // get memory size
            AX = 640;
            return;
        case 0x13: // disk
        {
            if (DL > 1 || !disks[DL].f) {
                CF = 1; // error
                AH = 1; // invalid
                return;
            }
            Disk *d = &disks[DL];
            int c = CH | ((CL & 0xc0) << 2), s = (CL & 0x3f) - 1;
            int lba = d->s * (d->h * c + DH) + s;
            switch (AH) {
                case 0x00: // reset disk system
                    AH = CF = 0;
                    return;
                case 0x02: // read sectors
                case 0x03: // write sectors
                    if (c >= d->c || DH >= d->h
                            || s < 0 || s >= d->s) {
                        CF = 1; // error
                        AH = 4; // sector
                        return;
                    }
                    fseek(d->f, lba << 9, SEEK_SET);
                    if (AH == 2) {
                        if (fread(&ES[BX], 512, AL, d->f) < 1) {
                            memset(&ES[BX], 0, AL << 9);
                        }
                    } else {
                        fwrite(&ES[BX], 512, AL, d->f);
                    }
                    AH = CF = 0;
                    return;
                case 0x05: // format
                    fseek(d->f, (lba - s) << 9, SEEK_SET);
                    for (int i = 0; i < d->s; ++i) {
                        fwrite(&buf, 512, 1, d->f);
                    }
                    AH = CF = 0;
                    return;
                case 0x08: // get drive params
                    AX = 0;
                    BL = d->type;
                    CH = d->c - 1;
                    CL = d->s | ((d->c >> 2) & 0xc0);
                    DH = d->h - 1;
                    DL = 1;
                    ES = DI = 0;
                    CF = 0;
                    return;
                case 0x15: // get disk type
                    AH = 1;
                    CF = 0;
                    return;
                case 0x17: // set disk type
                    CF = 0; // ignore
                    return;
                case 0x18: // set media type
                    d->c = c + 1;
                    d->s = s + 1;
                    ES = DI = 0;
                    AH = CF = 0;
                    return;
            }
            CF = 1;
            return;
        }
        case 0x15: // system
            switch (AH) {
                case 0x88: // get extended memory size
                    CF = 0;
                    AX = 0;
                    return;
            }
            break;
        case 0x16: // keyboard
        {
            int head = read16(&mem[0x41a]);
            int tail = read16(&mem[0x41c]);
            switch (AH) {
                case 0x00: // get keystroke
                    while (head == tail) {
                        bios(9);
                        tail = read16(&mem[0x41c]);
                    }
                    AX = read16(&mem[0x400 + head]);
                    write16(&mem[0x41a], 0x1e + ((head - 0x1c) & 0x1f));
                    return;
                case 0x01: // check for keystroke
                    if ((ZF = head == tail)) {
                        AX = 0; // empty
                    } else {
                        AX = read16(&mem[0x400 + head]);
                    }
                    return;
            }
            break;
        }
        case 0x1a: // time
            switch (AH) {
                case 0x00: // get system time
                    AL = mem[0x470];
                    CX = read16(&mem[0x46e]);
                    DX = read16(&mem[0x46c]);
                    mem[0x470] = 0;
                    return;
                case 0x01: // set system time
                    write16(&mem[0x46e], CX);
                    write16(&mem[0x46c], DX);
                    mem[0x470] = 0;
                    return;
                case 0x02: // get RTC time
                case 0x04: // get RTC date
                {
                    time_t t = time(NULL);
                    tm *lt = localtime(&t);
                    if (AH == 2) {
                        CH = bcd(lt->tm_hour);
                        CL = bcd(lt->tm_min);
                        DH = bcd(lt->tm_sec);
                        DL = lt->tm_isdst == 1 ? 1 : 0;
                    } else {
                        CH = bcd(lt->tm_year / 100 + 19);
                        CL = bcd(lt->tm_year % 100);
                        DH = bcd(lt->tm_mon + 1);
                        DL = bcd(lt->tm_mday);
                    }
                    CF = 0;
                    return;
                }
                case 0x03: // set RTC time
                case 0x05: // set RTC date
                    return; // ignore
            }
            break;
        case 0x1c: // timer handler
            return;
    }
    error("not implemented: int %02x,%02x", n, AH);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %esi
leal -0x8(%rsi), %eax
cmpl $0x14, %eax
ja 0x1df7
leaq 0x37d9(%rip), %rcx      # 0x5004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzwl 0x6de1(%rip), %ecx      # 0x861c
incw %cx
jne 0x186e
movzwl 0x6dd7(%rip), %eax      # 0x861e
incl %eax
cmpw $0x18, %ax
jb 0x1861
movw $0x18, %si
xorl %edx, %edx
divw %si
movb $0x1, 0x6dc1(%rip)      # 0x8620
movl %edx, %eax
movb %al, 0x6db7(%rip)       # 0x861e
movb $0x0, 0x6db1(%rip)      # 0x861f
movw %cx, 0x6da7(%rip)       # 0x861c
pushq $0x1c
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1e23
movq 0x126962(%rip), %rax    # 0x1281f0
movzbl (%rax), %eax
cmpq $0x5, %rax
ja 0x1e12
leaq 0x37b6(%rip), %rcx      # 0x5058
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %edi, %edi
callq 0x1090
movq %rsp, %rdi
movq %rax, (%rdi)
callq 0x1070
movq %rax, %rcx
movq 0x126929(%rip), %rax    # 0x1281f0
cmpb $0x2, (%rax)
jne 0x1b8f
movl 0x8(%rcx), %eax
pushq $0xa
popq %rsi
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x126913(%rip), %rdx    # 0x1281f8
movb %al, (%rdx)
movl 0x4(%rcx), %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x1268df(%rip), %rdx    # 0x1281d8
movb %al, (%rdx)
movl (%rcx), %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x1268f4(%rip), %rdx    # 0x128200
movb %al, (%rdx)
cmpl $0x1, 0x20(%rcx)
sete %al
jmp 0x1bee
movzwl 0x6ca8(%rip), %r14d     # 0x85ca
movzwl 0x6ca3(%rip), %ecx      # 0x85cc
movq 0x1268c0(%rip), %rax    # 0x1281f0
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x1b30
testl %eax, %eax
jne 0x1e12
pushq $0x9
popq %rbx
cmpw %cx, %r14w
jne 0x1b44
movl %ebx, %edi
callq 0x180f
movzwl 0x6c6d(%rip), %ecx      # 0x85cc
jmp 0x1947
xorl %eax, %eax
cmpq $0x0, 0x126a0d(%rip)    # 0x128378
setne %al
shll $0x6, %eax
incl %eax
movw %ax, 0x126846(%rip)     # 0x1281c0
jmp 0x1c4e
movw $0x280, 0x126838(%rip)  # imm = 0x280
jmp 0x1c4e
leaq 0x674c(%rip), %rbx      # 0x80e0
leaq 0x6815(%rip), %r14      # 0x81b0
callq 0x12be
testl %eax, %eax
je 0x1c4e
movzwl 0x6c1b(%rip), %eax      # 0x85ca
addl $-0x1e, %eax
movzwl 0x6c12(%rip), %r15d     # 0x85cc
leal 0x4(%r15), %ebp
andl $0x1f, %ebp
cmpl %eax, %ebp
je 0x1c4e
callq 0x127c
cmpl $-0x1, %eax
je 0x199b
movslq %eax, %rcx
movb (%rcx,%rbx), %cl
testb %cl, %cl
je 0x199b
movb %al, 0x400(%r15,%r14)
movb %cl, 0x401(%r15,%r14)
addb $0x1e, %bpl
movb %bpl, 0x6bd4(%rip)      # 0x85cc
movb $0x0, 0x6bce(%rip)      # 0x85cd
jmp 0x199b
movq 0x1267e8(%rip), %rax    # 0x1281f0
movb (%rax), %al
cmpb $-0x78, %al
jne 0x1e12
movb $0x0, 0x1267ff(%rip)    # 0x128218
andw $0x0, 0x12679f(%rip)    # 0x1281c0
jmp 0x1c4e
movq 0x1267c3(%rip), %rax    # 0x1281f0
cmpb $0xe, (%rax)
jne 0x1c4e
movq 0x126793(%rip), %rsi    # 0x1281d0
pushq $0x1
popq %rdi
pushq $0x1
popq %rdx
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1030
movq 0x126787(%rip), %rax    # 0x1281e0
movzbl (%rax), %eax
cmpq $0x1, %rax
ja 0x1b1a
imulq $0x18, %rax, %rax
leaq 0x1268ef(%rip), %r15    # 0x128360
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x1b1a
addq %rax, %r15
movq 0x126770(%rip), %rax    # 0x1281f8
movq 0x126749(%rip), %rcx    # 0x1281d8
movzbl (%rcx), %ecx
leal (,%rcx,4), %edx
movb (%rax), %dl
andl $0x3f, %ecx
movq 0x12674b(%rip), %rax    # 0x1281f0
movzbl (%rax), %r8d
cmpq $0x8, %r8
ja 0x1c59
movl 0x10(%r15), %r10d
movl 0x14(%r15), %r9d
movl %edx, %esi
imull %r10d, %esi
movq 0x126738(%rip), %r11    # 0x128200
movzbl (%r11), %r11d
addl %r11d, %esi
imull %r9d, %esi
leaq 0x3596(%rip), %rbx      # 0x5070
movslq (%rbx,%r8,4), %r8
addq %rbx, %r8
jmpq *%r8
cmpl 0xc(%r15), %edx
jge 0x1b0b
cmpl %r11d, %r10d
setle %dl
testl %ecx, %ecx
sete %r8b
orb %dl, %r8b
cmpl %r9d, %ecx
setg %dl
orb %r8b, %dl
cmpb $0x1, %dl
jne 0x1d68
movb $0x1, 0x126706(%rip)    # 0x128218
movb $0x4, (%rax)
jmp 0x1c4e
movb $0x1, %al
movb %al, 0x1266f6(%rip)     # 0x128218
movq 0x1266c7(%rip), %rcx    # 0x1281f0
movb %al, (%rcx)
jmp 0x1c4e
cmpw %cx, %r14w
sete 0x1266da(%rip)          # 0x128215
jne 0x1b7a
xorl %eax, %eax
jmp 0x1973
leaq 0x6665(%rip), %rax      # 0x81b0
movzwl 0x400(%r14,%rax), %eax
movw %ax, 0x126665(%rip)     # 0x1281c0
addb $0x4, %r14b
andb $0x1f, %r14b
addb $0x1e, %r14b
movb %r14b, 0x6a5c(%rip)     # 0x85ca
movb $0x0, 0x6a56(%rip)      # 0x85cb
jmp 0x1c4e
leaq 0x662f(%rip), %rax      # 0x81b0
movzwl 0x400(%r14,%rax), %eax
jmp 0x1973
movl 0x14(%rcx), %eax
pushq $0x64
popq %rdi
cltd
idivl %edi
addl $0x13, %eax
pushq $0xa
popq %rsi
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x12664b(%rip), %rdx    # 0x1281f8
movb %al, (%rdx)
movl 0x14(%rcx), %eax
cltd
idivl %edi
movsbl %dl, %eax
movb $0xa, %dl
idivb %dl
movsbl %ah, %edx
shlb $0x4, %al
orb %al, %dl
movq 0x12660d(%rip), %rax    # 0x1281d8
movb %dl, (%rax)
movl 0x10(%rcx), %eax
incl %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x12661f(%rip), %rdx    # 0x128200
movb %al, (%rdx)
movl 0xc(%rcx), %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x1265eb(%rip), %rcx    # 0x1281e0
movb %al, (%rcx)
movb $0x0, 0x12661a(%rip)    # 0x128218
jmp 0x1c4e
movzwl 0x1265bb(%rip), %eax    # 0x1281c2
movw %ax, 0x6a10(%rip)       # 0x861e
movzwl 0x1265af(%rip), %eax    # 0x1281c4
movb %al, 0x6a01(%rip)       # 0x861c
movb %ah, 0x69fc(%rip)       # 0x861d
jmp 0x1c47
movb 0x69f7(%rip), %al       # 0x8620
movq 0x1265a0(%rip), %rcx    # 0x1281d0
movb %al, (%rcx)
movd 0x69e2(%rip), %xmm0     # 0x861c
pshuflw $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3,4,5,6,7]
movd %xmm0, 0x12657b(%rip)   # 0x1281c2
movb $0x0, 0x69d2(%rip)      # 0x8620
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x15, %r8d
je 0x1d60
cmpl $0x17, %r8d
je 0x1bf7
cmpl $0x18, %r8d
jne 0x1ca3
incl %edx
movl %edx, 0xc(%r15)
andw $0x0, 0x126551(%rip)    # 0x1281ce
leaq 0x652c(%rip), %rdx      # 0x81b0
movq %rdx, 0x126695(%rip)    # 0x128320
andw $0x0, 0x126695(%rip)    # 0x128328
movl %ecx, 0x14(%r15)
xorl %ecx, %ecx
movb %cl, 0x126579(%rip)     # 0x128218
movb %cl, (%rax)
jmp 0x1c4e
movb $0x1, 0x12656e(%rip)    # 0x128218
jmp 0x1c4e
andw $0x0, 0x12650c(%rip)    # 0x1281c0
movb 0x8(%r15), %al
movq 0x126529(%rip), %rcx    # 0x1281e8
movb %al, (%rcx)
movb 0xc(%r15), %al
decb %al
movq 0x12652a(%rip), %rcx    # 0x1281f8
movb %al, (%rcx)
movl 0xc(%r15), %eax
shrl $0x2, %eax
andl $0xc0, %eax
orl 0x14(%r15), %eax
movq 0x1264f1(%rip), %rcx    # 0x1281d8
movb %al, (%rcx)
movb 0x10(%r15), %al
decb %al
movq 0x12650a(%rip), %rcx    # 0x128200
movb %al, (%rcx)
movq 0x1264e1(%rip), %rax    # 0x1281e0
movb $0x1, (%rax)
andw $0x0, 0x1264c4(%rip)    # 0x1281ce
leaq 0x649f(%rip), %rax      # 0x81b0
movq %rax, 0x126608(%rip)    # 0x128320
andw $0x0, 0x126608(%rip)    # 0x128328
jmp 0x1bf7
shll $0x9, %esi
movslq %esi, %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x10b0
leaq 0x126655(%rip), %rbx    # 0x128390
pushq $0x1
popq %r14
cmpl 0x14(%r15), %ebp
jge 0x1de1
movq (%r15), %rcx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1170
incl %ebp
jmp 0x1d3f
movb $0x1, (%rax)
jmp 0x1bf7
leal (%rcx,%rsi), %eax
addl $0x7fffff, %eax         # imm = 0x7FFFFF
shll $0x9, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x10b0
movq 0x12646c(%rip), %rax    # 0x1281f0
movzwl 0x12643b(%rip), %edi    # 0x1281c6
movq 0x12643e(%rip), %rcx    # 0x1281d0
movzbl (%rcx), %edx
movq (%r15), %rcx
addq 0x126581(%rip), %rdi    # 0x128320
cmpb $0x2, (%rax)
jne 0x1dd7
movl $0x200, %esi            # imm = 0x200
callq 0x1150
testq %rax, %rax
jne 0x1de1
movzwl 0x12640c(%rip), %edi    # 0x1281c6
addq 0x12655f(%rip), %rdi    # 0x128320
movq 0x126408(%rip), %rax    # 0x1281d0
movzbl (%rax), %edx
shll $0x9, %edx
xorl %esi, %esi
callq 0x1040
jmp 0x1de1
movl $0x200, %esi            # imm = 0x200
callq 0x1170
xorl %eax, %eax
movb %al, 0x12642f(%rip)     # 0x128218
movq 0x126400(%rip), %rcx    # 0x1281f0
movb %al, (%rcx)
jmp 0x1c4e
testl %esi, %esi
jne 0x1e09
leaq 0x38c1(%rip), %rdi      # 0x56c3
xorl %eax, %eax
callq 0x169d
movq 0x1263e0(%rip), %rax    # 0x1281f0
movb (%rax), %al
movzbl %al, %edx
leaq 0x38bf(%rip), %rdi      # 0x56db
xorl %eax, %eax
callq 0x169d