From b6f43e7144fb0eaa0c54f5f4891c320b4381dc55 Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Mon, 31 Dec 2007 15:45:58 +0000
Subject: [PATCH] Add z80 signals

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@472 42af7a65-404d-4744-a932-0658087f49c3
---
 Documentation/NuttX.html                   | 19 ++++---
 TODO                                       |  9 +++-
 arch/README.txt                            |  9 +++-
 arch/z80/include/z80/irq.h                 |  5 ++
 arch/z80/src/Makefile                      | 11 ++--
 arch/z80/src/common/up_internal.h          |  1 +
 arch/z80/src/common/up_schedulesigaction.c | 62 ++++++++++++----------
 arch/z80/src/common/up_sigdeliver.c        | 54 ++++++++++---------
 arch/z80/src/z80/Make.defs                 |  5 +-
 configs/README.txt                         |  7 +++
 configs/z80sim/defconfig                   | 14 ++---
 include/nuttx/arch.h                       |  2 +-
 12 files changed, 119 insertions(+), 79 deletions(-)

diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html
index c6455af6d2..4044642d45 100644
--- a/Documentation/NuttX.html
+++ b/Documentation/NuttX.html
@@ -417,6 +417,10 @@
   The level of network reliability is at a strong beta release level.  The
   baseline functionality of NuttX continues to mature and remains at
   post-beta or production level.
+  Extensive testing has been done for the ARM architecture using the GCC
+  compiler/toolchain.
+  Other architectures and the SDCC toolchain are also supported but not as
+  well exercised.
 </p>
 
 <p>
@@ -773,13 +777,14 @@ Other memory:
   </tr>
 </table>
 <ul>
-<li>ARM, ARM7 ARM7TDMI, ARM9, ARM926EJS are trademarks of Advanced RISC Machines, Limited.</li>
-<li>Cygwin is a trademark of Red Hat, Incorporated.</li>
-<li>Linux is a registered trademark of Linus Torvalds.</li>
-<li>LPC2148 is a trademark of NXP Semiconductors.</li>
-<li>TI is a tradename of Texas Instruments Incorporated.</li>
-<li>UNIX is a registered trademark of The Open Group.</li>
-<li>VxWorks is a registered trademark of Wind River Systems, Incorporated.</li>
+  <li>ARM, ARM7 ARM7TDMI, ARM9, ARM926EJS are trademarks of Advanced RISC Machines, Limited.</li>
+  <li>Cygwin is a trademark of Red Hat, Incorporated.</li>
+  <li>Linux is a registered trademark of Linus Torvalds.</li>
+  <li>LPC2148 is a trademark of NXP Semiconductors.</li>
+  <li>TI is a tradename of Texas Instruments Incorporated.</li>
+  <li>UNIX is a registered trademark of The Open Group.</li>
+  <li>VxWorks is a registered trademark of Wind River Systems, Incorporated.</li>
+  <li>Z80 is a registered trademark of Zilog, Inc.</li>
 </ul>
 <p>
   NOTE: NuttX is <i>not</i> licensed to use the POSIX trademark.  NuttX uses the POSIX
diff --git a/TODO b/TODO
index 57c3de98b6..a6e21812aa 100644
--- a/TODO
+++ b/TODO
@@ -8,7 +8,7 @@ NuttX TODO List (Last updated December 13, 2007)
   (1)  C++ Support
   (10) Network (net/, netutils/)
   (2)  USB (drivers/usbdev)
-  (2)  Libraries (lib/)
+  (3)  Libraries (lib/)
   (2)  File system (fs/, drivers/)
   (2)  Documentation (Documentation/)
   (3)  Build system
@@ -194,7 +194,12 @@ o Libraries (lib/)
                work as it should.  This is because the underlying
                representation of the environment is not an arry of pointers.
   Status:      Open
-  Priority:    Mediam
+  Priority:    Medium
+
+  Description: There are two different copies of getenv():  One in
+               sched/env_getenv.c and one in lib/lib_getenv().  Keep one.
+  Status:      Open
+  Priority:    Low
 
 o File system (fs/, drivers/)
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/arch/README.txt b/arch/README.txt
index f29edc15ef..104d988e74 100644
--- a/arch/README.txt
+++ b/arch/README.txt
@@ -159,9 +159,14 @@ arch/m68322
 arch/pjrc-8051
     8051 Microcontroller.  This port is not quite ready for prime time.
 
+arch/z80
+    z80 Microcontroller.
+    STATUS: Functional with no known defects.  There are still several
+    OS features that have not yet been tested (e.g., networking).
+
 The following architecture directories are deprecated.  They have been
