
; ===============================================================
; ---------------------------------------------------------------
; Error handling and debugging modules
; 2016-2017, Vladikcomper
; ---------------------------------------------------------------
; Error handler functions and calls
; ---------------------------------------------------------------

; ---------------------------------------------------------------
; Error handler control flags
; ---------------------------------------------------------------

; Screen appearence flags
_eh_address_error	equ	$01		; use for address and bus errors only (tells error handler to display additional "Address" field)
_eh_show_sr_usp		equ	$02		; displays SR and USP registers content on error screen

; Advanced execution flags
; WARNING! For experts only, DO NOT USES them unless you know what you're doing
_eh_return			equ	$20
_eh_enter_console	equ	$40
_eh_align_offset	equ	$80

; ---------------------------------------------------------------
; Errors vector table
; ---------------------------------------------------------------

; Default screen configuration
_eh_default			equ	0 ;_eh_show_sr_usp

; ---------------------------------------------------------------

ERROR_InvalidPriorityPtr:
	RaiseError "Invalid Priority layer", PRINT_InvalidPriorityPtr

PRINT_InvalidPriorityPtr:
	Console.WriteLine "%<pal1>Addr:    %<pal2>%<.l a0>"
	Console.WriteLine "%<pal1>Object:  %<pal0>%<.l (a0) sym|split>%<pal2>%<symdisp>"
	addq.w	#priority,a0
	Console.WriteLine "%<pal1>Layer:   %<pal2>%<.w (a0)>"
	subq.w	#priority - routine,a0
	Console.WriteLine "%<pal1>Routine: %<pal2>%<.b (a0)>"
	add.w	#status - routine,a0
	Console.WriteLine "%<pal1>Status:  %<pal2>%<.b (a0)>"
	rts

BusError:
	__ErrorMessage "BUS ERROR", _eh_default|_eh_address_error

AddressError:
	__ErrorMessage "ADDRESS ERROR", _eh_default|_eh_address_error

IllegalInstr:
	__ErrorMessage "ILLEGAL INSTRUCTION", _eh_default

ZeroDivide:
	__ErrorMessage "ZERO DIVIDE", _eh_default

ChkInstr:
	__ErrorMessage "CHK INSTRUCTION", _eh_default

TrapvInstr:
	__ErrorMessage "TRAPV INSTRUCTION", _eh_default

PrivilegeViol:
	__ErrorMessage "PRIVILEGE VIOLATION", _eh_default

Trace:
	__ErrorMessage "TRACE", _eh_default

Line1010Emu:
	__ErrorMessage "LINE 1010 EMULATOR", _eh_default

Line1111Emu:
	__ErrorMessage "LINE 1111 EMULATOR", _eh_default

ErrorExcept:
	__ErrorMessage "ERROR EXCEPTION", _eh_default

Trap0:
	__ErrorMessage "KOSM QUEUE TOO FULL", _eh_default

; ---------------------------------------------------------------
; Error handler external functions (compiled only when used)
; ---------------------------------------------------------------


; WARNING! This blocks are intended to compile if referenced anywhere in the code.
;	However, as of AS version 1.42 [Bld 55], "IFUSED" directive doesn't work properly.

; --------------------------------------------------------------
;	ifused ErrorHandler___extern_scrollconsole
ErrorHandler___extern__scrollconsole:

;	endif

; --------------------------------------------------------------
;	ifused ErrorHandler___extern__console_only
ErrorHandler___extern__console_only:
	dc.l	$46FC2700, $4FEFFFF2, $48E7FFFE, $47EF003C
	jsr		ErrorHandler___global__errorhandler_setupvdp(pc)
	jsr		ErrorHandler___global__error_initconsole(pc)
	dc.l	$4CDF7FFF, $487A0008, $2F2F0012, $4E7560FE
;	endif

; --------------------------------------------------------------
;	ifused ErrorHandler___extern__vsync
ErrorHandler___extern__vsync:
	dc.l	$41F900C0, $000444D0, $6BFC44D0, $6AFC4E75
;	endif

; ---------------------------------------------------------------
; Include error handler binary module
; ---------------------------------------------------------------

ErrorHandler:
	binclude "General/ErrorHandler.bin"

; ---------------------------------------------------------------
; WARNING!
;	DO NOT put any data from now on! DO NOT use ROM padding!
;	Symbol data should be appended here after ROM is compiled
;	by ConvSym utility, otherwise debugger modules won't be able
;	to resolve symbol names.
; ---------------------------------------------------------------
