From 821975f7bd6627ea0bcab6703a528f21b1cfa7e6 Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Sun, 30 Dec 2007 21:21:19 +0000
Subject: [PATCH] Needs same fixes as for up_head.asm

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@465 42af7a65-404d-4744-a932-0658087f49c3
---
 arch/z80/src/common/up_restoreusercontext.asm | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/arch/z80/src/common/up_restoreusercontext.asm b/arch/z80/src/common/up_restoreusercontext.asm
index 1840b260e1..b37b6a7526 100644
--- a/arch/z80/src/common/up_restoreusercontext.asm
+++ b/arch/z80/src/common/up_restoreusercontext.asm
@@ -64,27 +64,33 @@ _up_restoreusercontext:
 	pop	hl		; BC = Address of save structure
 	ld	sp, hl		; SP points to top of storage area
 
-	; Disable interrupts while we muck with the alternative registers
+	; Disable interrupts while we muck with the alternative registers.  The
+	; Correct interrupt state will be restore below
+
+	di
 
 	; Restore registers.  HL points to the beginning of the reg structure to restore
 
 	ex	af, af'			; Select alternate AF
 	pop	af			; Offset 0: AF' = I with interrupt state in carry
+	ex	af, af'			;   Restore original AF
 	pop	bc			; Offset 1: BC
 	pop	de			; Offset 2: DE
 	pop	ix			; Offset 3: IX
 	pop	iy			; Offset 4: IY
 	exx				;   Use alternate BC/DE/HL
-	pop	hl			; Offset 5: HL' = Stack pointer at time of interrupt
-	exx
+	ld	hl, #-2			;   Offset of SP to account for ret addr on stack
+	pop	de			; Offset 5: HL' = Stack pointer after return
+	add	hl, de			;   HL = Stack pointer value before return
+	exx				;   Restore original BC/DE/HL
 	pop	hl			; Offset 6: HL
 	pop	af			; Offset 7: AF
 
 	; Restore the stack pointer
 
-	exx
-	ld	sp, hl
-	exx
+	exx				; Use alternate BC/DE/HL
+	ld	sp, hl			; Set SP = saved stack pointer value before return
+	exx				; Restore original BC/DE/HL
 
 	; Restore interrupt state
 
-- 
GitLab