	TITLE	'BLOCK FILE TO TEXT FILE CONVERTER'
	MACLIB	MACRO
	ORG	100H
BDOS	SET	5
$-MACRO
	LXI	H,0
	DAD	SP
	SHLD	STACKPTR
	LXI	SP,STACK
	JMP	MAIN
	DS	40H
STACK:
STACKPTR:	DS	2
;
TFCB	SET	5CH
FCB1	DS	33
FCB2	DS	33
IBP	DB	0
OBP	DB	0
IBUF	DS	128
OBUF	DS	128
;
GNB:	;GET NEXT BYTE
	LDA	IBP
	CPI	80H
	JNZ	G0
	CALL	DISKR
	XRA	A
G0:
	MOV	E,A
	MVI	D,0
	INR	A
	STA	IBP
	PUSH	H
	LXI	H,IBUF
	DAD	D
	MOV	A,M
	POP	H
	INX	H
	RET
;
DISKR:
	PUSH H! PUSH D! PUSH B
	DISKIO SETDMA,IBUF
	DISKIO READ,FCB1
	POP B! POP D! POP H
	CPI	0	;ZERO IF NO ERRORS
	RZ
	CPI	1	;ONE IF END OF FILE
	JZ	FINIS
	JMP	ERR1	;DISK READ ERROR
;
SETINP:
	FILL	FCB1,FCB1+32
	FILL	FCB2,FCB2+32
	MOVE	TFCB,FCB1,16
	MOVE	TFCB+16,FCB2,16
	LDA	FCB1+1
	CPI	' '
	JZ	ERR2	;NO INPUT FILE
	LDA	FCB2+1
	CPI	' '
	JZ	ERR3	;NO OUTPUT FILE
	DISKIO OPEN,FCB1
	INR	A
	JZ	ERR4	;CAN'T OPEN FILE
	MVI	A,80H
	STA	IBP
	RET
;
SETOUT:
	DISKIO SEARCH,FCB2
	INR	A
	JZ	CREATE
	DISKIO DELETE,FCB2
CREATE	DISKIO MAKE,FCB2
	INR	A
	JZ	ERR5	;DIRECTORY FULL
NEXT:
	MVI	A,0
	STA	OBP
	RET
;
DISKW:
	PUSH H! PUSH D! PUSH B!
	DISKIO SETDMA,OBUF
	DISKIO WRITE,FCB2
	POP B! POP D! POP H
	CPI	0	;ZERO IF OK
	RZ
	JMP	ERR6	;ERROR IN WRITING RECORD
;
ONB:	;OUTPUT NEXT BYTE
	PUSH	PSW	;SAVE BYTE
	LDA	OBP
	CPI	80H
	JNZ	G1
	CALL	DISKW
	XRA	A
G1:
	MOV	E,A
	MVI	D,0
	INR	A
	STA	OBP
	XTHL
	MOV	A,H
	LXI	H,OBUF
	DAD	D
	MOV	M,A
	POP	H
	INX	H
	RET
;
; MAIN PROGRAM
;
MAIN:
	PRINT	<'ALS8 File to CP/M ASM File Converter',CR,LF>
	PRINT	<'As of November, 1979 - Tom Digate',CR,LF>
	CALL	SETINP
	CALL	SETOUT
;
; TO GET A BYTE FROM INPUT FILE, CALL GNB
; TO WRITE A BYTE TO OUTPUT FILE, CALL ONB
; TO EXIT, JMP TO FINIS
;
;
; INSERT PROCESSING CODE HERE...
;
CR	SET	0DH
STR	SET	27H	;SINGLE QUOTE CHARACTER
LF	SET	0AH
EOF	SET	01H	;END OF FILE
TAB	SET	09H
;
CHRI	SET	GNB
CHRO	SET	ONB
;
	CALL	CHRI	;GET FIRST BYTE AND DISCARD IT
