	title	MAP to SYM Convertsion
	name	('MAPTOSYM')
	maclib	base80

; Program MAPTOSYM

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20
;		Tel.:+49/040 4223247

;		Version 2.0, February 1988

; Convert .MAP files to .SYM format

;	MAPTOSYM {map_file{.ext} {sym_file{.ext}}

; The format of a .MAP file is as follows:

; item1 item2

; 'itemi' is defined as

; llll xxxx	- where llll is a 15 byte label name string
;		  xxxx is the 15 byte address including attributes

; item2 may be empty (cr, lf only)

	ext	strcm0,adda,fildrv,wrbuf,wrfcb
	ext	open,creatd,closef,fillin,emplin

	entry	$memry

_PRG	macro
	db	'MAPTOSYM'
	endm

_VER	macro
	db	'2.0'
	endm

;
; Some constants to make the program more readable
;
MAPsym	equ	2		; Number of .MAP symbols in line
SYMsym	equ	4		; Number of .SYM symbols in line
Offs2ad	equ	17		; Offset to .MAP symbol address
Offs2nd	equ	30		; Offset to 2nd .MAP symbol
MAPmax	equ	15		; Max length of .MAP symbol
HEXlen	equ	4		; Length of hex address
MAXstrg	equ	255		; Max length of file line

	ld	sp,(TPAtop)	; Load local stack
	ld	de,$ID
	call	strcm0		; Tell what we are
	ld	hl,FCBnam
	ld	a,(hl)
	dec	hl
	cp	' '		; Test parameter given
	jr	nz,got?par	; Yeap
	ld	de,$HELP
	call	strcm0		; Give some help if not
	jp	OS		; And exit
got?par:
	ld	de,FCBnm2
	ld	a,(de)
	dec	de
	cp	' '		; Test 2nd name given
	ld	bc,.fdrv+.fname
	call	z,_ldir		; Unpack if empty
	call	InitMemory	; Initialize required memory
	call	InitFiles	; Init files
	call	IniSYM		; Set number of symbols in .SYM file
NextLabel:
	ld	b,MAPsym	; Set number of symbols in .MAP file
	ld	c,0		; Init offset
ReadLoop:
	call	RdLine		; Read line from file
	jr	c,EndOfSYM	; End of file
	jr	z,ReadLoop	; Empty line
LabelLoop:
	call	DoLabel		; Process selected label and address
	ld	a,Offs2nd
	add	a,c		; Point to next symbol
	ld	c,a
	djnz	LabelLoop
	jr	NextLabel
EndOfSYM:
	ld	a,(SYMcnt)	; Get current count
	cp	SYMsym		; Test empty
	call	nz,WrLine	; Empty line if not
	call	closef		; Close file
	jp	OS
;
; Process label and address offsetted by BC
;
DoLabel:
	push	bc
	ld	b,0
	ld	hl,(SYMline)
	add	hl,bc		; Position in line
	ld	a,(hl)
	sub	cr		; Test defined
	jr	z,NoLabel	; Nope, skip
	push	hl		; Save start of symbol
	ld	a,Offs2ad
	call	adda		; Build index to address
	ld	de,(LinePtr)	; Get current address of file line
	ld	bc,HEXlen
	ldir			; Unpack hex address
	ld	a,' '
	ld	(de),a		; Set delimiter
	inc	de
	pop	hl		; Get back address
	ld	b,MAPmax	; Set max label length
CpyLabel:
	ld	a,(hl)		; Get character
	cp	' '		; Test blank
	jr	z,EndLabel	; Yeap, end
	ld	(de),a
	inc	hl
	inc	de
	djnz	CpyLabel
EndLabel:
	ld	a,tab
	ld	(de),a		; Set tab
	inc	de
	ld	(LinePtr),de	; Save pointer
	ld	hl,SYMcnt
	dec	(hl)		; Count down remaining labels
	jr	nz,NoLabel
	call	WrLine		; Empty line
	call	IniSYM		; Init SYM values
NoLabel:
	pop	bc
	ret
;
; Init SYM values
;
IniSYM:
	ld	a,SYMsym	; Set number of symbols in .SYM file
	ld	(SYMcnt),a
	ld	hl,(MAPline)	; Get base address of file line
	ld	(LinePtr),hl
	ret
;
; Process files
;
InitFiles:
	ld	de,FCBext	; Point to 1st to extension
	ld	hl,$$MAP
	call	setext		; Set default
	ld	de,FCBex2	; Point 2nd to extension
	ld	hl,$$SYM
	call	setext		; Set default
	ld	hl,(wrfcb)	; Point to FCB
	push	hl
	ld	e,l
	ld	d,h
	inc	de
	ld	(hl),0
	ld	bc,fcblen-1
	ldir			; Clear FCB
	pop	de
	ld	hl,FCB2
	ld	bc,.fdrv+.fname+.fext
	ldir			; Unpack FCB
	call	iniIN		; Init source file
	call	iniOT		; Init destination file
	ret
;
; Set default extension ^HL to ^DE
;
setext:
	ld	a,(de)
	cp	' '		; Test extension given
	ret	nz		; Yeap
	ld	bc,.fext	; Unpack if empty
;
; Move BC bytes from ^HL to ^DE
;
_ldir:
	ldir			; Unpack extension
	ret
;
; Read line from file
;
RdLine:
	push	bc
	ld	de,(SYMline)	; Get address of line
	dec	de		; Position to length
	ld	b,null
	call	fillin		; Read line
	pop	bc
	ret
;
; Put line to file
;
WrLine:
	ld	b,null
	ld	ix,(LinePtr)	; Get pointer
	ld	(ix-1),cr	; Close line
	ld	(ix+0),lf
	ld	(ix+1),b
	ld	de,(MAPline)	; Get base address of file line
	call	emplin		; Put line
	ret	nc		; Ok
	ld	hl,$WRTERR
	ld	de,(wrfcb)	; Get FCB
	jr	errfile
;
; Init source file
;
iniIN:
	ld	de,FCB		; Get FCB
	call	open		; Open file
	ret	nc		; File found
	ld	hl,$NOMAP
	jr	errfile
;
; Init destination file
;
iniOT:
	ld	de,(wrfcb)	; Get FCB
	call	creatd		; Create file
	ret	nc		; File created
	ld	hl,$NOSYM
errfile:
	call	strcm0		; Tell file name
	ex	de,hl
	call	strcm0		; Give error message
	jp	OS		; And exit
;
; Initialize required memory
;
InitMemory:
	ld	hl,($memry)	; Fetch base heap address
	ld	(wrbuf),hl	; Set buffer address
	ld	de,reclng
	add	hl,de
	ld	(wrfcb),hl	; Set FCB
	ld	de,fcblen
	add	hl,de		; Build top
	ld	(hl),MAXstrg	; Force max length
	inc	hl
	ld	(SYMline),hl	; Save address
	inc	h		; Next page
	ld	(MAPline),hl
	inc	h		; Next page
	ld	(heap),hl	; Set top of heap
	ret

	dseg

SYMcnt:
	db	0
$memry:
	dw	0
heap:
	dw	0
LinePtr:
	dw	0
SYMline:
	dw	0
MAPline:
	dw	0
$ID:
	_PRG
	db	' - Version '
	_VER
	db	cr,lf,lf,null
$HELP:
	_PRG
	db	' converts .MAP-files into .SYM-format'
	db	cr,lf,lf
	db	'MAP-files will be created by the PD-Linker ZLINK'
	db	 cr,lf,lf
	db	'Call it:'
	db	cr,lf,lf
	db	tab
	_PRG
	db	' {'
$$MAP:
	db	'MAP'
	db	'-file {'
$$SYM:
	db	'SYM'
	db	'-file}}'
	db	 cr,lf,null
$NOMAP:
	db	" : File does not exist!"
	db	cr,lf,null
$NOSYM:
	db	" : File cannot be created!"
	db	cr,lf,null
$WRTERR:
	db	" : Write error!"
	db	cr,lf,null

	end

