Direct I/O reads/writes from C programs

Richard Meredith rmeredith@cix.co.uk
Wed Mar 17 11:54:00 GMT 1999


> How does one directly access an I/O address from C?
> 
> For example if I want to write a char to port 0x80 (or read a char from 
> a
> port) what is the statement? 
> 
> How about memory mapped I/O? If one wants to directly access a specific
> memory address what are the statements? 
> 
> Is direct I/O and direct memory access part of ANSI C?

No. In protected operating systems, both are impossible: the operating 
system prevents you from doing it without going through the OS. In 
addition, such code is highly machine-dependent: many, if not most, 
processor architectures don't have separate I/O and memory space at the 
hardware level. The x86 is one of the few that does.

> I have an embedded X86 project that has to directly access the 
> hardware. I
> would prefer doing it in C rather than ASM.

Provided memory paging is not in use you can (usually) get absolute memory 
accesses by casting an address to a pointer:

char *logical_address = (char *) 0x1000;

and then read/write the physical addresses using the pointer. The accesses 
occur in the memory space of the program, so if paging is in use by the OS 
physical and logical addresses may differ.

For I/O space you need a short assembly-language wrapper for the in and 
out instructions: X86 compilers often have functions like

BYTE inp ( int port );
WORD inpw ( int port );
DWORD inpl ( int port );
void outp ( int port, BYTE data );
void outw ( int port, WORD data );
void outl ( int port, DWORD data );

to do port I/O/

 
_______________________________________________
New CrossGCC FAQ: http://www.objsw.com/CrossGCC
_______________________________________________
To remove yourself from the crossgcc list, send
mail to crossgcc-request@cygnus.com with the
text 'unsubscribe' (without the quotes) in the
body of the message.



More information about the crossgcc mailing list