* Convert CDId's Files  to  New CdId MegaCdPlayer format
* Remove's CdId's with Bad Total Cd Toc & 3rd Track Toc  Ie To Large Value's

		Machine	MC68020

		include /include/Wb_startup.i

start		jsr	openlibrarys
		bsr	windopen

* Open Title File  (default header)
		lea	DefSaveIndex,a0	
		lea	MegaSaveTitle,a1	filename
		move.l	#8,a2			size   (8 bytes)
		jsr	savefile	
* Open Index File  (default header)
		lea	DefSaveIndex,a0			
		lea	MegaSaveIndex,a1	filename
		move.l	#16,a2			size   (16 bytes)
		jsr	savefile
		clr.l	NumberOfCds
		clr.l	removedcds
		clr.l	Skip01Count
		lea	BadCdIdRemoved,a0
		move	#10,d0	x
		move	#30,d1	y
		bsr	Print		Print No. of Bad Cdid removed
		lea	Skip01Text,a0
		move	#10,d0	x
		move	#40,d1	y
		bsr	Print		Print No. of Bad Cdid removed
		
* Get CdId directory using FileRequester
		lea	CdIdReqTagList,a0
		jsr	RequestersTagList  Get Disk/Directory  Using Req.
		beq	Messageloop		Exit if Cancel selecte
		lea	SaveDirBuffer,a0 	Save Dir buffer Pt
		move.l	#511,d0 	 	Max size of buffer
		jsr	SaveDirRequester	Save CdId Root Dir.
		clr	SubDirNo

		
NextDirectory	bsr	ReadDirectory
		beq	Finished	If D0=0 Then Dir. not found, Finished
.loop		bsr	ReadNext
		beq	Finished	If D0=0 Then No more Files,  Finished
		tst.l	d1
		bpl.s	.loop		If D1=Plus Then is a Directory Skip
		move.l	a0,a4		Save FileName Pt into A4

**		move.l	a4,a0
		jsr	UseReqDirAddFile   Use Requester Directory & Add FileName
		beq	Messageloop
* Load CdId Text file  Cd Title, Author, Track Names
		move.l	#0,a0		Set A0=0 Memory Allocated for file
		move.l	d1,a1		File Name
		jsr	loadfile
		move.l	loadalcpt,temploadalcpt		save load Pt
		move.l	a0,a5				save start pt
		move.l	d0,d7				save size
		
* Make Cd Title & Aurthor Into one by taking out end Lf marker		
.lfloop		cmp.b	#10,(a0)+
		bne.s	.lfloop
		move.b	#" ",-1(a0)	Replace with a Space

		move.l	a4,a0
		bsr	ReadFileNameId	Read NoTracks,3rd Track Toc,Total Toc
		beq.s	.isCdIdfile
		add.l	#1,removedcds
		move.l	removedcds,d0
		bsr	HexDecConvert
		move	#29*8+10,d0	x
		move	#30,d1		y
		bsr	Print		Print Number of Cd's Removed 
		bra	.notCdIdfile

.isCdIdfile	move.l	a4,a0
		move	#10,d0
		move	#20,d1
		bsr	Print		Print File Name

		
* CdId files with "0" or "1" inbetween lines  remove the "1"'s

		moveq	#0,d2	"1" count
		moveq	#-1,d3	line count  (-1 dont count title)
		move.l	a5,a0
		moveq	#0,d1
.RemoveLoop	addq.l	#1,d1
		cmp.l	d7,d1		If EndOfFile
		bhi.s	.endfile
		cmp.b	#10,(a0)+
		bne.s	.RemoveLoop
.LFfound	addq.l	#1,d3		inc line count
		cmp.l	d7,d1		If lastchar of file
		beq.s	.RemoveLoop	then goto Removeloop to end
		cmp.b	#"0",(a0)	else Check if "0" char
		beq.s	.is0
		cmp.b	#"1",(a0)	or "1" char
		bne.s	.RemoveLoop
.is0		cmp.b	#10,1(a0)	and a LF char
		bne.s	.RemoveLoop
		addq.l	#1,d2		If is Then inc "0" "1" count
		bra.s	.RemoveLoop
.endfile	moveq	#0,d6		Clear Skip "1" Flag
		cmp	d4,d2		if "1" count < number of tracks
		blo.s	.NoSkip		then Noskip
		lsr.l	#1,d3		linecount /2
		cmp	d2,d3		if linecount < "1" count
		blo.s	.NoSkip
		
		moveq	#1,d6		Set Skip "1" Flag
		add.l	#1,Skip01Count
		move.l	Skip01Count,d0
		bsr	HexDecConvert
		move	#29*8+10,d0	x
		move	#40,d1		y
		bsr	Print		Print Number of 
