#
# Copyright (c) 1995 Berkeley Software Design, Inc.  All rights reserved.
# The Berkeley Software Design Inc. software License Agreement specifies
# the terms and conditions for redistribution.
#
#	BSDI $Id: ncr_runop,v 2.2 1995/07/18 22:53:33 cp Exp $

#
#	Don't reset location counter until after all code is done.
#	There is no filler when the location counter is moved.



#
#	runop interrrupts run from 0x1000 up
#	selector interrupts run from 0x999 down
#
	
	i_i2long	= 0x1000
	i_o2long	= 0x1001
	i_phase		= 0x1002
	i_emsgtossed	= 0x1003
	i_msgtossed	= 0x1004


	slot_done	= 0x04		#must not step on selector bits
	slot_newwork	= 0x02		#must match entry in selector



start:	jmp newwork;
	jmp reselectit;


newwork:

	memmv 4 physaddr dsa;
	memmv 4 zero save_partial;
	memmv 4 inlist	save_inlist;
	memmv 4 outlist	save_outlist;
	select atn ti t_select r resel; 

phaselock:
	jmp wait msg_in r p_msgin;
	jmp data_in r input_data;
	jmp data_out r output_data;
	jmp msg_out r p_msgout;
	jmp status r p_status;
	jmp cmd p_cmd;
	int i_phase;

resel:
	#
	# If we get here we have to turn on the newwork flag so
	# the selector will start this target again. We never talked to
	# the drive so we won't get reselected
	#
	memmv 4 slotaddr slotaddr3;
	memmv 4 slotaddr slotaddr4;
slotaddr3 = . + 4
	memmv 4 0 scratcha;
	or slot_newwork scratcha0;
slotaddr4 = . + 8
	memmv 4 scratcha 0;
findtarget = . + 4
	jmp 0;				# patched by device driver

p_msgout:
	memmv 4 ideflag scratcha;
	mv scratcha0 sfbr;
	or sist0 0 sfbr;
	jmp data 0x1 mask 0x1 perror;	
	blkmv msg_out ti t_id_msg;
msg_ostop:
	jmp phaselock;

perror:
	memmv 4 zero ideflag;
	blkmv msg_out 1 idemsg;		# initiator detected error 
	jmp phaselock;

p_cmd:
	blkmv cmd ti t_cmd;
	jmp phaselock;

p_status:
	blkmv status ti t_status; 
	jmp phaselock;

p_msgin:
	blkmv msg_in 1 rcvmsg;
	jmp data 0x00 done;		# complete message is zero
	jmp data 0x01 extmsg;
	jmp data 0x02 savepointers;
	jmp data 0x03 restorepointers;
	jmp data 0x04 msg_disconnect;
	jmp data 0x07 msg_reject;
	jmp data 0x80 mask 0x80 msg_id;
	clear ack;
	int i_msgtossed;

msg_reject:
	clear atn;
	clear ack;
	jmp phaselock;

done:
	ld 0 scntl2;
	clear ack;
	disconnect;
	memmv 4 slotaddr slotaddr1;
slotaddr = . + 4
	memmv 4 0 scratcha;
	or slot_done scratcha0;		# turn on done bit 
slotaddr1 = . + 8
	memmv 4 scratcha 0;
	int fly 0;
toselector:
selector = . + 4
	jmp 0;				# go back to selector code
					# patched by device driver

msg_id:
	clear ack;
	jmp phaselock;

savepointers:
	clear ack;
	memmv 8 t_partial save_t_partial;
	memmv 4 partial save_partial;
	memmv 4 inlist	save_inlist;
	memmv 4 outlist	save_outlist;
	jmp phaselock;

restorepointers:
	clear ack;

restorepointers1:

	memmv 8 save_t_partial t_partial;
	memmv 4 save_partial partial;
	memmv 4 save_inlist inlist;
	memmv 4	save_outlist outlist;
	jmp phaselock;

msg_disconnect:
	ld 0 scntl2;
	clear ack;
	disconnect;
	jmp toselector;

extmsg:
	clear ack;
	blkmv msg_in 1 rcvmsg;
	jmp not data 3 tossmsg;		#only extended we care about is 3 long
	clear ack;
	blkmv msg_in 1 rcvmsg;		
	jmp not data 1 tossmsg;		#not sync transfer
	clear ack;
	blkmv msg_in 2 syncmsgin;	#transfer in the sync data;
	clear ack;
	jmp phaselock;

tossmsg:
	clear ack;
	jmp not wait msg_in tossmsg1;
	blkmv msg_in 1 rcvmsg;
	jmp tossmsg;
tossmsg1:
	int i_emsgtossed;


output_data:
	memmv 4 partial scratcha;
	add 1 scratcha0;
	jmp not carry output_data1;
	memmv 8 t_partial t_data; 
	memmv 4 zero partial;
	jmp r  output_data2;
output_data1:
outlist = . + 4
	memmv 8 0 t_data;		
	memmv 4 t_data scratcha;
	add 1 scratcha3;		# ff stopper in unused byte
	int carry i_o2long;
output_data2:
	blkmv data_out ti t_data;
ostop:
	memmv 4 outlist scratcha;
	add 8 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha outlist;
	jmp wait data_out r output_data1;
	jmp phaselock;

otoss:
	set atn;
otoss1:
	jmp not wait data_out phaselock;
	blkmv data_out 1 trash;
	jmp otoss1;
	
	


input_data:
	memmv 4 partial scratcha;
	add 1 scratcha0;
	jmp not carry input_data1;
	memmv 8 t_partial t_data; 
	memmv 4 zero partial;
	jmp r  input_data2;
input_data1:
inlist = . + 4
	memmv 8 0 t_data;	
	memmv 4 t_data scratcha;
	add 1 scratcha3;		# ff stopper in unused byte
	int carry i_i2long;
input_data2:
	blkmv data_in ti t_data;
istop:
	memmv 4 inlist scratcha;
	add 8 scratcha0;
	add carry 0 scratcha1;
	memmv 4 scratcha inlist;
	jmp wait data_in r input_data1;
	jmp phaselock;

itoss:
	set atn;
itoss1:
	jmp not wait data_in phaselock;
	blkmv data_in 1 trash;
	jmp itoss1;

reselectit:

	memmv 4 physaddr dsa;
	memmv 1 t_select + 3 scntl3;
	memmv 1 t_select + 1 sxfer;
	jmp restorepointers1;

endcode:

t_id_msg:	. = . + 8
t_cmd:		. = . + 8
t_status:	. = . + 8
t_extmsg:	. = . + 8
t_data:		. = . + 8
t_select:	. = . + 4

t_partial:	. = . + 8
partial:	. = . + 4

save_t_partial:	. = . + 8
save_partial:	. = . + 8
save_inlist:	. = . + 4
save_outlist:	. = . + 4
physaddr:	. = . + 4
msgctl:		. = . + 4
ideflag:	. = . + 4
rcvmsg:		. = . + 16
syncmsgin:	. = . + 4
idemsg:		. = . + 4
trash:		. = . + 8

zero:		. = . + 4		# used as a zero source in memmv

nsg = 20
sg:		. = . + (nsg * 8)
eot:
