*=============================================================================
*
*
*			        Err4
*
*
*		   Copyright 1987  All Rights Reserved
*
*
*			  Christian Johnsen
*
*
*=============================================================================


*=============================================================================
*
*				Declarations
*
*=============================================================================

*=============================================================================
*
*		INCLUDE		"exec/types.i"
*		INCLUDE		"exec/tasks.i"
*		INCLUDE		"exec/libraries.i"
*		INCLUDE		"intuition/intuition.i"
*		INCLUDE		"graphics/gfxbase.i"
*		INCLUDE		"exec/nodes.i"
*		INCLUDE		"exec/lists.i"
*		INCLUDE		"exec/ports.i"
*		INCLUDE		"libraries/dos.i"
*		INCLUDE		"libraries/dosextens.i"
*		INCLUDE		"workbench/startup.i"
*
*=============================================================================


		INCLUDE	"TrapEQU"		file of above includes EQU'd

		IDNT	"Err4"			declare name

		XDEF	_ErrorHandler		reentry after error 
		XDEF	_Main		        reference for startup code

		XREF	_GOMF			entry for initialization
		XREF	_GOMFEnding		entry for termination
		XREF	_ProgramCounter		contains user program counter
		XREF	_LibraryErr		contains pointer to string
		XREF	_GeneralErr		contains pointer to string
		XREF	_SpecificErr		contains pointer to string
		XREF	_WHAP			BYTE BOOLEAN flag for selection

		XREF	_AbsExecBase		amiga.lib
		XREF	_romstart		amiga.lib

		XREF	_LVOClearScreen		grahics.library
		XREF	_LVOCloseWindow		intuition.library
		XREF	_LVOCloseLibrary	exec.library
		XREF	_LVODrawBorder		intuition.library
		XREF	_LVOGetMsg		exec.library
		XREF	_LVOMove		exec.library
		XREF	_LVOOpen		dos.library
		XREF	_LVOOpenLibrary		exec.library
		XREF	_LVOOpenWindow		intuition.library
		XREF	_LVORefreshGadgets	intuition.library
		XREF	_LVOReplyMsg		exec.library
		XREF	_LVOSetBPen		grahics.library
		XREF	_LVOPrintIText		intuition.library
		XREF	_LVOWindowToFront	intuition.library
		XREF	_LVOWaitPort		exec.library

Program		EQU	$000000			base address
FlagUp		EQU	$FF			on or selected status
FlagDown	EQU	$00			off or unselected status


*=============================================================================
*
*		       Program To Link With GOMF1.0
*
*=============================================================================


	RORG	Program			relocatable position origin

_Main

*-----------------------------------------------------------------------------
Err4
*-----------------------------------------------------------------------------

	MOVEM.L	D1-D7/A0-A6,-(A7)	stack registers to preserve
	MOVEA.L	_AbsExecBase,A6		get pointer to Exec base

OpenIntuition

	LEA.L	IntuitName,A1		set library name pointer
	MOVEQ	#0,D0			expect any version
	JSR	_LVOOpenLibrary(A6)	open Exec library
	MOVE.L	D0,IntuitLib		save library base pointer
	BEQ	NormalEnding		if not end clean else...

OpenWindow

	MOVEA.L	IntuitLib,A6		set pointer to library node
	LEA.L	TrapCreatorWindowStructure,A0 set new window pointer
	MOVEQ	#0,D0			expect pointer return
	JSR	_LVOOpenWindow(A6)	open the window
	MOVE.L	D0,WindowPnt		save pointer to new window
	BEQ	NormalEnding		no window - so exit else...
	MOVEA.L	D0,A0			set new window pointer
	MOVE.L	wd_RPort(A0),RastPnt	save rastport pointer
	MOVE.L	wd_UserPort(A0),UserPnt save IDCMP port pointer

TrapErrMessage

	MOVEA.L	IntuitLib,A6		set pointer to library base
	MOVEQ	#0,D0			x rastport offset
	MOVEQ	#0,D1			y rastport offset
	MOVE.L	RastPnt,A0		retrieve rastport pointer
	LEA.L	TrapIntuiTextList,A1	copy intuitext structure pointer
	JSR	_LVOPrintIText(A6)	print our title in window

