; Program to convert 1.2 FD files into include files for use with a68k.

; Corrected the bug in earlier version which led to a Berty Bassett 
;Intuition_lib.i file being generated.

; Copyright © M.Meany 1990

;		opt 		o+,ow-

		incdir		"df0:include/"
		include		"exec/exec_lib.i"
		include		"exec/exec.i"
		include		"libraries/dos_lib.i"
		include		"libraries/dos.i"
		include		"libraries/dosextens.i"

start		lea		dosname,a1
		moveq.l		#0,d0
		CALLEXEC	OpenLibrary
		move.l		d0,_DOSBase
		beq		quit
		CALLDOS		Output
		move.l		d0,d1
		move.l		#yo_text,d2
		move.l		#yo_len,d3
		CALLDOS		Write
		move.l		#5000,d0
		move.l		#MEMF_PUBLIC!MEMF_CLEAR,d1
		CALLEXEC	AllocMem
		move.l		d0,i_buffer
		beq		no_mem1
		move.l		#10000,d0
		move.l		#MEMF_PUBLIC!MEMF_CLEAR,d1
		CALLEXEC	AllocMem
		move.l		d0,o_buffer
		beq		no_mem2
		lea		fd_files,a4
		lea		inc_files,a5
		sub.l		#4,a4
		sub.l		#4,a5		
		moveq.l		#4,d7
		moveq.l		#17,d5		loop counter
exp_loop	add.l		d7,a4
		add.l		d7,a5
		move.l		(a4),d0
bp1		bsr		convert
		tst.l		d0
		beq.s		error_outINC
		move.l		d0,d6
		move.l		(a5),d1
		move.l		#MODE_NEWFILE,d2
		CALLDOS		Open
		move.l		d0,out_handle
		beq.s		error_outINC
		move.l		d0,d1
		move.l		o_buffer,d2
		move.l		d6,d3
		CALLDOS		Write
		move.l		out_handle,d1
		CALLDOS		Close
error_outINC	dbra		d5,exp_loop
		move.l		o_buffer,a1
		move.l		#10000,d0
		CALLEXEC	FreeMem
no_mem2		move.l		i_buffer,a1
		move.l		#5000,d0
		CALLEXEC	FreeMem
no_mem1		move.l		_DOSBase,a1
		CALLEXEC	CloseLibrary
quit		rts
		
out_handle	dc.l		0

fd_files	dc.l		fd1
		dc.l		fd2
		dc.l		fd3
		dc.l		fd4
		dc.l		fd5
		dc.l		fd6
		dc.l		fd7
		dc.l		fd8
		dc.l		fd9
		dc.l		fd10
		dc.l		fd11
		dc.l		fd12
		dc.l		fd13
		dc.l		fd14
		dc.l		fd15
		dc.l		fd16
		dc.l		fd17
		dc.l		fd18
		
inc_files	dc.l		inc1
		dc.l		inc2
		dc.l		inc3
		dc.l		inc4
		dc.l		inc5
		dc.l		inc6
		dc.l		inc7
		dc.l		inc8
		dc.l		inc9
		dc.l		inc10
		dc.l		inc11
		dc.l		inc12
		dc.l		inc13
		dc.l		inc14
		dc.l		inc15
		dc.l		inc16
		dc.l		inc17
		dc.l		inc18
		
fd1		dc.b		'Extras:fd1.2/clist_lib.fd',0
		even
fd2		dc.b		'Extras:fd1.2/console_lib.fd',0
		even
fd3		dc.b		'Extras:fd1.2/diskfont_lib.fd',0
		even
fd4		dc.b		'Extras:fd1.2/dos_lib.fd',0
		even
fd5		dc.b		'Extras:fd1.2/exec_lib.fd',0
		even
fd6		dc.b		'Extras:fd1.2/expansion_lib.fd',0
		even
fd7		dc.b		'Extras:fd1.2/graphics_lib.fd',0
		even
