; virtual memory mgr
bits 16
%define xvmemory

section .modhdr
    jmp init

section .text
    %include "std"
    %include "bits"
    %include "memory"
    %include "confseg"
    %include "printf"

    %define gdtidx2sel(i,r)    _gdtidx2sel i,r   ; gdtidx2sel :: int16 idx
                                                 ;   -> int8 ring -> int16 sel
    %define getempty()         _getempty         ; getempty :: void -> int16 idx
    %define growgdt()              _growgdt      ; growgdt :: void -> bool
    %define setentry(s,d,h,l,x)    _setentry s,d,h,l,x
    %define setentry(s,d,h,l,x,li) _setentry s,d,h,l,x,li
                                                ; setentry :: gdtentry *seg:dest
                                                ;   -> addr24 *hi:low
                                                ;   -> int8 access
                                                ;   -> int16 limit? -> bool
    %define show(s,a)           _show s,a       ; show :: gdtentry *seg:addr
                                                ;   -> void
    %define setgdtbitmap(i,x)   setbit(csselector,confseg.virbm,i,x)
                                                ; setgdtbitmap :: int16 idx
                                                ;   -> bool val -> void

    %macro _show 2
        mov ax,%2
        push ax
        mov ax,%1
        push ax
        call __show
        add sp,4
    %endmacro

    %macro _setentry 6
        mov ax,%6
        push ax
        mov ax,%5
        push ax
        mov ax,%4
        push ax
        mov ax,%3
        push ax
        mov ax,%2
        push ax
        mov ax,%1
        push ax
        call __setentry
        carry 6
    %endmacro
    %macro _setentry 5
        mov ax,0xffff
        push ax
        mov ax,%5
        push ax
        mov ax,%4
        push ax
        mov ax,%3
        push ax
        mov ax,%2
        push ax
        mov ax,%1
        push ax
        call __setentry
        carry 6
    %endmacro

    %macro _gdtidx2sel 2
        mov ax,%2
        push ax
        mov ax,%1
        push ax
        call __gdtidx2sel
        add sp,4
    %endmacro

    %macro _getempty 0
        call __getempty
    %endmacro

    %macro _growgdt 0
        call __growgdt
    %endmacro

    %include "virmem.s"
