#include "targetsys.h"
.file "asmhandler.s"
.text
.global stackptr,handler,asmhandler,regdata,handlerflag

.equ		SUSP,		8
.equ		SMSP,		12
.equ		SISP,		16
.equ		SVBR,		20
.equ		SCACR,		24
.equ		SCAAR,		28
.equ		SSFC,		32
.equ		SDFC,		36
.equ		SD0,		40

#if(DEVICE==68030)
.equ		SCRP,		104
.equ		SSRP,		112
.equ		STC,		120
.equ		STT0,		124
.equ		STT1,		128
.equ		SMMUSR,		134
#endif

#if(DEVICE==68040)
.equ		SURP,		104
.equ		SSRP,		112
.equ		STC,		120
.equ		SDTT0,		124
.equ		SDTT1,		128
.equ		SITT0,		132
.equ		SITT1,		136
.equ		SMMUSR,		142
#endif

#if(DEVICE==68040 || COPROCESSOR==TRUE)
.equ		FPCR,		144
.equ		FPSR,		148
.equ		FPIAR,		152
.equ		FP0,		156
#endif

asmhandler:	
		mov.l	%a7,stackptr
		movm.l	&0xffff,(regdata+SD0)
		movec.l	%usp,%d0
#if(DEVICE>=68010)
		movec.l	%vbr,%d1
		movec.l	%sfc,%d2
		movec.l	%dfc,%d3
#endif
#if(DEVICE==68010)
		mov.l	%ssp,%a0
#endif
#if(DEVICE>=68020)
		movec.l	%isp,%d4
		movec.l	%msp,%d5
		movec.l	%caar,%d6
		movec.l	%cacr,%d7
#endif
		mov.l	%d0,(regdata+SUSP)
#if(DEVICE>=68010)
		mov.l	%d1,(regdata+SVBR)
		mov.l	%d2,(regdata+SSFC)
		mov.l	%d3,(regdata+SDFC)
#endif
#if(DEVICE==68010)
		mov.l	%a0,(regdata+SISP)
#endif
#if(DEVICE>=68020)
		mov.l	%d4,(regdata+SISP)
		mov.l	%d5,(regdata+SMSP)
		mov.l	%d6,(regdata+SCAAR)
		mov.l	%d7,(regdata+SCACR)
#endif
#if(DEVICE==68030)
		lea	(regdata),%a0
		short	0xf028
		short	0x4e00
		short	0x0068		#pmove crp,(104,a0)
		short	0xf028
		short	0x4a00
		short 	0x0070		#pmove msrp,(112,a0)
		short	0xf028
		short	0x4200
		short	0x0078		#pmove mtc,(120,a0)
		short	0xf028
		short	0x0a00
		short	0x007c		#pmove mtt0,(124,a0)
		short	0xf028
		short	0x0e00
		short	0x0080		#pmove mtt1,(128,a0)
		short	0xf028
		short	0x6200
		short	0x0086		#pmove mmusr,(134,a0)
#endif
#if(DEVICE==68040)
		lea	(regdata),%a0
					#68040 instructions to effect supervisor
					#programer model support should be
					#inserted here and below !
#endif
#if(DEVICE==68040 || COPROCESSOR==TRUE)
		lea	(regdata),%a0
		short	0xf228
		short	0xb000
		short	0x0090 		#fmove.l fpcr,(&144,a0)
		short	0xf228
		short	0xa800
		short	0x0094 		#fmove.l fpsr,(&148,a0)
		short	0xf228
		short	0xa400
		short	0x0098 		#fmove.l fpiar,(&152,a0)
		short	0xf228
		short	0xf0ff
		short	0x009c 		#fmovem.l fp0-fp7,(&156,a0)
#endif
		jsr	handler
		cmp.b	&0,handlerflag
		blt	backtomonitor
		mov.l	(regdata+SUSP),%d0
#if(DEVICE>=68010)
		mov.l	(regdata+SVBR),%d1
		mov.l	(regdata+SSFC),%d2
		mov.l	(regdata+SDFC),%d3
#endif
#if(DEVICE>=68020)
		mov.l	(regdata+SISP),%d4
		mov.l	(regdata+SMSP),%d5
		mov.l	(regdata+SCAAR),%d6
		mov.l	(regdata+SCACR),%d7
#endif
		movec.l	%d0,%usp
#if(DEVICE>=68010)
		movec.l	%d1,%vbr
		movec.l	%d2,%sfc
		movec.l	%d3,%dfc
#endif
#if(DEVICE>=68020)
		movec.l	%d4,%isp
		movec.l	%d5,%msp
		movec.l	%d6,%caar
		movec.l	%d7,%cacr
#endif
		movm.l	(regdata+SD0),&0xffff
#if(DEVICE==68030)
		lea.l 	(SCRP),%a0
		short	0xf010
		short	0x4c00		#pmove (a0),crp
		lea.l 	(SSRP),%a0
		short	0xf010
		short	0x4800		#pmove (a0),srp
		lea.l 	(STC),%a0
		short	0xf010
		short	0x4000		#pmove (a0),tc
		lea.l 	(STT0),%a0
		short	0xf010
		short	0x0800		#pmove (a0),tt0
		lea.l 	(STT1),%a0
		short	0xf010
		short	0x0c00		#pmove (a0),tt1
		lea.l 	(SMMUSR),%a0
		short	0xf010
		short	0x6000		#pmove (a0),psr
#endif
#if(DEVICE==68040)
					#68040 instructions to effect supervisor
					#programer model support should be
					#inserted here !
#endif
#if(DEVICE==68040 || COPROCESSOR==TRUE)
		lea.l 	(FPCR),%a0
		short	0xf210
		short	0x9000 		#fmove.x (a0),fpcr
		lea.l 	(FPSR),%a0
		short	0xf210
		short	0x8800 		#fmove.x (a0),fpsr
		lea.l 	(FPIAR),%a0
		short	0xf210
		short	0x8400 		#fmove.x (a0),fpiar
		lea.l 	(FP0),%a0
		short	0xf210
		short	0xd0ff 		#fmovem.x (a0),fp0-fp7
#endif
		rte

backtomonitor:	mov.w	&0x2700,%sr
		jmp	abortstart
