home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / exec / interrupts / rbf / rbfhandler_c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  6.8 KB  |  203 lines

  1. ;/* rbfhandler_c.c - Execute me to assemble/compile me with Lattice 5.04
  2. Asm -iH:include/ -orbfhandler_a.o rbfhandler_a.asm
  3. LC -b1 -cfistq -v -y -j73 rbfhandler_c.c
  4. Blink FROM LIB:c.o,rbfhandler_c.o,rbfhandler_a.o TO rbfhandler LIBRARY LIB:LC.lib,LIB:Amiga.lib
  5. quit
  6. */
  7.  
  8. /* RBFHandler_c.c - C module of interrupt handler example
  9.  *
  10.  *  See also Serial Device chapter (most applications use the serial.device)
  11.  *
  12.  * To receive characters, this example requires ascii serial input 
  13.  * at your Amiga's current serial hardware baud rate (ie. 9600 after 
  14.  * reboot, else last baud rate used)
  15.  *
  16.  * Copyright (c) 1990 Commodore-Amiga, Inc.
  17.  *
  18.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  19.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  20.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  21.  * information on the correct usage of the techniques and operating system
  22.  * functions presented in this example.  The source and executable code of
  23.  * this example may only be distributed in free electronic form, via bulletin
  24.  * board or as part of a fully non-commercial and freely redistributable
  25.  * diskette.  Both the source and executable code (including comments) must
  26.  * be included, without modification, in any copy.  This example may not be
  27.  * published in printed form or distributed with any commercial product.
  28.  * However, the programming techniques and support routines set forth in
  29.  * this example may be used in the development of original executable
  30.  * software products for Commodore Amiga computers.
  31.  * All other rights reserved.
  32.  * This example is provided "as-is" and is subject to change; no warranties
  33.  * are made.  All use is at your own risk.  No liability or responsibility
  34.  * is assumed.
  35.  *
  36.  */
  37.  
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <exec/execbase.h>
  41. #include <exec/interrupts.h>
  42. #include <resources/misc.h>
  43. #include <hardware/custom.h>
  44. #include <hardware/intbits.h>
  45. #include <libraries/dos.h>
  46. #ifdef LATTICE
  47. #include <proto/all.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. int CXBRK(void) { return(0); }        /* Disable Lattice CTRL-C handling */
  52. int chkabort(void) { return(0); }    /* really */
  53. extern struct Custom far custom; /* defined in amiga.lib */
  54. UBYTE *GetMiscResource(long,UBYTE *);
  55. void FreeMiscResource(long);
  56. #pragma libcall MiscBase GetMiscResource 6 9002
  57. #pragma libcall MiscBase FreeMiscResource c 001
  58. #else
  59. extern struct Custom custom;
  60. /* Without pragmas, you must provide C bindings for misc.resource calls.
  61.  * See the Resources chapter.
  62.  */
  63. #endif
  64.  
  65. /* Our assembler interrupt handler code entry */
  66. extern void RBFHandler();
  67.  
  68. /* Our C subroutines */
  69. void tryRemSer(void);
  70. void cleanup(void);
  71. void cleanexit(UBYTE *,LONG);
  72.  
  73. #define BUFLEN 256
  74. struct OurData {
  75.     struct Task *maintask;
  76.     ULONG mainsig;
  77.     UWORD bufi;
  78.     UBYTE chbuf[BUFLEN+2];
  79.     UBYTE flbuf[BUFLEN+2];
  80.     UBYTE ourname[32];
  81.     };
  82.  
  83. struct OurData *ourdata = NULL;
  84. struct Interrupt *RBFInterrupt = NULL;
  85. struct Interrupt *PriorInterrupt = NULL;
  86. struct MiscResource *MiscBase = NULL;
  87. BOOL PriorEnable = FALSE;
  88. BOOL Installed = FALSE;
  89. BOOL GotPort = FALSE, GotBits = FALSE;
  90. BYTE mainsignum = -1;
  91. UBYTE *MyName = "RBF-Example";
  92. extern struct ExecBase *SysBase;
  93.  
  94. void main(int argc,char **argv)
  95.     {
  96.     struct Device *dev;
  97.     ULONG signals;
  98.     UBYTE *user;
  99.  
  100.     /* Try to get the serial hardware resources */
  101.     if (NULL == (MiscBase=(struct MiscResource *)OpenResource("misc.resource")))
  102.         cleanexit("Can't open misc.resource\n",RETURN_FAIL);
  103.  
  104.     GotPort = ((user = GetMiscResource(MR_SERIALPORT,MyName)) == NULL);
  105.     if (user)
  106.         {
  107.         printf("Serial hardware currently allocated by %s\n",user);
  108.         if (!(strcmp(user,"serial.device")))
  109.             {
  110.             printf("Will try to remove serial device\n");
  111.             Forbid();
  112.             dev=(struct Device *)FindName(&SysBase->DeviceList,"serial.device");
  113.             if(dev) RemDevice(dev);
  114.             Permit();
  115.             GotPort = ((GetMiscResource(MR_SERIALPORT,MyName)) == NULL);
  116.             }
  117.         }
  118.  
  119.     GotBits = ((GetMiscResource(MR_SERIALBITS,MyName)) == NULL);
  120.     if (GotPort && GotBits) printf("Allocated the serial hardware\n"); 
  121.     else cleanexit("Can't allocate the serial hardware\n",RETURN_FAIL);
  122.  
  123.     /* Allocate a signal so interrupts can signal main */
  124.     if (-1 == (mainsignum = AllocSignal(-1)))
  125.               cleanexit("Can't allocate signal\n",RETURN_FAIL);
  126.  
  127.     /* Allocate an Interrupt node structure: */
  128.     if (NULL == (RBFInterrupt = (struct Interrupt *)
  129.             AllocMem((LONG)sizeof(struct Interrupt), MEMF_PUBLIC|MEMF_CLEAR)))
  130.               cleanexit("Can't allocate interrupt structure\n",RETURN_FAIL);
  131.  
  132.     /* Allocate our data structure which includes our input buffers */
  133.     if (NULL == (ourdata = (struct OurData *)
  134.             AllocMem((LONG)sizeof(struct OurData), MEMF_PUBLIC|MEMF_CLEAR)))
  135.               cleanexit("Can't allocate data structure\n",RETURN_FAIL);
  136.  
  137.     /* Initialize ourdata structure */
  138.     ourdata->maintask = FindTask(NULL);
  139.     ourdata->mainsig = 1L << mainsignum;
  140.  
  141.     /* Initialize the Interrupt node */
  142.     RBFInterrupt->is_Node.ln_Type = NT_INTERRUPT;
  143.     RBFInterrupt->is_Node.ln_Pri  = 0;
  144.     strcpy(ourdata->ourname,MyName);
  145.     RBFInterrupt->is_Node.ln_Name = ourdata->ourname;
  146.  
  147.     RBFInterrupt->is_Data = (APTR)ourdata;
  148.     RBFInterrupt->is_Code = RBFHandler;
  149.  
  150.  
  151.     /* Save state of RBF interrupt and disable it */
  152.     PriorEnable = custom.intenar & INTF_RBF ? TRUE : FALSE;
  153.     custom.intena = INTF_RBF;
  154.  
  155.     /* Install the new interrupt handler */
  156.     PriorInterrupt = SetIntVector(INTB_RBF, RBFInterrupt);
  157.     Installed = TRUE;
  158.  
  159.     if (PriorInterrupt) printf("Replaced the %s RBF interrupt handler\n",
  160.                   PriorInterrupt->is_Node.ln_Name);
  161.  
  162.     printf("Enabling RBF interrupt...\n");
  163.     custom.intena = INTF_SETCLR | INTF_RBF;
  164.  
  165.     printf("Waiting for our handler to fill character buffer. CTRL-C to exit\n");
  166.     signals = Wait(ourdata->mainsig | SIGBREAKF_CTRL_C);
  167.  
  168.     if(signals & SIGBREAKF_CTRL_C) printf("Break...\n");
  169.     printf("Character buffer contains:\n");
  170.     puts(ourdata->chbuf);
  171.  
  172.     printf("\nRestoring previous handler and exiting...\n");
  173.     cleanup();
  174.     exit(RETURN_OK);
  175.     }
  176.  
  177. void cleanexit(s,e)
  178. UBYTE *s;
  179. LONG e;
  180.     {
  181.     if(*s) printf(s);
  182.     cleanup();
  183.     exit(e);
  184.     }
  185.  
  186. void cleanup()
  187.     {
  188.     if(Installed)
  189.         {
  190.         /* Disable serial int, restore prior handler and prior state */
  191.         custom.intena = INTF_RBF;
  192.         SetIntVector(INTB_RBF, PriorInterrupt);
  193.         if(PriorEnable) custom.intena = INTF_SETCLR|INTF_RBF;
  194.         }
  195.     if(ourdata) FreeMem(ourdata,(LONG)sizeof(struct OurData));
  196.     if(RBFInterrupt) FreeMem(RBFInterrupt,(LONG)sizeof(struct Interrupt));    
  197.     if(mainsignum != -1)  FreeSignal(mainsignum);
  198.     if(GotBits) FreeMiscResource(MR_SERIALBITS);
  199.     if(GotPort) FreeMiscResource(MR_SERIALPORT);
  200.     }
  201.  
  202.  
  203.