
	; Mercenary 2 - Damocles imager

	; A track contains $1800 bytes of data.

	; The format is singlesided, both sides contain the same data.

	; track format description:

	; sync ($A89A)
	; 4 unused bytes ($12345678)
	; 1 byte track number
	; $1800 bytes data
	; 1 word checksum

	; The checksum is calculated by adding all data bytes to a word variable
	; which is rotated to the left every step.

	; The MFM decoding is done by skipping all odd bits in the bitstream.

	; Similar formats: All other Novagen games on Amiga

		incdir	Includes:
		include	RawDIC.i

		SLAVE_HEADER
		dc.b	1	; Slave version
		dc.b	0	; Slave flags
		dc.l	DSK_1	; Pointer to the first disk structure
		dc.l	Text	; Pointer to the text displayed in the imager window

		dc.b	"$VER:"
Text:		dc.b	"Damocles imager V1.0",10,"by John Selck on 02.03.1999",0
		cnop	0,4

DSK_1:		dc.l	0		; Pointer to next disk structure
		dc.w	1		; Disk structure version
		dc.w	DFLG_SINGLESIDE|DFLG_ERRORSWAP	; Disk flags
		dc.l	TL_1		; List of tracks which contain data
		dc.l	0		; UNUSED, ALWAYS SET TO 0!
		dc.l	FL_DISKIMAGE	; List of files to be saved
		dc.l	0		; Table of certain tracks with CRC values
		dc.l	0		; Alternative disk structure, if CRC failed
		dc.l	0		; Called before a disk is read
		dc.l	0		; Called after a disk has been read

TL_1:		TLENTRY	1,79,$1800,$A89A,DMFM_Novagen
		TLEND

DMFM_Novagen:	; Decoder for tracks in Novagens very own format.

		moveq	#0,d1
		moveq	#20,d2
.l0		bsr.b	NextByte
		lsl.l	#8,d1
		move.b	d0,d1
		cmp.l	#$12345678,d1
		dbeq	d2,.l0

		bsr.b	NextByte	; skip track number

		moveq	#0,d2
		move.w	#$17ff,d1
.l1		bsr.b	NextByte	; decode data
		add.b	d0,d2
		rol.w	#1,d2
		move.b	d0,(a1)+
		dbra	d1,.l1

		bsr.b	NextByte	; compare checksum on disk
		move.b	d0,d1		; with calculated sum
		bsr.b	NextByte
		lsl.w	#8,d1
		move.b	d0,d1
		rol.w	#8,d1
		cmp.w	d1,d2
		bne.b	.error

		moveq	#IERR_OK,d0	; no error
		rts
.error		moveq	#IERR_CHECKSUM,d0	; checksum error
		rts

NextByte:	move.w	(a0)+,d0
		BITSKIP_B d0
		rts

