	title	DATA Generator fuer BASIC - Dateiausgaberoutinen
	name	('DATDAT')
	maclib	base80

; Das Programm wandelt eine Binaerdatei in DATA-Anweisungen fuer BASIC
;
; Dieser Teil schreibt die .BAS Datei
;
; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		Tel.: +49-40-4223247
;
;		Version 1.0, Januar 1995

	entry	WrHead,wrDATA

	extrn	Lopt,Iopt,Copt,Sopt,$MODNAM,DatLen,Heap

	extrn	decout,hexout,ascbyt,ascnib,string,fildrv
	extrn	multip,adda
	extrn	emplin,delete

	dseg

RESTORE:
	ds	2
$LINE:
	db	'65535',null
$NUM:
	db	'65535 ',null
$HEX:
	db	'xxxx',null
$WRERR:
	db	'Schreibfehler in Datei: ',eot
$LINE1.1:
	db	'REM Set up ',null
$LINE1.2:
	db	' machine-code routine'
$CRLF:
	db	cr,lf,null
$LINE2.1:
	db	'MEMORY &H',null
$LINE2.2:
	db	':',null
$LINE2.3:
	db	'=&H',null
$LINE2.4:
	db	':A=',null
$LINE2.5:
	db	':E=0',null
$LINE3.1:
	db	'RESTORE ',null
$LINE4.1:
	db	'FOR F=0 TO ',null
$LINE5.1:
	db	'S=0'
	db	cr,lf,null
$LINE6.1:
	db	'FOR G=0 TO 7'
	db	cr,lf,null
$LINE7.1:
	db	'READ X$:X=VAL("&H"+X$)'
	db	cr,lf,null
$LINE8.1:
	db	'S=S+X:',null
$LINE8.2:
	db	'POKE A,X:A=A+1'
	db	cr,lf,null
$LINE9.1:
	db	'NEXT'
	db	cr,lf,null
$LINE10.1:
	db	'READ X$:IF S<>VAL("&H"+X$) THEN E=E+1:PRINT "ERROR IN LINE";',null
$LINE10.2:
	db	'*F+',null
$LINE12.1:
	db	'IF E=0 THEN ',null
$LINE12.2:
	db	'RETURN'
	db	cr,lf,null
$LINE13.1:
	db	'PRINT E;"ERROR";:IF E=1 THEN PRINT ELSE PRINT "S"'
	db	cr,lf,null
$LINE14.1:
	db	'END'
	db	cr,lf,null
$DATA:
	db	'DATA '
$DATLIN:
	db	'd1,d2,d3,d4,d5,d6,d7,d8'
	db	null
$DATCRC:
	db	',crc'
	db	null
TMP:
	ds	1

	cseg

;
; Zeile schliessen
;
Fcrlf:
	ld	de,$CRLF
	jr	WrFile		; Einfach schliessen
;
; Dezimalwert schreiben
;
WrDcFile:
	ld	de,$LINE	; Zeiger auf Puffer
	push	de	
	ld	b,null
	call	decout		; Zeile wandeln
	inc	hl
	ld	(hl),null	; Zeile schliessen
	pop	de		; Alte Zeilennummer holen
	jr	WrFile		; Ausgeben
;
; Hexwert schreiben
;
WrHxFile:
	ld	de,$HEX
	push	de
	call	hexout		; Wert wandeln
	pop	de
	jr	WrFile		; Und schreiben
;
; Schreiben der aktuellen Zeilennummer
;
WrLine:
	ld	de,$NUM		; Zeiger auf Puffer
	push	de	
	ld	hl,(Lopt)	; Zeilennummer holen
	push	hl
	ld	b,' '
	call	decout		; Zeile wandeln
	inc	hl
	ld	(hl),null	; Zeile schliessen
	pop	de		; Alte Zeilennummer holen
	ld	hl,(Iopt)	; Inkrement holen
	add	hl,de
	ld	(Lopt),hl	; Neue Zeilennummer
	pop	de		; Zeile ausgeben
;
; Ausgeben einer Zeile und testen ob Fehler
;
WrFile:
	ld	b,null
	call	emplin		; Zeile schreiben
	ret	nc
	ld	de,$WRERR
	call	string		; Schreibfehler
	ld	de,FCB
	call	fildrv
	call	delete		; Datei loeschen
	jp	OS
;
; Schreiben des Kopfes
;
WrHead:
	ld	de,10
	ld	a,(Copt)	; Test ob Pruefsumme
	or	a
	jr	z,NoChk
	ld	de,14
NoChk:
	ld	hl,(Iopt)	; Inkrement holen
	call	multip
	ld	de,(Lopt)	; Zeilenstart holen
	add	hl,de		; RESTORE Zeilennummer berechnen
	ld	(RESTORE),hl
;
; Zeile 1: REM Set up NAME machine-code routine
;
	call	WrLine
	ld	de,$LINE1.1
	call	WrFile
	ld	de,$MODNAM
	call	WrFile
	ld	de,$LINE1.2
	call	WrFile
