by jhack · 8y ago · 0 comments
*(volatile std::uint32_t*)reg_name = val;
And the author's solution is to replace that one line with this: struct DeviceSetup {
enum class TableType : std::uint32_t {
inphase = 0,
quadrature,
table
};
std::uint32_t input_source : 8;
TableType table_type : 4;
std::uint32_t reserved : 20;
};
volatile auto device_registers_ptr = reinterpret_cast<DeviceSetup*>(DeviceControlAddress);
I'm not really seeing the readability and maintainability advantages here.For the second case, most of it goes away. You still need to know exactly how the value you set is interpreted, but you get a readable description of each part and you don't have to think of bit shifting every time they're set.
The declaration is longer of course. But what would you prefer to see at usage time:
MREG42 = MREG42 & table_type_mask | inphase_val
Or: device_setup_registers->table_type = TableType.inphaseWhich does not do what the author wants it to do.
volatile auto device_registers_ptr = reinterpret_cast<DeviceSetup*>(DeviceControlAddress);
'decltype(device_registers_ptr)' yields 'DeviceSetup* volatile', when clearly 'DeviceSetup volatile*' was intended. Insert some witty rant about programmers being attracted to complexity here.The first possible bug is aliasing. The type of reg_name and DeviceControlAddress are not given, so it is possible there isn't a bug.
The second bug relates to memory ordering. Adding "volatile" will tell the compiler to do things in order, but the compiler will not pass that requirement on to the CPU. The CPU itself may reorder things. I saw this affect an embedded system back in 1998, and the problem has only become more common in the 2 decades since. Generally you will need assembly code to avoid the bug.
int64_t val = (*reg0 << 32) | *reg1;
The compiler is allowed to re-order the register reads even if they are volatile pointers. To guarantee a specific order you need something like: int64_t val = reg0 << 32;
val |= reg1;
Sequence points are one of those dark corners of C/C++ which most people don't know about because they rarely matter unless you are dealing directly with hardware.You can still hit the problem. In my case, the code ran fine until we got an upgraded CPU. (from MPC6xx series to MPC74xx series)
Suppose you store to registers at 0xf0000ffc, then 0xf0000104, then 0xf0000ff8. You need the stores to happen in that order. The instructions execute in that order, creating 32-bit chunks of data headed out toward the memory bus. There are multiple write buffers however, so they can go in parallel, each getting a distinct write buffer. They then head out onto the memory bus in some randomish order determined by timing issues internal to the CPU.
In my case, I had to add "eieio" instructions between each pair of accesses for which ordering mattered. FYI, that is a real instruction, supposedly meaning "Enforce In-Order Execution of I/O".
The ARM architecture does this more sanely. Page table entries have a flag that lets you choose between regular "Device" memory or "Strongly-ordered" memory; the latter performs all memory accesses in order without needing any synchronization instructions, and is more convenient in simple situations.
[1] https://www.nxp.com/docs/en/reference-manual/MPC7410UM.pdf - Table 3-8
The proposed alternative of using a struct appears more confusing at first, but it's really helpful when referring back to the register layouts. TI makes pretty good usage of the method for the co-processors in the BeagleBones [1]. Generally I think it's nicer to have a `settings->control_bit = 1` than `*(base_prt + control_bit_offset) = 1`. Though the whole `reinterpret_cast` and volatile stuff in C++ confuses me everytime. C is _much_ easier but less type safe when dealing with volatile.
1: http://processors.wiki.ti.com/index.php/PRU-ICSS_Header_File...
> Generally you will need assembly code to avoid the bug.
What do you mean by this? Compiler outputs binary code the same way assembler does. If the CPU reorders your instructions, it will happen in both cases.
If there are some specific instructions which create memory/reordering barriers, they can be expressed in higher level languages as well.
__asm__ __volatile__("eieio":::"memory");
Normally the CPU will freely reorder loads and stores that go to different addresses. It is not necessarily reordering instructions; the reordering happens on the way to the memory bus.
That special instruction helps. It will ensure that previous accesses to IO memory will be done before any that follow. It also has a similar effect in RAM, but sadly not between RAM and IO memory. For that, which might be needed for a DMA engine, you'll also need the "sync" instruction.
As of 2011, you can. In C++ it’s with std::atomic. http://en.cppreference.com/w/cpp/atomic/atomic C11 offers comparable tools, I believe.
PowerPC does treat it like a memory barrier instruction (eieio - Enforce Inorder Execution of I/O), but that's a stronger guarantee than you'd want for all volatile accesses.