
	; Virus imager

	; A track contains $1800 bytes of data.

	; track format description:

	; sync ($8944)
	; 1 unused byte
	; $1800 bytes data
	; 1 word checksum

	; The checksum test is quite strange, a CRC16 calculation is done
	; which always leads to 0 when everything went ok.
	; Part of the CRC16 calculation are also 3 sync signal words and
	; the unused byte following the sync, and ofcourse the checksum.

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

	; Similar formats: Starglider 2 (uses the same CRC16 method)

		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	"Virus 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	; 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	Init		; Called before a disk is read
		dc.l	0		; Called after a disk has been read

TL_1:		TLENTRY	40,69,$1800,$8944,DMFM_Virus	; actually track 1 to 70, but only tracks 40 to 69 contain relevant data
		TLEND

DMFM_Virus:
		lea	CRC_table,a2	; initialise registers for CalcCRC16
		moveq	#0,d1
		moveq	#-1,d2
		moveq	#-1,d3

		subq.l	#2*3,a0		; 3 syncwords needed for CRC calculation

		moveq	#3,d6
.l0		bsr.b	NextByte
		bsr.b	CalcCRC16
		dbra	d6,.l0

		move.w	#$17ff,d6
.l1		bsr.b	NextByte	; decode data
		bsr.b	CalcCRC16
		move.b	d0,(a1)+
		dbra	d6,.l1

		moveq	#1,d6
.l2		bsr.b	NextByte
		bsr.b	CalcCRC16
		dbra	d6,.l2

		or.b	d2,d3
		bne.b	.error

		moveq	#IERR_OK,d0
		rts
.error		moveq	#IERR_CHECKSUM,d0
		rts

NextByte:	move.w	(a0)+,d0
		BITSKIP_B d0
		rts
CalcCRC16:
		move.b	d0,d1
		eor.b	d2,d1
		lea	(a2,d1.w),a3
		move.b	(a3),d2
		eor.b	d3,d2
		move.b	$0100(a3),d3
		rts

Init:		; initialisation of the CRC table.

		lea	CRC_table,a0
		moveq	#0,d1
.l1		moveq	#0,d2
		move.b	d1,d2
		lsl.w	#8,d2
		moveq	#7,d0
.l0		add.w	d2,d2
		bcc.b	.s0
		eor.w	#$1021,d2	; $1021 = standard CRC16 value
.s0		dbra	d0,.l0
		move.b	d2,$0100(a0)
		lsr.w	#8,d2
		move.b	d2,(a0)+
		addq.b	#1,d1
		bne.b	.l1

		moveq	#IERR_OK,d0
		rts

	section	"BSS",bss

CRC_table:	ds.b	$200

