From 0823879b84e84a8e9b843a64ca9f9f3192ce0c49 Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Fri, 15 Feb 2008 23:11:51 +0000
Subject: [PATCH] ez80 is code complete

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@691 42af7a65-404d-4744-a932-0658087f49c3
---
 arch/z80/include/z8/irq.h              |  2 +-
 arch/z80/src/common/up_arch.h          | 15 -------------
 arch/z80/src/z8/chip.h                 | 30 ++++++++++++++++++++++++++
 arch/z80/src/z8/z8_irq.c               | 25 +++++++++++++++++++++
 arch/z80/src/z8/z8_schedulesigaction.c |  6 +++---
 arch/z80/src/z8/z8_sigdeliver.c        | 25 ++++++++++++++++-----
 arch/z80/src/z80/chip.h                | 14 +++++++++++-
 7 files changed, 92 insertions(+), 25 deletions(-)

diff --git a/arch/z80/include/z8/irq.h b/arch/z80/include/z8/irq.h
index 7e8dbf699e..049c8b84cd 100644
--- a/arch/z80/include/z8/irq.h
+++ b/arch/z80/include/z8/irq.h
@@ -300,7 +300,7 @@ struct xcptcontext
   /* The following retains that state during signal execution */
 
   uint16 saved_pc;	/* Saved return address */
-  uint16 saved_i;	/* Saved interrupt state */
+  uint16 saved_irqctl;	/* Saved interrupt state */
 #endif
 };
 #endif
diff --git a/arch/z80/src/common/up_arch.h b/arch/z80/src/common/up_arch.h
index 5ef0b932c7..ff53010dbd 100644
--- a/arch/z80/src/common/up_arch.h
+++ b/arch/z80/src/common/up_arch.h
@@ -52,19 +52,4 @@
  * Definitions
  ************************************************************************************/
 
-/************************************************************************************
- * Inline Functions
- ************************************************************************************/
-
-#ifndef __ASSEMBLY__
-
-# define getreg8(a)           (*(volatile ubyte *)(a))
-# define putreg8(v,a)         (*(volatile ubyte *)(a) = (v))
-# define getreg32(a)          (*(volatile uint32 *)(a))
-# define putreg32(v,a)        (*(volatile uint32 *)(a) = (v))
-# define getreg(a)   getreg16(1)
-# define putreg(v,a) putreg16(v,a)
-
-#endif
-
 #endif  /* __UP_ARCH_H */
diff --git a/arch/z80/src/z8/chip.h b/arch/z80/src/z8/chip.h
index 99b058404c..be216f1e04 100644
--- a/arch/z80/src/z8/chip.h
+++ b/arch/z80/src/z8/chip.h
@@ -45,6 +45,14 @@
  * Definitions
  ************************************************************************************/
 
+/* Hexadecimal Representation *******************************************************/
+
+#ifdef __ASSEMBLY__
+# define _HX(h)   %##h
+#else
+# define _HX(w)   0x##h
+#endif
+
 /* Memory Map
  *
  * 64Kb Program Memory (64K series)
@@ -60,6 +68,28 @@
  *    f00 -   fff   : 256 byte control register area
  */
 
+/* Special Function Registers *******************************************************
+ *
+ * Because of the many different ez80 configurations, we will rely on the
+ * ZDS-II header file, ez8.h, to provide the correct addresses for each register.
+ */
+
+/* Register access macros ***********************************************************
+ *
+ * The register access mechanism provided in ez8.h differs from the useful in other
+ * NuttX architectures.  The following NuttX common macros will at least make the
+ * access compatible at the source level (however, strict type check is lost).
+ */
+
+#ifndef __ASSEMBLY__
+# define getreg8(a)           (a)
+# define putreg8(v,a)         ((a) = (v))
+# define getreg16(a)          (a)
+# define putreg16(v,a)        ((a) = (v))
+# define getreg32(a)          (a)
+# define putreg32(v,a)        ((a) = (v))
+#endif /* __ASSEMBLY__ */
+
 /************************************************************************************
  * Public Function Prototypes
  ************************************************************************************/
diff --git a/arch/z80/src/z8/z8_irq.c b/arch/z80/src/z8/z8_irq.c
index d3a46bf178..38f24a9686 100644
--- a/arch/z80/src/z8/z8_irq.c
+++ b/arch/z80/src/z8/z8_irq.c
@@ -44,6 +44,8 @@
 #include <nuttx/arch.h>
 #include <nuttx/irq.h>
 
+#include <ez8.h>
+
 #include "chip/switch.h"
 #include "up_internal.h"
 
@@ -81,6 +83,19 @@ struct z8_irqstate_s g_z8irqstate;
 
 irqstate_t irqsave(void)
 {
+  /* Bit 7 (IRQE) of the IRQCTL register determines if interrupts were
+   * enabled when this function was called.
+   */
+
+  register irqstate_t retval = getreg8(IRQCTL);
+
+  /* Disable interrupts */
+
+  DI();
+
+  /* Return the previous interrupt state */
+
+  return retval;
 }
 
 /****************************************************************************
@@ -93,4 +108,14 @@ irqstate_t irqsave(void)
 
 void irqrestore(irqstate_t flags)
 {
+  /* Bit 7 (IRQE) of the IRQCTL register determines if interrupts were
+   * enabled when irqsave() was called.
+   */
+
+  if ((flags & 0x80) != 0)
+    {
+      /* The IRQE bit was set, re-enable interrupts */
+
+      EI();
+    }
 }
