add generic functions & affine mouse drivers

This commit is contained in:
Frater 2026-01-08 21:36:51 +01:00
commit 892da28db7
6 changed files with 443 additions and 235 deletions

View file

@ -34,7 +34,6 @@
%define CGA_ODD_BANK 0x2000
%define BG16_SIZE 48
; ------------------------------------------------------------
; initialise le mode graphique (via l'int 10h)
;
@ -42,7 +41,7 @@
; ------------------------------------------------------------
gfx_init:
; init graphics mode
mov ah, 0x00 ; AH=00h set video mode
mov ah, 0x00 ; AH=00h set video mode
mov al, GFX_MODE
int 0x10
@ -56,46 +55,47 @@ gfx_init:
; In:
; CX = x (0..639)
; DX = y (0..199)
; AL = color (0=black, !=0=white)
; BL = color (0=black, !=0=white)
;
; ------------------------------------------------------------
gfx_putpixel:
push bx
push ax
push di
push es
mov ax, VIDEO_SEG
mov es,ax
mov ax, VIDEO_SEG
mov es, ax
; calcul de l'offset 'y':
; si y est impaire, DI+=0x2000
; DI = (y>>1)*80 + (x>>3) + (y&1)*0x2000
mov ax, dx
shr ax, 1 ; ax = y/2
; DI = (y>>1)*80 + (x>>3) + (y&1)*0x2000
mov bx, dx
and bx, 1 ; BX = y&1
mov di, ax ; di = y/2
shl di, 4 ; di = (y/2)*16
mov ax, dx
shr ax, 1 ; AX = y>>1
mov di, ax
mov ax, di
mov di, CGA_STRIDE
mul di ; DX:AX = (y>>1)*80
mov di, ax
test bx, 1
jz .bank_ok
shl ax, 6 ; ax = (y/2)*64
add di, ax ; di = (y/2)*(16+64) = *80
mov ax, cx ; x
shr ax, 3 ; AX = x/8
add di, ax
; ligne paire/impaire
test dl, 1
jz .ligne_paire
add di, CGA_ODD_BANK
.bank_ok:
mov ax, cx
shr ax, 3 ; AX = x>>3
add di, ax
.ligne_paire:
; masque bit = 0x80 >> (x&7)
mov bl, cl
and bl, 7 ; BL = x&7
and cl, 7 ; cl = x&7
mov ah, 080h
mov cl, bl
shr ah, cl ; AH = bitmask
shr ah, cl ; AH = bitmask
; write
cmp al, 0
cmp bl, 0
je .clear
.set:
@ -109,7 +109,7 @@ gfx_putpixel:
.done:
pop es
pop di
pop bx
pop ax
ret
; ------------------------------------------------------------

View file

@ -25,14 +25,24 @@
;
; controleur de souris via le i8042 (PC Classique)
;
%define BDA_SEGMENT 0x0040
%define PS2_PORT_BUFFER 0x60
%define PS2_PORT_CTRL 0x64
%define PS2_PORT_BUFFER 0x60
%define PS2_PORT_CTRL 0x64
%define MOUSE_CMD_RESET 0xff
%define MOUSE_CMD_DEFAULT 0xf6
%define MOUSE_EN_STREAM 0xf4
%define MOUSE_DIS_STREAM 0xf5
%define MOUSE_CMD_RESET 0xFF
%define MOUSE_CMD_DEFAULT 0xF6
%define MOUSE_EN_STREAM 0xF4
%define MOUSE_DIS_STREAM 0xF5
%define MOUSE_GET_ID 0xF2
%define MOUSE_SET_RATE 0xF3
%define MOUSE_ACK 0xFA
%define I8042_CMD_EN_AUX 0xA8
%define I8042_CMD_RD_CBYTE 0x20
%define I8042_CMD_WR_CBYTE 0x60
%define I8042_CMD_WRITE_AUX 0xD4
mouse_arrow:
dw 1001111111111111b ; 0x9FFF
@ -68,6 +78,10 @@ mouse_arrow:
dw 0000000110000000b ; 0x0180
dw 0000000000000000b ; 0x0000
; ------------------------------------------------------------
; initialise le i8042 keyboard and mouse (PS/2)
;
; ------------------------------------------------------------
mouse_init:
; Activer le port souris
mov ax, BDA_SEGMENT
@ -76,43 +90,169 @@ mouse_init:
; effacer les variables du drivers
mov [BDA_MOUSE_IDX],0
mov dword [BDA_MOUSE_BUFFER],0
mov byte [DBA_MOUSE_PACKETLEN],3
mov al, 0xA8
out PS2_PORT_CTRL, al
; Lire le command byte
mov al, 0x20
out PS2_PORT_CTRL, al
in al, PS2_PORT_BUFFER
call ps2_flush_output
; Activer le port souris (AUX)
call ps2_wait_ready_write
mov al, I8042_CMD_EN_AUX
out PS2_PORT_CTRL, al
; Lire command byte
call ps2_wait_ready_write
mov al, I8042_CMD_RD_CBYTE
out PS2_PORT_CTRL, al
call ps2_read ; AL = command byte
; Activer IRQ12 (bit 1)
or al, 0x02
or al, 02h
mov ah, al
; Réécrire le command byte
mov ah, al
mov al, PS2_PORT_BUFFER
out PS2_PORT_CTRL, al
mov al, ah
out PS2_PORT_BUFFER, al
; 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
call ps2_wait_ready_write
mov al, ah
out PS2_PORT_BUFFER, al
call ps2_flush_output
; --- RESET souris: FA, AA, ID
mov bl, MOUSE_CMD_RESET
call mouse_sendcmd
call ps2_read ; 0xAA attendu (self-test OK)
call ps2_read ; ID (souvent 0x00)
; Defaults: FA
mov bl, MOUSE_CMD_DEFAULT
call mouse_sendcmd
; Enable streaming: FA
mov bl, MOUSE_EN_STREAM
call mouse_sendcmd
; (option) détecter 3/4 bytes via F3 200/100/80 + F2
; call mouse_detect_packet_len
; installer ISR IRQ12 (INT 74h)
; call mouse_install_isr
; installer le handler
ret
mouse_detect_packet_len:
mov byte [DBA_MOUSE_PACKETLEN], 3
; envoyer une commande souris
mouse_sendcmd:
; F3 200
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 200
call mouse_sendcmd
; F3 100
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 100
call mouse_sendcmd
; F3 80
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 80
call mouse_sendcmd
; F2 -> ID
mov bl, MOUSE_GET_ID
call mouse_sendcmd
call ps2_read ; AL = ID
cmp al, 03h
je .is4
cmp al, 04h
je .is4
ret
.is4:
mov byte [DBA_MOUSE_PACKETLEN], 4
ret
; ------------------------------------------------------------
; fonction de gestion du i8042
;
; ------------------------------------------------------------
; attendre que le 8042 soit pret a recevoir de l'information
ps2_wait_ready_write:
.wait:
in al, PS2_PORT_CTRL
test al, 2
jnz .wait
mov al, 0xD4
out PS2_PORT_CTRL, al
in al, PS2_PORT_CTRL
test al, 02h ; IBF
jnz .wait
ret
.wait2:
in al, PS2_PORT_CTRL
test al, 2
jnz .wait2
mov al, bl ; BL = commande souris
out PS2_PORT_BUFFER, al
; attendre que le 8042 soit pret a lire de l'information
ps2_wait_ready_read:
.wait:
in al, PS2_PORT_CTRL
test al, 01h ; OBF
jz .wait
ret
; lire de l'information depuis le 8042
ps2_read:
call ps2_wait_ready_read
in al, PS2_PORT_BUFFER
ret
; vide le buffer interne du 8042 (information(s) ignorée(s))
ps2_flush_output:
.flush:
in al, PS2_PORT_CTRL
test al, 01h
jz .done
in al, PS2_PORT_BUFFER
jmp .flush
.done:
ret
; ------------------------------------------------------------
; envoye une commande souris
;
; BL = commande souris (ou data après une commande F3)
;
; renvoi CF=0 si ACK, CF=1 sinon
; ------------------------------------------------------------
mouse_sendcmd:
call ps2_wait_ready_write
mov al, I8042_CMD_WRITE_AUX
out PS2_PORT_CTRL, al
call ps2_wait_ready_write
mov al, bl
out PS2_PORT_BUFFER, al
call ps2_wait_ready_read ; AL = réponse
cmp al, MOUSE_ACK
jne .bad
clc
ret
.bad:
stc
ret
; ------------------------------------------------------------
; interrupt handler
;
; buffer[0] = status
; buffer[1] = déplacement x
; buffer[2] = déplacement y (inversé)
;
; ------------------------------------------------------------
mouse_handler:
push ax
push bx