home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / amiga / programm / 17379 < prev    next >
Encoding:
Text File  |  1992-12-15  |  4.4 KB  |  192 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: sparky!uunet!caen!malgudi.oar.net!chemabs!vjh21
  3. From: vjh21@cas.org (Vince Herried ext 2877)
  4. Subject: parallel.c (hacking @ cia)
  5. Message-ID: <1992Dec15.131455.20981@cas.org>
  6. Sender: usenet@cas.org
  7. Organization: CAS
  8. Date: Tue, 15 Dec 1992 13:14:55 GMT
  9. Lines: 181
  10.  
  11. recently I posted the source to a crude example on how to hack away
  12. at the parallel port via the CIA.  Here is another sample that
  13. follows the rules.
  14.  
  15.  
  16.  
  17. /*
  18. example of going direct to the cia's to do I/O to the parallel port
  19. */
  20.  
  21. #include <hardware/cia.h>
  22. #include <resources/cia.h>
  23. #include <resources/misc.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28.  
  29. struct CIA *ciaa,*ciab;
  30. struct Library *MiscBase = NULL;
  31. extern     int   Enable_Abort;
  32.  
  33. char str[180];         /* hex input buffer */
  34.  
  35. int GetHex (void)
  36. {
  37.    int i,j,k,l,ok;
  38.    UBYTE low_byte, hi_byte, byte;
  39.  
  40.    ok = FALSE;
  41.    do {
  42.       printf("Enter Hex string to output (0-9,A-F), blanks are ignored.\n==>");
  43.       gets(str);
  44.       k=l=0;
  45.       j = strlen(str);
  46.       ok = TRUE;
  47.  
  48.       for (i=0;i<j;i++)
  49.       {
  50.      byte=tolower(str[i]);
  51.  
  52.      if (byte == ' ') continue;  /* allow imbeded blanks for readability */
  53.  
  54.      if ( byte < '0' ||
  55.          (byte > '9' && byte < 'a') |
  56.           byte > 'f' )
  57.      {
  58.         printf("Bad hex character = %c, re-enter output.\n",byte);
  59.         ok=FALSE;
  60.         continue;
  61.      } /* if */
  62.  
  63.      if ( byte >= 'a' )
  64.      {
  65.         byte -= 'a' - 10;
  66.      } /* if byte */
  67.      else
  68.         byte -= '0';
  69.      if ( k == 0 )
  70.      {
  71.         k++;
  72.         hi_byte = byte;
  73.         str[l++] = byte;
  74.      } /* if k */
  75.      else
  76.      {
  77.         k = 0;
  78.         str[l-1] = (16 * hi_byte) + byte;
  79.      } /* else k */
  80.       } /* for    */
  81.    } while (ok != TRUE);
  82.  
  83.    return(l);
  84. } /* GetHex () */
  85.  
  86. void main (int argc,char *argv[])
  87.  
  88. {
  89.  
  90.    UBYTE SampleByte,command;
  91.    UBYTE *owner = NULL;    /* owner of misc resource */
  92.    int i,j;
  93.    Enable_Abort=0;    /* turn off ^c */
  94.  
  95.    if ( !(MiscBase = (struct Library *)OpenResource(MISCNAME)))
  96.    {
  97.       printf("Cannot open %s\n",MISCNAME);
  98.       exit(NULL);
  99.    } /* if !(MiscBase */
  100.  
  101.    if ((owner = AllocMiscResource(MR_PARALLELPORT,"Parallel port hog")) != NULL)
  102.    {
  103.       printf("Unable to allocate MR_PARALLELPORT because %s owns it\n",owner);
  104.       exit(NULL);
  105.    } /* if  */
  106.  
  107.    if ((owner = AllocMiscResource(MR_PARALLELBITS,"Parallel port hog")) != NULL)
  108.    {
  109.       printf("Unable to allocate MR_PARALLELBITS because %s owns it\n",owner);
  110.       FreeMiscResource(MR_PARALLELPORT);
  111.       exit(NULL);
  112.    } /* if  */
  113.  
  114.  
  115.    ciaa=(void *) 0xbfe001L;
  116.    ciab=(void *) 0xbfd000L;
  117.  
  118.    do {
  119.       do
  120.       {
  121.      puts("Enter an I to do input, O for output, E to exit");
  122.      gets(str);
  123.      command = tolower(str[0]);
  124.       } while (command != 'i' && command != 'o' && command != 'e');
  125.  
  126.  
  127.       if ( command == 'e' || command == 'x')
  128.       {
  129.      FreeMiscResource(MR_PARALLELBITS);
  130.      FreeMiscResource(MR_PARALLELPORT);
  131.      puts ("Ending by user request.");
  132.      exit(NULL);
  133.       } /* if + */
  134.  
  135.       if ( command == 'i')
  136.       {
  137.      puts("Doing an input from the parallel I/O port.");
  138.  
  139.       /* set CIAA port B direction to be all inputs */
  140.       ciaa->ciaddrb = 0x00;
  141.  
  142.       /* set CIAB port A pins POUT and SEL to be output */
  143.  
  144.       ciab->ciaddra |= 0x06;
  145.  
  146.       SampleByte = ciaa->ciaprb;       /* read a byte of data */
  147.  
  148.       printf("SampleByte = %02X\n",SampleByte);
  149.  
  150.       puts("End of input");
  151.       } /* if command */
  152.  
  153.       if ( command == 'o' )
  154.       {
  155.  
  156.      ciaa->ciaddrb = 0xff;
  157.      ciab->ciaddra &= 0xF9;
  158.  
  159.       j = GetHex();        /* read in hex string, return length */
  160.  
  161.       printf("%d bytes to send\n",j);
  162.  
  163.       for (i=0;i<j;i++)
  164.       {
  165.      SampleByte = str[i];
  166.      ciaa->ciaprb = SampleByte;      /* write a byte of data */
  167.      printf(" %02X",SampleByte);
  168.       }
  169.       puts(".\nEnd of output.");
  170.  
  171.       } /* if command */
  172.  
  173.    }  while(command != 'e');
  174.  
  175.  
  176.  
  177. } /* main () */
  178.  
  179.  
  180. -- 
  181. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  182.                  |     Vincent Herried  (614) 447-3600 x 2877
  183.                  |     Chemical Abstracts Service
  184.  _____________________           |     P.O. Box 3012
  185. (_)________________   \          |     Columbus, OH 43210-0012
  186.   ________________|\   \         |
  187.  (_)______________\_\   \        |     Current thought: People who cannot be
  188.    ______________________\       |     understood are geniuses, so management
  189.   (_)____________________|       |     assigns them the toughest projects.
  190.                  |
  191. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  192.