
; All Encompassing Memory Manager (EMS/CONV)
; EMS/XMS management code - Christopher Robert Evans 2005

emsfound   db 0
EMSID      db 'EMMXXXX0'
DREMSID    db 'EMMQXXX0'
EMSMSG     db 'EMS ',0
; EMS driver will handle DOS conventional memory support
dosram     dw 0

ems_handle	dw	0
ems_frame	dw	0		;seg of ems page, 0=no page
ems_initial_sz	dw	0		;16k-byte pages avail

dos_initial_seg	dw	0		;seg of initial dos seg
dos_initial_sz	dw	0		;paragraphs allocated

; ===========================================================================
DOS_Memory_Check proc far
; Check if any DOS memory is available DOS will report error if open
; impossible amount
; ===========================================================================
        mov  bx, -1
        mov  ah, 48h
        int  21h
        cmp  bx, 0     ; CF = CY BX=MAX
        je   _mo_nomem
        ; PS (periscope) it takes all of DOS memory, so use PSKEY and ot RUN.COM
        mov  dosram, bx  ; initial
        mov  ah, 48h
        int  21h
        jnc  _dosmem_ok
_mo_nomem:
        mov  ax, 0
        mov  dosram, ax
	stc
        ret
_dosmem_ok:
        clc
        mov   ax, dosram
        ret
DOS_Memory_Check endp

; ===========================================================================
ems_setup proc far
; ===========================================================================
        mov  ax, 0
        mov  emsfound, al

        mov  ax, 3567h  ; get dos vector
        int  21h
        ; ES:0A
        cld
        mov  si, offset EMSID
        mov  di, 0ah
        mov  cx, 8
        rep  cmpsb ;  DS:SI to ES:DI
        jcxz noemsdriver
        mov  si, offset DREMSID
        mov  di, 0ah
        mov  cx, 8
        rep  cmpsb ;  DS:SI to ES:DI
        jcxz noemsdriver
        stc
        mov  al, 1
        mov  emsfound, al
;        mov  ax, 4000h ; get EMS status
;        int  67h
;        or   ah, ah
;        jz   _noems
noemsdriver:
        mov  al, emsfound
        ret
ems_setup endp

; ===========================================================================
ems_info proc far
; ===========================================================================
_showems:
        mov   ax, dosram
        call putnum
        _echo ' '
        mov  al, emsfound
        or   al, al
        jz   _noemsmsg
        _print EMSMSG, 1
_noemsmsg:
        ret
ems_info endp

emsendcode    db 'X'
emsendblock   EQU emsendcode - emsfound
