title: Fixed address variable in C
body: <p>For embedded applications, it is often necessary to access fixed memory locations for peripheral registers.  The standard way I have found to do this is something like the following:</p>
  
  <pre><code>// access register 'foo_reg', which is located at address 0x100
  #define foo_reg *(int *)0x100
  
  foo_reg = 1;      // write to foo_reg
  int x = foo_reg;  // read from foo_reg
  </code></pre>
  
  <p>I understand how that works, but what I don't understand is how the space for foo_reg is allocated (i.e. what keeps the linker from putting another variable at 0x100?).  Can the space be reserved at the C level, or does there have to be a linker option that specifies that nothing should be located at 0x100.  I'm using the GNU tools (gcc, ld, etc.), so am mostly interested in the specifics of that toolset at the moment.</p>
  
  <p>Some additional information about my architecture to clarify the question:</p>
  
  <p>My processor interfaces to an FPGA via a set of registers mapped into the regular data space (where variables live) of the processor. So I need to point to those registers and block off the associated address space.  In the past, I have used a compiler that had an extension for locating variables from C code.  I would group the registers into a struct, then place the struct at the appropriate location:</p>
  
  <pre><code>typedef struct
  { 
     BYTE reg1;
     BYTE reg2;
     ...
  } Registers;
  
  Registers regs _at_ 0x100;
  
  regs.reg1 = 0;
  </code></pre>
  
  <p>Actually creating a 'Registers' struct reserves the space in the compiler/linker's eyes.</p>
  
  <p>Now, using the GNU tools, I obviously don't have the <em>at</em> extension.  Using the pointer method:</p>
  
  <pre><code>#define reg1 *(BYTE*)0x100;
  #define reg2 *(BYTE*)0x101;
  reg1 = 0
  
  // or
  #define regs *(Registers*)0x100
  regs-&gt;reg1 = 0;
  </code></pre>
  
  <p>This is a simple application with no OS and no advanced memory management.  Essentially:</p>
  
  <pre><code>void main()
  {
      while(1){
          do_stuff();
      }
  }
  </code></pre>