0

How do i find out what memory addresses are suitable for use ?

More specifically, the example of How to use a specific address is here: Pointer to a specific fixed address, but not information on Why this is a valid address for reading/writing.

I would like a way of finding out that addresses x to y are useable.

This is so i can do something similar to memory mapped IO without a specific simulator. (My linked Question relevant so i can use one set of addresses for testing on Ubuntu, and another for the actual software-on-chip)

Ubuntu specific answers please.

Community
  • 1
  • 1
NWS
  • 3,080
  • 1
  • 19
  • 34

3 Answers3

4

You can use whatever memory address malloc() returns. Moreover, you can specify how much memory you need. And with realloc() you even can change your mind afterwards.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
3

You're mixing two independent topics here. The Question that you're linking to, is regarding a micro controller's memory mapped IO. It's referring to the ATM128, a Microcontroller from the Atmel. The OP of that question is trying to write to one of the registers of it, these registers are given specific addresses.

If you're trying to write to the address of a register, you need to understand how memory mapped IO works, you need to read the spec for the chipset/IC your working on. Asking this talking about "Ubuntu specific answers" is meaningless.

Your program running on the Ubuntu OS is running it it's own virtual address space. So asking if addresses x to y are available for use is pretty pointless... unless you're accessing hardware, there's no point in looking for a specific address, just use what the OS gives you and you'll know you're good.


Based on your edit, the fact that you're trying to do a simulation of memory mapped IO, you could do something like:

#ifdef SIMULATION
    unsigned int TX_BUF_REG;  // The "simulated" 32-bit register
#else
#define TX_BUF_REG 0x123456   // The actual address of the reg you're simulating
#endif

Then use accessor macro's to read or write specific bits via a mask (as is typically done):

#define WRITE_REG_BITS(reg, bits) {reg |= bits;}
...
WRITE_REG_BITS(TX_BUF_REG, SOME_MASK);

Static variables can be used in simulations this way so you don't have to worry about what addresses are "safe" to write to.

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177
  • @NWS - OK... maybe more details would be helpful. If you're trying to do memory mapped IO on your desktop, then what IO are you trying to map? What architecture do you use? (x86?) Or are you saying you're trying to *simulate* another HW platform and would like to simulate the memory mapped IO that that HW platform does? – Mike Feb 04 '13 at 13:23
  • Mike, The only thing actually in need of simulating is the set of addresses, the HW does not do anything particularly special, so really i just need to be able to say, "use these addresses for simulation, and use these addresses for the real thing". I suspect @mouviciel has come up with the simplest option. – NWS Feb 04 '13 at 13:39
  • @NWS - Yes, you can use `#define`'s to compile your program for "simulation" mode or "normal". When it's in simulation you can use local static variables (an `int` works well to simulate a 32-bit register), you can use dynamic memory as mouviciel suggested, but there's really no point since the IOs in memory mapped IO are static (tied to hardware) and thus you know the size and quantity before coding. – Mike Feb 04 '13 at 14:03
  • @NWS - very simple example of what I was saying in the edit, I think that's pretty much what you're going for. – Mike Feb 04 '13 at 14:11
1

For the referenced ATMega128 microcontroller you look in the Datasheet to see which addresses are mapped to registers. On a PC with OS installed you won't have a chance to access hardware registers directly this way. At least not from userspace. Normally only device drivers (ring 0) are allowed to access hardware.

As already mentioned by others you have to use e.g. malloc() to tell the OS that you need a pointer to memory chuck that you are allowed to write to. This is because the OS manages the memory for the whole system.

bidifx
  • 1,640
  • 13
  • 19