-replaced by the logic in arm/arm and will deleted when arch/arm is fully
-verified.
+replaced by the logic in arm/arm and will deleted at some point in the
+future.
 
 arch/c5471
     Replaced with arch/arm/include/c5471 and arch/arm/src/c5471
diff --git a/arch/z80/include/z80/irq.h b/arch/z80/include/z80/irq.h
index 3bcaaed204..86bdfd273f 100644
--- a/arch/z80/include/z80/irq.h
+++ b/arch/z80/include/z80/irq.h
@@ -106,6 +106,11 @@ struct xcptcontext
 
 #ifndef CONFIG_DISABLE_SIGNALS
   void *sigdeliver; /* Actual type is sig_deliver_t */
+
+  /* The following retains that state during signal execution */
+
+  uint16 saved_pc;	/* Saved return address */
+  uint16 saved_i;	/* Saved interrupt state */
 #endif
 };
 #endif
diff --git a/arch/z80/src/Makefile b/arch/z80/src/Makefile
index e822fd05af..8917a4402e 100644
--- a/arch/z80/src/Makefile
+++ b/arch/z80/src/Makefile
@@ -126,11 +126,12 @@ $(COBJS): %$(OBJEXT): %.c
 
 $(SDCCLIBDIR)/myz80.lib: $(SDCCLIBDIR)/$(SDCCLIB)
 	@cat $(SDCCLIBDIR)/$(SDCCLIB) | \
-		grep -v calloc  | grep -v malloc  | grep -v realloc  | \
-		grep -v free    | grep -v vprintf | grep -v _strncpy | \
-		grep -v _strchr | grep -v _strlen | grep -v _strcmp  | \
-		grep -v _strcpy | grep -v _memcmp | grep -v _memcpy  | \
-		grep -v _memset | grep -v crt0 \
+		grep -v calloc   | grep -v malloc   | grep -v realloc  | \
+		grep -v free     | grep -v getenv   | grep -v vprintf  | \
+		grep -v sprintf  | grep -v _strncpy | grep -v _strchr  | \
+		grep -v _strlen  | grep -v _strcmp  | grep -v _strcpy  | \
+		grep -v _memcmp  | grep -v _memcpy  | grep -v _memset  | \
+		grep -v crt0 \
 		> myz80.lib
 	@sudo mv -f myz80.lib $(SDCCLIBDIR)/myz80.lib
 
diff --git a/arch/z80/src/common/up_internal.h b/arch/z80/src/common/up_internal.h
index 7b0ba44742..5e4db68da0 100644
--- a/arch/z80/src/common/up_internal.h
+++ b/arch/z80/src/common/up_internal.h
@@ -65,6 +65,7 @@
 #define SET_IRQCONTEXT(tcb)      up_copystate(current_regs, (tcb)->xcp.regs)
 #define SAVE_USERCONTEXT(tcb)    up_saveusercontext((tcb)->xcp.regs)
 #define RESTORE_USERCONTEXT(tcb) up_restoreusercontext((tcb)->xcp.regs)
+#define SIGNAL_RETURN(regs)      up_restoreusercontext(regs)
 
 /****************************************************************************
  * Public Types
diff --git a/arch/z80/src/common/up_schedulesigaction.c b/arch/z80/src/common/up_schedulesigaction.c
index f22144df41..946ba859b8 100644
--- a/arch/z80/src/common/up_schedulesigaction.c
+++ b/arch/z80/src/common/up_schedulesigaction.c
@@ -1,4 +1,4 @@
-/************************************************************
+/****************************************************************************
  * common/up_schedulesigaction.c
  *
  *   Copyright (C) 2007,2008 Gregory Nutt. All rights reserved.
@@ -31,38 +31,43 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Included Files
- ************************************************************/
+ ****************************************************************************/
 
 #include <nuttx/config.h>
+
 #include <sys/types.h>
 #include <sched.h>
 #include <debug.h>
+
 #include <nuttx/arch.h>
+#include <arch/irq.h>
+
 #include "os_internal.h"
 #include "up_internal.h"
-#include "up_arch.h"
 
-/************************************************************
+#ifndef CONFIG_DISABLE_SIGNALS
+
+/****************************************************************************
  * Private Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Public Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: up_schedule_sigaction
  *
  * Description:
@@ -93,13 +98,13 @@
  *       currently executing task -- just call the signal
  *       handler now.
  *
- ************************************************************/
+ ****************************************************************************/
 
