;
; This file and its contents are supplied under the terms of the
; Mozilla Public License (MPL) version 2.0.  You may only use this
; file in accordance with the terms of version 2.0 of the MPL.
;
; A full copy of the text of the MPL should have accompanied this
; source.  A copy of the MPL is also available via the Internet at
; https://www.mozilla.org/en-CA/MPL/2.0.
;
; This Source Code Form is "Incompatible With Secondary Licenses",
; as defined by the Mozilla Public License, v. 2.0.
;
; This file implements the interrupt handler.
;
; (C) Copyright Rich Teer 1990, 2023, 2026.
;

	globl	_int_handler
	globl	_proc_array
	globl	_current_process			; Should be _current_proc_id: _id is truncated by as68

_int_handler:
	MOVE.L	A0,-(A7)					; Save A0 on stack
	MOVE.L	D0,-(A7)					; Save D0 on stack
	MOVE.L	D1,-(A7)					; Save D1 on stack
	MOVE.L	_current_process,D0			; Move current proc id into D0
	MOVE.L	#$46,D1						; Move length of process descriptor structure into D1
	MULS	D1,D0						; Find offset into proc_array
	ADD.L	#_proc_array,D0				; Add base address of proc_array
	MOVE.L	D0,A0						; and copy to A0
	MOVE.L	(A7)+,D1					; Restore D1	
	MOVE.L	(A7)+,D0					; Restore D0
	MOVE.L	(A7),$04(A0)				; Put original A0 in PD
	MOVE.L	A1,$08(A0)					; Save A1 in PD
	MOVE.L	A2,$0C(A0)					; Save A2 in PD
	MOVE.L	A3,$10(A0)					; Save A3 in PD
	MOVE.L	A4,$14(A0)					; Save A4 in PD
	MOVE.L	A5,$18(A0)					; Save A5 in PD
	MOVE.L	A6,$1C(A0)					; Save A6 in PD
	MOVE.L	D0,$20(A0)					; Save D0 in PD
	MOVE.L	D1,$24(A0)					; Save D1 in PD
	MOVE.L	D2,$28(A0)					; Save D2 in PD
	MOVE.L	D3,$2C(A0)					; Save D3 in PD
	MOVE.L	D4,$30(A0)					; Save D4 in PD
	MOVE.L	D5,$34(A0)					; Save D5 in PD
	MOVE.L	D6,$38(A0)					; Save D6 in PD
	MOVE.L	D7,$3C(A0)					; Save D7 in PD
	MOVE.L	A7,D0						; Copy SP to D0
	ADDQ.L	#$04,D0						; Bypass A0 on the stack
	MOVE.L	D0,(A0)						; Save "fudged" SP in PD
	MOVE.L	(A7)+,A0					; Restore A0
	OR.W	#$01,$800034				; Reset interrupt in PIT
	MOVE.W	#$A0,$800020				; Disable the clock in the PIT to turn off time slicing
	MOVE.L	#_proc_array,A0				; Move address of MAIN's process descriptor into A0
	MOVE.L	$04(A0),saved_a0_2			; Get A0 from PD and save
	MOVE.L	$08(A0),A1					; Restore A1 from PD
	MOVE.L	$0C(A0),A2					; Restore A2 from PD
	MOVE.L	$10(A0),A3					; Restore A3 from PD
	MOVE.L	$14(A0),A4					; Restore A4 from PD
	MOVE.L	$18(A0),A5					; Restore A5 from PD
	MOVE.L	$1C(A0),A6					; Restore A6 from PD
	MOVE.L	$20(A0),D0					; Restore D0 from PD
	MOVE.L	$24(A0),D1					; Restore D1 from PD
	MOVE.L	$28(A0),D2					; Restore D2 from PD
	MOVE.L	$2C(A0),D3					; Restore D3 from PD
	MOVE.L	$30(A0),D4					; Restore D4 from PD
	MOVE.L	$34(A0),D5					; Restore D5 from PD
	MOVE.L	$38(A0),D6					; Restore D6 from PD
	MOVE.L	$3C(A0),D7					; Restore D7 from PD
	MOVE.L	(A0),A7						; Restore SP from PD
	MOVE.L	saved_a0_2,A0				; Restore A0
	RTS									; and return

saved_a0_2:
	DS.L	1							; Space for A0