fd8		dc.b		'Extras:fd1.2/icon_lib.fd',0
		even
fd9		dc.b		'Extras:fd1.2/intuition_lib.fd',0
		even
fd10		dc.b		'Extras:fd1.2/layers_lib.fd',0
		even
fd11		dc.b		'Extras:fd1.2/mathffp_lib.fd',0
		even
fd12		dc.b		'Extras:fd1.2/mathieeedoubbas_lib.fd',0
		even
fd13		dc.b		'Extras:fd1.2/mathieeedoubtrans_lib.fd',0
		even
fd14		dc.b		'Extras:fd1.2/mathtrans_lib.fd',0
		even
fd15		dc.b		'Extras:fd1.2/potgo_lib.fd',0
		even
fd16		dc.b		'Extras:fd1.2/cstrings_lib.fd',0
		even
fd17		dc.b		'Extras:fd1.2/timer_lib.fd',0
		even
fd18		dc.b		'Extras:fd1.2/translator_lib.fd',0
		even
		
inc1		dc.b		'workdisc:include/clist_lib.i',0
		even
inc2		dc.b		'workdisc:include/console_lib.i',0
		even
inc3		dc.b		'workdisc:include/diskfont_lib.i',0
		even
inc4		dc.b		'workdisc:include/dos_lib.i',0
		even
inc5		dc.b		'workdisc:include/exec_lib.i',0
		even
inc6		dc.b		'workdisc:include/expansion_lib.i',0
		even
inc7		dc.b		'workdisc:include/graphics_lib.i',0
		even
inc8		dc.b		'workdisc:include/icon_lib.i',0
		even
inc9		dc.b		'workdisc:include/intuition_lib.i',0
		even
inc10		dc.b		'workdisc:include/layers_lib.i',0
		even
inc11		dc.b		'workdisc:include/mathffp_lib.i',0
		even
inc12		dc.b		'workdisc:include/mathieeedoubbas_lib.i',0
		even
inc13		dc.b		'workdisc:include/mathieeedoubtrans_lib.i',0
		even
inc14		dc.b		'workdisc:include/mathtrans_lib.i',0
		even
inc15		dc.b		'workdisc:include/potgo_lib.i',0
		even
inc16		dc.b		'workdisc:include/cstrings_lib.i',0
		even
inc17		dc.b		'workdisc:include/timer_lib.i',0
		even
inc18		dc.b		'workdisc:include/translator_lib.i',0
		even

dosname		dc.b		'dos.library',0
		even
_DOSBase	dc.l		0

yo_text		dc.b		$0a,'ConvertFD © M.Meany 1990',$0a,$0a
		dc.b		' This utility will convert the .fd files on the `Extras` disc',$0a
		dc.b		' into assembler include files. The program assumes that the path',$0a
		dc.b		' `Workdisc:include/`  exsists. All the .i files will be created in',$0a
		dc.b		' this directory.',$0a,$0a
		dc.b		' My address: 1 Cromwell Rd., Southampton, Hant`s, SO1 2JH.',$0a,$0a
yo_len		equ		*-yo_text
		even
*****************************************************************************

convert		movem.l		d1-d7/a0-a7,-(sp)
		move.l		d0,d1
		move.l		#MODE_OLDFILE,d2
		CALLDOS		Open
		move.l		d0,handle_in
		beq.s		error_inFD
		move.l		d0,d1
		move.l		i_buffer,d2
		move.l		#5000,d3
		CALLDOS		Read
		move.l		d0,d7
		beq.s		error_inFD
		move.l		handle_in,d1
		CALLDOS		Close
		move.l		i_buffer,a5
		move.l		o_buffer,a4
		bsr		replace_hash
bp2		bsr		find_bias
		moveq.l		#0,d6
		lea		offset,a3
		bsr		do_conversion
		move.l		a4,d0
		sub.l		o_buffer,d0
