Question

I usually have a decent understanding of pointers when it comes to referencing another variable in a program but what about referencing/writing to registers. Are these two code versions going to perform the same?

#define REGISTER 0x0001 
volatile unsigned int* baseAddress = (unsigned int*) 0x60000000;
baseAddress[REGISTER] = 0x10101010;


volatile unsigned int* realRegister = (unsigned int*) 0x60000004;
realRegister = 0x10101010;

No correct solution

OTHER TIPS

You cannot have a pointer or reference to a register. Registers do not form part of the computer’s main memory, and are not addressable using memory addresses.

Your code pieces, despite using a #define called REGISTER, are completely unrelated to registers. They attempt to write to normal, addressable memory. On some operating system architectures, specific memory addresses may be mapped directly to hardware ports (including, on some devices, registers). Most modern operating systems, however, don’t support this. They use the concept of virtual memory instead, and pointers do not map directly to hardware locations.

Concerning the code you’ve posted, the two pieces are not identical. The second won’t compile because you are trying to assign an integer to a pointer. You can make it compile by dereferencing the pointer before the assignment (*realRegister = 0x10101010;) but the two pieces of code are still not identical:

The first piece of code writes its value at the address

reinterpret_cast<char*>(0x60000000) + sizeof(unsigned int)

The second piece of code, by contrast, writes its value at the address

reinterpret_cast<char*>(0x60000000) + 4

– The two pieces of code are only identical on machines where sizeof(unsigned int) == 4.

For your question, the term register has two conceptual meanings: a location inside the processor to hold values and a hardware addressable device's place to hold a value.

Let's skip the part about processor registers since those don't have addresses that the outside world can access. (Some processors do.)

Let's use for example a UART which is a device that converts parallel data to serial data, often used for output data from an embedded system.

Considering only the transmit section, there is a status register and a data register. The status register has bits that say when it is finished transmitting the character. The data register holds the character to be transmitted.

Let's assume these are 32-bit wide sequential registers and they start at the base address of 0x1000.

You can set up stencil as:

uint32_t * UART_BASE = (uint32-t *) 0x1000;

The status register can be referenced as UART_BASE[0] and the data register as UART_BASE[1].

There are other ways to represent the hardware device, such as using a structure and each register as a field within the structure; or each register has a separate pointer.

That should be (*realRegister) = 0x10101010;, but otherwise, yes, those appear to be the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top