diff --git a/arch/z80/src/z8/z8_schedulesigaction.c b/arch/z80/src/z8/z8_schedulesigaction.c
index ad02f0137c..fd9e715202 100644
--- a/arch/z80/src/z8/z8_schedulesigaction.c
+++ b/arch/z80/src/z8/z8_schedulesigaction.c
@@ -76,12 +76,12 @@ static void z8_sigsetup(FAR _TCB *tcb, sig_deliver_t sigdeliver, FAR chipreg_t *
 
   tcb->xcp.sigdeliver    = sigdeliver;
   tcb->xcp.saved_pc      = regs[XCPT_PC];
-  tcb->xcp.saved_i       = regs[XCPT_I];
+  tcb->xcp.saved_irqctl  = regs[XCPT_IRQCTL];
 
   /* Then set up to vector to the trampoline with interrupts disabled */
 
-  regs[XCPT_PC]  = (chipreg_t)up_sigdeliver;
-  regs[XCPT_I]   = 0;
+  regs[XCPT_PC]          = (chipreg_t)up_sigdeliver;
+  regs[XCPT_IRQCTL]      = 0;
 }
 
 /****************************************************************************
diff --git a/arch/z80/src/z8/z8_sigdeliver.c b/arch/z80/src/z8/z8_sigdeliver.c
index 857db51e5b..8ba3c552eb 100644
--- a/arch/z80/src/z8/z8_sigdeliver.c
+++ b/arch/z80/src/z8/z8_sigdeliver.c
@@ -64,6 +64,21 @@
  * Private Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: z8_copystate
+ ****************************************************************************/
+
+/* Maybe a little faster than most memcpy's */
+
+static void z8_copystate(FAR chipreg_t *dest, FAR const chipreg_t *src)
+{
+  int i;
+  for (i = 0; i < XCPTCONTEXT_REGS; i++)
+    {
+      *dest++ = *src++;
+    }
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -83,7 +98,7 @@ void up_sigdeliver(void)
 {
 #ifndef CONFIG_DISABLE_SIGNALS
   FAR _TCB  *rtcb = (_TCB*)g_readytorun.head;
-  chipret_t regs[XCPTCONTEXT_REGS];
+  chipreg_t regs[XCPTCONTEXT_REGS];
   sig_deliver_t sigdeliver;
 
   /* Save the errno.  This must be preserved throughout the signal handling
@@ -102,8 +117,8 @@ void up_sigdeliver(void)
   /* Save the real return state on the stack. */
 
   z8_copystate(regs, rtcb->xcp.regs);
-  regs[XCPT_PC] = rtcb->xcp.saved_pc;
-  regs[XCPT_I]  = rtcb->xcp.saved_i;
+  regs[XCPT_PC]     = rtcb->xcp.saved_pc;
+  regs[XCPT_IRQCTL] = rtcb->xcp.saved_irqctl;
 
   /* Get a local copy of the sigdeliver function pointer.  We do this so
    * that we can nullify the sigdeliver function point in the TCB and accept
@@ -115,7 +130,7 @@ void up_sigdeliver(void)
 
   /* Then restore the task interrupt state. */
 
-  irqrestore(regs[XCPT_I]);
+  irqrestore(regs[XCPT_IRQCTL]);
 
   /* Deliver the signals */
 
@@ -134,7 +149,7 @@ void up_sigdeliver(void)
    */
 
   up_ledoff(LED_SIGNAL);
-  z8_restoreusercontext(regs);
+  z8_restorecontext(regs);
 #endif
 }
 
diff --git a/arch/z80/src/z80/chip.h b/arch/z80/src/z80/chip.h
index d5c36bd19f..b960e481fb 100644
--- a/arch/z80/src/z80/chip.h
+++ b/arch/z80/src/z80/chip.h
@@ -45,7 +45,7 @@
  * Definitions
  ************************************************************************************/
 
-/* Bits in the Z80 FLAGS register */
+/* Bits in the Z80 FLAGS register ***************************************************/
 
 #define Z80_C_FLAG      0x01       /* Bit 0: Carry flag */
 #define Z80_N_FLAG      0x02       /* Bit 1: Add/Subtract flag  */
@@ -54,6 +54,18 @@
 #define Z80_Z_FLAG      0x40       /* Bit 5: Zero flag */
 #define Z80_S_FLAG      0x80       /* Bit 7: Sign flag */
 
+/* Register access macros ***********************************************************/
+
+#ifndef __ASSEMBLY__
+
+# define getreg8(a)           (*(volatile ubyte *)(a))
+# define putreg8(v,a)         (*(volatile ubyte *)(a) = (v))
+# define getreg32(a)          (*(volatile uint32 *)(a))
+# define putreg32(v,a)        (*(volatile uint32 *)(a) = (v))
+# define getreg(a)   getreg16(1)
+# define putreg(v,a) putreg16(v,a)
+
+#endif
 /************************************************************************************
  * Public Function Prototypes
  ************************************************************************************/
-- 
GitLab