#
# Copyright (c) 1995, 1996 Berkeley Software Design, Inc.  All rights reserved.
# The Berkeley Software Design Inc. software License Agreement specifies
# the terms and conditions for redistribution.
#
#	BSDI ncr_selector,v 1.5 2000/06/28 00:27:32 cp Exp
# 
	i_ssid_invalid	= 0x1000 - 1
	i_bogusreselect	= 0x1000 - 2
	i_nocode1	= 0x1000 - 3	
	i_check		= 0x1000 - 4

	slot_stopper	= 0x80
	slot_valid	= 0x01
	slot_newwork	= 0x02
	slot_done	= 0x04

start:
	memmv 4 physaddr dsa;
	memmv 4 zero scratcha;
	add 1 scratcha0;
	memmv 4 scratcha one;
	jmp newwork;

wait4work:
ledpatch1:
	jmp not 0; 		# actually a nop; patched to "or 0x1 gpreg"
	reselect newwork;
ledpatch2:
	jmp not 0;		# actually a nop; patched to "and 0xfe gpreg"
findtarget:
	mv ssid sfbr;
	jmp data 0x80 mask 0x80 si_valid;
	int i_ssid_invalid;	#single initiator
si_restart:
	mv scratcha0 sfbr;
si_valid:
	mv sfbr scratcha0;
	shiftl scratcha0;
	shiftl scratcha0;
	and 0x3c scratcha0;	# target field shift left 2 
	mv scratcha0 sfbr;
	mv sfbr scratcha1;
	memmv 4 scratcha savetid;

#
#	First reset sync values.
#	We do this because we have to complete this before
#	finishing the messge in phase or we may loose sync
#	data pushed at us before we have fifo set up

	memmv 1 scratcha1 addvalue1;
	memmv 4 reselect_srtes scratcha;
addvalue1 = . + 1
	add 0 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha fetchit1;
fetchit1 = . + 4
	memmv 4 0 scratcha;
	memmv 1 scratcha3 scntl3;
	memmv 1 scratcha1 sxfer;

phaselock:

	jmp not wait msg_in r notag;
	mv sbdl sfbr;
	jmp data 0x20 input_tag;
	jmp not data 0x80 mask 0x80 notag;

	blkmv msg_in 1 rcvmsg; 
	clear ack;
	jmp phaselock;

input_tag:
	blkmv msg_in 2 rcvmsg;
	clear ack;
	memmv 4 rcvmsg scratcha;	#scratcha1 holds tag number
	jmp doadd;

notag:
# 	and 0xef dcntl;


	memmv 4 savetid scratcha;
doadd:
	memmv 1 scratcha1 addvalue;
	memmv 4 reselect_ops scratcha;

addvalue = . + 1
	add 0 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha fetchit;
fetchit = . + 4
	memmv 4 0 scratcha;

selectit:
	mv scratcha0 sfbr;
	int not data 0x1 mask 0x1 i_bogusreselect;
	and 0xfe scratcha0;
	memmv 4 scratcha call_reselect;

	ld 1 scratcha0;
call_reselect = . + 4
	call 0;
	memmv 4 physaddr dsa;
	memmv 4 curop curop1;
curop1 = . + 8
	memmv 4 zero 0;
	mv scratchb0 sfbr;
	int data 0x0 mask 0x3 i_check;		#complete with check condition
	jmp data 0x2 mask 0x3 done;
	jmp data 0x3 mask 0x3 newwork;	# disconnected 
	int i_nocode1;

newwork:			#see if we can find something to do
ledpatch3:
	jmp not 0;		# actually a nop; patched to "and 0xfe gpreg"
	mv ctest2 sfbr;		# XXX find out why this is and add comment
startq = . + 4
	memmv 4 0 scratcha;	# q_flags to scratcha
	mv scratcha0 sfbr;
	jmp data 0 wait4work;

	memmv 4 startq scratcha;
	add 4 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha startq3;	# move address of q_runop
startq3 = . + 4
	memmv 4 0 calladdr;		# address of q_runop to calladdr


	ld 0 scratcha0;
calladdr = . + 4
	call 0;

	memmv 4 physaddr dsa;
curop = . + 8
	memmv 4 zero 0;

	mv scratchb0 sfbr;

	# return of
	# 1) didn't select
	# 2) completed
	# 3) started and disconnected
	# 0) completed with check
	jmp data 0x1 mask 0x3 findtarget;	#we didn't select
started:

	memmv 4 startq startq1;
startq1 = . + 8
	memmv 4 zero 0;		# zero to q_flags
	memmv 4 startq scratcha;
	add 8 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha startq2;	# pointer to q_pnext
startq2 = . + 4
	memmv 4 0 startq;		# *q_pnext into startq

	mv scratchb0 sfbr;
	jmp data 0x3 mask 0x3 newwork;		#started and disconnected
	int data 0x0 mask 0x3 i_check;		#complete with check condition
done:
	and 0xfc scratchb0;		# remove flag bits from address 
	memmv 4 doneq scratcha;
	add 4 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha doneq1;	# address of q_runop;
doneq1 = . + 8
	memmv 4 scratchb 0;		# update q_runop;
doneq = . + 8
	memmv 4 one 0;			# update q_flags;
	int fly 0;
	memmv 4 doneq scratcha;
	add 8 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha doneq2;	# pointer to next slot
doneq2 = . + 4
	memmv 4 0 doneq;
	jmp newwork;

	

reselect_ops:	. = . + 4
reselect_srtes:	. = . + 4
physaddr: 	. = . + 4
zero:		. = . + 4
one:		. = . + 4
savetid:	. = . + 4
rcvmsg:		. = . + 4

eot:

