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
|
|
|
|
|
2022-02-19 19:19:25 +03:00
|
|
|
mov byte [.mods], 0
|
|
|
|
mov byte [.ascii], 0
|
|
|
|
mov byte [.scan], 0
|
|
|
|
|
2020-09-04 02:37:24 +03:00
|
|
|
.loop:
|
|
|
|
cmp dword [int_08_ticks_counter], edx
|
2022-02-19 19:19:25 +03:00
|
|
|
je .done
|
2020-09-04 02:37:24 +03:00
|
|
|
|
|
|
|
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
|
2022-02-19 19:19:25 +03:00
|
|
|
mov byte [.ascii], al
|
|
|
|
mov byte [.scan], ah
|
2020-09-04 02:37:24 +03:00
|
|
|
|
2022-02-19 19:19:25 +03:00
|
|
|
mov ax, 0x0200
|
|
|
|
int 0x16
|
|
|
|
test al, 0x04
|
|
|
|
jz .done
|
|
|
|
|
|
|
|
; ctrl handling
|
|
|
|
mov byte [.mods], 0x04
|
|
|
|
add byte [.ascii], 0x60
|
2020-09-04 02:37:24 +03:00
|
|
|
|
|
|
|
.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
|
|
|
|
|
2022-02-21 00:55:52 +03:00
|
|
|
cmp byte [.scan], 0
|
2022-02-19 19:19:25 +03:00
|
|
|
je .fail
|
2021-05-13 03:56:25 +03:00
|
|
|
|
2022-02-19 19:19:25 +03:00
|
|
|
push dword [.mods]
|
|
|
|
push dword [.ascii]
|
|
|
|
push dword [.scan]
|
2020-09-28 23:39:28 +03:00
|
|
|
call getchar_internal
|
2022-02-19 19:19:25 +03:00
|
|
|
add esp, 3*4
|
2020-09-28 23:39:28 +03:00
|
|
|
|
2022-02-19 19:19:25 +03:00
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
|
|
|
xor eax, eax
|
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
|
2022-02-19 19:19:25 +03:00
|
|
|
|
|
|
|
.mods: dd 0
|
|
|
|
.ascii: dd 0
|
|
|
|
.scan: dd 0
|
2023-09-28 12:15:33 +03:00
|
|
|
|
|
|
|
section .note.GNU-stack noalloc noexec nowrite progbits
|