add keyboard support

This commit is contained in:
Frater 2026-01-12 09:53:48 +01:00
commit dd22ae44df
6 changed files with 496 additions and 55 deletions

6
.gitignore vendored
View file

@ -1,3 +1,9 @@
build/ build/
.vscode .vscode
.vscode/tasks.json .vscode/tasks.json
build-elf.bat
build.bat
build.sh
run.bat
.gdbinit
.gdb_history

View file

@ -36,15 +36,13 @@
%define BDA_VIDEO_BUF_SIZE 0x004C %define BDA_VIDEO_BUF_SIZE 0x004C
%define BDA_VIDEO_OFS_PAGE 0x004E %define BDA_VIDEO_OFS_PAGE 0x004E
; keyboard related information
%define BDA_KBD_HEAD 0x0080 ; byte
%define BDA_KBD_TAIL 0x0081 ; byte
%define BDA_KBD_FLAGS 0x0082 ; byte: bit0=shift, bit1=ctrl, bit2=alt, bit3=caps, bit4=ext(E0)
%define BDA_KBD_BUF 0x0090 ; buffer circulaire (par ex 32 entrées *2 = 64 bytes)
; 40:50 8 words Cursor position of pages 1-8, high order byte=row low order byte=column; %define KBD_BUF_MASK 0x1F ; 32 entrées => index 0..31
; changing this data isn't reflected immediately on the display
; 40:60 byte Cursor ending (bottom) scan line (don't modify)
; 40:61 byte Cursor starting (top) scan line (don't modify)
; 40:62 byte Active display page number
; 40:63 word Base port address for active 6845 CRT controller 3B4h = mono, 3D4h = color
; 40:65 byte 6845 CRT mode control register value (port 3x8h) ; EGA/VGA values emulate those of the MDA/CGA
; 40:66 byte CGA current color palette mask setting (port 3d9h) ; EGA and VGA values emulate the CGA
; ;
; information relative à la souris ; information relative à la souris
@ -71,3 +69,19 @@
%define BDA_CURSOR_SAVED 0x001A ; flag if image saved %define BDA_CURSOR_SAVED 0x001A ; flag if image saved
%define BDA_CURSOR_PTR 0x001B ; pointeur vers l'image du curseur %define BDA_CURSOR_PTR 0x001B ; pointeur vers l'image du curseur
%define BDA_CURSOR_BG 0x0020 ; 48 bytes max (3*16) %define BDA_CURSOR_BG 0x0020 ; 48 bytes max (3*16)
; -----------------------------------------------------------------------------------
; PC components I/O ports
; -----------------------------------------------------------------------------------
; controleur clavier/souris i8042 (AT-PS/2)
%define PS2_PORT_BUFFER 0x60
%define PS2_PORT_CTRL 0x64
; PIC 8259 ports (Programmable Interrupt Controller)
%define i8259_MASTER_CMD 0x20
%define i8259_MASTER_DATA 0x21
%define i8259_SLAVE_CMD 0xA0
%define i8259_SLAVE_DATA 0xA1

View file

@ -50,6 +50,7 @@ bits 16
%include "./drivers/debug.asm" %include "./drivers/debug.asm"
%include "./drivers/gfx_cgam.asm" %include "./drivers/gfx_cgam.asm"
%include "./drivers/mouse_ps2.asm" %include "./drivers/mouse_ps2.asm"
%include "./drivers/keyboard_ps2.asm"
%include "./services/generic.asm" %include "./services/generic.asm"
err_vganok db 'VGA Not Initialized',0 err_vganok db 'VGA Not Initialized',0
@ -72,6 +73,9 @@ reset:
; installé une table d'interruption "dummy" ; installé une table d'interruption "dummy"
call ivt_setup call ivt_setup
; initialisation des PIC 8259A
call pic_init
sti sti
; load other Rom ; load other Rom

View file

@ -0,0 +1,339 @@
; =============================================================================
; Project : Custom BIOS / ROM
; File : keyboard_ps2.asm
; Author : frater
; Created : 06 jan 2026
;
; License : GNU General Public License v3.0 or later (GPL-3.0+)
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
;
; =============================================================================
; -------------------------------
; scancode set 1 -> ASCII
; index = scancode (0..0x7F)
; 0 = non imprimable
; -------------------------------
kbd_map_nomod:
db 0,
db 27 ; 01 ESC
db '1','2','3','4','5','6','7','8','9','0','-','='
db 8 ; 0E Backspace
db 9 ; 0F Tab
db 'q','w','e','r','t','y','u','i','o','p','[',']'
db 13 ; 1C Enter
db 0 ; 1D Ctrl
db 'a','s','d','f','g','h','j','k','l',';','\'', '`'
db 0 ; 2A LShift
db '\\','z','x','c','v','b','n','m',',','.','/'
db 0 ; 36 RShift
db '*' ; 37 keypad *
db 0 ; 38 Alt
db ' ' ; 39 Space
times (0x80-($-kbd_map_nomod)) db 0
kbd_map_shift:
db 0
db 27
db '!','@','#','$','%','^','&','*','(',')','_','+'
db 8
db 9
db 'Q','W','E','R','T','Y','U','I','O','P','{','}'
db 13
db 0
db 'A','S','D','F','G','H','J','K','L',':','"','~'
db 0
db '|','Z','X','C','V','B','N','M','<','>','?'
db 0
db '*'
db 0
db ' '
times (0x80-($-kbd_map_shift)) db 0
; -----------------------------------------------------------
; Vérifie si une touche est disponible dans le buffer clavier
; Retour: CF=0 si une touche est dispo, CF=1 sinon
; -----------------------------------------------------------
kbd_kbhit:
push ds
mov ax, BDA_SEGMENT
mov ds, ax
mov al, [BDA_KBD_HEAD]
cmp al, [BDA_KBD_TAIL]
pop ds
je .empty
clc
ret
.empty:
stc
ret
; -----------------------------------------------------------
; Lit un caractère du buffer clavier (make+ascii)
; Retour: CF=0 si une touche est dispo, AX=word (scancode<<8 | ascii)
; CF=1 si aucune touche disponible
; -----------------------------------------------------------
kbd_poll:
push ds
mov ax, BDA_SEGMENT
mov ds, ax
call kbd_buf_pop
pop ds
ret
; -----------------------------------------------------------
; Lit un caractère du buffer clavier (make+ascii)
; Bloque jusqu'à ce qu'une touche soit disponible
; Retour: AX=word (scancode<<8 | ascii)
; -----------------------------------------------------------
kbd_getch:
.wait:
call kbd_poll
jc .wait
ret
; -----------------------------------------------------------
; Met à jour les modifiers sur make code
; -----------------------------------------------------------
kbd_init:
push ds
push ax
mov ax, BDA_SEGMENT
mov ds, ax
mov byte [BDA_KBD_HEAD], 0
mov byte [BDA_KBD_TAIL], 0
mov byte [BDA_KBD_FLAGS], 0
; installer INT 09h = IRQ1
cli
xor ax, ax
mov ds, ax
mov si, 0x09*4
mov word [ds:si], kbd_isr
mov ax, cs
mov word [ds:si+2], ax
sti
; unmask IRQ1 (master bit1 = 0)
in al, i8259_MASTER_DATA
and al, 0xFD
out i8259_MASTER_DATA, al
pop ax
pop ds
ret
; -----------------------------------------------------------
; Met à jour les modifiers sur make code
; Entrée: AL = scancode (sans bit7)
; -----------------------------------------------------------
kbd_buf_push:
; In: AX = word à pousser
; DS = 0x40
push bx
push di
mov bl, [BDA_KBD_HEAD]
mov bh, [BDA_KBD_TAIL]
; next_head = (head+1) & MASK
mov dl, bl
inc dl
and dl, KBD_BUF_MASK
; si next_head == tail => buffer plein => drop
cmp dl, bh
je .full
; écrire à BUF[head]
; offset = BDA_KBD_BUF + head*2
xor bh, bh
mov di, bx
shl di, 1
add di, BDA_KBD_BUF
mov [di], ax
; head = next_head
mov [BDA_KBD_HEAD], dl
.full:
pop di
pop bx
ret
; -----------------------------------------------------------
; pop un word du buffer clavier (circulaire)
; -----------------------------------------------------------
kbd_buf_pop:
; Out: CF=1 si vide, sinon AX=word, CF=0
; DS=0x40
push bx
push di
mov bl, [BDA_KBD_HEAD]
mov bh, [BDA_KBD_TAIL]
cmp bl, bh
je .empty
; lire BUF[tail]
xor bh, bh
mov di, bx
shl di, 1
add di, BDA_KBD_BUF
mov ax, [di]
; tail = (tail+1) & MASK
inc bh
and bh, KBD_BUF_MASK
mov [BDA_KBD_TAIL], bh
clc
pop di
pop bx
ret
.empty:
stc
pop di
pop bx
ret
; -----------------------------------------------------------
; Keyboard ISR (INT 09h)
; -----------------------------------------------------------
kbd_isr:
push ds
pusha
mov ax, BDA_SEGMENT
mov ds, ax
in al, PS2_PORT_BUFFER
cmp al, 0xE0
jne .sc
or byte [BDA_KBD_FLAGS], 0x10
jmp .eoi
.sc:
mov bl, al
test bl, 0x80
jz .make
and bl, 0x7F
mov al, bl
call kbd_update_modifiers_break
jmp .clear_eoi
.make:
mov al, bl
call kbd_update_modifiers_make
; filtrer modifiers (ne push pas)
cmp bl, 0x2A
je .clear_eoi
cmp bl, 0x36
je .clear_eoi
cmp bl, 0x1D
je .clear_eoi
cmp bl, 0x38
je .clear_eoi
cmp bl, 0x3A
je .clear_eoi
; scancode -> ASCII
mov al, bl
xor bh, bh
test byte [BDA_KBD_FLAGS], 0x01
jz .nomod
mov si, kbd_map_shift
jmp .doxlat
.nomod:
mov si, kbd_map_nomod
.doxlat:
; XLAT uses DS:BX, so set BX = scancode and DS points to table.
; On ne veut pas changer DS=0x40. Donc on ne peut pas XLAT directement ici.
; => on fait un accès indexé.
; AL = [CS:SI + scancode]
push ds
mov ax, cs
mov ds, ax
xor ah, ah
mov di, si
add di, bx
mov al, [di]
pop ds
; AX = (scancode<<8) | ascii
mov ah, bl
call kbd_buf_push
.clear_eoi:
and byte [BDA_KBD_FLAGS], 0xEF ; clear ext
.eoi:
mov al, i8259_MASTER_CMD
out i8259_MASTER_CMD, al
popa
pop ds
iret
; -----------------------------------------------------------
; Met à jour les modifiers sur make code
; Entrée: AL = scancode (sans bit7)
; -----------------------------------------------------------
kbd_update_modifiers_make:
; AL = scancode (make)
cmp al, 0x2A
je .shift_on
cmp al, 0x36
je .shift_on
cmp al, 0x1D
je .ctrl_on
cmp al, 0x38
je .alt_on
cmp al, 0x3A
je .caps_toggle
ret
.shift_on:
or byte [BDA_KBD_FLAGS], 0x01
ret
.ctrl_on:
or byte [BDA_KBD_FLAGS], 0x02
ret
.alt_on:
or byte [BDA_KBD_FLAGS], 0x04
ret
.caps_toggle:
xor byte [BDA_KBD_FLAGS], 0x08
ret
kbd_update_modifiers_break:
; AL = scancode (break)
cmp al, 0x2A
je .shift_off
cmp al, 0x36
je .shift_off
cmp al, 0x1D
je .ctrl_off
cmp al, 0x38
je .alt_off
ret
.shift_off:
and byte [BDA_KBD_FLAGS], 0xFE
ret
.ctrl_off:
and byte [BDA_KBD_FLAGS], 0xFD
ret
.alt_off:
and byte [BDA_KBD_FLAGS], 0xFB
ret

View file

@ -21,12 +21,6 @@
; ;
; ============================================================================= ; =============================================================================
;
; controleur de souris via le i8042 (PC Classique)
;
%define PS2_PORT_BUFFER 0x60
%define PS2_PORT_CTRL 0x64
; commandes ; commandes
%define MOUSE_CMD_RESET 0xFF %define MOUSE_CMD_RESET 0xFF
%define MOUSE_CMD_DEFAULT 0xF6 %define MOUSE_CMD_DEFAULT 0xF6
@ -173,10 +167,26 @@ mouse_init:
mov word [ds:si], mouse_handler mov word [ds:si], mouse_handler
add si, 2 add si, 2
mov word [ds:si], BDA_SEGMENT mov ax, cs
mov word [ds:si], cs
; test: aussi installer sur 0x2C (si PIC remappé base 0x28)
;mov si, 0x2C*4
;xor ax, ax
;mov ds, ax
;mov word [ds:si], mouse_handler
;add si, 2
;mov ax, cs
;mov word [ds:si], ax
sti sti
mov dx, 0x00E9
mov al, '!'
out dx, al
pop ax pop ax
pop ds pop ds
ret ret
@ -274,10 +284,10 @@ mouse_sendcmd:
out PS2_PORT_CTRL, al out PS2_PORT_CTRL, al
call ps2_wait_ready_write call ps2_wait_ready_write
mov al, bl mov al, bl
out PS2_PORT_BUFFER, al out PS2_PORT_BUFFER, al
call ps2_wait_ready_read ; AL = réponse call ps2_read ; AL = réponse
cmp al, MOUSE_ACK cmp al, MOUSE_ACK
jne .bad jne .bad
clc clc
@ -305,51 +315,70 @@ mouse_sendcmd:
; bit 7 = Y overflow ; bit 7 = Y overflow
; ------------------------------------------------------------ ; ------------------------------------------------------------
mouse_handler: mouse_handler:
pusha
push ds
; vider le byte qui a déclenché IRQ12
in al, PS2_PORT_BUFFER
mov dx, 0x00E9 ; debugcon
mov al, '*'
out dx, al ; imprime directement
; EOI PIC (slave puis master)
mov al, 0x20
out 0xA0, al
out 0x20, al
pop ds
popa
iret
; sauvegarder tout les registres ; sauvegarder tout les registres
pusha pusha
push ds
; use BDA segment ; use BDA segment
mov ax,BDA_MOUSE_SEG mov ax,BDA_MOUSE_SEG
mov ds,ax mov ds,ax
in al, PS2_PORT_BUFFER ; lire octet souris ; lire un octet depuis le contrôleur
; et le stocker dans le buffer
in al, PS2_PORT_BUFFER ; lire octet souris
mov byte [BDA_MOUSE_BUFFER + BDA_MOUSE_IDX], al mov byte [BDA_MOUSE_BUFFER + BDA_MOUSE_IDX], al
inc byte [BDA_MOUSE_IDX] inc byte [BDA_MOUSE_IDX]
; vérifier si le packet est complet
mov al, [DBA_MOUSE_PACKETLEN] mov al, [DBA_MOUSE_PACKETLEN]
cmp [BDA_MOUSE_IDX], al cmp byte [BDA_MOUSE_IDX], al
jne .done jne .done
; packet complet, on decode les données
mov byte [BDA_MOUSE_IDX], 0 mov byte [BDA_MOUSE_IDX], 0
mov al,'*' ; pour debug
call debug_putc
; decode buffer manuallement
mov al, [BDA_MOUSE_BUFFER] mov al, [BDA_MOUSE_BUFFER] ; status
mov [BDA_MOUSE_STATUS], al mov [BDA_MOUSE_STATUS], al
mov al, [BDA_MOUSE_BUFFER+1] mov al, [BDA_MOUSE_BUFFER+1] ; delta X
cbw cbw
add [BDA_MOUSE_X], ax add [BDA_MOUSE_X], ax
mov al, [BDA_MOUSE_BUFFER+2] mov al, [BDA_MOUSE_BUFFER+2] ; delta Y
cbw cbw
sub [BDA_MOUSE_Y], ax sub [BDA_MOUSE_Y], ax
; si packetlen = 4, gérer la molette; experimental ; si packetlen = 4, gérer la molette; experimental
mov al, [BDA_MOUSE_BUFFER+3] mov al, [BDA_MOUSE_BUFFER+3] ; delta Wheel
cbw cbw
add word [BDA_MOUSE_WHEEL], ax add word [BDA_MOUSE_WHEEL], ax
; debug: afficher les valeurs
mov si, BDA_MOUSE_BUFFER
mov cx, 4
.debug_loop:
mov al, [si]
call debug_puthex8
mov al, ' '
call debug_putc
inc si
loop .debug_loop
; clamp X ; clamp X
cmp word [BDA_MOUSE_X], 0 cmp word [BDA_MOUSE_X], 0
jge .x_ok_low jge .x_ok_low
@ -383,5 +412,6 @@ mouse_handler:
out 0x20, al ; EOI PIC maître out 0x20, al ; EOI PIC maître
; restaurer tous les registres ; restaurer tous les registres
pop ds
popa popa
iret iret

View file

@ -20,6 +20,8 @@
; ;
; ============================================================================= ; =============================================================================
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Détection RAM conventionnelle ; Détection RAM conventionnelle
; Teste la RAM de MEM_SEG_DEB -> MEM_SEG_END (0xA000, 640KB), par pas de 1KB. ; Teste la RAM de MEM_SEG_DEB -> MEM_SEG_END (0xA000, 640KB), par pas de 1KB.
@ -201,6 +203,52 @@ ivt_setvector:
pop es pop es
ret ret
; ------------------------------------------------------------
; Initialise les PIC 8259 (PC/AT):
; - Master offset 0x08
; - Slave offset 0x70
; - Cascade: IRQ2
; https://wiki.nox-rhea.org/back2root/ibm-pc-ms-dos/hardware/8259
; ------------------------------------------------------------
pic_init:
push ax
; ICW1: init + ICW4
mov al, 0x11 ; 000010001b
out i8259_MASTER_CMD, al
out i8259_SLAVE_CMD, al
; ICW2: vector offsets
mov al, 0x08
out i8259_MASTER_DATA, al
mov al, 0x70
out i8259_SLAVE_DATA, al
; ICW3: wiring
mov al, 0x04 ; master: slave on IRQ2: 0100b
out i8259_MASTER_DATA, al
mov al, 0x02 ; slave: cascade identity 2
out i8259_SLAVE_DATA, al
; ICW4: 8086 mode
mov al, 0x01
out i8259_MASTER_DATA, al
out i8259_SLAVE_DATA, al
; Masques: unmask IRQ2 (master) et IRQ12 (slave)
in al, i8259_MASTER_DATA
and al, 0xFB ; clear bit2
out i8259_MASTER_DATA, al
in al, i8259_SLAVE_DATA
and al, 0xEF ; clear bit4
out i8259_SLAVE_DATA, al
pop ax
ret
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Handler par défaut: fait juste IRET ; Handler par défaut: fait juste IRET
; IMPORTANT: doit être FAR (appelé par le CPU via IVT), et terminer par IRET ; IMPORTANT: doit être FAR (appelé par le CPU via IVT), et terminer par IRET