	title	Library Run Tool Loader For TURBO CHN files - LRUNTP RSX
	name	('LTPRSX')
	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	'LTPRSX  '	; 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,MIndex
	ld	bc,RSXdata
	ldir			; Unpack data from calling program
;
; >>>>>>>>>>>>><<<<<<<<<<<<<
; >>> Loader starts here <<<
; >>>>>>>>>>>>><<<<<<<<<<<<<
;
	ld	hl,(CLenx)	; Get entire length of data 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	sp,TPA		; Change stack
	xor	a		; Clear hi part of random record
	ld	(LBRfil+_RRN+2),a
;
; Part 1: Load RTL
;
	ld	de,(MIndex)	; Get index of RTL
	ld	bc,(MLenx)	; Get length of RTL
	ld	hl,TPA		; Init start address
	call	LoadLBR		; Load
;
; Part 2: Load chain file
;
	ld	de,(Index)	; Get index of RTL
	ld	bc,(Lenx)	; Get length of RTL
	ld	hl,(TPA+1)	; Init start address
	call	LoadLBR		; Load
;
	jr	Loaded		; Execute
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
;
; Load data from LBR
; Start address in reg HL
; Start index in reg DE
; Length of file in reg BC
;
LoadLBR:
	ld	(LBRfil+_RRN),de; Init record
	ld	(Loaddr),hl	; Init start address
	ld	(rLenx),bc	; Init length
LoadLoop:
	ld	hl,(rLenx)	; Load that sucker.
	ld	a,l		; See if done yet.
	or	h
	ret	z
	dec	hl
	ld	(rLenx),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.
;
rLenx:
	ds	2
LdMsg:
	db	'BAD LOAD',eot
SUBfile:
	db	'A'-'@','$$$     SUB'
	ds	4
;
; >>>>>>>>> Following data moved to RSX <<<<<<<<<
;
RSXpar	equ	$
MIndex:
	dw	0		; Index of RTL
MLenx:
	dw	0		; Length of RTL
Index:
	dw	0		; Index of member
Lenx:
	dw	0		; Length of member
CLenx:
	dw	0		; Entire length of program
EntUsr:
	db	0		; Current user
;
; Library FCB
;
LBRfil:
	ds	FCBlen
RSXdata	equ	$-RSXpar
;
; >>>>>>>>>>>>>>>>>>>>>>>-<<<<<<<<<<<<<<<<<<<<<<<
;
Loaddr:
	ds	2

	end

