; memory type is enum ; 00 - free ; 01 - unknown ; 02 - allocated by us ; 03 - reserved, like BIOS code or tables for the cpu ; 04 - unusable ; return the addr of the mtab ; out: es:bx mtab_ref: push cs pop es mov bx,mtab ret ; finds a place for a new item in the mm table ; this detects 'empty' slots by the size field being zero ; in: ds:si: pointer to first mtab item ; out: bx: offset for empty slot mtab_alloc: push ax call mtab_ref .loop: cmp WORD [es:bx],0x0000 je .end add bx,0x0010 jmp .loop .end: pop ax ret ; splits a mtab item at offset ; in: ds:si: pointer mtab item ; ax: split position in 16 byte blocks ; out: ds:si: pointer to lower half ; ds:di: pointer to upper half mtab_split: push cx ; remaining memory block length mov cx,[ds:si] mov [ds:si],ax ; this resizes the first item sub cx, ax push bx call mtab_alloc mov di,bx pop bx push si ; backup because movsw increments them push di ; ^ push cx ; counter for movsb mov es,ds mov cx,0x08 ; 8 words = 16 bytes rep movsw ; repeat cx times pop cx pop di pop si mov [ds:di],cx pop cx ret ; in: ax: segment ; bh: task id (0 for non-task memory) ; bl: memory type (see enum at top of file) ; cx: number of 16-byte blocks mtab_set: push ds push cx ; length of current item push dx ; cumulative position push bx ; pointer to mtab item call mtab_ref mov dx,0x0000 push bx ; use as iterator, restoring base value afterwards .findbase: ; we pick the mtab item where our segment lies in mov cx,[es:bx] add dx,cx cmp dx,ax jnbe .dupbase add bx,0x0010 jmp .findbase .dupbase: ; we duplicate it, so we potentially have one after and one before our new item mov si,bx call mtab_alloc mov di,bx pop bx push si ; backup because movsw increments them push di ; ^ push cx ; counter for movsb mov cx,0x08 ; a mtab item is 8 words = 16 bytes push es pop ds rep movsw pop cx pop di pop si .resizebase1: push ax ; size = [Segment ax] - ([End addr dx] - [Length cx]) sub ax,dx add ax,cx mov [si],ax pop ax .resizebase2: push dx sub dx,ax mov [di],dx pop dx .insert: push bx call mtab_alloc mov dx,bx pop bx .base1_link: push ax mov ax,dx sub ax,bx ; addr are relative to mtab start mov [si+02],ax pop ax .new_link: push ax mov ax,di sub ax,bx ; addr are relative to mtab start mov di,dx mov [di+02],ax pop ax pop bx mov [di+04],bx pop dx pop cx pop ds ret mtab_dump: push cs pop ds mov cx,0x0000 mov bp,mtab mov si,0x0000 .loop: push si mov ax, bp add ax, si add ax, 0x06 push ax mov al,[bp+si+0x05] push ax ; task id mov al,[bp+si+0x04] push bp push si mov bp,.typechar and ax,0x00FF mov si,ax mov al,[bp+si] pop si pop bp push ax ; memory type mov bx,cx add cx,[bp+si+0x00] mov ax,cx dec ax push ax ; end addr push bx ; start addr mov si,.linefmt call kprintf pop ax pop ax pop ax pop ax pop ax pop si ; look for next line mov ax,[bp+si+0x02] test ax,0xFFFF mov si,ax jnz .loop ret .linefmt: db "%X0 %XF %c %x %s", 0 .typechar: db "F?ARU", 0 ; pre-filled table for memory management align 16 mtab: ; interrupt vector table dw 0x0040 dw 0x0010 db 3 db 0 db "IVT", 0 align 16 ; bios data area (writes here during runtime) dw 0x0010 dw 0x0020 db 3 db 0 db "BDA", 0 align 16 dw 0x9FB0 dw 0x0030 db 0 db 0 db 0 align 16 dw 0x5000 dw 0x0040 db 4 db 0 db 0 align 16 dw 0x1000 dw 0x0000 db 4 db 0 db "BIOS ROM", 0 align 16 dw 0x0000 align 16 dw 0x0000 align 16 dw 0x0000 align 16 dw 0x0000