	.IF	LISTF
	.LIST
	.PAGE
	.ELSE
	.NOLIST
	.ENDC
;****************************************************************************
;
;	Winchester Disk Driver - part 2
;
;	File:	SAGE.PROMF.TEXT
;	Date:	28-Jul-83
;	Issue:	1A
;
;
;	COPYRIGHT (c) 1983 SAGE Computer Technology
;	All Rights Reserved
;
;****************************************************************************
;
;	History:
;
;	1     23-Mar-83 Initial SAGE IV release.
;	1A    28-Jul-83 Improved seek error recovery
;
;****************************************************************************


;****************************************************************************
;
;	Move data from controller buffer into memory
;
;****************************************************************************

WDMOVEI
	LEA	WP_DATA,A1	;Set up controller port address
	MOVEA.L WT_MEMA,A2	;Set up buffer address
	BSET	#7,WP_2B	;Disable read latch output buffers
	MOVE.B	#0FH,WP_2CB	;Set up CPU access to controller buffer
	MOVE.B	#0DH,WP_2CB	;Strobe to clear address counter
	MOVE.B	#0CH,WP_2CB

;	Handle logical track number
	TST.B	(A1)		;Throw away two bytes
	TST.B	(A1)		; Why, we don't know
	
	MOVE.B	#3,WP_PGCC	;Reset PGC
	MOVE.B	#41H,WP_PGCM	;Set into read mode
	
	MOVE.B	(A1),D0		;High byte of track number
	LSL.W	#8,D0		;Left justify in word
	MOVE.B	(A1),D0		;Low byte of track number
	MOVE.W	D0,WS_RTRK	;Save track number

;	Handle ignoring leading information
	MOVE.W	WT_LEAD,D2	;Check if any leading data
	BEQ.S	$20		;No leading data
	SUBQ.W	#1,D2		;Adjust for DB instruction
$10	TST.B	(A1)		;Advance counter
	DBF	D2,$10

;	Handle main data
$20	MOVE.W	WT_NUMB,D2	;Get byte count
	SUBQ.W	#1,D2		;Adjust for DB instruction
$30	MOVE.B	(A1),(A2)+	;Get a byte of data
	DBF	D2,$30
	
;	Handle ignoring trailing information
	MOVE.W	WT_TRAIL,D2	;Check if any trailing data
	BEQ.S	$50		;No trailing data
	SUBQ.W	#1,D2		;Adjust for DB instruction
$40	TST.B	(A1)		;Access to generate CRC
	DBF	D2,$40

;	Now verify the CRC
$50	TST.B	(A1)		;Access CRC bytes
	TST.B	(A1)
	BTST	#0,WP_PGCC
	BNE.S	$100		;Failed CRC

;	Verify the logical track number
	MOVE.W	WS_RTRK,D0
	CMP.W	WT_LTN,D0
	BNE.S	$110		;Failed logical track number
	CLR.B	WS_ERR		;Indicate success
	RTS
	
;	CRC error
$100	MOVE.B	#-8,WS_ERR	;CRC Error
	RTS
	
;	Logical track number error
$110	MOVEQ	#0,D2
	MOVE.B	WI_HEADS,D2
	DIVU	D2,D0
	MOVE.W	D0,WS_CCYL	;Store actual cylinder
	MOVE.B	#-12.,WS_ERR	 ;Wrong cylinder
	RTS


.PAGE
;****************************************************************************
;
;	Move data from disk to controller buffer
;
;****************************************************************************

WDREAD
	MOVE.B	#2,WP_2CB	;Set the R/W mux to read
	MOVE.B	#8,WP_2CB	;Disable write enable
	BSET	#6,WP_2B	;Disable the write data buffer
	BCLR	#7,WP_2B	;Enable read latch & shift register output
	
;	Set up buffer address counter
	MOVE.B	#0DH,WP_2CB	;Strobe to clear address counter
	MOVE.B	#0CH,WP_2CB
	MOVE.B	#0EH,WP_2CB	;Set up disk mode for controller buffer
	
;	Load 8253 counters
	MOVE.B	#30H,WP_8253+6	;Set counter 0 into mode 0
	MOVE.B	#70H,WP_8253+6	;Set counter 1 into mode 0
	MOVE.W	WI_LOWC,D0
	MOVE.B	D0,WP_8253	;Low count on counter 0
	LSR.W	#8,D0
	MOVE.B	D0,WP_8253	;High count on counter 0
	MOVE.W	WI_HIGHC,D0
	MOVE.B	D0,WP_8253+2	;Low count on counter 1
	LSR.W	#8,D0
	MOVE.B	D0,WP_8253+2	;High count on counter 1
	MOVE.B	#0BH,WP_2CB	;Strobe the Enable Index signal
	MOVE.B	#0AH,WP_2CB
	BSET	#1,WP_2B	;Enable winchester completion signal
	
	MOVEQ	#-1,D0
$30	BTST	#WB_INDEX,WP_1A ;Check for transfer completion
	DBNE	D0,$30
	BNE.S	$40		;No timeout
	MOVE.B	#-6,WS_ERR	;Set up error code
	
$40	BCLR	#1,WP_2B	;Disable winchester interrupt
	MOVE.B	#8,WP_2CB	;Disable writing
	BSET	#6,WP_2B	;Disable write buffer
	MOVE.B	#5,WP_2CB	;Disable ram outputs
	BSET	#7,WP_2B	;Disable output buffers of read latch
	MOVE.B	#0FH,WP_2CB	;Set up CPU mode for controller buffer
	RTS


                           