.NoSkip		


* count number of Track lines	(dont use any Extra lines)
* and replace blank lines with a "*"

		lea	OutPutBuffer,a4
		moveq	#0,d1		chars count
		moveq	#0,d2		added chars count
		move.l	a5,a0
.countloop	addq.l	#1,d1
		cmp.l	d7,d1		If EndOfFile
		bhi.s	.notCdIdfile	then Error Dont used It  (to few tracks)
		move.b	(a0),(a4)+	move to output buffer
		cmp.b	#10,(a0)+
		bne.s	.countloop

* Skip "0" or "1" lines if Flag set		
		tst	d6		if Skip "1" Flag = 0
		beq.s	.noskipcheck	then Dont Skip check
		cmp.b	#"0",(a0)
		beq.s	.yes0
		cmp.b	#"1",(a0)	else Skip check
		bne.s	.noskipcheck
.yes0		cmp.b	#10,1(a0)
		bne.s	.noskipcheck
		add.l	#2,a0		skip "1" & Lf Char's
		
.noskipcheck	cmp.b	#10,-2(a4)	If BlankLine (Ie 2 LF's) 
		bne.s	.notblankline
		move.b	#"*",-1(a4)	put "*" in place of LF (BlankLine)
		move.b	#10,(a4)+	then a LF
		addq.l	#1,d2		inc added chars count
.notblankline	dbf	d4,.countloop
		move.b	#10,(a4)+	Put a LF at End
		addq.l	#1,d2
		add.l	d2,d1		add added chars count
		move.l	d1,d7		set new File size

	
* Add to Index file end		
		move.l	#16,a2			size
		lea	MegaSaveIndex,a1	fileName
		lea	SaveIndexCdInfo,a0	mem pt
		jsr	savetoend	- Save file to end
		add.l	#1,NumberOfCds


* Add to Title file End		
		move.l	d7,a2			size
		lea	MegaSaveTitle,a1	fileName
		lea	OutPutBuffer,a0
		jsr	savetoend	- Save file to end


.notCdIdfile	move.l	temploadalcpt,loadalcpt		reload load pt
		jsr	loadfreememory	free allocated memory used by file
		bra	.loop

* load Index file so can update "Number of Cd's"

Finished	lea	SaveDirBuffer,a0 	Save Dir buffer Pt
		jsr	SetDirRequester		Set Directory to Root (as Saved)
		add	#1,SubDirNo		Inc Dir No.
		move	SubDirNo,d0
		cmp	#99,d0			If SubDirNo > 99
		bhi.s	.AllDoneNow		Then All Directorys Done
		lea	CdIdSubDir,a0
		Divul.l	#10,d1:d0		Else Convert SubDirNo to Ascii
		addi.b	#$30,d0
		move.b	d0,(a0)
		addi.b	#$30,d1
		move.b	d1,1(a0)
		jsr	UseReqDirAddFile   	Combine Sub Dir to Root Dir
		move.l	d1,a0
		jsr	SetDirRequester		Set to New Dir
		bra	NextDirectory		Now do that directory

.AllDoneNow	lea	MegaSaveIndex,a1	fileName
		move.l	#0,a0		Set A0=0 Memory Allocated for file
		jsr	loadfile
		move.l	loadalcpt,temploadalcpt		save load Pt
		move.l	NumberOfCds,8(a0)
			
		lea	MegaSaveIndex,a1	filename
		move.l	d0,a2			size
		jsr	savefile		Save Index file back
		move.l	temploadalcpt,loadalcpt		reload load pt
		jsr	loadfreememory	free allocated memory used by file
		
		lea	Completedtext,a0
		move	#10,d0
		move	#100,d1
		bsr	Print		Print all compleated


		
Messageloop	Bsr	WaitforMessage	
		btst.l	#9,d3			if CloseWindow
		beq.s	Messageloop

exit		bsr	windclose
		jsr	closelibrarys
		rts


Lftext		dc.b	10,0
		even

CdIdReqTagList	dc.l	ASL_Hail,CdIdReqHailpt
		dc.l	ASL_Width,500
		dc.l	ASL_Height,180
		dc.l	ASL_Dir,CdIdReqDirpt
		dc.l	ASL_ExtFlags1,%1	(no files shown)
		dc.l	0

