h56225
s 00205/00000/00000
d D 1.1 83/03/22 20:12:40 bog 1 0
c date and time created 83/03/22 20:12:40 by bog
e
u
4
U
t
T
I 1
;
;  RUN  version 0.0  7/29/82
;	amended 27 Jan 83 by Bogus to check for valid load,
;	  also serialization crud
;
;
;  This program loads CMD files generated by LINK86
;  which need to be fixed-up at load-time due to
;  references to segment values (far jumps/calls, or
;  32-bit pointers to data).
;
;   some constants
;
cr	equ	0dh
lf	equ	0ah

fcb	equ	5ch		;default file control block
buff	equ	80h		;default buffer in base page for i/o
blen	equ	80h		;size of default buffer

Run_Code	cseg	Byte	Public

run:
	mov	dx, offset signon
	call	printm		;print signon message
;
	mov	bx,fcb+9	;point to first byte of filetype
	mov	byte ptr [bx],'C'
	mov	byte ptr 1[bx],'M'
	mov	byte ptr 2[bx],'D'	;insert default filetype
;
	mov	dx,fcb
	call	openfile	;open the CMD file to load
;
	mov	dx,fcb
	call	loadfile	;load the file using BDOS load function
				;return segment of basepage in bx
	Inc	Ax		; Successful load?
	Jnz	Load_Ok		; Jump if yes

	Mov	Dx,Offset No_Room
	Jmp	Error

;
Load_Ok:
	mov	es,bx		;segment of basepage in es
	mov	si,3		;point to location of first segment in basepage
	mov	di,offset segbasearray
	mov	cx,8		;pick up 8 segments from basepage
getseg:
	mov	ax,es:[si]	;get base of segment
	mov	[di],ax		;store it in seg base array
	add	si,6		;point to next segment
	add	di,2		;point to next element of array
	dec	cx		;decrement seg count
	jnz	getseg		;loop until table filled
;
mainloop:
	call	getbyte		;get next byte from CMD file
	or	al,al		;zero means no more fixups
	jz	done		;finish up and go to loaded program
;
	mov	saveal,al	;save group numbers
	call	getword		;get paragraph offset of location
	push	ax		;save it
	call	getbyte		;get byte offset of location
	mov	ah,0		;clear high order byte before save
	push	ax		;save it
	mov	al,saveal	;restore group numbers
	mov	ah,0		;clear high order byte
	mov	si,ax
	and	si,0fh		;si has index of target group
	dec	si		;normalize to (0-7) range
	add	si,si		;make 16-bit index into array
	mov	cl,4
	shr	al,cl		;get index of location group
	mov	di,ax
	dec	di		;normalize to (0-7)
	add	di,di		;16-bit index into seg array
;
	mov	bx,offset segbasearray
	mov	dx,[bx+si]	;get target value in dx
	pop	si		;restore byte offset
	pop	ax		;restore paragraph offset
	add	ax,[bx+di]	;add base of location seg
	mov	es,ax		;es has paragraph of location to fix
	add	es:[si],dx	;add target value to contents of location
	jmps	mainloop
;
done:
	mov	dx,fcb
	call	closefile
	mov	dx,segbasearray+2	;get ds base
	call	setdmab		;set up default dma base
	mov	dx,80h
	call	setdma		;set up default dma offset
	mov	bx,offset segbasearray
	push	word ptr [bx]	;save initial cs of loaded prog
	push	word ptr 2[bx]	;initial ds
	push	word ptr 4[bx]	;initial es
	pop	es		;set up es before retf
	pop	ds		;ds, too
	sub	ax,ax
	push	ax		;loaded program starts at offset 0
	retf			;far return goes to loaded program
;
getbyte:			;get next byte from CMD file
	cmp	bptr,blen	;at end of buffer?
	jc	get1		;if not, get next char
	push	ds
	pop	dx		;get data segment
	call	setdmab		;set up dma base
	mov	dx,buff
	call	setdma		;set up dma offset
	mov	dx,fcb
	call	readrecord	;else read another record
	mov	bptr,0		;and reset buffer pointer
get1:
	mov	bl,bptr
	mov	bh,0
	mov	al,buff[bx]	;get next char from buffer
	inc	bptr
	ret
;
getword:			;get next word from CMD file
	call	getbyte		;get low byte
	push	ax		;save it
	call	getbyte		;get high byte
	mov	ah,al
	pop	bx		;restore low byte
	mov	al,bl
	ret
;
printm:				;print message at console
	mov	cl,9
	jmps	bdos
;
openfile:
	mov	cl,15
	call	bdos
	inc	al
	mov	dx, offset openerr
	jz	error		;print message and abort on error
	ret
;
closefile:
	mov	cl,16
	call	bdos
	inc	al
	mov	dx, offset closeerr
	jz	error
	ret
;
readrecord:			;sequential file read
	mov	cl,20
	call	bdos
	or	al,al
	mov	dx, offset readerr
	jnz	error
	ret
;
setdma:				;set dma address (offset)
	mov	cl,26
	jmps	bdos
;
setdmab:			;set dma address (base)
	mov	cl,51
	jmps	bdos
;
loadfile:			;load an open file
	mov	cl,59
	jmps	bdos
;
error:
	call	printm		;print message and abort
;
reset:
	mov	cl,0
	mov	dl,0
bdos:
	int	0e0h
	ret
;
	dseg

signon	Db	'--------------------------------------------------',13,10
	Db	'RUN-86   Large Model .CMD Loader   27 Jan 83  Vb.3',13,10
	Db	'Serial No. G100-0000-654321    All Rights Reserved',13,10
	Db	'Copyright (c) 1983          Digital Research, Inc.',13,10
	Db	'--------------------------------------------------',13,10
	Db	'$'

No_Room		db	'Load error$'
closeerr	db	'Close error$'
openerr		db	'No file$'
readerr		db	'Disk read error$'

saveal		rb	1
bptr		db	blen		;buffer pointer
;
segbasearray	rw	8		;filled with load addresses of segments
					;as determined from basepage after load
;
	end

E 1
