/	Bootstrap for the LSI-11

	KBSTAT = 0177560
	KBDATA = 0177562
	PRSTAT = 0177564
	PRDATA = 0177566

	RETRAN = 0122
	AKNOWL = 0101

	movpsw = 0106427

/	r0	is scratch 
/	r1	is pointer to memory
/	r2	is number of packets
/	r3	is checksum
/	r4	is byte counter

	mov	pc, sp		/ Initialize stack
	tst	-(sp)
	
	movpsw			/ Set maximum priority
	0340

	jsr	pc, getword	/ get pointer to memory
	mov	r0, r1
	jsr	pc, getword	/ get packet count
	mov	r0, r2
packet:
	jsr	pc, getword	/ get packet size
	mov	r0, r4
	mov	r4, -(sp)	/ remember packet size
	clr	r3		/ initialize checksum
dl1:
	clr	r0
dlw:
	tstb	*$KBSTAT	/ Wait for byte
	bpl	dlw
	bisb	*$KBDATA, r0	/ Get byte,
	add	r0, r3		/  add it to the checksum,
	movb	r0, (r1)+	/  and move it to memory
	dec	r4		/ Decrease byte count
	bne	dl1		/  and go back for more if needed

	jsr	pc, getword	/ Get transmitted checksum
	cmp	r3, r0		/  and compare it
	beq	next		/ If OK, get next packet

	movb	$RETRAN,*$PRDATA/ Else, send request for retransmit
	sub	(sp)+, r1	/  and reset pointer
	bpl	packet
next:
	movb	$AKNOWL,*$PRDATA/ Aknowledge data
	tst	(sp)+		/ Clear stack
	dec	r2		/ Decrement packet count
	bne	packet		/  and get next packet if needed

/	movpsw			/ Lower priority
/	0

	jsr	pc, getword	/ Get entry point ...
	mov	r0, r4		/ and store it in r4

	jsr	pc, getword	/ Get bss segment size
	tst	r0		/ 
	beq	go		/
1:	
	clrb	(r1)+		/ Clear all bytes within
	sob	r0,1b		/ the bss segment.	
go:
	jmp	(r4)		/  and jump to it
	
/ Reads a word from the terminal line and returns it in R0

getword:
	clr	r0
gw1:
	tstb	*$KBSTAT
	bpl	gw1
	bisb	*$KBDATA, r0
	swab	r0
gw2:
	tstb	*$KBSTAT
	bpl 	gw2
	bisb	*$KBDATA, r0
	swab	r0
	rts	pc
	