CdIdReqDirpt	dc.b "Work:CdIds",0
CdIdReqHailpt	dc.b "Select CdId's Directory",0
		even

* Cd Index File Format  (16 Bytes)
DefSaveIndex	dc.b	"CD@0"		Header
		dc.b	"V15",10	Version Number
NumberOfCds	dc.l	0		Number of Cd's on list
		dc.l	0		Total Number of Cds played (new for V1.5)

* New MegaCdPlayer Format saved with CdId's converted files
*				New CdId's Format	MegaCdPlayer Format
SaveIndexCdInfo	dc.l	0	3rd Track Toc		CdToc Last Track
		dc.w	0	Number of Tracks	Number of Tracks
		dc.l	0	Total Cd Toc		Not Used
		dc.l	0	Played Count		Played Count
		dc.w	"ID"	"ID"			Zero

Skip01Count	dc.l	0  Number of "0" or "1" in between tracknames skipped
removedcds	dc.l	0  Number of CdId Removed
BadCdIdRemoved	dc.b	"Number of Bad CdId Removed =",0
Skip01Text	dc.b	"Number of '0' '1' Skipped  =",0
		even

* Read Directory File Names		Call With  D1 - Pt to Directory to Read

ReadDirectory	move.l	dosbase,A6
***		move.l	#name,d1
		move.l	#-2,d2
		movea.l dosbase,A6
		jsr	Lock(a6)
		move.l	d0,locksav
		beq.s	.exit		If D0=0 then Directory Not Found
		move.l	dosbase,A6
		move.l	locksav,d1
		move.l	#fileinfo,d2	Pt to File Info Block (see P.152)
		jsr	Examine(a6)	get file info
		tst.l	d0
.exit		rts			If D0=0  Then Error!


ReadNext	move.l	dosbase,A6
		move.l	locksav,d1
		move.l	#fileinfo,d2
		jsr	ExNext(a6)
		tst.l	d0
		beq.s	.exit
		move.l	#fileinfo+8,a0		A0 Pt to File Name
		move.l	fileinfo+4,d1		If D1 is Plus   =Directory
.exit		rts				If D0 =0  Then No More files


MegaSaveTitle	dc.b	"Ram:Title",0
		even
MegaSaveIndex	dc.b	"Ram:Index",0
		even

SubDirNo	dc.w	0
CdIdSubDir	dc.b	"01",0
		even

SaveDirBuffer	ds.b	512
OutPutBuffer	ds.b	100*99+100	Space for 99 track name + title
		even

locksav		dc.l	0
temploadalcpt	dc.l	0



* Read File Name CdId    "ID99ABCDEF123456
*	   Number Tracks -> ||
*		 3rd Track -> |----||----|  <- Total Cd Toc's

ReadFileNameId	lea	SaveIndexCdInfo,a2
		cmp	#"ID",(a0)		Test If CdId File
		beq.s	.isCdIdfile
.notCdIdfile	moveq	#1,d0
		rts
.isCdIdfile	moveq	#0,d4
		move.b	2(a0),d4		Number of tracks (x10)
		sub.b	#"0",d4
		mulu	#10,d4
		move.b	3(a0),d0
		sub.b	#"0",d0
		add.b	d0,d4			Add unites
		move	d4,4(a2)
		
		lea	4(a0),a1
		bsr.s	.ConvertAsciiToHex
		move.l	d2,(a2)			3rd Track Toc
		cmp.l	#80*60*75,d2		Max = 80 mins x 60 x 75
		bhi.s	.notCdIdfile		If >Max  Then Dont use
		lea	10(a0),a1
		bsr.s	.ConvertAsciiToHex
		move.l	d2,6(a2)		Total Cd Toc
		cmp.l	#80*60*75,d2		Max = 80 mins x 60 x 75
		bhi.s	.notCdIdfile		If >Max  Then Dont use
		cmp.l	(a2),d2		If Total Cd Toc < 3rd Track Toc
		blo.s	.notCdIdfile	Then Dont use
*testing
*		cmp.l	#$025c65,d2
*		bne.s	.nnn
*		illegal
*.nnn
		moveq	#0,d0
		rts
		
.ConvertAsciiToHex
		moveq	#0,d2
		moveq	#6-1,d1
.loop		move.b	(a1)+,d0		read next character
		sub.b	#"A",d0
		bhs.s	.ischar
		add.b	#7,d0
.ischar		add.b	#10,d0			correct value 
		lsl.l	#4,d2			shift left digit 
		or.b	d0,d2			add digit
		dbf	d1,.loop
		rts