LOOP:
	CALL	CHRI	;GET LINE NUMBER
	CALL	CHRI
	CALL	CHRI
	CALL	CHRI
	CALL	CHRI	;GET FIRST CHARACTER AND DISCARD
;
	CALL	CHRI
	CPI	'*'	;IS IT A COMMENT LINE?
	JZ	COMMENT
	CPI	' '	;IS IT NO LABEL?
	JZ	NOLABEL
;
LABEL:
	CALL	CHRO
	CALL	CHRI
	CPI	' '	;END OF LABEL?
	JNZ	LABEL	;NO
NOLABEL:
	MVI	A,TAB
	CALL	CHRO	;PUT IN TAB AFTER LABELS
;
OPCDE:	;PROCESS OP-CODES NOW
	CALL	CHRI	;FIND FIRST NON BLANK
	CPI	' '
	JZ	OPCDE
FOUNDOP:
	CALL	CHRO
	CALL	CHRI
	CPI	CR
	JZ	ENDLINE
	CPI	' '
	JNZ	FOUNDOP	;STILL AN OP-CODE
	MVI	A,TAB
	CALL	CHRO	;OUTPUT A TAB BETWEEN OPCODE AND OPERAND
OPERAND:
	CALL	CHRI	;FIND NON-BLANK
OP2:
	CPI	' '
	JZ	COMMENTX
; THIS CHARACTER CAN BE *,CR, OR OPERAND
	CPI	'*'
	JZ	COMMENTX
	CPI	CR
	JZ	ENDLINE
	CPI	STR
	JZ	STRING
OP1:
	CALL	CHRO
	JMP	OPERAND	;GET NEXT CHARACTER
ENDLINE1:
	MVI	A,STR
	CALL	CHRO
	MVI	A,CR
ENDLINE:
	CALL	CHRO
	MVI	A,LF
	CALL	CHRO
	CALL	CHRI
	CPI	EOF
	JZ	FINIS
	JMP	LOOP
STRING:
	CALL	CHRO
	CALL	CHRI
	CPI	STR
	JZ	OP1
	CPI	CR
	JZ	ENDLINE1
	JMP	STRING
COMMENTX:
	MVI	A,TAB
	CALL	CHRO
COMMENT:
	MVI	A,';'
	CALL	CHRO
COMLOOP:
	CALL	CHRI
	CPI	CR
	JZ	ENDLINE
	CALL	CHRO
	JMP	COMLOOP
;
; FINIS CLOSES OUTPUT FILE WITH CONTROL-Z'S
;
FINIS:
	DISKIO SETDMA,OBUF
	MVI	B,80H
CLEAN:
	MVI	A,1AH
	PUSH	B
	CALL	ONB
	POP	B
	DCR	B
	JNZ	CLEAN
	DISKIO CLOSE,FCB2
	INR	A
	JZ	ERR7	;CAN'T CLOSE
	PRINT	<'Conversion Complete',CR,LF>
FINAL:	LHLD	STACKPTR
	SPHL
	RET
;
; ERROR HANDLING
;
ERR1:	LXI D,EMSG1 ! JMP PMSG
ERR2:	LXI D,EMSG2 ! JMP PMSG
ERR3:	LXI D,EMSG3 ! JMP PMSG
ERR4:	LXI D,EMSG4 ! JMP PMSG
ERR5:	LXI D,EMSG5 ! JMP PMSG
ERR6:	LXI D,EMSG6 ! JMP PMSG
ERR7:	LXI D,EMSG7 ! JMP PMSG
;
PMSG:	MVI C,9 ! CALL BDOS ! JMP FINAL
;
; ERROR MESSAGES
;
EMSG1:	DB	'Disk read error$'
EMSG2:	DB	'No input file specified$'
EMSG3:	DB	'No output file specified$'
EMSG4:	DB	'Can''t open input file$'
EMSG5:	DB	'Can''t make new output file$'
EMSG6:	DB	'Disk write error$'
EMSG7:	DB	'Can''t close output file$'
	END

