/*
 *  What follows is an example the uses the system resources to access the
 *  parallel port directly.
 *
 *          - Phillip Lindsay
 */


 /* parout.c - Parallel port resource example.
  *
  * Phillip Lindsay (c) Commodore-Amiga, Inc.
  *     Unlimited use granted as long as copyright notice remains intact.
  *
  */
 #include <exec/types.h>
 #include <exec/nodes.h>
 #include <exec/lists.h>
 #include <exec/interrupts.h>
 #include <hardware/custom.h>
 #include <hardware/intbits.h>
 #include <hardware/cia.h>
 #include <resources/misc.h>
 #include <resources/cia.h>     /* this header has defines for hardware addresses */
 
 #define CIAA_DDRPBOUT  0xff    /* CIAA port B all "ouputs" */
 
 #define MANX   /* using MANX Aztec C */
 
 #ifdef MANX
 #include <functions.h>
 #endif
 
 extern CPTR  GetMiscResource();
 extern VOID  FreeMiscResource();
 extern ULONG CIAARoutine();
 
 struct MiscResource *MiscResource=NULL;
 CPTR                       CIAAResource=NULL;
 
 char *myname = "myparallel";
 char *teststr= "This is a test line being sent to a parallel device.\n";
 
 struct Task *mytask;
 LONG       mysignal;
 ULONG      mysigmask;
 
 main()
 {
        struct Interrupt CIAAInterrupt;
        register count=0;
 
        MiscResource = (struct MiscResource *) OpenResource(MISCNAME);
        if(!MiscResource) exit(10);
 puts("after open misc.resource");
 
        CIAAResource = (CPTR) OpenResource(CIAANAME);
         if(!CIAAResource) exit(20);
 puts("after open ciaa.resource");
 
 /* this is where we get our 8bits for parallel transfer */
        if(GetMiscResource(MiscResource,MR_PARALLELPORT,myname)) exit(30);
 puts("after GetMiscResource PARALLELPORT (CIAA Port B)");
 
 /* this is where we get busy(bit 0), pout(bit 1), sel(bit 2) */
        if(GetMiscResource(MiscResource,MR_PARALLELBITS,myname))
         {
          FreeMiscResource(MiscResource,MR_PARALLELPORT);
           exit(40);
         }
 puts("after GetMiscResource PARALLELBITS BUSY/POUT/SEL (CIAB Port A bits 0,1,2)");
 
        if((mysignal = AllocSignal(-1L)) == -1L)
         {
          FreeMiscResource(MiscResource,MR_PARALLELPORT);
          FreeMiscResource(MiscResource,MR_PARALLELBITS);
           exit(50);
         }
 puts("after AllocSignal()");
 
        mysigmask = 1L << mysignal;
 
        /* now we own the parallel port, next handshake interrupt ACK */
        setmem(&CIAAInterrupt,(ULONG)sizeof(CIAAInterrupt),(ULONG)'\0');
 puts("after setmem()");
        CIAAInterrupt.is_Data = (APTR)      CIAAResource;
        CIAAInterrupt.is_Code = (VOID(*)()) CIAARoutine;
        CIAAInterrupt.is_Node.ln_Type = NT_INTERRUPT;
        CIAAInterrupt.is_Node.ln_Name = myname;
 puts("after interrupt init");
        if(AddICRVector(CIAAResource,CIAICRB_FLG,&CIAAInterrupt))
         {
          FreeMiscResource(MiscResource,MR_PARALLELPORT);
          FreeMiscResource(MiscResource,MR_PARALLELBITS);
          FreeSignal(mysignal);
           exit(50);
         }
 puts("after AddICRVector()");
 
        /* disable ACK interrupt */
        AbleICR(CIAAResource,CIAICRF_FLG);
 puts("after AbleICR() disable flag interrupt");
 
        /* set up direction for i/o ports used */
        ciaa.ciaddrb = CIAA_DDRPBOUT;   /* make CIAA port B all "ouputs" */
 
        /* make BUSY, SEL, POUT "inputs" on CIAA port A */
        ciab.ciaddra &= ( ~CIAF_PRTRBUSY | ~CIAF_PRTRPOUT | ~CIAF_PRTRSEL );
 
        /* clear any pending interrupts */
        SetICR(CIAAResource,CIAICRF_FLG);
 puts("after SetICR() clear pending flag interrupts");
 
        /* enable ACK interrupt */
        AbleICR(CIAAResource,CIAICRF_SETCLR|CIAICRF_FLG);
 puts("after AbleICR() enable flag interrupt");
 
        while(teststr[count])
         {
          if(ciab.ciapra & CIAF_PRTRBUSY) /* make sure device is not busy */
           continue;
          ciaa.ciaprb = teststr[count++]; /* write data to port */
          wait(mysigmask);                /* wait for ack       */
         }
 
        AbleICR(CIAAResource,CIAICRF_FLG);      /* disable interrupt */
        /* remove interrupt */
        RemICRVector(CIAAResource,CIAICRB_FLG,&CIAAInterrupt);
        /* free resources */
        FreeMiscResource(MiscResource,MR_PARALLELPORT);
        FreeMiscResource(MiscResource,MR_PARALLELBITS);
        FreeSignal(mysignal);
 }
 /* end of main() */
 
 
 ULONG  CIAARoutine()
 {
 #ifdef MANX
        geta4();
 #endif
        Signal(mytask,mysigmask);
        return(0L);
 }
 
 /* eof */
 
