fixed putch/writestring

This commit is contained in:
Frater 2026-01-25 21:15:19 +01:00
commit dcf67075e5
10 changed files with 1984 additions and 697 deletions

View file

@ -34,41 +34,35 @@
%define I8042_CMD_EN_AUX 0xA8
%define I8042_CMD_RD_CBYTE 0x20
%define I8042_CMD_WR_CBYTE 0x60
%define I8042_CMD_DIS_KBD 0xAD
%define I8042_CMD_DIS_KBD 0xAD
%define I8042_CMD_DIS_MOUSE 0xA7
%define I8042_CMD_WRITE_AUX 0xD4
%include "./services/cursor.asm"
; ------------------------------------------------------------
; remise a zero des valeurs internes du "drivers"
;
; ------------------------------------------------------------
mouse_reset:
mov ax, BDA_DATA_SEG
mov fs,ax
mov ax, BDA_DATA_SEG
mov fs,ax
; effacer les variables du drivers
mov byte [fs:BDA_MOUSE + mouse.idx],0
mov dword [fs:BDA_MOUSE + mouse.buffer],0
mov byte [fs:BDA_MOUSE + mouse.packetlen],3 ; 3 bytes par défaut
mov byte [fs:BDA_MOUSE + mouse.status],0
mov word [fs:BDA_MOUSE + mouse.x],320 ; vous pouvez aussi préciser le centre
mov word [fs:BDA_MOUSE + mouse.y],100 ; vous pouvez aussi préciser le centre
; experiemntal
mov word [fs:BDA_MOUSE + mouse.wheel],0
mov byte [fs:BDA_MOUSE + mouse.cur_visible], 1
mov word [fs:BDA_MOUSE + mouse.cur_x], 0
mov word [fs:BDA_MOUSE + mouse.cur_y], 0
mov byte [fs:BDA_MOUSE + mouse.cur_drawing], 0
mov ax, cs
mov word [fs:BDA_MOUSE + mouse.cur_seg], ax
mov word [fs:BDA_MOUSE + mouse.cur_ofs], mouse_arrow
ret
; effacer les variables du drivers
mov byte [fs:BDA_MOUSE + mouse.idx],0
mov dword [fs:BDA_MOUSE + mouse.buffer],0
mov byte [fs:BDA_MOUSE + mouse.packetlen],3 ; 3 bytes par défaut
mov byte [fs:BDA_MOUSE + mouse.status],0
mov word [fs:BDA_MOUSE + mouse.x],320 ; vous pouvez aussi préciser le centre
mov word [fs:BDA_MOUSE + mouse.y],100 ; vous pouvez aussi préciser le centre
; experiemntal
mov word [fs:BDA_MOUSE + mouse.wheel],0
mov byte [fs:BDA_MOUSE + mouse.cur_visible], 1
mov word [fs:BDA_MOUSE + mouse.cur_x], 0
mov word [fs:BDA_MOUSE + mouse.cur_y], 0
mov byte [fs:BDA_MOUSE + mouse.cur_drawing], 0
mov ax, cs
mov word [fs:BDA_MOUSE + mouse.cur_seg], ax
mov word [fs:BDA_MOUSE + mouse.cur_ofs], mouse_arrow
ret
; ------------------------------------------------------------
; initialise le i8042 keyboard and mouse (PS/2)
@ -76,90 +70,71 @@ mouse_reset:
; on envoit les commandes RESET/DEFAULT/STREAM
; ------------------------------------------------------------
mouse_init:
push ds
push ax
push ds
push ax
; reset des valeurs internes
call mouse_reset
cli
call ps2_wait_ready_write
mov al, I8042_CMD_DIS_KBD ; Disable Keyboard
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, I8042_CMD_DIS_MOUSE ; Disable Mouse
out i8042_PS2_CTRL, al
; Vider tout ce qui traîne
call ps2_flush_output
; Activer le port souris (AUX)
call ps2_wait_ready_write
mov al, I8042_CMD_EN_AUX
out i8042_PS2_CTRL, al
; Lire command byte
call ps2_wait_ready_write
mov al, I8042_CMD_RD_CBYTE
out i8042_PS2_CTRL, al
; reset des valeurs internes
call mouse_reset
call ps2_read ; AL = command byte
; Activer IRQ12 (bit 1)
or al, 02h
mov ah, al
; Réécrire command byte (commande 0x60 envoyée à 0x64)
call ps2_wait_ready_write
mov al, I8042_CMD_WR_CBYTE
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, ah
out i8042_PS2_DATA, al
call ps2_flush_output
; --- Reset
; réponse attendue : 0xAA, [ID]
mov bl, MOUSE_CMD_RESET
call mouse_sendcmd
call ps2_read ; 0xAA attendu (self-test OK)
call ps2_read ; ID (souvent 0x00)
; --- Default
mov bl, MOUSE_CMD_DEFAULT
call mouse_sendcmd
cli
call ps2_wait_ready_write
mov al, I8042_CMD_DIS_KBD ; Disable Keyboard
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, I8042_CMD_DIS_MOUSE ; Disable Mouse
out i8042_PS2_CTRL, al
; Vider tout ce qui traîne
call ps2_flush_output
; Activer le port souris (AUX)
call ps2_wait_ready_write
mov al, I8042_CMD_EN_AUX
out i8042_PS2_CTRL, al
; Lire command byte
call ps2_wait_ready_write
mov al, I8042_CMD_RD_CBYTE
out i8042_PS2_CTRL, al
call ps2_read ; AL = command byte
; Activer IRQ12 (bit 1)
or al, 02h
mov ah, al
; Réécrire command byte (commande 0x60 envoyée à 0x64)
call ps2_wait_ready_write
mov al, I8042_CMD_WR_CBYTE
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, ah
out i8042_PS2_DATA, al
call ps2_flush_output
; --- Reset
; réponse attendue : 0xAA, [ID]
mov bl, MOUSE_CMD_RESET
call mouse_sendcmd
call ps2_read ; 0xAA attendu (self-test OK)
call ps2_read ; ID (souvent 0x00)
; --- Default
mov bl, MOUSE_CMD_DEFAULT
call mouse_sendcmd
; --- streaming
mov bl, MOUSE_EN_STREAM
call mouse_sendcmd
; détecter le packet size (3/4 bytes)
call mouse_detect_packet_len
; installer ISR IRQ12 (INT 74h)
mov ax,cs
mov dx,ax
mov bx, isr_mouse_handler
mov ax, i8259_SLAVE_INT ; base offset IRQ8
add ax,4 ; IRQ 12
call ivt_setvector
; enable IRQ 1
mov ah,IRQ_ENABLED
mov al, 12
call pic_set_irq_mask
sti
pop ax
pop ds
ret
; --- streaming
mov bl, MOUSE_EN_STREAM
call mouse_sendcmd
; détecter le packet size (3/4 bytes)
call mouse_detect_packet_len
; installer ISR IRQ12 (INT 74h)
mov ax,cs
mov dx,ax
mov bx, isr_mouse_handler
mov ax, i8259_SLAVE_INT ; base offset IRQ8
add ax,4 ; IRQ 12
call ivt_setvector
; enable IRQ 1
mov ah,IRQ_ENABLED
mov al, 12
call pic_set_irq_mask
sti
pop ax
pop ds
ret
; ------------------------------------------------------------
; detect la taille du "payload" de la souris.
;
@ -167,42 +142,42 @@ mouse_init:
;
; ------------------------------------------------------------
mouse_detect_packet_len:
mov ax, BDA_DATA_SEG
mov fs,ax
mov ax, BDA_DATA_SEG
mov fs,ax
mov byte [fs:BDA_MOUSE + mouse.packetlen], 3
mov byte [fs:BDA_MOUSE + mouse.packetlen], 3
; SET_RATE + 200
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 200
call mouse_sendcmd
; SET_RATE + 200
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 200
call mouse_sendcmd
; SET_RATE + 100
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 100
call mouse_sendcmd
; SET_RATE + 100
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 100
call mouse_sendcmd
; SET_RATE + 80
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 80
call mouse_sendcmd
; SET_RATE + 80
mov bl, MOUSE_SET_RATE
call mouse_sendcmd
mov bl, 80
call mouse_sendcmd
; Get ID
mov bl, MOUSE_GET_ID
call mouse_sendcmd
call ps2_read ; AL = ID
; Get 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
cmp al, 03h
je .is4
cmp al, 04h
je .is4
ret
.is4:
mov byte [fs:BDA_MOUSE + mouse.packetlen], 4
ret
mov byte [fs:BDA_MOUSE + mouse.packetlen], 4
ret
; ------------------------------------------------------------
; fonction de gestion du i8042
@ -211,41 +186,41 @@ mouse_detect_packet_len:
; attendre que le 8042 soit pret a recevoir de l'information
ps2_wait_ready_write:
push cx
mov cx, 1000
push cx
mov cx, 1000
.wait:
in al, i8042_PS2_CTRL
test al, 02h ; IBF
jz .ok
loop .wait
in al, i8042_PS2_CTRL
test al, 02h ; IBF
jz .ok
loop .wait
.ok:
pop cx
ret
pop cx
ret
; attendre que le 8042 soit pret a lire de l'information
ps2_wait_ready_read:
.wait:
in al, i8042_PS2_CTRL
test al, 01h ; OBF
jz .wait
ret
in al, i8042_PS2_CTRL
test al, 01h ; OBF
jz .wait
ret
; lire de l'information depuis le 8042
ps2_read:
call ps2_wait_ready_read
in al, i8042_PS2_DATA
ret
call ps2_wait_ready_read
in al, i8042_PS2_DATA
ret
; vide le buffer interne du 8042 (information(s) ignorée(s))
ps2_flush_output:
.flush:
in al, i8042_PS2_CTRL
test al, 01h
jz .done
in al, i8042_PS2_DATA
jmp .flush
in al, i8042_PS2_CTRL
test al, 01h
jz .done
in al, i8042_PS2_DATA
jmp .flush
.done:
ret
ret
; ------------------------------------------------------------
; envoye une commande souris et attends le ACK
@ -255,23 +230,23 @@ ps2_flush_output:
; renvoi CF=0 si ACK, CF=1 sinon
; ------------------------------------------------------------
mouse_sendcmd:
call ps2_wait_ready_write
call ps2_wait_ready_write
mov al, I8042_CMD_WRITE_AUX
out i8042_PS2_CTRL, al
mov al, I8042_CMD_WRITE_AUX
out i8042_PS2_CTRL, al
call ps2_wait_ready_write
mov al, bl
out i8042_PS2_DATA, al
call ps2_wait_ready_write
mov al, bl
out i8042_PS2_DATA, al
call ps2_read ; AL = réponse
cmp al, MOUSE_ACK
jne .bad
clc
ret
call ps2_read ; AL = réponse
cmp al, MOUSE_ACK
jne .bad
clc
ret
.bad:
stc
ret
stc
ret
; ------------------------------------------------------------
; interrupt handler
@ -292,82 +267,81 @@ mouse_sendcmd:
; bit 7 = Y overflow
; ------------------------------------------------------------
isr_mouse_handler:
; sauvegarder tout les registres
pusha
push ds
; sauvegarder tout les registres
pusha
push ds
; use BDA segment
mov ax,BDA_DATA_SEG
mov ds,ax
; use BDA segment
mov ax,BDA_DATA_SEG
mov ds,ax
; lire un octet depuis le contrôleur et le stocker dans le buffer
in al, i8042_PS2_DATA ; lire octet souris
; lire un octet depuis le contrôleur et le stocker dans le buffer
in al, i8042_PS2_DATA ; lire octet souris
movzx bx, byte [BDA_MOUSE + mouse.idx]
mov byte [BDA_MOUSE + mouse.buffer + bx], al
inc byte [BDA_MOUSE + mouse.idx]
movzx bx, byte [BDA_MOUSE + mouse.idx]
mov byte [BDA_MOUSE + mouse.buffer + bx], al
inc byte [BDA_MOUSE + mouse.idx]
; vérifier si le packet est complet
mov al, [BDA_MOUSE + mouse.packetlen]
cmp byte [BDA_MOUSE + mouse.idx], al
jb .done
; vérifier si le packet est complet
mov al, [BDA_MOUSE + mouse.packetlen]
cmp byte [BDA_MOUSE + mouse.idx], al
jb .done
; packet complet, on decode les données
mov byte [BDA_MOUSE + mouse.idx], 0
mov bl, [BDA_MOUSE + mouse.buffer] ; status
mov [BDA_MOUSE + mouse.status], bl
; packet complet, on decode les données
mov byte [BDA_MOUSE + mouse.idx], 0
mov bl, [BDA_MOUSE + mouse.buffer] ; status
mov [BDA_MOUSE + mouse.status], bl
xor ax,ax
mov al, byte [BDA_MOUSE + mouse.buffer+1] ; delta X
test bl,00010000b ; signe X
jz .x_pos
or al, 0xF0
xor ax,ax
mov al, byte [BDA_MOUSE + mouse.buffer+1] ; delta X
test bl,00010000b ; signe X
jz .x_pos
or al, 0xF0
.x_pos:
cbw
add [BDA_MOUSE + mouse.x], ax
cbw
add [BDA_MOUSE + mouse.x], ax
xor ax,ax
mov al, byte [BDA_MOUSE + mouse.buffer+2] ; delta Y
test bl,00100000b ; signe Y
jz .y_pos
or al, 0xF0
xor ax,ax
mov al, byte [BDA_MOUSE + mouse.buffer+2] ; delta Y
test bl,00100000b ; signe Y
jz .y_pos
or al, 0xF0
.y_pos:
cbw
sub [BDA_MOUSE + mouse.y], ax
cbw
sub [BDA_MOUSE + mouse.y], ax
; si packetlen = 4, gérer la molette; experimental
movsx ax, byte [BDA_MOUSE + mouse.buffer+3] ; delta Wheel
cbw
add word [BDA_MOUSE + mouse.wheel], ax
; si packetlen = 4, gérer la molette; experimental
movsx ax, byte [BDA_MOUSE + mouse.buffer+3] ; delta Wheel
cbw
add word [BDA_MOUSE + mouse.wheel], ax
; clamp X
cmp word [BDA_MOUSE + mouse.x], 0
jge .x_ok_low
mov word [BDA_MOUSE + mouse.x], 0
; clamp X
cmp word [BDA_MOUSE + mouse.x], 0
jge .x_ok_low
mov word [BDA_MOUSE + mouse.x], 0
.x_ok_low:
cmp word [BDA_MOUSE + mouse.x], 639
jle .x_ok_high
mov word [BDA_MOUSE + mouse.x], 639
cmp word [BDA_MOUSE + mouse.x], 639
jle .x_ok_high
mov word [BDA_MOUSE + mouse.x], 639
.x_ok_high:
; clamp Y
cmp word [BDA_MOUSE + mouse.y], 0
jge .y_ok_low
mov word [BDA_MOUSE + mouse.y], 0
; clamp Y
cmp word [BDA_MOUSE + mouse.y], 0
jge .y_ok_low
mov word [BDA_MOUSE + mouse.y], 0
.y_ok_low:
cmp word [BDA_MOUSE + mouse.y], 199
jle .y_ok_high
mov word [BDA_MOUSE + mouse.y], 199
cmp word [BDA_MOUSE + mouse.y], 199
jle .y_ok_high
mov word [BDA_MOUSE + mouse.y], 199
.y_ok_high:
; call gfx_cursor_move
; call gfx_cursor_move
.done:
mov al, 0x20
out i8259_SLAVE_CMD, al ; EOI PIC esclave
out i8259_MASTER_CMD, al ; EOI PIC maître
mov al, 0x20
out i8259_SLAVE_CMD, al ; EOI PIC esclave
out i8259_MASTER_CMD, al ; EOI PIC maître
; restaurer tous les registres
pop ds
popa
iret
; restaurer tous les registres
pop ds
popa
iret