
	; Mercenary 2 - Damocles savegame imager

	; A track contains $1800 bytes of data.

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

	; track format description:

	; sync ($4489)
	; 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 savegame 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_ERRORS	; Disk flags
		dc.l	TL_1		; List of tracks which contain data
		dc.l	0		; UNUSED, ALWAYS SET TO 0!
		dc.l	FL_NULL		; 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	SaveFiles	; Called after a disk has been read

TL_1:		TLENTRY	2,21,$1800,SYNC_INDEX,DMFM_NovagenSG
		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

	; The sync-signal is $4489, but due to the most likely case
	; that not all 10 savegame slots are used, the sync may not
	; be present so DFLG_ERRORS has been set and rawdic_NextMFMword
	; has been used to allow unformatted tracks.

DMFM_NovagenSG:

		move.w	d0,d6		; track number

		move.w	#$4489,d0	; sync signal
		jsr	rawdic_NextMFMword(a5)
		bne.b	.exit
		bsr	DMFM_Novagen
		bne.b	.exit		; error? no saved game...

		subq.w	#2,d6
		move.w	d6,d0
		lsr.w	#1,d0		; calculate slot number
		lea	SlotFlags(pc),a0
		subq.b	#1,(a0,d0.w)	; track could be read, decrease SlotFlags

		and.w	#1,d6
		bne.b	.exit		; if track=even then check first 3 words

		move.l	-$1800(a1),d6
		cmp.l	#$45631405,d6
		bne.b	.in		; first 3 words have to be $456314054e75
		move.w	4-$1800(a1),d6
		cmp.w	#$4e75,d6
		beq.b	.exit
.in		addq.b	#1,(a0,d0.w)	; no saved game!

.exit		moveq	#IERR_OK,d0	; no error
	; (always ok, because the checksum error may occur due to not used slot)
		rts

SaveFiles:

		moveq	#9,d2
.l0		lea	SlotFlags(pc),a0
		tst.b	(a0,d2.w)
		bne.b	.s0
		move.b	d2,d0
		or.b	#"0",d0
		move.b	d0,FNum

		move.w	d2,d0
		mulu.w	#$1800*2,d0
		move.l	#$1cd8,d1	; length of a saved game
		lea	FName(pc),a0
		jsr	rawdic_SaveDiskFile(a5)
.s0		dbra	d2,.l0

		moveq	#IERR_OK,d0
		rts

SlotFlags:	dcb.b	10,2	; only if both tracks for a saved game
				; could be read without errors, the
				; SlotFlags value for the slot is 0.

FName:		dc.b	"Mercenary2.save"
FNum:		dc.b	"0",0

