This commit is contained in:
Frater 2026-01-19 09:50:33 +01:00
commit 9cb9033a25
6 changed files with 220 additions and 358 deletions

3
.gitignore vendored
View file

@ -9,6 +9,5 @@ build.sh
run.bat
.gdbinit
.gdb_history
link.ld
.gitignore
*.ld

View file

@ -75,15 +75,35 @@
; -----------------------------------------------------------------------------------
; controleur clavier/souris i8042 (AT-PS/2)
%define PS2_PORT_BUFFER 0x60
%define PS2_PORT_CTRL 0x64
%define i8042_PS2_DATA 0x60
%define i8042_PS2_CTRL 0x64
; PIC 8259 ports (Programmable Interrupt Controller)
; PIC 1 : Master
%define i8259_MASTER_CMD 0x20
%define i8259_MASTER_DATA 0x21
; PIC 2 : Slave
%define i8259_SLAVE_CMD 0xA0
%define i8259_SLAVE_DATA 0xA1
; i8259 Commands
%define ICW1_ICW4 0x01 ; Indicates that ICW4 will be present
%define ICW1_SINGLE 0x02 ; Single (cascade) mode
%define ICW1_INTERVAL4 0x04 ; Call address interval 4 (8)
%define ICW1_LEVEL 0x08 ; Level triggered (edge) mode
%define ICW1_INIT 0x10 ; Initialization - required!
%define ICW4_8086 0x01 ; 8086/88 (MCS-80/85) mode
%define ICW4_AUTO 0x02 ; Auto (normal) EOI
%define ICW4_BUF_SLAVE 0x08 ; Buffered mode/slave
%define ICW4_BUF_MASTER 0x0C ; Buffered mode/master
%define ICW4_SFNM 0x10 ; Special fully nested (not)
%define PIC_EOI 0x20
; Remap : IRQ0..7 -> 0x08..0x0F, IRQ8..15 -> 0x70..0x77
%define i8259_MASTER_INT 0x08
%define i8259_SLAVE_INT 0x70
; -----------------------------------------------------------------------------------
; bda_setup
; Initialise la BDA à zéro

View file

@ -47,8 +47,8 @@ bits 16
%include "./services/macros.asm"
%include "./drivers/debug.asm"
%include "./drivers/gfx_cgam.asm"
%include "./drivers/mouse_ps2.asm"
%include "./drivers/keyboard_ps2.asm"
;%include "./drivers/mouse_ps2.asm"
;%include "./drivers/keyboard_ps2.asm"
%include "./services/generic.asm"
err_vganok db 'VGA Not Initialized',0
@ -63,6 +63,8 @@ reset:
; detection de la mémoire totale
call ram_setup
sti
; modification du stack en "haut" de la RAM
mov ax, dx
sub ax, 0x0400 ; réserver les 16 dernier KB
@ -75,41 +77,60 @@ reset:
; installé une table d'interruption "dummy"
call ivt_setup
; initialisation des PIC 8259A
call pic_init
sti
; load Roms
call setup_load_rom
; on vérifie que le BIOS VGA a installé une INT 10h
call setup_check_vga
; on active le mode graphique
; call gfx_init
call kbd_init
call mouse_reset
call mouse_init
; on initialise le mode texte 80x25 par défaut DEBUG
mov ax, 0x0003
int 10h
cli
; initialisation des PIC 8259A
call pic_init
; call kbd_init
; test IRQ 0
mov ax,cs
mov dx,ax
mov bx,test_isr
mov ax,i8259_MASTER_INT
call ivt_setvector
sti
; on active le mode graphique
; call gfx_init
;call mouse_reset
;call mouse_init
endless:
mov ax,0x0050
mov ds,ax
; in al, 0x64
; test al, 1 ; OBF
; jz .no
; in al, 0x60 ; scancode dans AL
; mov byte [0x0001],al
;.no:
mov dx,0
call scr_gotoxy
mov ax,BDA_MOUSE_SEG
mov ds,ax
mov ax,0 ; adresse début dump
mov si,ax
mov cx, 0x10
mov cx, 0x20
.dump:
test cx,0x000F
jnz .sameline
inc dh
xor dl,dl
call scr_gotoxy
.sameline:
mov al,[ds:si]
inc si
@ -127,6 +148,26 @@ endless:
jmp endless
; -----------------------------------------------------------
; Keyboard ISR (IRQ -> INT)
; -----------------------------------------------------------
test_isr:
pusha
push ds
mov ax,0x0050
mov ds,ax
inc byte [0x0005]
mov al, PIC_EOI ; EOI master
out i8259_MASTER_CMD, al
pop ds
popa
iret
; ------------------------------------------------------------------
; Padding jusqu'au reset vector
; ------------------------------------------------------------------

