Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.
12453 Discussions

Nios - II memory alignment issue while accessing integer from non word align location

Altera_Forum
Honored Contributor II
1,328 Views

- Using Nios-II with eclipse. While accessing integer(32 bit) form non word align location getting incorrect value as described in below example.  

- Tested with eclipse version 13 and 16, both are giving same result.  

 

- Test case :  

-------------------------------------------------------------------------------- 

char c[10] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 

printf("int = 0x%08x", *(int*)(&c[2])); 

-------------------------------------------------------------------------------- 

above code should print "int = 0x66554433" but getting "int = 0x44332211"
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
426 Views

This is not due to Eclipse, it is a limitation of the Nios II architecture that does not support unaligned accesses. 

But anyway, please note that using an unaligned pointer accesses is "undefined behaviour" in the C standard, so your code could return anything and still be 100% correct according to the C standard. This kind of construct is not portable because it can work on some architectures and not on others. It could even work on some compilers and not others.
Altera_Forum
Honored Contributor II
426 Views

Daixiwen,  

 

Thanks for you feedback. But we are porting LUFA USB host/device stack on nios-II. In it there is requirement of unaligned memory access from stack itself. Also we have checked many compilers for different platforms like NXP LPC microcontrollers, AVR (from Atmel) microcontrollers, windows, linux etc. But with all we are getting expected result.  

 

Thanks, 

Vasu.
Altera_Forum
Honored Contributor II
426 Views

I'm afraid you will have to go through the LUFA code and change all the places where unaligned pointers are used. To help you, you can use the -Wcast-align option in gcc and it will generate a warning each time it thinks an unaligned transfer can happen. 

IIRC unaligned transfers trigger a bus error on some ARM architectures also so it's really not good practise when making portable code. 

 

GCC for ARM has a -mno-unaligned-access option that will automatically convert unaligned accesses into multiple byte accesses, but unfortunately it doesn't seem that this option is available for the Nios II architecture.
Ahmed_H_Intel1
Employee
426 Views

Hi Vasu,

Are you using "little Indian" or "big Indian "? please send a sample of the wrong and expected results to investigate.

Reply