;	OPT	l-	; Make executable code (DevPac etc.)
	EXEOBJ		; Make executable code (Macro68 etc.)

; Note:	Comment EXEOBJ out and remove semi-colon from first line
;	if you are using DevPac or some compatible assembler

*****************************************************************
*			xSum.asm V1.0				*
*	simple example for XPK interface by Ch.Schneider	*
*****************************************************************
*	Sums up all bytes in compressed or uncompressed file	*
*	This is a typical read-and-process xpk application.	*
*	Try it out... xSum a file, then compress it and xSum	*
*	it again. The result should be the same.		*
*	NOTE: Only start this from CLI!	No WBStartup		*
*	      included for simplicity! No CLI Parsing is done!	*
*****************************************************************

	OUTPUT	xSum		; Output to file "xSum" (Macro68, DevPac, etc.)
;	OBJFILE	"xSum"		; Output to file "xSum" (Adapt etc.)

	INCLUDE	AINCLUDE:IncDirs.i
	INCLUDE	xpk/xpk.i

************* Kludge to get around include problem :-)) ********
; Note: Not everyone really needs this stuff: Macro68 provides
; the possibility to have resident & preassembled includes ;-) TINAR :-)
_LVOAllocMem		EQU	-198
_LVOFreeMem		EQU	-210
_LVOOpenLibrary		EQU	-552
_LVOCloseLibrary	EQU	-414

_LVOOpen		EQU	-30
_LVOClose		EQU	-36
_LVOWrite		EQU	-48
_LVOOutput		EQU	-60

CALLSYS MACRO		; call a library via A6 without having to see _LVO
	JSR	_LVO\1(A6)
	ENDM
********* EOK (End-Of-Kludge :-) ***********

start:
	clr.b	-1(a0,d0.l)		; Convert LF to 0
	move.l	a0,filename		; Store commandline (no parsing!)

**************** Opening libraries **************
	move.l	4,a6			; ExecBase
	lea	dosname,a1
	moveq	#0,d0			; We don't care about version numbers
	CALLSYS	OpenLibrary
	lea	CannotOpenDosTxt,a0
	move.l	d0,dosbase		; Store DOS Library base!
	beq	cleanup			; ERROR -> Exit

	lea	xpkname,a1
	moveq	#0,d0			; We don't care about version numbers
	CALLSYS	OpenLibrary
	lea	CannotOpenXPKTxt,a0
	move.l	d0,xpkbase		; Store XPK Library base!
	beq	cleanup			; ERROR -> Exit

**************** Loading (and perhaps decrunching) file **************
	move.l	xpkbase,a6		; XPKBase to A6
	lea	tags,a0
	CALLSYS	XpkUnpack

	tst.l	d0
	beq	noerrorloadingfile
	
**************** Error loading file **************
	lea	errbuf,a0
	move.l	a0,a1
1$
	tst.b	(a1)+
	bne	1$			; Get terminating NULL
	move.b	#10,-(a1)		; Convert it to a LF
	bra	cleanup

**************** Calculating Sum **************
noerrorloadingfile:
	move.l	outbuf,a0
	moveq	#0,d0
	move.l	outlen,d7
1$
	moveq	#0,d1
	move.b	(a0)+,d1
	add.l	d1,d0			; Calculate Sum...
	subq.l	#1,d7
	bne	1$

	bsr	printnumber		; Simple number printing routine

	suba.l	a0,a0			; No error msg
************ General Cleanup Routine *************
cleanup:
	bsr	print			; Print error msg!

	move.l	4,a6
	move.l	outbuf,d0
	beq	1$
	move.l	d0,a1
	move.l	outbuflen,d0
	CALLSYS	FreeMem			; Free XPK output buffer
1$

	move.l	xpkbase,d0
	beq	2$			; Library not open
	move.l	d0,a1
	CALLSYS	CloseLibrary		; Close XPK library
2$
	move.l	dosbase,d0
	beq	3$			; Library not open
	move.l	d0,a1
	CALLSYS	CloseLibrary		; Close Dos library
3$
	moveq	#0,d0			; No error code!
	rts


********************** Support routines ********************
*************** Simple Decimal Number Printing Routine *****
printnumber:	; D0 = Number
	movem.l	d2-d7/a2-a6,-(a7)
	lea	numberbuffer,a0

	lea	powertentable,a1
	move.l	a0,a2
	moveq	#'0',d2
1$
	move.l	(a1)+,d1
	beq	4$
	cmp.l	d1,d0
	blo	1$
2$
	moveq	#'0'-1,d2
3$
	addq.b	#1,d2
	sub.l	d1,d0
	bcc	3$
	add.l	d1,d0
4$
	move.b	d2,(a2)+
	move.l	(a1)+,d1
	bne	2$

	move.b	#10,(a2)+		; Append LF!
	clr.b	(a2)
	bsr	print			; Output string

	movem.l	(a7)+,d2-d7/a2-a6
	rts


*************** Print String routine *****
print:	; A0 = String
	movem.l	d2-d7/a2-a6,-(a7)
	move.l	a0,d2
	beq	99$			; No string!
	moveq	#-1,d3
1$
	addq.l	#1,d3
	tst.b	(a0)+			; Calc string length!
	bne	1$

	move.l	dosbase,d0
	beq	99$			; No Doslibrary!
	move.l	d0,a6
	CALLSYS	Output			; Get output filehandle
	move.l	d0,d1			; Filehandle
	beq	99$			; No Output!

	CALLSYS	Write			; Write string!
99$
	movem.l	(a7)+,d2-d7/a2-a6
	rts


***************************************
*************** DATA Part *************
***************************************
	SECTION	data,data
dosbase:	dc.l	1
xpkbase:	dc.l	1

powertentable:
	dc.l	1000000000
	dc.l	100000000
	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1
	dc.l	0
	dc.l	0

tags:
		dc.l	XPK_InName
filename:	dc.l	0			; The file name to be read
		dc.l	XPK_GetError,errbuf	; A pointer to the error message buffer
		dc.l	XPK_GetOutBuf
		dc.l	outbuf			; Sets a pointer to the output buffer
		dc.l	XPK_GetOutLen
		dc.l	outlen			; Sets the number of bytes written
		dc.l	XPK_GetOutBufLen
		dc.l	outbuflen		; Sets the length of the output buffer
		dc.l	XPK_PassThru,-1		; Will pass through uncompressed data
		dc.l	TAG_DONE


outbuf:		ds.l	1
outlen:		ds.l	1
outbuflen:	ds.l	1

************** Byte size Data ***********
errbuf:		ds.b	XPKERRMSGSIZE+1		; +1 to make room for LF
numberbuffer:	ds.b	12			; Space for 10 digits + LF + 0

dosname:	dc.b	'dos.library',0
xpkname:	XPKNAME

CannotOpenDosTxt:	dc.b	"Cannot open dos.library!",10,0
CannotOpenXPKTxt:	dc.b	"Cannot open xpkmaster.library!",10,0

	END
