;
; 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 a subroutine to change context between processes.
;
; (C) Copyright Rich Teer 1990, 2023, 2026.
;

	globl	_chg_context
	globl	_io_chg_context

_chg_context:
	MOVE.L	$04(A7),A0					; Move address of process descriptor into A0
	MOVE.L	$04(A0),saved_a0_1			; 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_1,A0				; Restore A0
	RTS									; and return

_io_chg_context:
	MOVE.W	#$A1,$800020				; Enable the timer int the PIT to start time slicing
	MOVE.L	$04(A7),A0					; Move address of process descriptor into A0
	MOVE.L	$04(A0),saved_a0_1			; 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_1,A0				; Restore A0
	RTE									; and return

saved_a0_1:
	DS.L	1							; Space for A0