-void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver)
+void up_schedule_sigaction(FAR _TCB *tcb, sig_deliver_t sigdeliver)
 {
   /* Refuse to handle nested signal actions */
 
-  dbg("tcb=0x%p sigdeliver=0x%p\n", tcb, sigdeliver);
+  dbg("tcb=0x%p sigdeliver=0x%04x\n", tcb, (uint16)sigdeliver);
 
   if (!tcb->xcp.sigdeliver)
     {
@@ -115,7 +120,7 @@ void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver)
 
       dbg("rtcb=0x%p current_regs=0x%p\n", g_readytorun.head, current_regs);
 
-      if (tcb == (_TCB*)g_readytorun.head)
+      if (tcb == (FAR _TCB*)g_readytorun.head)
         {
           /* CASE 1:  We are not in an interrupt handler and
            * a task is signalling itself for some reason.
@@ -136,21 +141,21 @@ void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver)
 
           else
             {
-              /* Save the return lr and cpsr and one scratch register
+              /* Save the return address and interrupt state.
                * These will be restored by the signal trampoline after
                * the signals have been delivered.
                */
 
               tcb->xcp.sigdeliver    = sigdeliver;
-              tcb->xcp.saved_pc      = current_regs[REG_PC];
-              tcb->xcp.saved_cpsr    = current_regs[REG_CPSR];
+              tcb->xcp.saved_pc      = current_regs[XCPT_PC];
+              tcb->xcp.saved_i       = current_regs[XCPT_I];
 
               /* Then set up to vector to the trampoline with interrupts
                * disabled
                */
 
-              current_regs[REG_PC]    = (uint32)up_sigdeliver;
-              current_regs[REG_CPSR]  = SVC_MODE | PSR_I_BIT | PSR_F_BIT;
+              current_regs[XCPT_PC]  = (uint16)up_sigdeliver;
+              current_regs[XCPT_I]   = 0;
 
               /* And make sure that the saved context in the TCB
                * is the same as the interrupt return context.
@@ -174,17 +179,20 @@ void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver)
            */
 
           tcb->xcp.sigdeliver    = sigdeliver;
-          tcb->xcp.saved_pc      = tcb->xcp.regs[REG_PC];
-          tcb->xcp.saved_cpsr    = tcb->xcp.regs[REG_CPSR];
+          tcb->xcp.saved_pc      = tcb->xcp.regs[XCPT_PC];
+          tcb->xcp.saved_i       = tcb->xcp.regs[XCPT_I];
 
           /* Then set up to vector to the trampoline with interrupts
            * disabled
            */
 
-          tcb->xcp.regs[REG_PC]   = (uint32)up_sigdeliver;
-          tcb->xcp.regs[REG_CPSR] = SVC_MODE | PSR_I_BIT | PSR_F_BIT;
+          tcb->xcp.regs[XCPT_PC] = (uint16)up_sigdeliver;
+          tcb->xcp.regs[XCPT_I]  = 0;
         }
 
       irqrestore(flags);
     }
 }
+
+#endif /* CONFIG_DISABLE_SIGNALS */
+
diff --git a/arch/z80/src/common/up_sigdeliver.c b/arch/z80/src/common/up_sigdeliver.c
index 0877fffe31..adbc894cbd 100644
--- a/arch/z80/src/common/up_sigdeliver.c
+++ b/arch/z80/src/common/up_sigdeliver.c
@@ -1,7 +1,7 @@
-/************************************************************
+/****************************************************************************
  * common/up_sigdeliver.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 3. Neither the name NuttX nor the names of its contributors may be
  *    used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
@@ -31,39 +31,43 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Included Files
- ************************************************************/
+ ****************************************************************************/
 
 #include <nuttx/config.h>
+
 #include <sys/types.h>
 #include <sched.h>
 #include <debug.h>
+
 #include <nuttx/irq.h>
 #include <nuttx/arch.h>
+
 #include "os_internal.h"
 #include "up_internal.h"
-#include "up_arch.h"
 
-/************************************************************
+#ifndef CONFIG_DISABLE_SIGNALS
+
+/****************************************************************************
  * Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Public Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: up_sigdeliver
  *
  * Description:
@@ -72,7 +76,7 @@
  *   with and forced to branch to this location with interrupts
  *   disabled.
  *
- ************************************************************/
+ ****************************************************************************/
 
 void up_sigdeliver(void)
 {
@@ -81,9 +85,9 @@ void up_sigdeliver(void)
   uint32 regs[XCPTCONTEXT_REGS];
   sig_deliver_t sigdeliver;
 
-  /* Save the errno.  This must be preserved throughout the
-   * signal handling so that the the user code final gets
-   * the correct errno value (probably EINTR).
+  /* Save the errno.  This must be preserved throughout the signal handling
+   * so that the the user code final gets the correct errno value (probably
+   * EINTR).
    */
 
   int saved_errno = rtcb->errno;
@@ -97,8 +101,8 @@ void up_sigdeliver(void)
   /* Save the real return state on the stack. */
 
   up_copystate(regs, rtcb->xcp.regs);
-  regs[REG_PC]   = rtcb->xcp.saved_pc;
-  regs[REG_CPSR] = rtcb->xcp.saved_cpsr;
+  regs[XCPT_PC] = rtcb->xcp.saved_pc;
+  regs[XCPT_I]  = rtcb->xcp.saved_i;
 
   /* Get a local copy of the sigdeliver function pointer.
    * we do this so that we can nullify the sigdeliver
@@ -112,7 +116,7 @@ void up_sigdeliver(void)
 
   /* Then restore the task interrupt state. */
 
-  irqrestore(regs[REG_CPSR]);
+  irqrestore(regs[XCPT_I]);
 
   /* Deliver the signals */
 
@@ -132,6 +136,8 @@ void up_sigdeliver(void)
    */
 
   up_ledoff(LED_SIGNAL);
-  up_fullcontextrestore(regs);
+  SIGNAL_RETURN(regs);
 #endif
 }
