This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Direct I/O reads/writes from C programs


Dave wrote:
> 
> How does one directly access an I/O address from C?

On the x86, this is usually done with a library function or macro
supplied with the compiler.

> For example if I want to write a char to port 0x80 (or read a char from a
> port) what is the statement?

In Borland C:

outportb (0x80, c);

outputs the variable 'c' to port 0x80.

c = inportb (0x80);

gets a byte from port 0x80 and stores it in c.

As far as I know there is no similiar function supplied with Gnu C, but
it would be easy to write one using inline assembler statements.

> How about memory mapped I/O? If one wants to directly access a specific
> memory address what are the statements?

There was a discussion on this some time ago, different people do this
in different ways. I prefer to do it like this:

#define SYPCR (* (volatile char *) 0xfffa21)

This casts an absolute address into a pointer to the memory-mapped
register. Then you can use statements like

SYPCR = 0x20; /* write 0x20 to the register */
if (SYPCR & 0x10) { ... } /* read the register and test one bit */

to perform memory-mapped I/O;

In Gnu C, the compiler optimizes these accesses to a single instruction
on the 68000 family, I'm sure it can also do that on other
architectures.

> Is direct I/O and direct memory access part of ANSI C?
> 

x86 I/O port access is not part of the language, but direct memory
access can be done as described above.

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

Gnu C can cross-compile to 386 and higher, using the COFF file format. 
If you're using 8086, 186, or 286 then you'll have to use a commercial
compiler such as Borland C, because Gnu C can't support the 16-bit x86
architecture.
_______________________________________________
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.