;
; Zeile 2: MEMORY &HMEMTOP-1:NAME=&HMEMTOP:A=NAME(:E=0)
;
	call	WrLine
	ld	de,$LINE2.1
	call	WrFile
	ld	hl,(Sopt)
	dec	hl
	call	WrHxFile
	ld	de,$LINE2.2
	call	WrFile
	ld	de,$MODNAM
	call	WrFile
	ld	de,$LINE2.3
	call	WrFile
	ld	hl,(Sopt)
	call	WrHxFile
	ld	de,$LINE2.4
	call	WrFile
	ld	de,$MODNAM
	call	WrFile
	ld	a,(Copt)
	or	a
	ld	de,$LINE2.5
	call	nz,WrFile
	call	Fcrlf
;
; Zeile 3: RESTORE ZEILE
;
	call	WrLine
	ld	de,$LINE3.1
	call	WrFile
	ld	hl,(RESTORE)
	call	WrDcFile
	call	Fcrlf
;
; Zeile 4: FOR F=0 TO LINES-1
;
	call	WrLine
	ld	de,$LINE4.1
	call	WrFile
	ld	hl,(DatLen)
	dec	hl
	call	WrDcFile
	call	Fcrlf
;
; Zeile 5: (S=0)
;
	ld	a,(Copt)
	or	a
	jr	z,Line6
	call	WrLine
	ld	de,$LINE5.1
	call	WrFile
;
; Zeile 6: FOR G=0 TO 7
;
Line6:
	call	WrLine
	ld	de,$LINE6.1
	call	WrFile
;
; Zeile 7: READ X$:X=VAL("&H"+X$)
;
	call	WrLine
	ld	de,$LINE7.1
	call	WrFile
;
; Zeile 8: (S=S+X:)POKE A,X:A=A+1
;
	call	WrLine
	ld	a,(Copt)
	or	a
	ld	de,$LINE8.1
	call	nz,WrFile
	ld	de,$LINE8.2
	call	WrFile
;
; Zeile 9: NEXT
;
	call	WrLine
	ld	de,$LINE9.1
	call	WrFile
;
; Zeile 10: READ X$:IF S<>VAL("&H"+X$) THEN E=E+1:PRINT "ERROR IN LINE";INC*F+RESTORE
;
	ld	a,(Copt)
	or	a
	jr	z,Line11
	call	WrLine
	ld	de,$LINE10.1
	call	WrFile
	ld	hl,(Iopt)
	call	WrDcFile
	ld	de,$LINE10.2
	call	WrFile
	ld	hl,(RESTORE)
	call	WrDcFile
	call	Fcrlf
;
; Zeile 11: NEXT
;
Line11:
	call	WrLine
	ld	de,$LINE9.1
	call	WrFile
;
; Zeile 12: (IF E=0 THEN )RETURN
;
	call	WrLine
	ld	a,(Copt)
	or	a
	ld	de,$LINE12.1
	call	nz,WrFile
	ld	de,$LINE12.2
	call	WrFile
;
; Zeile 13: (PRINT E;"ERROR";:IF E=1 THEN PRINT ELSE PRINT "S")
;
	ld	a,(Copt)
	or	a
	ret	z
	call	WrLine
	ld	de,$LINE13.1
	call	WrFile
;
; Zeile 14: (END)
;
	call	WrLine
	ld	de,$LINE14.1
	call	WrFile
	ret
;
; Schreiben der DATA-Zeilen
;
; Format: DATA d1,d2,d3,d4,d5,d6,d7(,summe)
;
wrDATA:
	ld	hl,(Heap)	; Datenadresse laden
	ld	de,(DatLen)	; Laenge laden
Wr1Line:
	push	de
	push	hl
	call	WrLine		; Zeilennummer ausgeben
	pop	hl
	ld	b,8		; Daten pro Zeile
	exx
	ld	hl,0		; Pruefsumme loeschen
	exx
	ld	de,$DATLIN
.Wr1Line:
	ld	c,(hl)		; Byte laden
	call	ascbyt		; Byte in ASCII wandeln
	inc	de		; Komma fixieren
	ld	a,c		; Byte holen
	exx
	call	adda		; Zur Pruefsumme addieren
	exx
	djnz	.Wr1Line	; Acht Bytes per Zeile
	ld	de,$DATA
	call	WrFile		; Zeile ausgeben
	ld	a,(Copt)	; Test ob Pruefsumme
	or	a
	jr	z,noCRC
	exx
	ld	de,$DATCRC
	push	de
	inc	de
	ld	a,h
	call	ascnib		; Pruefsumme dreistellig
	ld	a,l
	ld	hl,TMP
	ld	(hl),a
	call	ascbyt
	pop	de
	call	WrFile		; Summe schreiben
	exx
noCRC:
	call	Fcrlf
	pop	de		; Laenge holen
	dec	de
	ld	a,d
	or	e
	jr	nz,Wr1Line
	ret

	end

