; -------- DPMI Functions for GABasic --------
; Gabriel Fernandez 2000

; DPMI calls are used to create arrays and strings
; so arrays can have a size of free ram

; *------------ GetMaxSize
; RET: EAX = Largest Block

GetMaxSize:
  MOV AX, 500h
  MOV EDI, meminfo
  INT 31h
  JC DpmiError
    mov ebx, meminfo
    mov eax, [ebx]
  RET
DpmiError:
  MOV ax, 1
  CALL NEAR EndProgram

; END GetMaxSize -------------*

; *------------ AllocBlock
; IN: EAX = Size
; RET: EAX = Linear Address
;      SI:DI = Handle
AllocBlock:
  PUSH EAX
  Call NEAR GetMaxSize
  POP EBX
  CMP EBX, EAX
  JG DpmiError2
    mov eax, tmp
    mov [eax], ebx
    mov bx, [eax+2]
    mov cx, [eax]
    MOV AX, 501h
    INT 31h
    JC DpmiError2
      mov eax, tmp
      mov [eax+2], bx
      mov [eax], cx
      mov ebx, [eax]
      mov eax, ebx
    RET
DpmiError2:
  mov ax, 1
  CALL NEAR EndProgram
; END AllocBlock -------------*

; *------------ ChangeBlockSize
; IN: EAX = New Size      SI:DI = Old Handle
; RET: EAX = New Linear Address
;      SI:DI = New Handle

ChangeBlockSize:
  mov ebx, tmp
  mov [ebx], eax
  mov cx, [ebx]
  mov ax, [ebx+2]
  mov bx, ax
  mov ax, 503h
  INT 31h
  JC DpmiError3
      mov eax, tmp
      mov [eax+2], bx
      mov [eax], cx
      mov ebx, [eax]
      mov eax, ebx
   RET
DpmiError3:
  mov ax, 2
  CALL NEAR EndProgram
; END ChangeBlockSize -------------*


; *------------ FreeBlock
; IN: SI:DI = Handle to free
FreeBlock:
  MOV ax, 502h
  INT 31h
  RET
; END FreeBlock -------------*

; ---- MoveData
; EAX, dest
; EBX, Src   ECX, Size
           
MoveData:
  PUSH ECX
  MOV ESI, EAX
  MOV EDI, EBX
  REP MOVSB       
  POP ECX
RET        ; END MoveData

; ---- GetSeg
; EAX: Pmode Address
; Rets:  EAX - Segment
;        EBX - Offset

Getseg:
  mov ebx, 10h
  mov edx, 0
  div ebx
  mov ebx, edx
RET
