/* * isa.c : Code to illustrate the access the Four 8-Bit I/O Lines * obtained by interfacing the ISA Bus * * Authors: Abhishek Dutta * Badri Adhikari * Pushpeshwor Pokharel * * * Coded by: Abhishek Dutta * http://www.mycgiserver.com/~thelinuxmaniac * 17 Jan 2006 */ #include #include #include /* Change these constants and addresses according to your device settings */ const int OUTPUT_PORT = 0x338; /* Starting address of output port */ const int INPUT_PORT = 0x238; /* Starting address of input port */ const int LENGTH = 3; /* No of continuous port addresses */ const int SLEEP_TIME = 1; /* Delay for DANCING LIGHT TEST */ void clear_screen(void); void write_port(void); void read_port(void); /* DANCING LIGHT TEST * Method: Connect eight LED (Light Emitting Diode) to any of the 8 output lines * and run this test.It will turn the LED ON one by one with some delay. * * Integer constants for running the dancing light test */ int a[9] = {1,2,4,8,16,32,64,128,256}; int b[9] = {256,128,64,32,16,8,4,2,1}; void test_output(short count) { short i,j; clear_screen(); /* Run the dancing lights 'count' no of times */ printf("No of tests\t= %d",count); printf("\nDELAY\t\t= %d",SLEEP_TIME); printf("\n\n\t\tCan you see the lights dancing ;)\n"); for(i=0;i OUTPUT_PORT+LENGTH) printf("\tERR: Port address out of range 0x%x - 0x%x",OUTPUT_PORT,OUTPUT_PORT+LENGTH); else { printf("Enter data: "); scanf("%x",&data); outb(data,port); printf("\tWritten 0x%x to address 0x%x",data,port); } } } /* Reads a BYTE from the specified port */ void read_port(void) { int port,data; clear_screen(); printf("\n\t\tRead from ports 0x%x - 0x%x",INPUT_PORT,INPUT_PORT+LENGTH); printf("\n\t\tEnter negative address to return to MAIN MENU\n"); printf("\t\tEnter hexadecimal values for address and data.\n"); while(1) { printf("\nEnter port address to READ FROM: "); scanf("%x",&port); if(port <0) return; if(port INPUT_PORT+LENGTH) printf("\tERR: Port address out of range 0x%x - 0x%x",INPUT_PORT,INPUT_PORT+LENGTH); else { data = inb(port); printf("\tByte read from address 0x%x = 0x%x",port,data); } } } int main() { char choice; int count; count = 1; /* Default value for no of times to run the DANCING LIGHT test */ /* * Get access to the I/O ports from KERNEL * * NOTE: 'ioperm' only allows access to limited range of I/O * address 0 - 0x3FF */ if(ioperm(OUTPUT_PORT,LENGTH+1,1)) { perror("ERR"); printf("Unable to get permission to access OUTPUT ADDRESS 0x%x - 0x%x",OUTPUT_PORT,OUTPUT_PORT+LENGTH); printf("\nYou must be 'root' to be able to run this program.\n\n"); exit(1); } if(ioperm(INPUT_PORT,LENGTH+1,1)) { perror("ERR"); printf("Unable to get permission to access INPUT ADDRESS 0x%x - 0x%x",INPUT_PORT,INPUT_PORT+LENGTH); printf("\nYou must be 'root' to be able to run this program.\n\n"); exit(1); } do { clear_screen(); printf("\nProgram for Input/Output operations using ISA Interface"); printf("\n-------------------------------------------------------"); printf("\nMain Menu"); printf("\n\n\t1. Write to a port"); printf("\n\t2. Read from a port"); printf("\n\t3. Run the output test"); printf("\n\t9. Exit"); printf("\n\nEnter Choice (0 - 9): "); fflush(stdin); choice = getchar(); switch(choice) { case '1': write_port(); break; case '2': read_port(); break; case '3': printf("\nNo of times to run the test ? "); scanf("%d",&count); test_output(count); break; case '9': printf("\nBye!\n"); break; default: break; } } while(choice != '9'); /* Release the permissions for the ports */ ioperm(OUTPUT_PORT,LENGTH,0); ioperm(INPUT_PORT,LENGTH,0); return 0; }