/* RCVMODEM.C
   EE490 Section 4

   Receive data from host port on APK modem via parallel port.

   Portions taken from Motorola's SPEC2.C:

                 * 23 Oct. 95  - RLR:  First release  *
		 *                                    *
		 * Copyright (c) MOTOROLA 1995        *
		 * Semiconductor Products Sector      *
		 * Digital Signal Processing Division *

   12/3/96 - Brendan Donahe:	First release
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>

#define	PP_BASE	0x378 			/* parallel port base address */

void getbuf(unsigned char *buf);	/* perform handshaking and get
					   data from parallel port */

void main() {
  unsigned char buf;                    /* single character buffer */

  system("cls");
  fprintf(stdout,"Press any key to end program...\n\n");

  do {
    getbuf(&buf);	                /* "blocking" wait for data */
    fprintf(stdout,"%c",buf);		/* display data	*/
  } while (kbhit() == 0);               /* check for keystroke */
}


void getbuf(unsigned char *buf) {
  unsigned char ack1, ack2, ack3;       /* ACK storage */

  outportb(PP_BASE+2,0x25);            	/* INI~ high for sync request */
  
  do {                         		/* Wait until ACK is low */
    ack1 = (inportb(PP_BASE+1) & 0x40); /*   3 times to cut down on */
    ack2 = (inportb(PP_BASE+1) & 0x40); /*   noise */
    ack3 = (inportb(PP_BASE+1) & 0x40);
  } while (ack1 | ack2 | ack3);

  outportb(PP_BASE+2,0x21);            	/* INI~ low to start transfer */
  outportb(PP_BASE+2,0x20);	        /* STRobe high */
  *buf = inportb (PP_BASE);   		/* get data from DSP */
  outportb(PP_BASE+2,0x21);   		/* STRobe low */
}

