;
; masc - Macintoy Amiga Sound Converter
;
; by Blaise Tarr
; © 1992
;

EXECBASE	equ 4
BUFFLEN		equ 10000

	SECTION gnomes,CODE

Start:
        cnop    0,2
        move.l  sp,Initial_SP

	lea	fname1,a1
	; look at command line arguments
	cmpi.b	#$20,(a0)
	bne.s	f1		; found first argument
	addq.l	#1,a0
	subq.w	#1,d0
	beq	how		; just a bunch of spaces, tell how to use

f1:	move.b	(a0)+,(a1)+
	subq	#1,d0
	beq	how		; only 1 name was specified
	cmpi.b	#$20,-1(a1)
	bne.s	f1
	move.b	#0,-1(a1)	; zero terminate file name

	lea	fname2,a1
	cmpi.b	#$20,(a0)
	bne.s	f2		; found second argument
	addq.l	#1,a0
	subq.w	#1,d0
	beq	how		; just spaces after first argument

f2:	move.b	(a0)+,(a1)+
	subq	#1,d0
	beq.s	gotit		; got both file names
	cmpi.b	#$20,-1(a1)
	bne.s	f2
gotit:	move.b	#0,-1(a1)	; zero terminate second file name

	; open the DOS library
	move.l	EXECBASE,a6
	lea	DOSNAME,a1
	jsr	-408(a6)	; OldOpenLibrary
	move.l	d0,DOSBASE
	beq	end

	move.l	DOSBASE,a5
	move.l	#fname1,d1
	move.l	#1005,d2	; Mode_old
	jsr	-30(a5)		; Open
	move.l	d0,file1hd	; save file1 handle
	beq	cexit

	move.l	#fname2,d1
	move.l	#1006,d2	; Mode_new
	jsr	-30(a5)		; Open
	move.l	d0,file2hd	; save file2 handle
	beq.s	cexit


domore:	move.l	#BUFFLEN,d3	; length of buffer in d3
	move.l	#buffer,d2	; address of buffer in d2
	move.l	file1hd,d1	; file handle 1 in d1
	jsr	-42(a5)		; Read

	move.l	#buffer,a3
	move.l	d0,d5
	subq.w	#1,d5
	moveq	#-128,d2
conv:	move.b	(a3),d3
	add.b	d2,d3
	move.b	d3,(a3)+
	dbra	d5,conv

	move.l	d0,d3
	move.l	#buffer,d2
	move.l	file2hd,d1
	jsr	-48(a5)		; Write

	cmpi.w	#BUFFLEN,d0
	blt.s	cexit		; EOF was readched

	bra.s	domore


cexit:	move.l	file1hd,d1	; file 1 handle in d1
	beq.s	.1ce		; if 0 -> never opened
	jsr	-36(a5)		; Close
.1ce:	move.l	file2hd,d1	; file 2 handle in d1
	beq.s	.2ce		; if 0 -> never opened
	jsr	-36(a5)		: Close
.2ce:	move.l	DOSBASE,d1
	beq.s	.3ce
	move.l	d1,a1
	jsr	-414(a6)	; CloseLib
.3ce:


end:	move.l	Initial_SP,sp
	rts


how:
	; open the DOS library
	move.l	EXECBASE,a6
	lea	DOSNAME,a1
	jsr	-408(a6)	; OldOpenLibrary
	move.l	d0,DOSBASE
	beq	end
	move.l	DOSBASE,a5

	jsr	-60(a5)		; Output
	move.l	d0,d1		; handle for standard output
	move.l	#txt,d2		; address of buffer
	move.l	#yo-txt,d3	; # of bytes to write
	jsr	-48(a5)		; Write
	bra.s	cexit

	cnop	0,2

txt:	dc.b	'Usage:  masc <infile> <outfile>',10
yo:

	cnop	0,2

DOSNAME:	dc.b	'dos.library',0


	SECTION err,DATA

DOSBASE:	dc.l	0
file1hd		dc.l	0
file2hd		dc.l	0


	SECTION yoo,BSS
fname1		ds.b	200
fname2		ds.b	200
Initial_SP:	ds.l	1
buffer		ds.b	BUFFLEN

	end