C is a common language for embedded systems, because of its broad compiler support and being lightweight on memory.
The volatile
specifier tells the compiler not to optimise anything associated with the variable. There are a few important use cases of this, including when we interface with hardware that changes the value itself (as opposed to within the program).
In Nios, using
volatile
tells the compiler to useldwio
andstwio
instructions instead ofldw
andstw
.
Certain types will map to particular bit lengths. From here, we can effectively interface with any embedded device as flexibly as needed.
int
corresponds to a 32-bit number, so any 32-bit system can store addresses inint
.short int
corresponds to a 16-bit number, i.e., a half-word in a 32-bit system.long int
corresponds to a 64-bit number, i.e., a full word in a 64-bit system.char
corresponds to an 8-bit number, i.e., a byte in a 32-bit system.