/*	Musterprogramm fuer IDE-Portadapter      */


#include <stdio.h>,<conio.h>,<dos.h>,<string.h>

#define portadrs 	0x176     // Physische Adressierung
#define dir_reg      	0x175
#define dat_reg      	0x174
#define ioport_a    	0x1c
#define ioport_b    	0x1d
#define ioport_c    	0x1e
#define ioport_d    	0x1f

#define  dir_a    	0         // Logische Adressierung
#define  dat_a          1
#define  port_a         2
#define  dir_b    	3
#define  dat_b          4
#define  port_b         5
#define  dir_c    	6
#define  dat_c          7
#define  port_c         8
#define  dir_d    	9
#define  dat_d         10
#define  port_d        11


// Globale Variable

int portregs [12];          // Kopien der Portbelegung

int portctl  [12];	    // Steuerregister fr Portzugriffe

int bitmasks [8];          // Masken fr Bitzugriffe

// E-A-Zugriffsfunktionen


// 1. Physische Ein- und Ausgabe

void dirout(int adrs, int databyte)  // Ausgabe Richtungsregister
{
outportb (portadrs, adrs); outportb (dir_reg,databyte);
}

void datout (int adrs, int databyte)  // Ausgabe Datenregister
{
outportb (portadrs, adrs); outportb (dat_reg, databyte);
}

int datin (int adrs)                // Porteingabe
{
outportb (portadrs, adrs); return  inportb (dat_reg);
}


// Logische Ein- und Ausgabe

void out (int adrs, int data)

{

switch ((portctl [adrs] & 0xff00))
{
	case 0x0100:                 // Richtungsregister
	portregs [adrs] = data;     // Kopie speichern
	outportb (portadrs, (portctl [adrs] & 0xff));
	outportb (dir_reg,data);
	break;

	case  0x0200:               // Datenregister
	portregs [adrs] = data;    // Kopie speichern
	outportb (portadrs, (portctl [adrs] & 0xff));
	outportb (dat_reg,data);
	break;

}

return;

}


int in (int adrs)

{

if ((portctl [adrs] & 0xff00) == 0x0200)
{
outportb (portadrs, (portctl [adrs] & 0xff));
portregs [adrs] =  inportb (dat_reg);

}

return portregs [adrs];
}

void bitout (int adrs, int bitpos, int data)

{


switch ((portctl [adrs] & 0xff00))
{
	case 0x0100:                 // Richtungsregister
	if (data == 0) portregs [adrs] = (portregs [adrs] & !bitmasks [bitpos] & 0xff);
		else   portregs [adrs] = (portregs [adrs] | bitmasks [bitpos]);
	outportb (portadrs, (portctl [adrs] & 0xff));
	outportb (dir_reg,portregs [adrs]);
	break;

	case  0x0200:               // Datenregister
	if (data == 0) portregs [adrs] = (portregs [adrs] & !bitmasks [bitpos] & 0xff);
		else   portregs [adrs] = (portregs [adrs] | bitmasks [bitpos]);
	outportb (portadrs, (portctl [adrs] & 0xff));
	outportb (dat_reg,portregs [adrs]);
	break;

}

return;

}


int bitin (int adrs, int bitpos)

{

if ((portctl [adrs] & 0xff00) == 0x0200)
{
outportb (portadrs, (portctl [adrs] & 0xff));
portregs [adrs] =  inportb (dat_reg);
}

if ((portregs [adrs] & bitmasks [bitpos]) != 0) return 0;
 else return 1;

}







/*       Hauptprogrammm       */


void main()
{
char auxstring [20];     // Hilfszeichenkette fr Debugging

int n;

int chartab [10];  /* 7 Seg Character Table 0...9*/



// hier die eigenen Variablen definieren





// Initialisierung

for (n=0; n<=11; n++)
portregs [n] = 0;

portctl  [0] = 0x011c;                       // Port A
portctl  [1] = 0x021c;
portctl  [2] = 0x031c;
portctl  [3] = 0x011d;                       // Port B
portctl  [4] = 0x021d;
portctl  [5] = 0x031d;
portctl  [6] = 0x011e;                       // Port C
portctl  [7] = 0x021e;
portctl  [8] = 0x031e;
portctl  [9] = 0x011f;                      // Port D
portctl [10] = 0x021f;
portctl [11] = 0x031f;

bitmasks[0] = 1;
bitmasks[1] = 2;
bitmasks[2] = 4;
bitmasks[3] = 8;
bitmasks[4] = 16;
bitmasks[5] = 32;
bitmasks[6] = 64;
bitmasks[7] = 128;

chartab[0] = 0xc0;
chartab[1] = 0xf9;
chartab[2] = 0xa4;
chartab[3] = 0xb0;
chartab[4] = 0x99;
chartab[5] = 0x92;
chartab[6] = 0x82;
chartab[7] = 0xf8;
chartab[8] = 0x80;
chartab[9] = 0x90;


//  eigene Initialisierungen *******************************

	 clrscr();

	printf("Es geht los\n");


// das eigene Programm *******************************


printf ("Fertig. Beliebiges Zeichen eingeben\n");
scanf ("%s",auxstring);



}