View file

@ -21,335 +21,127 @@
;
; =============================================================================
; -------------------------------
; 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',';',0x27, 0x60
db 0 ; 2A LShift
db 0x5c,'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
kbc_wait_in_empty:
in al, i8042_PS2_CTRL
test al, 0x02 ; IBF
jnz kbc_wait_in_empty
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
kbc_wait_out_full:
in al, i8042_PS2_CTRL
test al, 0x01 ; OBF
jz kbc_wait_out_full
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
kbc_flush_output:
in al, i8042_PS2_CTRL
test al, 0x01 ; OBF ?
jz .done
in al, i8042_PS2_DATA; jette la donnée
jmp kbc_flush_output
.done:
ret
; -----------------------------------------------------------
; Met à jour les modifiers sur make code
; -----------------------------------------------------------
kbc_read_cmdbyte:
call kbc_wait_in_empty
mov al, 0x20
out i8042_PS2_CTRL, al
call kbc_wait_out_full
in al, i8042_PS2_DATA
ret
kbc_write_cmdbyte:
; entrée: AL = nouveau command byte
push ax
call kbc_wait_in_empty
mov al, 0x60
out i8042_PS2_CTRL, al
call kbc_wait_in_empty
pop ax
out i8042_PS2_DATA, al
ret
kbd_init:
push ds
push ax
; gestion du buffer clavier
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
; call kbc_wait_in_empty
; mov al, 0x20
; out i8042_PS2_CTRL, al
;
; call kbc_wait_out_full
; in al, i8042_PS2_DATA ; AL = command byte
;
; or al, 0x01 ; bit0: enable IRQ1
; and al, 0xEF ; bit4: 0 = keyboard enabled
;
; mov ah, al ; save new cmd byte
;
; ; write command byte
; call kbc_wait_in_empty
; mov al, 0x60
; out i8042_PS2_CTRL, al
;
; call kbc_wait_in_empty
; mov al, ah
; out i8042_PS2_DATA, al
;
; ; unmask IRQ1 & IRQ2 (master bit 1 & 2 = 0) bit 1 = IRQ 1
; in al, i8259_MASTER_DATA
; and al, 0xF9 ; 11111001b
; out i8259_MASTER_DATA, al
; setup Interrupt Table
; installer IRQ1 : MASTER_OFFSET + 1
mov ax, i8259_MASTER_INT ; base offset IRQ0
inc ax ; IRQ 1
shl ax,2 ; vect * 4
mov si, ax
xor ax, ax ; segment 0x0000
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
mov si, 0x09*4
xor ax, ax
mov ds, ax
mov word [ds:si], kbd_isr
mov ax, cs
mov word [ds:si+2], ax
sti
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)
; Keyboard ISR (IRQ -> INT)
; -----------------------------------------------------------
kbd_isr:
pusha
in al, 0x60 ; consomme scancode (obligatoire)
mov dx, 0x00E9 ; debugcon
mov al, '*'
out dx, al
mov al, 0x20 ; EOI master
out 0x20, al
popa
iret
; kbd_isr:
push ds
pusha
push ds
mov ax, BDA_SEGMENT
mov ds, ax
in al, 0x60 ; consomme scancode (obligatoire)
in al, PS2_PORT_BUFFER
cmp al, 0xE0
jne .sc
or byte [BDA_KBD_FLAGS], 0x10
jmp .eoi
mov ax,0x0050
mov ds,ax
inc byte [0x0000]
.sc:
mov bl, al
mov al, PIC_EOI ; EOI master
out i8259_MASTER_CMD, 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
pop ds
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
iret

View file