+
+#endif /* CONFIG_DISABLE_SIGNALS */
\ No newline at end of file
diff --git a/arch/z80/src/z80/Make.defs b/arch/z80/src/z80/Make.defs
index d4e38b11cc..0688d159b3 100644
--- a/arch/z80/src/z80/Make.defs
+++ b/arch/z80/src/z80/Make.defs
@@ -41,12 +41,9 @@ CMN_CSRCS	= up_initialize.c up_allocateheap.c up_initialstate.c \
 		  up_blocktask.c  up_unblocktask.c up_exit.c up_releasepending.c \
 		  up_reprioritizertr.c up_copystate.c up_irq.c up_idle.c \
 		  up_assert.c up_mdelay.c up_udelay.c \
+		  up_schedulesigaction.c up_sigdeliver.c \
 		  up_registerdump.c up_usestack.c
 
-ifneq ($(CONFIG_DISABLE_SIGNALS),y)
-CMD_CSRCS	+= up_schedulesigaction.c up_sigdeliver.c
-endif
-
 CHIP_ASRCS	= 
 CHIP_CSRCS	= 
 
diff --git a/configs/README.txt b/configs/README.txt
index 23ac940366..60151e243e 100644
--- a/configs/README.txt
+++ b/configs/README.txt
@@ -313,6 +313,13 @@ configs/pjrc-8051
     8051 Microcontroller.  This port uses the PJRC 87C52 development system
     and the SDCC toolchain.   This port is not quite ready for prime time.
 
+configs/z80zim
+    z80 Microcontroller.  This port uses a Z80 instruction set simulator.
+    That simulator can be found in the NuttX CVS at
+    http://nuttx.cvs.sourceforge.net/nuttx/misc/sims/z80sim.
+    This port also the SDCC toolchain (http://sdcc.sourceforge.net/")
+    (verified with version 2.6.0).
+
 Other ports for the for the TI TMS320DM270, M683222 and for MIPS are in various
 states of progress
 
diff --git a/configs/z80sim/defconfig b/configs/z80sim/defconfig
index 6141c34b13..0008e8adde 100644
--- a/configs/z80sim/defconfig
+++ b/configs/z80sim/defconfig
@@ -112,13 +112,13 @@ CONFIG_DEV_CONSOLE=n
 # o pthread_condtimedwait() depends on signals to wake
 #   up waiting tasks.
 #
-CONFIG_DISABLE_CLOCK=n
-CONFIG_DISABLE_POSIX_TIMERS=n
-CONFIG_DISABLE_PTHREAD=n
-CONFIG_DISABLE_SIGNALS=n
-CONFIG_DISABLE_MQUEUE=n
-CONFIG_DISABLE_MOUNTPOINT=n
-CONFIG_DISABLE_ENVIRON=n
+CONFIG_DISABLE_CLOCK=y
+CONFIG_DISABLE_POSIX_TIMERS=y
+CONFIG_DISABLE_PTHREAD=y
+CONFIG_DISABLE_SIGNALS=y
+CONFIG_DISABLE_MQUEUE=y
+CONFIG_DISABLE_MOUNTPOINT=y
+CONFIG_DISABLE_ENVIRON=y
 
 #
 # Misc libc settings
diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h
index db17b30de3..f7fc6582ef 100644
--- a/include/nuttx/arch.h
+++ b/include/nuttx/arch.h
@@ -57,7 +57,7 @@
  * Public Variables
  ****************************************************************************/
 
-typedef void  (*sig_deliver_t)(_TCB *tcb);
+typedef void (*sig_deliver_t)(FAR _TCB *tcb);
 
 /****************************************************************************
  * Public Function Prototypes
-- 
GitLab