[comp.sys.mac.programmer] Hack to assist tracking down SysError 11

mandel@vax.anes.tulane.edu (Jeff E Mandel MD MS) (05/10/91)

While searching for something else, I came across this routine that I vaguely
recall someone expressing interest in such a problem. I figure the world needs
more hacks, so I submit it for your amusement.

The problem this hack attacks is that of the dread system error 11. By calling
the PROC startup, the Exception Vector table (see MC68020 User's Manual, 2nd
Edition, section 6.2 (what, you don't keep yours handy?)). I have patched the
ones related to the 68881, and fixed it so it breaks to MacsBug and does an IP
on the address that caused the error. Note that as long as you are running
under MultiFinder, you don't have to worry about restoring the vector table
when your program exits (especially if it crashes and burns).

Just in case you were wondering, "Jeff, why would a busy guy like you be
playing around with low level stuff like this?", it goes back to when I was
writing a real-time digital filter for processing some waveforms coming in off
an AD board, and was doing some floating point math at interrupt level. The
problem is that if the instruction that the CPU was working on when you get the
interrupt is a call to the 68881, and you don't save its internal state during
the interrupt routine, then restore it, it is bad (in the true Ghostbusters
sense of the word). For those of you wondering, "Jeff, why would anyone write
real-time DSP stuff in assembly language with in-line calls to the 68881?", I
just got Meridian's Open Ada, and hopefully won't need to do stuff like this
anymore.

--------------------------------------------------

	TITLE		'Error handler'
	BLANKS		ON
	CASE		ON
	String		Pascal
	Machine		MC68020
	MC68881

	PRINT	OFF
	INCLUDE	'Traps.a'
	INCLUDE	'ToolEqu.a'
	INCLUDE	'QuickEqu.a'
	INCLUDE	'SysEqu.a'
	INCLUDE	'SysErr.a'
	LOAD	'FlowCtlMacs.d'
	PRINT	ON

*  The stackframe for the routine.

StackFrame	RECORD	0
StatusReg	DS.W	1
ProgCounter	DS.L	1
VectorOffset	DS.W	1
Instruction	DS.L	1
InternalRegs	DS.L	2
		ENDR

	
disaster	PROC	Export
	
	With	StackFrame
	
	MOVE.L	Instruction(SP),A0
	MOVE.W	VectorOffset(SP),D1
	AND.W	#$FFF,D1
	Switch# D1
	Case#.S	$0C0
		PEA		#'Branch Error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$0C4
		PEA		#'Inexact result Error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$0C8
		PEA		#'Divide by zero Error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$0CC
		PEA		#'Underflow Error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$0D0
		PEA		#'Operand Error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$0D4
		PEA		#'Overflow Error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$0D8
		PEA		#'Not a number;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$034
		PEA		#'Coprocessor protocol error;IP A0'
		_DebugStr
		Leave#.S
	Case#.S	$038
		PEA		#'Format error;IP A0'
		_DebugStr
		Leave#.S
	Default#.S
		PEA		#'No Idea;IP A0'
		_DebugStr
	EndS#

	RTE
	End

StartUp	PROC	EXPORT
	MOVE.L	A0,-(SP)
	
*  Patch the low memory interrupt dispatch table so 68881 coprocessor
*  exceptions give us meaningful messages

	LEA		disaster,A0
	MOVE.L	A0,$0C0
	MOVE.L	A0,$0C4
	MOVE.L	A0,$0C8
	MOVE.L	A0,$0CC
	MOVE.L	A0,$0D0
	MOVE.L	A0,$0D4
	MOVE.L	A0,$0D8
	MOVE.L	A0,$034
	MOVE.L	A0,$038

	MOVE.L	(SP)+,A0
	RTS
	
	ENDP
	END

Jeff E Mandel MD MS
Asst. Professor of Anesthesiology
Tulane University School of Medicine
New Orleans, LA
mandel@vax.anes.tulane.edu