;$VER: BSC_old.s v1.01 (01-Mar-98)
;Updated by Zebedee/Area 51 from BSC by Crain/101

	opt	o+,w-
	section	bsc,code_c

ProgStart
	bsr.b	OpenDosLibrary
	beq.s	Quit
	bsr	ShowProgramTitle
	bsr.b	AllocateMemory
	beq.s	ClDos
	bsr	AskSourceFilename
	bsr	GetFileLength
	beq.s	FrMem
	bsr	ConvertFile
FrMem	bsr.b	FreeMemory
ClDos	bsr	CloseDosLibrary
Quit	moveq	#0,d0
	rts

OpenDosLibrary
	move.l	4.w,a6
	lea	Dos_L.l,a1
	jsr	-$198(a6)	;OldOpenLibrary(a1)exec
	move.l	d0,Dos_B.l
	rts

AllocateMemory
	move.l	4.w,a6
	move.l	#$25800,d0
	move.l	#$10000,d1
	jsr	-$C6(a6)	;AllocMem(d0,d1)exec
	move.l	d0,MemPtr1.l
	beq.s	.noMem1
	move.l	4.w,a6
	move.l	#$320,d0
	move.l	#$10000,d1
	jsr	-$C6(a6)	;AllocMem(d0,d1)exec
	move.l	d0,MemPtr2.l
	bne.s	.NoMem2
.noMem1	move.l	#NoChip,d2
	bsr	PrintText
	move.l	MemPtr1.l,d0
.NoMem2	rts

FreeMemory
	move.l	4.w,a6
	move.l	#$25800,d0
	move.l	MemPtr1.l,a1
	jsr	-$D2(a6)	;FreeMem(a1,d0)exec
	move.l	4.w,a6
	move.l	#$320,d0
	move.l	MemPtr2.l,a1
	jmp	-$D2(a6)	;FreeMem(a1,d0)exec

CloseDosLibrary
	move.l	4.w,a6
	move.l	Dos_B.l,a1
	jmp	-$19E(a6)	;CloseLibrary(a1)exec

OpenTargetFile
	move.l	Dos_B.l,a6
	move.l	#TargetN,d1
	move.l	#$3EE,d2
	jsr	-$1E(a6)	;Open(d1,d2)dos
	move.l	d0,FilePtr.l
	beq.s	ShowErrorSaving
	rts

WriteToFile
	move.l	Dos_B.l,a6
	move.l	FilePtr.l,d1
	move.l	MemPtr2.l,d2
	jsr	-$30(a6)	;Write(d1,d2,d3)dos
	cmp.l	#0,d0
	beq.s	ShowErrorSaving
	rts

CloseFile
	move.l	Dos_B.l,a6
	move.l	FilePtr.l,d1
	jmp	-$24(a6)	;Close(d1)dos

ShowErrorSaving
	move.l	#ErrSave,d2
	bsr	PrintText
	clr.l	d0
	rts

GetFileLength
	move.l	Dos_B.l,a6
	move.l	#SourceN,d1
	move.l	#$3ED,d2
	jsr	-$1E(a6)	;Open(d1,d2)dos
	move.l	d0,FilePtr.l
	beq.s	ShowLoadError
	move.l	d0,d1
	move.l	MemPtr1.l,d2
	move.l	#$257FF,d3	;$257FF=!153599
	jsr	-$2A(a6)	;Read(d1,d2,d3)dos
	move.l	d0,FileLen.l
	move.l	FilePtr.l,d1
	jsr	-$24(a6)	;Close(d1)dos
	move.l	FileLen.l,d0
	rts

ShowLoadError
	move.l	#LoadErr,d2
	bsr	PrintText
	move.l	FileLen.l,d0
	rts

ShowProgramTitle
	move.l	Dos_B.l,a6
	jsr	-$3C(a6)	;Output()dos
	move.l	d0,StdOut.l
	move.l	#Title,d2
	bsr	PrintText
	rts

AskSourceFilename
	move.l	#AskRead,d2
	bsr	PrintText
	bsr	GetUserInput
	move.l	#Buffer,a0
	move.l	#SourceN,a1
.loop	cmp.b	#10,(a0)
	beq.s	AskTargetFilename
	move.b	(a0)+,(a1)+
	bra.s	.loop

AskTargetFilename
	move.l	#AskSave,d2
	bsr	PrintText
	bsr	GetUserInput
	move.l	#Buffer,a0
	move.l	#TargetN,a1
.loop	cmp.b	#10,(a0)
	beq.s	AskByteWordLong
	move.b	(a0)+,(a1)+
	bra.s	.loop

AskByteWordLong
	move.l	#AskBWL,d2
	bsr	PrintText
	bsr	GetUserInput
	move.l	#Buffer,a0
	move.b	(a0)+,Align.l
	cmp.b	#10,(a0)
	beq.s	chk_a
MoveUp	move.l	#CrsrUp,d2
	bsr	PrintText
	bra.s	AskByteWordLong
chk_a	move.b	Align.l,d0
	cmp.b	#$61,d0	;a
	blt.s	chk_L
	sub.b	#$20,d0
	move.b	d0,Align.l
chk_L	cmp.b	#$4C,d0	;L
	bne.s	chk_W
	move.l	#4,Width.l
	bra.s	AskItemsPerLine
chk_W	cmp.b	#$57,d0	;W
	bne.s	chk_B
	move.l	#2,Width.l
	bra.s	AskItemsPerLine
chk_B	cmp.b	#$42,d0	;B
	bne.s	MoveUp
	move.l	#1,Width.l
