;--------------------------------------------------------------------
;Track-Loader written for News-Flash by VECTOR of MIRAGE
;12-Feb-1990
;--------------------------------------------------------------------

	include		"VD0:include/hardware.i"
	include		"VD0:include/macros"

	opt		p+,o+			;pc-relative & optimize

;--------------------------------------------------------------------

execbase	equ	$4
forbid		equ	-132
permit		equ	-138

;--------------------------------------------------------------------
	SECTION		Main,Code

loader
	movea.l		(execbase).w,a6		;multitasking off
	jsr		forbid(a6)

	DRIVEON		0001			;drive 0 on

	SEEKZERO	$1200,D0		;goto track 0
	STEPOUT					;step outside
	STEPTRACK	$1200,d0

	lea		$dff000,a6		;hardware base ->a6
	move.w		#$4489,DSKSYNC(a6)	;Syncword
	move.w		#$4000,DSKLEN(a6)	;disable writing
	move.w		#$7f00,ADKCON(a6)	;clear ADKCON
	move.w		#$9500,ADKCON(a6)	;MFM, No PRECOMP

	lea		buffer,a0
;--------------------------------------------------------------------
	moveq		#9,d7			;read 10 tracks
	lea		buffer,a0		;decode buffer -> a0

doit	LOWERHEAD
dolow	bsr.s		readtrack		;read track
	bsr		decodedostrack		;decode track
	bne.s		dolow			;if ERROR -> read again
	
	UPPERHEAD
doup	bsr.s		readtrack		;read track
	bsr.s		decodedostrack		;decode track
	bne.s		doup			;if ERROR -> read again

	STEPTRACK	$1200,d0		;step a track

	dbf		d7,doit			;decode next track

	DRIVEOFF	0001			;drive 0 off

	movea.l		(execbase).w,a6		;start multitasking
	jsr		permit(a6)

	rts
;--------------------------------------------------------------------
readtrack
	move.l		#trackbuffer,DSKPTH(a6)
	move.w		#$9980,DSKLEN(a6)	;read $1980 words
	move.w		#$9980,DSKLEN(a6)	;(Whole track)

	WAITDISK				;wait till disk DMA
						;is finished
	rts
;--------------------------------------------------------------------
;a0 -> pointer to decodebuffer
;a1 -> track buffer (±12Kb)
;--------------------------------------------------------------------
decodedostrack
	lea		trackbuffer,a1		;pointer to trackbuffer
	move.l		#$55555555,d3
	moveq		#10,d5			;decode 11 blocks

decodenextblock
	cmpi.w		#$4489,(a1)+		;check for next sync word
	beq.s		nogapfound

nosyncfound
	cmpi.w		#$4489,(a1)+		;search for next sync
	bne.s		nosyncfound

nogapfound
	cmpi.w		#$4489,(a1)		;skip all remaining syncs
	bne.s		skippedsyncs
	addq.w		#2,a1
	bra.s		nogapfound

skippedsyncs
	move.l		(a1)+,d0		;decode Info Blk.
	move.l		(a1)+,d1		;See article.
	and.l		d3,d0
	and.l		d3,d1
	add.l		d0,d0
	or.l		d1,d0

	move.l		d0,d1
	andi.l		#$FF000000,d1		;check if header is alright
	cmpi.l		#$FF000000,d1
	bne.s		headererror		;if not -> ERROR

	andi.l		#$0000FF00,d0		;get sector number
	add.l		d0,d0

	move.l		a0,a2
	adda.l		d0,a2			;add to get the right pos.

	adda.w		#$28,a1			;skip the rest

	move.l		(a1)+,d4		;decode Data-Checksum
	move.l		(a1)+,d1		;store it in D4
	and.l		d3,d4
	and.l		d3,d1
	add.l		d4,d4
	or.l		d1,d4

	moveq		#127,d6			;decode 2*128 longwords
	clr.l		d2

decodenext					;decode data block
	move.l		512(a1),d1
	move.l		(a1)+,d0

	eor.l		d0,d2			;calculate Checksum
	eor.l		d1,d2

	and.l		d3,d0			;decode longword
	and.l		d3,d1
	add.l		d0,d0
	or.l		d1,d0

	move.l		d0,(a2)+		;store longword in buffer

	dbf		d6,decodenext		;decode next longword

	and.l		d3,d2			;skip illegal bits
	cmp.l		d4,d2			;compare checksums
	bne.s		chksumerror		;in not equal -> error

	adda.w		#$0200,a1		;add for next block

	dbf		d5,decodenextblock

	adda.w		#$1600,a0		;update position in buffer

	clr.l		d0			;Return code : OK
	rts

chksumerror
	move.w		#$00F0,$dff180		;Return code : ERROR
	moveq		#-1,d0	
	rts

headererror
	move.w		#$0F00,$dff180		;Return code : ERROR
	moveq		#-1,d0
	rts

;--------------------------------------------------------------------
	section	buffers,bss_c

buffer			ds.b	10*22*512	;decode buffer
trackbuffer		ds.b	20000		;track buffer

;--------------------------------------------------------------------

