	title	Library Run Tool Loader - LRUN RSX
	name	('LRUNRSX')
	include	LRUN.LIB

; This version bases upon LRUN20.ASM by Gary P. Novosielski, 1982
; It is the relocated loader part now implemented by an RSX

; It now requires CP/M 3.x as well as a Z80 CPU
; Modified by W.Cirsovius

; ===== RSX header starts =====
;
LRSX:
	db	0,0,0,0,0,0	; Six byte serial number
xBDOS:
	jp	GoRSX		; Jump to RSX program
_BDOS:
	jp	$-$		; Jump to next RSX
	dw	0		; Address of previous RSX
	db	0ffh		; Remove flag (0 keep permanent)
	db	0		; Non bank only flag
	db	'LRUNRSX '	; Any eight character name
	db	0		; Loader flag
	dw	0		; Spare (reserved)
;
; ===== RSX header ends =====
;
GoRSX:
;
; Test 1 : Is BDOS function an RSX call?
;
	ld	a,c		; Get function
	cp	RSX		; Test RSX call
	jr	nz,_BDOS	; Nope, exit
;
; Test 2 : Is RSX function an LRUN call?
;
	ld	a,(de)
	cp	MyRSX		; Test call
	jr	nz,_BDOS	; Nope, exit
	inc	de
;
; Test 3 : Is RSX function an LRUN init call?
;
	ld	a,(de)		; Get parameter
	or	a
	ld	l,a
	ld	h,a
	ret	z		; Return NULL to indicate ok
;
; Test 4 : Is RSX function an LRUN execute call?
;
	dec	a		; Only ONE allowed as parameter now
	ld	h,0
	ld	b,h
	ld	a,-1
	ld	l,a
	ret	nz		; Error
;
; Call ok, save entry data
;
	inc	de		; Point to data field
	ex	de,hl
	ld	de,Index
	ld	bc,RSXdata
	ldir			; Unpack data from calling program
;
; >>>>>>>>>>>>><<<<<<<<<<<<<
; >>> Loader starts here <<<
; >>>>>>>>>>>>><<<<<<<<<<<<<
;
	ld	hl,(Lenx)	; Get length of .COM member to load.
	add	hl,hl		; Calculate highest address
	add	hl,hl		; To see if it will fit in
	add	hl,hl		; available memory
	add	hl,hl
	add	hl,hl		; 128*(Records+2)
	add	hl,hl
	add	hl,hl
	inc	h		; Remember +2 because program
	ex	de,hl		; starts at address 0100H
	ld	hl,(TPATOP)	; This is the top address
	or	a
	sbc	hl,de		; Test enough memory
	ret	c		; No, error return
;
; The library file is still open.  The open FCB has been
; moved up here into high memory with the loader code.
;
	ld	hl,(Index)	; Get index of member
	ld	(LBRfil+_RRN),hl; Init it
	xor	a		; Clear hi part of random record
	ld	(LBRfil+_RRN+2),a
	ld	hl,TPA
	ld	(Loaddr),hl
	ld	sp,hl		; Change stack
LoadLoop:
	ld	hl,(Lenx)	; Load that sucker.
	ld	a,l		; See if done yet.
	or	h
	jr	z,Loaded
	dec	hl
	ld	(Lenx),hl
	ld	hl,(Loaddr)	; Increment for next time
	ld	d,h
	ld	e,l
	ld	bc,reclng
	add	hl,bc
	ld	(Loaddr),hl
	ld	c,DMA
	call	BDOS		; but use old value (DE)
	ld	de,LBRfil
	ld	c,Rrd
	call	BDOS		; Read the sector
	or	a		; Ok?
	jr	nz,Err		; No, bail out.
	ld	hl,(LBRfil+_RRN)
	inc	hl		; Increment random record field
	ld	(LBRfil+_RRN),hl
	jr	LoadLoop	; Until done.
Err:
	ld	a,_JP		; Prevent execution of bad code
	ld	(TPA),a
	ld	hl,Errx
	ld	(TPA+1),hl
	jr	Loaded		; Execute dummy program instead
Errx:
	ld	hl,BOOT		; One more time, but this time
	ld	(TPA+1),hl	; Jump to BOOT
	ld	de,LdMsg
	ld	c,Msg
	call	BDOS
	ld	de,SUBfile
	ld	c,Del
	call	BDOS		; Abort SUBMIT if in progress
Loaded:
	ld	a,(EntUsr)
	ld	e,a
	ld	c,Usr
	call	BDOS		; Reset user
	ld	de,TBUFF
	ld	c,DMA
	call	BDOS		; Reset disk buffer
	ld	de,lf
	ld	c,Con
	call	BDOS		; Close line
	jp	TPA		; Enter loaded program
;
LdMsg:
	db	'BAD LOAD',eot
SUBfile:
	db	'A'-'@','$$$     SUB'
	ds	4
;
; >>>>>>>>> Following data moved to RSX <<<<<<<<<
;
Index:
	dw	0		; Index of member
Lenx:
	dw	0		; Length of member
EntUsr:
	db	0		; Current user
;
; Library FCB
;
LBRfil:
	ds	FCBlen
RSXdata	equ	$-Index
;
; >>>>>>>>>>>>>>>>>>>>>>>-<<<<<<<<<<<<<<<<<<<<<<<
;
Loaddr:
	ds	2

	end

