/*
 * Program to use interrupts to send data over 6821 parallel port.
 * 
 */

#include <hc11.h>
#include "pia.h"

unsigned char s[] = "AARDVARK";

int i;

main()
{
	/* Set up 6821 Port B for output with handshaking
	 */
	PIA_CRB = 0x00;		/* Select DDRB */
	PIA_DDRB = 0xff;	/* Make Port B output */
	/*
	 * Set up Port B for handshaking:
	 * CRB =  X X 1 0 0 1 0 1
	 *        | | | | | | | |
	 *        | | | | | | IRQB1 set on falling edge of CB1, 
	 *        | | | | | |   interrupt generated
	 *        | | | | | | 
	 *        | | | | | Select Port B Data
	 *        | | | | | 
	 *        | | CB2 is output, interlock handshaking
	 *        | |
	 *        Read-only flag bits
	 */
	PIA_CRB = 0x25;

	/* Set up interrupt vector */
	IRQ_JMP = JMP_OP_CODE;
	IRQ_VEC = irq_isr;
	
	/* Clear IRQB1 by reading Port B */
	PIA_B;
	
	enable();
	
	i = 0;
	
	PIA_B = s[i++];

	while (TRUE)
	{
	}
}

#pragma interrupt_handler irq_isr;
void irq_isr(void)
{
	PIA_B;           		/* Read PIA_B to clear interupt flag */
	PIA_B = s[i++];  		/* Put data on PIA_B output */
	if (s[i] == '\0') 
		PIA_CRB = 0x24;     /* Done with string; disable interrupts */
}


/*
 * Program to receive string on PIA Port A and display it
 * on PIA Port B using interrupts
 *
 */

#include <hc11.h>
#include "pia.h"

main()
{

	/* Set up 6821 Port A for input with handshaking
	 */
	PIA_CRA = 0x00;		/* Select DDRA */
	PIA_DDRA = 0x00;	/* Make Port A input */
	/*
	 * Set up Port A for handshaking:
	 * CRA =  X X 1 0 0 1 0 1
	 *        | | | | | | | |
	 *        | | | | | | | Interrupt generated on falling edge of CA1.
	 *        | | | | | | |
	 *        | | | | | | IRQA1 set on falling edge of CA1, 
	 *        | | | | | | 
	 *        | | | | | Select Port A Data
	 *        | | | | | 
	 *        | | CA2 interlock handshaking
	 *        | |
	 *        Read-only flag bits
	 */
	PIA_CRA = 0x25;

	/* Set up 6821 Port B for output
	 */
	PIA_CRB = 0x00;		/* Select DDRB */
	PIA_DDRB = 0xff;	/* Make Port B output */
	/*
	 * Set up Port B for no handshaking:
	 * CRB =  X X 0 0 0 1 0 0
	 *        | | | | | | | |
	 *        | | | | | | | no interrupt generated
	 *        | | | | | | |
	 *        | | | | | | IRQB1 set on falling edge of CB1, 
	 *        | | | | | | 
	 *        | | | | | Select Port B Data
	 *        | | | | | 
	 *        | | CB2 is input, no interrupts
	 *        | |
	 *        Read-only flag bits
	 */
	PIA_CRB = 0x04;

	/* Set up interrupt vector */
	IRQ_JMP = JMP_OP_CODE;
	IRQ_VEQ = irq_isr;
	
	/* Clear IRQA1 flag by reading A */
	PIA_A;
	
	enable();

	while (TRUE)
	{
	}
}

#pragma interrupt_handler irq_isr;
void irq_isr(void)
{
	PIA_B = PIA_A;   /* Put data on PIA_B output; clear interrupt flag */
}