From 9cb9033a2561cbb065b191dd1821feb61eea193f Mon Sep 17 00:00:00 2001 From: Frater <13979047+saintfrater@users.noreply.github.com> Date: Mon, 19 Jan 2026 09:50:33 +0100 Subject: [PATCH 1/5] testing --- .gitignore | 3 +- src/bda.asm | 24 ++- src/boot.asm | 81 ++++++-- src/drivers/keyboard_ps2.asm | 384 ++++++++--------------------------- src/drivers/mouse_ps2.asm | 30 ++- src/services/generic.asm | 64 +++--- 6 files changed, 224 insertions(+), 362 deletions(-) diff --git a/.gitignore b/.gitignore index f65926b..696c7b8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,5 @@ build.sh run.bat .gdbinit .gdb_history -link.ld .gitignore - +*.ld diff --git a/src/bda.asm b/src/bda.asm index 9be7bb5..a268212 100644 --- a/src/bda.asm +++ b/src/bda.asm @@ -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 diff --git a/src/boot.asm b/src/boot.asm index bef61d4..0996c99 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -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 ; ------------------------------------------------------------------ diff --git a/src/drivers/keyboard_ps2.asm b/src/drivers/keyboard_ps2.asm index 4057685..8ebf0ee 100644 --- a/src/drivers/keyboard_ps2.asm +++ b/src/drivers/keyboard_ps2.asm @@ -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 \ No newline at end of file diff --git a/src/drivers/mouse_ps2.asm b/src/drivers/mouse_ps2.asm index 56f9085..6d8d823 100644 --- a/src/drivers/mouse_ps2.asm +++ b/src/drivers/mouse_ps2.asm @@ -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] diff --git a/src/services/generic.asm b/src/services/generic.asm index 27e6c01..d6f9af6 100644 --- a/src/services/generic.asm +++ b/src/services/generic.asm @@ -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 \ No newline at end of file + iret + From 82724598ee19c7582c2b62099f0bb3abf9fa0112 Mon Sep 17 00:00:00 2001 From: Frater <13979047+saintfrater@users.noreply.github.com> Date: Mon, 19 Jan 2026 22:11:51 +0100 Subject: [PATCH 2/5] fix init PIC + enable IRQ1 & 12 handeling --- src/bda.asm | 17 +-- src/boot-test.asm | 209 ++++++++++++++++++++++++++++++++++ src/boot.asm | 90 ++++++--------- src/drivers/gfx_cgam.asm | 10 +- src/drivers/keyboard_ps2.asm | 97 +++------------- src/drivers/mouse_ps2.asm | 96 ++++++++-------- src/services/generic.asm | 211 ++++++++++++++++++++--------------- 7 files changed, 440 insertions(+), 290 deletions(-) create mode 100644 src/boot-test.asm diff --git a/src/bda.asm b/src/bda.asm index a268212..5b9f59f 100644 --- a/src/bda.asm +++ b/src/bda.asm @@ -48,7 +48,7 @@ ; information relative à la souris ; -%define BDA_MOUSE_SEG 0x0050 +%define BDA_DATA_SEG 0x0050 %define BDA_MOUSE_BUFFER 0x0000 ; dword; buffer (jusqu'à 4 octets) %define BDA_MOUSE_IDX 0x0004 ; byte 0..3 @@ -104,6 +104,9 @@ %define i8259_MASTER_INT 0x08 %define i8259_SLAVE_INT 0x70 +%define IRQ_ENABLED 0x00 +%define IRQ_DISABLED 0x01 + ; ----------------------------------------------------------------------------------- ; bda_setup ; Initialise la BDA à zéro @@ -112,18 +115,10 @@ bda_setup: pusha push ds - mov ax, BDA_SEGMENT + mov ax, BDA_DATA_SEG mov es, ax - mov ax, 0 - mov cx,0xFF - xor di, di - rep stosb ; clear BDA area - - mov ax, BDA_MOUSE_SEG - mov es, ax - - mov ax, 0 + mov ax, 0x5F mov cx,0xFF xor di, di rep stosb ; clear BDA area diff --git a/src/boot-test.asm b/src/boot-test.asm new file mode 100644 index 0000000..e5d7859 --- /dev/null +++ b/src/boot-test.asm @@ -0,0 +1,209 @@ +; Compilation: nasm -f bin bios.asm -o bios.bin +; Taille: 64 KB + +BITS 16 + +section .text +org 0x0000 + +%define BIOS_DATA_SEG 0x0050 + +; ------------------------------------------------------------------ +; POINT D'ENTRÉE DU BIOS (Cible du jump du reset vector) +; ------------------------------------------------------------------ +start: + cli + cld + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov sp, 0x7000 + + ; Créer une table "cohérente" de handler + call setup_ivt + + ; Initialisation du PIC (Indispensable pour IRQ0/IRQ1) + mov al, 0x11 + out 0x20, al + out 0xA0, al + mov al, 0x08 ; IRQ0-7 -> INT 08h-0Fh + out 0x21, al + mov al, 0x70 ; IRQ8-15 -> INT 70h-77h + out 0xA1, al + + mov al, 0x04 + out 0x21, al + mov al, 0x02 + out 0xA1, al + + mov al, 0x01 + out 0x21, al + out 0xA1, al + + ; Scan et Initialisation des options ROMS (VGA BIOS, etc) + call setup_load_rom + + ; initialise l'écran en mode texte + mov ax,0x0003 + int 0x10 + + ; Installation des ISR + mov word [0x08*4], timer_isr + mov word [0x08*4+2], 0xF000 + + mov word [0x09*4], keyboard_isr + mov word [0x09*4+2], 0xF000 + + ; Démasquer IRQ0 et IRQ1 + mov al, 0xFC + out 0x21, al + sti + + mov ax, BIOS_DATA_SEG + mov es, ax + mov di, 0x0 ; Adresse physique de tes variables + mov cx, 4 + xor ax,ax + rep stosb ; Met 4 octets à zéro + +main_loop: + mov ax,BIOS_DATA_SEG + mov fs,ax + + mov al, [fs:0x00] ; Timer + mov di, 0 + call draw_hex_byte + + mov al, [fs:0x01] ; Clavier + mov di, 6 + call draw_hex_byte + + mov al, [fs:0x02] ; Clavier + mov di, 12 + call draw_hex_byte + + jmp main_loop + +; ------------------------------------------------------------------ +; HANDLERS & HELPERS +; ------------------------------------------------------------------ +default_isr: + iret + + +timer_isr: + push ax + push gs + + mov ax, BIOS_DATA_SEG + mov gs, ax + + inc byte [gs:0x00] + + mov al, 0x20 + out 0x20, al + + pop gs + pop ax + iret + +keyboard_isr: + push ax + push gs + + mov ax, BIOS_DATA_SEG + mov gs, ax + + in al, 0x60 ; Ack clavier + mov byte [gs:0x02],al + + inc byte [gs:0x01] + + mov al, 0x20 + out 0x20, al + + pop gs + pop ax + iret + +draw_hex_byte: + push ax + + mov bx, 0xb800 + mov es,bx + + shr al, 4 + call .draw_nibble + pop ax +; add di, 2 + +.draw_nibble: + and al, 0x0F + add al, '0' + cmp al, '9' + jbe .print + add al, 7 +.print: + mov ah, 0x0F ; Blanc sur Noir + stosw + ret + +; ---------------------------------------------------------------------------- +; construire une table "vide" de pointeur ISR +; ---------------------------------------------------------------------------- +setup_ivt: + ; installation de la table d'interrupts + xor ax, ax + mov es, ax ; ES = 0000h -> base IVT + xor di, di ; DI = 0000h -> offset IVT + + mov ax, default_isr ; offset du handler + mov dx, cs ; segment du handler (ROM) + + mov cx, 256 ; 256 vecteurs +.fill: + stosw ; write offset (AX) -> [ES:DI], DI += 2 + xchg ax, dx ; AX = segment, DX = offset + stosw ; write segment (AX) -> [ES:DI], DI += 2 + xchg ax, dx ; AX = offset, DX = segment + loop .fill + ; fin de la table d'interrupt + ret + +; --------------------------------------------------------------------------- +; Détection les ROM supplementaires +; Teste la RAM de 0xC000 jusqu’à 0xE000, par pas de 2KB. +; +; Si une ROM est détectée, le code de la ROM sera appelé. +; --------------------------------------------------------------------------- +setup_load_rom: + mov bx, 0xC000 +.scanloop: + mov gs, bx + cmp word[gs:0x0000],0xAA55 ; ROM signature + jne .nextblock + + ; appel le code (en 0x0003) de la ROM + push cs + push word .nextblock ; address de retour + + push bx ; segment + push 0x0003 ; offset entry rom + retf ; call far ds:0x0003 +.nextblock: + add bx, 0x80 ; add 2kb + cmp bx, 0xE000 + jbe .scanloop + ret + +; ------------------------------------------------------------------ +; Padding jusqu'au reset vector +; ------------------------------------------------------------------ +times 0xFFF0 - ($ - $$) db 0xFF + + +reset_vector: + jmp 0xF000:start + db '06/01/2026' + db 0 ; le stub tient dans 16 octets (ou moins) \ No newline at end of file diff --git a/src/boot.asm b/src/boot.asm index 0996c99..2e4f161 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -29,26 +29,19 @@ bits 16 %define DEBUG_PORT 0xe9 ; 0x402 ou 0xe9 ; initial stack -%define STACK_SEG 0x0030 ; compatible avec le BMM (Bios Memory Maps) -%define STACK_TOP 0x0100 ; choix commun en RAM basse (pile descendante) - -%define MEM_SEG_DEB 0x0800 ; 0x0800:0000 = 0x8000 (32KB) évite IVT/BDA + stack 7C00 -%define MEM_SEG_END 0xA000 ; 0xA000:0000 = 0xA0000 (début zone vidéo) -%define MEM_SEG_STEP 0x0040 ; 1KB = 0x400 bytes = 0x40 paragraphs - +%define STACK_SEG 0x1000 ; segment de base section .text bits 16 - jmp reset ; ce jump n'est pas sensé être executer, il est présent uniquement pour le debug ; definition du BDA %include "./bda.asm" -%include "./services/macros.asm" +; %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 @@ -56,27 +49,29 @@ err_vganok db 'VGA Not Initialized',0 reset: cli + cld + ; il n'existe aucun 'stack' par défaut mov ax, STACK_SEG mov ss, ax - mov sp, STACK_TOP ; haut du segment, mot-aligné - - ; 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 - mov ss, ax - mov sp, 0x1000 ; sommet de pile - - ; initialisation du BDA - call bda_setup + mov sp, 0xFFFE ; haut du segment (64ko de stack), mot-aligné ; installé une table d'interruption "dummy" call ivt_setup + call bda_setup + + ; test IRQ 0 + mov ax,cs + mov dx,ax + mov bx,test_isr + mov ax,i8259_MASTER_INT + + call ivt_setvector + + ; initialisation des PIC 8259A + call pic_init + ; load Roms call setup_load_rom @@ -87,18 +82,13 @@ reset: mov ax, 0x0003 int 10h - cli - ; initialisation des PIC 8259A - call pic_init -; call kbd_init + ; enable IRQ 0 + mov ah,IRQ_ENABLED + mov al,0 + call pic_set_irq_mask - ; test IRQ 0 - mov ax,cs - mov dx,ax - mov bx,test_isr - mov ax,i8259_MASTER_INT + call kbd_init - call ivt_setvector sti @@ -106,18 +96,11 @@ reset: ; call gfx_init ;call mouse_reset - ;call mouse_init + call mouse_init endless: - mov ax,0x0050 + mov ax,BDA_DATA_SEG 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 @@ -153,21 +136,22 @@ endless: ; Keyboard ISR (IRQ -> INT) ; ----------------------------------------------------------- test_isr: - pusha - push ds + push ax + push fs - mov ax,0x0050 - mov ds,ax - inc byte [0x0005] + mov ax,BDA_DATA_SEG + mov fs,ax + inc byte [fs:0x000F] + + inc byte [fs:0x0005] mov al, PIC_EOI ; EOI master out i8259_MASTER_CMD, al - pop ds - popa + pop fs + pop ax iret - ; ------------------------------------------------------------------ ; Padding jusqu'au reset vector ; ------------------------------------------------------------------ @@ -184,6 +168,6 @@ reset_vector: ; code minimal au reset jmp 0xF000:reset builddate db '06/01/2026' -times 16-($-$$) db 0 ; le stub tient dans 16 octets (ou moins) + times 16-($-$$) db 0 ; le stub tient dans 16 octets (ou moins) diff --git a/src/drivers/gfx_cgam.asm b/src/drivers/gfx_cgam.asm index bfdff66..1aa2105 100644 --- a/src/drivers/gfx_cgam.asm +++ b/src/drivers/gfx_cgam.asm @@ -91,7 +91,7 @@ gfx_calc_addr: ; BL = color (0=black, !=0=white) ; ------------------------------------------------------------ gfx_putpixel: - PUSH_ABCD + pusha push di push es @@ -113,7 +113,7 @@ gfx_putpixel: .done: pop es pop di - POP_ABCD + popa ret ; ------------------------------------------------------------ @@ -206,7 +206,7 @@ gfx_getpixel_fast: gfx_cursor_draw: pusha - mov ax, BDA_MOUSE_SEG + mov ax, BDA_DATA_SEG mov ds, ax mov ax, VIDEO_SEG mov es, ax @@ -411,7 +411,7 @@ gfx_cursor_draw: gfx_cursor_savebg: pusha ; sauvegarde registres - mov ax, BDA_MOUSE_SEG + mov ax, BDA_DATA_SEG mov ds, ax mov ax, VIDEO_SEG @@ -491,7 +491,7 @@ gfx_cursor_savebg: gfx_cursor_restorebg: pusha ; sauvegarde registres - mov ax, BDA_MOUSE_SEG + mov ax, BDA_DATA_SEG mov ds, ax mov ax, VIDEO_SEG diff --git a/src/drivers/keyboard_ps2.asm b/src/drivers/keyboard_ps2.asm index 8ebf0ee..5004388 100644 --- a/src/drivers/keyboard_ps2.asm +++ b/src/drivers/keyboard_ps2.asm @@ -21,50 +21,21 @@ ; ; ============================================================================= -kbc_wait_in_empty: - in al, i8042_PS2_CTRL - test al, 0x02 ; IBF - jnz kbc_wait_in_empty - ret - -kbc_wait_out_full: - in al, i8042_PS2_CTRL - test al, 0x01 ; OBF - jz kbc_wait_out_full - ret - -kbc_flush_output: - in al, i8042_PS2_CTRL - test al, 0x01 ; OBF ? +kbd_flush: + in al, 0x64 ; Lire le Status Register + test al, 1 ; Est-ce qu'il y a une donnée (Output Buffer Full) ? jz .done - in al, i8042_PS2_DATA; jette la donnée - jmp kbc_flush_output + in al, 0x60 ; Lire et ignorer la donnée + jmp kbd_flush .done: ret - -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 + call kbd_flush + ; gestion du buffer clavier mov ax, BDA_SEGMENT mov ds, ax @@ -72,55 +43,19 @@ kbd_init: mov byte [BDA_KBD_TAIL], 0 mov byte [BDA_KBD_FLAGS], 0 - cli + mov ax,cs + mov dx,ax + mov bx,kbd_isr -; 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 word [ds:si], kbd_isr - mov ax, cs - mov word [ds:si+2], ax + call ivt_setvector - 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 + ; enable IRQ 1 + mov ah,IRQ_ENABLED + mov al,1 + call pic_set_irq_mask pop ax pop ds @@ -135,7 +70,7 @@ kbd_isr: in al, 0x60 ; consomme scancode (obligatoire) - mov ax,0x0050 + mov ax,BDA_DATA_SEG mov ds,ax inc byte [0x0000] diff --git a/src/drivers/mouse_ps2.asm b/src/drivers/mouse_ps2.asm index 6d8d823..b59910a 100644 --- a/src/drivers/mouse_ps2.asm +++ b/src/drivers/mouse_ps2.asm @@ -34,8 +34,11 @@ %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_MOUSE 0xA7 %define I8042_CMD_WRITE_AUX 0xD4 + %define CURSOR_W 16 %define CURSOR_H 16 @@ -78,7 +81,7 @@ mouse_arrow: ; ; ------------------------------------------------------------ mouse_reset: - mov ax, BDA_MOUSE_SEG + mov ax, BDA_DATA_SEG mov ds,ax ; effacer les variables du drivers @@ -112,8 +115,18 @@ mouse_init: ; reset des valeurs internes call mouse_reset - ; Activer le port souris - call ps2_flush_output + 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 @@ -124,6 +137,7 @@ mouse_init: 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) @@ -159,24 +173,22 @@ mouse_init: call mouse_detect_packet_len ; installer ISR IRQ12 (INT 74h) - cli - mov si, 0x74 * 4 - xor ax, ax - mov ds, ax + mov ax,cs + mov dx,ax + mov bx, isr_mouse_handler - mov word [ds:si], mouse_handler - add si, 2 - mov ax, cs - mov word [ds:si], cs + 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 - -mov dx, 0x00E9 -mov al, '!' -out dx, al - - pop ax pop ds ret @@ -229,10 +241,15 @@ mouse_detect_packet_len: ; attendre que le 8042 soit pret a recevoir de l'information ps2_wait_ready_write: + push cx + mov cx, 1000 .wait: in al, i8042_PS2_CTRL test al, 02h ; IBF - jnz .wait + jz .ok + loop .wait +.ok: + pop cx ret ; attendre que le 8042 soit pret a lire de l'information @@ -304,38 +321,19 @@ mouse_sendcmd: ; bit 6 = X overflow ; bit 7 = Y overflow ; ------------------------------------------------------------ -mouse_handler: - pusha - push ds - - ; vider le byte qui a déclenché IRQ12 - in al, i8042_PS2_DATA - - mov dx, 0xE9 ; 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 - +isr_mouse_handler: ; sauvegarder tout les registres pusha push ds ; use BDA segment - mov ax,BDA_MOUSE_SEG + mov ax,BDA_DATA_SEG mov ds,ax - ; lire un octet depuis le contrôleur - ; et le stocker dans le buffer + ; lire un octet depuis le contrôleur et le stocker dans le buffer in al, i8042_PS2_DATA ; lire octet souris - mov byte [BDA_MOUSE_BUFFER + BDA_MOUSE_IDX], al + movzx bx, byte [BDA_MOUSE_IDX] + mov byte [BDA_MOUSE_BUFFER + bx], al inc byte [BDA_MOUSE_IDX] ; vérifier si le packet est complet @@ -345,10 +343,6 @@ mouse_handler: ; packet complet, on decode les données mov byte [BDA_MOUSE_IDX], 0 - mov al,'*' ; pour debug - call debug_putc - - mov al, [BDA_MOUSE_BUFFER] ; status mov [BDA_MOUSE_STATUS], al @@ -385,17 +379,17 @@ mouse_handler: mov word [BDA_MOUSE_Y], 199 .y_ok_high: ; restaurer l'ancien arrière plan du curseur - call gfx_cursor_restorebg + ;call gfx_cursor_restorebg ; sauvegarder le nouvel arrière plan du curseur - call gfx_cursor_savebg + ;call gfx_cursor_savebg - call gfx_cursor_draw + ;call gfx_cursor_draw .done: mov al, 0x20 - out 0xA0, al ; EOI PIC esclave - out 0x20, al ; EOI PIC maître + out i8259_SLAVE_CMD, al ; EOI PIC esclave + out i8259_MASTER_CMD, al ; EOI PIC maître ; restaurer tous les registres pop ds diff --git a/src/services/generic.asm b/src/services/generic.asm index d6f9af6..9fbbd67 100644 --- a/src/services/generic.asm +++ b/src/services/generic.asm @@ -20,7 +20,9 @@ ; ; ============================================================================= - +%define MEM_SEG_DEB 0x0800 ; 0x0800:0000 = 0x8000 (32KB) évite IVT/BDA + stack 7C00 +%define MEM_SEG_END 0xA000 ; 0xA000:0000 = 0xA0000 (début zone vidéo) +%define MEM_SEG_STEP 0x0040 ; 1KB = 0x400 bytes = 0x40 paragraphs ; --------------------------------------------------------------------------- ; Détection RAM conventionnelle @@ -70,7 +72,7 @@ ram_setup: .fail_restore: ; restaurer avant de sortir - mov [es:di], bx + mov [es:di], bx .done: ; AX contient le nb de KB valides *à partir de MEM_SEG_DEB*. @@ -87,7 +89,7 @@ ram_setup: mov bx, BDA_SEGMENT mov ds, bx mov [BDA_INFO_MEM_SIZE], ax - mov [BDA_INFO_MEM_SEG],dx + mov [BDA_INFO_MEM_SEG], dx ret @@ -98,27 +100,23 @@ ram_setup: ; Si une ROM est détectée, le code de la ROM sera appelé. ; --------------------------------------------------------------------------- setup_load_rom: - mov bx, 0xC000 + mov bx, 0xC000 .scanloop: - mov ds, bx - cmp word[0x0000],0xAA55 ; ROM signature - jne .norom - - ; Debug - mov al,'.' - call debug_putc + mov ds, bx + cmp word[0x0000],0xAA55 ; ROM signature + jne .norom ; appel le code (en 0x0003) de la ROM - push cs - push word .norom ; address de retour + push cs + push word .norom ; address de retour - push ds - push 0x0003 + push ds + push 0x0003 retf ; call far ds:0x0003 .norom: - add bx, 0x80 ; add 2kb - cmp bx, 0xE000 - jbe .scanloop + add bx, 0x80 ; add 2kb + cmp bx, 0xE000 + jbe .scanloop .end: ret @@ -130,25 +128,22 @@ setup_load_rom: ; --------------------------------------------------------------------------- setup_check_vga: ; vérification de l'offset 0x0000:0x0040 qui DOIT etre différent de @default_isr - xor ax,ax - mov es,ax + xor ax,ax + mov es,ax ; Lire INT10: offset+segment depuis 0000:0040 - mov bx, [es:0x0040] ; BX = offset INT10 - mov cx, [es:0x0042] ; CX = segment INT10 - mov ax, cs + mov bx, [es:0x0040] ; BX = offset INT10 + mov cx, [es:0x0042] ; CX = segment INT10 + mov ax, cs ; Comparer avec default_isr (offset) et CS (segment) - mov dx, default_isr ; DX = offset default_isr (dans notre CS) - cmp bx, dx - jne .init_ok ; vecteur different, bios initialisé - cmp cx, ax - jne .init_ok + mov dx, default_isr ; DX = offset default_isr (dans notre CS) + cmp bx, dx + jne .init_ok ; vecteur different, bios initialisé + cmp cx, ax + jne .init_ok - mov ax,cs ; vecteur identique, sans doute pas d'initialisation - mov ds,ax - - mov si, err_vganok - call debug_puts + mov ax,cs ; vecteur identique, sans doute pas d'initialisation + mov ds,ax .init_ok: ret @@ -158,20 +153,20 @@ setup_check_vga: ; --------------------------------------------------------------------------- ivt_setup: ; installation de la table d'interrupts - xor ax, ax - mov es, ax ; ES = 0000h -> base IVT - xor di, di ; DI = 0000h -> offset IVT + xor ax, ax + mov es, ax ; ES = 0000h -> base IVT + xor di, di ; DI = 0000h -> offset IVT - mov ax, default_isr ; offset du handler - mov dx, cs ; segment du handler (ROM) + mov ax, default_isr ; offset du handler + mov dx, cs ; segment du handler (ROM) - mov cx, 256 ; 256 vecteurs + mov cx, 256 ; 256 vecteurs .fill: stosw ; write offset (AX) -> [ES:DI], DI += 2 - xchg ax, dx ; AX = segment, DX = offset + xchg ax, dx ; AX = segment, DX = offset stosw ; write segment (AX) -> [ES:DI], DI += 2 - xchg ax, dx ; AX = offset, DX = segment - loop .fill + xchg ax, dx ; AX = offset, DX = segment + loop .fill ; fin de la table d'interrupt ret @@ -183,29 +178,29 @@ ivt_setup: ; bx = offset handler ; --------------------------------------------------------------------------- ivt_setvector: - push es - push di + push es + push di - shl ax,2 ; ax=id *4 - mov di,ax + shl ax,2 ; ax=id *4 + mov di,ax - xor ax,ax - mov es, ax ; ES = 0000h -> base IVT + xor ax,ax + mov es, ax ; ES = 0000h -> base IVT - mov ax, bx ; offset du handler + mov ax, bx ; offset du handler cli stosw ; write offset (AX) -> [ES:DI], DI += 2 - xchg ax, dx ; AX = segment, DX = offset + xchg ax, dx ; AX = segment, DX = offset stosw ; write segment (AX) -> [ES:DI], DI += 2 sti - pop di - pop es + pop di + pop es ret io_wait: - xor al,al - out 0x80,al + xor al,al + out 0x80,al ret ; --------------------------------------------------------------------------- @@ -216,52 +211,90 @@ io_wait: ; https://wiki.nox-rhea.org/back2root/ibm-pc-ms-dos/hardware/8259 ; --------------------------------------------------------------------------- pic_init: - push ax + cli + push ax + + ; force un End Of Interrupt sur les 2 PIC + mov al, 0x20 + out i8259_SLAVE_CMD, al ; EOI PIC esclave + out i8259_MASTER_CMD, al ; EOI PIC maître + ; 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_MASTER_CMD, al + out i8259_SLAVE_CMD, al - mov al, ICW1_INIT | ICW1_ICW4 - out i8259_SLAVE_CMD, al - call io_wait - - - 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 + mov al, i8259_MASTER_INT + out i8259_MASTER_DATA, al ; ICW2: Master PIC vector offset + mov al, i8259_SLAVE_INT + out i8259_SLAVE_DATA, al ; ICW2: Slave PIC vector offset ; 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 + 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 ; 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 + mov al, 0x02 ; slave: cascade identity 2 + out i8259_SLAVE_DATA, al ; ICW3: tell Slave PIC its cascade identity (0000 0010) ; ICW4: 8086 mode - mov al, ICW4_8086 - out i8259_MASTER_DATA, al - call io_wait + mov al, ICW4_8086 + out i8259_MASTER_DATA, al + out i8259_SLAVE_DATA, al - mov al, ICW4_8086 - out i8259_SLAVE_DATA, al - call io_wait - - - ; Unmask both PICs. - xor al,al - out i8259_MASTER_DATA, al - out i8259_SLAVE_DATA, al - - pop ax + pop ax ret +; ------------------------------------------------------------------ +; set_irq_mask +; AH = 0 (Enable/Unmask) ou 1 (Disable/Mask) +; AL = Numéro de l'IRQ (0 à 15) +; ------------------------------------------------------------------ +pic_set_irq_mask: + push bx + push cx + push dx + push ax ; Sauvegarde AX pour récupérer AH plus tard + + mov cl, al ; CL = numéro d'IRQ + ; Par défaut, PIC Maître (Data port) + mov dx, i8259_MASTER_DATA + + cmp cl, 8 + jl .is_master + + ; Si IRQ >= 8, on passe sur le PIC Esclave + sub cl, 8 ; Ajuste l'index du bit (8-15 -> 0-7) + ; Port Data du PIC Esclave + mov dx, i8259_SLAVE_DATA + +.is_master: + ; Préparation du masque de bit + mov bl, 1 + shl bl, cl ; BL contient maintenant le bit correspondant (ex: IRQ 3 -> 00001000) + + in al, dx ; Lire l'IMR actuel depuis le port (0x21 ou 0xA1) + + test ah, ah ; AH est-il à 1 (Disable) ? + jnz .do_disable + +.do_enable: + not bl ; Inverser le masque (ex: 11110111) + and al, bl ; Mettre le bit à 0 (Unmask) + jmp .write_back + +.do_disable: + or al, bl ; Mettre le bit à 1 (Mask) + +.write_back: + out dx, al ; Écrire le nouveau masque dans l'IMR + + pop ax ; Restaurer AX + pop dx + pop cx + pop bx + ret + ; --------------------------------------------------------------------------- ; Handler par défaut: fait juste IRET ; IMPORTANT: doit être FAR (appelé par le CPU via IVT), et terminer par IRET From 96215660361179e4426e4e19791688eadae259e6 Mon Sep 17 00:00:00 2001 From: saintfrater <13979047+saintfrater@users.noreply.github.com> Date: Tue, 20 Jan 2026 14:55:09 +0100 Subject: [PATCH 3/5] fix mouse management --- src/bda.asm | 41 ++++++++++++--------- src/boot.asm | 14 +++---- src/drivers/gfx_cgam.asm | 50 +++++++++++++------------ src/drivers/mouse_ps2.asm | 77 +++++++++++++++++++++------------------ 4 files changed, 96 insertions(+), 86 deletions(-) diff --git a/src/bda.asm b/src/bda.asm index 5b9f59f..9cce339 100644 --- a/src/bda.asm +++ b/src/bda.asm @@ -49,26 +49,31 @@ ; %define BDA_DATA_SEG 0x0050 +%define BDA_MOUSE 0x0000 -%define BDA_MOUSE_BUFFER 0x0000 ; dword; buffer (jusqu'à 4 octets) -%define BDA_MOUSE_IDX 0x0004 ; byte 0..3 -%define DBA_MOUSE_PACKETLEN 0x0005 -%define DBA_CURSOR_MASK 0x0006 ; byte pixels mask (bit per pixel) -%define BDA_MOUSE_STATUS 0x0007 ; byte -%define BDA_MOUSE_X 0x0008 ; word -%define BDA_MOUSE_Y 0x000A ; word -%define BDA_MOUSE_WHEEL 0x000C ; word +struc mouse + .buffer resb 4 ; i8042 input buffer + .idx resb 1 ; index in the buffer + .packetlen resb 1 ; max buffer len (3 ou 4) -%define BDA_CURSOR_VISIBLE 0x000E ; byte (0/1) -%define BDA_CURSOR_OLDX 0x000F ; word -%define BDA_CURSOR_OLDY 0x0011 ; word -%define BDA_CURSOR_NEWX 0x0013 ; word -%define BDA_CURSOR_NEWY 0x0015 ; word -%define BDA_CURSOR_BITOFF 0x0017 ; byte 0..7 bits d'offset (x&7) -%define BDA_CURSOR_BYTES 0x0019 ; byte 2 ou 3 bytes as source for cursor image -%define BDA_CURSOR_SAVED 0x001A ; flag if image saved -%define BDA_CURSOR_PTR 0x001B ; pointeur vers l'image du curseur -%define BDA_CURSOR_BG 0x0020 ; 48 bytes max (3*16) + .status resb 1 ; mouse status (button etc) + .wheel resb 1 ; if a packet size is 4; experimental + .x resw 1 ; + .y resw 1 + ; cursor management + .cur_oldx resw 1 + .cur_oldy resw 1 + .cur_newx resw 1 + .cur_newy resw 1 + .cur_visible resb 1 + .cur_seg resw 1 ; segment / offset of the pointer + .cur_ofs resw 1 + .cur_mask resb 1 ; pixels mask (bit per pixel) + .cur_bit_ofs resb 1 ; 0..7 bits d'offset (x&7) + .cur_bytes resb 1 ; byte 2 ou 3 bytes as source for cursor image + .bkg_saved resb 1 ; background saved + .bkg_buffer resb 48 ; buffer for saved background +endstruc ; ----------------------------------------------------------------------------------- ; PC components I/O ports diff --git a/src/boot.asm b/src/boot.asm index 2e4f161..b3897bc 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -137,18 +137,16 @@ endless: ; ----------------------------------------------------------- test_isr: push ax - push fs - mov ax,BDA_DATA_SEG - mov fs,ax - inc byte [fs:0x000F] - - inc byte [fs:0x0005] + ; push fs + ;mov ax,BDA_DATA_SEG + ;mov fs,ax + ;inc byte [fs:0x0005] mov al, PIC_EOI ; EOI master out i8259_MASTER_CMD, al - - pop fs + ; pop fs + pop ax iret diff --git a/src/drivers/gfx_cgam.asm b/src/drivers/gfx_cgam.asm index 1aa2105..83ee555 100644 --- a/src/drivers/gfx_cgam.asm +++ b/src/drivers/gfx_cgam.asm @@ -43,7 +43,7 @@ gfx_init: mov al, GFX_MODE int 0x10 - mov byte [BDA_CURSOR_SAVED],0 ; flag image saved + mov byte [BDA_MOUSE + mouse.bkg_saved],0 ; flag image saved ; dessine un background "check-board" call gfx_background @@ -159,13 +159,13 @@ gfx_background: gfx_cursor_calc_align: mov al, cl and al, 7 ; offset = x&7 - mov byte [BDA_CURSOR_BITOFF], al + mov byte [BDA_MOUSE + mouse.cur_bit_ofs], al cmp al, 0 jne .unaligned - mov byte [BDA_CURSOR_BYTES], 2 + mov byte [BDA_MOUSE + mouse.cur_bytes], 2 ret .unaligned: - mov byte [BDA_CURSOR_BYTES], 3 + mov byte [BDA_MOUSE + mouse.cur_bytes], 3 ret ; ------------------------------------------------------------ @@ -211,15 +211,15 @@ gfx_cursor_draw: mov ax, VIDEO_SEG mov es, ax - mov ax, word [BDA_CURSOR_PTR] + mov ax, word [BDA_MOUSE + mouse.cur_ofs] mov si, ax - mov cx, word [BDA_CURSOR_NEWX] - mov dx, word [BDA_CURSOR_NEWY] + mov cx, word [BDA_MOUSE + mouse.cur_newx] + mov dx, word [BDA_MOUSE + mouse.cur_newy] ; calcule offset et bytes/ligne (2 ou 3) call gfx_cursor_calc_align - mov bl, byte [BDA_CURSOR_BITOFF]; 0..7 + mov bl, byte [BDA_MOUSE + mouse.cur_bit_ofs]; 0..7 ; calcule DI de départ (attention: gfx_calc_addr modifie CL) push cx @@ -417,16 +417,16 @@ gfx_cursor_savebg: mov ax, VIDEO_SEG mov es, ax - cmp byte [BDA_CURSOR_SAVED], 0 + cmp byte [BDA_MOUSE + mouse.bkg_saved], 0 jne .done ; déjà sauvé - mov cx, [BDA_MOUSE_X] - mov dx, [BDA_MOUSE_Y] + mov cx, [BDA_MOUSE + mouse.x] + mov dx, [BDA_MOUSE + mouse.y] - mov byte [BDA_CURSOR_SAVED], 1 ; flag image saved + mov byte [BDA_MOUSE + mouse.bkg_saved], 1 ; flag image saved - mov word [BDA_CURSOR_NEWX], cx ; sauvegarde la nouvelle position - mov word [BDA_CURSOR_NEWY], dx + mov word [BDA_MOUSE + mouse.cur_newx], cx ; sauvegarde la nouvelle position + mov word [BDA_MOUSE + mouse.cur_newy], dx call gfx_calc_addr ; ES:DI = byteaddr pour (X,Y) mov dx,0x2000 ; offset pour lignes impaire @@ -434,15 +434,15 @@ gfx_cursor_savebg: jl .no_odd_bank neg dx ; Y est déja une ligne impaire, on retire l'offset .no_odd_bank: - - mov si, BDA_CURSOR_BG ; DS:SI = buffer de sauvegarde + mov ax, BDA_MOUSE + mouse.bkg_buffer + mov si, ax ; DS:SI = buffer de sauvegarde mov bx, 8 and cx,0x0007 ; alignement X jnz .row3 .row3: ; sauve 3 bytes/ligne - mov byte [BDA_CURSOR_BYTES], 3 ; 3 bytes/ligne + mov byte [BDA_MOUSE + mouse.cur_bytes], 3 ; 3 bytes/ligne ; ligne 'y', peut etre paire ou impaire mov ax, word [es:di] ; charge first word mov word [ds:si], ax ; sauvegarde @@ -464,7 +464,7 @@ gfx_cursor_savebg: jmp .done .row2: ; sauve 2 bytes/ligne - mov byte [BDA_CURSOR_BYTES], 2 ; par défaut + mov byte [BDA_MOUSE + mouse.cur_bytes], 2 ; par défaut ; ligne 'y', peut etre paire ou impaire mov ax, word [es:di] ; charge first word mov word [ds:si], ax ; sauvegarde @@ -497,12 +497,12 @@ gfx_cursor_restorebg: mov ax, VIDEO_SEG mov es, ax - cmp byte [BDA_CURSOR_SAVED], 0 ; pas de background sauvé + cmp byte [BDA_MOUSE + mouse.bkg_saved], 0 ; pas de background sauvé je .done - mov cx,word [BDA_CURSOR_OLDX] ; restore de la position - mov dx,word [BDA_CURSOR_OLDY] - mov byte [BDA_CURSOR_SAVED], 0 ; le background a été restauré + mov cx,word [BDA_MOUSE + mouse.cur_oldx] ; restore de la position + mov dx,word [BDA_MOUSE + mouse.cur_oldy] + mov byte [BDA_MOUSE + mouse.bkg_saved], 0 ; le background a été restauré call gfx_calc_addr ; ES:DI = byteaddr pour (X,Y) mov dx,0x2000 ; offset pour lignes impaire @@ -510,10 +510,12 @@ gfx_cursor_restorebg: jl .no_odd_bank neg dx ; Y est déja une ligne impaire, on retire l'offset .no_odd_bank: - mov si, BDA_CURSOR_BG ; DS:SI = buffer de sauvegarde + + mov ax, BDA_MOUSE + mouse.bkg_buffer + mov si, ax ; DS:SI = buffer de sauvegarde mov bx, 8 - cmp byte [BDA_CURSOR_BYTES], 2 ; si la sauvegarde est en 2 bytes/ligne + cmp byte [BDA_MOUSE + mouse.cur_bytes], 2 ; si la sauvegarde est en 2 bytes/ligne je .row2 .row3: ; restore en 3 bytes/ligne diff --git a/src/drivers/mouse_ps2.asm b/src/drivers/mouse_ps2.asm index b59910a..42b174a 100644 --- a/src/drivers/mouse_ps2.asm +++ b/src/drivers/mouse_ps2.asm @@ -82,25 +82,27 @@ mouse_arrow: ; ------------------------------------------------------------ mouse_reset: mov ax, BDA_DATA_SEG - mov ds,ax + mov fs,ax ; effacer les variables du drivers - mov byte [BDA_MOUSE_IDX],0 - mov dword [BDA_MOUSE_BUFFER],0 - mov byte [DBA_MOUSE_PACKETLEN],3 + mov byte [fs:BDA_MOUSE + mouse.idx],0 + mov dword [fs:BDA_MOUSE + mouse.buffer],0 + mov byte [fs:BDA_MOUSE + mouse.packetlen],3 - mov byte [BDA_MOUSE_STATUS],0 - mov word [BDA_MOUSE_X],320 ; vous pouvez aussi préciser le centre - mov word [BDA_MOUSE_Y],100 ; vous pouvez aussi préciser le centre + 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 [BDA_MOUSE_WHEEL],0 + mov word [fs:BDA_MOUSE + mouse.wheel],0 - mov byte [BDA_CURSOR_VISIBLE], 1 - mov word [BDA_CURSOR_OLDX], 0 - mov word [BDA_CURSOR_OLDY], 0 + mov byte [fs:BDA_MOUSE + mouse.cur_visible], 1 + mov word [fs:BDA_MOUSE + mouse.cur_oldx], 0 + mov word [fs:BDA_MOUSE + mouse.cur_oldy], 0 - mov word [BDA_CURSOR_PTR], mouse_arrow + mov ax, cs + mov word [fs:BDA_MOUSE + mouse.cur_seg], ax + mov word [fs:BDA_MOUSE + mouse.cur_ofs], mouse_arrow ret ; ------------------------------------------------------------ @@ -200,7 +202,10 @@ mouse_init: ; ; ------------------------------------------------------------ mouse_detect_packet_len: - mov byte [DBA_MOUSE_PACKETLEN], 3 + mov ax, BDA_DATA_SEG + mov fs,ax + + mov byte [fs:BDA_MOUSE + mouse.packetlen], 3 ; SET_RATE + 200 mov bl, MOUSE_SET_RATE @@ -231,7 +236,7 @@ mouse_detect_packet_len: je .is4 ret .is4: - mov byte [DBA_MOUSE_PACKETLEN], 4 + mov byte [fs:BDA_MOUSE + mouse.packetlen], 4 ret ; ------------------------------------------------------------ @@ -332,51 +337,51 @@ isr_mouse_handler: ; 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_IDX] - mov byte [BDA_MOUSE_BUFFER + bx], al - inc byte [BDA_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, [DBA_MOUSE_PACKETLEN] - cmp byte [BDA_MOUSE_IDX], al + mov al, [BDA_MOUSE + mouse.packetlen] + cmp byte [BDA_MOUSE + mouse.idx], al jne .done ; packet complet, on decode les données - mov byte [BDA_MOUSE_IDX], 0 - mov al, [BDA_MOUSE_BUFFER] ; status - mov [BDA_MOUSE_STATUS], al + mov byte [BDA_MOUSE + mouse.idx], 0 + mov al, [BDA_MOUSE + mouse.buffer] ; status + mov [BDA_MOUSE + mouse.status], al - mov al, [BDA_MOUSE_BUFFER+1] ; delta X + mov al, [BDA_MOUSE + mouse.buffer+1] ; delta X cbw - add [BDA_MOUSE_X], ax + add [BDA_MOUSE + mouse.x], ax - mov al, [BDA_MOUSE_BUFFER+2] ; delta Y + mov al, [BDA_MOUSE + mouse.buffer+2] ; delta Y cbw - sub [BDA_MOUSE_Y], ax + sub [BDA_MOUSE + mouse.y], ax ; si packetlen = 4, gérer la molette; experimental - mov al, [BDA_MOUSE_BUFFER+3] ; delta Wheel + mov al, [BDA_MOUSE + mouse.buffer+3] ; delta Wheel cbw - add word [BDA_MOUSE_WHEEL], ax + add word [BDA_MOUSE + mouse.wheel], ax ; clamp X - cmp word [BDA_MOUSE_X], 0 + cmp word [BDA_MOUSE + mouse.x], 0 jge .x_ok_low - mov word [BDA_MOUSE_X], 0 + mov word [BDA_MOUSE + mouse.x], 0 .x_ok_low: - cmp word [BDA_MOUSE_X], 639 + cmp word [BDA_MOUSE + mouse.x], 639 jle .x_ok_high - mov word [BDA_MOUSE_X], 639 + mov word [BDA_MOUSE + mouse.x], 639 .x_ok_high: ; clamp Y - cmp word [BDA_MOUSE_Y], 0 + cmp word [BDA_MOUSE + mouse.y], 0 jge .y_ok_low - mov word [BDA_MOUSE_Y], 0 + mov word [BDA_MOUSE + mouse.y], 0 .y_ok_low: - cmp word [BDA_MOUSE_Y], 199 + cmp word [BDA_MOUSE + mouse.y], 199 jle .y_ok_high - mov word [BDA_MOUSE_Y], 199 + mov word [BDA_MOUSE + mouse.y], 199 .y_ok_high: ; restaurer l'ancien arrière plan du curseur ;call gfx_cursor_restorebg From c7509b186151604884d4d1cbdfd78c592d486b60 Mon Sep 17 00:00:00 2001 From: Frater <13979047+saintfrater@users.noreply.github.com> Date: Tue, 20 Jan 2026 16:12:07 +0100 Subject: [PATCH 4/5] merge to main + hires --- src/boot.asm | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/boot.asm b/src/boot.asm index b3897bc..f59945e 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -88,12 +88,10 @@ reset: call pic_set_irq_mask call kbd_init - - sti ; on active le mode graphique - ; call gfx_init + call gfx_init ;call mouse_reset call mouse_init @@ -146,7 +144,7 @@ test_isr: mov al, PIC_EOI ; EOI master out i8259_MASTER_CMD, al ; pop fs - + pop ax iret From 5e24809e345d2ea8c6e6c1cd04d4e97206c0ec0f Mon Sep 17 00:00:00 2001 From: Frater <13979047+saintfrater@users.noreply.github.com> Date: Tue, 20 Jan 2026 16:12:32 +0100 Subject: [PATCH 5/5] prepare HiRes --- src/boot.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/boot.asm b/src/boot.asm index f59945e..4b1bb3c 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -79,8 +79,8 @@ reset: call setup_check_vga ; on initialise le mode texte 80x25 par défaut DEBUG - mov ax, 0x0003 - int 10h + ;mov ax, 0x0003 + ;int 10h ; enable IRQ 0 mov ah,IRQ_ENABLED