trap-main.c

00001 /* Trap example for 68HC11
00002    Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00052 #include "trap.h"
00053 
00054 #ifdef USE_INTERRUPT_TABLE
00055 
00056 /* Interrupt table used to connect our timer_interrupt handler.
00057 
00058    Note: the `XXX_handler: foo' notation is a GNU extension which is
00059    used here to ensure correct association of the handler in the struct.
00060    This is why the order of handlers declared below does not follow
00061    the HC11 order.  */
00062 struct interrupt_vectors __attribute__((section(".vectors"))) vectors = 
00063 {
00064   res0_handler:           fatal_interrupt, /* res0 */
00065   res1_handler:           fatal_interrupt,
00066   res2_handler:           fatal_interrupt,
00067   res3_handler:           fatal_interrupt,
00068   res4_handler:           fatal_interrupt,
00069   res5_handler:           fatal_interrupt,
00070   res6_handler:           fatal_interrupt,
00071   res7_handler:           fatal_interrupt,
00072   res8_handler:           fatal_interrupt,
00073   res9_handler:           fatal_interrupt,
00074   res10_handler:          fatal_interrupt, /* res 10 */
00075   sci_handler:            fatal_interrupt, /* sci */
00076   spi_handler:            fatal_interrupt, /* spi */
00077   acc_overflow_handler:   fatal_interrupt, /* acc overflow */
00078   acc_input_handler:      fatal_interrupt,
00079   timer_overflow_handler: fatal_interrupt,
00080   output5_handler:        fatal_interrupt, /* out compare 5 */
00081   output4_handler:        fatal_interrupt, /* out compare 4 */
00082   output3_handler:        fatal_interrupt, /* out compare 3 */
00083   output2_handler:        fatal_interrupt, /* out compare 2 */
00084   output1_handler:        fatal_interrupt, /* out compare 1 */
00085   capture3_handler:       fatal_interrupt, /* in capt 3 */
00086   capture2_handler:       fatal_interrupt, /* in capt 2 */
00087   capture1_handler:       fatal_interrupt, /* in capt 1 */
00088   rtii_handler:           fatal_interrupt,
00089   irq_handler:            fatal_interrupt, /* IRQ */
00090   xirq_handler:           fatal_interrupt, /* XIRQ */
00091   illegal_handler:        fatal_interrupt, /* illegal */
00092   cop_fail_handler:       fatal_interrupt,
00093   cop_clock_handler:      fatal_interrupt,
00094 
00095   /* What we really need.  */
00096   swi_handler:            os_trap_handler, /* swi */
00097   reset_handler:          _start
00098 };
00099 
00100 #endif
00101 
00102 extern int global_result;
00103 
00104 int
00105 main()
00106 {
00107   int result;
00108   int failed;
00109 
00110   serial_init ();
00111 
00112 #ifndef USE_INTERRUPT_TABLE
00113   /* With interrupt table, we can't change the SWI handler.  */
00114 
00115   /* Test with simple trap handler.  */
00116   set_interrupt_handler (SWI_VECTOR, (interrupt_t) simple_trap_handler);
00117   print ("Using simple trap handler: ");
00118   failed = 0;
00119   simple_trap_handler (1);
00120   if (global_result != 1)
00121     {
00122       print ("  Simple trap handler failed, didn't returned 1\r\n");
00123       failed = 1;
00124     }
00125   
00126   simple_trap_handler (2);
00127   if (global_result != 4)
00128     {
00129       print ("  Simple trap handler failed, didn't returned 4\r\n");
00130       failed = 1;
00131     }
00132   if (failed == 0)
00133     print ("OK\r\n");
00134 
00135   /* Test with add trap handler.  */
00136   set_interrupt_handler (SWI_VECTOR, (interrupt_t) add_trap_handler);
00137   failed = 0;
00138   print ("Using add trap handler: ");
00139   result = add_trap_handler (1, 2, 3, 4);
00140   if (result != 10)
00141     {
00142       print ("Add trap handler didn't returned 10\r\n");
00143       failed = 1;
00144     }
00145   result = add_trap_handler (5, 6, 7, 8);
00146   if (result != 26)
00147     {
00148       print ("Add trap handler didn't returned 26\r\n");
00149       failed = 1;
00150     }
00151   if (failed == 0)
00152     print ("OK\r\n");
00153 #endif
00154 
00155   set_interrupt_handler (SWI_VECTOR, (interrupt_t) os_trap_handler);
00156   failed = 0;
00157   print ("Using OS trap handler...\r\n");
00158   result = os_trap_handler (0, "  Hello World from os_trap_handler...\r\n");
00159   if (result != 0)
00160     {
00161       print ("  OS call 0 failed\r\n");
00162       failed = 1;
00163     }
00164 
00165   result = os_trap_handler (0, "  Type a character ");
00166   if (result != 0)
00167     {
00168       print ("  OS call 0 failed\r\n");
00169       failed = 1;
00170     }
00171   
00172   result = os_trap_handler (1);
00173 
00174   result = os_trap_handler (2, 23, 44);
00175   if (result != 23 + 44)
00176     {
00177       print ("  OS add system call failed\r\n");
00178       failed = 1;
00179     }
00180   
00181   if (failed == 0)
00182     print ("OS trap call test ok.\r\n");
00183   
00184   print ("Trap handler test finished.\r\n");
00185   return 0;
00186 }