SetupGOMF

	MOVEQ	#0,D0			expect a return code
	JSR	_GOMF			initialize GOMF1.0
	TST.L	D0			was there an error?
	BEQ	TrapErrAwaitMessage	if not continue else...
	BRA	CloseTheWindow		quit clean

_ErrorHandler

	TST.B	PrintFlag		should we print the normal message
	BEQ	TrapErrAwaitMessage	if so do nothing else...
	BSR	PrintIt			print message from GOMF

TrapErrAwaitMessage

	MOVE.B	#FlagUp,PrintFlag	set the print flag
	MOVEA.L	_AbsExecBase,A6		set pointer to library base
	MOVEQ	#0,D0			expect a message pointer
	MOVEA.L	UserPnt,A0		copy message port pointer
	JSR	_LVOWaitPort(A6)	get a message
	MOVEA.L	_AbsExecBase,A6		set pointer to library base
	MOVEQ	#0,D0			expect a message pointer
	MOVEA.L	UserPnt,A0		copy message port pointer
	JSR	_LVOGetMsg(A6)		get a message
	TST.L	D0			did we get one?
	BEQ	TrapErrAwaitMessage	no so skip back else...
	MOVEA.L	D0,A1			copy message pointer
	MOVE.L	D0,MsgPnt		save message node pointer
	MOVE.L	im_Class(A1),D1		fetch intuition message class

CloseWindowMessage

	CMPI.L	#CLOSEWINDOW,D1		is it a close gadget message?
	BEQ	DoClose			if so quit else...

GadgetUpMessage

	CMPI.L	#GADGETUP,D1		is it a gadget up message?
	BNE	TrapWhatElse		if so quit else...

TrapGadgets

	MOVEA.L	im_IAddress(A1),A0	get gadget structure address
	MOVE.W	gg_GadgetID(A0),D0	get gadget id number
	CMPI.W	#1,D0			address error gadget?
	BEQ	DoTrap3			if so perform error else...

IllInstructGadget

	CMPI.W	#2,D0			illegal instruction gadget?
	BEQ	DoTrap4			if so perform error else...

Div0Gadget

	CMPI.W	#3,D0			division by zero gadget?
	BEQ	DoTrap5			if so perform error else...

CHKInstructGadget

	CMPI.W	#4,D0			CHK instruction gadget?
	BEQ	DoTrap6			if so perform error else...

OverflowGadget

	CMPI.W	#5,D0			overflow error gadget?
	BEQ	DoTrap7			if so perform error else...

PiviledgeGadget

	CMPI.W	#6,D0			privelege violation gadget?
	BEQ	DoTrap8			if so perform error else...

Opcode1010Gadget

	CMPI.W	#7,D0			opcode 1010 gadget?
	BEQ	DoTrapA			if so perform error else...

Opcode1111Gadget

	CMPI.W	#8,D0			opcode 1111 gadget?
	BEQ	DoTrapB			if so perform error else...

TrapWhatElse

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	BRA	TrapErrAwaitMessage	snooze until more news

*-----------------------------------------------------------------------------
ErrorRoutines
*-----------------------------------------------------------------------------

DoTrap3

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	MOVE.L	3,D0			cause address error

DoTrap4

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	DC.W	$4529			illegal opcode value

DoTrap5

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	MOVEQ	#0,D0			set for zero division
	MOVEQ	#0,D1			set for zero division
	DIVU	D0,D1			divide zero by zero

DoTrap6

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	MOVE.L	#1,D0			set D0 to small value
	MOVE.L	#-1,D1			set D1 to negative value
	CHK	D0,D1			cause a CHK trap

DoTrap7

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
 	MOVE	#%00000010,CCR		set overflow bit of status register
	TRAPV				cause the trap call

CreditsCopyrightNotice

AuthorName	DC.B	'  Written in assembler by Christian Johnsen '
Notice		DC.B	' COPYRIGHT © 1987 - ALL RIGHTS RESERVED  '
		CNOP	0,2

DoTrap8

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	MOVE.W	#25,SR			violate privilege

DoTrapA

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	DC.W	$A123			instruction word between A000 and AFFF