Completedtext	dc.b	"Completed Convertion!    Click Close Window to exit",0
		even





* * * Wait for Message * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WaitforMessage	movem.l	A0-A1/A6,-(SP)
		move.l  WindowHandle,a0
		move.l	86(a0),a0	get user port pt
		movea.l	4,A6
		jsr	WaitPort(a6)	Wait for Message
		move.l  WindowHandle,a0
		move.l	86(a0),a0	get user port pt
		jsr	GetMsg(a6)	Get Message
		move.l	d0,a1
		move.l	20(a1),d3	message type	d3= IDCMP  see AML p.175
		move	24(a1),d4	Code specifier (char code etc)
		move.l	28(a1),Iaddress
		jsr	ReplyMsg(A6)	Reply Messsage
		movem.l	(SP)+,A0-A1/A6
		rts
				
Iaddress	dc.l	0	Gadget Address
		


* Hex to Decimal Ascii    (uses 680020 Divul.l instruction)
* D0 - Hex number

HexDecConvert	lea	dcharbuf,a0
HexDecConvert2	moveq	#10-1,d2	10 digit number
.loop		Divul.l	#10,d1:d0			(680020 Instruction)
		addi.b	#$30,d1
		move.b	d1,(a0,d2)
		dbf	d2,.loop
* do some left digit zero blanking
		moveq	#10-1,d1  		(dont blank last digit zero)
.zloop		cmp.b	#$30,(a0)+
		bne.s	.fin			skip over if zero 
		dbf	d1,.zloop
.fin		subq.l	#1,a0			correct a0		
		rts

dcharbuf	ds.b 12
		even




* Print Text

* A0 - Pt to text to print
* D0 - X pos  
* D1 - Y pos

print		MOVEM.L	D0-D7/A0-A6,-(SP)
		move.l	a0,printpt
		lea	ptextcolour,a1
		move.l	intuitionbase,a6
		move.l	WindowHandle,a0
		move.l	50(a0),a0
		jsr	PrintIText(a6)
		MOVEM.L	(SP)+,D0-D7/A0-A6
		rts
	

ptextcolour	dc.b	4,0	text,Background colour
		dc.b	1	output mode
		even
		dc.w	0,0	x,y pos
printfont	dc.l	0	font
printpt		dc.l	0	text pt
		dc.l	0	no more text
	

windopen	move.l	Intuitionbase,a6
		lea	windowdef,a0
		jsr	openwindow(a6)
		move.l	d0,WindowHandle
		move.l	d0,a0
		move.l	$2e(a0),screenhandle	Get Window's Screen Handle
 		rts				from Window Structure


windclose	move.l	Intuitionbase,a6
		move.l	WindowHandle,d0
		beq.s	.exit
		move.l	WindowHandle,a0
		jsr	closewindow(a6)
.exit		rts

ClearWindow	MOVEM.L	A6,-(SP)
		moveq	#0,d0			X pos
		moveq	#0,d1			Y pos
		lea	_ClrWindowImage,a1
		move.l	intuitionbase,a6
		move.l	WindowHandle,a0
		move.l	50(a0),a0		Rp
		jsr	EraseImage(a6)		Erase Old Gadget Image
		MOVEM.L	(SP)+,A6
		rts

_ClrWindowImage	dc.w	4,11				x,y position
		dc.w	600-4-18,250-11-2		width & height
		dc.w	2
		dc.l	0	(Image data not used)
		dc.b	0,0
		dc.l	0



windowdef	dc.w	10,20		x,y
		dc.w	600,250		width & height
		dc.b	0,1		print and background colours
		dc.l	$200		IDCMP
		dc.l	$100f
		dc.l	0		Gadget pt
		dc.l	0
		dc.l	windname
		dc.l	0	screenhd (Set to 0 for Workbench or public)
		dc.l	0
		dc.w	200,40,600,200
		dc.w	$1		 (1=Workbench Screen  2=Public Screen)

windname	dc.b	"Convert CdIds to MegaCdPlayer files format",0
		even
WindowHandle	dc.l	0
screenhandle	dc.l	0	
	
	
openwindow	equ	-204
closewindow	equ	-72
execbase	equ	4
GetMsg		equ	-372
ReplyMsg	equ	-378
WaitPort	equ	-384
DisplayBeep	equ	-96
PrintIText	equ	-216
EraseImage	equ	-630

CurrentDir		equ	-126
SetCurrentDirName	equ	-558
ExNext			equ	-108


		include /include/Open_Close_All_Librarys
		include /include/LoadFile2

