	.title	k11hex	create k11prt.sav and/or k11rt4.sav from hex file
	.ident	/2.23/


;	25-Oct-84  11:14:41  Brian Nelson
;
;	Usage:	run k11hex
;		*k11prt=k11prt
; or		*k11rt4=k11rt4




	.mcall	.close	,.csigen,.exit	,.readw	,.writw	,.print

	.psect

	.macro	sob	reg	,dst
	dec	reg
	bne	dst
	.endm	sob

	.macro	mapch	ch
	movb	map(ch)	,ch
	.endm	mapch

map:	.rept	48.
	.byte	377
	.endr
	.byte	0,1,2,3,4,5,6,7,8.,9.
	.byte	377,377,377,377,377,377,377
	.byte	10.,11.,12.,13.,14.,15.
	.rept	256.-70.
	.byte	377
	.endr

	.even
rtwork:	.blkw	10
inblk:	.word	0
outblk:	.word	0
inpnt:	.word	0
outpnt:	.word	0
inbuff:	.blkw	400
outbuf:	.blkw	400

	.enabl	lc
wrerr:	.asciz	/Write error on device/
rderr:	.asciz	/Read  error from file/
badch:	.asciz	/Non HEX character read from input/
badchk:	.asciz	/Checksum error/
alldone:.asciz	/All done/
csibad:	.asciz	/Could not open the files on the command line/
	.even
defext:	.rad50	/HEX/
	.rad50	/SAVSAVSAV/




convert:.csigen	#handld,#defext,#0	; get a command
	bcc	5$			; ok
	.print	#csibad			; no good
	.exit

5$:	mov	#77777	,inpnt		; ensure fist getc call gets a block
10$:	mov	#40	,r5		; loop for the data record please
	clr	r4			; clear checksum if we use it
20$:	call	getc			; get a character
	bcs	60$			; most likely all done
	mov	r0	,r3		; save it
	mapch	r3
	bpl	30$			; char was ok
	.print	#badch			; not ok
30$:	asl	r3			; avoid mul for systems w/o EIS
	asl	r3			; avoid mul for systems w/o EIS
	asl	r3			; avoid mul for systems w/o EIS
	asl	r3			; avoid mul for systems w/o EIS
	call	getc			; get the next one please
	bcs	80$			; exit on i/o error please
	mapch	r0			; convert it
	bpl	40$			; it was ok
	.print	#badch			; error
40$:	add	r3	,r0		; add it in and copy the byte
	add	r0	,r4		; add into simpleminded checksum
	call	putc			; write it out
	bcs	70$			; i/o error on write
	sob	r5	,20$		; finished with the read
	call	getchk			; read the checksum
	cmp	r0	,r4		; do they match ?
	beq	50$			; yes
	.print	#badchk			; no, print error and exit
50$:	br	10$			; read the next record now

60$:	call	close			; close up and exit
	br	100$			; bye

70$:	.print	#wrerr			; write error
	br	100$			; exit
80$:	.print	#rderr			; a read error
	br	100$			;

100$:	.print	#alldone
	.exit


getchk:	clr	r3
	call	getc
	bcs	100$
	call	getc
	call	getc
	mov	#4	,r2
10$:	call	getc
	asl	r3			; avoid mul for systems w/o EIS
	asl	r3			; avoid mul for systems w/o EIS
	asl	r3			; avoid mul for systems w/o EIS
	asl	r3			; avoid mul for systems w/o EIS
	mapch	r0
	add	r0	,r3
	sob	r2	,10$
100$:	mov	r3	,r0
	return



getc:	call	.getc			; get the next character
	bcs	100$			; oops
	cmpb	r0	,#15		; carriage return or less?
	blos	getc			; yes, get the next character then
	clc				; no, return it
100$:	return				; exit with c bit error code

.getc:	cmp	inpnt	,#1000
	blo	10$
	clr	inpnt			; clear the buffer offset also
	.readw	#rtwork,#3,#inbuff,#400,inblk
	bcs	100$			; end of file
	inc	inblk			; next time the next block please
10$:	mov	inpnt	,r0		; get the current buffer offset
	inc	inpnt			; + 1
	movb	inbuff(r0),r0		; and return the character
	clc				; success
100$:	return				; exit with success in 'c' bit


putc:	cmp	outpnt	,#1000		; room for more output data
	blo	10$
	clr	outpnt			; clear the buffer offset also
	mov	r0	,-(sp)		; save the character being put
	.writw	#rtwork,#0,#outbuff,#400,outblk
	mov	(sp)+	,r0		; restore that character please
	bcs	100$			; end of file
	inc	outblk			; next time the next block please
10$:	mov	outpnt	,r1		; get the current buffer offset
	inc	outpnt			; + 1
	movb	r0	,outbuff(r1)	; and return the character
	clc				; success
100$:	return				; exit with success in 'c' bit


close:	tst	outpnt			; anything to dump out
	beq	20$			; no, should not be
	mov	outblk	,r2		; save current output block #
10$:	cmp	r2	,outblk		; did it dump it's buffer?
	bne	20$			; yes
	clr	r0			; yes, dump a bunch of nulls out
	call	putc			; until putc clears the pointer
	br	10$
20$:	.close	#0			; close the two file now
	.close	#3			; output also please
	return

handld:	.blkw	500

	.end	convert