DoTrapB

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	DC.W	$F123			instruction word between F000 and FFFF

DoClose

	MOVEA.L	MsgPnt,A1		set message structure pointer
	BSR	FreeMessage		return message structure
	MOVEA.L	_AbsExecBase,A6		set pointer to library base
	MOVEQ	#0,D0			expect a message pointer
	MOVEA.L	UserPnt,A0		copy message port pointer
	JSR	_LVOGetMsg(A6)		try for another message
	TST.L	D0			did we get one?
	BNE	DoClose			if so skip back else...
	MOVEQ	#0,D0			expect a return code
	JSR	_GOMFEnding		allow GOMF1.0 to terminate
	TST.L	D0			was there an error?
	BEQ	CloseTheWindow		if not continue else...
	BRA	NormalEnding		quit but leave window open

CloseTheWindow

	TST.L	WindowPnt		window open?
	BEQ	EndIntuition		if not skip else...
	MOVEA.L	WindowPnt,A0		pass window pointer
	MOVEA.L	IntuitLib,A6		get library base pointer
	JSR	_LVOCloseWindow(A6)	if so close up window

*-----------------------------------------------------------------------------
NormalEnding
*-----------------------------------------------------------------------------

EndIntuition

	TST.L	IntuitLib		Intuition open?
	BEQ	EndUp			if not skip else...
	MOVEA.L	_AbsExecBase,A6		get exec library base pointer
	MOVEA.L	IntuitLib,A1		get library node
	JSR	_LVOCloseLibrary(A6)	close it

EndUp

	MOVEM.L	(A7)+,D1-D7/A0-A6	stack registers to preserve
	MOVEQ	#0,D1			return favourable error code
	RTS				task terminated cleanly

*-----------------------------------------------------------------------------
PrintIt
*-----------------------------------------------------------------------------

BlankOutOldMessage

	MOVE.L	#BlankLine,Line1	blank old message line
	MOVE.L	#BlankLine,Line2	blank old message line
	MOVE.L	#BlankLine,Line3	blank old message line
	MOVE.L	#BlankLine,Line4	blank old message line
	MOVEA.L	IntuitLib,A6		set pointer to library base
	MOVEQ	#0,D0			x rastport offset
	MOVEQ	#0,D1			y rastport offset
	MOVE.L	RastPnt,A0		retrieve rastport pointer
	LEA.L	TrapIntuiTextList,A1	copy intuitext structure pointer
	JSR	_LVOPrintIText(A6)	print our title in window

SetNewMessage

	LEA.L	_LibraryErr,A0		set pointer to GOMF feedback element
	MOVE.L	(A0)+,Line1		setup a line
	MOVE.L	(A0)+,Line2		setup a line
	MOVE.L	(A0)+,Line3		setup a line
	MOVE.B	(A0),D0			get the WHAP flag
	TST.B	D0			what's the state returned?
	BNE	DidWhap			if true then whapped
	MOVE.L	#NotWhapped,Line4	if not advise user
	BRA	PrintLines		print it

DidWhap

	MOVE.L	#Whapped,Line4		if not advise user

PrintLines

	LEA.L	_ProgramCounter,A0	set pointer to GOMF feedback element
	MOVE.L	(A0),D0			setup a line
	LEA.L	PCNum,A0		set pointer string segment
	BSR	PlexHexText		make PC a string and place it
	MOVE.L	#PCString,PCText	set pointer to program counter string
	MOVEA.L	IntuitLib,A6		set pointer to library base
	MOVEQ	#0,D0			x rastport offset
	MOVEQ	#0,D1			y rastport offset
	MOVE.L	RastPnt,A0		retrieve rastport pointer
	LEA.L	TrapIntuiTextList,A1	copy intuitext structure pointer
	JSR	_LVOPrintIText(A6)	print our title in window
	RTS				back for another call

*-----------------------------------------------------------------------------
FreeMessage
*-----------------------------------------------------------------------------

	MOVEA.L	_AbsExecBase,A6		get library base pointer
	JSR	_LVOReplyMsg(A6)	return the message structure
	RTS				back for more

*-----------------------------------------------------------------------------
PlexHexText
*-----------------------------------------------------------------------------

	MOVE.L	D2,-(A7)		stack register temporarily
	MOVEQ	#3,D2			set up count register
	BRA	DecodeBits		skip shifting first time through