@ -118,12 +118,12 @@ mouse_init:
; Activer le port souris (AUX)
call ps2_wait_ready_write
mov al, I8042_CMD_EN_AUX
out PS2_PORT_CTRL, al
out i8042_PS2_CTRL, al
; Lire command byte
call ps2_wait_ready_write
mov al, I8042_CMD_RD_CBYTE
out PS2_PORT_CTRL, al
out i8042_PS2_CTRL, al
call ps2_read ; AL = command byte
; Activer IRQ12 (bit 1)
@ -133,10 +133,10 @@ mouse_init:
; Réécrire command byte (commande 0x60 envoyée à 0x64)
call ps2_wait_ready_write
mov al, I8042_CMD_WR_CBYTE
out PS2_PORT_CTRL, al
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, ah
out PS2_PORT_BUFFER, al
out i8042_PS2_DATA, al
call ps2_flush_output
@ -230,7 +230,7 @@ mouse_detect_packet_len:
; attendre que le 8042 soit pret a recevoir de l'information
ps2_wait_ready_write:
.wait:
in al, PS2_PORT_CTRL
in al, i8042_PS2_CTRL
test al, 02h ; IBF
jnz .wait
ret
@ -238,7 +238,7 @@ ps2_wait_ready_write:
; attendre que le 8042 soit pret a lire de l'information
ps2_wait_ready_read:
.wait:
in al, PS2_PORT_CTRL
in al, i8042_PS2_CTRL
test al, 01h ; OBF
jz .wait
ret
@ -246,16 +246,16 @@ ps2_wait_ready_read:
; lire de l'information depuis le 8042
ps2_read:
call ps2_wait_ready_read
in al, PS2_PORT_BUFFER
in al, i8042_PS2_DATA
ret
; vide le buffer interne du 8042 (information(s) ignorée(s))
ps2_flush_output:
.flush:
in al, PS2_PORT_CTRL
in al, i8042_PS2_CTRL
test al, 01h
jz .done
in al, PS2_PORT_BUFFER
in al, i8042_PS2_DATA
jmp .flush
.done:
ret
@ -271,11 +271,11 @@ mouse_sendcmd:
call ps2_wait_ready_write
mov al, I8042_CMD_WRITE_AUX
out PS2_PORT_CTRL, al
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, bl
out PS2_PORT_BUFFER, al
out i8042_PS2_DATA, al
call ps2_read ; AL = réponse
cmp al, MOUSE_ACK
@ -309,7 +309,7 @@ mouse_handler:
push ds
; vider le byte qui a déclenché IRQ12
in al, PS2_PORT_BUFFER
in al, i8042_PS2_DATA
mov dx, 0xE9 ; debugcon
mov al, '*'
@ -324,10 +324,6 @@ mouse_handler:
popa
iret
; sauvegarder tout les registres
pusha
push ds
@ -338,7 +334,7 @@ mouse_handler:
; lire un octet depuis le contrôleur
; et le stocker dans le buffer
in al, PS2_PORT_BUFFER ; lire octet souris
in al, i8042_PS2_DATA ; lire octet souris
mov byte [BDA_MOUSE_BUFFER + BDA_MOUSE_IDX], al
inc byte [BDA_MOUSE_IDX]

View file

@ -203,55 +203,69 @@ ivt_setvector:
pop es
ret
; ------------------------------------------------------------
io_wait:
xor al,al
out 0x80,al
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
; starts the initialization sequence (in cascade mode)
mov al, ICW1_INIT | ICW1_ICW4
out i8259_MASTER_CMD, al
call io_wait
mov al, ICW1_INIT | ICW1_ICW4
out i8259_SLAVE_CMD, al
call io_wait
; 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
mov al, i8259_MASTER_INT
out i8259_MASTER_DATA, al ; ICW2: Master PIC vector offset
call io_wait
mov al, i8259_SLAVE_INT
out i8259_SLAVE_DATA, al ; ICW2: Slave PIC vector offset
call io_wait
; Master: cascade (IRQ2 => 1 << IRQ2 = 0x04)
mov al, 0x04 ; master: slave on IRQ2: 0100b
out i8259_MASTER_DATA, al ; ICW3: tell Master PIC that there is a slave PIC at IRQ2
call io_wait
; Slave: numéro de ligne sur master (2)
mov al, 0x02 ; slave: cascade identity 2
out i8259_SLAVE_DATA, al ; ICW3: tell Slave PIC its cascade identity (0000 0010)
call io_wait
; ICW4: 8086 mode
mov al, 0x01
mov al, ICW4_8086
out i8259_MASTER_DATA, al
call io_wait
mov al, ICW4_8086
out i8259_SLAVE_DATA, al
call io_wait
; Masques: unmask IRQ2 (master) et IRQ12 (slave)
in al, i8259_MASTER_DATA
and al, 0xFB ; clear bit2
; Unmask both PICs.
xor al,al
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
; IMPORTANT: doit être FAR (appelé par le CPU via IVT), et terminer par IRET
; ---------------------------------------------------------------------------
default_isr:
iret
iret