;*************************** AMUS Program Label ******************************
; Filename: HEXCVT.M68                                      Date: 01/13/89
; Category: UTIL         Hash Code: 151-620-647-741      Version: 1.0(102)
; Initials: PHI/AM       Name: JIM DULANEY
; Company: SYSTEMS TECHNOLOGY                      Telephone #: 
; Related Files: USES FILER.M68 (MACROS)
; Min. Op. Sys.:                               Expertise Level: INT
; Special: 
; Description: Converts a binary file to a intel hex file.
; .HEXCVT filename.ext
; 
;*****************************************************************************
;08/21/81 - TDW, SYSTEMS TECHNOLOGY
;10/23/82 - [100] Converted to L
;11/01/82 - [101] Cosmetic improvements
;03/12/83 - [102] Bug in incrementing current address


	SEARCH	SYS
	SEARCH	SYSSYM
	SEARCH	TRM
	SEARCH	FILER			; was copy		[101]

	VMAJOR=1
	VMINOR=0
	VEDIT=102.
RECLNG	=	16.			; standard rec length

;Storage areas
	.OFINI
.OFDEF	INPDDB,D.DDB
.OFDEF	HEXDDB,D.DDB
.OFDEF	LODADR,2
.OFDEF	CURADR,2
.OFDEF	SAVJCB,2
.OFDEF	RECSIZ,2
.OFDEF	CHKSUM,2
.OFDEF	STRBUF,10
.OFDEF	RECBUF,RECLNG
	.OFSIZ	IMPSIZ

HEXCVT:	PHDR	-1,0,PH$REE!PH$REU
	CRLF				; 			[101]
	TYPE	<== AM-100>		; 			[101]
	MOV	#'/,D1			; 			[101]
	TTY				;			[101]
	TYPESP	<L Convert file to Intel HEX Format Version> ;	[101]
	VCVT	HEXCVT+2,OT$TRM
	TYPECR	< ==>
	CRLF				; 			[101]
	BYP
	LIN
	BNE	1$
	TYPECR	<USAGE:	HEXCVT filespec.LIT,load-bias(HEX)>
	EXIT
1$:
	GETIMP	IMPSIZ,A5,EXIT

	PUSH	A2			; save line index for output file
	OPNINP	INPDDB(A5),LIT		; do input file
	POP	A2			; get line index
	OPNOUT	HEXDDB(A5),HEX,HEX	; do output

;set hex for numeric conversions
	JOBIDX	A6
	MOVW	JOBTYP(A6),SAVJCB(A5)
	ORW	#J.HEX,JOBTYP(A6)

	CLRW	LODADR(A5)
	CLRW	CURADR(A5)
	INC	A2			; skip commas if any
	BYP
	LIN
	BEQ	2$
	GTOCT				; this will get a hex value
	BMI	3$
	MOVW	D1,LODADR(A5)		; set load bias
	MOVW	D1,CURADR(A5)
	BR	2$
3$:	TYPECR	<Load bias error.>
	JMP	DONE
2$:

;D1 = work reg, char reg
;A2 --> hex strbuf
;A5 --> work memory
LOOP:	CTRLC	DONE			; allow user to exit
	TYPE	<.>			; let him know we are alive
	MOVB	#':,D1			; record mark frame 0
	CALL	PUTC

	CLRW	CHKSUM(A5)		; start with zero
	CALL	GETREC			; get a record
	MOVW	RECSIZ(A5),D1		; get the record size
	BEQ	LSTREC
	ADDW	D1,CHKSUM(A5)		; and add to 
	CALL	OCVT2
	MOVW	CURADR(A5),D1		; load address fr 3-6
	SWAB	D1
	ADDW	D1,CHKSUM(A5)
	SWAB	D1
	ADDW	D1,CHKSUM(A5)		; place both bytes in checksum
	CALL	OCVT4
	CLR	D1			; record type 0
	TSTW	RECSIZ(A5)
	BNE	5$
	MOV	#1,D1
5$:	ADDW	D1,CHKSUM(A5)
	CALL	OCVT2
	LEA	A3,RECBUF(A5)
	MOVW	RECSIZ(A5),D0		; loop counter
	BEQ	20$
10$:	MOVB	(A3)+,D1
	ADDW	D1,CHKSUM(A5)
	CALL	OCVT2
	SOB	D0,10$

20$:	MOVW	CHKSUM(A5),D1
	NEG	D1
	AND	#377,D1
	CALL	OCVT2
	CALL	NEWLIN
	MOVW	RECSIZ(A5),D7
	ADDW	D7,CURADR(A5)	; increment load address by recsiz [102]
	JMP	LOOP

LSTREC:	CLR	D1
	CALL	OCVT2
	MOVW	LODADR(A5),D1		; orginal load address is execute adr
	SWAB	D1
	ADDW	D1,CHKSUM(A5)
	SWAB	D1
	ADDW	D1,CHKSUM(A5)
	CALL	OCVT4
	MOV	#1,D1			; record type 1
	ADDW	D1,CHKSUM(A5)
	CALL	OCVT2
	MOVW	CHKSUM(A5),D1		; and check sum record
	NEG	D1
	AND	#377,D1
	CALL	OCVT2
	CALL	NEWLIN
	
DONE:
	CRLF
	TYPECR	<DONE>
	JOBIDX	A6
	MOVW	SAVJCB(A5),JOBTYP(A6)
	CLOSE	INPDDB(A5)
	CLOSE	HEXDDB(A5)
EXIT:	EXIT	

NEWLIN:	MOVB	#CR,D1			; cr
	CALL	PUTC
	MOVB	#LF,D1			; lf

PUTC:	PUTCHR	HEXDDB(A5)
	RTN

GETREC:
	MOV	#RECLNG,D0		; standard record length
	LEA	A1,RECBUF(A5)		; point to rec buffer
	CLRW	RECSIZ(A5)		; start with zero
10$:	GETCHR	INPDDB(A5),(A1)+	; get a chr into buffer
	BEQ	20$			; z flag is eof
	INCW	RECSIZ(A5)		; recsiz one more
	SOB	D0,10$			; loop until done
20$:	RTN

;flags for OCVT will be bit 3, with number in D1, A2->STRBUF
;	OCVT	2,OT$MEM

OCVT4:
	LEA	A2,STRBUF(A5)
	OCVT	4,OT$MEM
	BR	OCVT11
OCVT2:
	LEA	A2,STRBUF(A5)
	OCVT	2,OT$MEM			; record length fr 1,2
OCVT11:
	CLRB	@A2
	LEA	A2,STRBUF(A5)
	PUTSTR	HEXDDB(A5)
	RTN


	END