Connecting Tech Pros Worldwide Forums | Help | Site Map

In and Out on port space within C - any standard?

James Harris
Guest
 
Posts: n/a
#1: Nov 14 '05
I'm wanting to write in C some code in which it is necessary to address a device as I/O
space rather than as part of memory. I would like the code to be as standard - and hence
as portable - as possible. It's intended to be later ported to other hardware.

Options?

1) Set up some inline assembler in a header file. I believe I'd need to use the asm
keyword but that this isn't ansi C. Is that correct?
2) Write a separate routine in assembler (to do the actual in and out) and link it with
the C code.
3) Use the appropriate C calls - perhaps they already exist....?

I've seen asm overridden with __asm__ but how does that work? It may stop the compiler
complaining but doesn't it result in the same thing in the end?

Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned
nothing.
--
Thanks,
James



Mike Wahler
Guest
 
Posts: n/a
#2: Nov 14 '05

re: In and Out on port space within C - any standard?


"James Harris" <no.email.please> wrote in message
news:4042764f$0$25003$fa0fcedb@lovejoy.zen.co.uk.. .[color=blue]
> I'm wanting to write in C some code in which it is necessary to address a[/color]
device as I/O[color=blue]
> space rather than as part of memory.[/color]

Standard C doesn't support any direct device access at all, 'i/o space',
'memory', or otherwise.
[color=blue]
>I would like the code to be as standard - and hence
> as portable - as possible. It's intended to be later ported to other[/color]
hardware.[color=blue]
>
> Options?[/color]

No portable ones.
[color=blue]
>
> 1) Set up some inline assembler in a header file. I believe I'd need to[/color]
use the asm[color=blue]
> keyword but that this isn't ansi C. Is that correct?[/color]

The keyword is, but not the assembler statements themselves.
[color=blue]
> 2) Write a separate routine in assembler (to do the actual in and out) and[/color]
link it with[color=blue]
> the C code.
> 3) Use the appropriate C calls - perhaps they already exist....?[/color]

Not standard ones. Standard C does not define any interfaces
with other languages.
[color=blue]
>
> I've seen asm overridden with __asm__ but how does that work?[/color]

However your implementor designed it. Consult your documentation.
[color=blue]
>It may stop the compiler
> complaining but doesn't it result in the same thing in the end?
>
> Help much appreciated. I have checked the FAQ but searches on "asm" and[/color]
"assem" returned[color=blue]
> nothing.[/color]

We can't help you here, because your project is outside the domain of
the standard language. Try checking support resources for your
particular implementation.

-Mike


James Harris
Guest
 
Posts: n/a
#3: Nov 14 '05

re: In and Out on port space within C - any standard?



"James Harris" <no.email.please> wrote in message
news:4042764f$0$25003$fa0fcedb@lovejoy.zen.co.uk.. .[color=blue]
> I'm wanting to write in C some code in which it is necessary to address a device as I/O
> space rather than as part of memory. I would like the code to be as standard - and hence
> as portable - as possible. It's intended to be later ported to other hardware.
>
> Options?
>
> 1) Set up some inline assembler in a header file. I believe I'd need to use the asm
> keyword but that this isn't ansi C. Is that correct?
> 2) Write a separate routine in assembler (to do the actual in and out) and link it with
> the C code.
> 3) Use the appropriate C calls - perhaps they already exist....?
>
> I've seen asm overridden with __asm__ but how does that work? It may stop the compiler
> complaining but doesn't it result in the same thing in the end?
>
> Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned
> nothing.[/color]

I meant to add that I'd wondered if I should use a Pragma for this but all the FAQ says
about them is they are well defined escape-hatch. What does /that/ mean? - confused....


Leor Zolman
Guest
 
Posts: n/a
#4: Nov 14 '05

re: In and Out on port space within C - any standard?


On Sun, 29 Feb 2004 23:30:21 -0000, "James Harris" <no.email.please> wrote:
[color=blue]
>I'm wanting to write in C some code in which it is necessary to address a device as I/O
>space rather than as part of memory. I would like the code to be as standard - and hence
>as portable - as possible. It's intended to be later ported to other hardware.
>
>Options?
>
>1) Set up some inline assembler in a header file. I believe I'd need to use the asm
>keyword but that this isn't ansi C. Is that correct?[/color]

Correct, nor even ISO C. Raspberries for the asm-in-header-file approach.
[color=blue]
>2) Write a separate routine in assembler (to do the actual in and out) and link it with
>the C code.[/color]

That one gets my vote. Localize /all/ the platform-specific code into its
own dedicated module, and then use, say, conditional compilation to select
the platform to compile for.
[color=blue]
>3) Use the appropriate C calls - perhaps they already exist....?[/color]

No such Standard calls exist, no.
[color=blue]
>
>I've seen asm overridden with __asm__ but how does that work? It may stop the compiler
>complaining but doesn't it result in the same thing in the end?[/color]

I don't know, but all that asm stuff should be in the platform-specific
module, however it works.
[color=blue]
>
>Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned
>nothing.[/color]

Good luck,
-leor



Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Malcolm
Guest
 
Posts: n/a
#5: Nov 14 '05

re: In and Out on port space within C - any standard?



"James Harris" <no.email.please> wrote in message[color=blue]
> I'm wanting to write in C some code in which it is necessary to
> address a device as I/O space rather than as part of memory.
>[/color]
There's no way of doing this portably. What you want to do is separate out
all the hardware interface code into a separate file, and then make the
functions C-callable.
How you actually write the hardware layer depends on your platform. Probably
you will have some version of the asm keyword that allows inline assembly
(and does a lot of book-keeping for you). However you will have to rewrite
the code to port to another platform, so keep it separated out and as short
as possible.


Jack Klein
Guest
 
Posts: n/a
#6: Nov 14 '05

re: In and Out on port space within C - any standard?


On Sun, 29 Feb 2004 23:45:52 GMT, "Mike Wahler"
<mkwahler@mkwahler.net> wrote in comp.lang.c:
[color=blue]
> "James Harris" <no.email.please> wrote in message
> news:4042764f$0$25003$fa0fcedb@lovejoy.zen.co.uk.. .[color=green]
> > I'm wanting to write in C some code in which it is necessary to address a[/color]
> device as I/O[color=green]
> > space rather than as part of memory.[/color]
>
> Standard C doesn't support any direct device access at all, 'i/o space',
> 'memory', or otherwise.[/color]

[snip]
[color=blue][color=green]
> > 1) Set up some inline assembler in a header file. I believe I'd need to[/color]
> use the asm[color=green]
> > keyword but that this isn't ansi C. Is that correct?[/color]
>
> The keyword is, but not the assembler statements themselves.[/color]

I'm afraid you are thinking C++ here, not C. There is no "asm"
keyword in C, it has never been part of any standard, not even the
original 1989 ANSI one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mike Wahler
Guest
 
Posts: n/a
#7: Nov 14 '05

re: In and Out on port space within C - any standard?



"Jack Klein" <jackklein@spamcop.net> wrote in message
news:e8b540ds3mhv45glfdo261od26n772u5gi@4ax.com...[color=blue]
> On Sun, 29 Feb 2004 23:45:52 GMT, "Mike Wahler"
> <mkwahler@mkwahler.net> wrote in comp.lang.c:[/color]
[color=blue][color=green]
> > The keyword is, but not the assembler statements themselves.[/color]
>
> I'm afraid you are thinking C++ here, not C. There is no "asm"
> keyword in C, it has never been part of any standard, not even the
> original 1989 ANSI one.[/color]

Yes, oops. Thanks for the correction.

-Mike


Pierre Maurette
Guest
 
Posts: n/a
#8: Nov 14 '05

re: In and Out on port space within C - any standard?


"James Harris" <no.email.please> a écrit ...[color=blue]
> I'm wanting to write in C some code in which it is necessary to address a[/color]
device as I/O[color=blue]
> space rather than as part of memory. I would like the code to be as[/color]
standard - and hence[color=blue]
> as portable - as possible. It's intended to be later ported to other[/color]
hardware.[color=blue]
>
> Options?
>
> 1) Set up some inline assembler in a header file. I believe I'd need to[/color]
use the asm[color=blue]
> keyword but that this isn't ansi C. Is that correct?[/color]

In C99 standard:

J.5.10 The asm keyword
1 The asm keyword may be used to insert assembly language directly into the
translator output (6.8). The most common implementation is via a statement
of the form:
asm ( character-string-literal );

Maybe you can try anything like writing conditional blocks in a header, one
per hardware.

typedef unsigned char u8;

For Intel with short port address (0 to 255), 8 bits wide ports:

const u8 PORT_IN = 0xF0;
const u8 PORT_OUT = 0xF4;

inline void getPort(u8 MemVar)
{
asm
{
push ax
in ax, PORT_IN
mov byte[MemVar], ax
pop ax
}
}

inline void setPort(u8 MemVar)
{
asm
{
push ax
mov ax, byte[MemVar]
out PORT_OUT, ax
pop ax
}
}

For another hardware with memory-mapped I/O:

const volatile u8* const PORT_IN = (volatile u8*) 0x00000412;
u8* const PORT_OUT = (volatile u8*) 0x0000041A;

inline void getPort(u8 MemVar){MemVar = PORT_IN;}

inline void setPort(u8 MemVar){PORT-_OUT = MemeVar;}

[color=blue]
> 2) Write a separate routine in assembler (to do the actual in and out) and[/color]
link it with[color=blue]
> the C code.[/color]
Slow, no ? And no more portable.

Pierre






Richard Bos
Guest
 
Posts: n/a
#9: Nov 14 '05

re: In and Out on port space within C - any standard?


"Pierre Maurette" <mmaauurreettttttee.ppiieerrrree@@ffrreeee.ffrr>
wrote:
[color=blue]
> "James Harris" <no.email.please> a écrit ...[color=green]
> > 1) Set up some inline assembler in a header file. I believe I'd need to
> > use the asm keyword but that this isn't ansi C. Is that correct?[/color]
>
> In C99 standard:
>
> J.5.10 The asm keyword[/color]

Note, first, that the name of that section is "Common Extensions", and
second, that most appendices, J included, aren't normative. IOW, this is
a description of what compiler writers have added to their compilers
besides C proper, it is not ISO C.
This means, for example, that when invoked in ISO mode, _no_ compiler is
allowed to complain about asm being used as a variable name, no matter
whether it provides an assembly extension or not.
[color=blue]
> 1 The asm keyword may be used to insert assembly language directly into the
> translator output (6.8). The most common implementation is via a statement
> of the form:[/color]

Note: most common. _Not_ required, or even allowed in pure ISO mode.

Richard
CBFalconer
Guest
 
Posts: n/a
#10: Nov 14 '05

re: In and Out on port space within C - any standard?


Pierre Maurette wrote:[color=blue]
> "James Harris" <no.email.please> a écrit ...
>[color=green]
> > I'm wanting to write in C some code in which it is necessary to
> > address a device as I/O space rather than as part of memory. I
> > would like the code to be as standard - and hence as portable
> > as possible. It's intended to be later ported to other hardware.
> >
> > Options?
> >
> > 1) Set up some inline assembler in a header file. I believe I'd
> > need to use the asm keyword but that this isn't ansi C. Is that
> > correct?[/color]
>
> In C99 standard:
>
> J.5.10 The asm keyword
> 1 The asm keyword may be used to insert assembly language directly
> into the translator output (6.8). The most common implementation
> is via a statement of the form:
>
> asm ( character-string-literal );[/color]

You omitted the precursor to that section, which is:

J.5 Common extensions

[#1] The following extensions are widely used in many
systems, but are not portable to all implementations. The
inclusion of any extension that may cause a strictly
conforming program to become invalid renders an
implementation nonconforming. Examples of such extensions
are new keywords, extra library functions declared in
standard headers, or predefined macros with names that do
not begin with an underscore.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Closed Thread