AskItemsPerLine
	move.l	#AskIPL,d2
	bsr	PrintText
	bsr	GetUserInput
	move.l	#Buffer,a0
	move.w	(a0)+,Items.l
	cmp.b	#10,(a0)
	beq.s	InputIsValid
	move.w	Items.l,d2
	cmp.b	#10,d2
	beq.s	InputIsValid
Invalid	move.l	#CrsrUp,d2
	bsr	PrintText
	bra	chk_a

InputIsValid
	moveq	#0,d0
	moveq	#0,d1
	move.w	Items.l,d0
	move.b	d0,d1
	asr.l	#8,d0
	cmp.b	#10,d1
	bne.s	.notCR
	move.b	d0,d1
	move.b	#$30,d0	;"0"
.notCR	cmp.b	#$39,d0	;"9"
	bgt.s	Invalid
	cmp.b	#$39,d1	;"9"
	bgt.s	Invalid
	cmp.b	#$30,d0	;"0"
	blt.s	Invalid
	cmp.b	#$30,d1	;"0"
	blt.s	Invalid
	sub.l	#$30,d0	;Convert to 0x????????
	sub.l	#$30,d1	;Convert to 0x????????
	mulu	#10,d0
	add.l	d0,d1
	move.l	d1,ItemsHX.l
	rts

ConvertFile
	bsr	OpenTargetFile
	beq	Error
	move.l	MemPtr1.l,a3
NxtLine	move.l	MemPtr2.l,a5
	move.l	#$C7,d0
.loop	clr.l	(a5)+
	dbra	d0,.loop
	move.l	MemPtr2.l,a4
	move.b	#9,(a4)+	;tab
	move.b	#$64,(a4)+	;"d"
	move.b	#$63,(a4)+	;"c"
	move.b	#$2E,(a4)+	;"."
	move.b	Align.l,(a4)+
	move.b	#9,(a4)+	;tab
	moveq.l	#6,d7
	move.l	ItemsHX.l,d5
Dollar	move.b	#$24,(a4)+	;"$"
	addq.l	#1,d7
	move.l	Width.l,d6
.loop	bsr.b	ConvertToHex
	subq.l	#1,d6
	bne.s	.loop
	move.b	#$2C,(a4)+	;","
	addq.l	#1,d7
	subq.l	#1,d5
	bne.s	Dollar
	subq.l	#1,a4
	move.b	#10,(a4)+
	move.l	d7,d3
	movem.l	d1-d7/a0-a6,-(sp)
	bsr	WriteToFile
	movem.l	(sp)+,d1-d7/a0-a6
	cmp.l	#0,d0
	beq.s	Error
	move.l	a3,d0
	move.l	MemPtr1.l,d1
	sub.l	d1,d0
	cmp.l	FileLen.l,d0
	blt	NxtLine
	bsr	CloseFile
Error	rts

ConvertToHex
	moveq	#0,d2
	move.b	(a3)+,d2
	move.l	d2,d3
	and.l	#$F0,d2
	asr.l	#4,d2
	and.l	#15,d3
	add.l	#$30,d2	;"0"
	add.l	#$30,d3	;"0"
	cmp.b	#$39,d2	;"9"
	ble.s	.number
	addq.l	#7,d2
.number	cmp.b	#$39,d3	;"9"
	ble.s	.done
	addq.l	#7,d3
.done	move.b	d2,(a4)+
	move.b	d3,(a4)+
	addq.l	#2,d7
	rts

PrintText
	move.l	Dos_B.l,a6
	move.l	d2,a0
	moveq	#0,d3
.loop	addq.l	#1,d3
	cmp.b	#0,(a0)+
	bne.s	.loop
	subq.l	#1,d3
	move.l	StdOut.l,d1
	jmp	-$30(a6)	;Write(d1,d2,d3)dos

GetUserInput
	move.l	Dos_B.l,a6
	move.l	StdOut.l,d1
	move.l	#Buffer,d2
	move.l	#$FF,d3
	jmp	-$2A(a6)	;Read(d1,d2,d3)dos

	section	bscdata,data

Version	dc.b	'$VER: BSC Binary to Source Converter v1.01 (19-Feb-95)'
Buffer	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.b	'BSC originally coded by Crain/101 in NOV-88,'
	dc.b	' rewritten by Zebedee/Carnage in FEB-95'
AskRead	dc.b	'Binary data file to read: ',0
AskSave	dc.b	'Save assembler source as: ',0
AskBWL	dc.b	'[B]yte / [W]ord / [L]ong: ',0
AskIPL	dc.b	'# of data items per line: ',0
Title	dc.b	$9B,'0;2mBinary to Source Converter v1.01',$A
	dc.b	$9B,'0mBy CRAIN of 101 - The Maltese Hackers',$A
	dc.b	'Rewritten by Zebedee/Carnage',$A,$A,0
ErrSave	dc.b	'ERROR: I can''t save the file!',$9B,'0m',$A,0
NoChip	dc.b	'ERROR: Sorry, but there isn''t enough CHIPMEM',$9B,'0m',$A,0
LoadErr	dc.b	'ERROR: There seems to be a loading error!',$9B,'0m',$A,0
Dos_L	dc.b	'dos.library',0
Dos_B	dc.l	0
MemPtr1	dc.l	0
MemPtr2	dc.l	0
TargetN	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
FilePtr	dc.l	0
SourceN	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0
FileLen	dc.l	0
StdOut	dc.l	0
CrsrUp	dc.b	11
	dc.b	0
Align	dc.l	0
Items	dc.l	0
Width	dc.l	0
ItemsHX	dc.l	0
	dc.w	0
	end