#include <stdio.h>
#include <asm/types.h>
#include <unistd.h>
#include <asm/io.h>

#define PORT_BASE 0x240
#define	DATA 0x01
#define	CLOCK 0x02

int main(void) {

	int portdata, prevdata;
	int clocks = 0, chars = 0;
	/* attempt to reserve region in io space */
	if(ioperm(PORT_BASE, 3, 1) < 0) {
		fprintf(stderr, "Cannot reserve region in io space, aborting...\n");
		exit(1);
	}
	prevdata = 0;

	while(1) {
		portdata = inb(PORT_BASE);
		if(portdata != prevdata) {
			prevdata = portdata;
// printf("portdata=0x%02x, CLOCK=0x%02x, portdata&CLOCK=0x%02x\n", portdata, CLOCK, portdata&CLOCK);
			if(portdata & CLOCK) {
				if(chars++ == 75) {
					printf("\n");
					chars = 0;
				}
				if(portdata & DATA)
					printf("-");
				else
					printf("_");
			}
		}
	}

	/* attempt to release reserved region in io space */
	if(ioperm(PORT_BASE, 3, 0) < 0) {
		fprintf(stderr, "Cannot release region in io space, aborting...\n");
		exit(1);
	}

	return(0);  /* just to be nice ;) */

}
