From 1a283222f7fd5f9b8c211e1162339e530660e249 Mon Sep 17 00:00:00 2001 From: Frater <13979047+saintfrater@users.noreply.github.com> Date: Fri, 30 Jan 2026 16:21:35 +0100 Subject: [PATCH] api change done --- doc/API.md | 10 +- readme.md | 2 +- src/bda.asm | 10 - src/boot.asm | 8 +- src/drivers/gfx_cgam.asm | 659 ++++++++++++++++++++++++------------ src/services/generic.asm | 248 +++++++------- src/services/gui_window.asm | 315 +++++++++++++++++ 7 files changed, 898 insertions(+), 354 deletions(-) create mode 100644 src/services/gui_window.asm diff --git a/doc/API.md b/doc/API.md index 0b89cd6..091afef 100644 --- a/doc/API.md +++ b/doc/API.md @@ -1,4 +1,9 @@ -# BDA (Bios Data Area) Custom +# API + + * [CGA](/doc/CGA.md) controleur graphique + + +## BDA (Bios Data Area) Custom Le projet utilise une extension de la BDA standard IBM PC. @@ -16,4 +21,5 @@ Le projet utilise une extension de la BDA standard IBM PC. 0800:0000 : Stack (0x8000 - 0x18000 approx) ou Code (selon boot.asm). - B800:0000 : VRAM CGA. \ No newline at end of file + B800:0000 : VRAM CGA. + diff --git a/readme.md b/readme.md index 48953cb..d31f747 100644 --- a/readme.md +++ b/readme.md @@ -251,6 +251,6 @@ Ce projet est avant tout un **laboratoire d’apprentissage et d’exploration** 🚧 **Work in progress** Designed for experimentation, learning, and documentation. -parcourez l'[API ](/doc/API.md) pour plus d'informations +parcourez l'[API](/doc/API.md) pour plus d'informations --- diff --git a/src/bda.asm b/src/bda.asm index 6b01dc5..0e357dc 100644 --- a/src/bda.asm +++ b/src/bda.asm @@ -24,16 +24,6 @@ ; https://wiki.nox-rhea.org/back2root/ibm-pc-ms-dos/hardware/informations/bios_data_area ; -; Définition globale des parametres -%define arg1 [bp+4] -%define arg2 [bp+6] -%define arg3 [bp+8] -%define arg4 [bp+10] -%define arg5 [bp+12] -%define arg6 [bp+14] -%define arg7 [bp+16] -%define arg8 [bp+18] - %define BDA_SEGMENT 0x0040 ; segment BDA ; custom vars diff --git a/src/boot.asm b/src/boot.asm index f48a935..f86bf11 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -53,6 +53,7 @@ section .text %include "./drivers/mouse_ps2.asm" %include "./drivers/keyboard_ps2.asm" %include "./services/generic.asm" +%include "./services/gui_window.asm" err_vganok db 'VGA Not Initialized',0 @@ -104,6 +105,8 @@ reset: ; on active le mode graphique GFX INIT + ; DEMO + GFX GOTOXY, 290, 80 GFX TXT_MODE, GFX_TXT_WHITE_TRANSPARENT @@ -132,7 +135,10 @@ reset: GFX GOTOXY, 8,8 GFX WRITE, cs, helloworld - GFX GFX_CRS_UPDATE + GFX MOUSE_MOVE + + GFX LINE_VERT, 55, 30, 147, 1 + GFX LINE_VERT, 54, 31, 148, 0 mov cx,8 mov dx,24 diff --git a/src/drivers/gfx_cgam.asm b/src/drivers/gfx_cgam.asm index 4f21219..f40cdcc 100644 --- a/src/drivers/gfx_cgam.asm +++ b/src/drivers/gfx_cgam.asm @@ -46,10 +46,6 @@ %define GFX_TXT_WHITE 00000010b %define GFX_TXT_BLACK 00000011b -%macro GFX_DRV 1 - call word [cs:graph_driver + ((%1)*2)] -%endmacro - %macro GFX 1-* ; %1 est l'index de la fonction ; On itère sur les arguments suivants en sens inverse (Convention C) @@ -79,7 +75,13 @@ %define TXT_MODE 4 %define PUTCH 5 %define WRITE 6 -%define GFX_CRS_UPDATE 7 +%define LINE_VERT 7 +%define LINE_HORIZ 8 +%define DRAW_RECT 9 +%define FILL_RECT 10 +%define MOUSE_HIDE 11 +%define MOUSE_SHOW 12 +%define MOUSE_MOVE 13 ; ------------------------------------------------------------ ; COMMENTAIRE SUR LA MÉTHODE D'APPEL @@ -100,8 +102,14 @@ graph_driver: dw cga_set_writemode ; mode texte dw cga_putc ; dessin d'un caractère dw cga_write ; dessin d'une chaine de caractère - dw cga_mouse_cursor_move ; déplacement du curseur dw cga_line_vertical ; dessin d'une ligne verticale + dw cga_line_horizontal ; dessin d'une ligne horizontale + dw cga_draw_rect ; dessin d'un rectangle + dw cga_fill_rect ; dessin d'un rectangle plein + dw cga_mouse_hide ; déplacement du curseur + dw cga_mouse_show ; déplacement du curseur + dw cga_mouse_cursor_move ; déplacement du curseur + ; jmp gfx_fill_rect ; Remplissage rectangle (Nouveau) @@ -154,24 +162,26 @@ cga_calc_addr: ; si y est impaire, DI+=0x2000 ; DI = (y>>1)*80 + (x>>3) + (y&1)*0x2000 mov ax, dx - shr ax, 1 ; ax = y/2 + shr ax, 1 ; ax = y/2 mov di, ax - shl di, 4 ; (y/2)*16 - shl ax, 6 ; (y/2)*64 - add di, ax ; *80 + shl di, 4 ; (y/2)*16 + shl ax, 6 ; (y/2)*64 + add di, ax ; *80 + mov ax, cx - shr ax, 3 - add di, ax - test dl, 1 - jz .even - add di, CGA_ODD_BANK - .even: - ; masque bit = 0x80 >> (x&7) - push cx - and cl, 7 - mov ah, 080h - shr ah, cl - pop cx + shr ax, 3 ; x / 3 + add di, ax ; di = offset banque paire + + mov bx, dx + and bx, 1 ; BX = 0 ou 1 + neg bx ; BX = 0 ou 0xFFFF (-1) + and bx, 0x2000 ; BX = 0 ou 0x2000 (CGA_ODD_BANK) + add di, bx ; Application de l'offset + + ; calcul du masque du pixel + and cl, 7 + mov ah, 0x80 + shr ah, cl ret ; --------------------------------------------------------------------------- @@ -293,17 +303,20 @@ get_glyph_offset: ret ; --------------------------------------------------------------------------- -; cga_putc_unalign -; In : AL = char (ASCII) -; Uses: FS:BDA_GFX + gfx.cur_* (offset, add_ofs, shift, mode) -; Notes: +; cga_putc_unalign (car) ; - x non aligné (x&7 != 0) ; - écrit sur 2 bytes (di et di+1) dans chaque banque ; --------------------------------------------------------------------------- cga_putc: push bp mov bp, sp - sub sp, 2 ; variable locale + sub sp, 2 ; 1 word variable locale + + ; --- Définition des arguments --- + %define .car word [bp+4] + + ; --- Définition des variables locales --- + %define .cpt word [bp-2] pusha push fs @@ -314,7 +327,7 @@ cga_putc: mov bx, BDA_DATA_SEG mov fs, bx - mov ax, [bp+4] ; arg1 + mov ax, .car call get_glyph_offset @@ -324,7 +337,7 @@ cga_putc: mov ch, [fs:BDA_GFX + gfx.cur_mode] mov cl, [fs:BDA_GFX + gfx.cur_shift] - mov word [bp-2], 4 + mov .cpt, 4 .row_loop: ; gestion du fond de texte @@ -383,7 +396,7 @@ cga_putc: .next: add di,CGA_STRIDE - dec word [bp-2] + dec .cpt jnz .row_loop ; Avancer curseur d'un caractère (8 pixels) @@ -404,9 +417,13 @@ cga_write: mov bp, sp push ax - mov ax, arg1 + ; --- Définition des arguments --- + %define .txt_seg word [bp+4] + %define .txt_ofs word [bp+6] + + mov ax, .txt_seg mov ds, ax - mov si, arg2 + mov si, .txt_ofs .loops: lodsb @@ -422,6 +439,7 @@ cga_write: ret ; ------------------------------------------------------------ +; cga_putpixel (x,y,color) ; Dessine un pixel, accès VRAM direct. ; ; CX = x (0..639) @@ -429,11 +447,22 @@ cga_write: ; ES = Target Segment (usually VIDEO_SEG) ; BL = color (0=black, !=0=white) ; ------------------------------------------------------------ -cga_local_putpixel: - call cga_calc_addr +cga_putpixel: + push bp + mov bp, sp + + ; --- Définition des arguments --- + %define .x word [bp+4] + %define .y word [bp+6] + %define .color byte [bp+8] + + pusha + mov cx, .x + mov dx, .y + call cga_calc_addr ; write - cmp bl, 0 + cmp .color, 0 je .clear .set: @@ -445,56 +474,38 @@ cga_local_putpixel: and byte [es:di], ah .done: + popa + leave ret ; ------------------------------------------------------------ -; Dessine un pixel, accès VRAM direct. +; cga_getpixel (x,y) +; Lit un pixel, accès VRAM direct. ; -; CX = x (0..639) -; DX = y (0..199) -; BL = color (0=black, !=0=white) -; ------------------------------------------------------------ -cga_putpixel: - push ax - push di - push es - mov ax, VIDEO_SEG - mov es,ax - call cga_calc_addr - - ; write - cmp bl, 0 - je .clear - - .set: - or byte [es:di], ah - jmp .done - - .clear: - not ah - and byte [es:di], ah - - .done: - pop es - pop di - pop ax - ret - -; ------------------------------------------------------------ -; Lit un pixel (CX=x, DX=y) ; Out: AL=0/1 ; ------------------------------------------------------------ cga_getpixel: - push di - push es + push bp + mov bp, sp - call cga_calc_addr - mov al, [es:di] - and al, ah - setnz al + ; --- Définition des arguments --- + %define .x word [bp+4] + %define .y word [bp+6] - pop es - pop di + push di + push es + + mov cx, .x + mov dx, .y + + call cga_calc_addr + mov al, [es:di] + and al, ah + setnz al + + pop es + pop di + leave ret ; ------------------------------------------------------------ @@ -514,39 +525,12 @@ cga_background: rep stosd ret + + + ; ------------------------------------------------------------ -; Dessine un pixel, en [ES:DI] accès VRAM direct. +; cga_line_vertical (x, y0, y1, color) ; -; CX = x (0..639) -; DX = y (0..199) -; BL = color (0=black, !=0=white) -; ------------------------------------------------------------ -cga_putpixel_fast: - cmp bl, 0 - je .clear - - .set: - or byte [es:di], ah - jmp .done - - .clear: - not ah - and byte [es:di], ah - .done: - ret - -; ------------------------------------------------------------ -; Lit un pixel, en [ES:DI] accès VRAM direct. -; -; ------------------------------------------------------------ -cga_getpixel_fast: - mov al, [es:di] - and al, ah - setnz al - ret - -; ------------------------------------------------------------ -; cga_line_vertical ; Dessine une ligne x, y0, y1, color ; ------------------------------------------------------------ cga_line_vertical: @@ -557,223 +541,470 @@ cga_line_vertical: %define .x word [bp+4] %define .y1 word [bp+6] %define .y2 word [bp+8] - %define .color byte [bp+10] ; (Attention à l'alignement sur pile) + %define .color byte [bp+10] pusha push es - ; Setup ES = Video Segment (0xB800) - mov si, VIDEO_SEG - mov es, si + ; Setup ES = Video Segment + mov ax, VIDEO_SEG ; Utiliser AX plutot que SI pour setup ES + mov es, ax call cga_mouse_hide mov bx, .y1 mov dx, .y2 - ; Ordonner Y (BX < DX) + ; 1. Ordonner Y (BX = Debut, DX = Fin) cmp bx, dx jle .y_sorted xchg bx, dx .y_sorted: - ; Calculer l'adresse du premier point (X, Y_Start) -; push dx ; Sauve Y_Fin -; push cx ; Sauve Couleur + ; 2. Calculer la hauteur MAINTENANT (avant que DX/BX ne soient modifiés) + sub dx, bx ; DX = Hauteur - 1 + inc dx ; DX = Hauteur en pixels + push dx ; Sauvegarde de la hauteur (compteur) sur la pile [1] - mov cx, arg1 ; CX = X (pour cga_calc_addr) - mov dx, arg3 ; DX = Y_Start (pour cga_calc_addr) - call cga_calc_addr ; Return: DI=Offset, AH=BitMask (ex: 00100000) + ; 3. Calculer l'adresse de départ + mov cx, .x ; CX = X + mov dx, bx ; DX = Y_Start (Argument pour cga_calc_addr) + call cga_calc_addr ; DI = Offset, AH = Masque Bit - pop cx ; Récup Couleur - pop dx ; Récup Y_Fin (dans DX) + ; 4. Préparer les registres pour la boucle + mov al, ah ; AL = Masque + not al ; AL = ~Masque (pour effacer/noir) - ; Préparer le masque inverse pour le cas "Noir" (AND) - mov al, ah ; AL = Masque (00100000) - not al ; AL = ~Masque (11011111) + ; Charger la couleur (CORRECTION BUG COULEUR) + mov cl, .color ; On charge la couleur dans CL maintenant - ; BX sert maintenant de compteur courant, on a Y_Fin dans DX - ; Utilisons BP comme compteur de hauteur. - - sub dx, bx ; DX = Hauteur - 1 (delta) - inc dx ; DX = Nombre de pixels à dessiner + ; Récupérer le compteur de hauteur + pop dx ; DX = Hauteur restaurée [1] .v_loop: - cmp cl, 0 + cmp cl, 0 ; Test de la couleur (CL) jz .draw_black .draw_white: - or byte [es:di], ah ; Allumer le bit + or byte [es:di], ah ; Allumer le bit (AH) jmp .next_line .draw_black: - and byte [es:di], al ; Eteindre le bit + and byte [es:di], al ; Eteindre le bit (AL) .next_line: - ; --- Logique Next Line CGA (Optimisée) --- - xor di, 0x2000 ; Flip banque (Pair <-> Impair) - test di, 0x2000 ; Si on est passé à 0x2000 (Impair), on a juste descendu de 1 ligne visuelle - jnz .skip_add ; C'est bon. - add di, 80 ; Si on revient à 0x0000 (Pair), on doit avancer de 80 octets + ; --- Logique Next Line CGA --- + xor di, 0x2000 ; Bascule banque 0 <-> 1 + test di, 0x2000 ; Si on est passé à la banque 1 (0x2000) + jnz .skip_add ; On a visuellement descendu d'une ligne, c'est bon. + add di, 80 ; Si on revient banque 0, il faut avancer de 80 octets. .skip_add: - dec dx + dec dx ; Décrémenter le compteur de hauteur jnz .v_loop call cga_mouse_show pop es popa + leave ; Leave gère le 'mov sp, bp / pop bp' ret + ; ------------------------------------------------------------ ; cga_line_horizontal -; Dessine une ligne de (AX, DX) à (BX, DX) CL: color +; Dessine une ligne horizontale de (x1, y) à (x2, y) ; ------------------------------------------------------------ cga_line_horizontal: + push bp + mov bp, sp + + ; --- Définition des arguments --- + %define .x1 word [bp+4] + %define .x2 word [bp+6] + %define .y word [bp+8] + %define .color byte [bp+10] + pusha push es + ; Setup ES = Video Segment + mov ax, VIDEO_SEG + mov es, ax + call cga_mouse_hide - mov si, VIDEO_SEG - mov es, si + mov ax, .x1 + mov bx, .x2 - ; 1. Ordonner X (AX < BX) + ; 1. Ordonner X (AX = Gauche, BX = Droite) cmp ax, bx jle .x_sorted xchg ax, bx - .x_sorted: - ; Calculer l'adresse de départ - ; On a besoin de l'adresse de l'octet de AX. - push cx ; Sauve Couleur - push bx ; Sauve X_Fin - mov cx, ax ; CX = X_Start - ; DX est déjà Y - call cga_calc_addr ; DI = Offset du premier byte - ; AH = Masque du bit précis (pas utile pour le remplissage, on recalculera) + ; 2. Calculer l'adresse de départ + ; On a besoin de l'adresse de l'octet contenant AX. + ; AX (X_Start) et BX (X_End) sont conservés. - pop bx ; Récup X_Fin - pop cx ; Récup Couleur (CL) + mov cx, ax ; CX = X_Start pour cga_calc_addr + mov dx, .y ; DX = Y + + push bx ; Sauvegarde X_End [1] + call cga_calc_addr ; DI = Offset VRAM, AH = BitMask (on ne servira pas de AH ici) + pop bx ; Récupère X_End [1] + + ; 3. Préparer la couleur + mov dl, .color ; DL = Couleur (0 ou 1) ; --- ANALYSE DES OCTETS --- - ; Start Byte Index = AX >> 3 - ; End Byte Index = BX >> 3 + ; Index Octet Début = X1 >> 3 + ; Index Octet Fin = X2 >> 3 - mov si, ax ; SI = X_Start - shr si, 3 ; Byte index start + mov si, ax + shr si, 3 ; SI = Index octet start - mov bp, bx ; BP = X_Fin - shr bp, 3 ; Byte index end + mov cx, bx + shr cx, 3 ; CX = Index octet end - ; Cas spécial : Tout tient dans le même octet ? - cmp si, bp + ; Cas spécial : Tout dans le même octet ? + cmp si, cx je .single_byte ; ============================================ - ; CAS MULTI-BYTE + ; CAS MULTI-BYTE (Gauche -> Milieu -> Droite) ; ============================================ - ; --- 1. Gérer l'octet de GAUCHE (Start) --- + ; --- A. PARTIE GAUCHE (Start) --- ; On doit dessiner du bit (AX&7) jusqu'au bit 0 (droite de l'octet) ; Masque = 0xFF >> (AX & 7) - push cx ; Save couleur + push cx ; Sauve Index End mov cx, ax - and cx, 7 ; X % 8 - mov al, 0xFF - shr al, cl ; AL = Masque Gauche (ex: si X%8=2, 00111111) - pop cx ; Restore couleur + and cx, 7 ; CL = X1 % 8 + mov dh, 0xFF + shr dh, cl ; DH = Masque Gauche (ex: 00111111) - call .apply_mask_at_di ; Applique AL sur [ES:DI] selon CL + call .apply_mask ; Applique DH sur [ES:DI] selon couleur DL inc di ; Octet suivant inc si ; Index suivant + pop cx ; Restaure Index End - ; --- 2. Gérer le MILIEU (Full Bytes) --- - ; Tant que SI < BP, on remplit tout l'octet - jmp .check_middle + ; --- B. PARTIE CENTRALE (Full Bytes) --- + ; Tant que SI < CX, on remplit tout l'octet + cmp si, cx + jge .do_right_part ; Si on a atteint le dernier octet, on saute - .loop_middle: - cmp cl, 0 - jz .middle_black - mov byte [es:di], 0xFF ; Blanc total - jmp .middle_next + mov al, 0x00 ; Couleur Noir par défaut + cmp dl, 0 + jz .fill_loop + mov al, 0xFF ; Couleur Blanc - .middle_black: - mov byte [es:di], 0x00 ; Noir total - - .middle_next: + .fill_loop: + mov byte [es:di], al inc di inc si + cmp si, cx + jl .fill_loop - .check_middle: - cmp si, bp - jl .loop_middle ; Continue tant qu'on n'est pas au dernier octet - - ; Gérer l'octet de DROITE (End) --- + .do_right_part: + ; --- C. PARTIE DROITE (End) --- ; On doit dessiner du bit 7 jusqu'au bit (BX&7) ; Masque = ~(0xFF >> ((BX & 7) + 1)) - ; Ex: Si Fin%8 = 0 (1 pixel), shift 1 -> 01111111, NOT -> 10000000. Correct. - push cx mov cx, bx and cx, 7 - inc cx ; Nb de pixels à garder à gauche - mov al, 0xFF - shr al, cl ; Masque des pixels à IGNORER (droite) - not al ; Masque des pixels à DESSINER (gauche) - pop cx + inc cx ; +1 car on veut inclure le pixel + mov dh, 0xFF + shr dh, cl + not dh ; DH = Masque Droite (ex: 11100000) - call .apply_mask_at_di + call .apply_mask jmp .done ; ============================================ ; CAS SINGLE BYTE (Début et Fin dans le même octet) ; ============================================ .single_byte: - ; Masque Start = 0xFF >> (AX & 7) - push cx + ; Masque Gauche = 0xFF >> (X1 & 7) mov cx, ax and cx, 7 - mov ah, 0xFF - shr ah, cl ; AH = Masque départ (ex: 00011111) - - ; Masque End (Pixels à garder jusqu'à BX) - ; On veut garder les pixels de 7 jusqu'à BX&7 - ; Astuce: Masque Combiné = MasqueStart AND MasqueEnd + mov dh, 0xFF + shr dh, cl + ; Masque Droite = ~(0xFF >> ((X2 & 7) + 1)) mov cx, bx and cx, 7 inc cx - mov al, 0xFF - shr al, cl - not al ; AL = Masque Fin (ex: 11110000) + mov ah, 0xFF ; Utilise AH temporaire + shr ah, cl + not ah - and al, ah ; Intersection des deux masques - pop cx + ; Intersection des masques + and dh, ah ; DH = Masque Final (ex: 00011100) - call .apply_mask_at_di + call .apply_mask jmp .done - ; --- Helper interne : Applique le masque AL à l'adresse DI selon couleur CL --- - .apply_mask_at_di: - cmp cl, 0 + ; --- Helper local : Applique le masque DH sur [ES:DI] --- + ; Entrée : DH = Masque, DL = Couleur, DI = Adresse + .apply_mask: + cmp dl, 0 jz .apply_black - or [es:di], al ; Blanc: OR Masque + or byte [es:di], dh ; Blanc : OR ret - .apply_black: - not al ; Noir: AND ~Masque - and [es:di], al + not dh + and byte [es:di], dh ; Noir : AND ~Masque + not dh ; Restaure le masque (optionnel si non réutilisé) ret .done: + call cga_mouse_show + pop es + popa + leave + ret + +; ------------------------------------------------------------ +; cga_draw_rect +; Dessine un rectangle vide (contour) +; Entrée : x1, y1, x2, y2, color +; ------------------------------------------------------------ +cga_draw_rect: + push bp + mov bp, sp + + ; Arguments + %define .x1 word [bp+4] + %define .y1 word [bp+6] + %define .x2 word [bp+8] + %define .y2 word [bp+10] + %define .color word [bp+12] ; word pour l'alignement pile (mais on utilise byte) + + pusha + + ; 1. Ordonner X (x1 < x2) + mov ax, .x1 + mov bx, .x2 + cmp ax, bx + jle .x_ok + xchg ax, bx + mov .x1, ax + mov .x2, bx + .x_ok: + + ; 2. Ordonner Y (y1 < y2) + mov ax, .y1 + mov bx, .y2 + cmp ax, bx + jle .y_ok + xchg ax, bx + mov .y1, ax + mov .y2, bx + .y_ok: + + ; 3. Dessiner les 4 lignes + ; Pour éviter les pixels en double dans les coins, on peut ajuster légèrement, + ; mais pour un driver simple, tracer les 4 lignes brutes est acceptable. + + ; Haut (x1,y1 -> x2,y1) + push word .color + push word .y1 + push word .x2 + push word .x1 + call cga_line_horizontal + add sp, 8 + + ; Bas (x1,y2 -> x2,y2) + push word .color + push word .y2 + push word .x2 + push word .x1 + call cga_line_horizontal + add sp, 8 + + ; Gauche (x1,y1 -> x1,y2) + push word .color + push word .y2 + push word .y1 + push word .x1 + call cga_line_vertical + add sp, 8 + + ; Droite (x2,y1 -> x2,y2) + push word .color + push word .y2 + push word .y1 + push word .x2 + call cga_line_vertical + add sp, 8 + + popa + leave + ret + +; ------------------------------------------------------------ +; cga_fill_rect +; Dessine un rectangle plein +; Entrée : x1, y1, x2, y2, color +; ------------------------------------------------------------ +cga_fill_rect: + push bp + mov bp, sp + + %define .x1 word [bp+4] + %define .y1 word [bp+6] + %define .x2 word [bp+8] + %define .y2 word [bp+10] + %define .color byte [bp+12] + + pusha + push es + + ; Setup ES + mov ax, VIDEO_SEG + mov es, ax + + call cga_mouse_hide + + ; 1. Tri des coordonnées + mov ax, .x1 + mov bx, .x2 + cmp ax, bx + jle .x_sorted + xchg ax, bx + mov .x1, ax + mov .x2, bx + .x_sorted: + + mov ax, .y1 + mov bx, .y2 + cmp ax, bx + jle .y_sorted + xchg ax, bx + mov .y1, ax + mov .y2, bx + .y_sorted: + + ; Calculer hauteur + sub bx, ax ; BX = y2 - y1 + inc bx ; BX = hauteur + mov dx, bx ; DX = Compteur de hauteur + + ; 2. Calculer l'adresse de départ (Haut-Gauche) + mov cx, .x1 ; CX = x1 + mov dx, .y1 ; DX = y1 + push dx ; Sauve Y1 (car cga_calc_addr peut modifier DX/BX selon implémentation) + + ; Note: cga_calc_addr attend CX=x, DX=y + call cga_calc_addr ; DI = Offset début ligne + + pop bx ; Récupère Y1 dans BX (pour savoir si pair/impair) + + ; 3. Préparer les masques gauche/droite (comme Line Horizontal) + ; Masque Gauche + mov cx, .x1 + and cx, 7 + mov dh, 0xFF + shr dh, cl ; DH = Masque Gauche + + ; Masque Droite + mov cx, .x2 + and cx, 7 + inc cx + mov ah, 0xFF + shr ah, cl + not ah ; AH = Masque Droite + + ; Index octets + mov si, .x1 + shr si, 3 ; SI = Index start byte + mov cx, .x2 + shr cx, 3 ; CX = Index end byte + + ; Récupérer la hauteur (Y2 - Y1 + 1) + mov bp, .y2 + sub bp, .y1 + inc bp ; BP = Hauteur (Loop counter) + + ; Sauvegarder l'état pair/impair initial + ; Si Y1 est impair, le premier saut de ligne doit être "vers le haut" (-0x2000 + 80) + ; ou plus simplement : on gère le flip 0x2000 à chaque ligne. + + ; --- BOUCLE VERTICALE --- + .row_loop: + push di ; Sauve le début de la ligne actuelle + push si ; Sauve index byte start + + ; --- Remplissage Horizontal (copié de line_horizontal) --- + + ; Cas Single Byte + cmp si, cx + je .single_byte_row + + ; Partie Gauche + mov al, dh ; Masque Gauche + call .apply_mask_fill + inc di + inc si + + ; Partie Centrale + jmp .check_mid + .mid_loop: + cmp byte .color, 0 + je .blk + mov byte [es:di], 0xFF + jmp .nxt + .blk: + mov byte [es:di], 0x00 + .nxt: + inc di + inc si + .check_mid: + cmp si, cx + jl .mid_loop + + ; Partie Droite + mov al, ah ; Masque Droite + call .apply_mask_fill + jmp .row_done + + .single_byte_row: + mov al, dh + and al, ah ; Intersection masques + call .apply_mask_fill + + .row_done: + pop si + pop di ; Restaure début de ligne + + ; --- Passage à la ligne suivante (Logique CGA optimisée) --- + xor di, 0x2000 ; Flip banque + test di, 0x2000 + jnz .bank_switch_ok ; Si on passe de 0->1, on a descendu d'une ligne visuelle + add di, 80 ; Si on passe de 1->0, on remonte, donc faut ajouter 80 + .bank_switch_ok: + + dec bp + jnz .row_loop call cga_mouse_show pop es popa + leave + ret + +; Helper local pour Fill Rect +; AL = Masque, .color = couleur +.apply_mask_fill: + cmp byte .color, 0 + jz .ap_black + or byte [es:di], al + ret +.ap_black: + not al + and byte [es:di], al ret ; ; ------------------------------------------------------------ diff --git a/src/services/generic.asm b/src/services/generic.asm index f637fcc..c624480 100644 --- a/src/services/generic.asm +++ b/src/services/generic.asm @@ -27,25 +27,26 @@ ; Si une ROM est détectée, le code de la ROM sera appelé. ; --------------------------------------------------------------------------- setup_load_rom: - mov bx, 0xC000 -.scanloop: - mov ds, bx - cmp word[0x0000],0xAA55 ; ROM signature - jne .norom + mov bx, 0xC000 - ; appel le code (en 0x0003) de la ROM - push cs - push word .norom ; address de retour + .scanloop: + mov ds, bx + cmp word[0x0000],0xAA55 ; ROM signature + jne .norom - push ds - push 0x0003 - retf ; call far ds:0x0003 -.norom: - add bx, 0x80 ; add 2kb - cmp bx, 0xE000 - jbe .scanloop -.end: - ret + ; appel le code (en 0x0003) de la ROM + push cs + push word .norom ; address de retour + + push ds + push 0x0003 + retf ; call far ds:0x0003 + .norom: + add bx, 0x80 ; add 2kb + cmp bx, 0xE000 + jbe .scanloop + .end: + ret ; --------------------------------------------------------------------------- ; Test si le vecteur INT 10h à été modifié (par la ROM) @@ -54,47 +55,48 @@ setup_load_rom: ; été chargée. ; --------------------------------------------------------------------------- setup_check_vga: - ; vérification de l'offset 0x0000:0x0040 qui DOIT etre différent de @default_isr - 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 + ; vérification de l'offset 0x0000:0x0040 qui DOIT etre différent de @default_isr + 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 - ; 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 + ; 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 ax,cs ; vecteur identique, sans doute pas d'initialisation - mov ds,ax -.init_ok: - ret + mov ax,cs ; vecteur identique, sans doute pas d'initialisation + mov ds,ax + .init_ok: + ret ; --------------------------------------------------------------------------- ; IVT install (8088, mode réel) - remplit les 256 vecteurs avec un handler IRET ; --------------------------------------------------------------------------- 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 + ; 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 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 + 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 ; --------------------------------------------------------------------------- ; IVT install (8088, mode réel) - installe un handler a une interrupt calculée @@ -104,30 +106,30 @@ 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 - cli - stosw ; write offset (AX) -> [ES:DI], DI += 2 - xchg ax, dx ; AX = segment, DX = offset - stosw ; write segment (AX) -> [ES:DI], DI += 2 - sti + mov ax, bx ; offset du handler + cli + stosw ; write offset (AX) -> [ES:DI], DI += 2 + xchg ax, dx ; AX = segment, DX = offset + stosw ; write segment (AX) -> [ES:DI], DI += 2 + sti - pop di - pop es - ret + pop di + pop es + ret io_wait: - xor al,al - out 0x80,al - ret + xor al,al + out 0x80,al + ret ; --------------------------------------------------------------------------- ; Initialise les PIC 8259 (PC/AT): @@ -137,39 +139,39 @@ io_wait: ; https://wiki.nox-rhea.org/back2root/ibm-pc-ms-dos/hardware/8259 ; --------------------------------------------------------------------------- pic_init: - cli - 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 + ; 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 - out i8259_SLAVE_CMD, al + ; starts the initialization sequence (in cascade mode) + mov al, ICW1_INIT | ICW1_ICW4 + out i8259_MASTER_CMD, al + out i8259_SLAVE_CMD, al - 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 + 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 + ; 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 - ; 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) + ; 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) - ; ICW4: 8086 mode - mov al, ICW4_8086 - out i8259_MASTER_DATA, al - out i8259_SLAVE_DATA, al + ; ICW4: 8086 mode + mov al, ICW4_8086 + out i8259_MASTER_DATA, al + out i8259_SLAVE_DATA, al - pop ax - ret + pop ax + ret ; ------------------------------------------------------------------ ; set_irq_mask @@ -177,54 +179,48 @@ pic_init: ; 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 + pusha - mov cl, al ; CL = numéro d'IRQ - ; Par défaut, PIC Maître (Data port) - mov dx, i8259_MASTER_DATA + 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 + 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 + ; 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) + ; 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) + in al, dx ; Lire l'IMR actuel depuis le port (0x21 ou 0xA1) - test ah, ah ; AH est-il à 1 (Disable) ? - jnz .do_disable + 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_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) + .do_disable: + or al, bl ; Mettre le bit à 1 (Mask) -.write_back: - out dx, al ; Écrire le nouveau masque dans l'IMR + .write_back: + out dx, al ; Écrire le nouveau masque dans l'IMR - pop ax ; Restaurer AX - pop dx - pop cx - pop bx - ret + popa + 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 diff --git a/src/services/gui_window.asm b/src/services/gui_window.asm new file mode 100644 index 0000000..262f122 --- /dev/null +++ b/src/services/gui_window.asm @@ -0,0 +1,315 @@ +; ============================================================================= +; Project : Custom BIOS / ROM +; File : gui_window.asm +; Author : frater +; +; 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 . +; +; ============================================================================= + +%macro GUI 1-* + ; %1 est l'index de la fonction + ; On itère sur les arguments suivants en sens inverse (Convention C) + + %rep %0 - 1 ; Répéter pour (Nombre d'args - 1) + %rotate -1 ; Prend le dernier argument + push %1 ; L'empile + %endrep + + %rotate -1 ; On revient au premier argument (l'index de fonction) + call word [cs:graph_driver + ((%1)*2)] + + ; Nettoyage de la pile (Convention CDECL: l'appelant nettoie) + ; Chaque argument = 2 octets (1 word). + add sp, (%0 - 1) * 2 +%endmacro + +; ------------------------------------------------------------ +; TABLE DE SAUT (VECTEURS API) +; Cette table doit être située au début du driver pour être +; accessible par la GUI à des offsets fixes. +; ------------------------------------------------------------ +%define WINDOW 0 + +; ------------------------------------------------------------ +; COMMENTAIRE SUR LA MÉTHODE D'APPEL +; +; GFX FUNCTION, Arg1, Arg2, Arg3 +; +; GFX GFX_PUTPIXEL, 320, 100, 1 +; +; Si vous changez le code du driver, tant que la table au début +; ne change pas d'ordre, la GUI n'a pas besoin d'être recompilée. +; ------------------------------------------------------------ + +gui_driver: + dw draw_white + +; ============================================================================= +; Fonction : draw_window +; Description : Dessine une fenêtre style Mac System 1.0 +; Entrées (Pile) : +; Arg1 : X (Word) +; Arg2 : Y (Word) +; Arg3 : Largeur (Word) +; Arg4 : Hauteur (Word) +; Arg5 : Pointeur vers titre (DS:Offset) (Word) +; ============================================================================= +draw_window: + push bp + mov bp, sp + + ; --- Arguments --- + %define .x word [bp+4] + %define .y word [bp+6] + %define .w word [bp+8] + %define .h word [bp+10] + %define .title word [bp+12] + + pusha + + ; Variables locales (registres) + ; BX = x2, DX = y2 + + ; ---------------------------------------------------- + ; Ombre Portée (Drop Shadow) + ; ---------------------------------------------------- + ; On dessine un rectangle noir décalé de +1,+1 + ; x1+1, y1+1, x2+1, y2+1 -> NOIR + mov ax, .x + add ax, .w + inc ax ; x2 + 1 + mov bx, ax ; BX = right limit + + mov ax, .y + add ax, .h + inc ax ; y2 + 1 + mov dx, ax ; DX = bottom limit + + mov ax, .x + inc ax ; x1 + 1 + mov cx, .y + inc cx ; y1 + 1 + + ; Appel cga_fill_rect(x+1, y+1, x+w+1, y+h+1, BLACK) + push word 0 ; Couleur 0 (Noir) pour l'ombre + push dx ; y2 + push bx ; x2 + push cx ; y1 + push ax ; x1 + call cga_fill_rect + add sp, 10 + + ; ---------------------------------------------------- + ; Corps de la Fenêtre (Main Body) + ; ---------------------------------------------------- + ; On dessine un rectangle BLANC par dessus l'ombre + ; Cela crée le corps et "découpe" l'ombre pour ne laisser que le bord visible. + + mov bx, .x + add bx, .w ; BX = x2 + mov dx, .y + add dx, .h ; DX = y2 + + ; Remplissage Blanc (Corps) + push word 1 ; Couleur 1 (Blanc) + push dx + push bx + push word .y + push word .x + call cga_fill_rect + add sp, 10 + + ; Contour Noir (Border) + push word 0 ; Couleur 0 (Noir) + push dx + push bx + push word .y + push word .x + call cga_draw_rect + add sp, 10 + + ; ---------------------------------------------------- + ; Barre de Titre (Title Bar) + ; ---------------------------------------------------- + ; Hauteur standard ~18 pixels + %define TITLE_HEIGHT 18 + + ; Dessiner la ligne de séparation sous le titre + mov cx, .y + add cx, TITLE_HEIGHT + + push word 0 ; Couleur Noir + push cx ; Y de la ligne + push bx ; X2 (largeur fenêtre) + push word .x ; X1 + call cga_line_horizontal + add sp, 8 + + ; --- Pinstripes (Lignes horizontales) --- + ; On dessine une ligne noire une ligne sur deux à l'intérieur du titre + ; De y+1 à y+17, step 2 + + mov si, .y + inc si ; Commence à y+1 + inc si ; Premier trait noir à y+2 (laissons 1px blanc sous le cadre haut) + + mov di, .y + add di, TITLE_HEIGHT + dec di ; Fin juste avant la ligne de séparation + + .pinstripe_loop: + cmp si, di + jae .pinstripe_done + + push word 0 ; Noir + push si ; Y courant + push bx ; X2 + push word .x ; X1 + call cga_line_horizontal + add sp, 8 + + add si, 2 ; Sauter 2 pixels (1 blanc, 1 noir) + jmp .pinstripe_loop + .pinstripe_done: + + ; ---------------------------------------------------- + ; Le Carré de Fermeture (Close Box) + ; ---------------------------------------------------- + ; Petit carré blanc à gauche avec contour noir + ; Position: x+6, y+4, taille 9x9 (exemple) + + mov cx, .x + add cx, 6 ; Box X1 + mov dx, .y + add dx, 4 ; Box Y1 + + mov si, cx + add si, 9 ; Box X2 + mov di, dx + add di, 9 ; Box Y2 + + ; Remplir en blanc (pour effacer les pinstripes) + push word 1 + push di + push si + push dx + push cx + call cga_fill_rect + add sp, 10 + + ; Contour noir + push word 0 + push di + push si + push dx + push cx + call cga_draw_rect + add sp, 10 + + ; ---------------------------------------------------- + ; Le Titre (Texte) + ; ---------------------------------------------------- + ; Il faut centrer le texte et effacer les lignes derrière lui. + + ; a) Calculer la longueur du titre + mov si, .title + xor cx, cx ; CX = longueur + .strlen: + lodsb + test al, al + jz .calc_center + inc cx + jmp .strlen + + .calc_center: + ; b) Calcul X Centré = X_win + (W_win/2) - (Len * 8 / 2) + mov ax, .w + shr ax, 1 ; W / 2 + add ax, .x ; Centre de la fenêtre + + mov bx, cx ; Sauver Len + shl cx, 2 ; Len * 4 (car Len * 8 / 2 = Len * 4) + sub ax, cx ; AX = X de départ du texte + + ; c) Effacer la zone derrière le texte (Remplir Blanc) + ; On ajoute un padding de 4px autour du texte + mov cx, ax ; Text X start + sub cx, 4 ; Padding gauche (Rect X1) + + mov dx, .y + add dx, 2 ; Rect Y1 (juste sous le cadre haut) + + push bx ; Sauve longueur char pour plus tard + + ; Calcul Rect X2 = Text X + (Len * 8) + 4 + mov si, ax ; Start Text + mov ax, bx ; Len + shl ax, 3 ; Len * 8 (largeur pixel texte) + add si, ax + add si, 4 ; Padding droite (Rect X2) + + mov di, .y + add di, TITLE_HEIGHT + dec di ; Rect Y2 (juste au dessus ligne séparation) + + ; Dessiner le rectangle blanc de masquage + push word 1 ; Blanc + push di ; Y2 + push si ; X2 + push dx ; Y1 + push cx ; X1 + call cga_fill_rect + add sp, 10 + + pop bx ; Récupérer longueur + + ; d) Écrire le texte + ; Recalculer position X exacte (CX avait X1 du rect, on veut X1 + 4) + add cx, 4 + + ; GFX GOTOXY (X, Y) -> Y centré dans la barre de titre + ; Centre barre ~9px. Font 8px. Y = Win_Y + 5 + mov dx, .y + add dx, 5 + + ; Note: GFX GOTOXY utilise des arguments pile, attention à l'ordre API + ; On suppose que vous utilisez votre macro GFX ou appel direct + + ; GFX GOTOXY, cx, dx + push dx ; Y + push cx ; X + push word 3 ; ID GOTOXY + call cga_set_charpos ; Appel direct driver ou via vecteur + add sp, 6 + + ; Mode texte Noir sur Blanc (ou Transparent, car on a mis un fond blanc) + ; GFX TXT_MODE, GFX_TXT_BLACK + push word 3 ; GFX_TXT_BLACK_ON_WHITE (ou transparent) + push word 4 ; ID TXT_MODE + call cga_set_writemode + add sp, 4 + + ; GFX WRITE, cs, title + push word .title ; Offset + push ds ; Segment (supposé DS, sinon passer le seg en arg) + push word 6 ; ID WRITE + call cga_write + add sp, 6 + + popa + leave + ret \ No newline at end of file