ShiftToNextBits

	LSR.L	#8,D0			shift error number right 1 byte

DecodeBits

	MOVEQ	#0,D1			zero register
	MOVE.B	D0,D1			get first byte
	ANDI.B	#%00001111,D1		mask upper nybble
	BSR	HexToASCII		convert digit
	MOVE.B	D0,D1			get first byte
	LSR.B	#4,D1			shift upper nibble low
	ANDI.B	#%00001111,D1		mask upper nybble
	BSR	HexToASCII		convert digit
	DBEQ	D2,ShiftToNextBits	decrement counter until done
	MOVE.L	(A7)+,D2		retrieve stacked register
	RTS				hex number converted to ASCII string 

*-----------------------------------------------------------------------------
HexToASCII
*-----------------------------------------------------------------------------

	CMPI.B	#10,D1			decimal digit or hex letter?
	BLT	AddZero			if so skip next operation
	ADDI.B	#'A'-'0'-$0A,D1		offset to letters

AddZero

	ADDI.B	#'0',D1			convert it to ASCII
	MOVE.B	D1,-(A0)		hex digit added to buffer
	RTS				


*=============================================================================
*
*			Memory  Constants / Variables
*
*=============================================================================


*-----------------------------------------------------------------------------
			SECTION	TrapInitVars,DATA
*-----------------------------------------------------------------------------

		CNOP	0,4		align structure

IntuitName	DC.B	'intuition.library',0

		CNOP	0,4		align structure

DefaultInitialization

IntuitLib	DC.L	0		intuition library base pointer
RastPnt		DC.L	0		rastport pointer
WindowPnt	DC.L	0		window pointer
UserPnt		DC.L	0		user reply port pointer
MsgPnt		DC.L	0		tempory store for message pointer
PrintFlag	DC.B	FlagDown	flag for return message printing


*============================================================================
*
*			Trap Tester  Window Structures
*
*============================================================================


		CNOP	0,4		align structure

TrapCreatorWindowStructure

	DC.W	277,15,335,176
	DC.B	0,1
	DC.L	GADGETUP!CLOSEWINDOW
	DC.L	WINDOWCLOSE!WINDOWDRAG!WINDOWDEPTH!ACTIVATE
	DC.L	TrapGadget3,0
	DC.L	TrapName,0,0
	DC.W	335,176,335,176,WBENCHSCREEN

TrapName

	DC.B	' Err4 ',0
	CNOP 0,2

TrapGadgetList

TrapGadget3

	DC.L	TrapGadget1
	DC.W	183,70,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord1,0,TrapText1,0,0
	DC.W	2
	DC.L	0

TrapBord1

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2
	DC.B	5
	DC.L	TrapBordVectors1,0

TrapBordVectors1

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText1

	DC.B	3,0,RP_JAM2,0
	DC.W	8,4
	DC.L	0,TrapIText1,0

TrapIText1

	DC.B	'Illegal Inst',0
	CNOP 0,2

TrapGadget1

	DC.L	TrapGadget5
	DC.W	36,70,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord2,0,TrapText2,0,0
	DC.W	1
	DC.L	0

TrapBord2

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors2,0

TrapBordVectors2

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText2

	DC.B	3,0,RP_JAM2,0
	DC.W	5,4
	DC.L	0,TrapIText2,0

TrapIText2

	DC.B	'Address Error',0
	CNOP 0,2

TrapGadget5

	DC.L	TrapGadget6
	DC.W	37,94,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord3,0,TrapText3,0,0
	DC.W	3
	DC.L	0

TrapBord3

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors3,0

TrapBordVectors3

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText3

	DC.B	3,0,RP_JAM2,0
	DC.W	13,4
	DC.L	0,TrapIText3,0

TrapIText3

	DC.B	'Divide Zero',0
	CNOP 0,2

TrapGadget6

	DC.L	TrapGadget7
	DC.W	183,94,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord4,0,TrapText4,0,0
	DC.W	4
	DC.L	0

TrapBord4

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors4
	DC.L	0

