*---------------------------------------------------------------------
*
*  LINK.CMD (for MiniFLEX on Pertec FD3712)
*     Place the track, sector of the specified file (typically the
*     FLEX system file (e.g., DOS.SYS) into bytes 5 and 6 of the 
*     boot loader present at the first sector on track 0
*
*     This source file was created by disassembling LINK.CMD from
*     a SWTPC FLEX 1.0 (MiniFLEX) disk.
*
*     Updated for the FD3712 to use track 0, sector 1 as the boot
*     sector instead of track 0, sector 0.
*
*     Mike Douglas, 5/1/22
*
*---------------------------------------------------------------------
SYSFCB	equ	$7740		;FLEX system FCB
FMS	equ	$7806		;entry point for FMS calls
FMSCLS	equ	$7803		;close all files
WARMS	equ	$7103		;FLEX re-entry address
PSTRNG	equ	$7118		;display string on console
GETFIL	equ	$7127		;build FCB from command line parameter
SETEXT	equ	$712D		;set default extension
RPTERR	equ	$713C		;report error from the FCB
EOT	equ	$04		;end of string for PSTRNG

* FMS equates

OPENRD	equ	$01		;open for read
OPENWRT	equ	$02		;open for write
READSEC	equ	$09		;read a sector
WRTSEC	equ	$0A		;write a sector

* Offsets into the FMS file control block (FCB)

fcbCMD	equ	0		;FMS command
fcbERR	equ	1		;error status
fcbSTRK	equ	17		;start track of file
fcbSSEC	equ	18		;start sector of file
fcbCTRK	equ	30		;track of current file position
fcbCSEC	equ	31		;sector of current file position
fcbSBUF	equ	64		;start of sector buffer in FCB

	org	$7600

entry	bra	start
	fcb	1		;version number

start	ldx	#SYSFCB		;get file name from command line
	jsr	GETFIL
	bcs	badFile		;invalid file name format

	ldx	#SYSFCB		;set a default .SYS extension
	ldaa	#4
	jsr	SETEXT

	ldx	#SYSFCB		;open the file for read
	ldaa	#OPENRD
	staa	fcbCMD,x
	jsr	FMS
	bne	openErr		;file open error

	clr	fcbCTRK,x	;read 1st sector of the boot loader
	ldaa	#1		;(track 0, sector 1)
	staa	fcbCSEC,x
	ldaa	#READSEC
	staa	fcbCMD,x
	jsr	FMS
	bne	sysErr		;report a disk error

	ldaa	fcbSTRK,x	;A=track num of specified FLEX file
	ldab	fcbSSEC,x	;B=sector num of specified FLEX file
	staa	fcbSBUF+5,x	;set track in the boot loader
	stab	fcbSBUF+6,x	;set sector in the boot loader

	ldaa	#WRTSEC		;update the loader on disk
	staa	fcbCMD,x
	jsr	FMS
	beq	pgmExit		;close files and exit

*----------------------------------------------------------------
*  File/disk error processing
*----------------------------------------------------------------
openErr	ldaa	fcbERR,x	;A=error code
	cmpa	#$04		;file not found?
	beq	notFnd

	cmpa	#$40		;write protect error?
	bne	sysErr		;no, report some other error

	ldx	#mWrtPrt	;disk write protected?
	bra	dspExit		;display message and exit

notFnd	ldx	#mNotFnd	;file not found
	bra	dspExit

badFile	ldx	#mBadFil	;invalid file name

dspExit	jsr	PSTRNG		;display error message
	bra	pgmExit

sysErr	jsr	RPTERR		;let system report the error

* pgmExit - Close open FCB and warm start FLEX

pgmExit	jsr	FMSCLS
	jmp	WARMS

*----------------------------------------------------------------
*  Message constants
*----------------------------------------------------------------
mNotFnd	fcc	"NO SUCH FILE"
	fcb	EOT

mBadFil	fcc	"ILLEGAL FILE NAME"
	fcb	EOT

mWrtPrt	fcc	"DISK IS PROTECTED"
	fcb	EOT
	
	end	entry
