; ----------------------------------------------------------------------------
.area   _CODE

; ----------------------------------------------------------------------------
; void DisableInterrupts( );
.globl _DisableInterrupts
_DisableInterrupts::
	di
	ld hl, #0xC9FB
	ld ( #0x0038 ), hl
	ei
	
	ret
	
; ----------------------------------------------------------------------------
; void ResetAmsDOSInterrupt( );
.globl _ResetAmsDOSInterrupt
_ResetAmsDOSInterrupt::
amsdosIntPtr::
	ld hl, #0
	ld ( #0x0039 ), hl
	
	ret
	
; ----------------------------------------------------------------------------
amsdosInt::
	ld ( amsdos_backup_sp + 1 ), sp
	
amsdos_stackPtr::
	ld sp, #0
				
	push ix
	push iy
	push af
	push bc
	push de
	push hl
	exx
	ex af, af'
	push ix
	push iy
	push af
	push bc
	push de
	push hl
	
	; Disable Upper ROM
	set 3, c
	out (c),c
	
amsdos_callbackPtr::
	call 0
	
	pop hl
	pop de
	pop bc
	
	; Disable Upper ROM
	out (c),c

	pop af
	pop iy
	pop ix
	ex af, af'
	exx
	pop hl
	pop de
	pop bc
	pop af
	pop iy
	pop ix

amsdos_backup_sp::
	ld sp, #0
	
amsdosIntJpPtr::
	jp 0
	
; ----------------------------------------------------------------------------
; void SetupAmsDOSInterrupt( unsigned char *interruptStackPtr, void ( *callbackPtr )( ) );
.globl _SetupAmsDOSInterrupt
_SetupAmsDOSInterrupt::
	di
	
	ld hl, ( #0x0039 )
	ld ( amsdosIntPtr + 1 ), hl
	ld ( amsdosIntJpPtr + 1 ), hl
	
	ld hl, #2
	add hl, sp
	ld a, ( hl )
	inc hl
	ld h, ( hl )
	ld l, a ; interruptStackPtr
	ld ( amsdos_stackPtr + 1 ), hl	
	
	ld hl, #4
	add hl, sp
	ld a, ( hl )
	inc hl
	ld h, ( hl )
	ld l, a ; callbackPtr
	ld ( amsdos_callbackPtr + 1 ), hl	
	
	ld hl, #amsdosInt
	ld ( #0x0039 ), hl
	
	ei
	
	ret

; ----------------------------------------------------------------------------
; void SetupInterrupts( unsigned char *interruptStackPtr );
.globl _SetupInterrupts
_SetupInterrupts::
	ld hl, #2
	add hl, sp
	ld a, ( hl )
	inc hl
	ld h, ( hl )
	ld l, a ; interruptStackPtr
	ld ( int_1st_stackPtr + 1 ), hl	

	ld hl, #int_1st
	ld ( #0x0039 ), hl
	
	ret
	
; ----------------------------------------------------------------------------
; void SetCustomInterrupt( const EInterrupt inter, void ( *callbackPtr )( ) )
.globl _SetCustomInterrupt
_SetCustomInterrupt::
	ld hl, #2
	add hl, sp
	ld c, ( hl ) ; inter
	
	ld hl, #3
	add hl, sp
	ld a, ( hl )
	inc hl
	ld d, ( hl )
	ld e, a ; callbackPtr
	
	ld hl, #int_callbacks
	ld b, #0
	add hl, bc
	add hl, bc
	ld ( hl ), e
	inc hl
	ld ( hl ), d

	ret
	
; ----------------------------------------------------------------------------
int_1st::
	ld ( backup_hl + 1 ), hl
	pop hl
	ld ( backup_sp + 1 ), sp
	ld ( backup_ret + 1 ), hl
	
int_1st_stackPtr::
	ld sp, #0
				
	push ix
	push iy
	push af
	push bc
	push de
	push hl
	exx
	ex af, af'
	push ix
	push iy
	push af
	push bc
	push de
	push hl

	ld b, #0xF5
	in a, ( c )
	rrca
	jr c, int_000_recover				
	
int_call::
	call int_000
	
int_resume::				
	pop hl
	pop de
	pop bc
	pop af
	pop iy
	pop ix
	ex af, af'
	exx
	pop hl
	pop de
	pop bc
	pop af
	pop iy
	pop ix

backup_hl::
	ld hl, #0
	
backup_sp::
	ld sp, #0

	ei
backup_ret::
	jp 0
	
; ----------------------------------------------------------------------------
int_callbacks::
	.dw #0 ; int 0
	.dw #0 ; int 1
	.dw #0 ; int 2
	.dw #0 ; int 3
	.dw #0 ; int 4
	.dw #0 ; int 5

; ----------------------------------------------------------------------------
; IN a = interrupt number (0-5)
; OUT hl = callback ptr (null by default; if not set!)
getCallback::
	ld hl, #int_callbacks
	ld b, #0
	ld c, a
	add hl, bc
	add hl, bc
	
	ld a, ( hl )
	inc hl
	ld h, ( hl )
	ld l, a

	ret

; ----------------------------------------------------------------------------
; IN a = interrupt number (0-5)
jumpToCallback::
	call getCallback
	ld a, h
	or l
	ret z	
	jp ( hl )
	
; ----------------------------------------------------------------------------
int_000_recover::
	call int_000
	jr int_resume
	
; ----------------------------------------------------------------------------
int_000::
	ld hl, #int_052	
	ld ( int_call + 1 ), hl	
	xor a
	jp jumpToCallback
	
; ----------------------------------------------------------------------------
int_052::
	ld hl, #int_104
	ld ( int_call + 1 ), hl		
	ld a, #1
	jp jumpToCallback
	
; ----------------------------------------------------------------------------
int_104::
	ld hl, #int_156	
	ld ( int_call + 1 ), hl		
	ld a, #2
	jp jumpToCallback
	
; ----------------------------------------------------------------------------
int_156::
	ld hl, #int_208
	ld ( int_call + 1 ), hl		
	ld a, #3
	jp jumpToCallback
	
; ----------------------------------------------------------------------------
int_208::
	ld hl, #int_260
	ld ( int_call + 1 ), hl		
	ld a, #4
	jp jumpToCallback
	
; ----------------------------------------------------------------------------
int_260::
	ld hl, #int_000
	ld ( int_call + 1 ), hl		
	ld a, #5
	jp jumpToCallback
