// File: /workspace/github_cpp_makefile_star2_O2/7shi[P]8086run/v2/bios.cpp
// Function: bios(int)
void bios(int n) {
    void intr(int);
    static uint8_t buf[512];
    static FILE *fhcopy;
    static bool hread;
    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;
                uint16_t scanasc = decodeKey(getch());
                if (scanasc) {
                    write16(&mem[0x41e + tail], scanasc);
                    write16(&mem[0x41c], 0x1e + next);
                }
            }
            return;
        case 0x10: // video
            switch (AH) {
                case 0x00:
#ifdef _WIN32
                    system("cls");
#else
                    printf("\x1b[H\x1b[J");
                    fflush(stdout);
#endif
                    cleared = true;
                    return;
                case 0x02:
                {
#ifdef _WIN32
                    HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);
                    COORD pos = {DL, DH};
                    SetConsoleCursorPosition(hStd, pos);
#else
                    printf("\x1b[%d;%dH", DH + 1, DL + 1);
                    fflush(stdout);
#endif
                    return;
                }
                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
        {
            switch (AH) { // ORIGINAL EXTENSIONS
                case 0xfe: // hopen
                    if (fhcopy) fclose(fhcopy);
                    hread = AL != 1;
                    CF = !(fhcopy = fopen((char *) &DS[DX], hread ? "rb" : "wb"));
                    return;
                case 0xff: // hcopy
                    if (fhcopy) {
                        if (hread) {
                            CX = fread(&DS[DX], 1, 512, fhcopy);
                        } else {
                            CX = fwrite(&DS[DX], 1, CX, fhcopy);
                        }
                        if (!CX) {
                            fclose(fhcopy);
                            fhcopy = NULL;
                        }
                    } else {
                        CX = 0;
                    }
                    return;
            }
            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 0x14: // serial port
            CF = 1; // error
            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 0x17: // parallel port
            CF = 1; // error
            return;
        case 0x18: // boot fault
            error("Boot failed (INT 18H)");
            return;
        case 0x19: // bootstrap
            error("Bootstrap (INT 19H) not implemented");
            return;
        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 0x1b: // Control-Break handler
            return;
        case 0x1c: // timer handler
            return;
    }
    error("not implemented: int %02x,%02x", n, AH);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %edi, %ebx
