	.IF	LIST4
	.LIST
	.PAGE
	.ELSE
	.NOLIST
	.ENDC
;****************************************************************************
;
;	Exception Trap Handler:
;
;	File:	SAGE.PROM4.TEXT
;	Date:	23-Mar-83
;	Issue:	3
;
;	COPYRIGHT (c) 1982, 1983 SAGE Computer Technology
;	All Rights Reserved
;
;****************************************************************************
;
;	Release History:
;
;	1	13-Jun-82  Initial release.
;	2	10-Dec-82  Revised Trap identification to not rely on stack.
;			   Allow time for terminal output before processing
;			   exception error.
;	3	23-Mar-83  Initial SAGE IV release.
;
;****************************************************************************



.PAGE
;****************************************************************************
;
;	Default exception handler entry points
;
;	The following eleven exception handler entry points call the default
;	trap handler and will never return.  The handler uses the return
;	address to determine which entry point was actually entered.
;
;****************************************************************************

TRAP1	MOVE.L	D0,REGTRAP	;Bus error
	MOVEQ	#2,D0
	BRA.S	DTRAPH
	
TRAP2	MOVE.L	D0,REGTRAP	;Address error
	MOVEQ	#4,D0
	BRA.S	DTRAPH
	
TRAP3	MOVE.L	D0,REGTRAP	;Illegal instruction errors
	MOVEQ	#6,D0
	BRA.S	DTRAPH
	
TRAP4	MOVE.L	D0,REGTRAP	;Zero divide, CHK, and TRAPV errors
	MOVEQ	#8,D0
	BRA.S	DTRAPH
	
TRAP5	MOVE.L	D0,REGTRAP	;Privilege violation error
	MOVEQ	#10.,D0
	BRA.S	DTRAPH
	
TRAP6	MOVE.L	D0,REGTRAP	;Exceptions reserved by 68000 manufacturer
	MOVEQ	#12.,D0
	BRA.S	DTRAPH
	
TRAP7	MOVE.L	D0,REGTRAP	;Unassigned TRAP exceptions
	MOVEQ	#14.,D0
	BRA.S	DTRAPH
	
TRAP8	MOVE.L	D0,REGTRAP	;User interrupts (including autovectors)
	MOVEQ	#16.,D0
	BRA.S	DTRAPH
	
TRAP9	MOVE.L	D0,REGTRAP	;Parity error
	MOVEQ	#18.,D0
	BRA.S	DTRAPH
	
TRAP10	MOVE.L	D0,REGTRAP	;Debugger TRAP (#15)
	MOVEQ	#20.,D0
	BRA.S	DTRAPH
	
TRAP11	MOVE.L	D0,REGTRAP	;Trace exception (not entered directly)
	MOVEQ	#22.,D0
;	Fall into DTRAPH

;	Default trap handler
DTRAPH	MOVEM.L D1-D7/A0-A6,REGTRAP+4 ;Save all the registers
	MOVE	USP,A0		;Save user stack pointer
	MOVE.L	A0,REGUS
	MOVE.L	D0,D5
	MOVEQ	#22.,D0		;Check for legal entry point limit
	CMP.L	D0,D5
	BHI.S	$40		;Bad entry point!
				;Possibly DTRAPH entered directly or
				;  system stack pointer was not to RAM.
	CMP.B	#4,D5
	BHI.S	$10		;Entry was not Bus error or Address error
	MOVE.W	(A7)+,D4	;Save extra Bus/Address error data
	MOVE.L	(A7)+,D3
	MOVE.W	(A7)+,D2
$10	MOVE.W	(A7)+,REGSR	;Save Status Register contents
	MOVE.L	(A7)+,REGPC	;Save Program Counter contents
	MOVE.L	A7,REGA7	;Save original system stack pointer
	LEA	SYSSTACK,A7	;Set up new system stack in debugger area

$20	CMPI.B	#20.,D5
	BEQ	BRKTRAP		;Debugger trap detected
	BGT	TRCTRAP		;Trace trap detected
	CMPI.B	#18.,D5		;Parity error?
	BNE.S	$35		;No
	MOVE.B	#1,CPARITY	;Resets parity
	MOVE.B	#0,CPARITY	;Reenables parity
	TST.B	SAGE4		;Is this a SAGE 4
	BEQ.S	$35		;Not a SAGE 4
	BSET	#2,WP_2B	;Resets parity
	BCLR	#2,WP_2B	;Reenables parity
	
$35	MOVEQ	#-1,D0
$37	ROR.B	#8,D0		;Just for delay
	DBF	D0,$37		;Delay for last characters to get out
	MOVE	#2700H,SR	;Disable interrupts
	MOVE.B	#38H,F8255-2	;Turn off floppy
	BCLR	#TRACE,BMODE	;Disable debuger tracing
	CLR.B	BUGBIOS		;Cancel BIOS I/O
	LEA	$50,A1		;Set up return address for TERMINIT
	BRA	TERMINIT	;Initialize terminal

;	Illegal trap handler entry detected
$40	CLR.L	D5		;Zero type indicates illegal entry
	BRA	$20		;Bypass handling exception information

;	TERMINIT will return here
$50	LEA	MSG1,A4		;Output <CR> EXCEPTION:	 '
	LEA	$70,A5		;Completion address
$60	BRA	NRTEXT

$70	LEA	EMSGTBL,A4	;Output error type
	ADDA.W	0(A4,D5.W),A4
	LEA	$80,A5		;Completion address
	BRA	$60

$80	LEA	EMSG,A4		;Output 'Error at'
	LEA	$90,A5		;Completion address
	BRA	$60

$90	MOVE.L	REGPC,D0	;Output Program Counter value
	BSR	PRNTADDR	;Print physical & relative
	BSR	TERMCRLF
	CMPI.B	#4,D5		;Check for extra information
	BHI	DEBUG		;Not Bus or Address error
	LEA	BEMSG1,A0	;Printout "Function: "
	BSR	TERMTEXT
	MOVE.W	D4,D0		;Printout function code
	BSR	TERMHEXW
	LEA	BEMSG2,A0	;Printout "  Access: "
	BSR	TERMTEXT
	MOVE.L	D3,D0		;Printout access address
	BSR	TERMHEXL
	LEA	BEMSG3,A0	;Printout "  Inst: "
	BSR	TERMTEXT
	MOVE.W	D2,D0
	BSR	TERMHEXW
	BSR	TERMCRLF
	BRA	DEBUG		;Now enter debugger


.PAGE
;****************************************************************************
;
;	Set up default TRAP vectors
;
;****************************************************************************

SETTRAP LEA	BE_TRAP,A0	;Base of trap vector area
	LEA	TRAPTBL,A1	;Base of trap initialization table
	LEA	PROM,A2		;Base of PROM area
$10	CLR.L	D0
	MOVE.W	(A1)+,D0	;Get an initialization table entry
	BEQ.S	$40		;Done with table
	BMI.S	$20		;Was replicate count
	ADD.L	A2,D0		;Form PROM routine address
	MOVE.L	D0,(A0)+	;Store routine address in vector area
	BRA.S	$10		;Try next table entry
;
;	Replicate following table entry
$20	CLR.W	D1		;Make sure high byte of word is zero
	MOVE.B	D0,D1		;Set up replicate counter (gets rid of sign)
	MOVE.W	(A1)+,D0	;Get entry from initialization table
	ADD.L	A2,D0		;Form PROM routine address
$30	MOVE.L	D0,(A0)+	;Store routine address in vector area
	DBF	D1,$30		;Loop back until done with replicate count
	BRA.S	$10		;Try next table entry

$40	RTS

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