
recv	=	$c088
xmit	=	$c088
stat	=	$c089
rset	=	$c089
comm	=	$c08a
ctrl	=	$c08b

dcmd	=	$42
unit	=	$43
bkptr	=	$44
blok	=	$46

dent	=	$48
idxl	=	$4a
idxh	=	$4c
idxp	=	$4e

iobuff	=	$60

clrscrn	=	$fc58
scrn	=	$5ae
dostyp	=	$ff
sosid	=	$c00
entlen	=	sosid+$23
kernel	=	$2000

	*=	$800

	jmp	find

idbyte	.byt	$38,$18,$01,$31
idoffs	.byt	$05,$07,$0b,$0c

find	lda	#$c7
	sta	test+2
L1	ldy	#3
L2	ldx	idoffs,y
test	lda	$c000,x
	cmp	idbyte,y
	bne	L4
	dey
	bpl	L2
	lda	test+2
	asl
	asl
	asl
	asl
	sta	unit
	bra	found
L4	dec	test+2
	lda	test+2
	cmp	#$c1
	bcs	L1
	jmp	booterr

found	jsr	init
	ldy	#0
	sty	iobuff
	sty	idxl
	sty	idxh
	sty	idxp
	sty	blok+1
	iny
	iny
	sty	blok
	lda	#$c
	sta	iobuff+1
	sta	idxl+1
rddir	jsr	goread
	bcs	bterr2
	inc	iobuff+1
	inc	iobuff+1
	inc	blok
	lda	blok
	cmp	#$6
	bcc	rddir
	lda	sosid
	ora	sosid+1
bterr	bne	booterr
	lda	#4
	bne	nxdent1
nxdent	lda	idxl
nxdent1	clc
	adc	entlen
	tay
	bcc 	nxdent2
	inc	idxl+1
	lda	idxl+1
	lsr
	bcs	nxdent2
	cmp	#$a
	beq	nopro
	ldy	#4
nxdent2 sty	idxl
	lda	sysname
	and	#$f
	tay
lookpro lda	(idxl),y
	cmp	sysname,y
	bne	nxdent
	dey
	bpl	lookpro
	and	#$f0
	cmp	#$20
	bne	booterr
	ldy	#$10
	lda	(idxl),y
	cmp	#dostyp
	bne	booterr
	iny
	lda	(idxl),y
	sta	blok
	iny
	lda	(idxl),y
	sta	blok+1
	lda	#0
	sta	idxl
	ldy	#$1e
	sty	idxl+1
	sty	iobuff+1
	iny
	sty	idxh+1
rdkernl jsr	goread
bterr2	bcs	booterr
	inc	iobuff+1
	inc	iobuff+1
	ldy	idxp
	inc	idxp
	lda	(idxl),y
	sta	blok
	lda	(idxh),y
	sta	blok+1
	ora	(idxl),y
	bne	rdkernl
	jmp	kernel

nopro
booterr	jmp	quitmes
sysname	.byt	$26
	.asc	"PRODOS"
	
quitmes	jsr	clrscrn
	ldy	#meslen
prmess	lda	errmes,y
	ora	#$80
	sta	scrn,y
	dey
	bpl	prmess
hang	jmp	hang
	
meslen	=	28
errmes	.asc	"*** UNABLE TO LOAD PRODOS ***"

getch	ldx	unit
	lda	stat,x
	and	#$08
	beq	getch
	lda	recv,x
	rts

putch	ldx	unit
	lda	stat,x
	and	#$10
	beq	putch
	tya
	sta	xmit,x
	rts

sndcmd	ldy	cmdbf
	jsr	putch
	ldy	cmdbf+1
	jsr	putch
	ldy	cmdbf+2
	jsr	putch
	rts

bkcnt	.word	0
cmdbf	.dsb	3,0

init	lda	#$48		;'H'
	sta	cmdbf
	jsr	sndcmd
	jsr	getch
	cmp 	#$2b		;'+'
	bne	init
	rts
	
goread	stz	bkcnt
	stz	bkcnt+1
	lda	iobuff
	sta	bkptr
	lda	iobuff+1
	sta	bkptr+1
	lda	#$52		;'R'
	sta	cmdbf
	lda	blok
	sta	cmdbf+1
	lda	blok+1
	sta	cmdbf+2
	jsr	sndcmd
	jsr	getch
	cmp	#$2b		;'+'
	beq	L5
	sec
	rts
L5	jsr	getch
	sta	(bkptr)
	inc	bkptr
	bne	L6
	inc	bkptr+1
L6	inc	bkcnt
	bne	L5
	inc	bkcnt+1
	lda	#2
	cmp	bkcnt+1
	bne	L5
	jsr	getch		;cksum
	jsr	getch		;cksum
	clc
	rts

	