cmpl $0x1c, %edi
ja 0x158f
movl %ebx, %eax
leaq 0x4c52(%rip), %rcx      # 0x6040
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x127fea(%rip), %rax    # 0x1293e8
movb $0x1, (%rax)
jmp 0x1bac
leaq 0x4d15(%rip), %rdi      # 0x6122
jmp 0x166d
leaq 0x127f87(%rip), %r15    # 0x1293a0
movq 0x20(%r15), %rax
movzbl (%rax), %edx
cmpl $0xff, %edx
je 0x173f
cmpl $0xfe, %edx
jne 0x17d2
movq 0x7f31(%rip), %rdi      # 0x9370
testq %rdi, %rdi
je 0x1449
callq 0x10e0
movq (%r15), %rax
leaq 0x127f3d(%rip), %rcx    # 0x129390
movzwl 0x4(%rcx), %edi
leaq 0x1280a2(%rip), %rcx    # 0x129500
addq 0x30(%rcx), %rdi
cmpb $0x1, (%rax)
setne 0x7f0c(%rip)            # 0x9378
leaq 0x4ca9(%rip), %rax      # 0x611c
leaq 0x4ca5(%rip), %rsi      # 0x611f
cmovneq %rax, %rsi
callq 0x1130
movq %rax, 0x7ee6(%rip)      # 0x9370
testq %rax, %rax
leaq 0x127f54(%rip), %rax    # 0x1293e8
sete (%rax)
jmp 0x1bac
leaq 0x7edd(%rip), %r14      # 0x9380
movzwl 0x41a(%r14), %r15d
movzwl 0x41c(%r14), %eax
leaq 0x127ee6(%rip), %rcx    # 0x1293a0
movq 0x20(%rcx), %rcx
movzbl (%rcx), %ecx
cmpl $0x1, %ecx
je 0x17b7
testl %ecx, %ecx
jne 0x158f
pushq $0x9
popq %rbx
cmpw %ax, %r15w
jne 0x1784
movl %ebx, %edi
callq 0x13ce
movzwl 0x41c(%r14), %eax
jmp 0x14d5
leaq 0x127ea9(%rip), %rax    # 0x1293a0
movq 0x20(%rax), %rax
cmpb $-0x78, (%rax)
jne 0x158f
leaq 0x127edd(%rip), %rax    # 0x1293e8
movb $0x0, (%rax)
leaq 0x127e7b(%rip), %rax    # 0x129390
andw $0x0, (%rax)
jmp 0x1bac
leaq 0x7e5b(%rip), %rbx      # 0x9380
callq 0x529a
testl %eax, %eax
je 0x1bac
movzwl 0x41a(%rbx), %eax
addl $-0x1e, %eax
movzwl 0x41c(%rbx), %r14d
leal 0x4(%r14), %ebp
andl $0x1f, %ebp
cmpl %eax, %ebp
je 0x1bac
callq 0x5258
movl %eax, %edi
callq 0x4c88
testw %ax, %ax
je 0x1525
movw %ax, 0x400(%r14,%rbx)
addb $0x1e, %bpl
movb %bpl, 0x41c(%rbx)
movb $0x0, 0x41d(%rbx)
jmp 0x1525
leaq 0x4b6c(%rip), %rdi      # 0x60f4
xorl %eax, %eax
callq 0x1eb0
leaq 0x127e0a(%rip), %rax    # 0x1293a0
movq 0x20(%rax), %rax
movzbl (%rax), %edx
leaq 0x4bb8(%rip), %rdi      # 0x615c
movl %ebx, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1eb0
leaq 0x127de0(%rip), %rax    # 0x1293a0
movq 0x20(%rax), %rcx
movzbl (%rcx), %ecx
cmpl $0xe, %ecx
je 0x18c6
cmpl $0x2, %ecx
je 0x188b
testl %ecx, %ecx
jne 0x1bac
leaq 0x4b24(%rip), %rdi      # 0x610c
xorl %eax, %eax
callq 0x1030
movq 0x79ca(%rip), %rax      # 0x8fc0
movq (%rax), %rdi
callq 0x1120
leaq 0x127eed(%rip), %rax    # 0x1294f2
jmp 0x13fe
leaq 0x7d6f(%rip), %rcx      # 0x9380
movzwl 0x46c(%rcx), %esi
incw %si
jne 0x164b
movzwl 0x46e(%rcx), %eax
incl %eax
cmpw $0x18, %ax
jb 0x163e
movw $0x18, %di
xorl %edx, %edx
divw %di
movb $0x1, 0x470(%rcx)
movl %edx, %eax
movb %al, 0x46e(%rcx)
movb $0x0, 0x46f(%rcx)
movw %si, 0x46c(%rcx)
pushq $0x1c
popq %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1f98
leaq 0x4acb(%rip), %rdi      # 0x6138
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1eb0
leaq 0x127d19(%rip), %r14    # 0x1293a0
movq 0x20(%r14), %rax
movzbl (%rax), %eax
cmpq $0x5, %rax
ja 0x158f
leaq 0x4a15(%rip), %rcx      # 0x60b4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %edi, %edi
callq 0x10d0
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
callq 0x10a0
movq %rax, %rcx
movq 0x20(%r14), %rax
cmpb $0x2, (%rax)
jne 0x1900
movl 0x8(%rcx), %eax
pushq $0xa
popq %rsi
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x28(%r14), %rdx
movb %al, (%rdx)
movl 0x4(%rcx), %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x8(%r14), %rdx
movb %al, (%rdx)
movl (%rcx), %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x30(%r14), %rdx
movb %al, (%rdx)
cmpl $0x1, 0x20(%rcx)
sete %al
jmp 0x1956
xorl %eax, %eax
cmpq $0x0, 0x7a41(%rip)      # 0x9158
setne %al
shll $0x6, %eax
incl %eax
leaq 0x127c6a(%rip), %rcx    # 0x129390
movw %ax, (%rcx)
jmp 0x1bac
leaq 0x127c5b(%rip), %rax    # 0x129390
movw $0x280, (%rax)          # imm = 0x280
jmp 0x1bac
movq 0x7c2a(%rip), %rcx      # 0x9370
testq %rcx, %rcx
je 0x19d7
leaq 0x127c3a(%rip), %rbx    # 0x129390
movzwl 0x4(%rbx), %edi
leaq 0x127d9f(%rip), %rax    # 0x129500
addq 0x30(%rax), %rdi
cmpb $0x1, 0x7c0c(%rip)      # 0x9378
jne 0x19e8
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x11c0
jmp 0x19f4
movzwl 0x400(%r15,%r14), %eax
leaq 0x127bfc(%rip), %rcx    # 0x129390
movw %ax, (%rcx)
addb $0x4, %r15b
andb $0x1f, %r15b
addb $0x1e, %r15b
movb %r15b, 0x41a(%r14)
movb $0x0, 0x41b(%r14)
jmp 0x1bac
leaq 0x127c27(%rip), %rcx    # 0x1293e5
cmpw %ax, %r15w
sete (%rcx)
jne 0x18f2
xorl %eax, %eax
jmp 0x171f
movq 0x10(%r15), %rcx
movzbl (%rcx), %ecx
cmpq $0x1, %rcx
ja 0x18e0
imulq $0x18, %rcx, %rcx
leaq 0x7952(%rip), %r12      # 0x9140
movq (%rcx,%r12), %rdi
testq %rdi, %rdi
je 0x18e0
addq %rcx, %r12
movq 0x8(%r15), %rcx
movq 0x28(%r15), %r8
movzbl (%rcx), %ecx
leal (,%rcx,4), %esi
movb (%r8), %sil
andl $0x3f, %ecx
cmpl $0x8, %edx
ja 0x1a1a
movl 0x10(%r12), %r10d
movl 0x14(%r12), %r9d
movl %esi, %r8d
imull %r10d, %r8d
movq 0x30(%r15), %r11
movzbl (%r11), %r11d
addl %r11d, %r8d
imull %r9d, %r8d
movl %edx, %edx
leaq 0x4884(%rip), %rbx      # 0x60cc
movslq (%rbx,%rdx,4), %rdx
addq %rbx, %rdx
jmpq *%rdx
cmpl 0xc(%r12), %esi
jge 0x1879
cmpl %r11d, %r10d
setle %dl
testl %ecx, %ecx
sete %sil
orb %dl, %sil
cmpl %r9d, %ecx
setg %dl
orb %sil, %dl
cmpb $0x1, %dl
jne 0x1b2b
leaq 0x127b68(%rip), %rcx    # 0x1293e8
movb $0x1, (%rcx)
movb $0x4, (%rax)
jmp 0x1bac
movq 0x10(%rax), %rcx
movq 0x30(%rax), %rax
movzbl (%rax), %esi
incl %esi
movzbl (%rcx), %edx
incl %edx
leaq 0x486f(%rip), %rdi      # 0x6113
xorl %eax, %eax
callq 0x1030
movq 0x770e(%rip), %rax      # 0x8fc0
movq (%rax), %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1120
movq (%rax), %rsi
pushq $0x1
popq %rdi
pushq $0x1
popq %rdx
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1060
leaq 0x127b01(%rip), %rcx    # 0x1293e8
movb $0x1, %dl
movb %dl, (%rcx)
movb %dl, (%rax)
jmp 0x1bac
movzwl 0x400(%r15,%r14), %eax
jmp 0x171f
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 0x28(%r14), %rdx
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 0x8(%r14), %rax
movb %dl, (%rax)
movl 0x10(%rcx), %eax
incl %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x30(%r14), %rdx
movb %al, (%rdx)
movl 0xc(%rcx), %eax
cltd
idivl %esi
shll $0x4, %eax
orl %edx, %eax
movq 0x10(%r14), %rcx
movb %al, (%rcx)
leaq 0x127a85(%rip), %rax    # 0x1293e8
movb $0x0, (%rax)
jmp 0x1bac
leaq 0x127a1e(%rip), %rax    # 0x129390
movzwl 0x2(%rax), %ecx
leaq 0x7a03(%rip), %rdx      # 0x9380
movw %cx, 0x46e(%rdx)
movzwl 0x4(%rax), %eax
movb %al, 0x46c(%rdx)
movb %ah, 0x46d(%rdx)
movb $0x0, 0x470(%rdx)
jmp 0x1bac
leaq 0x79d9(%rip), %rax      # 0x9380
movb 0x470(%rax), %cl
movq (%r14), %rdx
movb %cl, (%rdx)
movd 0x46c(%rax), %xmm0
pshuflw $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3,4,5,6,7]
leaq 0x1279ca(%rip), %rcx    # 0x129390
movd %xmm0, 0x2(%rcx)
movb $0x0, 0x470(%rax)
jmp 0x1bac
leaq 0x1279b2(%rip), %rax    # 0x129390
andw $0x0, 0x2(%rax)
jmp 0x1bac
movzwl 0x2(%rbx), %edx
pushq $0x1
popq %rsi
callq 0x1220
movw %ax, 0x2(%rbx)
testw %ax, %ax
jne 0x1bac
movq 0x7968(%rip), %rdi      # 0x9370
callq 0x10e0
andq $0x0, 0x795b(%rip)      # 0x9370
jmp 0x1bac
cmpl $0x15, %edx
je 0x1b23
cmpl $0x17, %edx
je 0x195c
cmpl $0x18, %edx
jne 0x13f7
leaq 0x127954(%rip), %rdx    # 0x129390
andw $0x0, 0xe(%rdx)
incl %esi
movl %esi, 0xc(%r12)
movl %ecx, 0x14(%r12)
leaq 0x792c(%rip), %rcx      # 0x9380
leaq 0x127aa5(%rip), %rdx    # 0x129500
movq %rcx, (%rdx)
andw $0x0, 0x8(%rdx)
leaq 0x12797e(%rip), %rcx    # 0x1293e8
xorl %edx, %edx
movb %dl, (%rcx)
movb %dl, (%rax)
jmp 0x1bac
leaq 0x127914(%rip), %rax    # 0x129390
andw $0x0, (%rax)
movb 0x8(%r12), %cl
movq 0x18(%r15), %rdx
movb %cl, (%rdx)
movb 0xc(%r12), %cl
decb %cl
movq 0x28(%r15), %rdx
movb %cl, (%rdx)
movl 0xc(%r12), %ecx
shrl $0x2, %ecx
andl $0xc0, %ecx
orl 0x14(%r12), %ecx
movq 0x8(%r15), %rdx
movb %cl, (%rdx)
movb 0x10(%r12), %cl
decb %cl
movq 0x30(%r15), %rdx
movb %cl, (%rdx)
movq 0x10(%r15), %rcx
movb $0x1, (%rcx)
andw $0x0, 0xe(%rax)
leaq 0x78af(%rip), %rax      # 0x9380
leaq 0x127a28(%rip), %rcx    # 0x129500
movq %rax, (%rcx)
andw $0x0, 0x8(%rcx)
jmp 0x195c
shll $0x9, %r8d
movslq %r8d, %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x1100
leaq 0x7674(%rip), %rbx      # 0x9170
pushq $0x1
popq %r14
cmpl 0x14(%r12), %ebp
jge 0x1b9b
movq (%r12), %rcx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1220
incl %ebp
jmp 0x1b00
movb $0x1, (%rax)
jmp 0x195c
leal (%rcx,%r8), %eax
addl $0x7fffff, %eax         # imm = 0x7FFFFF
shll $0x9, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1100
movq (%r15), %rax
movq 0x20(%r15), %rsi
leaq 0x127841(%rip), %rbx    # 0x129390
movzwl 0x6(%rbx), %edi
leaq 0x1279a6(%rip), %r14    # 0x129500
movzbl (%rax), %edx
movq (%r12), %rcx
addq (%r14), %rdi
cmpb $0x2, (%rsi)
jne 0x1b91
movl $0x200, %esi            # imm = 0x200
callq 0x11c0
testq %rax, %rax
jne 0x1b9b
movzwl 0x6(%rbx), %edi
addq (%r14), %rdi
movq (%r15), %rax
movzbl (%rax), %edx
shll $0x9, %edx
xorl %esi, %esi
callq 0x1070
jmp 0x1b9b
movl $0x200, %esi            # imm = 0x200
callq 0x1220
leaq 0x127846(%rip), %rax    # 0x1293e8
xorl %ecx, %ecx
movb %cl, (%rax)
movq 0x20(%r15), %rax
movb %cl, (%rax)
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq