how to maps an structure to a physical address such as 0x10000

H

henry

List,

Does any know how to write C code to map an structure to a physical
address such as 0x10000?

Thanks.

---henry
 
G

Guest

Does any know how to write C code to map an structure to a physical
address such as 0x10000?

Standard C doesn't actually allow this, or rather the only ways to
attempt it are technically Undefined Behaviour according to the C
Standard

struct Port
{
unsigned char control;
unsigned char data;
}

Port *my_channel = (struct Port*)0x10000;

my_channel->data = oxEE;


this "casts" the address into a pointer to a Port structure.
You might look up the "volatile" keyword as well
 
B

Bill Cunningham

henry said:
List,

Does any know how to write C code to map an structure to a physical
address such as 0x10000?

Thanks.

I think that's beyond STD C your going to need OS access for that.

Bill
 
H

henry

Well, it's extremely system-specific of course, but using pointers with
something like this might suffice:

     struct foo *MappedPointer = (struct foo *)0x10000;

You now have a pointer to that struct which points to the address in question.

Actually, it assigns 0x1000 to MappedPointer. Why it calls
MappedPointer maps to 0x1000?

---henry
 
M

maciek borzecki

Actually, it assigns 0x1000 to MappedPointer. Why it calls
MappedPointer maps to 0x1000?

---henry
perfectly acceptable and commonly used when writing dirvers, ie. it's
sometimes/commonly the case that some HW might have memory mapped
registers. Then the easiest way to access the registers is to map them
to a structure, and fix the pointer at the memory address where the
registers are mapped to.

maciek
 
J

Jens Thoms Toerring

Actually, it assigns 0x1000 to MappedPointer. Why it calls
MappedPointer maps to 0x1000?

There's no "call", there is only an assignment. You set the
value stored in the pointer "MappedPointer" to 0x10000 and
then you use this pointer to indirectly access the memory
at this place (assuming that your OS allows you to do so).
So it's not exactly "mapping a structure" (you can't map a
structure, a new structure gets created when you define one
and the compiler decides where in memory it will appear)
but it does the job. It's like doing

struct foo my_struct;
struc foo *MappedPointer = &my_struct;

so that you can use

MappedPointer->some_member = 3;

instead of

my_struct.some_member = 3;
Regards, Jens
 
N

Nate Eldredge

maciek borzecki said:
perfectly acceptable and commonly used when writing dirvers, ie. it's
sometimes/commonly the case that some HW might have memory mapped
registers. Then the easiest way to access the registers is to map them
to a structure, and fix the pointer at the memory address where the
registers are mapped to.

If you don't want to think of it as a pointer, you can also do this:

#define MappedStructure (*(struct foo *)0x10000)

void wibble_the_frob(void) {
MappedStructure.frob = wibble;
}
 
J

James Kuyper

Standard C doesn't actually allow this, or rather the only ways to
attempt it are technically Undefined Behaviour according to the C
Standard

struct Port
{
unsigned char control;
unsigned char data;
}

Port *my_channel = (struct Port*)0x10000;

my_channel->data = oxEE;


this "casts" the address into a pointer to a Port structure.
You might look up the "volatile" keyword as well

Technically, that conversion doesn't directly have undefined behavior.
What the standard says about it is: "An integer may be converted to any
pointer type. Except as previously specified, the result is
implementation-defined, might not be correctly aligned, might not point
to an entity of the referenced type, and might be a trap
representation." 6.3.2.3p5. However, if any of the mentioned
possibilities actually occur, it's virtually certain that there's
undefined behavior somewhere else in the program, probably during the
initialization of my_channel.

In practice, you can reasonably expect that on any implementation where
the specific address you're using is documented as having a special
fixed meaning, the "undefined behavior" will take the form of your code
actually working as intended. However, the code won't be portable to any
implementation where it is not so documented.
 
H

henry

Nate said:
If you don't want to think of it as a pointer, you can also do this:

#define MappedStructure (*(struct foo *)0x10000)

void wibble_the_frob(void) {
MappedStructure.frob = wibble;
}

May I do following?

MappedStructure = (*(struct foo *)0x10000)
 
N

Nate Eldredge

henry said:
May I do following?

MappedStructure = (*(struct foo *)0x10000)

That will read the `struct foo' at 0x10000 and copy it into
MappedStructure, but further assignments to MappedStructure will only
modify the copy, not the data at 0x10000. So that isn't what you want.
 
B

Ben Bacarisse

henry said:
Nate Eldredge wrote:

May I do following?

MappedStructure = (*(struct foo *)0x10000)

Yes, but then I'd call it "CopiedStructure" since this assignment
copies the whole of the struct foo at addrees 0x10000 into another
object called MappedStructure. This name will confuse people.

Changes you make will need to be copied back if the memory at that
location need to see them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top