handle commands coming from virtual keyboard

This commit is contained in:
Tibo 2021-10-07 18:03:32 +02:00
parent 127b32237b
commit a97e70ee99

View file

@ -63,9 +63,14 @@ int 0x10
.loop: .loop:
;; read a character, ASCII in al ;; read a character, ASCII in al
mov ah, 0x00 ; Read character mov ah, 0x0 ; Read character
int 0x16 int 0x16
;; a null character will be sent before every command from the virtual keyboard
;; so if a null is detected go to the virtual_kbd_loop instead
cmp al, 0x0 ; null
je .virtual_kbd_loop_init
mov ah, 0x1 mov ah, 0x1
mov dx, 0x0 mov dx, 0x0
int 0x14 int 0x14
@ -81,10 +86,33 @@ int 0x10
jmp nonewline ; all other characters jmp nonewline ; all other characters
.nl_reset_state:
mov byte[is_from_virtual_kbd], 0x0 ; reset state variable
.nl: .nl:
call newline call newline
jmp .loop jmp .loop
.virtual_kbd_loop_init:
mov byte[is_from_virtual_kbd], 0x1 ; update state variable
.virtual_kbd_loop:
;; read a character, ASCII in al
mov ah, 0x0
int 0x16
cmp al, 0x08 ; backspace
je change_color_mode
cmp al, 0x1b ; esc
je beep
;; an enter character signifies the end of a command
;; so a newline should get printed and then the program should resume
;; from the main .loop again
cmp al, 0xD ; enter
je .nl_reset_state
jmp nonewline ; all other characters
;; Update the color_mode variable with a byte from the keyboard ;; Update the color_mode variable with a byte from the keyboard
;; ;;
;; CLOBBERS ;; CLOBBERS
@ -94,7 +122,7 @@ change_color_mode:
call readbyte ; get byte from keyboard (in al) call readbyte ; get byte from keyboard (in al)
mov [color_mode], al ; store byte in color_mode variable mov [color_mode], al ; store byte in color_mode variable
jmp start.loop jmp compare_virtual_and_jump
;; Simulates going to a new line ;; Simulates going to a new line
;; ;;
@ -181,7 +209,7 @@ beep:
and al, 11111100b ; Clear bits 1 and 0 (disable speaker, clear channel select) and al, 11111100b ; Clear bits 1 and 0 (disable speaker, clear channel select)
out 61h, al ; Send new value out 61h, al ; Send new value
jmp start.loop jmp compare_virtual_and_jump
;; Handles all other characters by writing them to the screen ;; Handles all other characters by writing them to the screen
;; ;;
@ -212,7 +240,7 @@ nonewline:
.eol: .eol:
call newline call newline
jmp start.loop jmp compare_virtual_and_jump
.noeol: .noeol:
;; Move cursor forward ;; Move cursor forward
@ -220,6 +248,15 @@ nonewline:
mov ah, 0x02 ; set cursor position, dh (row) already set, dl (col) updated on prev line mov ah, 0x02 ; set cursor position, dh (row) already set, dl (col) updated on prev line
int 0x10 int 0x10
jmp compare_virtual_and_jump
;; Compares the is_from_virtual_kbd state variable to 1 to check if the command
;; came from the virtual keyboard
;; if it did, jump to the virtual keyboard loop
;; if it didn't, jump to the main loop
compare_virtual_and_jump:
cmp byte[is_from_virtual_kbd], 0x1
je start.virtual_kbd_loop
jmp start.loop jmp start.loop
;; Reads two characters from the keyboard buffer and converts them into a single byte ;; Reads two characters from the keyboard buffer and converts them into a single byte
@ -273,6 +310,8 @@ read_duration:
;; Variable to store color mode information for writing to the screen ;; Variable to store color mode information for writing to the screen
color_mode: db 0x02 color_mode: db 0x02
;; State variable for detecting whether keystrokes are virtual or real
is_from_virtual_kbd: db 0x0
;; Magic number must be in the last 2 bytes of the sector so ;; Magic number must be in the last 2 bytes of the sector so
;; fill the binary with zeroes until it is 510 bytes in size ;; fill the binary with zeroes until it is 510 bytes in size