error_inFD	movem.l		(sp)+,d1-d7/a0-a7
		rts
		
handle_in	dc.l		0

*****************************************************************************

do_conversion	move.l		#'(',d3
		moveq.l		#$0a,d2
		subq.l		#1,d7
char_loop	move.b		(a5)+,d4
		cmp.b		d4,d2
		bne.s		not_CR
		moveq.l		#0,d6	
		bra		not_brace
not_CR		cmp.b		d4,d3
		bne.s		not_brace
		bsr		got_brace
not_brace	move.b		d4,(a4)+
		dbra		d7,char_loop
		rts
		
*****************************************************************************

got_brace	not.l		d6
		beq.s		dont_equate
		bsr		hex_con
		moveq.l		#11,d0
		movea.l		a3,a0
.loop		move.b		(a0)+,(a4)+
		dbra		d0,.loop
dont_equate	rts

*****************************************************************************
; Replace all # symbols with ; symbols so these lines will be ignored by the
;assembler.

replace_hash	move.l		a5,a0
		move.l		d7,d0
		subq.l		#1,d0
		move.l		#'#',d1
repl_loop	cmp.b		(a0)+,d1
		bne.s		not_hash
		move.b		#';',-1(a0)
not_hash	dbra		d0,repl_loop
		rts
		
*****************************************************************************

; Finds the starting offset for this file and stores it in d7.

; Entry		a5 must hold start address of input buffer.

; Exit		d7 will hold starting offset.

; Corrupt	a0

find_bias	movem.l		d1-d2/a1-a3,-(sp)
		move.l		a5,a0
		lea		string,a1
		move.l		#2,d1		length  of string-2
		move.b		(a1)+,d5
.loop		cmp.b		(a0)+,d5
		bne.s		.loop
		move.l		a0,a2		copy buffer pointer
		move.l		a1,a3		copy string pointer
		move.l		d1,d2		copy counter
.loop1		cmp.b		(a2)+,(a3)+
		dbne		d2,.loop1
		bne.s		.loop
		adda.l		#4,a0
		moveq.l		#0,d5
		move.b		(a0)+,d5
		sub.w		#'0',d5
		mulu.w		#10,d5
		add.b		(a0),d5
		sub.w		#'0',d5
		movem.l		(sp)+,d1-d2/a1-a3
		rts

string		dc.b	'bias',0
		even


*****************************************************************************

; routine to convert a word into a 4 byte ASCII string for printing
; CORRUPTED d0,d1,a0

hex_con		move.l		d5,d1		get offset
		lea		hex,a0		addr to write string
		move.w		d1,d0		get copy of word
		lsr.w		#8,d0		move MSB into LSB
		swap		d1		store word safely
		jsr		hexconvert	convert MSB
		swap		d1		retrieve word
		move.w		d1,d0		copy into d0
		jsr		hexconvert	convert LSB
		addq.l		#6,d5		bump offset
		rts				finished so return

; routine to convert a byte to a 2 byte ASCII string for printing
; ENTRY d0=byte  a0->address to store string

hexconvert 	move.b		d0,d1
		andi.b		#$f0,d0		mask off 1st nibble
		lsr.b		#4,d0		correct nibble position
		jsr		h_convert		convert to ASCII
		move.b		d1,d0		get copy of byte
		andi.b		#$0f,d0		mask off 2nd nibble
		jsr		h_convert		convert to ASCII
		rts				leave

h_convert		cmpi.b		#$0a,d0		is nibble a letter
		blt.s		add1		if not branch
		addi.b		#$07,d0		add letter offset
add1		addi.b		#$30,d0		add numeric offset
		move.b		d0,(a0)+	store value
		rts				return

offset		dc.b		$09,'equ',$09,'-$'
hex		dc.b		'    ',$09
		even

*****************************************************************************

; General variables

		rts
i_buffer	dc.l		0
o_buffer	dc.l		0
;file_len	dc.l		len
		
