***********************************
        	include stdmacro.ah
        	locals __

	OvrInfo struc
  	Reserv0 DD ?
  	FileOfs DD ?                                  ;File offset of a unit
  	OvrSize DW ?                                  ;Size of a segment, 08h
  	TabSize DW ?                                  ;Relocation table size, 0Ah
  	Reserv2 DW ?
  	Reserv3 DW ?
  	OvrAddr DW ?                                  ;Segment to store code, 10h
	OvrInfo ends

	.MODEL TPASCAL

	dataseg
     	extrn OvrCodeList: word
     	extrn OvrDosHandle: word
     	extrn PrefixSeg   : word

     	OvrFileBase equ DS:[offset OvrCodeList - 4]

	codeseg

     	public DosOvrReadFunc

	;function OvrReadFunc(OvrSeg: Word): Integer;  far;

	OvrSeg equ WP SS:[BX+4]

	proc DosOvrReadFunc
     	mov  BX, SP                                ;Make stack frame
     	mov  ES, OvrSeg                            ;Load info segment address
     	push DS                                    ;Save Turbo DS
     	mov  BX, OvrDosHandle                      ;Overlay file handle to BX
     	mov  DI, PrefixSeg                         ;PSP segment to DI
     	add  DI, 10h                               ;DI is a program image seg

     	mov  DX, WP ES:OvrInfo.FileOfs[0]          ;Set CX:DX to be file offset
     	mov  CX, WP ES:OvrInfo.FileOfs[2]          ;
     	add  DX, WP OvrFileBase[0]                 ;Correct file pointer to
     	adc  CX, WP OvrFileBase[2]                 ;absolute value
     	mov  AX, 4200h                             ;LSeek segment start
     	int  21h                                   ;

     	mov  DS, ES:OvrInfo.OvrAddr                ;Segment to read code
     	xor  DX, DX                                ;DS:DX -> overlay buffer
     	mov  CX, ES:OvrInfo.OvrSize                ;Byte count to read
     	DOSCall 3Fh                                ;Read code from disk
     	jc   __OExit                               ;Check, if read error
     	if_ AX { CX, __Error                       ;Check read byte count

     	add  AX, 0Fh                               ;Make paragraph block size
     	mov  CL, 4                                 ;& calculate segment for
     	shr  AX, CL                                ;overlay relocation table
     	add  AX, ES:OvrInfo.OvrAddr                ;
     	mov  DS, AX                                ;DS:DX -> place to store
     	xor  DX, DX                                ;relocation table
     	mov  CX, ES:OvrInfo.TabSize                ;Relocation table size
     	jcxz __ODone                               ;If no relo, all done
     	DOSCall 3Fh                                ;Read relocation table
     	jc   __OExit                               ;Exit, if read error
     	if_ AX { CX, __Error                       ;If read error, exit

     	shr  CX, 1                                 ;Calculate relocation count
     	xor  SI, SI                                ;Zero table addressing reg.
     	mov  ES, ES:OvrInfo.OvrAddr                ;Code address to ES
     	cld                                        ;SI increased
   	__ReloNext:
     	lodsw                                      ;Load relocation & copy it
     	mov  BX, AX                                ;to BX
     	add  ES:[BX], DI                           ;Correct segment fixup
   	loop __ReloNext

   	__ODone:
     	xor  AX, AX                                ;Return ovrOk code
     	jmp short __OExit                          ;& exit

   	__Error:
     	mov  AX, 100                               ;Return read error code

   	__OExit:  pop  DS                            ;Restore DS
     	retf 2                                     ;& return
	endp

	end