TrapBordVectors4

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText4

	DC.B	3,0,RP_JAM2,0
	DC.W	25,4
	DC.L	0,TrapIText4,0

TrapIText4

	DC.B	'CHK Inst',0
	CNOP 0,2

TrapGadget7

	DC.L	TrapGadget8
	DC.W	37,118,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord5,0,TrapText5,0,0
	DC.W	5
	DC.L	0

TrapBord5

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors5,0

TrapBordVectors5

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText5

	DC.B	3,0,RP_JAM2,0
	DC.W	5,4
	DC.L	0,TrapIText5,0

TrapIText5

	DC.B	'Overflow Trap',0
	CNOP 0,2

TrapGadget8

	DC.L	TrapGadget9
	DC.W	184,118,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord6,0,TrapText6,0,0
	DC.W	6
	DC.L	0

TrapBord6

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors6,0

TrapBordVectors6

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText6

	DC.B	3,0,RP_JAM2,0
	DC.W	8,4
	DC.L	0,TrapIText6,0

TrapIText6

	DC.B	'Priv Violate',0
	CNOP 0,2

TrapGadget9

	DC.L	TrapGadget10
	DC.W	38,142,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord7,0,TrapText7,0,0
	DC.W	7
	DC.L	0

TrapBord7

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors7,0

TrapBordVectors7

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText7

	DC.B	3,0,RP_JAM2,0
	DC.W	14,5
	DC.L	0,TrapIText7,0

TrapIText7

	DC.B	'1010 Opcode',0
	CNOP 0,2

TrapGadget10

	DC.L	0
	DC.W	185,142,113,16,GADGHCOMP,RELVERIFY,BOOLGADGET
	DC.L	TrapBord8,0,TrapText8,0,0
	DC.W	8
	DC.L	0

TrapBord8

	DC.W	-2,-1
	DC.B	1,0,RP_JAM2,5
	DC.L	TrapBordVectors8,0

TrapBordVectors8

	DC.W	0,0,116,0,116,17,0,17,0,0

TrapText8

	DC.B	3,0,RP_JAM2,0
	DC.W	12,5
	DC.L	0,TrapIText8,0

TrapIText8

	DC.B	'1111 Opcode',0
	CNOP 0,2

TrapIntuiTextList

TrapText9

	DC.B	1,0,RP_JAM2,0
	DC.W	18,15
	DC.L	TOPAZ80
Line1	DC.L	TrapIText9,TrapText10

TrapIText9

	DC.B	'This program has been linked with    ',0
	CNOP 0,2

TrapText10

	DC.B	1,0,RP_JAM2,0
	DC.W	18,26
	DC.L	TOPAZ80
Line2	DC.L	TrapIText10,TrapText11

TrapIText10

	DC.B	'GOMF 1.0, which will trap any errors ',0
	CNOP 0,2

TrapText11

	DC.B	1,0,RP_JAM2,0
	DC.W	18,38
	DC.L	TOPAZ80
Line3	DC.L	TrapIText11,TrapText12

TrapIText11

	DC.B	'created by your gadget selections.   ',0
	CNOP 0,2

TrapText12

	DC.B	1,0,RP_JAM2,0
	DC.W	18,50
	DC.L	TOPAZ80
Line4	DC.L	TrapIText12
PCText	DC.L	0

TrapIText12

	DC.B	'Please select a gadget to test it.   ',0
	CNOP 0,2

Whapped

	DC.B	'User selected the WHAP gadget.	   ',0
	CNOP 0,2

NotWhapped

	DC.B	'User did not select the WHAP gadget.  ',0
	CNOP 0,2

PCString

	DC.B	1,0,RP_JAM2,0
	DC.W	168,15
	DC.L	TOPAZ80
	DC.L	PCNumberString,0

PCNumberString

	DC.B	'     PC          '
PCNum	
	DC.B	0
	CNOP 0,2

BlankLine

	DC.B	'                                     ',0
	CNOP 0,2

TOPAZ80

	DC.L	TOPAZname
	DC.W	TOPAZ_EIGHTY
	DC.B	0,0

TOPAZname

	DC.B	'topaz.font',0
	CNOP 0,2


	END				assembly completed

