2020-09-06 06:15:55 +03:00
|
|
|
section .realmode
|
2020-09-04 02:37:24 +03:00
|
|
|
|
|
|
|
int_08_ticks_counter: dd 0
|
2021-01-17 18:42:15 +03:00
|
|
|
int_08_callback: dd 0
|
2020-09-04 02:37:24 +03:00
|
|
|
|
|
|
|
int_08_isr:
|
|
|
|
bits 16
|
2021-01-17 18:42:15 +03:00
|
|
|
pushf
|
2020-09-04 02:37:24 +03:00
|
|
|
inc dword [cs:int_08_ticks_counter]
|
2021-01-17 18:42:15 +03:00
|
|
|
popf
|
|
|
|
jmp far [cs:int_08_callback]
|
2020-09-04 02:37:24 +03:00
|
|
|
bits 32
|
|
|
|
|
2020-09-28 23:39:28 +03:00
|
|
|
extern getchar_internal
|
|
|
|
|
2021-03-04 15:48:31 +03:00
|
|
|
global _pit_sleep_and_quit_on_keypress
|
|
|
|
_pit_sleep_and_quit_on_keypress:
|
2020-09-04 02:37:24 +03:00
|
|
|
; Hook int 0x08
|
|
|
|
mov edx, dword [0x08*4]
|
2021-01-17 18:42:15 +03:00
|
|
|
mov dword [int_08_callback], edx
|
2020-09-04 02:37:24 +03:00
|
|
|
mov dword [0x08*4], int_08_isr
|
|
|
|
|
|
|
|
; pit_ticks in edx
|
|
|
|
mov edx, dword [esp+4]
|
|
|
|
|
|
|
|
mov dword [int_08_ticks_counter], 0
|
|
|
|
|
2021-03-13 05:21:01 +03:00
|
|
|
; Save GDT in case BIOS overwrites it
|
|
|
|
sgdt [.gdt]
|
|
|
|
|
|
|
|
; Save IDT
|
|
|
|
sidt [.idt]
|
|
|
|
|
|
|
|
; Load BIOS IVT
|
|
|
|
lidt [.rm_idt]
|
|
|
|
|
2020-09-04 02:37:24 +03:00
|
|
|
; Save non-scratch GPRs
|
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
push ebp
|
|
|
|
|
|
|
|
; Jump to real mode
|
|
|
|
jmp 0x08:.bits16
|
|
|
|
.bits16:
|
|
|
|
bits 16
|
|
|
|
mov ax, 0x10
|
|
|
|
mov ds, ax
|
|
|
|
mov es, ax
|
|
|
|
mov fs, ax
|
|
|
|
mov gs, ax
|
|
|
|
mov ss, ax
|
|
|
|
mov eax, cr0
|
|
|
|
and al, 0xfe
|
|
|
|
mov cr0, eax
|
|
|
|
jmp 0x00:.cszero
|
|
|
|
.cszero:
|
|
|
|
xor ax, ax
|
|
|
|
mov ds, ax
|
|
|
|
mov es, ax
|
|
|
|
mov fs, ax
|
|
|
|
mov gs, ax
|
|
|
|
mov ss, ax
|
|
|
|
|
|
|
|
sti
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
cmp dword [int_08_ticks_counter], edx
|
|
|
|
je .timeout
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
push edx
|
|
|
|
mov ah, 0x01
|
|
|
|
xor al, al
|
|
|
|
int 0x16
|
|
|
|
pop edx
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
jz .loop
|
|
|
|
|
|
|
|
; on keypress
|
|
|
|
xor ax, ax
|
|
|
|
int 0x16
|
|
|
|
jmp .done
|
|
|
|
|
|
|
|
.timeout:
|
|
|
|
xor eax, eax
|
|
|
|
|
|
|
|
.done:
|
|
|
|
cli
|
|
|
|
|
2021-03-13 05:21:01 +03:00
|
|
|
; Restore GDT
|
|
|
|
o32 lgdt [ss:.gdt]
|
|
|
|
|
|
|
|
; Restore IDT
|
|
|
|
o32 lidt [ss:.idt]
|
|
|
|
|
2020-09-04 02:37:24 +03:00
|
|
|
; Jump back to pmode
|
|
|
|
mov ebx, cr0
|
|
|
|
or bl, 1
|
|
|
|
mov cr0, ebx
|
|
|
|
jmp 0x18:.bits32
|
|
|
|
.bits32:
|
|
|
|
bits 32
|
|
|
|
mov bx, 0x20
|
|
|
|
mov ds, bx
|
|
|
|
mov es, bx
|
|
|
|
mov fs, bx
|
|
|
|
mov gs, bx
|
|
|
|
mov ss, bx
|
|
|
|
|
|
|
|
; Restore non-scratch GPRs
|
|
|
|
pop ebp
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
|
|
|
|
; Dehook int 0x08
|
2021-01-17 18:42:15 +03:00
|
|
|
mov edx, dword [int_08_callback]
|
2020-09-04 02:37:24 +03:00
|
|
|
mov dword [0x08*4], edx
|
|
|
|
|
2021-05-13 03:56:25 +03:00
|
|
|
test eax, eax
|
|
|
|
jz .out
|
|
|
|
|
2021-03-04 00:47:46 +03:00
|
|
|
xor edx, edx
|
|
|
|
mov dl, ah
|
|
|
|
xor ah, ah
|
2020-09-28 23:39:28 +03:00
|
|
|
push eax
|
2021-03-04 00:47:46 +03:00
|
|
|
push edx
|
2020-09-28 23:39:28 +03:00
|
|
|
call getchar_internal
|
|
|
|
pop edx
|
2021-03-04 00:47:46 +03:00
|
|
|
pop edx
|
2020-09-28 23:39:28 +03:00
|
|
|
|
2021-05-13 03:56:25 +03:00
|
|
|
.out:
|
2020-09-04 02:37:24 +03:00
|
|
|
ret
|
2021-03-13 05:21:01 +03:00
|
|
|
|
|
|
|
.gdt: dq 0
|
|
|
|
.idt: dq 0
|
|
|
|
.rm_idt: dw 0x3ff
|
|
|
|
dd 0
|