From: vjh21@cas.org (Vince Herried ext 2877)
Subject: parallel.c (hacking @ cia)
Organization: CAS
Date: Tue, 15 Dec 1992 13:14:55 GMT

recently I posted the source to a crude example on how to hack away
at the parallel port via the CIA.  Here is another sample that
follows the rules.



/*
example of going direct to the cia's to do I/O to the parallel port
*/

#include <hardware/cia.h>
#include <resources/cia.h>
#include <resources/misc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct CIA *ciaa,*ciab;
struct Library *MiscBase = NULL;
extern	 int   Enable_Abort;

char str[180];	     /* hex input buffer */

int GetHex (void)
{
   int i,j,k,l,ok;
   UBYTE low_byte, hi_byte, byte;

   ok = FALSE;
   do {
      printf("Enter Hex string to output (0-9,A-F), blanks are ignored.\n==>");
      gets(str);
      k=l=0;
      j = strlen(str);
      ok = TRUE;

      for (i=0;i<j;i++)
      {
	 byte=tolower(str[i]);

	 if (byte == ' ') continue;  /* allow imbeded blanks for readability */

	 if ( byte < '0' ||
	     (byte > '9' && byte < 'a') |
	      byte > 'f' )
	 {
	    printf("Bad hex character = %c, re-enter output.\n",byte);
	    ok=FALSE;
	    continue;
	 } /* if */

	 if ( byte >= 'a' )
	 {
	    byte -= 'a' - 10;
	 } /* if byte */
	 else
	    byte -= '0';
	 if ( k == 0 )
	 {
	    k++;
	    hi_byte = byte;
	    str[l++] = byte;
	 } /* if k */
	 else
	 {
	    k = 0;
	    str[l-1] = (16 * hi_byte) + byte;
	 } /* else k */
      } /* for	*/
   } while (ok != TRUE);

   return(l);
} /* GetHex () */

void main (int argc,char *argv[])

{

   UBYTE SampleByte,command;
   UBYTE *owner = NULL;    /* owner of misc resource */
   int i,j;
   Enable_Abort=0;	/* turn off ^c */

   if ( !(MiscBase = (struct Library *)OpenResource(MISCNAME)))
   {
      printf("Cannot open %s\n",MISCNAME);
      exit(NULL);
   } /* if !(MiscBase */

   if ((owner = AllocMiscResource(MR_PARALLELPORT,"Parallel port hog")) != NULL)
   {
      printf("Unable to allocate MR_PARALLELPORT because %s owns it\n",owner);
      exit(NULL);
   } /* if  */

   if ((owner = AllocMiscResource(MR_PARALLELBITS,"Parallel port hog")) != NULL)
   {
      printf("Unable to allocate MR_PARALLELBITS because %s owns it\n",owner);
      FreeMiscResource(MR_PARALLELPORT);
      exit(NULL);
   } /* if  */


   ciaa=(void *) 0xbfe001L;
   ciab=(void *) 0xbfd000L;

   do {
      do
      {
	 puts("Enter an I to do input, O for output, E to exit");
	 gets(str);
	 command = tolower(str[0]);
      } while (command != 'i' && command != 'o' && command != 'e');


      if ( command == 'e' || command == 'x')
      {
	 FreeMiscResource(MR_PARALLELBITS);
	 FreeMiscResource(MR_PARALLELPORT);
	 puts ("Ending by user request.");
	 exit(NULL);
      } /* if + */

      if ( command == 'i')
      {
	 puts("Doing an input from the parallel I/O port.");

      /* set CIAA port B direction to be all inputs */
      ciaa->ciaddrb = 0x00;

      /* set CIAB port A pins POUT and SEL to be output */

      ciab->ciaddra |= 0x06;

      SampleByte = ciaa->ciaprb;       /* read a byte of data */

      printf("SampleByte = %02X\n",SampleByte);

      puts("End of input");
      } /* if command */

      if ( command == 'o' )
      {

	 ciaa->ciaddrb = 0xff;
	 ciab->ciaddra &= 0xF9;

      j = GetHex();        /* read in hex string, return length */

      printf("%d bytes to send\n",j);

      for (i=0;i<j;i++)
      {
	 SampleByte = str[i];
	 ciaa->ciaprb = SampleByte;	  /* write a byte of data */
	 printf(" %02X",SampleByte);
      }
      puts(".\nEnd of output.");

      } /* if command */

   }  while(command != 'e');



} /* main () */


-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
				 |     Vincent Herried  (614) 447-3600 x 2877
				 |     Chemical Abstracts Service
 _____________________           |     P.O. Box 3012
(_)________________   \          |     Columbus, OH 43210-0012
  ________________|\   \         |
 (_)______________\_\   \        |     Current thought: People who cannot be
   ______________________\       |     understood are geniuses, so management
  (_)____________________|       |     assigns them the toughest projects.
				 |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Newsgroups: comp.sys.amiga.programmer